Commit 61b64044 authored by Rafael Monnerat's avatar Rafael Monnerat

slapos_accounting: Calculate User's "Outstanding" Amounts on SlapOS

    This consider Maximum Delay and Maximum Credit a user can have
parent 17686abf
...@@ -2,7 +2,6 @@ from Products.ZSQLCatalog.SQLCatalog import SimpleQuery, ComplexQuery ...@@ -2,7 +2,6 @@ from Products.ZSQLCatalog.SQLCatalog import SimpleQuery, ComplexQuery
portal = context.getPortalObject() portal = context.getPortalObject()
params = {"node_category_strict_membership": ['account_type/asset/receivable', params = {"node_category_strict_membership": ['account_type/asset/receivable',
'account_type/liability/payable']} 'account_type/liability/payable']}
from DateTime import DateTime
if at_date: if at_date:
params['at_date'] = at_date params['at_date'] = at_date
params['grouping_query'] = ComplexQuery( params['grouping_query'] = ComplexQuery(
...@@ -12,6 +11,9 @@ if at_date: ...@@ -12,6 +11,9 @@ if at_date:
else: else:
params['grouping_reference'] = None params['grouping_reference'] = None
if resource_uid is not None:
params["resource_uid"] = resource_uid
return portal.portal_simulation.getInventoryAssetPrice( return portal.portal_simulation.getInventoryAssetPrice(
mirror_section_uid = context.getUid(), mirror_section_uid = context.getUid(),
simulation_state=('stopped', 'delivered'), simulation_state=('stopped', 'delivered'),
......
...@@ -50,7 +50,7 @@ ...@@ -50,7 +50,7 @@
</item> </item>
<item> <item>
<key> <string>_params</string> </key> <key> <string>_params</string> </key>
<value> <string>at_date=None, **kw</string> </value> <value> <string>at_date=None, resource_uid=None, **kw</string> </value>
</item> </item>
<item> <item>
<key> <string>id</string> </key> <key> <string>id</string> </key>
......
portal = context.getPortalObject()
from DateTime import DateTime
if contract is None:
contract = portal.portal_catalog.getResultValue(
portal_type="Cloud Contract",
default_destination_section_uid=context.getUid(),
validation_state=['invalidated', 'validated'],
)
if contract is None:
return context.Entity_statOutstandingAmount()
# We evaluate Multiple scenarios
amount = context.Entity_statOutstandingAmount()
# All payed so just return
if not amount:
return amount
maximum_invoice_delay = contract.getMaximumInvoiceDelay()
maximum_invoice_credit = 0.0
# For now we only support those 2 currencies
currency_uid_list = [
portal.currency_module.EUR.getUid(),
portal.currency_module.CNY.getUid(),
]
for currency_uid in currency_uid_list:
for line in contract.objectValues(
portal_type="Cloud Contract Line"):
if line.getPriceCurrencyUid() == currency_uid:
maximum_invoice_credit = line.getMaximumInvoiceCredit()
amount_per_currency = context.Entity_statOutstandingAmount(resource_uid=currency_uid)
if amount_per_currency > maximum_invoice_credit:
return amount_per_currency # We exceed maximum amount because
# user already requested too much
if maximum_invoice_delay:
# Recalculate now ignoring the all invoices from lastest days.
at_date = DateTime()-maximum_invoice_delay
return context.Entity_statOutstandingAmount(
at_date=at_date)
return amount
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>Script_magic</string> </key>
<value> <int>3</int> </value>
</item>
<item>
<key> <string>_bind_names</string> </key>
<value>
<object>
<klass>
<global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
</klass>
<tuple/>
<state>
<dictionary>
<item>
<key> <string>_asgns</string> </key>
<value>
<dictionary>
<item>
<key> <string>name_container</string> </key>
<value> <string>container</string> </value>
</item>
<item>
<key> <string>name_context</string> </key>
<value> <string>context</string> </value>
</item>
<item>
<key> <string>name_m_self</string> </key>
<value> <string>script</string> </value>
</item>
<item>
<key> <string>name_subpath</string> </key>
<value> <string>traverse_subpath</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</state>
</object>
</value>
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>contract=None, **kw</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Entity_statSlapOSOutstandingAmount</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment