changeset ab8859266c7e in modules/purchase_shipment_cost:default
details: 
https://hg.tryton.org/modules/purchase_shipment_cost?cmd=changeset&node=ab8859266c7e
description:
        Add back the shipment cost when recompute move unit price

        When the unit price of a move is updated, the shipment cost that was 
added must
        be added back.

        issue10196
        review348201004
diffstat:

 setup.py                                                |    1 +
 stock.py                                                |   16 +
 tests/scenario_purchase_shipment_cost_invoice_stock.rst |  142 ++++++++++++++++
 tests/test_purchase_shipment_cost.py                    |    5 +
 tryton.cfg                                              |    2 +
 5 files changed, 166 insertions(+), 0 deletions(-)

diffs (213 lines):

diff -r a3642ff66705 -r ab8859266c7e setup.py
--- a/setup.py  Sat Mar 13 23:43:51 2021 +0100
+++ b/setup.py  Wed Apr 28 00:35:04 2021 +0200
@@ -69,6 +69,7 @@
         extras_require[dep] = get_require_version('trytond_%s' % dep)
 
 tests_require = [get_require_version('proteus'),
+    get_require_version('trytond_account_invoice_stock'),
     get_require_version('trytond_account_stock_continental'),
     get_require_version('trytond_account_stock_anglo_saxon'),
     get_require_version('trytond_purchase')]
diff -r a3642ff66705 -r ab8859266c7e stock.py
--- a/stock.py  Sat Mar 13 23:43:51 2021 +0100
+++ b/stock.py  Wed Apr 28 00:35:04 2021 +0200
@@ -190,6 +190,22 @@
     unit_shipment_cost = fields.Numeric('Unit Shipment Cost',
         digits=price_digits, readonly=True)
 
+    def _compute_unit_price(self, unit_price):
+        if self.unit_shipment_cost:
+            unit_price -= self.unit_shipment_cost
+        unit_price = super()._compute_unit_price(unit_price)
+        if self.unit_shipment_cost:
+            unit_price += self.unit_shipment_cost
+        return unit_price
+
+    def _compute_component_unit_price(self, unit_price):
+        if self.unit_shipment_cost:
+            unit_price -= self.unit_shipment_cost
+        unit_price = super()._compute_component_unit_price(unit_price)
+        if self.unit_shipment_cost:
+            unit_price += self.unit_shipment_cost
+        return unit_price
+
     # Split the shipment cost if account_stock_continental is activated
     def _get_account_stock_move_lines(self, type_):
         pool = Pool()
