Commit d634a213 authored by Romain Courteaud's avatar Romain Courteaud

slapos_accounting: drop InstanceTree_requestUpdateOpenSaleOrder

parent 1822277b
from zExceptions import Unauthorized
if REQUEST is not None:
raise Unauthorized
if 1:
raise NotImplementedError('Stop creating Open Sale Order from Instance Tree')
from DateTime import DateTime
portal = context.getPortalObject()
instance_tree = context
tag = '%s_%s' % (instance_tree.getUid(), script.id)
activate_kw = {'tag': tag}
if portal.portal_activities.countMessageWithTag(tag) > 0:
# nothing to do
return
def storeWorkflowComment(document, comment):
portal_workflow = document.portal_workflow
last_workflow_item = portal_workflow.getInfoFor(ob=document,
name='comment', wf_id='edit_workflow')
if last_workflow_item != comment:
portal_workflow.doActionFor(document, action='edit_action', comment=comment)
def newOpenOrder(start_date, service,
software_release, software_type,
hosting_subscription, instance_tree,
activate_kw,
price=None, temp_order=False):
if temp_order:
order_portal_type = 'Sale Order'
line_portal_type = 'Sale Order Line'
cell_portal_type = 'Sale Order Cell'
base_id = 'movement'
module = portal.sale_order_module
aggregate_value_list = []
else:
order_portal_type = 'Open Sale Order'
line_portal_type = 'Open Sale Order Line'
cell_portal_type = 'Open Sale Order Cell'
base_id = 'path'
module = portal.open_sale_order_module
aggregate_value_list = [
hosting_subscription,
instance_tree
]
open_sale_order = module.newContent(
portal_type=order_portal_type,
temp_object=temp_order,
effective_date=DateTime(),
start_date=start_date,
# Ensure stop date value is higher than start date
# it will be updated by OpenSaleOrder_updatePeriod
stop_date=start_date + 1,
activate_kw=activate_kw,
destination_value=person,
destination_section_value=person,
destination_decision_value=person,
title="%s SlapOS Subscription" % person.getTitle(),
ledger_value=portal.portal_categories.ledger.automated,
destination_project_value=instance_tree.getFollowUpValue(),
)
resource_vcl = [
'software_release/%s' % software_release.getRelativeUrl(),
'software_type/%s' % software_type.getRelativeUrl()
]
resource_vcl.sort()
assert len(resource_vcl) == 2, service
# Add lines
open_order_line = open_sale_order.newContent(
portal_type=line_portal_type,
temp_object=temp_order,
resource_value=service,
variation_category_list=resource_vcl,
quantity_unit=service.getQuantityUnit(),
base_contribution_list=service.getBaseContributionList(),
use=service.getUse(),
# stop_date=calculateOpenOrderLineStopDate(open_sale_order_line,
# instance_tree, start_date_delta=start_date_delta),
activate_kw=activate_kw
)
"""
subscription_request = instance_tree.getAggregateRelatedValue(portal_type="Subscription Request")
# Define the start date of the period, this can variates with the time.
# start_date_delta = 0
if subscription_request is not None:
# Quantity is double because the first invoice has to
# charge for 2 months
edit_kw['quantity'] = subscription_request.getQuantity()
"""
cell_key = list(open_order_line.getCellKeyList(base_id=base_id))[0]
open_order_cell = open_order_line.newCell(
base_id=base_id,
portal_type=cell_portal_type,
temp_object=temp_order,
*cell_key
)
open_order_cell.edit(
mapped_value_property_list=['price','quantity'],
quantity=1,
predicate_category_list=cell_key,
variation_category_list=cell_key,
aggregate_value_list=aggregate_value_list,
activate_kw=activate_kw
)
open_sale_order.SaleOrder_applySaleTradeCondition(batch_mode=1, force=1)
if price is None:
price = open_order_cell.getPrice()
open_order_cell.edit(
price=price
)
storeWorkflowComment(open_order_line, "Created for %s" % instance_tree.getRelativeUrl())
# instance_tree.converge(comment="Last open order: %s" % open_sale_order_line.getRelativeUrl())
open_order_explanation = "Added %s." % str(open_order_line.getId())
storeWorkflowComment(open_sale_order, open_order_explanation)
if not temp_order:
open_sale_order.order(activate_kw=activate_kw)
open_sale_order.validate(activate_kw=activate_kw)
return open_sale_order, open_order_line, open_order_cell
if instance_tree.getCausalityState() == 'diverged':
open_sale_order = None
person = instance_tree.getDestinationSectionValue(portal_type="Person")
# Template document does not have person relation
if person is not None:
# Search an existing related open order
open_order_cell = portal.portal_catalog.getResultValue(
portal_type='Open Sale Order Cell',
aggregate__uid=instance_tree.getUid())
is_open_order_creation_needed = False
# Simply check that it has never been simulated
if instance_tree.getSlapState() == 'destroy_requested':
# Line should be deleted
if (open_order_cell is not None) and (open_order_cell.getParentValue().getValidationState() == "invalidated"):
instance_tree.converge(comment="Last open order: %s" % open_order_cell.getRelativeUrl())
elif open_order_cell is None:
# User has no Open Sale Order (likely).
# No need to charge, as it was never allocated
is_open_order_creation_needed = False
instance_tree.converge(comment="No open order needed as it was never allocated")
elif open_order_cell is None:
# Let's add
is_open_order_creation_needed = True
# Let's create the open order
if is_open_order_creation_needed:
# Search for matching resource
service, software_release, software_type = instance_tree.InstanceTree_getSoftwareProduct()
if (service is None) or (service.getUse() != 'trade/sale'):
storeWorkflowComment(instance_tree, "No software product found")
return
#raise NotImplementedError('No Software Product defined for %s' % instance_tree.getRelativeUrl())
currency = instance_tree.getFollowUpValue().Project_getAccountingCurrency()
if currency is None:
storeWorkflowComment(instance_tree, "No currency found")
return
#assert instance_tree.getPortalType() in service.getRequiredAggregatedPortalTypeList()
# Sale Supply price do not match Open Order
# Create a temp Sale Order to calculate the real price
_, _, order_cell = newOpenOrder(
DateTime(), service,
software_release, software_type,
None, instance_tree,
activate_kw,
temp_order=True
)
if order_cell.getPrice() is None:
# No price found, do not create anything for now
storeWorkflowComment(instance_tree, "No price found")
return
hosting_subscription = portal.hosting_subscription_module.newContent(
portal_type="Hosting Subscription",
title=instance_tree.getTitle(),
follow_up_value=instance_tree.getFollowUpValue(),
ledger_value=portal.portal_categories.ledger.automated,
)
hosting_subscription.validate()
start_date = hosting_subscription.HostingSubscription_calculateSubscriptionStartDate()
open_sale_order, open_order_line, open_order_cell = newOpenOrder(
start_date, service,
software_release, software_type,
hosting_subscription, instance_tree,
activate_kw,
price=order_cell.getPrice()
)
if open_order_cell is not None:
open_order_line = open_order_cell.getParentValue()
assert open_order_line.getResource().startswith('software_product')
assert open_order_line.getQuantityUnit() == 'time/month', open_order_line.getQuantityUnit()
assert open_order_line.getBaseContribution() == 'base_amount/invoicing/discounted'
assert open_order_line.getBaseContributionList()[1] == 'base_amount/invoicing/taxable'
assert open_order_line.getUse() == 'trade/sale'
#assert open_order_line.getPrice() == 1, open_order_line.getPrice()
assert open_order_cell.getQuantity() == 1
open_order_line.getParentValue().OpenSaleOrder_updatePeriod()
# Person_storeOpenSaleOrderJournal should fix all divergent Instance Tree in one run
assert instance_tree.getCausalityState() == 'solved'
return open_sale_order
<?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>REQUEST=None</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>InstanceTree_requestUpdateOpenSaleOrder</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