Commit a46144a5 authored by Romain Courteaud's avatar Romain Courteaud

slapos_accounting: set price on the open order

it requires to create a temp sale order, where sale supply is automatically applied
parent c21cc2ea
......@@ -18,9 +18,26 @@ def storeWorkflowComment(document, comment):
portal.portal_workflow.doActionFor(document, 'edit_action', comment=comment)
def newOpenOrder(start_date):
new_open_sale_order = portal.open_sale_order_module.newContent(portal_type="Open Sale Order")
new_open_sale_order.edit(
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
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
open_sale_order = module.newContent(
temp_object=temp_order,
specialise=specialise,
effective_date=DateTime(),
start_date=start_date,
......@@ -34,14 +51,85 @@ def newOpenOrder(start_date):
title="%s SlapOS Subscription" % person.getTitle(),
ledger_value=portal.portal_categories.ledger.automated,
destination_project_value=instance_tree.getFollowUpValue(),
activate_kw=activate_kw
)
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=[
hosting_subscription,
instance_tree
],
activate_kw=activate_kw
)
open_sale_order.SaleOrder_applySaleTradeCondition(batch_mode=1, force=1)
# Check compatibility with previous template
if specialise is not None:
assert open_sale_order.getSpecialise() == specialise
if price is None:
price = open_order_cell.getPrice()
open_order_cell.edit(
price=price
)
new_open_sale_order.order(activate_kw=activate_kw)
new_open_sale_order.validate(activate_kw=activate_kw)
return new_open_sale_order
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_order = None
open_sale_order = None
person = instance_tree.getDestinationSectionValue(portal_type="Person")
# Template document does not have person relation
if person is not None:
......@@ -102,82 +190,28 @@ if instance_tree.getCausalityState() == 'diverged':
# XXX add comment on instance tree
return
open_sale_order = newOpenOrder(start_date)
open_order_explanation = ""
# Add lines
open_order_line = open_sale_order.newContent(portal_type="Open Sale Order Line")
resource_vcl = [
'software_release/%s' % software_release.getRelativeUrl(),
'software_type/%s' % software_type.getRelativeUrl()
]
resource_vcl.sort()
assert len(resource_vcl) == 2, service
edit_kw = {
'resource_value': service,
'variation_category_list': resource_vcl,
'quantity_unit': service.getQuantityUnit(),
'base_contribution_list': service.getBaseContributionList(),
'use': service.getUse()
}
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()
open_order_line.edit(
activate_kw=activate_kw,
# stop_date=calculateOpenOrderLineStopDate(open_sale_order_line,
# instance_tree, start_date_delta=start_date_delta),
**edit_kw
)
base_id = 'path'
# XXX XXX XXX TODO WRONG: it should not get the first random variations
# but match the instance tree parameters instead
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='Open Sale Order Cell',
*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=[
hosting_subscription,
instance_tree
],
# Sale Supply price do not match Open Order
# Create a temp Sale Order to calculate the real price
sale_order, order_line, order_cell = newOpenOrder(
start_date, service,
software_release, software_type,
hosting_subscription, instance_tree,
activate_kw,
temp_order=True
)
open_order = open_order_line.getParentValue()
open_order.SaleOrder_applySaleTradeCondition(batch_mode=1, force=1)
# Check compatibility with previous template
if specialise is not None:
assert open_order.getSpecialise() == specialise
open_order_cell.edit(
price=open_order_cell.getPrice()
if order_cell.getPrice() is None:
# No price found, do not create anything for now
# XXX add comment on Instance Tree
return
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()
)
# XXX XXX XXX assert open_order_cell.getPrice() is not None
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 open_order_line is not None:
open_order = open_order_line.getParentValue()
assert open_order_line.getResource().startswith('software_product')
assert open_order_line.getQuantityUnit() == 'time/month', open_order_line.getQuantityUnit()
......@@ -187,8 +221,8 @@ if instance_tree.getCausalityState() == 'diverged':
#assert open_order_line.getPrice() == 1, open_order_line.getPrice()
#assert open_order_line.getQuantity() == 1
open_order.OpenSaleOrder_updatePeriod()
open_sale_order.OpenSaleOrder_updatePeriod()
# Person_storeOpenSaleOrderJournal should fix all divergent Instance Tree in one run
assert instance_tree.getCausalityState() == 'solved'
return open_order
return open_sale_order
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