Commit 7c643698 authored by Romain Courteaud's avatar Romain Courteaud

slapos_subscription_request: create discount with a standalone script

parent f346c226
from zExceptions import Unauthorized
if REQUEST is not None:
raise Unauthorized
portal = context.getPortalObject()
open_sale_order_cell = context
hosting_subscription = open_sale_order_cell.getAggregateValue(portal_type='Hosting Subscription')
open_sale_order = open_sale_order_cell.getParentValue()
if open_sale_order_cell.getPortalType() == 'Open Sale Order Cell':
open_sale_order = open_sale_order.getParentValue()
assert open_sale_order.getValidationState() == 'validated'
start_date = open_sale_order.getStartDate()
next_period_date = hosting_subscription.getNextPeriodicalDate(current_date)
sale_packing_list_edit_kw = dict(
title="discount for %s" % open_sale_order.getReference(),
start_date=start_date,
# It should match the first open order invoice
stop_date=next_period_date,
specialise_value=open_sale_order.getSpecialiseValue(),
source_value=open_sale_order.getSourceValue(),
source_section_value=open_sale_order.getSourceSectionValue(),
source_decision_value=open_sale_order.getSourceDecisionValue(),
source_project_value=open_sale_order.getSourceProjectValue(),
destination_value=open_sale_order.getDestinationValue(),
destination_section_value=open_sale_order.getDestinationSectionValue(),
destination_decision_value=open_sale_order.getDestinationDecisionValue(),
destination_project_value=open_sale_order.getDestinationProjectValue(),
ledger_value=open_sale_order.getLedgerValue(),
# XXX XXX XXX XXX causality_value=subscription_request,
price_currency_value=open_sale_order.getPriceCurrencyValue(),
activate_kw=activate_kw
)
unused_day_count = current_date - start_date
if (0 < unused_day_count):#(subscription_request.getPrice() != 0) and (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",
# description='XXX unused day %s - prevision %s' % (unused_day_count, context.getQuantityPrecisionFromResource(subscription_request.getResourceValue())),
**sale_packing_list_edit_kw
)
"""
price = -subscription_request.getPrice() * (unused_day_count / (next_period_date - start_date))
precision = context.getQuantityPrecisionFromResource(subscription_request.getPriceCurrencyValue())
# Use currency precision to reduce the float length
price = float(('%%0.%sf' % precision) % price)
discount_service = portal.restrictedTraverse('service_module/slapos_discount')
sale_packing_list.newContent(
portal_type="Sale Packing List Line",
resource_value=discount_service,
# Use a quantity of 1 to be able to count how many discount were distributed
quantity=1,
price=price,
quantity_unit_value=discount_service.getQuantityUnitValue(),
base_contribution_list=discount_service.getBaseContributionList(),
use=discount_service.getUse(),
activate_kw=activate_kw
)
"""
variation_category_list = open_sale_order_cell.getVariationCategoryList()
sale_packing_list_line = sale_packing_list.newContent(
portal_type="Sale Packing List Line",
resource_value=open_sale_order_cell.getResourceValue(),
variation_category_list=variation_category_list,
quantity_unit_value=open_sale_order_cell.getQuantityUnitValue(),
base_contribution_list=open_sale_order_cell.getResourceValue().getBaseContributionList(),
use=open_sale_order_cell.getResourceValue().getUse(),
activate_kw=activate_kw
)
if variation_category_list:
base_id = 'movement'
cell_key = list(sale_packing_list_line.getCellKeyList(base_id=base_id))[0]
sale_packing_list_cell = sale_packing_list_line.newCell(
base_id=base_id,
portal_type="Sale Packing List Cell",
*cell_key
)
sale_packing_list_cell.edit(
mapped_value_property_list=['price','quantity'],
predicate_category_list=cell_key,
variation_category_list=cell_key,
activate_kw=activate_kw
)
else:
sale_packing_list_cell = sale_packing_list_line
quantity = open_sale_order_cell.getQuantity() * (unused_day_count / (next_period_date - start_date))
# precision = context.getQuantityPrecisionFromResource(subscription_request.getResourceValue())
# XXX use currency precision, to ensure accounting is readable?
precision = context.getQuantityPrecisionFromResource(open_sale_order_cell.getPriceCurrencyValue())
# precision = 3
# Use currency precision to reduce the float length
quantity = float(('%%0.%sf' % precision) % quantity)
# raise NotImplementedError('%s %s -- %s' % (precision, quantity, subscription_request.getResourceValue().getBaseUnitQuantity()))
price = -open_sale_order_cell.getPrice() * (unused_day_count / (next_period_date - start_date))
precision = context.getQuantityPrecisionFromResource(open_sale_order_cell.getPriceCurrencyValue())
# Use currency precision to reduce the float length
price = float(('%%0.%sf' % precision) % price)
aggregate_value_list = [x for x in open_sale_order_cell.getAggregateValue() if (x.getPortalType() != 'Hosting Subscription')]
sale_packing_list_cell.edit(
# Quantity is negative, to reduce the stock of the consumed product
quantity=-quantity,
price=open_sale_order_cell.getPrice(),
# quantity=1,
# price=price,
aggregate_value_list=aggregate_value_list,
activate_kw=activate_kw
)
# """
sale_packing_list.Delivery_fixBaseContributionTaxableRate()
sale_packing_list.Base_checkConsistency()
sale_packing_list.confirm()
sale_packing_list.stop()
sale_packing_list.deliver()
return sale_packing_list
<?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>current_date, activate_kw=None, REQUEST=None</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>OpenSaleOrderCell_createDiscountSalePackingList</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
from erp5.component.module.DateUtils import getClosestDate, addToDate
from zExceptions import Unauthorized
if REQUEST is not None:
raise Unauthorized
portal = context.getPortalObject()
subscription_request = context
......@@ -126,99 +130,6 @@ open_sale_order.validate()
#######################################################
# Discount
unused_day_count = current_date - start_date
if (0 < unused_day_count):#(subscription_request.getPrice() != 0) and (0 < unused_day_count):
# If the open order starts before today,
# generate a discount to the user on his next invoice
open_order_edit_kw['title'] = "first invoice discount for %s" % open_sale_order.getReference()
sale_packing_list = portal.sale_packing_list_module.newContent(
portal_type="Sale Packing List",
# It should match the first open order invoice
stop_date=next_period_date,
description='XXX unused day %s - prevision %s' % (unused_day_count, context.getQuantityPrecisionFromResource(subscription_request.getResourceValue())),
**open_order_edit_kw
)
"""
price = -subscription_request.getPrice() * (unused_day_count / (next_period_date - start_date))
precision = context.getQuantityPrecisionFromResource(subscription_request.getPriceCurrencyValue())
# Use currency precision to reduce the float length
price = float(('%%0.%sf' % precision) % price)
discount_service = portal.restrictedTraverse('service_module/slapos_discount')
sale_packing_list.newContent(
portal_type="Sale Packing List Line",
resource_value=discount_service,
# Use a quantity of 1 to be able to count how many discount were distributed
quantity=1,
price=price,
quantity_unit_value=discount_service.getQuantityUnitValue(),
base_contribution_list=discount_service.getBaseContributionList(),
use=discount_service.getUse(),
activate_kw=activate_kw
)
"""
sale_packing_list_line = sale_packing_list.newContent(
portal_type="Sale Packing List Line",
resource_value=subscription_request.getResourceValue(),
variation_category_list=variation_category_list,
quantity_unit_value=subscription_request.getQuantityUnitValue(),
base_contribution_list=subscription_request.getResourceValue().getBaseContributionList(),
use=subscription_request.getResourceValue().getUse(),
activate_kw=activate_kw
)
if variation_category_list:
base_id = 'movement'
cell_key = list(sale_packing_list_line.getCellKeyList(base_id=base_id))[0]
sale_packing_list_cell = sale_packing_list_line.newCell(
base_id=base_id,
portal_type="Sale Packing List Cell",
*cell_key
)
sale_packing_list_cell.edit(
mapped_value_property_list=['price','quantity'],
predicate_category_list=cell_key,
variation_category_list=cell_key,
activate_kw=activate_kw
)
else:
sale_packing_list_cell = sale_packing_list_line
quantity = subscription_request.getQuantity() * (unused_day_count / (next_period_date - start_date))
# precision = context.getQuantityPrecisionFromResource(subscription_request.getResourceValue())
# XXX use currency precision, to ensure accounting is readable?
precision = context.getQuantityPrecisionFromResource(subscription_request.getPriceCurrencyValue())
# precision = 3
# Use currency precision to reduce the float length
quantity = float(('%%0.%sf' % precision) % quantity)
# raise NotImplementedError('%s %s -- %s' % (precision, quantity, subscription_request.getResourceValue().getBaseUnitQuantity()))
price = -subscription_request.getPrice() * (unused_day_count / (next_period_date - start_date))
precision = context.getQuantityPrecisionFromResource(subscription_request.getPriceCurrencyValue())
# Use currency precision to reduce the float length
price = float(('%%0.%sf' % precision) % price)
sale_packing_list_cell.edit(
# Quantity is negative, to reduce the stock of the consumed product
quantity=-quantity,
price=subscription_request.getPrice(),
# quantity=1,
# price=price,
aggregate_value_list=[
subscription_request.getAggregateValue()
],
activate_kw=activate_kw
)
# """
sale_packing_list.Delivery_fixBaseContributionTaxableRate()
sale_packing_list.Base_checkConsistency()
sale_packing_list.confirm()
sale_packing_list.stop()
sale_packing_list.deliver()
open_order_cell.OpenSaleOrderCell_createDiscountSalePackingList(current_date, activate_kw=activate_kw)
return open_sale_order
......@@ -50,7 +50,7 @@
</item>
<item>
<key> <string>_params</string> </key>
<value> <string></string> </value>
<value> <string>REQUEST=None</string> </value>
</item>
<item>
<key> <string>id</string> </key>
......
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