Debian Codegraph @

addons/l10n_hu_edi/tests/common.py

raw
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from odoo import Command
4 from odoo.addons.account.tests.common import AccountTestInvoicingCommon
5
6 import datetime
7
8
9 class L10nHuEdiTestCommon(AccountTestInvoicingCommon):
10 @classmethod
11 @AccountTestInvoicingCommon.setup_country('hu')
12 def setUpClass(cls):
13 super().setUpClass()
14
15 cls.today = datetime.date.today()
16 cls.yesterday = cls.today - datetime.timedelta(days=1)
17
18 # Company
19 company = cls.company_data['company']
20 company.write({
21 'city': 'Budapest',
22 'zip': '1073',
23 'street': 'Akácfa utca 74.',
24 'country_id': cls.env.ref('base.hu').id,
25 'vat': '27725414-2-13',
26 })
27
28 cls.write_edi_credentials()
29
30 # Products
31 cls.product_service = cls.env['product.product'].create({
32 'name': 'Consultancy Service',
33 'type': 'service',
34 'uom_id': cls.env.ref('uom.product_uom_hour').id,
35 'property_account_income_id': cls.company_data['default_account_revenue'].id,
36 'property_account_expense_id': cls.company_data['default_account_expense'].id,
37 })
38 cls.product_no_uom = cls.env['product.product'].create({
39 'name': 'Item without UoM',
40 'type': 'service',
41 'property_account_income_id': cls.company_data['default_account_revenue'].id,
42 'property_account_expense_id': cls.company_data['default_account_expense'].id,
43 })
44
45 # Partners
46 cls.partner_company = cls.env['res.partner'].create({
47 'name': 'Magyar Vevő Kft.',
48 'is_company': True,
49 'street': 'Alkotmány utca 11.',
50 'city': 'Debrecen',
51 'zip': '4000',
52 'country_id': cls.env.ref('base.hu').id,
53 'vat': '14933477-2-13',
54 'invoice_edi_format': False,
55 })
56 cls.partner_group_company_1 = cls.env['res.partner'].create({
57 'name': 'MOL Nyrt.',
58 'is_company': True,
59 'street': 'Dombóvári út 28.',
60 'city': 'Budapest',
61 'zip': '1117',
62 'country_id': cls.env.ref('base.hu').id,
63 'vat': '10625790-4-44',
64 'l10n_hu_group_vat': '17781774-5-44',
65 })
66 cls.partner_group_company_2 = cls.env['res.partner'].create({
67 'name': 'MOL Petrolkémia Zrt.',
68 'is_company': True,
69 'street': 'TVK-Ipartelep, TVK Központi Irodaház 2119/3hrsz. 136. ép.',
70 'city': 'Tiszaújváros',
71 'zip': '3581',
72 'country_id': cls.env.ref('base.hu').id,
73 'vat': '10725759-4-05',
74 'l10n_hu_group_vat': '17781774-5-44',
75 })
76
77 # Currency rates
78 currency_eur = cls.env.ref('base.EUR')
79 currency_eur.active = True
80 cls.env['res.currency.rate'].create(
81 {
82 'name': cls.today,
83 'currency_id': currency_eur.id,
84 'company_id': company.id,
85 'inverse_company_rate': '380.77',
86 }
87 )
88 cls.env['res.currency.rate'].create(
89 {
90 'name': cls.yesterday,
91 'currency_id': currency_eur.id,
92 'company_id': company.id,
93 'inverse_company_rate': '377.66',
94 }
95 )
96
97 # Taxes
98 cls.tax_vat = cls.env['account.chart.template'].ref('F27')
99 cls.tax_vat_18 = cls.env['account.chart.template'].ref('F18')
100 cls.tax_aam = cls.env['account.chart.template'].ref('VA')
101 cls.tax_price_include = cls.env['account.tax'].create({
102 'name': 'Excise tax',
103 'amount_type': 'percent',
104 'amount': 30.0,
105 'country_id': company.account_fiscal_country_id.id,
106 'tax_exigibility': 'on_invoice',
107 'price_include_override': 'tax_included',
108 'include_base_amount': True,
109 'l10n_hu_tax_type': 'VAT',
110 'invoice_repartition_line_ids': [
111 Command.create({'repartition_type': 'base'}),
112 Command.create({
113 'repartition_type': 'tax',
114 'account_id': cls.company_data['default_account_tax_sale'].id,
115 }),
116 ],
117 'refund_repartition_line_ids': [
118 Command.create({'repartition_type': 'base'}),
119 Command.create({
120 'repartition_type': 'tax',
121 'account_id': cls.company_data['default_account_tax_sale'].id,
122 }),
123 ],
124 })
125
126 @classmethod
127 def write_edi_credentials(cls):
128 # Set up test EDI user
129 return cls.company_data['company'].write({
130 'l10n_hu_edi_server_mode': 'test',
131 'l10n_hu_edi_username': 'this',
132 'l10n_hu_edi_password': 'that',
133 'l10n_hu_edi_signature_key': 'some_key',
134 'l10n_hu_edi_replacement_key': 'abcdefghijklmnop',
135 })
136
137 def _create_simple_move(self, move_type='out_invoice', currency=None, amount=None):
138 journal = self.company_data['default_journal_sale'] if move_type in self.env['account.move'].get_sale_types() else self.company_data['default_journal_purchase']
139
140 return self.env['account.move'].create({
141 'move_type': move_type,
142 'journal_id': journal.id,
143 'currency_id': (currency or self.env.ref('base.HUF')).id,
144 'partner_id': self.partner_company.id,
145 'invoice_date': self.today,
146 'delivery_date': self.today,
147 'invoice_line_ids': [
148 Command.create({
149 'product_id': self.product_a.id,
150 'price_unit': amount or 10000.0,
151 'quantity': 1,
152 'tax_ids': [Command.set(self.tax_vat.ids)],
153 })
154 ]
155 })
156
157 def create_invoice_simple(self, currency=None, amount=None):
158 """ Create a really basic invoice - just one line. """
159 return self._create_simple_move(move_type='out_invoice', currency=currency, amount=amount)
160
161 def create_bill_simple(self, currency=None):
162 """ Create a really basic bill - just one line. """
163 return self._create_simple_move(move_type='in_invoice', currency=currency)
164
165 def create_credit_note_simple(self, currency=None):
166 """ Create a really basic credit note - just one line. """
167 return self._create_simple_move(move_type='out_refund', currency=currency)
168
169 def create_refund_simple(self, currency=None):
170 """ Create a really basic bill refund - just one line. """
171 return self._create_simple_move(move_type='in_refund', currency=currency)
172
173 def create_invoice_simple_discount(self):
174 """ Create a really basic invoice with a discount - just one line. """
175 return self.env['account.move'].create({
176 'move_type': 'out_invoice',
177 'journal_id': self.company_data['default_journal_sale'].id,
178 'currency_id': self.env.ref('base.HUF').id,
179 'partner_id': self.partner_company.id,
180 'invoice_date': self.today,
181 'delivery_date': self.today,
182 'invoice_line_ids': [
183 Command.create({
184 'product_id': self.product_a.id,
185 'price_unit': 10000.0,
186 'quantity': 1,
187 'discount': 20,
188 'tax_ids': [Command.set(self.tax_vat.ids)],
189 })
190 ]
191 })
192
193 def create_invoice_tax_price_include(self):
194 """ Create an invoice with :
195 * one line using a tax with price included
196 * one line using a tax with price included and a discount
197 """
198 return self.env['account.move'].create({
199 'move_type': 'out_invoice',
200 'journal_id': self.company_data['default_journal_sale'].id,
201 'currency_id': self.env.ref('base.HUF').id,
202 'partner_id': self.partner_company.id,
203 'invoice_date': self.today,
204 'delivery_date': self.today,
205 'invoice_line_ids': [
206 Command.create({
207 'product_id': self.product_a.id,
208 'price_unit': 1000.00,
209 'quantity': 1,
210 'tax_ids': [Command.set(self.tax_price_include.ids)],
211 }),
212 Command.create({
213 'product_id': self.product_a.id,
214 'price_unit': 1000.00,
215 'quantity': 1,
216 'discount': 20,
217 'tax_ids': [Command.set(self.tax_price_include.ids)],
218 }),
219 ]
220 })
221
222 def create_advance_invoice(self):
223 """ Create a sale order and an advance invoice. """
224 self.product_a.invoice_policy = 'order'
225 pricelist_huf = self.env['product.pricelist'].create({
226 'name': 'HUF pricelist',
227 'currency_id': self.company_data['company'].currency_id.id,
228 'company_id': False,
229 })
230 sale_order = self.env['sale.order'].with_context(tracking_disable=True).create({
231 'partner_id': self.partner_company.id,
232 'pricelist_id': pricelist_huf.id,
233 'order_line': [
234 Command.create({
235 'product_id': self.product_a.id,
236 'product_uom_qty': 1,
237 'price_unit': 10000.0,
238 'tax_ids': [Command.set(self.tax_vat.ids)],
239 })
240 ]
241 })
242 sale_order.action_confirm()
243
244 downpayment = self.env['sale.advance.payment.inv'].with_context({
245 'active_model': 'sale.order',
246 'active_ids': [sale_order.id],
247 'active_id': sale_order.id,
248 'default_journal_id': self.company_data['default_journal_sale'].id,
249 }).create({
250 'advance_payment_method': 'fixed',
251 'fixed_amount': 6350.0,
252 })
253 downpayment.create_invoices()
254 return sale_order, sale_order.invoice_ids
255
256 def create_final_invoice(self, sale_order):
257 """ Create a final invoice for a sale order """
258 advance_invoice = sale_order.invoice_ids
259 final_payment = self.env['sale.advance.payment.inv'].with_context({
260 'active_model': 'sale.order',
261 'active_ids': [sale_order.id],
262 'active_id': sale_order.id,
263 'default_journal_id': self.company_data['default_journal_sale'].id,
264 }).create({
265 'advance_payment_method': 'delivered',
266 })
267 final_payment.create_invoices()
268 final_invoice = sale_order.invoice_ids - advance_invoice
269
270 return final_invoice
271
272 def create_invoice_complex_huf(self):
273 """ Create a complex invoice in HUF, with cash rounding. """
274 return self.env['account.move'].create({
275 'move_type': 'out_invoice',
276 'journal_id': self.company_data['default_journal_sale'].id,
277 'currency_id': self.env.ref('base.HUF').id,
278 'partner_id': self.partner_company.id,
279 'invoice_date': self.today,
280 'delivery_date': self.today,
281 'l10n_hu_payment_mode': 'TRANSFER',
282 'invoice_cash_rounding_id': self.env.ref('l10n_hu_edi.cash_rounding_1_huf').id,
283 'invoice_line_ids': [
284 Command.create({
285 'product_id': self.product_a.id,
286 'price_unit': 10.00,
287 'quantity': 3,
288 'discount': 20,
289 'tax_ids': [Command.set(self.tax_vat.ids)],
290 }),
291 Command.create({
292 'product_id': self.product_b.id,
293 'price_unit': 19.99,
294 'quantity': 1,
295 'tax_ids': [Command.set(self.tax_vat.ids)],
296 }),
297 Command.create({
298 'product_id': self.product_service.id,
299 'price_unit': 50.00,
300 'quantity': 2,
301 'tax_ids': [Command.set(self.tax_vat_18.ids)],
302 }),
303 Command.create({
304 'product_id': self.product_no_uom.id,
305 'price_unit': 200.00,
306 'quantity': 1,
307 'tax_ids': [Command.set(self.tax_aam.ids)],
308 })
309 ]
310 })
311
312 def create_invoice_complex_eur(self):
313 """ Create a complex invoice in EUR with several lines, different taxes, and discounts. """
314 return self.env['account.move'].create({
315 'move_type': 'out_invoice',
316 'journal_id': self.company_data['default_journal_sale'].id,
317 'currency_id': self.env.ref('base.EUR').id,
318 'partner_id': self.partner_company.id,
319 'invoice_date': self.yesterday,
320 'delivery_date': self.today,
321 'l10n_hu_payment_mode': 'TRANSFER',
322 'invoice_line_ids': [
323 Command.create({
324 'product_id': self.product_a.id,
325 'price_unit': 10.00,
326 'quantity': 3,
327 'discount': 20,
328 'tax_ids': [Command.set(self.tax_vat.ids)],
329 }),
330 Command.create({
331 'product_id': self.product_b.id,
332 'price_unit': 19.99,
333 'quantity': 1,
334 'tax_ids': [Command.set(self.tax_vat.ids)],
335 }),
336 Command.create({
337 'product_id': self.product_service.id,
338 'price_unit': 50.00,
339 'quantity': 2,
340 'tax_ids': [Command.set(self.tax_vat_18.ids)],
341 }),
342 Command.create({
343 'product_id': self.product_no_uom.id,
344 'price_unit': 200.00,
345 'quantity': 1,
346 'tax_ids': [Command.set(self.tax_aam.ids)],
347 })
348 ]
349 })
350
351 def create_reversal(self, invoice, is_modify=False, amount=None):
352 """ Create a credit note that reverses an invoice. """
353 wizard_vals = {'journal_id': invoice.journal_id.id}
354 wizard_reverse = self.env['account.move.reversal'].with_context(active_ids=invoice.ids, active_model='account.move').create(wizard_vals)
355 wizard_reverse.reverse_moves(is_modify=is_modify)
356 reversal_moves = wizard_reverse.new_move_ids
357 if amount:
358 reversal_moves.invoice_line_ids.write({
359 'price_unit': amount
360 })
361 return reversal_moves
362
363 def create_debit_note(self, invoice, amount=None):
364 """ Create Debit Note """
365 wizard_vals = {
366 'journal_id': invoice.journal_id.id,
367 'copy_lines': True,
368 }
369 move_debit_note_wiz = self.env['account.debit.note'].with_context(active_model="account.move", active_ids=invoice.ids).create(wizard_vals)
370 move_debit_note_wiz.create_debit()
371 debit_note = self.env['account.move'].search([('debit_origin_id', '=', invoice.id)])
372 if amount:
373 debit_note.invoice_line_ids.write({
374 'price_unit': amount
375 })
376 return debit_note
377
378 def register_payment(self, invoice, amount):
379 payment_register = self.env['account.payment.register'].with_context(active_model='account.move', active_ids=invoice.ids).create({
380 'payment_date': self.today,
381 'journal_id': self.company_data['default_journal_bank'].id,
382 'amount': amount,
383 })
384 payment_register.action_create_payments()
385
386 def create_cancel_wizard(self):
387 """ Create an invoice, send it, and create a cancellation wizard for it. """
388 invoice = self.create_invoice_simple()
389 invoice.action_post()
390 send_and_print = self.create_send_and_print(invoice, sending_methods=[])
391 self.assertTrue(send_and_print.extra_edi_checkboxes and send_and_print.extra_edi_checkboxes.get('hu_nav_30', {}).get('checked'))
392 self.assertFalse(invoice._l10n_hu_edi_check_invoices())
393 send_and_print.action_send_and_print()
394 cancel_wizard = self.env['l10n_hu_edi.cancellation'].with_context({"default_invoice_id": invoice.id}).create({
395 'code': 'ERRATIC_DATA',
396 'reason': 'Some reason...',
397 })
398 return invoice, cancel_wizard