diff -r a3642ff66705 -r ab8859266c7e 
tests/scenario_purchase_shipment_cost_invoice_stock.rst
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/scenario_purchase_shipment_cost_invoice_stock.rst   Wed Apr 28 
00:35:04 2021 +0200
@@ -0,0 +1,142 @@
+==================================================
+Purchase Shipment Cost with Invoice Stock Scenario
+==================================================
+
+Imports::
+
+    >>> import datetime as dt
+    >>> 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)
+
+    >>> today = dt.date.today()
+
+Activate modules::
+
+    >>> config = activate_modules([
+    ...         'purchase_shipment_cost',
+    ...         'account_invoice_stock',
+    ...         'purchase',
+    ...         ])
+
+    >>> Carrier = Model.get('carrier')
+    >>> Move = Model.get('stock.move')
+    >>> Party = Model.get('party.party')
+    >>> ProductCategory = Model.get('product.category')
+    >>> ProductTemplate = Model.get('product.template')
+    >>> ProductUom = Model.get('product.uom')
+    >>> Purchase = Model.get('purchase.purchase')
+    >>> ShipmentIn = Model.get('stock.shipment.in')
+
+Create company::
+
+    >>> _ = create_company()
+    >>> company = get_company()
+
+Create chart of accounts::
+
+    >>> _ = create_chart(company)
+    >>> accounts = get_accounts(company)
+
+Create fiscal year::
+
+    >>> fiscalyear = set_fiscalyear_invoice_sequences(
+    ...     create_fiscalyear(company))
+    >>> fiscalyear.click('create_period')
+
+Create account categories::
+
+    >>> account_category = ProductCategory(name="Account Category")
+    >>> account_category.accounting = True
+    >>> account_category.account_expense = accounts['expense']
+    >>> account_category.save()
+
+Create supplier::
+
+    >>> supplier = Party(name='Supplier')
+    >>> supplier.save()
+
+Create products::
+
+    >>> unit, = ProductUom.find([('name', '=', 'Unit')])
+
+    >>> template = ProductTemplate()
+    >>> template.name = 'Product'
+    >>> template.default_uom = unit
+    >>> template.type = 'goods'
+    >>> template.list_price = Decimal('20')
+    >>> template.purchasable = True
+    >>> template.account_category = account_category
+    >>> product, = template.products
+    >>> product.cost_price = Decimal('8')
+    >>> template.save()
+    >>> product, = template.products
+
+    >>> carrier_template = ProductTemplate()
+    >>> carrier_template.name = 'Carrier Product'
+    >>> carrier_template.default_uom = unit
+    >>> carrier_template.type = 'service'
+    >>> carrier_template.list_price = Decimal('5')
+    >>> carrier_product, = carrier_template.products
+    >>> carrier_product.cost_price = Decimal('3')
+    >>> carrier_template.save()
+    >>> carrier_product, = carrier_template.products
+
+Create carrier::
+
+    >>> carrier = Carrier()
+    >>> party = Party(name='Carrier')
+    >>> party.save()
+    >>> carrier.party = party
+    >>> carrier.carrier_product = carrier_product
+    >>> carrier.save()
+
+Purchase a product::
+
+    >>> purchase = Purchase()
+    >>> purchase.party = supplier
+    >>> line = purchase.lines.new()
+    >>> line.product = product
+    >>> line.quantity = 1
+    >>> line.unit_price = Decimal('10')
+    >>> purchase.click('quote')
+    >>> purchase.click('confirm')
+    >>> purchase.state
+    'processing'
+
+Receive the product::
+
+    >>> shipment = ShipmentIn()
+    >>> shipment.supplier = supplier
+    >>> move, = purchase.moves
+    >>> shipment.incoming_moves.append(Move(id=move.id))
+    >>> shipment.carrier = carrier
+    >>> shipment.cost_used
+    Decimal('3.0000')
+    >>> shipment.click('receive')
+    >>> shipment.state
+    'received'
+    >>> move, = shipment.incoming_moves
+    >>> move.unit_price
+    Decimal('13.0000')
+
+Post the invoice with a different price::
+
+    >>> invoice, = purchase.invoices
+    >>> line, = invoice.lines
+    >>> line.unit_price = Decimal('9')
+    >>> invoice.invoice_date = today
+    >>> invoice.click('post')
+
+Check unit price of move::
+
+    >>> move.reload()
+    >>> move.unit_price
+    Decimal('12.0000')
diff -r a3642ff66705 -r ab8859266c7e tests/test_purchase_shipment_cost.py
--- a/tests/test_purchase_shipment_cost.py      Sat Mar 13 23:43:51 2021 +0100
+++ b/tests/test_purchase_shipment_cost.py      Wed Apr 28 00:35:04 2021 +0200
@@ -23,6 +23,11 @@
             checker=doctest_checker,
             optionflags=doctest.REPORT_ONLY_FIRST_FAILURE))
     suite.addTests(doctest.DocFileSuite(
+            'scenario_purchase_shipment_cost_invoice_stock.rst',
+            tearDown=doctest_teardown, encoding='utf-8',
+            checker=doctest_checker,
+            optionflags=doctest.REPORT_ONLY_FIRST_FAILURE))
+    suite.addTests(doctest.DocFileSuite(
             'scenario_purchase_shipment_cost_with_account_stock.rst',
             tearDown=doctest_teardown, encoding='utf-8',
             checker=doctest_checker,
diff -r a3642ff66705 -r ab8859266c7e tryton.cfg
--- a/tryton.cfg        Sat Mar 13 23:43:51 2021 +0100
+++ b/tryton.cfg        Wed Apr 28 00:35:04 2021 +0200
@@ -8,9 +8,11 @@
     res
     stock
 extras_depend:
+    account_invoice_stock
     account_product
     account_stock_continental
     account_stock_anglo_saxon
+    product_kit
 xml:
     stock.xml
     carrier.xml

Reply via email to