"""Test invoices using Form to emulate UI onchange behavior.""" from odoo.tests.common import Form import json results = [] def test_invoice(label, partner_vals, product_name, product_cat_xmlid, price=100.0): env.cr.execute("SAVEPOINT test") try: partner = env['res.partner'].search([('name', '=', partner_vals['name'])], limit=1) if not partner: partner = env['res.partner'].create(partner_vals) cat = env.ref(product_cat_xmlid) product = env['product.product'].search([('name', '=', product_name)], limit=1) if not product: product = env['product.product'].create({ 'name': product_name, 'type': 'service', 'list_price': price, 'categ_id': cat.id, }) with Form(env['account.move'].with_context(default_move_type='out_invoice')) as inv_form: inv_form.partner_id = partner with inv_form.invoice_line_ids.new() as line: line.product_id = product line.quantity = 1 line.price_unit = price inv = inv_form.save() line = inv.invoice_line_ids[0] results.append({ 'label': label, 'fiscal_position': inv.fiscal_position_id.name or '(none)', 'taxes': line.tax_ids.mapped('name'), 'income_account': f"{line.account_id.code} {line.account_id.name}", 'subtotal': inv.amount_untaxed, 'tax_total': inv.amount_tax, 'total': inv.amount_total, 'partner_tags': [c.name for c in partner.category_id], }) finally: env.cr.execute("ROLLBACK TO SAVEPOINT test") test_invoice("Ontario (HST 13%)", {'name': 'TEST CUST ON', 'country_id': env.ref('base.ca').id, 'state_id': env.ref('base.state_ca_on').id, 'customer_rank': 1}, 'TEST SaaS', 'nexa_coa_setup.pc_saas', 100.0) test_invoice("US (Zero-rated)", {'name': 'TEST CUST US', 'country_id': env.ref('base.us').id, 'customer_rank': 1}, 'TEST SaaS', 'nexa_coa_setup.pc_saas', 100.0) test_invoice("Quebec (GST+QST)", {'name': 'TEST CUST QC', 'country_id': env.ref('base.ca').id, 'state_id': env.ref('base.state_ca_qc').id, 'customer_rank': 1}, 'TEST SaaS', 'nexa_coa_setup.pc_saas', 100.0) test_invoice("Intercompany -> Westin", {'name': 'Westin Healthcare Inc'}, 'TEST Consulting', 'nexa_coa_setup.pc_consulting', 150.0) for r in results: print("---") print(json.dumps(r, indent=2, default=str))