Skip to content

Commit

Permalink
l10n_fr_chorus_account: improve error management
Browse files Browse the repository at this point in the history
add mail activity when chorus reject an invoice
add filter for invoice in error and invoice to send
reject all invoice without chorus identifier
add num/sum of invoice in error in the dashboard
  • Loading branch information
sebastienbeau committed Sep 29, 2024
1 parent 95c94f7 commit a3e89bc
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 21 deletions.
2 changes: 2 additions & 0 deletions l10n_fr_chorus_account/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
"data/transmit_method.xml",
"data/cron.xml",
"data/mail_template.xml",
"data/mail_activity_data.xml",
"wizard/account_invoice_chorus_send_view.xml",
"views/account_journal_dashboard_view.xml",
"views/chorus_flow.xml",
"views/chorus_partner_service.xml",
"views/partner.xml",
Expand Down
11 changes: 11 additions & 0 deletions l10n_fr_chorus_account/data/mail_activity_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>

<record id="mail_activity_type_chorus_error" model="mail.activity.type">
<field name="name">Traité l'erreur de transmission Chorus</field>
<field name="icon">fa-exclamation-triangle</field>
<field name="sequence">3</field>
<field name="res_model_id" ref="account.model_account_move" />
</record>

</odoo>
1 change: 1 addition & 0 deletions l10n_fr_chorus_account/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
from . import company
from . import config_settings
from . import account_move
from . import account_journal
45 changes: 45 additions & 0 deletions l10n_fr_chorus_account/models/account_journal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2024 Akretion (https://www.akretion.com).
# @author Sébastien BEAU <sebastien.beau@akretion.com>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).


from odoo import models
from odoo.tools.misc import formatLang


class AccountJournal(models.Model):
_inherit = "account.journal"

def get_journal_dashboard_datas(self):
number_chorus_error = sum_chorus_error = 0
res = super().get_journal_dashboard_datas()
if self.type == "sale":
data = self.env["account.move"].read_group(
[
("journal_id", "=", self.id),
(
"activity_type_id",
"=",
self.env.ref(
"l10n_fr_chorus_account.mail_activity_type_chorus_error"
).id,
),
],
["amount_total_signed"],
["journal_id"],
)
if data:
number_chorus_error = data[0]["journal_id_count"]
currency = self.currency_id or self.company_id.currency_id
sum_chorus_error = formatLang(
self.env,
currency.round(data[0]["amount_total_signed"]),
currency_obj=currency,
)
res.update(
{
"number_chorus_error": number_chorus_error,
"sum_chorus_error": sum_chorus_error,
}
)
return res
15 changes: 15 additions & 0 deletions l10n_fr_chorus_account/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,3 +493,18 @@ def chorus_api_check_commitment_number(
)
logger.warning("Commitment number %s not found in Chorus Pro.", order_ref)
return False

def _chorus_set_as_rejected(self, error):
self.ensure_one()
self.chorus_flow_id = None
self.activity_schedule(
"l10n_fr_chorus_account.mail_activity_type_chorus_error",
note=_(
"This invoice has been <b>rejected by Chorus Pro</b> "
"for the following reason:<br/><i>%s</i><br/>"
"You should fix the error and send this invoice to "
"Chorus Pro again."
)
% error,
user_id=self.create_uid.id,
)
48 changes: 27 additions & 21 deletions l10n_fr_chorus_account/models/chorus_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# @author: Alexis de Lattre <alexis.delattre@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import json
import logging

from odoo import _, api, fields, models
Expand Down Expand Up @@ -54,6 +55,7 @@ class ChorusFlow(models.Model):
readonly=True,
help="Invoices in the flow after potential rejections",
)
chorus_response = fields.Text()

@api.depends("status")
def _compute_status_display(self):
Expand Down Expand Up @@ -119,30 +121,10 @@ def chorus_api_consulter_cr(self, api_params, session=None):
error.get("libelleErreurDP"),
)
)
# If we can identify the invoice in Odoo, we detach it
# from the flow, so that it can be fixed and re-transmitted
if error.get("numeroDP"):
invoice = self.env["account.move"].search(
[
("company_id", "=", self.company_id.id),
("name", "=", error["numeroDP"]),
],
limit=1,
)
if invoice:
invoice.message_post(
body=_(
"This invoice has been <b>rejected by Chorus Pro</b> "
"for the following reason:<br/><i>%s</i><br/>"
"You should fix the error and send this invoice to "
"Chorus Pro again."
)
% error.get("libelleErreurDP")
)
invoice.sudo().write({"chorus_flow_id": False})
res = {
"status": answer.get("etatCourantDepotFlux"),
"notes": notes or answer.get("libelle"),
"chorus_response": json.dumps(answer),
}
return (res, session)

Expand Down Expand Up @@ -265,8 +247,32 @@ def get_invoice_identifiers(self):
for inv in flow.invoice_ids:
if inv.name in invnum2chorus:
inv.write(invnum2chorus[inv.name])
flow._process_rejected_invoices()
logger.info("End of the retrieval of chorus invoice identifiers")

def _process_rejected_invoices(self):
self.ensure_one()
if self.chorus_response:
inv2errors = {
error["numeroDP"]: error.get("libelleErreurDP")
for error in json.loads(self.chorus_response)["listeErreurDP"]
}
else:
inv2errors = {}
for invoice in self.invoice_ids:
if not invoice.chorus_identifier:
# All the infoice without any chorus_identifier are considered as
# rejected.
# Most of the time chorus have sucessfully returned the right error
# message in the response when calling flow update state
# but sometime it doesn't ;) so missing invoice are we always rejected,
# invoice without error message will have a generic error message
invoice._chorus_set_as_rejected(
inv2errors.get(
invoice.name, _("Internal Chorus Error, please Resumit")
)
)

@api.model
def chorus_cron(self):
self = self.with_context(chorus_raise_if_ko=False)
Expand Down
29 changes: 29 additions & 0 deletions l10n_fr_chorus_account/views/account_journal_dashboard_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>

<record id="account_journal_dashboard_kanban_view" model="ir.ui.view">
<field name="model">account.journal</field>
<field name="inherit_id" ref="account.account_journal_dashboard_kanban_view" />
<field name="arch" type="xml">
<div class="row" t-if="dashboard.number_waiting" position="after">
<div class="row text-danger" t-if="dashboard.number_chorus_error">
<div class="col overflow-hidden text-left">
<a
type="object"
name="open_action"
class="text-danger"
context="{'search_default_chorus_error': 1}"
id="account_dashboard_error_chorus"
>
<t t-esc="dashboard.number_chorus_error" /> Error Chorus
</a>
</div>
<div class="col-auto text-right">
<span><t t-esc="dashboard.sum_chorus_error" /></span>
</div>
</div>
</div>
</field>
</record>

</odoo>
20 changes: 20 additions & 0 deletions l10n_fr_chorus_account/views/account_move.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_account_invoice_filter" />
<field name="arch" type="xml">
<filter name="to_check" position="after">
<separator />
<filter
string="Error Chorus"
name="chorus_error"
domain="[
('activity_type_id', '=', %(mail_activity_type_chorus_error)d)
]"
/>
<filter
string="To send to Chorus"
name="chorus_to_send"
domain="[
('transmit_method_id.code', '=', 'fr-chorus'),
('chorus_flow_id', '=', False),
('payment_state', '=', 'not_paid'),
('state', '=', 'posted'),
]"
/>
</filter>
<filter name="status" position="after">
<filter
string="Chorus Status"
Expand Down

0 comments on commit a3e89bc

Please sign in to comment.