changeset ca6ba04bb2be in modules/stock_shipment_cost:default
details: 
https://hg.tryton.org/modules/stock_shipment_cost?cmd=changeset&node=ca6ba04bb2be
description:
        Move carrier and cost from sale_shipment_cost to stock_shipment_cost

        issue10375
        review358031002
diffstat:

 doc/index.rst              |   3 +-
 stock.py                   |  68 +++++++++++++++++++++++++++++++++++++++++++++-
 stock.xml                  |  18 ++++++++++++
 tryton.cfg                 |   2 +
 view/shipment_out_form.xml |  20 +++++++++++++
 5 files changed, 109 insertions(+), 2 deletions(-)

diffs (169 lines):

diff -r df2d180aecaf -r ca6ba04bb2be doc/index.rst
--- a/doc/index.rst     Sat May 15 09:22:28 2021 +0200
+++ b/doc/index.rst     Wed May 19 12:44:43 2021 +0200
@@ -2,5 +2,6 @@
 Stock Shipment Cost Module
 ##########################
 
-The *Stock Shipment Cost Module* adds a shipment cost on the outgoing moves.
+The *Stock Shipment Cost Module* adds a shipment cost on the outgoing moves
+which is calculated from the carrier purchase price.
 This cost is added to the product margin reports.
diff -r df2d180aecaf -r ca6ba04bb2be stock.py
--- a/stock.py  Sat May 15 09:22:28 2021 +0200
+++ b/stock.py  Wed May 19 12:44:43 2021 +0200
@@ -5,6 +5,7 @@
 from trytond.model import ModelView, Workflow, fields
 from trytond.pool import PoolMeta, Pool
 from trytond.pyson import Eval
+from trytond.transaction import Transaction
 
 from trytond.modules.product import price_digits, round_price
 
@@ -28,13 +29,75 @@
 class ShipmentCostMixin:
     __slots__ = ()
 
+    carrier = fields.Many2One('carrier', 'Carrier', states={
+            'readonly': Eval('state').in_(['done', 'cancelled']),
+            },
+        depends=['state'])
+
+    cost_used = fields.Function(fields.Numeric(
+            "Cost", digits=price_digits,
+            states={
+                'invisible': Eval('cost_edit', False),
+                },
+            depends=['cost_edit']),
+        'on_change_with_cost_used')
+    cost = fields.Numeric(
+        "Cost", digits=price_digits,
+        states={
+            'invisible': ~Eval('cost_edit', False),
+            'readonly': Eval('state').in_(['done', 'cancelled']),
+            },
+        depends=['state'])
+    cost_edit = fields.Boolean(
+        "Edit Cost",
+        states={
+            'readonly': Eval('state').in_(['done', 'cancelled']),
+            },
+        depends=['state'],
+        help="Check to edit the cost.")
+
+    def _get_carrier_context(self):
+        return {}
+
+    def get_carrier_context(self):
+        return self._get_carrier_context()
+
+    @fields.depends('carrier', 'company', methods=['_get_carrier_context'])
+    def _compute_costs(self):
+        pool = Pool()
+        Currency = pool.get('currency.currency')
+        costs = {
+            'cost': None,
+            }
+        if self.carrier:
+            with Transaction().set_context(self._get_carrier_context()):
+                cost, currency_id = self.carrier.get_purchase_price()
+            if cost is not None:
+                cost = Currency.compute(
+                    Currency(currency_id), cost, self.company.currency,
+                    round=False)
+                costs['cost'] = round_price(cost)
+        return costs
+
+    @fields.depends('cost', 'state', 'cost_edit', methods=['_compute_costs'])
+    def on_change_with_cost_used(self, name=None):
+        cost = self.cost
+        if not self.cost_edit and self.state not in {'cancelled', 'done'}:
+            cost = self._compute_costs()['cost']
+        return cost
+
+    @fields.depends('cost_edit', 'cost_used')
+    def on_change_cost_edit(self):
+        if self.cost_edit:
+            self.cost = self.cost_used
+
     @property
     def shipment_cost_moves(self):
         raise NotImplementedError
 
     def _get_shipment_cost(self):
         "Return the cost for the shipment in the company currency"
-        return Decimal(0)
+        return self.cost_used or Decimal(0)
 
     @classmethod
     def set_shipment_cost(cls, shipments):
@@ -82,6 +145,9 @@
     @ModelView.button
     @Workflow.transition('done')
     def done(cls, shipments):
+        for shipment in shipments:
+            shipment.cost = shipment.cost_used
+        cls.save(shipments)
         super().done(shipments)
         cls.set_shipment_cost(shipments)
 
diff -r df2d180aecaf -r ca6ba04bb2be stock.xml
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/stock.xml Wed May 19 12:44:43 2021 +0200
@@ -0,0 +1,18 @@
+<?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. -->
+<tryton>
+    <data>
+        <record model="ir.ui.view" id="shipment_out_view_form">
+            <field name="model">stock.shipment.out</field>
+            <field name="inherit" ref="stock.shipment_out_view_form"/>
+            <field name="name">shipment_out_form</field>
+        </record>
+
+        <record model="ir.ui.view" id="shipment_out_return_view_form">
+            <field name="model">stock.shipment.out.return</field>
+            <field name="inherit" ref="stock.shipment_out_return_view_form"/>
+            <field name="name">shipment_out_form</field>
+        </record>
+    </data>
+</tryton>
diff -r df2d180aecaf -r ca6ba04bb2be tryton.cfg
--- a/tryton.cfg        Sat May 15 09:22:28 2021 +0200
+++ b/tryton.cfg        Wed May 19 12:44:43 2021 +0200
@@ -1,8 +1,10 @@
 [tryton]
 version=6.1.0
 depends:
+    carrier
     ir
     product
     stock
 xml:
+    stock.xml
     stock_reporting_margin.xml
diff -r df2d180aecaf -r ca6ba04bb2be view/shipment_out_form.xml
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/view/shipment_out_form.xml        Wed May 19 12:44:43 2021 +0200
@@ -0,0 +1,20 @@
+<?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="//field[@name='warehouse']" position="after">
+        <label name="carrier"/>
+        <field name="carrier"/>
+    </xpath>
+    <xpath expr="//page[@id='other']" position="before">
+        <page id="costs" string="Costs">
+            <group id="costs" col="-1" colspan="4">
+                <label name="cost"/>
+                <label name="cost_used"/>
+                <field name="cost"/>
+                <field name="cost_used"/>
+                <field name="cost_edit"/>
+            </group>
+        </page>
+    </xpath>
+</data>

Reply via email to