Commit a106e867 authored by Romain Courteaud's avatar Romain Courteaud

XXXXXXXXXXXXXXXXXXXXXXXXXXXX EXPAND

slapos_accounting:

* stop using fixConsistency to set hosting subscription periodicity
  Set it directly from the script which create it

slapos_subscription_request:

* do not set the stop_date on the open order
* generate a discount on the first invoice
  If the open order does not start the same day, do not ask user to pay for those missing days
* no need to call OpenSaleOrder_updatePeriod when creating the open order
parent 261e70fc
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Interaction Workflow Interaction" module="erp5.portal_type"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>categories</string> </key>
<value>
<tuple>
<string>before_script/portal_workflow/slapos_accounting_interaction_workflow/script_Base_fixConsistency</string>
</tuple>
</value>
</item>
<item>
<key> <string>description</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>interaction_HostingSubscription_afterAddClone</string> </value>
</item>
<item>
<key> <string>portal_type</string> </key>
<value> <string>Interaction Workflow Interaction</string> </value>
</item>
<item>
<key> <string>portal_type_filter</string> </key>
<value>
<tuple>
<string>Hosting Subscription</string>
</tuple>
</value>
</item>
<item>
<key> <string>portal_type_group_filter</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>temporary_document_disallowed</string> </key>
<value> <int>1</int> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string>HostingSubscription_afterAddClone</string> </value>
</item>
<item>
<key> <string>trigger_method_id</string> </key>
<value>
<tuple>
<string>manage_afterAdd</string>
<string>manage_afterClone</string>
</tuple>
</value>
</item>
<item>
<key> <string>trigger_once_per_transaction</string> </key>
<value> <int>1</int> </value>
</item>
<item>
<key> <string>trigger_type</string> </key>
<value> <int>2</int> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Interaction Workflow Interaction" module="erp5.portal_type"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>categories</string> </key>
<value>
<tuple>
<string>before_script/portal_workflow/slapos_accounting_interaction_workflow/script_HostingSubscription_fixPeriodicity</string>
</tuple>
</value>
</item>
<item>
<key> <string>description</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>interaction_HostingSubscription_fixConsistency</string> </value>
</item>
<item>
<key> <string>portal_type</string> </key>
<value> <string>Interaction Workflow Interaction</string> </value>
</item>
<item>
<key> <string>portal_type_filter</string> </key>
<value>
<tuple>
<string>Hosting Subscription</string>
</tuple>
</value>
</item>
<item>
<key> <string>portal_type_group_filter</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>temporary_document_disallowed</string> </key>
<value> <int>1</int> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string>HostingSubscription_fixConsistency</string> </value>
</item>
<item>
<key> <string>trigger_method_id</string> </key>
<value>
<tuple>
<string>fixConsistency</string>
</tuple>
</value>
</item>
<item>
<key> <string>trigger_once_per_transaction</string> </key>
<value> <int>0</int> </value>
</item>
<item>
<key> <string>trigger_type</string> </key>
<value> <int>2</int> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Workflow Script" module="erp5.portal_type"/>
</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>state_change</string> </value>
</item>
<item>
<key> <string>_proxy_roles</string> </key>
<value>
<tuple>
<string>Manager</string>
</tuple>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>script_Base_fixConsistency</string> </value>
</item>
<item>
<key> <string>portal_type</string> </key>
<value> <string>Workflow Script</string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value>
<none/>
</value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
from erp5.component.module.DateUtils import addToDate, getClosestDate
hosting_subscription = state_change['object']
edit_kw = {}
if hosting_subscription.getPeriodicityHour() is None:
edit_kw['periodicity_hour_list'] = [0]
if hosting_subscription.getPeriodicityMinute() is None:
edit_kw['periodicity_minute_list'] = [0]
if hosting_subscription.getPeriodicityMonthDay() is None:
start_date = hosting_subscription.HostingSubscription_calculateSubscriptionStartDate()
start_date = getClosestDate(target_date=start_date, precision='day')
while start_date.day() >= 29:
start_date = addToDate(start_date, to_add={'day': -1})
edit_kw['periodicity_month_day_list'] = [start_date.day()]
if edit_kw:
hosting_subscription.edit(**edit_kw)
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Workflow Script" module="erp5.portal_type"/>
</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>state_change</string> </value>
</item>
<item>
<key> <string>_proxy_roles</string> </key>
<value>
<tuple>
<string>Manager</string>
</tuple>
</value>
</item>
<item>
<key> <string>description</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>script_HostingSubscription_fixPeriodicity</string> </value>
</item>
<item>
<key> <string>portal_type</string> </key>
<value> <string>Workflow Script</string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value>
<none/>
</value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
...@@ -37,11 +37,7 @@ module = portal.portal_trash ...@@ -37,11 +37,7 @@ module = portal.portal_trash
tmp_sale_order = module.newContent( tmp_sale_order = module.newContent(
portal_type='Sale Order', portal_type='Sale Order',
temp_object=True, temp_object=True,
#effective_date=now+1,
start_date=now, start_date=now,
# Ensure stop date value is higher than start date
# it will be updated by OpenSaleOrder_updatePeriod
# stop_date=now + 2,
destination_value=subscriber_person_value, destination_value=subscriber_person_value,
destination_section=source_section, destination_section=source_section,
#destination_decision_value=source_decision_value, #destination_decision_value=source_decision_value,
......
from erp5.component.module.DateUtils import getClosestDate, addToDate
portal = context.getPortalObject() portal = context.getPortalObject()
subscription_request = context subscription_request = context
...@@ -10,21 +12,40 @@ activate_kw = None ...@@ -10,21 +12,40 @@ activate_kw = None
hosting_subscription = portal.hosting_subscription_module.newContent( hosting_subscription = portal.hosting_subscription_module.newContent(
portal_type="Hosting Subscription", portal_type="Hosting Subscription",
title="hosting %s" % subscription_request.getTitle(), title="hosting %s" % subscription_request.getTitle(),
#follow_up_value=instance_tree.getFollowUpValue(),
ledger_value=portal.portal_categories.ledger.automated, ledger_value=portal.portal_categories.ledger.automated,
) )
edit_kw = {}
if hosting_subscription.getPeriodicityHour() is None:
edit_kw['periodicity_hour_list'] = [0]
if hosting_subscription.getPeriodicityMinute() is None:
edit_kw['periodicity_minute_list'] = [0]
if hosting_subscription.getPeriodicityMonthDay() is None:
start_date = hosting_subscription.HostingSubscription_calculateSubscriptionStartDate()
start_date = getClosestDate(target_date=start_date, precision='day')
while start_date.day() >= 29:
start_date = addToDate(start_date, to_add={'day': -1})
edit_kw['periodicity_month_day_list'] = [start_date.day()]
if edit_kw:
hosting_subscription.edit(**edit_kw)
hosting_subscription.validate() hosting_subscription.validate()
####################################################### #######################################################
# Open Sale Order # Open Sale Order
start_date = hosting_subscription.HostingSubscription_calculateSubscriptionStartDate()
open_sale_order = portal.open_sale_order_module.newContent( current_date = getClosestDate(target_date=hosting_subscription.getCreationDate(), precision='day')
portal_type="Open Sale Order", next_period_date = hosting_subscription.getNextPeriodicalDate(current_date)
if subscription_request.getQuantityUnit() == 'time/month':
start_date = addToDate(next_period_date, to_add={'month': -1})
assert hosting_subscription.getNextPeriodicalDate(start_date) == next_period_date
else:
raise ValueError('Unsupported quantity unit %s' % subscription_request.getQuantityUnit())
open_order_edit_kw = dict(
start_date=start_date, 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,
specialise_value=subscription_request.getSpecialiseValue(), specialise_value=subscription_request.getSpecialiseValue(),
source_value=subscription_request.getDestinationValue(), source_value=subscription_request.getDestinationValue(),
source_section_value=subscription_request.getDestinationSectionValue(), source_section_value=subscription_request.getDestinationSectionValue(),
...@@ -40,6 +61,16 @@ open_sale_order = portal.open_sale_order_module.newContent( ...@@ -40,6 +61,16 @@ open_sale_order = portal.open_sale_order_module.newContent(
activate_kw=activate_kw activate_kw=activate_kw
) )
open_sale_order = portal.open_sale_order_module.newContent(
portal_type="Open Sale Order",
# Do not set the stop_date, as we don't know
# when the user will close the subscription
stop_date=None,
**open_order_edit_kw
)
variation_category_list = subscription_request.getVariationCategoryList() variation_category_list = subscription_request.getVariationCategoryList()
open_order_line = open_sale_order.newContent( open_order_line = open_sale_order.newContent(
portal_type="Open Sale Order Line", portal_type="Open Sale Order Line",
...@@ -78,10 +109,37 @@ open_order_cell.edit( ...@@ -78,10 +109,37 @@ open_order_cell.edit(
], ],
activate_kw=activate_kw activate_kw=activate_kw
) )
open_sale_order.OpenSaleOrder_updatePeriod()
open_sale_order.plan() open_sale_order.plan()
open_sale_order.validate() open_sale_order.validate()
#######################################################
# Discount
unused_day_count = current_date - start_date
if 0 < unused_day_count:
# If the open order starts before today,
# generate a discount to the user on his next invoice
sale_packing_list = portal.sale_packing_list_module.newContent(
portal_type="Sale Packing List",
title="first invoice discount for %s" % open_sale_order.getReference(),
# It should match the first open order invoice
stop_date=next_period_date,
**open_order_edit_kw
)
discount_service = portal.restrictedTraverse('service_module/slapos_discount')
sale_packing_list.newContent(
portal_type="Sale Packing List Line",
resource_value=discount_service,
quantity=unused_day_count / (next_period_date - start_date),
price=-subscription_request.getPrice(),
quantity_unit_value=discount_service.getQuantityUnitValue(),
base_contribution_list=discount_service.getBaseContributionList(),
use=discount_service.getUse(),
activate_kw=activate_kw
)
sale_packing_list.confirm()
sale_packing_list.stop()
sale_packing_list.deliver()
return open_sale_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