changeset 2fb80252d8f4 in modules/sale_complaint:default
details: 
https://hg.tryton.org/modules/sale_complaint?cmd=changeset;node=2fb80252d8f4
description:
        Compute amount of complaint action

        issue9899
        review335241002
diffstat:

 CHANGELOG                         |    1 +
 complaint.py                      |  141 ++++++++++++++++++++++++++++++++++++-
 tests/scenario_sale_complaint.rst |   14 +++
 view/action_form.xml              |   26 +++---
 view/action_line_form.xml         |   11 +-
 view/action_line_list.xml         |    6 +-
 view/action_list.xml              |    1 +
 7 files changed, 174 insertions(+), 26 deletions(-)

diffs (359 lines):

diff -r 5a1c7bd33adb -r 2fb80252d8f4 CHANGELOG
--- a/CHANGELOG Sat Dec 19 17:08:46 2020 +0100
+++ b/CHANGELOG Sat Dec 26 23:58:50 2020 +0100
@@ -1,3 +1,4 @@
+* Compute amount of complaint action
 * Add active field to complaint type
 
 Version 5.8.0 - 2020-11-02
diff -r 5a1c7bd33adb -r 2fb80252d8f4 complaint.py
--- a/complaint.py      Sat Dec 19 17:08:46 2020 +0100
+++ b/complaint.py      Sat Dec 26 23:58:50 2020 +0100
@@ -2,6 +2,7 @@
 # this repository contains the full copyright notices and license terms.
 
 from collections import defaultdict
+from decimal import Decimal
 
 from trytond.i18n import gettext
 from trytond.model import ModelSQL, ModelView, Workflow, fields
@@ -397,6 +398,16 @@
         states=_line_states, depends=_line_depends,
         help='Leave empty for the same price.')
 
+    amount = fields.Function(fields.Numeric(
+            "Amount", digits=(16, Eval('currency_digits', 2)),
+            depends=['currency_digits']),
+        'on_change_with_amount')
+    currency = fields.Function(fields.Many2One(
+            'currency.currency', "Currency"),
+        'on_change_with_currency')
+    currency_digits = fields.Function(fields.Integer("Currency Digits"),
+        'on_change_with_currency_digits')
+
     result = fields.Reference('Result', selection='get_result', readonly=True)
 
     complaint_state = fields.Function(
@@ -419,6 +430,63 @@
                     'sale.line', 'account.invoice.line'}):
             return self.complaint.origin.unit.digits
 
+    @fields.depends(
+        'quantity', 'unit_price', 'currency', 'sale_lines', 'invoice_lines',
+        'complaint', '_parent_complaint.origin_model',
+        '_parent_complaint.origin')
+    def on_change_with_amount(self, name=None):
+        if self.complaint:
+            if self.complaint.origin_model in {
+                    'sale.line', 'account.invoice.line'}:
+                if self.quantity is not None:
+                    quantity = self.quantity
+                else:
+                    quantity = self.complaint.origin.quantity
+                if self.unit_price is not None:
+                    unit_price = self.unit_price
+                else:
+                    unit_price = self.complaint.origin.unit_price
+                amount = Decimal(str(quantity)) * unit_price
+                if self.currency:
+                    amount = self.currency.round(amount)
+                return amount
+            elif self.complaint.origin_model == 'sale.sale':
+                if not self.sale_lines:
+                    if self.complaint and self.complaint.origin:
+                        return self.complaint.origin.untaxed_amount
+                else:
+                    return sum(
+                        getattr(l, 'amount', None) or Decimal(0)
+                        for l in self.sale_lines)
+            elif self.complaint.origin_model == 'account.invoice':
+                if not self.invoice_lines:
+                    if self.complaint and self.complaint.origin:
+                        return self.complaint.origin.untaxed_amount
+                else:
+                    return sum(
+                        getattr(l, 'amount', None) or Decimal(0)
+                        for l in self.invoice_lines)
+
+    @fields.depends(
+        'complaint',
+        '_parent_complaint.origin_model', '_parent_complaint.origin')
+    def on_change_with_currency(self, name=None):
+        if (self.complaint
+                and self.complaint.origin_model in {
+                    'sale.sale', 'sale.line',
+                    'account.invoice', 'account.invoice.line'}):
+            return self.complaint.origin.currency.id
+
+    @fields.depends(
+        'complaint',
+        '_parent_complaint.origin_model', '_parent_complaint.origin')
+    def on_change_with_currency_digits(self, name=None):
+        if (self.complaint
+                and self.complaint.origin_model in {
+                    'sale.sale', 'sale.line',
+                    'account.invoice', 'account.invoice.line'}):
+            return self.complaint.origin.currency.digits
+
     @classmethod
     def get_complaint_states(cls):
         pool = Pool()
