Commit ffcd9d34 authored by Romain Courteaud's avatar Romain Courteaud

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 5999c128
......@@ -37,11 +37,7 @@ module = portal.portal_trash
tmp_sale_order = module.newContent(
portal_type='Sale Order',
temp_object=True,
#effective_date=now+1,
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_section=source_section,
#destination_decision_value=source_decision_value,
......
......@@ -34,14 +34,18 @@ hosting_subscription.validate()
#######################################################
# Open Sale Order
start_date = hosting_subscription.HostingSubscription_calculateSubscriptionStartDate()
open_sale_order = portal.open_sale_order_module.newContent(
portal_type="Open Sale Order",
current_date = getClosestDate(target_date=hosting_subscription.getCreationDate(), precision='day')
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,
# 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(),
source_value=subscription_request.getDestinationValue(),
source_section_value=subscription_request.getDestinationSectionValue(),
......@@ -57,6 +61,16 @@ open_sale_order = portal.open_sale_order_module.newContent(
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()
open_order_line = open_sale_order.newContent(
portal_type="Open Sale Order Line",
......@@ -95,10 +109,37 @@ open_order_cell.edit(
],
activate_kw=activate_kw
)
open_sale_order.OpenSaleOrder_updatePeriod()
open_sale_order.plan()
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
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