From 125d62fbb6800fa7834c1e07ad0393930ce7a4e6 Mon Sep 17 00:00:00 2001 From: Florian da Costa Date: Tue, 23 Apr 2024 18:29:53 +0200 Subject: [PATCH 1/2] [FIX] Fix not enough available amount msg for draft prepaid supplier invoice the draft invoice amount is already substracted from the available amount --- project_invoicing_subcontractor/models/account_move.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/project_invoicing_subcontractor/models/account_move.py b/project_invoicing_subcontractor/models/account_move.py index ddddf30..edaf69a 100644 --- a/project_invoicing_subcontractor/models/account_move.py +++ b/project_invoicing_subcontractor/models/account_move.py @@ -299,9 +299,7 @@ def _compute_subcontractor_state(self): # noqa: C901 ) color = "danger" elif ( - float_compare( - available_amount, amount, precision_digits=precision - ) + float_compare(available_amount, 0, precision_digits=precision) == -1 ): account_reasons.append( From f32b2e98a9e60602b35c41c9117fe963d141acc2 Mon Sep 17 00:00:00 2001 From: Florian da Costa Date: Fri, 26 Apr 2024 12:29:50 +0200 Subject: [PATCH 2/2] [FIX] Error message on prepaid invoices On draft invoice, make comparaison consistent, we need to compare what is available with the draft invoice On confirmed invoices, make a message with consistent amount. we deduce confirmed supplier invoices from available totals to avoid case where we have available amount > invoice but still display a message as there is not enough amount (case where others invoices are validated an have priority) Also deduce ongoing supplier prepaid invoice from available total on partner/project to show what is actually available once all is paid. Else we could have strange case where total of the project is lower than available --- .../models/account_analytic_account.py | 10 ++++- .../models/account_move.py | 39 ++++++++++++++++--- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/project_invoicing_subcontractor/models/account_analytic_account.py b/project_invoicing_subcontractor/models/account_analytic_account.py index 687aa65..d068fb1 100644 --- a/project_invoicing_subcontractor/models/account_analytic_account.py +++ b/project_invoicing_subcontractor/models/account_analytic_account.py @@ -17,9 +17,17 @@ def _compute_prepaid_amount(self): move_lines, paid_lines = account._prepaid_move_lines() total_amount = -sum(move_lines.mapped("amount_currency")) or 0.0 available_amount = -sum(paid_lines.mapped("amount_currency")) or 0.0 + not_paid_lines = move_lines - paid_lines + supplier_not_paid = not_paid_lines.filtered( + lambda line: line.amount_currency > 0.0 + ) + available_amount -= sum(supplier_not_paid.mapped("amount_currency")) account.prepaid_total_amount = total_amount + # this one is used for display/info, so we show what is really available + # as if all supplier invoices were paid. account.prepaid_available_amount = available_amount - # Keep available_amount without to_pay supplier invoices + # Keep available_amount without to_pay supplier invoices neither ongoing + # supplier invoices because it is used to make them to pay. account.available_amount = ( -sum( move_lines.filtered(lambda m: m.prepaid_is_paid).mapped( diff --git a/project_invoicing_subcontractor/models/account_move.py b/project_invoicing_subcontractor/models/account_move.py index edaf69a..a89001a 100644 --- a/project_invoicing_subcontractor/models/account_move.py +++ b/project_invoicing_subcontractor/models/account_move.py @@ -279,10 +279,8 @@ def _compute_subcontractor_state(self): # noqa: C901 ) break total_amount = analytic_account.prepaid_total_amount - available_amount = analytic_account.available_amount + available_amount = analytic_account.prepaid_available_amount if inv.state == "draft": - total_amount -= amount - available_amount -= amount other_draft_invoices = self.env["account.move.line"].search( [ ("parent_state", "=", "draft"), @@ -291,14 +289,45 @@ def _compute_subcontractor_state(self): # noqa: C901 ("move_id.move_type", "=", ["in_invoice", "in_refund"]), ] ) - if float_compare(total_amount, 0, precision_digits=precision) == -1: + if ( + inv.state == "draft" + and float_compare( + total_amount, amount, precision_digits=precision + ) + == -1 + ): account_reasons.append( - """Le solde du compte analytique %s est négatif %s. """ + """Le solde du compte analytique %s n'est pas suffisant : %s. """ """Il est necessaire de facturer le client.""" % (analytic_account.name, total_amount) ) color = "danger" + elif inv.state == "draft" and ( + float_compare( + available_amount, amount, precision_digits=precision + ) + == -1 + ): + account_reasons.append( + """Le solde payé du compte analytique %s est insuffisant %s. """ + """La facture sera payable une fois que le client aura reglé """ + """ses factures.""" + % (analytic_account.name, available_amount) + ) + if color != "red": + color = "info" elif ( + inv.state != "draft" + and float_compare(total_amount, 0, precision_digits=precision) + == -1 + ): + account_reasons.append( + """Le solde du compte analytique %s est négatif %s. """ + """Il est necessaire de facturer le client.""" + % (analytic_account.name, total_amount) + ) + color = "danger" + elif inv.state != "draft" and ( float_compare(available_amount, 0, precision_digits=precision) == -1 ):