changeset b33da97e4687 in modules/account_stock_shipment_cost:default
details: 
https://hg.tryton.org/modules/account_stock_shipment_cost?cmd=changeset&node=b33da97e4687
description:
        Show cost allocation before and after posting

        issue11558
        review413301003
diffstat:

 CHANGELOG                                      |    2 +
 __init__.py                                    |    6 +-
 account.py                                     |  111 +++++++++++++++++++++++-
 account.xml                                    |   32 ++++++-
 tests/scenario_account_stock_shipment_cost.rst |   25 ++++-
 view/shipment_cost_form.xml                    |    3 +-
 view/shipment_cost_show_form.xml               |    9 ++
 view/shipment_cost_show_shipment_list.xml      |    7 +
 8 files changed, 183 insertions(+), 12 deletions(-)

diffs (332 lines):

diff -r ff1179530c06 -r b33da97e4687 CHANGELOG
--- a/CHANGELOG Wed Jun 15 22:05:41 2022 +0200
+++ b/CHANGELOG Mon Jul 18 00:16:26 2022 +0200
@@ -1,3 +1,5 @@
+* Show cost allocation before and after posting
+
 Version 6.4.0 - 2022-05-02
 * Bug fixes (see mercurial logs for details)
 * Add allocation method
diff -r ff1179530c06 -r b33da97e4687 __init__.py
--- a/__init__.py       Wed Jun 15 22:05:41 2022 +0200
+++ b/__init__.py       Mon Jul 18 00:16:26 2022 +0200
@@ -17,11 +17,13 @@
         account.ShipmentCost,
         account.ShipmentCost_Shipment,
         account.ShipmentCost_ShipmentReturn,
+        account.ShipmentCostShow,
+        account.ShipmentCostShowShipment,
         account.InvoiceLine,
         stock.ShipmentOut,
         stock.ShipmentOutReturn,
         module='account_stock_shipment_cost', type_='model')
     Pool.register(
+        account.PostShipmentCost,
+        account.ShowShipmentCost,
         module='account_stock_shipment_cost', type_='wizard')
-    Pool.register(
-        module='account_stock_shipment_cost', type_='report')
diff -r ff1179530c06 -r b33da97e4687 account.py
--- a/account.py        Wed Jun 15 22:05:41 2022 +0200
+++ b/account.py        Mon Jul 18 00:16:26 2022 +0200
@@ -6,10 +6,11 @@
 from trytond.i18n import gettext
 from trytond.model import ModelSQL, ModelView, Workflow, fields
 from trytond.modules.company.model import CompanyValueMixin
-from trytond.modules.product import round_price
+from trytond.modules.product import price_digits, round_price
 from trytond.pool import Pool, PoolMeta
 from trytond.pyson import Eval, Id
 from trytond.transaction import Transaction
+from trytond.wizard import Button, StateTransition, StateView, Wizard
 
 from .exceptions import NoShipmentWarning, SamePartiesWarning
 
@@ -138,10 +139,14 @@
                     'invisible': Eval('state') != 'cancelled',
                     'depends': ['state'],
                     },
-                'post': {
+                'post_wizard': {
                     'invisible': Eval('state') != 'draft',
                     'depends': ['state'],
                     },
+                'show': {
+                    'invisible': Eval('state').in_(['draft', 'cancelled']),
+                    'depends': ['state']
+                    },
                 })
 
     @classmethod
@@ -199,13 +204,18 @@
         return {l.invoice.party for l in self.invoice_lines}
 
     def allocate_cost_by_shipment(self):
-        self.factors = self._get_shipment_factors()
+        self.factors = self._get_factors('shipment')
         self._allocate_cost(self.factors)
 
     def unallocate_cost_by_shipment(self):
-        factors = self.factors or self._get_shipment_factors()
+        factors = self.factors or self._get_factors('shipment')
         self._allocate_cost(factors, sign=-1)
 
+    def _get_factors(self, method=None):
+        if method is None:
+            method = self.allocation_method
+        return getattr(self, '_get_%s_factors' % method)()
+
     def _get_shipment_factors(self):
         shipments = self.all_shipments
         length = Decimal(len(shipments))
@@ -239,7 +249,18 @@
             klass.set_shipment_cost(shipments)
 
     @classmethod
-    @ModelView.button
+    @ModelView.button_action(
+        'account_stock_shipment_cost.wizard_shipment_cost_post')
+    def post_wizard(cls, shipment_costs):
+        pass
+
+    @classmethod
+    @ModelView.button_action(
+        'account_stock_shipment_cost.wizard_shipment_cost_show')
+    def show(cls, shipment_costs):
+        pass
+
+    @classmethod
     @Workflow.transition('posted')
     def post(cls, shipment_costs):
         pool = Pool()