@@ -459,12 +527,10 @@
                 sale = self.complaint.origin
                 if self.sale_lines:
                     sale_lines = [l.line for l in self.sale_lines]
-                    line2qty = {l.line.id: l.quantity
-                        if l.quantity is not None else l.line.quantity
-                        for l in self.sale_lines}
-                    line2price = {l.line.id: l.unit_price
-                        if l.unit_price is not None else l.line.unit_price
-                        for l in self.sale_lines}
+                    line2qty = {
+                        l.line.id: l.get_quantity() for l in self.sale_lines}
+                    line2price = {
+                        l.line.id: l.get_unit_price() for l in self.sale_lines}
                     default['quantity'] = lambda o: line2qty.get(o['id'])
                     default['unit_price'] = lambda o: line2price.get(o['id'])
                 else:
@@ -569,6 +635,16 @@
         "Unit Price", digits=price_digits, states=_states, depends=_depends,
         help='Leave empty for the same price.')
 
+    amount = fields.Function(fields.Numeric(
+            "Amount", digits=(16, Eval('currency_digits', 2)),
+            depends=['currency_digits']),
+        'on_change_with_amount')
+    currency = fields.Function(fields.Many2One(
+            'currency.currency', "Currency"),
+        'on_change_with_currency')
+    currency_digits = fields.Function(fields.Integer("Currency Digits"),
+        'on_change_with_currency_digits')
+
     complaint_state = fields.Function(
         fields.Selection('get_complaint_states', "Complaint State"),
         'on_change_with_complaint_state')
@@ -582,6 +658,31 @@
     def on_change_with_unit_digits(self, name=None):
         raise NotImplementedError
 
+    @fields.depends('currency', methods=['get_quantity', 'get_unit_price'])
+    def on_change_with_amount(self, name=None):
+        quantity = self.get_quantity() or 0
+        unit_price = self.get_unit_price() or Decimal(0)
+        amount = Decimal(str(quantity)) * unit_price
+        if self.currency:
+            amount = self.currency.round(amount)
+        return amount
+
+    def get_quantity(self):
+        raise NotImplementedError
+
+    def get_unit_price(self):
+        raise NotImplementedError
+
+    @fields.depends('action', '_parent_action.currency')
+    def on_change_with_currency(self, name=None):
+        if self.action and self.action.currency:
+            return self.action.currency.id
+
+    @fields.depends('action', '_parent_action.currency')
+    def on_change_with_currency_digits(self, name=None):
+        if self.action and self.action.currency:
+            return self.action.currency.digits
+
     @classmethod
     def get_complaint_states(cls):
         pool = Pool()
@@ -624,6 +725,20 @@
         if self.line:
             return self.line.unit.digits
 
+    @fields.depends('quantity', 'line')
+    def get_quantity(self):
+        if self.quantity is not None:
+            return self.quantity
+        elif self.line:
+            return self.line.quantity
+
+    @fields.depends('unit_price', 'line')
+    def get_unit_price(self):
+        if self.unit_price is not None:
+            return self.unit_price
+        elif self.line:
+            return self.line.unit_price
+
 
 class Action_InvoiceLine(_Action_Line, ModelView, ModelSQL):
     'Customer Complaint Action - Invoice Line'
@@ -647,3 +762,17 @@
     def on_change_with_unit_digits(self, name=None):
         if self.line:
             return self.line.unit.digits
+
+    @fields.depends('quantity', 'line')
+    def get_quantity(self):
+        if self.quantity is not None:
+            return self.quantity
+        elif self.line:
+            return self.line.quantity
+
+    @fields.depends('unit_price', 'line')
+    def get_unit_price(self):
+        if self.unit_price is not None:
+            return self.unit_price
+        elif self.line:
+            return self.line.unit_price
diff -r 5a1c7bd33adb -r 2fb80252d8f4 tests/scenario_sale_complaint.rst
--- a/tests/scenario_sale_complaint.rst Sat Dec 19 17:08:46 2020 +0100
+++ b/tests/scenario_sale_complaint.rst Sat Dec 26 23:58:50 2020 +0100
@@ -106,6 +106,8 @@
     >>> sale_line.quantity = 2
     >>> sale.click('quote')
     >>> sale.click('confirm')
+    >>> sale.untaxed_amount
+    Decimal('50.00')
 
 Post the invoice::
 
@@ -121,6 +123,8 @@
     >>> complaint.origin = sale
     >>> action = complaint.actions.new()
     >>> action.action = 'sale_return'
+    >>> action.amount
+    Decimal('50.00')
     >>> complaint.save()
     >>> complaint.state
     'draft'
@@ -152,6 +156,8 @@
     >>> sale_line.unit_price = Decimal('5')
     >>> sale_line = action.sale_lines.new()
     >>> sale_line.line = sale.lines[1]
