| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from odoo.tests.common import TransactionCase |
| 5 |
from odoo.tests import Form |
| 6 |
from odoo import Command |
| 7 |
|
| 8 |
|
| 9 |
class TestPurchaseRequisitionSale(TransactionCase): |
| 10 |
|
| 11 |
@classmethod |
| 12 |
def setUpClass(cls): |
| 13 |
super().setUpClass() |
| 14 |
|
| 15 |
cls.client = cls.env['res.partner'].create({'name': 'Client'}) |
| 16 |
cls.vendor_1 = cls.env['res.partner'].create({'name': 'Vendor 1'}) |
| 17 |
cls.vendor_2 = cls.env['res.partner'].create({'name': 'Vendor 2'}) |
| 18 |
|
| 19 |
cls.sub_service = cls.env['product.product'].create({ |
| 20 |
'name': 'Subcontracted service', |
| 21 |
'type': 'service', |
| 22 |
'seller_ids': [Command.create({ |
| 23 |
'partner_id': cls.vendor_1.id, |
| 24 |
'price': 10.0, |
| 25 |
'delay': 0, |
| 26 |
})], |
| 27 |
'service_to_purchase': True, |
| 28 |
}) |
| 29 |
|
| 30 |
def test_01_purchase_requisition_services(self): |
| 31 |
""" Create an alternative RFQ for a RFQ automatically genrated from a sale order containing a service that |
| 32 |
has the "service_to_purchase" activated. |
| 33 |
""" |
| 34 |
|
| 35 |
sale_order = self.env['sale.order'].create({ |
| 36 |
'partner_id': self.client.id, |
| 37 |
'order_line': [ |
| 38 |
Command.create({ |
| 39 |
'product_id': self.sub_service.id, |
| 40 |
'product_uom_qty': 5, |
| 41 |
}) |
| 42 |
] |
| 43 |
}) |
| 44 |
sale_order.action_confirm() |
| 45 |
self.assertEqual(sale_order.purchase_order_count, 1, "A RFQ should be created, since `service_to_purchase` has been activated for this product") |
| 46 |
purchase_order = sale_order._get_purchase_orders() |
| 47 |
self.assertEqual(len(purchase_order), 1, "There should be only one Purchase Order linked to this Sale Order") |
| 48 |
|
| 49 |
|
| 50 |
action = purchase_order.action_create_alternative() |
| 51 |
alt_po_wizard = Form(self.env['purchase.requisition.create.alternative'].with_context(**action['context'])) |
| 52 |
alt_po_wizard.partner_ids = self.vendor_2 |
| 53 |
alt_po_wizard.copy_products = True |
| 54 |
alt_po_wizard = alt_po_wizard.save() |
| 55 |
alt_po_wizard.action_create_alternative() |
| 56 |
self.assertEqual(len(purchase_order.alternative_po_ids), 2, "Base PO should be linked with the alternative PO") |
| 57 |
|
| 58 |
|
| 59 |
alt_po = purchase_order.alternative_po_ids.filtered(lambda po: po.id != purchase_order.id) |
| 60 |
linked_so = alt_po._get_sale_orders() |
| 61 |
self.assertEqual(len(linked_so), 1, "The Sale Order from the original Purchase Order should be linked") |
| 62 |
self.assertEqual(linked_so.id, sale_order.id, "The Sale Order linked to the alternative PO must be the same as the original one") |
| 63 |
self.assertEqual(sale_order.purchase_order_count, 2, "Both the original PO and the alternative one should be there") |