@@ -322,6 +343,86 @@
         required=True, ondelete='CASCADE')
 
 
+class ShowShipmentCostMixin(Wizard):
+    start_state = 'show'
+    show = StateView('account.shipment_cost.show',
+        'account_stock_shipment_cost.shipment_cost_show_view_form', [])
+
+    @property
+    def factors(self):
+        return self.record._get_factors()
+
+    def default_show(self, fields):
+        shipments = []
+        cost = self.record.cost
+        default = {
+            'cost': round_price(cost),
+            'shipments': shipments,
+            }
+        factors = self.factors
+        for shipment in self.record.all_shipments:
+            shipments.append({
+                    'shipment': str(shipment),
+                    'cost': round_price(cost * factors[str(shipment)]),
+                    })
+        return default
+
+
+class PostShipmentCost(ShowShipmentCostMixin):
+    "Post Shipment Cost"
+    __name__ = 'account.shipment_cost.post'
+    post = StateTransition()
+
+    @classmethod
+    def __setup__(cls):
+        super().__setup__()
+        cls.show.buttons.extend([
+                Button("Cancel", 'end', 'tryton-cancel'),
+                Button("Post", 'post', 'tryton-ok', default=True),
+                ])
+
+    def transition_post(self):
+        self.model.post([self.record])
+        return 'end'
+
+
+class ShowShipmentCost(ShowShipmentCostMixin):
+    "Show Shipment Cost"
+    __name__ = 'account.shipment_cost.show'
+
+    @classmethod
+    def __setup__(cls):
+        super().__setup__()
+        cls.show.buttons.extend([
+                Button("Close", 'end', 'tryton-close', default=True),
+                ])
+
+    @property
+    def factors(self):
+        return self.record.factors or super().factors
+
+
+class ShipmentCostShow(ModelView):
+    "Shipment Cost Show"
+    __name__ = 'account.shipment_cost.show'
+
+    cost = fields.Numeric("Cost", digits=price_digits, readonly=True)
+    shipments = fields.One2Many(
+        'account.shipment_cost.show.shipment', None, "Shipments",
+        readonly=True)
+
+
+class ShipmentCostShowShipment(ModelView):
+    "Post Shipment Cost"
+    __name__ = 'account.shipment_cost.show.shipment'
+
+    shipment = fields.Reference("Shipments", [
+            ('stock.shipment.out', "Shipment"),
+            ('stock.shipment.out.return', "Shipment Return"),
+            ], readonly=True)
+    cost = fields.Numeric("Cost", digits=price_digits, readonly=True)
+
+
 class InvoiceLine(metaclass=PoolMeta):
     __name__ = 'account.invoice.line'
     shipment_cost = fields.Many2One(
diff -r ff1179530c06 -r b33da97e4687 account.xml
--- a/account.xml       Wed Jun 15 22:05:41 2022 +0200
+++ b/account.xml       Mon Jul 18 00:16:26 2022 +0200
@@ -130,11 +130,17 @@
         </record>
 
         <record model="ir.model.button" id="shipment_cost_post_button">
-            <field name="name">post</field>
+            <field name="name">post_wizard</field>
             <field name="string">Post</field>
             <field name="model" search="[('model', '=', 
'account.shipment_cost')]"/>
         </record>
 
+        <record model="ir.model.button" id="shipment_cost_show_button">
+            <field name="name">show</field>
+            <field name="string">Show</field>
+            <field name="model" search="[('model', '=', 
'account.shipment_cost')]"/>
+        </record>
+
         <record model="ir.model.button" id="shipment_cost_draft_button">
             <field name="name">draft</field>
             <field name="string">Draft</field>
@@ -151,6 +157,30 @@
             <field name="rule_group" ref="rule_group_shipment_cost_companies"/>
         </record>
 
+        <record model="ir.action.wizard" id="wizard_shipment_cost_post">
+            <field name="name">Post Shipment Cost</field>
+            <field name="wiz_name">account.shipment_cost.post</field>
+            <field name="model">account.shipment_cost</field>
+        </record>
+
+        <record model="ir.action.wizard" id="wizard_shipment_cost_show">
+            <field name="name">Show Shipment Cost</field>
+            <field name="wiz_name">account.shipment_cost.show</field>
+            <field name="model">account.shipment_cost</field>
+        </record>
+
+        <record model="ir.ui.view" id="shipment_cost_show_view_form">
+            <field name="model">account.shipment_cost.show</field>
+            <field name="type">form</field>
+            <field name="name">shipment_cost_show_form</field>
+        </record>
+
+        <record model="ir.ui.view" id="shipment_cost_show_shipment_view_list">
+            <field name="model">account.shipment_cost.show.shipment</field>
+            <field name="type">tree</field>
+            <field name="name">shipment_cost_show_shipment_list</field>
+        </record>
+
         <record model="ir.ui.view" id="invoice_line_view_form">
             <field name="model">account.invoice.line</field>
             <field name="inherit" 
