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 ddddf30..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,20 @@ 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 ( + elif inv.state == "draft" and ( float_compare( available_amount, amount, precision_digits=precision ) @@ -312,6 +316,29 @@ def _compute_subcontractor_state(self): # noqa: C901 ) 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 + ): + 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" else: account_reasons.append( """Le solde payé du compte analytique %s est suffisant. """