| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
import logging |
| 5 |
from lxml import etree |
| 6 |
from freezegun import freeze_time |
| 7 |
from odoo import tools |
| 8 |
from odoo.tests import Form, tagged |
| 9 |
from odoo.addons.l10n_it_edi.tests.common import TestItEdi |
| 10 |
|
| 11 |
_logger = logging.getLogger(__name__) |
| 12 |
|
| 13 |
|
| 14 |
@tagged('post_install_l10n', 'post_install', '-at_install') |
| 15 |
class TestItEdiDDT(TestItEdi): |
| 16 |
|
| 17 |
@classmethod |
| 18 |
def setUpClass(cls): |
| 19 |
super().setUpClass() |
| 20 |
|
| 21 |
|
| 22 |
uom_unit = cls.env.ref('uom.product_uom_unit') |
| 23 |
uom_hour = cls.env.ref('uom.product_uom_hour') |
| 24 |
|
| 25 |
|
| 26 |
cls.tax_22 = cls.env['account.tax'].with_company(cls.company).create({ |
| 27 |
'name': '22% test tax', |
| 28 |
'amount': 22.0, |
| 29 |
'amount_type': 'percent' |
| 30 |
}) |
| 31 |
|
| 32 |
|
| 33 |
cls.default_pricelist = cls.env['product.pricelist'].with_company(cls.company).create({ |
| 34 |
'name': 'default_pricelist', |
| 35 |
'currency_id': cls.company.currency_id.id, |
| 36 |
}) |
| 37 |
product_category = cls.env['product.category'].with_company(cls.company).create({'name': 'Test category'}) |
| 38 |
cls.products = cls.env['product.product'].with_company(cls.company).create([ |
| 39 |
{ |
| 40 |
'name': 'product_service_delivery', |
| 41 |
'categ_id': product_category.id, |
| 42 |
'standard_price': 200.0, |
| 43 |
'list_price': 180.0, |
| 44 |
'type': 'service', |
| 45 |
'uom_id': uom_unit.id, |
| 46 |
'default_code': 'SERV_DEL', |
| 47 |
'invoice_policy': 'delivery', |
| 48 |
'taxes_id': [(6, 0, [])], |
| 49 |
'supplier_taxes_id': [(6, 0, [])], |
| 50 |
}, { |
| 51 |
'name': 'product_service_order', |
| 52 |
'categ_id': product_category.id, |
| 53 |
'standard_price': 40.0, |
| 54 |
'list_price': 90.0, |
| 55 |
'type': 'service', |
| 56 |
'uom_id': uom_hour.id, |
| 57 |
'description': 'Example of product to invoice on order', |
| 58 |
'default_code': 'PRE-PAID', |
| 59 |
'invoice_policy': 'order', |
| 60 |
'taxes_id': [(6, 0, [])], |
| 61 |
'supplier_taxes_id': [(6, 0, [])], |
| 62 |
}, { |
| 63 |
'name': 'product_order_no', |
| 64 |
'categ_id': product_category.id, |
| 65 |
'standard_price': 235.0, |
| 66 |
'list_price': 280.0, |
| 67 |
'type': 'consu', |
| 68 |
'weight': 0.01, |
| 69 |
'uom_id': uom_unit.id, |
| 70 |
'default_code': 'FURN_9999', |
| 71 |
'invoice_policy': 'order', |
| 72 |
'expense_policy': 'no', |
| 73 |
'taxes_id': [(6, 0, [])], |
| 74 |
'supplier_taxes_id': [(6, 0, [])], |
| 75 |
}, { |
| 76 |
'name': 'product_delivery_no', |
| 77 |
'categ_id': product_category.id, |
| 78 |
'standard_price': 55.0, |
| 79 |
'list_price': 70.0, |
| 80 |
'type': 'consu', |
| 81 |
'weight': 0.01, |
| 82 |
'uom_id': uom_unit.id, |
| 83 |
'default_code': 'FURN_7777', |
| 84 |
'invoice_policy': 'delivery', |
| 85 |
'expense_policy': 'no', |
| 86 |
'taxes_id': [(6, 0, [])], |
| 87 |
'supplier_taxes_id': [(6, 0, [])], |
| 88 |
} |
| 89 |
]) |
| 90 |
|
| 91 |
def test_deferred_invoice(self): |
| 92 |
""" Create a sale order with multiple DDTs, and create an invoice with a later date. |
| 93 |
The export has to have the TipoDocumento TD24 for Deferred Invoice. |
| 94 |
""" |
| 95 |
self.env.user.group_ids |= self.env.ref("sales_team.group_sale_salesman") |
| 96 |
|
| 97 |
with freeze_time('2020-02-02 18:00'): |
| 98 |
self.sale_order = self.env['sale.order'].with_company(self.company).create({ |
| 99 |
'partner_id': self.italian_partner_a.id, |
| 100 |
'partner_invoice_id': self.italian_partner_a.id, |
| 101 |
'partner_shipping_id': self.italian_partner_a.id, |
| 102 |
'order_line': [ |
| 103 |
(0, 0, { |
| 104 |
'name': product.name, |
| 105 |
'product_id': product.id, |
| 106 |
'product_uom_qty': 5, |
| 107 |
'price_unit': product.list_price, |
| 108 |
'tax_ids': self.tax_22 |
| 109 |
}) for product in self.products |
| 110 |
], |
| 111 |
'pricelist_id': self.default_pricelist.id, |
| 112 |
'picking_policy': 'direct', |
| 113 |
'name': 'SO-ITDDT0001', |
| 114 |
}) |
| 115 |
self.sale_order.action_confirm() |
| 116 |
|
| 117 |
|
| 118 |
for _i in range(2): |
| 119 |
self._create_delivery(self.sale_order, 1) |
| 120 |
|
| 121 |
|
| 122 |
with freeze_time('2020-02-03 09:00'): |
| 123 |
deferred_invoice = self.sale_order._create_invoices() |
| 124 |
deferred_invoice.action_post() |
| 125 |
|
| 126 |
|
| 127 |
invoice_xml = deferred_invoice._l10n_it_edi_render_xml() |
| 128 |
expected_xml = self._get_stock_ddt_test_file_content("deferred_invoice.xml") |
| 129 |
result = etree.fromstring(invoice_xml) |
| 130 |
expected = etree.fromstring(expected_xml) |
| 131 |
self.assertXmlTreeEqual(result, expected) |
| 132 |
|
| 133 |
def _create_delivery(self, sale_order, qty=1): |
| 134 |
""" Create a picking of a limited quantity and create a backorder """ |
| 135 |
pickings = sale_order.picking_ids.filtered(lambda picking: picking.state != 'done') |
| 136 |
pickings.move_ids.write({'quantity': qty}) |
| 137 |
wizard_action = pickings.button_validate() |
| 138 |
context = wizard_action['context'] |
| 139 |
wizard = Form(self.env['stock.backorder.confirmation'].with_context(context)) |
| 140 |
confirm_dialog = wizard.save() |
| 141 |
confirm_dialog.process() |
| 142 |
|
| 143 |
@classmethod |
| 144 |
def _get_stock_ddt_test_file_content(cls, filename): |
| 145 |
""" Get the content of a test file inside this module """ |
| 146 |
path = 'l10n_it_stock_ddt/tests/expected_xmls/' + filename |
| 147 |
with tools.file_open(path, mode='rb') as test_file: |
| 148 |
return test_file.read() |