From a8102d513f67b9b22a93faa30098dcaa862b8c64 Mon Sep 17 00:00:00 2001 From: laurentbarontini Date: Wed, 17 Jun 2026 00:08:52 +0200 Subject: [PATCH] Fee CN/DN --- modules/purchase/purchase.py | 4 ++ modules/purchase_trade/fee.py | 38 +++++++++++++++---- modules/purchase_trade/view/fee_form.xml | 2 + .../purchase_trade/view/fee_tree_sequence.xml | 1 + .../view/fee_tree_sequence2.xml | 1 + 5 files changed, 38 insertions(+), 8 deletions(-) diff --git a/modules/purchase/purchase.py b/modules/purchase/purchase.py index 4f9cd0f..39b356a 100755 --- a/modules/purchase/purchase.py +++ b/modules/purchase/purchase.py @@ -1017,6 +1017,9 @@ class Purchase( logger.info("PROCESS_INVOICE:%s",action) invoice = purchase.create_invoice(lots,action) if not invoice: + logger.info( + "PROCESS_INVOICE_NO_INVOICE: purchase=%s action=%s", + getattr(purchase, 'id', None), action) continue if action == 'prov': invoice.reference = 'Provisional' @@ -1028,6 +1031,7 @@ class Purchase( if invoices: cls._save_invoice(invoices,prepayment) + return invoices @classmethod def _save_invoice(cls, invoices,prepayment=None): diff --git a/modules/purchase_trade/fee.py b/modules/purchase_trade/fee.py index 4ae6f3d..90f107a 100755 --- a/modules/purchase_trade/fee.py +++ b/modules/purchase_trade/fee.py @@ -107,8 +107,9 @@ class Fee(ModelSQL,ModelView): ('invoiced', 'Invoiced'), ], string='State', readonly=True) - fee_landed_cost = fields.Function(fields.Boolean("Inventory"),'get_landed_status') - inv = fields.Function(fields.Many2One('account.invoice',"Invoice"),'get_invoice') + fee_landed_cost = fields.Function(fields.Boolean("Inventory"),'get_landed_status') + inv = fields.Function(fields.Many2One('account.invoice',"Invoice"),'get_invoice') + dn_cn = fields.Many2One('account.invoice', "DN/CN", readonly=True) weight_type = fields.Selection([ ('net', 'Net'), @@ -371,6 +372,7 @@ class Fee(ModelSQL,ModelView): Warning = Pool().get('res.user.warning') fees_to_invoice = [] for fee in fees: + was_invoiced = fee.state == 'invoiced' if fee.state == 'invoiced': warning_name = Warning.format( "Fee already invoiced", [fee]) @@ -385,7 +387,28 @@ class Fee(ModelSQL,ModelView): if fee.purchase: fl = FeeLots.search([('fee','=',fee.id)]) logger.info("PROCESS_FROM_FEE:%s",fl) - Purchase._process_invoice([fee.purchase],[e.lot for e in fl],'service') + fee.adjust_purchase_values() + invoices = Purchase._process_invoice( + [fee.purchase], [e.lot for e in fl], 'service') + invoice = invoices.get(fee.purchase) if invoices else None + logger.info( + "PROCESS_FROM_FEE_RESULT: fee=%s purchase=%s invoice=%s " + "was_invoiced=%s", + getattr(fee, 'id', None), getattr(fee.purchase, 'id', None), + getattr(invoice, 'id', None), was_invoiced) + if not invoice: + if was_invoiced: + raise UserError( + "No debit note or credit note was created for " + "fee %s. The current fee values may be identical " + "to the last invoiced values." % getattr( + fee, 'id', None)) + raise UserError( + "No invoice was created for fee %s. Please check " + "that the fee has linked lots and invoiceable values." + % getattr(fee, 'id', None)) + if was_invoiced: + cls.write([fee], {'dn_cn': invoice.id}) fees_to_invoice.append(fee) else: raise UserError( @@ -434,9 +457,9 @@ class Fee(ModelSQL,ModelView): ('quantity', '>', 0), ('invoice.state', '!=', 'cancelled'), ], order=[ - ('invoice.invoice_date', 'DESC'), - ('invoice.id', 'DESC'), - ('id', 'DESC'), + ('invoice.invoice_date', 'ASC'), + ('invoice.id', 'ASC'), + ('id', 'ASC'), ], limit=1) if invoice_lines: return invoice_lines[0].invoice @@ -450,8 +473,7 @@ class Fee(ModelSQL,ModelView): invoices, key=lambda invoice: ( invoice.invoice_date or datetime.date.min, - invoice.id or 0), - reverse=True)[0] + invoice.id or 0))[0] def get_landed_status(self,name): if self.product: diff --git a/modules/purchase_trade/view/fee_form.xml b/modules/purchase_trade/view/fee_form.xml index 2dda642..e46d558 100755 --- a/modules/purchase_trade/view/fee_form.xml +++ b/modules/purchase_trade/view/fee_form.xml @@ -17,6 +17,8 @@