Commit 58895acb authored by Rafael Monnerat's avatar Rafael Monnerat

slapos_accounting&subscription_request: Include script to calculate deposit balance

  Make it reusable so that we can inspect the balance of the user for other purposes

slapos_accounting: Entity_getDepositBalanceAmount
parent 1e59644a
portal = context.getPortalObject()
assert_price_kw = {
'resource_uid': currency_uid,
'portal_type': portal.getPortalAccountingMovementTypeList(),
'ledger_uid': portal.portal_categories.ledger.automated.getUid(),
}
# entity is the depositor
# mirror_section_uid is the payee/recipient
entity_uid = context.getUid()
# Total received
deposit_amount = portal.portal_simulation.getInventoryAssetPrice(
section_uid=entity_uid,
mirror_section_uid=mirror_section_uid,
mirror_node_uid=portal.restrictedTraverse('account_module/deposit_received').getUid(),
#node_category_strict_membership=['account_type/income'],
simulation_state= ('stopped', 'delivered'),
# Do not gather deposit reimburse
# when it does not yet have a grouping_reference
omit_asset_decrease=1,
grouping_reference=None,
**assert_price_kw
)
# Total reserved
payable_amount = portal.portal_simulation.getInventoryAssetPrice(
mirror_section_uid=entity_uid,
section_uid=mirror_section_uid,
# Do not gather deposit receivable
# when it does not yet have a grouping_reference
omit_asset_decrease=1,
node_category_strict_membership=['account_type/asset/receivable',
'account_type/liability/payable'],
simulation_state= ('planned', 'confirmed', 'started', 'stopped', 'delivered'),
grouping_reference=None,
**assert_price_kw
)
return deposit_amount - payable_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>_bind_names</string> </key>
<value>
<object>
<klass>
<global name="_reconstructor" module="copy_reg"/>
</klass>
<tuple>
<global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
<global name="object" module="__builtin__"/>
<none/>
</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>currency_uid, mirror_section_uid</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Entity_getDepositBalanceAmount</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
...@@ -20,10 +20,6 @@ if item is None: ...@@ -20,10 +20,6 @@ if item is None:
resource = subscription_request.getResourceValue() resource = subscription_request.getResourceValue()
raise ValueError('Unsupported resource: %s' % resource.getRelativeUrl()) raise ValueError('Unsupported resource: %s' % resource.getRelativeUrl())
# Use list setter, to ensure it crashes if item is still None
# subscription_request.setAggregateValueList([item])
# If the virtual master is not in the expected subscription status, # If the virtual master is not in the expected subscription status,
# do not accept any new service (compute node, instance) for it # do not accept any new service (compute node, instance) for it
if (((subscription_request.getSourceProjectValue() is not None) and if (((subscription_request.getSourceProjectValue() is not None) and
...@@ -37,55 +33,19 @@ if (((subscription_request.getSourceProjectValue() is not None) and ...@@ -37,55 +33,19 @@ if (((subscription_request.getSourceProjectValue() is not None) and
total_price = subscription_request.getTotalPrice() total_price = subscription_request.getTotalPrice()
if 0 < total_price: if 0 < total_price:
# Check that user has enough guarantee deposit to request a new service customer = subscription_request.getDestinationSectionValue()
portal = context.getPortalObject() balance = customer.Entity_getDepositBalanceAmount(
assert_price_kw = { subscription_request.getPriceCurrencyUid(),
'resource_uid': subscription_request.getPriceCurrencyUid(), subscription_request.getSourceSectionUid())
'portal_type': portal.getPortalAccountingMovementTypeList(),
'ledger_uid': portal.portal_categories.ledger.automated.getUid(),
}
deposit_amount = portal.portal_simulation.getInventoryAssetPrice(
section_uid= subscription_request.getDestinationSectionUid(),
mirror_section_uid= subscription_request.getSourceSectionUid(),
mirror_node_uid=portal.restrictedTraverse('account_module/deposit_received').getUid(),
#node_category_strict_membership=['account_type/income'],
simulation_state= ('stopped', 'delivered'),
# Do not gather deposit reimburse
# when it does not yet have a grouping_reference
omit_asset_decrease=1,
grouping_reference=None,
#src__=1,
**assert_price_kw
)
#return deposit_amount
payable_amount = portal.portal_simulation.getInventoryAssetPrice(
mirror_section_uid= subscription_request.getDestinationSectionUid(),
section_uid= subscription_request.getSourceSectionUid(),
# Do not gather deposit receivable
# when it does not yet have a grouping_reference
omit_asset_decrease=1,
node_category_strict_membership=['account_type/asset/receivable',
'account_type/liability/payable'],
simulation_state= ('planned', 'confirmed', 'started', 'stopped', 'delivered'),
grouping_reference=None,
**assert_price_kw
)
# XXX what is the guarantee deposit account_type? # XXX what is the guarantee deposit account_type?
if deposit_amount < payable_amount + total_price: if balance < total_price:
# if not enough, user will have to pay a deposit for the subscription
# XXX probably create an event asking for a deposit
#pass
return markHistory(subscription_request, return markHistory(subscription_request,
'Not enough deposit from user') 'Your user does not have enough deposit.')
# raise NotImplementedError('NO deposit_amount %s\npayable_amount %s\ntotal_price %s' % (deposit_amount, payable_amount, total_price))
#return 'YES deposit_amount %s\npayable_amount %s\ntotal_price %s' % (deposit_amount, payable_amount, total_price)
if subscription_request.checkConsistency(): if subscription_request.checkConsistency():
return markHistory(subscription_request, str(subscription_request.checkConsistency()[0].getTranslatedMessage())) return markHistory(subscription_request,
str(subscription_request.checkConsistency()[0].getTranslatedMessage()))
subscription_request.SubscriptionRequest_createOpenSaleOrder() subscription_request.SubscriptionRequest_createOpenSaleOrder()
subscription_request.validate() subscription_request.validate()
......
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