changeset 31b3659c68ef in modules/sale_shipment_cost:default
details:
https://hg.tryton.org/modules/sale_shipment_cost?cmd=changeset;node=31b3659c68ef
description:
Exclude by default shipment cost from promotion amount
issue9776
review322501006
diffstat:
CHANGELOG | 1 +
sale.py | 22 ++++
sale.xml | 8 +-
tests/scenario_sale_shipment_cost_promotion.rst | 122 ++++++++++++++++++++++++
tests/test_sale_shipment_cost.py | 5 +
view/promotion_form.xml | 9 +
6 files changed, 166 insertions(+), 1 deletions(-)
diffs (211 lines):
diff -r 68871a06c04c -r 31b3659c68ef CHANGELOG
--- a/CHANGELOG Mon Dec 21 00:18:02 2020 +0100
+++ b/CHANGELOG Tue Dec 22 00:24:04 2020 +0100
@@ -1,3 +1,4 @@
+* Exclude by default shipment cost from promotion amount
* Allow shipment cost price to be edited
Version 5.8.0 - 2020-11-02
diff -r 68871a06c04c -r 31b3659c68ef sale.py
--- a/sale.py Mon Dec 21 00:18:02 2020 +0100
+++ b/sale.py Tue Dec 22 00:24:04 2020 +0100
@@ -338,8 +338,30 @@
class Promotion(metaclass=PoolMeta):
__name__ = 'sale.promotion'
+ amount_shipment_cost_included = fields.Boolean(
+ "Amount with Shipment Cost Included",
+ states={
+ 'invisible': ~Eval('amount'),
+ },
+ depends=['amount'])
+
+ @classmethod
+ def default_amount_shipment_cost_included(cls):
+ return False
+
def get_context_formula(self, sale_line):
context = super(Promotion, self).get_context_formula(sale_line)
if sale_line and sale_line.shipment_cost:
context['names']['unit_price'] = sale_line.shipment_cost
return context
+
+ def get_sale_amount(self, sale):
+ amount = super().get_sale_amount(sale)
+ if not self.amount_shipment_cost_included:
+ amount -= sum(l.amount for l in sale.lines if l.shipment_cost)
+ if not self.untaxed_amount:
+ amount -= sum(
+ v['amount']
+ for l in sale.lines if l.shipment_cost
+ for v in l._get_taxes().values())
+ return amount
diff -r 68871a06c04c -r 31b3659c68ef sale.xml
--- a/sale.xml Mon Dec 21 00:18:02 2020 +0100
+++ b/sale.xml Tue Dec 22 00:24:04 2020 +0100
@@ -23,6 +23,12 @@
<field name="inherit" ref="sale.sale_line_view_tree_sequence"/>
<field name="name">sale_line_tree_sequence</field>
</record>
-
+ </data>
+ <data depends="sale_promotion">
+ <record model="ir.ui.view" id="promotion_view_form">
+ <field name="model">sale.promotion</field>
+ <field name="inherit" ref="sale_promotion.promotion_view_form"/>
+ <field name="name">promotion_form</field>
+ </record>
</data>
</tryton>
diff -r 68871a06c04c -r 31b3659c68ef
tests/scenario_sale_shipment_cost_promotion.rst
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/scenario_sale_shipment_cost_promotion.rst Tue Dec 22 00:24:04
2020 +0100
@@ -0,0 +1,122 @@
+=====================================
+Sale Shipment Cost Promotion Scenario
+=====================================
+
+Imports::
+
+ >>> import datetime
+ >>> from dateutil.relativedelta import relativedelta
+ >>> from decimal import Decimal
+ >>> from proteus import Model, Wizard
+ >>> from trytond.tests.tools import activate_modules
+ >>> from trytond.modules.company.tests.tools import create_company, \
+ ... get_company
+ >>> from trytond.modules.account.tests.tools import create_fiscalyear, \
+ ... create_chart, get_accounts
+ >>> from trytond.modules.account_invoice.tests.tools import \
+ ... set_fiscalyear_invoice_sequences, create_payment_term
+ >>> today = datetime.date.today()
+
+Activate modules::
+
+ >>> config = activate_modules([
+ ... 'sale_shipment_cost',
+ ... 'sale',
+ ... 'sale_promotion',
+ ... ])
+
+Create company::
+
+ >>> _ = create_company()
+ >>> company = get_company()
+
+Create chart of accounts::
+
+ >>> _ = create_chart(company)
+ >>> accounts = get_accounts(company)
+
+Create customer::
+
+ >>> Party = Model.get('party.party')
+ >>> customer = Party(name='Customer')
+ >>> customer.save()
+
+Create account category::
+
+ >>> ProductCategory = Model.get('product.category')
+ >>> account_category = ProductCategory(name="Account Category")
+ >>> account_category.accounting = True
+ >>> account_category.account_revenue = accounts['revenue']
+ >>> account_category.save()
+
+Create product::
+
+ >>> ProductUom = Model.get('product.uom')
+ >>> ProductTemplate = Model.get('product.template')
+ >>> Product = Model.get('product.product')
+ >>> unit, = ProductUom.find([('name', '=', 'Unit')])
+
+ >>> template = ProductTemplate()
+ >>> template.name = 'Product'
+ >>> template.default_uom = unit
+ >>> template.type = 'goods'
+ >>> template.salable = True
+ >>> template.list_price = Decimal('20')
+ >>> template.account_category = account_category
+ >>> template.save()
+ >>> product, = template.products
+
+ >>> carrier_template = ProductTemplate()
+ >>> carrier_template.name = 'Carrier Product'
+ >>> carrier_template.default_uom = unit
+ >>> carrier_template.type = 'service'
+ >>> carrier_template.salable = True
+ >>> carrier_template.list_price = Decimal('3')
+ >>> carrier_template.account_category = account_category
+ >>> carrier_template.save()
+ >>> carrier_product, = carrier_template.products
+
+Create Promotion::
+
+ >>> Promotion = Model.get('sale.promotion')
+ >>> promotion = Promotion(name='product 2 free')
+ >>> promotion.amount = Decimal('101')
+ >>> promotion.amount_shipment_cost_included = False
+ >>> promotion.currency = company.currency
+ >>> promotion.products.extend([Product(carrier_product.id)])
+ >>> promotion.formula = '0.0'
+ >>> promotion.save()
+
+Create carrier::
+
+ >>> Carrier = Model.get('carrier')
+ >>> carrier = Carrier()
+ >>> party = Party(name='Carrier')
+ >>> party.save()
+ >>> carrier.party = party
+ >>> carrier.carrier_product = carrier_product
+ >>> carrier.save()
+
+Sale products without the promotion::
+
+ >>> Sale = Model.get('sale.sale')
+ >>> sale = Sale()
+ >>> sale.party = customer
+ >>> sale.carrier = carrier
+ >>> sale_line = sale.lines.new()
+ >>> sale_line.product = product
+ >>> sale_line.quantity = 5.0
+ >>> sale.click('quote')
+ >>> cost_line = sale.lines[-1]
+ >>> cost_line.amount
+ Decimal('3.00')
+
+Increase sale to get the promotion::
+
+ >>> sale.click('draft')
+ >>> line = sale.lines[0]
+ >>> line.quantity = 6.0
+ >>> sale.click('quote')
+ >>> cost_line = sale.lines[-1]
+ >>> cost_line.amount
+ Decimal('0.00')
diff -r 68871a06c04c -r 31b3659c68ef tests/test_sale_shipment_cost.py
--- a/tests/test_sale_shipment_cost.py Mon Dec 21 00:18:02 2020 +0100
+++ b/tests/test_sale_shipment_cost.py Tue Dec 22 00:24:04 2020 +0100
@@ -27,4 +27,9 @@
tearDown=doctest_teardown, encoding='utf-8',
checker=doctest_checker,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE))
+ suite.addTests(doctest.DocFileSuite(
+ 'scenario_sale_shipment_cost_promotion.rst',
+ tearDown=doctest_teardown, encoding='utf-8',
+ checker=doctest_checker,
+ optionflags=doctest.REPORT_ONLY_FIRST_FAILURE))
return suite
diff -r 68871a06c04c -r 31b3659c68ef view/promotion_form.xml
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/view/promotion_form.xml Tue Dec 22 00:24:04 2020 +0100
@@ -0,0 +1,9 @@
+<?xml version="1.0"?>
+<!-- This file is part of Tryton. The COPYRIGHT file at the top level of
+this repository contains the full copyright notices and license terms. -->
+<data>
+ <xpath expr="//group[@id='amount']" position="inside">
+ <label name="amount_shipment_cost_included" string="Shipment Cost
Included"/>
+ <field name="amount_shipment_cost_included"/>
+ </xpath>
+</data>