[FIX] account_peppol: hide peppol_move_state on vendor bills, refunds

- `peppol_move_state` is only useful when sending an invoice/credit note.
  Hide this field from the form view and the tree view for vendor bills and refunds
- when getting new documents from the IAP, ignore messages with peppol state
  `error` as these are not useful for the users.
  Also, if there is an error when creating an invoice from the attachment,
  the vendor bill should still have `peppol_move_state` set to `done`,
  because we have received the vendor bill after all.
  Users can fill out missing details if necessary, by looking at the attachment.

Part-of: odoo/odoo#139718
This commit is contained in:
aliya
2023-10-25 20:56:05 +00:00
parent 8bea49fef6
commit d5a2a802c9
2 changed files with 26 additions and 33 deletions
@@ -77,12 +77,21 @@ class AccountEdiProxyClientUser(models.Model):
return super()._get_proxy_identification(company, proxy_type)
def _peppol_get_new_documents(self):
params = {
'domain': {
'direction': 'incoming',
'errors': False,
}
}
for edi_user in self:
proxy_acks = []
params['domain']['receiver_identifier'] = edi_user.edi_identification
try:
# request all messages that haven't been acknowledged
messages = edi_user._make_request(
f"{edi_user._get_server_url()}/api/peppol/1/get_all_documents")
url=f"{edi_user._get_server_url()}/api/peppol/1/get_all_documents",
params=params,
)
except AccountEdiProxyError as e:
_logger.error(
'Error while receiving the document from Peppol Proxy: %s', e.message)
@@ -91,8 +100,6 @@ class AccountEdiProxyClientUser(models.Model):
message_uuids = [
message['uuid']
for message in messages.get('messages', [])
if message['direction'] == 'incoming'
and message['receiver'] == edi_user.edi_identification
]
if not message_uuids:
continue
@@ -105,20 +112,6 @@ class AccountEdiProxyClientUser(models.Model):
)
for uuid, content in all_messages.items():
error_move_vals = {
'move_type': 'in_invoice',
'peppol_move_state': 'error',
'company_id': company.id,
'extract_can_show_send_button': False,
'peppol_message_uuid': uuid,
}
if content.get('error'):
# in this case there is no attachment that we could add to the account move
move = self.env['account.move'].create(error_move_vals)
move._message_log(body=_('Error when receiving via Peppol: %s', content['error']['message']))
proxy_acks.append(uuid)
continue
enc_key = content["enc_key"]
document_content = content["document"]
filename = content["filename"] or 'attachment' # default to attachment, which should not usually happen
@@ -151,27 +144,27 @@ class AccountEdiProxyClientUser(models.Model):
default_peppol_message_uuid=uuid,
)\
._create_document_from_attachment(attachment.id)
if partner_endpoint:
move._message_log(body=_(
'Peppol document has been received successfully. Sender endpoint: %s', partner_endpoint))
else:
move._message_log(
body=_('Peppol document has been received successfully'))
# pylint: disable=broad-except
except Exception:
# if the invoice creation fails for any reason,
# we want to create an empty invoice with the attachment
move = self.env['account.move'].create(error_move_vals)
move = self.env['account.move'].create({
'move_type': 'in_invoice',
'peppol_move_state': 'done',
'company_id': company.id,
'extract_can_show_send_button': False,
'peppol_message_uuid': uuid,
})
attachment_vals.update({
'res_model': 'account.move',
'res_id': move.id,
})
self.env['ir.attachment'].create(attachment_vals)
if partner_endpoint:
move._message_log(body=_(
'Failed to import a Peppol document. Sender endpoint: %s', partner_endpoint))
else:
move._message_log(body=_('Failed to import a Peppol document.'))
if partner_endpoint:
move._message_log(body=_(
'Peppol document has been received successfully. Sender endpoint: %s', partner_endpoint))
else:
move._message_log(body=_('Peppol document has been received successfully'))
proxy_acks.append(uuid)
@@ -16,11 +16,10 @@
<xpath expr="//div[@name='journal_div']" position="after">
<label for="peppol_move_state"
invisible="not peppol_move_state or state == 'draft'"/>
invisible="not peppol_move_state or state == 'draft' or move_type in ('in_invoice', 'in_refund')"/>
<div name="peppol_div"
class="d-flex"
invisible="not peppol_move_state or state == 'draft'">
invisible="not peppol_move_state or state == 'draft' or move_type in ('in_invoice', 'in_refund')">
<field name="peppol_is_demo_uuid" invisible="1"/>
<field name="peppol_move_state" class="oe_inline"/>
<span class="mx-1" invisible="not peppol_is_demo_uuid"> (Demo)</span>
@@ -55,13 +54,14 @@
</field>
</record>
<!-- to be removed in master -->
<record id="account_peppol_view_in_invoice_bill_tree_inherit" model="ir.ui.view">
<field name="name">account.out.invoice.tree.inherit</field>
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_in_invoice_tree"/>
<field name="arch" type="xml">
<field name="state" position="before">
<field name="peppol_move_state" optional="hide"/>
<field name="peppol_move_state" invisible="1"/>
</field>
</field>
</record>