+    >>> action.amount
+    Decimal('25.00')
     >>> complaint.save()
     >>> complaint.state
     'draft'
@@ -179,6 +185,8 @@
     >>> action = complaint.actions.new()
     >>> action.action = 'sale_return'
     >>> action.quantity = 1
+    >>> action.amount
+    Decimal('10.00')
     >>> complaint.click('wait')
     >>> complaint.click('approve')
     >>> complaint.state
@@ -197,6 +205,8 @@
     >>> complaint.origin = invoice
     >>> action = complaint.actions.new()
     >>> action.action = 'credit_note'
+    >>> action.amount
+    Decimal('50.00')
     >>> complaint.click('wait')
     >>> complaint.click('approve')
     >>> complaint.state
@@ -225,6 +235,8 @@
     >>> invoice_line = action.invoice_lines.new()
     >>> invoice_line.line = invoice.lines[1]
     >>> invoice_line.quantity = 1
+    >>> action.amount
+    Decimal('15.00')
     >>> complaint.click('wait')
     >>> complaint.click('approve')
     >>> complaint.state
@@ -249,6 +261,8 @@
     >>> action = complaint.actions.new()
     >>> action.action = 'credit_note'
     >>> action.quantity = 1
+    >>> action.amount
+    Decimal('10.00')
     >>> complaint.click('wait')
     >>> complaint.click('approve')
     >>> complaint.state
diff -r 5a1c7bd33adb -r 2fb80252d8f4 view/action_form.xml
--- a/view/action_form.xml      Sat Dec 19 17:08:46 2020 +0100
+++ b/view/action_form.xml      Sat Dec 26 23:58:50 2020 +0100
@@ -1,21 +1,23 @@
 <?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>
+<form col="6">
     <label name="complaint"/>
-    <field name="complaint" colspan="3"/>
+    <field name="complaint" colspan="5"/>
+
     <label name="action"/>
     <field name="action"/>
     <label name="result"/>
     <field name="result"/>
-    <field name="sale_lines" colspan="4" product="line"/>
-    <field name="invoice_lines" colspan="4" product="line"/>
-    <group colspan="4" col="6" id="line">
-        <label name="quantity"/>
-        <field name="quantity"/>
-        <label name="unit"/>
-        <field name="unit"/>
-        <label name="unit_price"/>
-        <field name="unit_price"/>
-    </group>
+
+    <field name="sale_lines" colspan="6" product="line"/>
+
+    <field name="invoice_lines" colspan="6" product="line"/>
+
+    <label name="quantity"/>
+    <field name="quantity" symbol="unit"/>
+    <label name="unit_price"/>
+    <field name="unit_price" symbol="currency"/>
+    <label name="amount"/>
+    <field name="amount" symbol="currency"/>
 </form>
diff -r 5a1c7bd33adb -r 2fb80252d8f4 view/action_line_form.xml
--- a/view/action_line_form.xml Sat Dec 19 17:08:46 2020 +0100
+++ b/view/action_line_form.xml Sat Dec 26 23:58:50 2020 +0100
@@ -1,15 +1,16 @@
 <?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>
+<form col="6">
     <label name="line"/>
     <field name="line"/>
     <label name="action"/>
     <field name="action"/>
+    <newline/>
+
     <label name="quantity"/>
-    <field name="quantity"/>
-    <label name="unit"/>
-    <field name="unit"/>
+    <field name="quantity" symbol="unit"/>
     <label name="unit_price"/>
-    <field name="unit_price"/>
+    <field name="unit_price" symbol="currency"/>
+    <field name="amount" symbol="currency"/>
 </form>
diff -r 5a1c7bd33adb -r 2fb80252d8f4 view/action_line_list.xml
--- a/view/action_line_list.xml Sat Dec 19 17:08:46 2020 +0100
+++ b/view/action_line_list.xml Sat Dec 26 23:58:50 2020 +0100
@@ -4,7 +4,7 @@
 <tree editable="1">
     <field name="line"/>
     <field name="action"/>
-    <field name="quantity"/>
-    <field name="unit"/>
-    <field name="unit_price"/>
+    <field name="quantity" symbol="unit"/>
+    <field name="unit_price" symbol="currency"/>
+    <field name="amount" symbol="currency"/>
 </tree>
diff -r 5a1c7bd33adb -r 2fb80252d8f4 view/action_list.xml
--- a/view/action_list.xml      Sat Dec 19 17:08:46 2020 +0100
+++ b/view/action_list.xml      Sat Dec 26 23:58:50 2020 +0100
@@ -4,5 +4,6 @@
 <tree>
     <field name="complaint" expand="1"/>
     <field name="action" expand="1"/>
+    <field name="amount" symbol="currency" sum="Amount"/>
     <field name="result"/>
 </tree>

Reply via email to