Commit 4354b756 authored by Romain Courteaud's avatar Romain Courteaud

Simplify open order creation.

Try to reduce the number of documents created.
Always set a stop date on the open order line.
parent 7632c3b2
......@@ -56,7 +56,9 @@ if REQUEST is not None:\n
\n
if context.getCausalityState() == \'diverged\':\n
context.getDestinationSectionValue(portal_type="Person").Person_storeOpenSaleOrderJournal()\n
context.converge()\n
\n
# Person_storeOpenSaleOrderJournal should fix all divergent Hosting Subscription in one run\n
assert context.getCausalityState() == \'solved\'\n
</string> </value>
</item>
<item>
......
<?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>_body</string> </key>
<value> <string encoding="cdata"><![CDATA[
from Products.ERP5Type.DateUtils import addToDate, getClosestDate\n
from DateTime import DateTime\n
\n
person = context\n
portal = person.getPortalObject()\n
\n
previous_open_sale_order = portal.portal_catalog.getResultValue(\n
default_destination_uid=person.getUid(),\n
portal_type="Open Sale Order",\n
validation_state="validated")\n
\n
hosting_subscription_mapping = {}\n
if previous_open_sale_order is not None:\n
for open_sale_order_line in previous_open_sale_order.contentValues(portal_type=\'Open Sale Order Line\'):\n
hosting_subscription_mapping[open_sale_order_line.getAggregate(portal_type=\'Hosting Subscription\')] = open_sale_order_line.getId()\n
\n
now = DateTime()\n
\n
add_kw_kw = {}\n
for add_kw in add_kw_list:\n
hosting_url = add_kw[\'aggregate\']\n
if hosting_url not in hosting_subscription_mapping:\n
existing = len(portal.portal_catalog(\n
portal_type=\'Open Sale Order Line\',\n
default_aggregate_uid=portal.restrictedTraverse(hosting_url).getUid(),\n
limit=1,\n
)) > 0\n
if not existing:\n
# Hosting Subscription has never been related to any open order\n
add_kw_kw[hosting_url] = add_kw.copy()\n
add_kw_kw[hosting_url][\'explanation\'] = [\'Added because of %s \' % hosting_url]\n
\n
modify_kw_kw = {}\n
for hosting_url, start_date in start_date_tuple_list:\n
start_date = getClosestDate(target_date=start_date, precision=\'day\')\n
while start_date.day() >= 29:\n
start_date = addToDate(start_date, to_add={\'day\': -1})\n
if hosting_url in hosting_subscription_mapping:\n
line_id = hosting_subscription_mapping[hosting_url]\n
line = previous_open_sale_order[line_id]\n
if line.getStartDate() is None:\n
modify_kw_kw[line.getAggregate(portal_type=\'Hosting Subscription\')] = {\'start_date\': start_date}\n
modify_kw_kw[line.getAggregate(portal_type=\'Hosting Subscription\')][\'explanation\'] = [\'Set start date because of %s\' % hosting_url]\n
elif hosting_url in add_kw_kw:\n
add_kw_kw[hosting_url][\'start_date\'] = start_date\n
add_kw_kw[hosting_url][\'explanation\'].append(\'Added start date because of %s\' % hosting_url)\n
\n
if previous_open_sale_order is not None:\n
for line in previous_open_sale_order.contentValues(portal_type=\'Open Sale Order Line\'):\n
old_stop_date = line.getStopDate()\n
if old_stop_date is not None:\n
# if stop_date is not None and old_stop_date < stop_date:\n
# modify_kw_kw.setdefault(line.getAggregate(portal_type=\'Hosting Subscription\'), {})\n
# modify_kw_kw[line.getAggregate(portal_type=\'Hosting Subscription\')].setdefault(\'explanation\', [])\n
# modify_kw_kw[line.getAggregate(portal_type=\'Hosting Subscription\')][\'stop_date\'] = stop_date\n
# modify_kw_kw[line.getAggregate(portal_type=\'Hosting Subscription\')][\'explanation\'].append(\'Set stop date because of stop_date %s\' % stop_date)\n
if old_stop_date < now:\n
new_stop_date = addToDate(old_stop_date, to_add={\'month\': 1})\n
while new_stop_date < now:\n
new_stop_date = addToDate(new_stop_date, to_add={\'month\': 1})\n
hosting_id = line.getAggregate(portal_type=\'Hosting Subscription\')\n
modify_kw_kw.setdefault(hosting_id, {})\n
modify_kw_kw[line.getAggregate(portal_type=\'Hosting Subscription\')].setdefault(\'explanation\', [])\n
modify_kw_kw[line.getAggregate(portal_type=\'Hosting Subscription\')][\'stop_date\'] = new_stop_date\n
modify_kw_kw[line.getAggregate(portal_type=\'Hosting Subscription\')][\'explanation\'].append(\'Set stop date becase of new_stop_date %s\' % new_stop_date)\n
\n
for (key, value) in add_kw_kw.copy().iteritems():\n
# if stop_date is not None:\n
# add_kw_kw[key][\'stop_date\'] = stop_date\n
# add_kw_kw[key][\'explanation\'].append(\'Set stop date beacuse of stop_date %s \' % stop_date)\n
# else:\n
start_date = add_kw_kw[key].get(\'start_date\')\n
if start_date is not None:\n
new_stop_date = addToDate(start_date, to_add={\'month\': 1})\n
while new_stop_date < now:\n
new_stop_date = addToDate(new_stop_date, to_add={\'month\': 1})\n
add_kw_kw[key][\'stop_date\'] = new_stop_date\n
add_kw_kw[key][\'explanation\'].append(\'Set stop date because of new_stop_date %s\' % new_stop_date)\n
\n
for (key, value) in modify_kw_kw.copy().iteritems():\n
# if stop_date is not None:\n
# modify_kw_kw[key][\'stop_date\'] = stop_date\n
# modify_kw_kw[key][\'explanation\'].append(\'Set stop date because of stop_date %s\' % stop_date)\n
# else:\n
start_date = modify_kw_kw[key].get(\'start_date\')\n
if start_date is not None:\n
new_stop_date = addToDate(start_date, to_add={\'month\': 1})\n
while new_stop_date < now:\n
new_stop_date = addToDate(new_stop_date, to_add={\'month\': 1})\n
modify_kw_kw[key][\'stop_date\'] = new_stop_date\n
modify_kw_kw[key][\'explanation\'].append(\'Set stop date because of new_stop_date %s\' % new_stop_date)\n
\n
remove_id_list = set()\n
if previous_open_sale_order is not None:\n
for remove_hosting in remove_hosting_list:\n
if remove_hosting in add_kw_kw:\n
add_kw_kw[remove_hosting][\'explanation\'].append(\'Not removed because just added\')\n
continue\n
if remove_hosting in modify_kw_kw:\n
modify_kw_kw[remove_hosting][\'explanation\'].append(\'Not removed because just modified\')\n
continue\n
if remove_hosting in hosting_subscription_mapping:\n
line = previous_open_sale_order[hosting_subscription_mapping.get(remove_hosting)]\n
if (line.getStartDate() is not None and line.getStopDate() is not None and line.getStartDate() < line.getStopDate()):\n
remove_id_list.add((line.getId(), \'Removed %s\' % line.getPath()))\n
return add_kw_kw, modify_kw_kw, list(remove_id_list)\n
]]></string> </value>
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>remove_hosting_list, add_kw_list, start_date_tuple_list</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Person_getOpenSaleOrderDifference</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
......@@ -50,46 +50,213 @@
</item>
<item>
<key> <string>_body</string> </key>
<value> <string>portal = context.getPortalObject()\n
<value> <string encoding="cdata"><![CDATA[
from Products.ERP5Type.DateUtils import addToDate, getClosestDate\n
from DateTime import DateTime\n
\n
remove_hosting_list = []\n
add_kw_list = []\n
start_date_tuple_list = []\n
portal = context.getPortalObject()\n
now = DateTime()\n
person = context\n
tag = \'%s_%s\' % (person.getUid(), script.id)\n
activate_kw = {\'tag\': tag}\n
if portal.portal_activities.countMessageWithTag(tag) > 0:\n
# nothing to do\n
return\n
\n
def getWorkflowDate(document, action):\n
def newOpenOrder(open_sale_order):\n
open_order_edit_kw = {\n
\'effective_date\': now,\n
\'activate_kw\': activate_kw,\n
}\n
if open_sale_order is None:\n
open_sale_order_template = portal.restrictedTraverse(\n
portal.portal_preferences.getPreferredOpenSaleOrderTemplate())\n
new_open_sale_order = open_sale_order_template.Base_createCloneDocument(batch_mode=1)\n
open_order_edit_kw.update({\n
\'destination\': person.getRelativeUrl(),\n
\'destination_decision\': person.getRelativeUrl(),\n
\'title\': "%s SlapOS Subscription" % person.getTitle(),\n
})\n
else:\n
new_open_sale_order = open_sale_order.Base_createCloneDocument(batch_mode=1)\n
open_sale_order.setExpirationDate(now, activate_kw=activate_kw)\n
new_open_sale_order.edit(**open_order_edit_kw)\n
new_open_sale_order.order(activate_kw=activate_kw)\n
new_open_sale_order.validate(activate_kw=activate_kw)\n
return new_open_sale_order\n
\n
def storeWorkflowComment(document, comment):\n
portal.portal_workflow.doActionFor(document, \'edit_action\', comment=comment)\n
\n
def getHostingSubscriptionSimulationStartDate(hosting_subscription):\n
workflow_item_list = portal.portal_workflow.getInfoFor(\n
ob=document,\n
ob=hosting_subscription,\n
name=\'history\',\n
wf_id=\'instance_slap_interface_workflow\')\n
start_date = None\n
for item in workflow_item_list:\n
if item.get(\'action\') == action:\n
return item.get(\'time\')\n
raise ValueError(\'Document %s has no action %s\' % (document.getPath(),\n
action))\n
if item.get(\'action\') == \'request_instance\':\n
start_date = item.get(\'time\')\n
break\n
if start_date is None:\n
# Compatibility with old Hosting subscription\n
start_date = hosting_subscription.getCreationDate()\n
\n
start_date = getClosestDate(target_date=start_date, precision=\'day\')\n
while start_date.day() >= 29:\n
start_date = addToDate(start_date, to_add={\'day\': -1})\n
\n
return start_date\n
\n
def getHostingSubscriptionSimulationStopDate(hosting_subscription, start_date):\n
workflow_item_list = portal.portal_workflow.getInfoFor(\n
ob=hosting_subscription,\n
name=\'history\',\n
wf_id=\'instance_slap_interface_workflow\')\n
end_date = now\n
for item in workflow_item_list:\n
if item.get(\'action\') == \'request_destroy\':\n
end_date = item.get(\'time\')\n
break\n
\n
stop_date = start_date\n
while stop_date <= end_date:\n
stop_date = addToDate(stop_date, to_add={\'month\': 1})\n
return stop_date\n
\n
# Prevent concurrent transaction to update the open order\n
context.serialize()\n
\n
# First, check the existing open order. Does some lines need to be removed, updated?\n
open_sale_order_list = portal.portal_catalog(\n
default_destination_uid=person.getUid(),\n
portal_type="Open Sale Order",\n
validation_state="validated",\n
limit=2,\n
)\n
open_sale_order_count = len(open_sale_order_list)\n
if open_sale_order_count == 0:\n
open_sale_order = None\n
elif open_sale_order_count == 1:\n
open_sale_order = open_sale_order_list[0].getObject()\n
else:\n
raise ValueError, "Too many open order \'%s\' found: %s" % (person.getRelativeUrl(), [x.path for x in open_sale_order_list])\n
\n
delete_line_list = []\n
add_line_list = []\n
\n
updated_hosting_subscription_dict = {}\n
deleted_hosting_subscription_dict = {}\n
\n
if open_sale_order is not None:\n
for open_order_line in open_sale_order.contentValues(\n
portal_type=\'Open Sale Order Line\'):\n
current_start_date = open_order_line.getStartDate()\n
current_stop_date = open_order_line.getStopDate()\n
\n
# Prevent mistakes\n
assert current_start_date is not None\n
assert current_stop_date is not None\n
assert current_start_date < current_stop_date\n
\n
hosting_subscription = open_order_line.getAggregateValue(portal_type=\'Hosting Subscription\')\n
assert current_start_date == getHostingSubscriptionSimulationStartDate(hosting_subscription)\n
\n
# First check if the hosting subscription has been correctly simulated (this script may run only once per year...)\n
stop_date = getHostingSubscriptionSimulationStopDate(hosting_subscription, current_start_date)\n
if current_stop_date != stop_date:\n
# Bingo, new subscription to generate\n
try:\n
assert stop_date == getHostingSubscriptionSimulationStopDate(hosting_subscription, current_stop_date)\n
except:\n
raise ValueError, "%s (%s) != %s (%s)" % (stop_date,\n
current_start_date,\n
getHostingSubscriptionSimulationStopDate(hosting_subscription,\n
current_stop_date),\n
current_stop_date)\n
open_order_line.edit(\n
stop_date=stop_date,\n
activate_kw=activate_kw)\n
storeWorkflowComment(open_order_line,\n
\'Stop date updated to %s\' % stop_date)\n
\n
if hosting_subscription.getSlapState() == \'destroy_requested\':\n
# Line should be deleted\n
assert hosting_subscription.getCausalityState() == \'diverged\'\n
delete_line_list.append(open_order_line.getId())\n
hosting_subscription.converge(comment="Last open order: %s" % open_order_line.getRelativeUrl())\n
deleted_hosting_subscription_dict[hosting_subscription.getRelativeUrl()] = None\n
updated_hosting_subscription_dict[hosting_subscription.getRelativeUrl()] = None\n
\n
if hosting_subscription.getCausalityState() == \'diverged\':\n
hosting_subscription.converge(comment="Nothing to do on open order.")\n
updated_hosting_subscription_dict[hosting_subscription.getRelativeUrl()] = None\n
\n
# Time to check the open order line to add (remaining diverged Hosting\n
# Subscription normally)\n
for hosting_subscription in portal.portal_catalog(\n
portal_type=\'Hosting Subscription\',\n
default_destination_section_uid=context.getUid(),\n
):\n
causality_state="diverged"):\n
hosting_subscription = hosting_subscription.getObject()\n
add_kw_list.append({\n
\'title\': hosting_subscription.getTitle(),\n
\'aggregate\': hosting_subscription.getRelativeUrl(),\n
})\n
if hosting_subscription.getSlapState() == \'destroy_requested\':\n
remove_hosting_list.append(hosting_subscription.getRelativeUrl())\n
try:\n
start_date = getWorkflowDate(hosting_subscription, \'request_instance\')\n
except ValueError:\n
start_date = hosting_subscription.getCreationDate()\n
start_date_tuple_list.append((hosting_subscription.getRelativeUrl(),\n
start_date))\n
\n
context.Person_updateOpenSaleOrder(\n
remove_hosting_list,\n
add_kw_list,\n
start_date_tuple_list)\n
</string> </value>
if hosting_subscription.getCausalityState() == \'diverged\':\n
# Simply check that it has never been simulated\n
assert len(portal.portal_catalog(\n
portal_type=\'Open Sale Order Line\',\n
default_aggregate_uid=hosting_subscription.getUid(),\n
limit=1)) == 0\n
\n
# Let\'s add\n
add_line_list.append(hosting_subscription)\n
else:\n
# Should be in the list of lines to remove\n
assert (hosting_subscription.getRelativeUrl() in deleted_hosting_subscription_dict) or \\\n
(hosting_subscription.getRelativeUrl() in updated_hosting_subscription_dict)\n
\n
new_open_sale_order = None\n
if (delete_line_list):\n
# All Verifications done. Time to clone/create open order\n
new_open_sale_order = newOpenOrder(open_sale_order)\n
\n
open_order_explanation = ""\n
# Remove lines\n
new_open_sale_order.deleteContent(delete_line_list)\n
open_order_explanation += "Removed %s." % str(delete_line_list)\n
\n
storeWorkflowComment(new_open_sale_order, open_order_explanation)\n
\n
if (add_line_list):\n
# No need to create a new open order to add lines\n
if (new_open_sale_order) is None:\n
if open_sale_order is None:\n
new_open_sale_order = newOpenOrder(None)\n
else:\n
new_open_sale_order = open_sale_order\n
\n
open_order_explanation = ""\n
# Add lines\n
added_line_list = []\n
open_sale_order_line_template = portal.restrictedTraverse(\n
portal.portal_preferences.getPreferredOpenSaleOrderLineTemplate())\n
for hosting_subscription in add_line_list:\n
open_sale_order_line = open_sale_order_line_template.Base_createCloneDocument(batch_mode=1,\n
destination=new_open_sale_order)\n
start_date = getHostingSubscriptionSimulationStartDate(hosting_subscription)\n
open_sale_order_line.edit(\n
activate_kw=activate_kw,\n
title=hosting_subscription.getTitle(),\n
start_date=start_date,\n
stop_date=getHostingSubscriptionSimulationStopDate(hosting_subscription, start_date),\n
aggregate_value=hosting_subscription,\n
)\n
storeWorkflowComment(open_sale_order_line, "Created for %s" % hosting_subscription.getRelativeUrl())\n
hosting_subscription.converge(comment="First open order: %s" % open_sale_order_line.getRelativeUrl())\n
added_line_list.append(open_sale_order_line.getId())\n
open_order_explanation += "Added %s." % str(added_line_list)\n
]]></string> </value>
</item>
<item>
<key> <string>_params</string> </key>
......
<?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>_body</string> </key>
<value> <string encoding="cdata"><![CDATA[
from DateTime import DateTime\n
\n
person = context\n
portal = person.getPortalObject()\n
tag = \'%s_updateOpenSaleOrder\' % person.getUid()\n
activate_kw = {\'tag\': tag}\n
if portal.portal_activities.countMessageWithTag(tag) > 0:\n
# nothing to do\n
return\n
\n
def storeWorkflowComment(document, comment):\n
portal.portal_workflow.doActionFor(document, \'edit_action\', comment=comment)\n
\n
add_kw_kw, modify_kw_kw, remove_id_list = context.Person_getOpenSaleOrderDifference(\n
remove_hosting_list,\n
add_kw_list,\n
start_date_tuple_list)\n
\n
now = DateTime()\n
previous_open_sale_order = portal.portal_catalog.getResultValue(\n
default_destination_uid=person.getUid(),\n
portal_type="Open Sale Order",\n
validation_state="validated")\n
\n
if not(add_kw_kw or modify_kw_kw or remove_id_list):\n
return # nothing to do\n
\n
open_order_edit_kw = {\n
\'effective_date\': now,\n
\'activate_kw\': activate_kw,\n
}\n
if previous_open_sale_order is None:\n
open_sale_order_template = portal.restrictedTraverse(\n
portal.portal_preferences.getPreferredOpenSaleOrderTemplate())\n
open_sale_order = open_sale_order_template.Base_createCloneDocument(batch_mode=1)\n
open_order_edit_kw.update({\n
\'destination\': person.getRelativeUrl(),\n
\'destination_decision\': person.getRelativeUrl(),\n
\'title\': "SlapOS Subscription Open Sale Order",\n
})\n
else:\n
open_sale_order = previous_open_sale_order.Base_createCloneDocument(batch_mode=1)\n
previous_open_sale_order.setExpirationDate(now, activate_kw=activate_kw)\n
open_sale_order.edit(**open_order_edit_kw)\n
open_sale_order.order(activate_kw=activate_kw)\n
open_sale_order.validate(activate_kw=activate_kw)\n
\n
open_sale_order_line_template = portal.restrictedTraverse(\n
portal.portal_preferences.getPreferredOpenSaleOrderLineTemplate())\n
for kw in add_kw_kw.itervalues():\n
explanation_list = kw.pop(\'explanation\', [])\n
line = open_sale_order_line_template.Base_createCloneDocument(batch_mode=1,\n
destination=open_sale_order)\n
line.edit(activate_kw=activate_kw, **kw)\n
for explanation in explanation_list:\n
storeWorkflowComment(line, explanation)\n
\n
for hs, kw in modify_kw_kw.iteritems():\n
line = [q for q in open_sale_order.contentValues() if q.getAggregate(portal_type=\'Hosting Subscription\') == hs][0]\n
explanation_list = kw.pop(\'explanation\', [])\n
line.edit(activate_kw=activate_kw, **kw)\n
for explanation in explanation_list:\n
storeWorkflowComment(line, explanation)\n
\n
open_sale_order.deleteContent([q[0] for q in remove_id_list])\n
for explanation in remove_id_list:\n
storeWorkflowComment(open_sale_order, explanation[1])\n
]]></string> </value>
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>remove_hosting_list, add_kw_list, start_date_tuple_list</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Person_updateOpenSaleOrder</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
......@@ -714,7 +714,7 @@ class TestOpenSaleOrderAlarm(testSlapOSMixin):
.Base_createCloneDocument(batch_mode=1)
self.tic()
person.Person_updateOpenSaleOrder()
person.Person_storeOpenSaleOrderJournal()
self.tic()
open_sale_order_list = self.portal.portal_catalog(
......@@ -722,16 +722,8 @@ class TestOpenSaleOrderAlarm(testSlapOSMixin):
portal_type='Open Sale Order',
default_destination_uid=person.getUid()
)
self.assertEqual(1, len(open_sale_order_list))
open_sale_order = open_sale_order_list[0]
self.assertEqual('SlapOS Subscription Open Sale Order',
open_sale_order.getTitle())
self.assertEqual(0, len(open_sale_order.contentValues()))
open_sale_order_template = self.portal.restrictedTraverse(
self.portal.portal_preferences.getPreferredOpenSaleOrderTemplate())
self.assertTrue(all([q in open_sale_order.getCategoryList() \
for q in open_sale_order_template.getCategoryList()]))
# No need to create any open order without hosting subscription
self.assertEqual(0, len(open_sale_order_list))
@simulateByEditWorkflowMark('HostingSubscription_requestUpdateOpenSaleOrder')
def test_alarm_HS_diverged(self):
......@@ -805,7 +797,7 @@ class TestHostingSubscription_requestUpdateOpenSaleOrder(testSlapOSMixin):
line.getQuantity())
self.assertEqual(open_sale_order_line_template.getPrice(),
line.getPrice())
self.assertEqual(None, line.getStartDate())
self.assertEqual(DateTime().earliestTime(), line.getStartDate())
def test_usualLifetime_HostingSubscription(self):
person = self.portal.person_module.template_member\
......@@ -870,7 +862,7 @@ class TestHostingSubscription_requestUpdateOpenSaleOrder(testSlapOSMixin):
self.assertEqual(request_time, line.getStartDate())
self.assertEqual(stop_date, line.getStopDate())
destroy_time = DateTime('2012/02/01')
destroy_time = DateTime('2112/02/01')
subscription.workflow_history['instance_slap_interface_workflow'].append({
'comment':'Simulated request instance',
'error_message': '',
......@@ -926,6 +918,9 @@ class TestHostingSubscription_requestUpdateOpenSaleOrder(testSlapOSMixin):
self.assertEqual(open_sale_order_line_template.getPrice(),
line.getPrice())
self.assertEqual(request_time, archived_line.getStartDate())
now = DateTime()
while stop_date <= destroy_time:
stop_date = addToDate(stop_date, to_add={'month': 1})
self.assertEqual(stop_date, line.getStopDate())
def test_lateAnalysed_HostingSubscription(self):
......@@ -993,8 +988,7 @@ class TestHostingSubscription_requestUpdateOpenSaleOrder(testSlapOSMixin):
self.assertEqual(request_time, line.getStartDate())
stop_date = request_time
now = DateTime()
while stop_date < now:
while stop_date <= destroy_time:
stop_date = addToDate(stop_date, to_add={'month': 1})
self.assertEqual(stop_date, line.getStopDate())
......@@ -1085,42 +1079,23 @@ class TestHostingSubscription_requestUpdateOpenSaleOrder(testSlapOSMixin):
default_destination_uid=person.getUid()
)
self.assertEqual(2, len(open_sale_order_list))
self.assertEqual(1, len(open_sale_order_list))
validated_open_sale_order_list = [q for q in open_sale_order_list
if q.getValidationState() == 'validated']
archived_open_sale_order_list = [q for q in open_sale_order_list
if q.getValidationState() == 'archived']
self.assertEqual(1, len(validated_open_sale_order_list))
self.assertEqual(1, len(archived_open_sale_order_list))
self.assertEqual(0, len(archived_open_sale_order_list))
validated_open_sale_order = validated_open_sale_order_list[0].getObject()
archived_open_sale_order = archived_open_sale_order_list[0]\
.getObject()
self.assertEqual(open_sale_order.getRelativeUrl(),
archived_open_sale_order.getRelativeUrl())
validated_line_list = validated_open_sale_order.contentValues(
portal_type='Open Sale Order Line')
archived_line_list = archived_open_sale_order.contentValues(
portal_type='Open Sale Order Line')
self.assertEqual(2, len(validated_line_list))
self.assertEqual(1, len(archived_line_list))
archived_line = archived_line_list[0].getObject()
self.assertEqual(line.getRelativeUrl(), archived_line.getRelativeUrl())
self.assertEqual(subscription.getRelativeUrl(),
archived_line.getAggregate())
self.assertTrue(all([q in archived_line.getCategoryList() \
for q in open_sale_order_line_template.getCategoryList()]))
self.assertEqual(open_sale_order_line_template.getResource(),
archived_line.getResource())
self.assertEqual(open_sale_order_line_template.getQuantity(),
line.getQuantity())
self.assertEqual(open_sale_order_line_template.getPrice(),
line.getPrice())
self.assertEqual(request_time, archived_line.getStartDate())
self.assertEqual(stop_date, archived_line.getStopDate())
stop_date_2 = request_time_2
now = DateTime()
......@@ -1157,7 +1132,79 @@ class TestHostingSubscription_requestUpdateOpenSaleOrder(testSlapOSMixin):
def test_hosting_subscription_start_date_not_changed(self):
# if there was no request_instance the getCreationDate has been used
# but if request_instance appeared start_date is not changed
raise NotImplementedError
person = self.portal.person_module.template_member\
.Base_createCloneDocument(batch_mode=1)
self.tic()
subscription = self.portal.hosting_subscription_module\
.template_hosting_subscription.Base_createCloneDocument(batch_mode=1)
subscription.edit(reference='TESTHS-%s' % self.generateNewId(),
destination_section=person.getRelativeUrl())
self.portal.portal_workflow._jumpToStateFor(subscription, 'validated')
self.tic()
subscription.HostingSubscription_requestUpdateOpenSaleOrder()
self.tic()
request_time = DateTime('2112/01/01')
subscription.workflow_history['instance_slap_interface_workflow'].append({
'comment':'Simulated request instance',
'error_message': '',
'actor': 'ERP5TypeTestCase',
'slap_state': 'start_requested',
'time': request_time,
'action': 'request_instance'
})
self.tic()
subscription.HostingSubscription_requestUpdateOpenSaleOrder()
self.tic()
self.assertEqual(subscription.getCausalityState(), 'solved')
open_sale_order_list = self.portal.portal_catalog(
portal_type='Open Sale Order',
default_destination_uid=person.getUid()
)
self.assertEqual(1, len(open_sale_order_list))
open_sale_order = open_sale_order_list[0].getObject()
self.assertEqual('validated', open_sale_order.getValidationState())
open_sale_order_line_list = open_sale_order.contentValues(
portal_type='Open Sale Order Line')
self.assertEqual(1, len(open_sale_order_line_list))
line = open_sale_order_line_list[0].getObject()
self.assertEqual(subscription.getCreationDate().earliestTime(),
line.getStartDate())
def test_hosting_subscription_diverged_to_solve(self):
# check that HS becomes solved even if not modification is needed on open
# order
person = self.portal.person_module.template_member\
.Base_createCloneDocument(batch_mode=1)
self.tic()
subscription = self.portal.hosting_subscription_module\
.template_hosting_subscription.Base_createCloneDocument(batch_mode=1)
subscription.edit(reference='TESTHS-%s' % self.generateNewId(),
destination_section=person.getRelativeUrl())
self.portal.portal_workflow._jumpToStateFor(subscription, 'validated')
self.assertEqual(subscription.getCausalityState(), 'diverged')
self.tic()
subscription.HostingSubscription_requestUpdateOpenSaleOrder()
self.tic()
self.assertEqual(subscription.getCausalityState(), 'solved')
self.portal.portal_workflow._jumpToStateFor(subscription, 'diverged')
subscription.reindexObject()
self.assertEqual(subscription.getCausalityState(), 'diverged')
self.assertEqual(subscription.getSlapState(), 'draft')
self.tic()
subscription.HostingSubscription_requestUpdateOpenSaleOrder()
self.tic()
self.assertEqual(subscription.getCausalityState(), 'solved')
class TestSlapOSTriggerBuildAlarm(testSlapOSMixin):
@simulateByTitlewMark('SimulationMovement_buildSlapOS')
......
233
\ No newline at end of file
234
\ No newline at end of file
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