diff --git a/models/account_move.py b/models/account_move.py index e6b28565ec77a3211fd3125524afb609e65c26f9..0cdbdf72e13b516d7a308bc428f44fd1d0ffa7c3 100644 --- a/models/account_move.py +++ b/models/account_move.py @@ -7,6 +7,9 @@ from odoo import api, fields, models class AccountMove(models.Model): _inherit = "account.move" + # ------------------------------------------------------ + # Fields declaration + # ------------------------------------------------------ purchasing_centre_id = fields.Many2one( comodel_name="res.partner", string="Centrale d'achat", @@ -18,10 +21,11 @@ class AccountMove(models.Model): compute="_compute_membership_number", store=True, ) - - # ------------------------------------------------------ - # Fields declaration - # ------------------------------------------------------ + payment_balance_date = fields.Date( + string="Date de solde de paiement", + compute="_compute_payment_balance_date", + store=True, + ) # ------------------------------------------------------ # SQL Constraints @@ -70,6 +74,29 @@ class AccountMove(models.Model): else: move.membership_number = False + @api.depends("payment_state") + def _compute_payment_balance_date(self): + """ + Calcule la dernière date de paiement pour les factures et avoirs clients à l'état + "payé" afin de conserver la date de solde de la facture. + """ + for invoice in self: + invoice.payment_balance_date = False + if ( + invoice.move_type in ["out_invoice", "out_refund"] + and invoice.payment_state == "paid" + ): + payment_dates = invoice._get_reconciled_payments().mapped("date") + statement_dates = invoice._get_reconciled_statement_lines().mapped( + "date" + ) + reversed_dates = invoice._get_reconciled_invoices().mapped( + "invoice_date" + ) + all_dates = payment_dates + statement_dates + reversed_dates + if all_dates: + invoice.payment_balance_date = max(all_dates) + # ------------------------------------------------------ # Onchange / Constraints # ------------------------------------------------------ diff --git a/views/account_move.xml b/views/account_move.xml index d5bef18b637eeb9a83c5adb14a41035dd30f8256..d3729cee4bc89ea94fbf62f845c0e15dea976cc8 100644 --- a/views/account_move.xml +++ b/views/account_move.xml @@ -18,6 +18,9 @@ attrs="{'invisible': [('move_type', 'not in', ('out_invoice', 'out_refund'))]}" /> </xpath> + <xpath expr="//field[@name='to_check']" position="after"> + <field name="payment_balance_date" /> + </xpath> </field> </record> @@ -31,39 +34,44 @@ <field name="purchasing_centre_id" optional="hide" /> <field name="membership_number" optional="hide" /> </xpath> + <xpath expr="//field[@name='payment_state']" position="after"> + <field name="payment_balance_date" optional="hide" /> + </xpath> </field> </record> <!-- Inherit Search View --> <record id="emgidi_view_account_invoice_filter" model="ir.ui.view"> - <field name="name" /> - <field name="model">account.move</field> - <field name="inherit_id" ref="account.view_account_invoice_filter" /> - <field name="arch" type="xml"> - <xpath expr="//field[@name='invoice_user_id']" position="after"> - <field name="purchasing_centre_id" /> - <field name="membership_number" /> - </xpath> + <field name="name" /> + <field name="model">account.move</field> + <field name="inherit_id" ref="account.view_account_invoice_filter" /> + <field name="arch" type="xml"> + <xpath expr="//field[@name='invoice_user_id']" position="after"> + <field name="purchasing_centre_id" /> + <field name="membership_number" /> + </xpath> - <xpath expr="//filter[@name='due_date']" position="after"> - <separator /> - <filter + <xpath expr="//filter[@name='due_date']" position="after"> + <separator /> + <filter string="Clients centrales" name="customer_purchasing_centre" domain="[('purchasing_centre_id', '!=', False)]" /> - </xpath> + <separator /> + <filter name="payment_balance_date" date="payment_balance_date" /> + </xpath> - <xpath expr="//filter[@name='status']" position="after"> - <filter + <xpath expr="//filter[@name='status']" position="after"> + <filter name="group_purchasing_centre" string="Centrale d'achat" context="{'group_by': 'purchasing_centre_id'}" /> - </xpath> + </xpath> - </field> - </record> + </field> + </record> <!-- Inherit Action --> <record id="account.action_move_out_invoice_type" model="ir.actions.act_window">