| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
|
| 3 |
from datetime import datetime, timedelta |
| 4 |
from unittest.mock import patch |
| 5 |
from urllib.parse import urljoin |
| 6 |
|
| 7 |
from freezegun import freeze_time |
| 8 |
|
| 9 |
from odoo import Command |
| 10 |
from odoo.addons.account.tests.test_account_move_send import TestAccountMoveSendCommon |
| 11 |
from odoo.exceptions import UserError |
| 12 |
from odoo.tests import tagged |
| 13 |
from odoo.tests.common import HttpCase |
| 14 |
|
| 15 |
CALL_API_METHOD = 'odoo.addons.l10n_tw_edi_ecpay.models.account_move.call_ecpay_api' |
| 16 |
|
| 17 |
|
| 18 |
@tagged('post_install_l10n', 'post_install', '-at_install') |
| 19 |
class L10nTWITestEdi(TestAccountMoveSendCommon, HttpCase): |
| 20 |
|
| 21 |
@classmethod |
| 22 |
@TestAccountMoveSendCommon.setup_country('tw') |
| 23 |
def setUpClass(cls): |
| 24 |
super().setUpClass() |
| 25 |
cls.company_data['company'].write({ |
| 26 |
'l10n_tw_edi_ecpay_staging_mode': True, |
| 27 |
'l10n_tw_edi_ecpay_merchant_id': '1234', |
| 28 |
'l10n_tw_edi_ecpay_hashkey': 'aaBBccDDeeFFggHH', |
| 29 |
'l10n_tw_edi_ecpay_hashIV': 'bbCCDDeeFFggHHaa', |
| 30 |
'phone': '+886 123 456 781', |
| 31 |
}) |
| 32 |
cls.partner_a.write({ |
| 33 |
'phone': '+886 123 456 789', |
| 34 |
'street': 'street七美', |
| 35 |
'city': '中正區', |
| 36 |
'state_id': cls.env.ref('base.state_tw_tpc').id, |
| 37 |
'country_id': cls.env.ref('base.tw').id, |
| 38 |
'company_type': 'person', |
| 39 |
}) |
| 40 |
cls.partner_b.write({ |
| 41 |
'phone': '+886 123 456 789', |
| 42 |
'street': 'street七美', |
| 43 |
'city': '信義區', |
| 44 |
'state_id': cls.env.ref('base.state_tw_klc').id, |
| 45 |
'country_id': cls.env.ref('base.tw').id, |
| 46 |
'vat': '24153791', |
| 47 |
'company_type': 'company', |
| 48 |
}) |
| 49 |
# We can reuse this invoice for the flow tests. |
| 50 |
cls.basic_invoice = cls.init_invoice( |
| 51 |
'out_invoice', partner=cls.partner_a, products=cls.product_a, taxes=cls.tax_sale_a, |
| 52 |
) |
| 53 |
cls.basic_invoice.action_post() |
| 54 |
cls.basic_invoice_b2b = cls.init_invoice( |
| 55 |
'out_invoice', partner=cls.partner_b, products=cls.product_a, |
| 56 |
) |
| 57 |
cls.basic_invoice_b2b.action_post() |
| 58 |
|
| 59 |
def test_01_can_generate_file(self): |
| 60 |
""" |
| 61 |
Simply test that with a valid configuration, we can generate the file. |
| 62 |
""" |
| 63 |
with patch(CALL_API_METHOD, new=self._test_01_mock): |
| 64 |
json_data = self.basic_invoice._l10n_tw_edi_generate_invoice_json() |
| 65 |
self.assertTrue(json_data) |
| 66 |
self.assertNotIn("InvoiceRemark", json_data) |
| 67 |
|
| 68 |
# Validate the customer data |
| 69 |
self.assertEqual(json_data.get("MerchantID"), "1234") |
| 70 |
self.assertEqual(json_data.get("CustomerName"), "partner_a") |
| 71 |
self.assertEqual(json_data.get("CustomerEmail"), "partner_a@tsointsoin") |
| 72 |
self.assertEqual(json_data.get("CustomerPhone"), "0123456789") |
| 73 |
self.assertEqual(json_data.get("SalesAmount"), 1050.0) |
| 74 |
self.assertEqual(json_data.get("CustomerAddr"), "street七美, 中正區 TPC, Taiwan") |
| 75 |
|
| 76 |
self.basic_invoice.write({'ref': 'Test Reference'}) |
| 77 |
json_data_with_ref = self.basic_invoice._l10n_tw_edi_generate_invoice_json() |
| 78 |
self.assertEqual(json_data_with_ref.get("InvoiceRemark"), "Test Reference") |
| 79 |
|
| 80 |
@freeze_time("2025-01-06 15:00:00") |
| 81 |
def test_02_basic_submission(self): |
| 82 |
""" |
| 83 |
This tests the most basic flow: an invoice is successfully sent to the ECpay platform, and then pass validation. |
| 84 |
""" |
| 85 |
send_and_print = self.create_send_and_print(self.basic_invoice) |
| 86 |
with patch(CALL_API_METHOD, new=self._test_02_mock): |
| 87 |
send_and_print.action_send_and_print() |
| 88 |
|
| 89 |
# Now that the invoice has been sent successfully, we assert that some info have been saved correctly. |
| 90 |
self.assertRecordValues( |
| 91 |
self.basic_invoice, |
| 92 |
[{ |
| 93 |
'l10n_tw_edi_ecpay_invoice_id': 'AB11100099', |
| 94 |
'l10n_tw_edi_invoice_create_date': datetime.strptime('2025-01-06 15:00:00', '%Y-%m-%d %H:%M:%S'), |
| 95 |
'l10n_tw_edi_state': 'valid', |
| 96 |
}] |
| 97 |
) |
| 98 |
|
| 99 |
self.assertTrue(self.basic_invoice.l10n_tw_edi_file_id) |
| 100 |
|
| 101 |
def test_03_failed_submission(self): |
| 102 |
""" |
| 103 |
This test will test a flow that fails when sending the invoice to the ECpay platform, an UserError to be raised |
| 104 |
""" |
| 105 |
send_and_print = self.create_send_and_print(self.basic_invoice) |
| 106 |
with patch(CALL_API_METHOD, new=self._test_03_mock): |
| 107 |
with self.assertRaises(UserError): |
| 108 |
send_and_print.action_send_and_print() |
| 109 |
|
| 110 |
@freeze_time("2025-01-06 15:00:00") |
| 111 |
def test_04_invalid_invoice(self): |
| 112 |
""" |
| 113 |
This tests the flow of invalidating an invoice that has already been sent to the ECpay platform. |
| 114 |
""" |
| 115 |
send_and_print = self.create_send_and_print(self.basic_invoice) |
| 116 |
with patch(CALL_API_METHOD, new=self._test_04_mock): |
| 117 |
send_and_print.action_send_and_print() |
| 118 |
wizard_vals = {'journal_id': self.basic_invoice.journal_id.id, 'reason': 'refund'} |
| 119 |
wizard_reverse = self.env['account.move.reversal']\ |
| 120 |
.with_context(active_ids=self.basic_invoice.id, active_model='account.move').create(wizard_vals) |
| 121 |
wizard_reverse.reverse_moves(is_modify=True) |
| 122 |
self.assertEqual(self.basic_invoice.l10n_tw_edi_state, 'invalid') |
| 123 |
|
| 124 |
@freeze_time("2025-01-06 15:00:00") |
| 125 |
def test_05_refund_invoice(self): |
| 126 |
""" |
| 127 |
This tests the flow of refunding an invoice that has already been sent to the ECpay platform with an offline |
| 128 |
agreement type. |
| 129 |
And make sure that the credit note is created with the correct fields and values from the wizard |
| 130 |
""" |
| 131 |
send_and_print = self.create_send_and_print(self.basic_invoice) |
| 132 |
with patch(CALL_API_METHOD, new=self._test_05_mock): |
| 133 |
send_and_print.action_send_and_print() |
| 134 |
wizard_vals = { |
| 135 |
'journal_id': self.basic_invoice.journal_id.id, |
| 136 |
'l10n_tw_edi_refund_agreement_type': 'offline', |
| 137 |
'l10n_tw_edi_allowance_notify_way': 'email', |
| 138 |
'reason': 'refund', |
| 139 |
} |
| 140 |
wizard_reverse = self.env['account.move.reversal']\ |
| 141 |
.with_context(active_ids=self.basic_invoice.id, active_model='account.move').create(wizard_vals) |
| 142 |
wizard_reverse.reverse_moves(is_modify=False) |
| 143 |
credit_note = wizard_reverse.new_move_ids |
| 144 |
credit_note.action_post() |
| 145 |
send_and_print_credit_note = self.create_send_and_print(credit_note) |
| 146 |
send_and_print_credit_note.action_send_and_print() |
| 147 |
self.assertRecordValues( |
| 148 |
credit_note, |
| 149 |
[{ |
| 150 |
'reversed_entry_id': self.basic_invoice.id, |
| 151 |
'l10n_tw_edi_refund_agreement_type': 'offline', |
| 152 |
'l10n_tw_edi_refund_invoice_number': '20250106000000021', |
| 153 |
'l10n_tw_edi_refund_state': 'agreed', |
| 154 |
'l10n_tw_edi_ecpay_invoice_id': 'AB11100099', |
| 155 |
'l10n_tw_edi_invoice_create_date': datetime(2025, 1, 6, 15, 0, 0), |
| 156 |
}] |
| 157 |
) |
| 158 |
|
| 159 |
@freeze_time("2025-01-06 15:00:00") |
| 160 |
def test_06_refund_invoice_agreed_invoice_allowance(self): |
| 161 |
""" |
| 162 |
This tests the flow of refunding an invoice that has already been sent to the ECpay platform with an online |
| 163 |
agreement type including the flow that customer agreeing to the invoice allowance. |
| 164 |
And make sure that the credit note is created with the correct fields and values from the wizard |
| 165 |
""" |
| 166 |
send_and_print = self.create_send_and_print(self.basic_invoice) |
| 167 |
with patch(CALL_API_METHOD, new=self._test_06_mock): |
| 168 |
send_and_print.action_send_and_print() |
| 169 |
wizard_vals = { |
| 170 |
'journal_id': self.basic_invoice.journal_id.id, |
| 171 |
'l10n_tw_edi_refund_agreement_type': 'online', |
| 172 |
'l10n_tw_edi_allowance_notify_way': 'email', |
| 173 |
'reason': 'refund', |
| 174 |
} |
| 175 |
wizard_reverse = self.env['account.move.reversal']\ |
| 176 |
.with_context(active_ids=self.basic_invoice.id, active_model='account.move').create(wizard_vals) |
| 177 |
wizard_reverse.reverse_moves(is_modify=False) |
| 178 |
credit_note = wizard_reverse.new_move_ids |
| 179 |
credit_note.action_post() |
| 180 |
send_and_print_credit_note = self.create_send_and_print(credit_note) |
| 181 |
send_and_print_credit_note.action_send_and_print() |
| 182 |
self.assertRecordValues( |
| 183 |
credit_note, |
| 184 |
[{ |
| 185 |
'reversed_entry_id': self.basic_invoice.id, |
| 186 |
'l10n_tw_edi_refund_agreement_type': 'online', |
| 187 |
'l10n_tw_edi_refund_invoice_number': '20250106000000021', |
| 188 |
'l10n_tw_edi_refund_state': 'to_be_agreed', |
| 189 |
'l10n_tw_edi_ecpay_invoice_id': 'AB11100099', |
| 190 |
'l10n_tw_edi_invoice_create_date': datetime(2025, 1, 6, 15, 0, 0), |
| 191 |
}] |
| 192 |
) |
| 193 |
|
| 194 |
# test the step that the customer agrees invoice allowance |
| 195 |
api_url = urljoin( |
| 196 |
credit_note.get_base_url(), |
| 197 |
f"/invoice/ecpay/agreed_invoice_allowance/{credit_note.id}?access_token={credit_note._portal_ensure_token()}") |
| 198 |
response = self.url_open( |
| 199 |
api_url, |
| 200 |
data={"RtnCode": "1"} |
| 201 |
) |
| 202 |
self.assertEqual(response.status_code, 200) |
| 203 |
self.assertRecordValues( |
| 204 |
credit_note, |
| 205 |
[{ |
| 206 |
'l10n_tw_edi_refund_invoice_number': '20250106000000021', |
| 207 |
'l10n_tw_edi_refund_state': 'agreed', |
| 208 |
}] |
| 209 |
) |
| 210 |
|
| 211 |
def test_07_fail_data_validation(self): |
| 212 |
""" |
| 213 |
This tests the data validation when trying to send to the ECpay platform. |
| 214 |
""" |
| 215 |
# the partner is b2b but has no tax id |
| 216 |
test_partner = self.env['res.partner'].create({ |
| 217 |
'name': 'Test Partner', |
| 218 |
'phone': '+886 123 456 789', |
| 219 |
'street': 'street七美', |
| 220 |
'city': '中正區', |
| 221 |
'state_id': self.env.ref('base.state_tw_tpc').id, |
| 222 |
'company_type': 'company', |
| 223 |
}) |
| 224 |
invoice_a = self.init_invoice( |
| 225 |
'out_invoice', partner=test_partner, products=self.product_a, taxes=self.tax_sale_a, |
| 226 |
) |
| 227 |
invoice_a.action_post() |
| 228 |
send_and_print = self.create_send_and_print(invoice_a) |
| 229 |
with self.assertRaises(UserError): |
| 230 |
send_and_print.action_send_and_print() |
| 231 |
|
| 232 |
# the partner is b2b and has an invalid tax id |
| 233 |
test_partner.vat = '1234567A' |
| 234 |
invoice_b = self.init_invoice( |
| 235 |
'out_invoice', partner=test_partner, products=self.product_a, taxes=self.tax_sale_a, |
| 236 |
) |
| 237 |
invoice_b.action_post() |
| 238 |
send_and_print = self.create_send_and_print(invoice_b) |
| 239 |
with self.assertRaises(UserError): |
| 240 |
send_and_print.action_send_and_print() |
| 241 |
|
| 242 |
# the partner's phone number is invalid |
| 243 |
test_partner.vat = '12345678' |
| 244 |
test_partner.phone = '123+456+789' |
| 245 |
invoice_c = self.init_invoice( |
| 246 |
'out_invoice', partner=test_partner, products=self.product_a, taxes=self.tax_sale_a, |
| 247 |
) |
| 248 |
invoice_c.action_post() |
| 249 |
send_and_print = self.create_send_and_print(invoice_c) |
| 250 |
with self.assertRaises(UserError): |
| 251 |
send_and_print.action_send_and_print() |
| 252 |
# the invoice type is invalid |
| 253 |
test_partner.phone = '+886 123 456 789' |
| 254 |
invoice_d = self.init_invoice( |
| 255 |
'out_invoice', partner=test_partner, products=self.product_a, taxes=self.tax_sale_a, |
| 256 |
) |
| 257 |
invoice_d.l10n_tw_edi_invoice_type = '08' |
| 258 |
invoice_d.action_post() |
| 259 |
send_and_print = self.create_send_and_print(invoice_d) |
| 260 |
with self.assertRaises(UserError): |
| 261 |
send_and_print.action_send_and_print() |
| 262 |
|
| 263 |
def test_08_invoice_with_downpayment(self): |
| 264 |
"""Ensure downpayment with -ve quantity is normalized for ECPay JSON.""" |
| 265 |
invoice = self.init_invoice( |
| 266 |
'out_invoice', partner=self.partner_a, products=self.product_a, taxes=self.tax_sale_a, |
| 267 |
) |
| 268 |
invoice.write({ |
| 269 |
"invoice_line_ids": [ |
| 270 |
Command.create({ |
| 271 |
"name": "Downpayment", |
| 272 |
"price_unit": 10.0, |
| 273 |
"quantity": -1.0, |
| 274 |
"tax_ids": self.tax_sale_a, |
| 275 |
}), |
| 276 |
], |
| 277 |
}) |
| 278 |
invoice.action_post() |
| 279 |
|
| 280 |
self.assertListEqual( |
| 281 |
invoice._l10n_tw_edi_generate_invoice_json()['Items'], |
| 282 |
[ |
| 283 |
{ |
| 284 |
'ItemSeq': 1, |
| 285 |
'ItemName': 'product_a', |
| 286 |
'ItemCount': 1.0, |
| 287 |
'ItemWord': 'Units', |
| 288 |
'ItemPrice': 1000.0, |
| 289 |
'ItemTaxType': '1', |
| 290 |
'ItemAmount': 1000.0 |
| 291 |
}, |
| 292 |
{ |
| 293 |
'ItemSeq': 2, |
| 294 |
'ItemName': 'Downpayment', |
| 295 |
'ItemCount': 1.0, |
| 296 |
'ItemWord': False, |
| 297 |
'ItemPrice': -10.0, |
| 298 |
'ItemTaxType': '1', |
| 299 |
'ItemAmount': -10.0 |
| 300 |
}, |
| 301 |
], |
| 302 |
) |
| 303 |
|
| 304 |
def test_09_refund_invoice_with_payment(self): |
| 305 |
""" |
| 306 |
This tests the flow of refunding an invoice that has the payment has been registered and |
| 307 |
has already been sent to the ECpay platform with an offline agreement type. |
| 308 |
Make sure that the credit note is created with the correct fields and values from the wizard |
| 309 |
and the credit note is successfully sent to the ECpay platform |
| 310 |
""" |
| 311 |
send_and_print = self.create_send_and_print(self.basic_invoice) |
| 312 |
with patch(CALL_API_METHOD, new=self._test_09_mock): |
| 313 |
basic_invoice_json = self.basic_invoice._l10n_tw_edi_generate_invoice_json() |
| 314 |
send_and_print.action_send_and_print() |
| 315 |
# register payment |
| 316 |
self.env['account.payment.register'].with_context(active_model='account.move', active_ids=self.basic_invoice.ids).create({ |
| 317 |
'amount': self.basic_invoice.amount_total, |
| 318 |
})._create_payments() |
| 319 |
self.assertEqual(self.basic_invoice.payment_state, self.env['account.move']._get_invoice_in_payment_state()) |
| 320 |
# create credit note |
| 321 |
wizard_vals = { |
| 322 |
'journal_id': self.basic_invoice.journal_id.id, |
| 323 |
'l10n_tw_edi_refund_agreement_type': 'offline', |
| 324 |
'l10n_tw_edi_allowance_notify_way': 'email', |
| 325 |
'reason': 'refund', |
| 326 |
} |
| 327 |
wizard_reverse = self.env['account.move.reversal']\ |
| 328 |
.with_context(active_ids=self.basic_invoice.id, active_model='account.move').create(wizard_vals) |
| 329 |
wizard_reverse.reverse_moves(is_modify=False) |
| 330 |
credit_note = wizard_reverse.new_move_ids |
| 331 |
credit_note.action_post() |
| 332 |
credit_note_json = credit_note._l10n_tw_edi_generate_issue_allowance_json() |
| 333 |
# Ensure that the sale amount on invoice json and the allowance amount on credit note json are the same |
| 334 |
self.assertEqual(basic_invoice_json.get('SalesAmount'), credit_note_json.get('AllowanceAmount')) |
| 335 |
send_and_print_credit_note = self.create_send_and_print(credit_note) |
| 336 |
send_and_print_credit_note.action_send_and_print() |
| 337 |
self.assertRecordValues( |
| 338 |
credit_note, |
| 339 |
[{ |
| 340 |
'reversed_entry_id': self.basic_invoice.id, |
| 341 |
'l10n_tw_edi_refund_agreement_type': 'offline', |
| 342 |
'l10n_tw_edi_refund_invoice_number': '20250106000000021', |
| 343 |
'l10n_tw_edi_refund_state': 'agreed', |
| 344 |
'l10n_tw_edi_ecpay_invoice_id': 'AB11100099', |
| 345 |
'l10n_tw_edi_invoice_create_date': datetime(2025, 1, 6, 15, 0, 0), |
| 346 |
}] |
| 347 |
) |
| 348 |
|
| 349 |
@freeze_time("2025-01-06 15:00:00") |
| 350 |
def test_10_basic_submission_b2b_partner_created(self): |
| 351 |
""" |
| 352 |
This tests the following flow for B2B invoices: a buyer is created, then an invoice is successfully sent |
| 353 |
to the ECpay platform, and then pass validation. |
| 354 |
""" |
| 355 |
send_and_print = self.create_send_and_print(self.basic_invoice_b2b) |
| 356 |
with patch(CALL_API_METHOD, new=self._test_10_mock): |
| 357 |
send_and_print.action_send_and_print() |
| 358 |
|
| 359 |
# Now that the invoice has been sent successfully, we assert that some info have been saved correctly. |
| 360 |
self.assertRecordValues( |
| 361 |
self.basic_invoice_b2b, |
| 362 |
[{ |
| 363 |
'l10n_tw_edi_ecpay_invoice_id': 'AB11100099', |
| 364 |
'l10n_tw_edi_invoice_create_date': datetime.strptime('2025-01-06 15:00:00', '%Y-%m-%d %H:%M:%S'), |
| 365 |
'l10n_tw_edi_state': 'valid', |
| 366 |
}] |
| 367 |
) |
| 368 |
|
| 369 |
self.assertTrue(self.basic_invoice_b2b.l10n_tw_edi_file_id) |
| 370 |
|
| 371 |
@freeze_time("2025-01-06 15:00:00") |
| 372 |
def test_11_basic_submission_b2b_partner_exists(self): |
| 373 |
""" |
| 374 |
This tests the following flow for B2B invoices: the buyer already exists, then an invoice is successfully |
| 375 |
sent to the ECpay platform, and then pass validation. |
| 376 |
""" |
| 377 |
send_and_print = self.create_send_and_print(self.basic_invoice_b2b) |
| 378 |
with patch(CALL_API_METHOD, new=self._test_11_mock): |
| 379 |
send_and_print.action_send_and_print() |
| 380 |
|
| 381 |
# Now that the invoice has been sent successfully, we assert that some info have been saved correctly. |
| 382 |
self.assertRecordValues( |
| 383 |
self.basic_invoice_b2b, |
| 384 |
[{ |
| 385 |
'l10n_tw_edi_ecpay_invoice_id': 'AB11100099', |
| 386 |
'l10n_tw_edi_invoice_create_date': datetime.strptime('2025-01-06 15:00:00', '%Y-%m-%d %H:%M:%S'), |
| 387 |
'l10n_tw_edi_state': 'valid', |
| 388 |
}] |
| 389 |
) |
| 390 |
|
| 391 |
self.assertTrue(self.basic_invoice_b2b.l10n_tw_edi_file_id) |
| 392 |
|
| 393 |
@freeze_time("2025-01-06 15:00:00") |
| 394 |
def test_12_basic_submission_b2b_partner_creation_failed(self): |
| 395 |
""" |
| 396 |
This tests the following flow for B2B invoices: creating the buyer failed, invoice should not be created. |
| 397 |
""" |
| 398 |
send_and_print = self.create_send_and_print(self.basic_invoice_b2b) |
| 399 |
with patch(CALL_API_METHOD, new=self._test_12_mock): |
| 400 |
with self.assertRaises(UserError): |
| 401 |
send_and_print.action_send_and_print() |
| 402 |
|
| 403 |
@freeze_time("2025-01-13 15:00:00") |
| 404 |
def test_13_b2b_refund_upload_deadline_restriction(self): |
| 405 |
"""Test B2B Refund compliance with ECPay's upload deadline restriction. |
| 406 |
|
| 407 |
Context: |
| 408 |
- Government Rule: Allowances must be uploaded within 7 days of creation. |
| 409 |
- ECPay Restriction: The API rejects allowances where 'AllowanceDate' is older |
| 410 |
than 6 days from the current upload time. |
| 411 |
|
| 412 |
Scenario: |
| 413 |
We simulate a refund for an ECPay-submitted invoice created 7 days ago. |
| 414 |
We verify that 'AllowanceDate' is OMITTED from the JSON payload. |
| 415 |
By omitting it, ECPay defaults the date to 'Now', ensuring the request |
| 416 |
passes validation regardless of the gap. (not covered here) |
| 417 |
""" |
| 418 |
seven_days_ago = datetime.now() - timedelta(days=7) |
| 419 |
invoice = self.init_invoice("out_invoice", partner=self.partner_b, products=self.product_a, taxes=self.tax_sale_a) |
| 420 |
invoice.write({ |
| 421 |
"invoice_date": seven_days_ago.date(), |
| 422 |
"l10n_tw_edi_invoice_create_date": seven_days_ago, |
| 423 |
"l10n_tw_edi_ecpay_invoice_id": "AB11100099" # simulate it was already sent |
| 424 |
}) |
| 425 |
invoice.action_post() |
| 426 |
|
| 427 |
wizard_vals = { |
| 428 |
"journal_id": invoice.journal_id.id, |
| 429 |
"reason": "Refund 7 days later", |
| 430 |
"l10n_tw_edi_refund_agreement_type": "offline", |
| 431 |
} |
| 432 |
wizard = self.env["account.move.reversal"].with_context( |
| 433 |
active_ids=invoice.ids, |
| 434 |
active_model="account.move" |
| 435 |
).create(wizard_vals) |
| 436 |
wizard.reverse_moves() |
| 437 |
credit_note = wizard.new_move_ids |
| 438 |
credit_note.action_post() |
| 439 |
|
| 440 |
json_data = credit_note._l10n_tw_edi_generate_issue_allowance_json() |
| 441 |
self.assertNotIn("AllowanceDate", json_data, |
| 442 |
"B2B Allowances should not include AllowanceDate to avoid >6 day limit errors." |
| 443 |
) |
| 444 |
|
| 445 |
@freeze_time("2026-01-05 18:00:00") |
| 446 |
def test_14_tw_datetime_conversion(self): |
| 447 |
"""Test that datetime conversion to TW timezone works correctly. |
| 448 |
|
| 449 |
Scenario: |
| 450 |
An invoice is created and send to ecpay at "2026-01-05 18:00:00" UTC. |
| 451 |
In TW timezone, this corresponds to "2026-01-06 02:00:00" (UTC+8). |
| 452 |
|
| 453 |
Ensure the convert_utc_time_to_tw_time function works to convert the 'InvoiceDate' in the allowance JSON to TW date |
| 454 |
""" |
| 455 |
invoice = self.init_invoice("out_invoice", partner=self.partner_a, products=self.product_a, taxes=self.tax_sale_a) |
| 456 |
invoice.write({ |
| 457 |
"l10n_tw_edi_invoice_create_date": datetime(2026, 1, 5, 18, 0, 0), |
| 458 |
"l10n_tw_edi_ecpay_invoice_id": "AB11100099" # simulate it was already sent |
| 459 |
}) |
| 460 |
invoice.action_post() |
| 461 |
|
| 462 |
wizard_vals = { |
| 463 |
"journal_id": invoice.journal_id.id, |
| 464 |
"reason": "Refund", |
| 465 |
"l10n_tw_edi_refund_agreement_type": "offline", |
| 466 |
} |
| 467 |
wizard = self.env["account.move.reversal"].with_context( |
| 468 |
active_ids=invoice.ids, |
| 469 |
active_model="account.move" |
| 470 |
).create(wizard_vals) |
| 471 |
wizard.reverse_moves() |
| 472 |
credit_note = wizard.new_move_ids |
| 473 |
credit_note.action_post() |
| 474 |
|
| 475 |
json_data = credit_note._l10n_tw_edi_generate_issue_allowance_json() |
| 476 |
self.assertEqual(json_data.get("InvoiceDate"), "2026-01-06") |
| 477 |
|
| 478 |
# ------------------------------------------------------------------------- |
| 479 |
# Patched methods |
| 480 |
# ------------------------------------------------------------------------- |
| 481 |
def _test_01_mock(self, endpoint, params, company_id, is_b2b=False): |
| 482 |
if endpoint == "/GetCompanyNameByTaxID": |
| 483 |
return { |
| 484 |
"RtnCode": 1, |
| 485 |
"CompanyName": "Test Company", |
| 486 |
} |
| 487 |
else: |
| 488 |
raise UserError('Unexpected endpoint called during a test: %s with params %s.' % (endpoint, params)) |
| 489 |
|
| 490 |
def _test_02_mock(self, endpoint, json_data, company_id, is_b2b=False): |
| 491 |
if endpoint == "/Issue": |
| 492 |
return { |
| 493 |
"RtnCode": 1, |
| 494 |
"RtnMsg": "Success", |
| 495 |
"InvoiceNo": "AB11100099", |
| 496 |
"InvoiceDate": "2025-01-06 23:00:00", |
| 497 |
"RandomNumber": "6868" |
| 498 |
} |
| 499 |
elif endpoint == "/GetIssue": |
| 500 |
return { |
| 501 |
"RtnCode": 1, |
| 502 |
"RtnMsg": "Success", |
| 503 |
"IIS_Sales_Amount": self.basic_invoice.amount_total, |
| 504 |
"IIS_Invalid_Status": "0", |
| 505 |
"IIS_Issue_Status": "1", |
| 506 |
"IIS_Relate_Number": "20250106000000020", |
| 507 |
"IIS_Remain_Allowance_Amt": 0, |
| 508 |
} |
| 509 |
elif endpoint == "/GetCompanyNameByTaxID": |
| 510 |
return { |
| 511 |
"RtnCode": 1, |
| 512 |
"CompanyName": "Test Company", |
| 513 |
} |
| 514 |
else: |
| 515 |
raise UserError('Unexpected endpoint called during a test: %s with params %s.' % (endpoint, json_data)) |
| 516 |
|
| 517 |
def _test_03_mock(self, endpoint, params, company_id, is_b2b=False): |
| 518 |
if endpoint == "/Issue": |
| 519 |
return { |
| 520 |
"RtnCode": 0, |
| 521 |
"RtnMsg": "Error", |
| 522 |
} |
| 523 |
else: |
| 524 |
raise UserError('Unexpected endpoint called during a test: %s with params %s.' % (endpoint, params)) |
| 525 |
|
| 526 |
def _test_04_mock(self, endpoint, params, company_id, is_b2b=False): |
| 527 |
if endpoint == "/Issue": |
| 528 |
return { |
| 529 |
"RtnCode": 1, |
| 530 |
"RtnMsg": "Success", |
| 531 |
"InvoiceNo": "AB11100099", |
| 532 |
"InvoiceDate": "2025-01-06 23:00:00", |
| 533 |
"RandomNumber": "6868" |
| 534 |
} |
| 535 |
elif endpoint == "/GetIssue": |
| 536 |
return_data = { |
| 537 |
"RtnCode": 1, |
| 538 |
"RtnMsg": "Success", |
| 539 |
"IIS_Sales_Amount": self.basic_invoice.amount_total, |
| 540 |
"IIS_Invalid_Status": "1" if self.basic_invoice.l10n_tw_edi_invalidate_reason else "0", |
| 541 |
"IIS_Issue_Status": "1", |
| 542 |
"IIS_Relate_Number": "20250106000000020", |
| 543 |
"IIS_Remain_Allowance_Amt": 0, |
| 544 |
} |
| 545 |
return return_data |
| 546 |
elif endpoint == "/Invalid": |
| 547 |
return { |
| 548 |
"RtnCode": 1, |
| 549 |
"RtnMsg": "Success", |
| 550 |
"InvoiceNo": "AB11100099" |
| 551 |
} |
| 552 |
elif endpoint == "/GetCompanyNameByTaxID": |
| 553 |
return { |
| 554 |
"RtnCode": 1, |
| 555 |
"CompanyName": "Test Company", |
| 556 |
} |
| 557 |
else: |
| 558 |
raise UserError('Unexpected endpoint called during a test: %s with params %s.' % (endpoint, params)) |
| 559 |
|
| 560 |
def _test_05_mock(self, endpoint, params, company_id, is_b2b=False): |
| 561 |
if endpoint == "/Issue": |
| 562 |
return { |
| 563 |
"RtnCode": 1, |
| 564 |
"RtnMsg": "Success", |
| 565 |
"InvoiceNo": "AB11100099", |
| 566 |
"InvoiceDate": "2025-01-06 23:00:00", |
| 567 |
"RandomNumber": "6868" |
| 568 |
} |
| 569 |
elif endpoint == "/GetIssue": |
| 570 |
return_data = { |
| 571 |
"RtnCode": 1, |
| 572 |
"RtnMsg": "Success", |
| 573 |
"IIS_Sales_Amount": self.basic_invoice.amount_total, |
| 574 |
"IIS_Invalid_Status": "0", |
| 575 |
"IIS_Issue_Status": "1", |
| 576 |
"IIS_Relate_Number": "20250106000000020", |
| 577 |
"IIS_Remain_Allowance_Amt": 0, |
| 578 |
} |
| 579 |
return return_data |
| 580 |
elif endpoint == "/Allowance": |
| 581 |
return { |
| 582 |
"RtnCode": 1, |
| 583 |
"RtnMsg": "Success", |
| 584 |
"IA_Allow_No": "20250106000000021", |
| 585 |
"IA_Invoice_No": "AB11100099", |
| 586 |
"IA_Date": "2025-01-06 23:00:00", |
| 587 |
"IA_Remain_Allowance_Amt": 0, |
| 588 |
} |
| 589 |
elif endpoint == "/GetCompanyNameByTaxID": |
| 590 |
return { |
| 591 |
"RtnCode": 1, |
| 592 |
"CompanyName": "Test Company", |
| 593 |
} |
| 594 |
else: |
| 595 |
raise UserError('Unexpected endpoint called during a test: %s with params %s.' % (endpoint, params)) |
| 596 |
|
| 597 |
def _test_06_mock(self, endpoint, params, company_id, is_b2b=False): |
| 598 |
if endpoint == "/Issue": |
| 599 |
return { |
| 600 |
"RtnCode": 1, |
| 601 |
"RtnMsg": "Success", |
| 602 |
"InvoiceNo": "AB11100099", |
| 603 |
"InvoiceDate": "2025-01-06 23:00:00", |
| 604 |
"RandomNumber": "6868" |
| 605 |
} |
| 606 |
elif endpoint == "/GetIssue": |
| 607 |
return_data = { |
| 608 |
"RtnCode": 1, |
| 609 |
"RtnMsg": "Success", |
| 610 |
"IIS_Sales_Amount": self.basic_invoice.amount_total, |
| 611 |
"IIS_Invalid_Status": "0", |
| 612 |
"IIS_Issue_Status": "1", |
| 613 |
"IIS_Relate_Number": "20250106000000020", |
| 614 |
"IIS_Remain_Allowance_Amt": 0, |
| 615 |
} |
| 616 |
return return_data |
| 617 |
elif endpoint == "/AllowanceByCollegiate": |
| 618 |
return { |
| 619 |
"RtnCode": 1, |
| 620 |
"RtnMsg": "Success", |
| 621 |
"IA_Allow_No": "20250106000000021", |
| 622 |
"IA_Invoice_No": "AB11100099", |
| 623 |
"IA_Date": "2025-01-06 23:00:00", |
| 624 |
"IA_Remain_Allowance_Amt": 0, |
| 625 |
} |
| 626 |
elif endpoint == "/GetCompanyNameByTaxID": |
| 627 |
return { |
| 628 |
"RtnCode": 1, |
| 629 |
"CompanyName": "Test Company", |
| 630 |
} |
| 631 |
else: |
| 632 |
raise UserError('Unexpected endpoint called during a test: %s with params %s.' % (endpoint, params)) |
| 633 |
|
| 634 |
def _test_09_mock(self, endpoint, params, company_id, is_b2b=False): |
| 635 |
if endpoint == "/Issue": |
| 636 |
return { |
| 637 |
"RtnCode": 1, |
| 638 |
"RtnMsg": "Success", |
| 639 |
"InvoiceNo": "AB11100099", |
| 640 |
"InvoiceDate": "2025-01-06 23:00:00", |
| 641 |
"RandomNumber": "6868" |
| 642 |
} |
| 643 |
elif endpoint == "/GetIssue": |
| 644 |
return_data = { |
| 645 |
"RtnCode": 1, |
| 646 |
"RtnMsg": "Success", |
| 647 |
"IIS_Sales_Amount": self.basic_invoice.amount_total, |
| 648 |
"IIS_Invalid_Status": "0", |
| 649 |
"IIS_Issue_Status": "1", |
| 650 |
"IIS_Relate_Number": "20250106000000020", |
| 651 |
"IIS_Remain_Allowance_Amt": 0, |
| 652 |
} |
| 653 |
return return_data |
| 654 |
elif endpoint == "/Allowance": |
| 655 |
return { |
| 656 |
"RtnCode": 1, |
| 657 |
"RtnMsg": "Success", |
| 658 |
"IA_Allow_No": "20250106000000021", |
| 659 |
"IA_Invoice_No": "AB11100099", |
| 660 |
"IA_Date": "2025-01-06 23:00:00", |
| 661 |
"IA_Remain_Allowance_Amt": 0, |
| 662 |
} |
| 663 |
elif endpoint == "/GetCompanyNameByTaxID": |
| 664 |
return { |
| 665 |
"RtnCode": 1, |
| 666 |
"CompanyName": "Test Company", |
| 667 |
} |
| 668 |
else: |
| 669 |
raise UserError('Unexpected endpoint called during a test: %s with params %s.' % (endpoint, params)) |
| 670 |
|
| 671 |
def _test_10_mock(self, endpoint, json_data, company_id, is_b2b=False): |
| 672 |
if endpoint == "/Issue": |
| 673 |
return { |
| 674 |
"RtnCode": 1, |
| 675 |
"RtnMsg": "Success", |
| 676 |
"InvoiceNumber": "AB11100099", |
| 677 |
"RandomNumber": "6868" |
| 678 |
} |
| 679 |
elif endpoint == "/GetIssue": |
| 680 |
return { |
| 681 |
"RtnCode": 1, |
| 682 |
"RtnMsg": "Success", |
| 683 |
"RtnData": { |
| 684 |
"RelateNumber": "20250106000000020", |
| 685 |
"SalesAmount": self.basic_invoice_b2b.amount_total, |
| 686 |
"Issue_Status": "1", |
| 687 |
"Invalid_Status": "0", |
| 688 |
}, |
| 689 |
} |
| 690 |
elif endpoint == "/GetCompanyNameByTaxID": |
| 691 |
return { |
| 692 |
"RtnCode": 1, |
| 693 |
"CompanyName": "Test Company", |
| 694 |
} |
| 695 |
elif endpoint == "/MaintainMerchantCustomerData": |
| 696 |
return { |
| 697 |
"RtnCode": 1, |
| 698 |
"RtnMsg": "新增成功", |
| 699 |
} |
| 700 |
else: |
| 701 |
raise UserError('Unexpected endpoint called during a test: %s with params %s.' % (endpoint, json_data)) |
| 702 |
|
| 703 |
def _test_11_mock(self, endpoint, json_data, company_id, is_b2b=False): |
| 704 |
if endpoint == "/Issue": |
| 705 |
return { |
| 706 |
"RtnCode": 1, |
| 707 |
"RtnMsg": "Success", |
| 708 |
"InvoiceNumber": "AB11100099", |
| 709 |
"RandomNumber": "6868" |
| 710 |
} |
| 711 |
elif endpoint == "/GetIssue": |
| 712 |
return { |
| 713 |
"RtnCode": 1, |
| 714 |
"RtnMsg": "Success", |
| 715 |
"RtnData": { |
| 716 |
"RelateNumber": "20250106000000020", |
| 717 |
"SalesAmount": self.basic_invoice_b2b.amount_total, |
| 718 |
"Issue_Status": "1", |
| 719 |
"Invalid_Status": "0", |
| 720 |
}, |
| 721 |
} |
| 722 |
elif endpoint == "/GetCompanyNameByTaxID": |
| 723 |
return { |
| 724 |
"RtnCode": 1, |
| 725 |
"CompanyName": "Test Company", |
| 726 |
} |
| 727 |
elif endpoint == "/MaintainMerchantCustomerData": |
| 728 |
return { |
| 729 |
"RtnCode": 6160052, |
| 730 |
"RtnMsg": "新增成功", |
| 731 |
} |
| 732 |
else: |
| 733 |
raise UserError('Unexpected endpoint called during a test: %s with params %s.' % (endpoint, json_data)) |
| 734 |
|
| 735 |
def _test_12_mock(self, endpoint, json_data, company_id, is_b2b=False): |
| 736 |
if endpoint == "/MaintainMerchantCustomerData": |
| 737 |
return { |
| 738 |
"RtnCode": 6160051, |
| 739 |
"RtnMsg": "新增成功", |
| 740 |
} |
| 741 |
else: |
| 742 |
raise UserError('Unexpected endpoint called during a test: %s with params %s.' % (endpoint, json_data)) |