| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
|
| 3 |
import base64 |
| 4 |
import json |
| 5 |
|
| 6 |
from odoo import SUPERUSER_ID, api, models |
| 7 |
|
| 8 |
|
| 9 |
class AccountMoveSend(models.AbstractModel): |
| 10 |
_inherit = 'account.move.send' |
| 11 |
|
| 12 |
@api.model |
| 13 |
def _is_tw_edi_applicable(self, move): |
| 14 |
return (move.move_type == 'out_invoice' |
| 15 |
and ('debit_origin_id' not in self.env['account.move']._fields or not move.debit_origin_id) |
| 16 |
and move.state == 'posted' |
| 17 |
and move.company_id.country_id.code == 'TW' |
| 18 |
and not move.l10n_tw_edi_state |
| 19 |
and move.company_id._is_ecpay_enabled()) |
| 20 |
|
| 21 |
@api.model |
| 22 |
def _is_tw_edi_issue_allowance_applicable(self, move): |
| 23 |
return (move.move_type == 'out_refund' |
| 24 |
and move.state == 'posted' |
| 25 |
and move.company_id.country_id.code == 'TW' |
| 26 |
and move.reversed_entry_id |
| 27 |
and move.reversed_entry_id.l10n_tw_edi_ecpay_invoice_id |
| 28 |
and not move.l10n_tw_edi_refund_invoice_number |
| 29 |
and not move.l10n_tw_edi_refund_state |
| 30 |
and move.company_id._is_ecpay_enabled()) |
| 31 |
|
| 32 |
def _get_all_extra_edis(self): |
| 33 |
# EXTENDS 'account' |
| 34 |
res = super()._get_all_extra_edis() |
| 35 |
res.update({ |
| 36 |
'tw_ecpay_send': {'label': self.env._("Send to Ecpay"), 'is_applicable': self._is_tw_edi_applicable}, |
| 37 |
'tw_ecpay_issue_allowance': {'label': self.env._("Send to Ecpay (Issue Allowance)"), |
| 38 |
'is_applicable': self._is_tw_edi_issue_allowance_applicable}, |
| 39 |
}) |
| 40 |
return res |
| 41 |
|
| 42 |
# ------------------------------------------------------------------------- |
| 43 |
# SENDING METHODS |
| 44 |
# ------------------------------------------------------------------------- |
| 45 |
|
| 46 |
@api.model |
| 47 |
def _l10n_tw_edi_generate_ecpay_json(self, invoice, invoice_data): |
| 48 |
need_file = ( |
| 49 |
((invoice_data['invoice_edi_format'] == 'tw_ecpay' |
| 50 |
or 'manual' in invoice_data['sending_methods']) |
| 51 |
and invoice.company_id._is_ecpay_enabled()) |
| 52 |
or 'tw_ecpay_send' in invoice_data['extra_edis'] |
| 53 |
or 'tw_ecpay_issue_allowance' in invoice_data['extra_edis'] |
| 54 |
) |
| 55 |
file_name = '' |
| 56 |
json_content = {} |
| 57 |
# It should always be generated when sending and downloading. |
| 58 |
if need_file: |
| 59 |
if 'tw_ecpay_send' in invoice_data['extra_edis']: |
| 60 |
json_content = invoice._l10n_tw_edi_generate_invoice_json() |
| 61 |
file_name = f'{invoice.name.replace("/", "_")}_ecpay.json' |
| 62 |
elif 'tw_ecpay_issue_allowance' in invoice_data['extra_edis']: |
| 63 |
json_content = invoice._l10n_tw_edi_generate_issue_allowance_json() |
| 64 |
file_name = f'{invoice.name.replace("/", "_")}_ecpay_issue_allowance.json' |
| 65 |
invoice_data['ecpay_attachments'] = { |
| 66 |
'name': file_name, |
| 67 |
'raw': json.dumps(json_content), |
| 68 |
'mimetype': 'application/json', |
| 69 |
'res_model': invoice._name, |
| 70 |
'res_id': invoice.id, |
| 71 |
'res_field': 'l10n_tw_edi_file', # Binary field |
| 72 |
} |
| 73 |
|
| 74 |
@api.model |
| 75 |
def _hook_invoice_document_before_pdf_report_render(self, invoice, invoice_data): |
| 76 |
# EXTENDS 'account' |
| 77 |
super()._hook_invoice_document_before_pdf_report_render(invoice, invoice_data) |
| 78 |
self._l10n_tw_edi_generate_ecpay_json(invoice, invoice_data) |
| 79 |
|
| 80 |
@api.model |
| 81 |
def _call_web_service_before_invoice_pdf_render(self, invoices_data): |
| 82 |
# EXTENDS 'account' |
| 83 |
super()._call_web_service_before_invoice_pdf_render(invoices_data) |
| 84 |
|
| 85 |
for invoice, invoice_data in invoices_data.items(): |
| 86 |
if any(key in invoice_data['extra_edis'] for key in ('tw_ecpay_send', 'tw_ecpay_issue_allowance')): |
| 87 |
if 'ecpay_attachments' in invoice_data: |
| 88 |
json_content = json.loads(invoice_data['ecpay_attachments']['raw']) |
| 89 |
# If the invoice was downloaded but not sent, the json file could already be there. |
| 90 |
elif invoice.l10n_tw_edi_file: |
| 91 |
json_content = json.loads(base64.b64decode(invoice.l10n_tw_edi_file)) |
| 92 |
# If we don't have the file data and the file, we will regenerate it. |
| 93 |
else: |
| 94 |
self._l10n_tw_edi_generate_ecpay_json(invoice, invoice_data) |
| 95 |
if 'ecpay_attachments' not in invoice_data: |
| 96 |
continue # If an error occurred, it'll be in invoice_data['error'] so we can skip this invoice |
| 97 |
json_content = json.loads(invoice_data['ecpay_attachments']['raw']) |
| 98 |
|
| 99 |
if 'tw_ecpay_send' in invoice_data['extra_edis']: |
| 100 |
if errors := invoice._l10n_tw_edi_send(json_content): |
| 101 |
invoice_data["error"] = { |
| 102 |
"error_title": self.env._("Error when sending the invoices to ECPay."), |
| 103 |
"errors": errors, |
| 104 |
} |
| 105 |
elif 'tw_ecpay_issue_allowance' in invoice_data['extra_edis']: |
| 106 |
if errors := invoice._l10n_tw_edi_issue_allowance(json_content): |
| 107 |
invoice_data["error"] = { |
| 108 |
"error_title": self.env._("Error when sending the allowances to ECPay."), |
| 109 |
"errors": errors, |
| 110 |
} |
| 111 |
if 'tw_ecpay_send' in invoice_data['extra_edis'] or 'tw_ecpay_issue_allowance' in invoice_data['extra_edis']: |
| 112 |
if self._can_commit(): |
| 113 |
self._cr.commit() |
| 114 |
|
| 115 |
def _call_web_service_after_invoice_pdf_render(self, invoices_data): |
| 116 |
# EXTENDS 'account' |
| 117 |
super()._call_web_service_after_invoice_pdf_render(invoices_data) |
| 118 |
for invoice, invoice_data in invoices_data.items(): |
| 119 |
# No need to check tw_ecpay_issue_allowance will go to continue since l10n_tw_edi_state is not invoiced |
| 120 |
if 'tw_ecpay_send' not in invoice_data['extra_edis'] or invoice.l10n_tw_edi_state != 'invoiced': |
| 121 |
continue |
| 122 |
if errors := invoice._l10n_tw_edi_update_ecpay_invoice_info(): |
| 123 |
invoice_data["error"] = { |
| 124 |
"error_title": self.env._("Error when getting the invoice data from ECPay."), |
| 125 |
"errors": errors, |
| 126 |
} |
| 127 |
# We commit again if possible, to ensure that the invoice status is set in the database in case of errors later. |
| 128 |
if self._can_commit(): |
| 129 |
self._cr.commit() |
| 130 |
|
| 131 |
@api.model |
| 132 |
def _link_invoice_documents(self, invoices_data): |
| 133 |
# EXTENDS 'account' |
| 134 |
super()._link_invoice_documents(invoices_data) |
| 135 |
|
| 136 |
attachments_vals = [ |
| 137 |
invoice_data.get('ecpay_attachments') |
| 138 |
for invoice_data in invoices_data.values() |
| 139 |
if invoice_data.get('ecpay_attachments') |
| 140 |
] |
| 141 |
|
| 142 |
if attachments_vals: |
| 143 |
attachments = self.env['ir.attachment'].with_user(SUPERUSER_ID).create(attachments_vals) |
| 144 |
res_ids = [attachment.res_id for attachment in attachments] |
| 145 |
self.env['account.move'].browse(res_ids).invalidate_recordset(fnames=['l10n_tw_edi_file_id', 'l10n_tw_edi_file']) |