-
-
Notifications
You must be signed in to change notification settings - Fork 614
Expand file tree
/
Copy pathtest_l10n_es_pos_sii.py
More file actions
394 lines (370 loc) · 14.3 KB
/
Copy pathtest_l10n_es_pos_sii.py
File metadata and controls
394 lines (370 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# Copyright 2023 Aures Tic - Almudena de la Puente <almudena@aurestic.es>
# Copyright 2023 Aures Tic - Jose Zambudio <jose@aurestic.es>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
import json
from odoo import Command
from odoo.tests import tagged
from odoo.tools.misc import file_path
from odoo.addons.l10n_es_aeat_sii_oca.tests.test_l10n_es_aeat_sii import (
TestL10nEsAeatSiiBase,
)
from odoo.addons.point_of_sale.tests.common import TestPoSCommon
@tagged("post_install", "-at_install")
class TestSpainPosSii(TestPoSCommon, TestL10nEsAeatSiiBase):
@classmethod
def setUpClass(cls, chart_template_ref=None):
chart_template_ref = chart_template_ref or "es_pymes"
super().setUpClass(chart_template_ref=chart_template_ref)
cls.company.write(
{
"sii_enabled": True,
"sii_test": True,
"sii_method": "manual",
"vat": "ESU2687761C",
"sii_description_method": "manual",
"tax_agency_id": cls.env.ref("l10n_es_aeat.aeat_tax_agency_spain"),
}
)
cls.customer.write(
{
"country_id": cls.env.ref("base.es").id,
"vat": "F35999705",
}
)
cls.fr_country = cls.env.ref("base.fr")
cls.other_customer.write(
{
"country_id": cls.fr_country.id,
"vat": "FR82542065479",
}
)
cls.PosSession = cls.env["pos.session"]
cls.config = cls.basic_config
cls.config.write(
{
"iface_l10n_es_simplified_invoice": True,
"company_id": cls.company.id,
}
)
cls.env.user.write(
{"groups_id": [(3, cls.env.ref("account.group_account_manager").id)]}
)
cls.tax_21b = cls.env.ref(
f"account.{cls.env.user.company_id.id}_account_tax_template_s_iva21b"
)
cls.tax_account = cls.env.ref(
f"account.{cls.env.user.company_id.id}_account_common_477"
)
cls.tax_10b = cls.env.ref(
f"account.{cls.env.user.company_id.id}_account_tax_template_s_iva10b"
)
cls.product21 = cls.create_product(
"Product 21b",
cls.categ_basic,
100.0,
100.0,
tax_ids=cls.tax_21b.ids,
)
cls.product10 = cls.create_product(
"Product 10b",
cls.categ_basic,
100.0,
100.0,
tax_ids=cls.tax_10b.ids,
)
def setUp(self):
super().setUp()
self._create_session_closed()
self.session = self.PosSession.search([], limit=1, order="id desc")
self.order = self.session.order_ids[:1]
@classmethod
def _create_invoice(cls, move_type):
return cls.env["account.move"].create(
{
"company_id": cls.company.id,
"partner_id": cls.partner.id,
"invoice_date": "2018-02-01",
"move_type": move_type,
"invoice_line_ids": [
Command.create(
{
"product_id": cls.product.id,
"account_id": cls.account_expense.id,
"name": "Test line",
"price_unit": 100,
"quantity": 1,
},
)
],
}
)
def _create_session_closed(self):
cash = self.cash_pm1
self._run_test(
{
"payment_methods": cash,
"orders": [
{
"pos_order_lines_ui_args": [(self.product21, 1)],
"payments": [(cash, 121)],
"customer": False,
"is_invoiced": False,
"uid": "00100-010-0001",
},
{
"pos_order_lines_ui_args": [(self.product10, 1)],
"payments": [(cash, 110)],
"customer": self.other_customer,
"is_invoiced": False,
"uid": "00100-010-0002",
},
{
"pos_order_lines_ui_args": [
(self.product21, 1),
(self.product10, 1),
],
"payments": [(cash, 231)],
"customer": self.customer,
"is_invoiced": False,
"uid": "00100-010-0003",
},
],
"journal_entries_before_closing": {},
"journal_entries_after_closing": {
"session_journal_entry": {
"line_ids": [
{
"account_id": self.tax_account.id,
"partner_id": False,
"debit": 0,
"credit": 42,
"reconciled": False,
},
{
"account_id": self.tax_account.id,
"partner_id": False,
"debit": 0,
"credit": 20,
"reconciled": False,
},
{
"account_id": self.sales_account.id,
"partner_id": False,
"debit": 0,
"credit": 200,
"reconciled": False,
},
{
"account_id": self.sales_account.id,
"partner_id": False,
"debit": 0,
"credit": 200,
"reconciled": False,
},
{
"account_id": cash.receivable_account_id.id,
"partner_id": False,
"debit": 462,
"credit": 0,
"reconciled": True,
},
],
},
"cash_statement": [
(
(462,),
{
"line_ids": [
{
"account_id": (
cash.journal_id.default_account_id.id
),
"partner_id": False,
"debit": 462,
"credit": 0,
"reconciled": False,
},
{
"account_id": cash.receivable_account_id.id,
"partner_id": False,
"debit": 0,
"credit": 462,
"reconciled": True,
},
]
},
)
],
"bank_payments": [],
},
}
)
def _compare_sii_dict(self, json_file, order):
"""Helper method for comparing the expected SII dict with ."""
module = "l10n_es_pos_sii"
result_dict = order._get_aeat_invoice_dict()
path = file_path(f"{module}/tests/json/{json_file}")
if not path:
raise Exception("Incorrect JSON file: %s" % json_file)
with open(path) as f:
expected_dict = json.loads(f.read())
self.assertEqual(expected_dict, result_dict)
return order
def test_01_partner_sii_enabled(self):
company_02 = self.env["res.company"].create({"name": "Company 02"})
self.env.user.company_ids += company_02
self.assertTrue(self.partner.sii_enabled)
self.partner.company_id = company_02
self.assertFalse(self.partner.sii_enabled)
def test_02_json_orders(self):
json_by_taxes = {
self.tax_21b: {
"json": "sii_pos_order_iva21b.json",
"name": "Shop0001",
},
self.tax_10b: {
"json": "sii_pos_order_iva10b.json",
"name": "Shop0002",
},
(self.tax_10b + self.tax_21b): {
"json": "sii_pos_order_iva21b_iva10b.json",
"name": "Shop0003",
},
}
for order in self.session.order_ids:
taxes = order.lines.mapped("tax_ids_after_fiscal_position").sorted(
key=lambda tax: tax.amount
)
order.write(
{
"l10n_es_unique_id": json_by_taxes.get(taxes, {}).get("name"),
"date_order": "2023-06-14",
}
)
order.send_sii()
self._compare_sii_dict(json_by_taxes.get(taxes, {}).get("json"), order)
def test_03_is_aeat_simplified_invoice(self):
for order in self.session.order_ids:
self.assertTrue(order._is_aeat_simplified_invoice())
def test_04_sii_description(self):
self.order.company_id.write(
{
"sii_pos_description": "Test POS description",
}
)
session = self.order.session_id
default_partner = self.env.ref("l10n_es.partner_simplified")
order = self.env["pos.order"].create(
{
"company_id": self.order.company_id.id,
"session_id": session.id,
"pricelist_id": default_partner.property_product_pricelist.id,
"partner_id": default_partner.id,
"lines": [
(
0,
0,
{
"name": "TPV/0001",
"product_id": self.product21.id,
"price_unit": 100,
"discount": 0.0,
"qty": 1.0,
"tax_ids": [(6, 0, self.product21.taxes_id.ids)],
"price_subtotal": 100,
"price_subtotal_incl": 100 + 21,
},
)
],
"amount_tax": 21,
"amount_total": 100 + 21,
"amount_paid": 121,
"amount_return": 0,
}
)
order.action_pos_order_paid()
self.assertEqual(order.sii_description, "Test POS description")
def test_05_refund_sii_refund_type(self):
cash = self.cash_pm1
self._start_pos_session(cash, 462.0)
refund_order = self._create_orders(
[
{
"pos_order_lines_ui_args": [(self.product21, -1)],
"payments": [(cash, -121)],
"customer": False,
"is_invoiced": False,
"uid": "00100-010-0004",
},
]
).get("00100-010-0004")
refund_order.write(
{
"l10n_es_unique_id": "Shop0004",
"date_order": "2023-06-14",
}
)
self._compare_sii_dict("sii_pos_order_refund_iva21b.json", refund_order)
self.assertEqual(refund_order.sii_refund_type, "I")
def test_06_automatic_send(self):
self.company.send_mode = "auto"
cash = self.cash_pm1
pos_session = self._start_pos_session(cash, 462.0)
self._create_orders(
[
{
"pos_order_lines_ui_args": [(self.product21, 1)],
"payments": [(cash, 121)],
"customer": False,
"is_invoiced": False,
"uid": "00100-010-0004",
},
]
)
pos_session.post_closing_cash_details(583.0)
sii_send_cron = self.env.ref("l10n_es_aeat_sii_oca.invoice_send_to_sii")
Trigger = self.env["ir.cron.trigger"].sudo()
before = Trigger.search_count([("cron_id", "=", sii_send_cron.id)])
pos_session.close_session_from_ui()
after = Trigger.search_count([("cron_id", "=", sii_send_cron.id)])
self.assertEqual(after, before + 1)
trigger = Trigger.search(
[("cron_id", "=", sii_send_cron.id)], order="id desc", limit=1
)
self.assertTrue(trigger)
self.assertTrue(trigger.call_at)
def test_07_export_for_ui_session_is_closed(self):
cash = self.cash_pm1
pos_session = self._start_pos_session(cash, 462.0)
self._create_orders(
[
{
"pos_order_lines_ui_args": [(self.product21, 1)],
"payments": [(cash, 121)],
"customer": False,
"is_invoiced": False,
"uid": "00100-010-0004",
},
]
)
res = pos_session.order_ids.export_for_ui()
self.assertTrue(
all(
"sii_session_closed" in x and x["sii_session_closed"] is False
for x in res
),
"The session is not closed",
)
pos_session.post_closing_cash_details(583.0)
pos_session.close_session_from_ui()
res = pos_session.order_ids.export_for_ui()
self.assertTrue(
all(
"sii_session_closed" in x and x["sii_session_closed"] is True
for x in res
),
"The session is closed",
)
def test_08_is_aeat_unidentified_document(self):
for order in self.session.order_ids:
self.assertTrue(order._is_aeat_unidentified_document())