|
2 | 2 | # Robin Keunen <[email protected]>
|
3 | 3 | # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
|
4 | 4 |
|
| 5 | +import datetime |
5 | 6 |
|
6 |
| -from odoo import tests |
7 |
| -from odoo.tests.common import Form |
| 7 | +from odoo import fields, tests |
8 | 8 |
|
9 | 9 |
|
10 | 10 | class TestAccountInvoice(tests.common.TransactionCase):
|
11 |
| - def test_move_line_product_is_added_to_invoice(self): |
12 |
| - purchase_order = self.browse_ref("purchase.purchase_order_4") |
13 |
| - product = self.browse_ref("product.product_product_1") |
14 |
| - supplier_loc = self.env.ref("stock.stock_location_suppliers") |
15 |
| - stock_loc = self.env.ref("stock.warehouse0").lot_stock_id |
| 11 | + @classmethod |
| 12 | + def setUpClass(cls): |
| 13 | + super().setUpClass() |
| 14 | + |
| 15 | + cls.purchase_order = cls.env.ref("purchase.purchase_order_4") |
| 16 | + cls.product = cls.env.ref("product.product_product_1") |
| 17 | + cls.supplier_loc = cls.env.ref("stock.stock_location_suppliers") |
| 18 | + cls.stock_loc = cls.env.ref("stock.warehouse0").lot_stock_id |
16 | 19 |
|
17 | 20 | # Confirm order
|
18 |
| - purchase_order.button_confirm() |
19 |
| - picking = purchase_order.picking_ids |
20 |
| - moves = picking.move_ids_without_package |
21 |
| - self.assertEquals(len(purchase_order.order_line), len(moves)) |
| 21 | + cls.purchase_order.button_confirm() |
| 22 | + cls.picking = cls.purchase_order.picking_ids |
| 23 | + cls.moves = cls.picking.move_ids |
| 24 | + |
| 25 | + cls.moves[0].quantity_done = cls.moves[0].product_qty |
| 26 | + cls.moves[1].quantity_done = cls.moves[1].product_qty |
| 27 | + cls.moves[2].quantity_done = cls.moves[2].product_qty - 1 |
| 28 | + |
| 29 | + def _validate_picking(self): |
| 30 | + wizard = self.picking.button_validate() |
| 31 | + backorder_confirmation = ( |
| 32 | + self.env[wizard["res_model"]].with_context(**wizard["context"]).create({}) |
| 33 | + ) |
| 34 | + backorder_confirmation.process_cancel_backorder() |
22 | 35 |
|
23 |
| - moves[0].quantity_done = moves[0].product_qty |
24 |
| - moves[1].quantity_done = moves[1].product_qty |
25 |
| - moves[2].quantity_done = moves[2].product_qty - 1 |
| 36 | + def _create_invoice(self): |
| 37 | + res = self.purchase_order.action_create_invoice() |
| 38 | + return self.env["account.move"].browse(res["res_id"]) |
26 | 39 |
|
| 40 | + def test_move_line_product_is_added_to_invoice(self): |
27 | 41 | new_move = self.env["stock.move"].create(
|
28 | 42 | {
|
29 |
| - "picking_id": picking.id, |
30 |
| - "name": product.name, |
31 |
| - "product_id": product.id, |
32 |
| - "product_uom": product.uom_id.id, |
| 43 | + "picking_id": self.picking.id, |
| 44 | + "name": self.product.name, |
| 45 | + "product_id": self.product.id, |
| 46 | + "product_uom": self.product.uom_id.id, |
33 | 47 | "quantity_done": 20,
|
34 |
| - "location_id": supplier_loc.id, |
35 |
| - "location_dest_id": stock_loc.id, |
| 48 | + "location_id": self.supplier_loc.id, |
| 49 | + "location_dest_id": self.stock_loc.id, |
36 | 50 | }
|
37 | 51 | )
|
| 52 | + self._validate_picking() |
38 | 53 |
|
39 |
| - wizard = picking.button_validate() |
40 |
| - backorder_confirmation = self.env[wizard["res_model"]].browse( |
41 |
| - wizard["res_id"] |
42 |
| - ) |
43 |
| - backorder_confirmation.process_cancel_backorder() |
44 |
| - |
45 |
| - res = purchase_order.with_context( |
46 |
| - create_bill=True |
47 |
| - ).action_view_invoice() |
48 |
| - ctx = res.get("context") |
49 |
| - f = Form( |
50 |
| - self.env["account.invoice"].with_context(ctx), |
51 |
| - view="account.invoice_supplier_form", |
52 |
| - ) |
53 |
| - invoice = f.save() |
| 54 | + invoice = self._create_invoice() |
54 | 55 |
|
55 | 56 | self.assertRecordValues(
|
56 | 57 | invoice.invoice_line_ids,
|
57 | 58 | [
|
58 | 59 | {
|
59 |
| - "product_id": moves[0].product_id.id, |
60 |
| - "quantity": moves[0].quantity_done, |
| 60 | + "product_id": self.moves[0].product_id.id, |
| 61 | + "quantity": self.moves[0].quantity_done, |
61 | 62 | },
|
62 | 63 | {
|
63 |
| - "product_id": moves[1].product_id.id, |
64 |
| - "quantity": moves[1].quantity_done, |
| 64 | + "product_id": self.moves[1].product_id.id, |
| 65 | + "quantity": self.moves[1].quantity_done, |
65 | 66 | },
|
66 | 67 | {
|
67 |
| - "product_id": moves[2].product_id.id, |
68 |
| - "quantity": moves[2].quantity_done, |
| 68 | + "product_id": self.moves[2].product_id.id, |
| 69 | + "quantity": self.moves[2].quantity_done, |
| 70 | + }, |
| 71 | + { |
| 72 | + "product_id": new_move.product_id.id, |
| 73 | + "quantity": new_move.quantity_done, |
| 74 | + "price_unit": self.product.lst_price, |
| 75 | + "product_uom_id": new_move.product_uom.id, |
| 76 | + "display_type": "product", |
69 | 77 | },
|
| 78 | + ], |
| 79 | + ) |
| 80 | + |
| 81 | + def test_with_supplierinfo(self): |
| 82 | + """Same test, but the price is now defined on supplierinfo instead of on |
| 83 | + the product. |
| 84 | + """ |
| 85 | + self.env["product.supplierinfo"].create( |
| 86 | + { |
| 87 | + "product_id": self.product.id, |
| 88 | + "product_tmpl_id": self.product.product_tmpl_id.id, |
| 89 | + "partner_id": self.purchase_order.partner_id.id, |
| 90 | + "price": 1, |
| 91 | + "currency_id": self.purchase_order.currency_id.id, |
| 92 | + "date_start": fields.Date.today() - datetime.timedelta(days=90), |
| 93 | + "date_end": fields.Date.today() + datetime.timedelta(days=90), |
| 94 | + } |
| 95 | + ) |
| 96 | + new_move = self.env["stock.move"].create( |
| 97 | + { |
| 98 | + "picking_id": self.picking.id, |
| 99 | + "name": self.product.name, |
| 100 | + "product_id": self.product.id, |
| 101 | + "product_uom": self.product.uom_id.id, |
| 102 | + "quantity_done": 20, |
| 103 | + "location_id": self.supplier_loc.id, |
| 104 | + "location_dest_id": self.stock_loc.id, |
| 105 | + } |
| 106 | + ) |
| 107 | + self._validate_picking() |
| 108 | + |
| 109 | + invoice = self._create_invoice() |
| 110 | + |
| 111 | + self.assertRecordValues( |
| 112 | + invoice.invoice_line_ids[-1], |
| 113 | + [ |
70 | 114 | {
|
71 | 115 | "product_id": new_move.product_id.id,
|
72 | 116 | "quantity": new_move.quantity_done,
|
| 117 | + "price_unit": 1, |
| 118 | + "product_uom_id": new_move.product_uom.id, |
| 119 | + "display_type": "product", |
73 | 120 | },
|
74 | 121 | ],
|
75 | 122 | )
|
0 commit comments