ref="account_invoice.invoice_line_view_form"/>
diff -r ff1179530c06 -r b33da97e4687 
tests/scenario_account_stock_shipment_cost.rst
--- a/tests/scenario_account_stock_shipment_cost.rst    Wed Jun 15 22:05:41 
2022 +0200
+++ b/tests/scenario_account_stock_shipment_cost.rst    Mon Jul 18 00:16:26 
2022 +0200
@@ -149,12 +149,25 @@
     'draft'
     >>> bool(shipment_cost1.number)
     True
-    >>> shipment_cost1.click('post')
+    >>> post_shipment_cost = Wizard('account.shipment_cost.post', 
[shipment_cost1])
+    >>> post_shipment_cost.form.cost
+    Decimal('10.0000')
+    >>> sorted([s.cost for s in post_shipment_cost.form.shipments])
+    [Decimal('5.0000'), Decimal('5.0000')]
+    >>> post_shipment_cost.execute('post')
     >>> shipment_cost1.state
     'posted'
     >>> bool(shipment_cost1.posted_date)
     True
 
+Show shipment cost::
+
+    >>> show_shipment_cost = Wizard('account.shipment_cost.show', 
[shipment_cost1])
+    >>> show_shipment_cost.form.cost
+    Decimal('10.0000')
+    >>> sorted([s.cost for s in show_shipment_cost.form.shipments])
+    [Decimal('5.0000'), Decimal('5.0000')]
+
 Check shipment cost::
 
     >>> shipment1.reload()
@@ -178,8 +191,14 @@
     >>> shipment_cost2 = ShipmentCost()
     >>> shipment_cost2.invoice_lines.append(InvoiceLine(line.id))
     >>> shipment_cost2.shipments.append(ShipmentOut(shipment1.id))
+    >>> shipment_cost2.save()
+    >>> post_shipment_cost = Wizard('account.shipment_cost.post', 
[shipment_cost2])
+    >>> post_shipment_cost.form.cost
+    Decimal('2.0000')
+    >>> sorted([s.cost for s in post_shipment_cost.form.shipments])
+    [Decimal('2.0000')]
     >>> try:
-    ...     shipment_cost2.click('post')
+    ...     post_shipment_cost.execute('post')
     ... except SamePartiesWarning as warning:
     ...     _, (key, *_) = warning.args
     ...     raise  # doctest: +IGNORE_EXCEPTION_DETAIL
@@ -187,7 +206,7 @@
         ...
     SamePartiesWarning: ...
     >>> Warning(user=config.user, name=key).save()
-    >>> shipment_cost2.click('post')
+    >>> post_shipment_cost.execute('post')
     >>> shipment_cost2.state
     'posted'
 
diff -r ff1179530c06 -r b33da97e4687 view/shipment_cost_form.xml
--- a/view/shipment_cost_form.xml       Wed Jun 15 22:05:41 2022 +0200
+++ b/view/shipment_cost_form.xml       Mon Jul 18 00:16:26 2022 +0200
@@ -22,6 +22,7 @@
     <group col="-1" colspan="2" id="buttons">
         <button name="cancel" icon="tryton-cancel"/>
         <button name="draft" icon="tryton-clear"/>
-        <button name="post" icon="tryton-ok"/>
+        <button name="post_wizard" icon="tryton-ok"/>
+        <button name="show" icon="tryton-search"/>
     </group>
 </form>
diff -r ff1179530c06 -r b33da97e4687 view/shipment_cost_show_form.xml
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/view/shipment_cost_show_form.xml  Mon Jul 18 00:16:26 2022 +0200
@@ -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. -->
+<form>
+    <label name="cost"/>
+    <field name="cost"/>
+
+    <field name="shipments" mode="tree" colspan="4"/>
+</form>
diff -r ff1179530c06 -r b33da97e4687 view/shipment_cost_show_shipment_list.xml
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/view/shipment_cost_show_shipment_list.xml Mon Jul 18 00:16:26 2022 +0200
@@ -0,0 +1,7 @@
+<?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. -->
+<tree>
+    <field name="shipment" expand="1"/>
+    <field name="cost"/>
+</tree>

Reply via email to