changeset a517ee697a57 in modules/commission:default
details: 
https://hg.tryton.org/modules/commission?cmd=changeset&node=a517ee697a57
description:
        Set commission date when invoice is cancelled

        issue11552
        review431221003
diffstat:

 invoice.py                                          |   23 ++-
 tests/scenario_commission_credit_posted_invoice.rst |  131 ++++++++++++++++++++
 2 files changed, 147 insertions(+), 7 deletions(-)

diffs (185 lines):

diff -r 420e1fce3300 -r a517ee697a57 invoice.py
--- a/invoice.py        Wed Jun 15 22:01:01 2022 +0200
+++ b/invoice.py        Thu Jun 16 14:35:48 2022 +0200
@@ -51,14 +51,10 @@
         return all_commissions
 
     @classmethod
-    @Workflow.transition('paid')
-    def paid(cls, invoices):
+    def set_commissions_date(cls, invoices):
         pool = Pool()
         Date = pool.get('ir.date')
         Commission = pool.get('commission')
-
-        super(Invoice, cls).paid(invoices)
-
         date2commissions = defaultdict(list)
         for company, c_invoices in groupby(invoices, key=lambda i: i.company):
             with Transaction().set_context(company=company.id):
@@ -82,15 +78,28 @@
             Commission.write(*to_write)
 
     @classmethod
+    @Workflow.transition('paid')
+    def paid(cls, invoices):
+        super().paid(invoices)
+        cls.set_commissions_date(invoices)
+
+    @classmethod
     @ModelView.button
     @Workflow.transition('cancelled')
     def cancel(cls, invoices):
         pool = Pool()
         Commission = pool.get('commission')
 
-        invoices_to_revert_commission = [x for x in invoices if not x.move]
+        invoices_to_revert_commission = []
+        invoices_to_set_date = []
+        for invoice in invoices:
+            if invoice.move:
+                invoices_to_set_date.append(invoice)
+            else:
+                invoices_to_revert_commission.append(invoice)
 
-        super(Invoice, cls).cancel(invoices)
+        super().cancel(invoices)
+        cls.set_commissions_date(invoices_to_set_date)
 
         to_delete = []
         to_save = []
diff -r 420e1fce3300 -r a517ee697a57 
tests/scenario_commission_credit_posted_invoice.rst
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/scenario_commission_credit_posted_invoice.rst       Thu Jun 16 
14:35:48 2022 +0200
@@ -0,0 +1,131 @@
+=========================================
+Commission Credit Posted Invoice Scenario
+=========================================
+
+Imports::
+
+    >>> 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)
+
+Activate modules::
+
+    >>> config = activate_modules('commission')
+
+    >>> Agent = Model.get('commission.agent')
+    >>> Commission = Model.get('commission')
+    >>> Invoice = Model.get('account.invoice')
+    >>> Party = Model.get('party.party')
+    >>> Plan = Model.get('commission.plan')
+    >>> ProductCategory = Model.get('product.category')
+    >>> Template = Model.get('product.template')
+    >>> Uom = Model.get('product.uom')
+
+Create company::
+
+    >>> _ = create_company()
+    >>> company = get_company()
+
+Create fiscal year::
+
+    >>> fiscalyear = set_fiscalyear_invoice_sequences(
+    ...     create_fiscalyear(company))
+    >>> fiscalyear.click('create_period')
+
+Create chart of accounts::
+
+    >>> _ = create_chart(company)
+    >>> accounts = get_accounts(company)
+
+Create customer::
+
+    >>> customer = Party(name='Customer')
+    >>> customer.save()
+
+Create account category::
+
+    >>> account_category = ProductCategory(name="Account Category")
+    >>> account_category.accounting = True
+    >>> account_category.account_expense = accounts['expense']
+    >>> account_category.account_revenue = accounts['revenue']
+    >>> account_category.save()
+
+Create commission product::
+
+    >>> unit, = Uom.find([('name', '=', 'Unit')])
+    >>> template = Template()
+    >>> template.name = 'Commission'
+    >>> template.default_uom = unit
+    >>> template.type = 'service'
+    >>> template.list_price = Decimal(0)
+    >>> template.account_category = account_category
+    >>> template.save()
+    >>> commission_product, = template.products
+
+Create commission plan::
+
+    >>> plan = Plan(name='Plan')
+    >>> plan.commission_product = commission_product
+    >>> plan.commission_method = 'payment'
+    >>> line = plan.lines.new()
+    >>> line.formula = 'amount * 0.1'
+    >>> plan.save()
+
+Create agent::
+
+    >>> agent_party = Party(name='Agent')
+    >>> agent_party.save()
+    >>> agent = Agent(party=agent_party)
+    >>> agent.type_ = 'agent'
+    >>> agent.plan = plan
+    >>> agent.currency = company.currency
+    >>> agent.save()
+
+Create product sold::
+
+    >>> template = Template()
+    >>> template.name = 'Product'
+    >>> template.default_uom = unit
+    >>> template.type = 'service'
+    >>> template.list_price = Decimal(100)
+    >>> template.account_category = account_category
+    >>> template.save()
+    >>> product, = template.products
+
+Create invoice and credit it before paying::
+
+    >>> invoice = Invoice()
+    >>> invoice.party = customer
+    >>> invoice.agent = agent
+    >>> line = invoice.lines.new()
+    >>> line.product = product
+    >>> line.quantity = 1
+    >>> line.unit_price = Decimal(100)
+    >>> invoice.click('post')
+    >>> line, = invoice.lines
+    >>> commission, = line.commissions
+    >>> bool(commission.date)
+    False
+    >>> commission.amount
+    Decimal('10.0000')
+    >>> credit = Wizard('account.invoice.credit', [invoice])
+    >>> credit.execute('credit')
+    >>> credit_note, = credit.actions[0]
+    >>> credit_note.state = 'paid'
+    >>> credit_line, = credit_note.lines
+    >>> credit_commission, = Commission.find([
+    ...     ('id', '!=', commission.id),
+    ...     ])
+    >>> bool(credit_commission.date)
+    True
+    >>> credit_commission.amount
+    Decimal('-10.0000')
+    >>> commission.reload()
+    >>> bool(commission.date)
+    True

Reply via email to