| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from odoo.fields import Command |
| 5 |
from odoo.addons.sale.tests.common import SaleCommon |
| 6 |
|
| 7 |
|
| 8 |
class TestSaleMargin(SaleCommon): |
| 9 |
|
| 10 |
def test_sale_margin(self): |
| 11 |
""" Test the sale_margin module in Odoo. """ |
| 12 |
self.product.standard_price = 700.0 |
| 13 |
order = self.empty_order |
| 14 |
|
| 15 |
order.order_line = [ |
| 16 |
Command.create({ |
| 17 |
'price_unit': 1000.0, |
| 18 |
'product_uom_qty': 10.0, |
| 19 |
'product_id': self.product.id, |
| 20 |
}), |
| 21 |
] |
| 22 |
|
| 23 |
order.action_confirm() |
| 24 |
|
| 25 |
self.assertEqual(order.margin, 3000.00, "Sales order profit should be 6000.00") |
| 26 |
self.assertEqual(order.margin_percent, 0.3, "Sales order margin should be 30%") |
| 27 |
|
| 28 |
def test_negative_margin(self): |
| 29 |
""" Test the margin when sales price is less then cost.""" |
| 30 |
order = self.empty_order |
| 31 |
self.service_product.standard_price = 40.0 |
| 32 |
|
| 33 |
order.order_line = [ |
| 34 |
Command.create({ |
| 35 |
'price_unit': 20.0, |
| 36 |
'product_uom_qty': 1.0, |
| 37 |
'state': 'draft', |
| 38 |
'product_id': self.service_product.id, |
| 39 |
}), |
| 40 |
Command.create({ |
| 41 |
'price_unit': -100.0, |
| 42 |
'purchase_price': 0.0, |
| 43 |
'product_uom_qty': 1.0, |
| 44 |
'state': 'draft', |
| 45 |
'product_id': self.product.id, |
| 46 |
}), |
| 47 |
] |
| 48 |
|
| 49 |
order.action_confirm() |
| 50 |
|
| 51 |
self.assertEqual(order.order_line[0].margin, -20.00, "Sales order profit should be -20.00") |
| 52 |
self.assertEqual(order.order_line[0].margin_percent, -1, "Sales order margin percentage should be -100%") |
| 53 |
self.assertEqual(order.order_line[1].margin, -100.00, "Sales order profit should be -100.00") |
| 54 |
self.assertEqual(order.order_line[1].margin_percent, 1.00, "Sales order margin should be 100% when the cost is zero and price defined") |
| 55 |
|
| 56 |
self.assertEqual(order.margin, -120.00, "Sales order margin should be -120.00") |
| 57 |
self.assertEqual(order.margin_percent, 1.5, "Sales order margin should be 150%") |
| 58 |
|
| 59 |
def test_margin_no_cost(self): |
| 60 |
""" Test the margin when cost is 0 margin percentage should always be 100%.""" |
| 61 |
order = self.empty_order |
| 62 |
order.order_line = [Command.create({ |
| 63 |
'product_id': self.product.id, |
| 64 |
'price_unit': 70.0, |
| 65 |
'product_uom_qty': 1.0, |
| 66 |
})] |
| 67 |
|
| 68 |
|
| 69 |
self.assertEqual(order.order_line[0].margin, 70.00, "Sales order profit should be 70.00") |
| 70 |
self.assertEqual(order.order_line[0].margin_percent, 1.0, "Sales order margin percentage should be 100.00") |
| 71 |
|
| 72 |
self.assertEqual(order.margin, 70.00, "Sales order profit should be 70.00") |
| 73 |
self.assertEqual(order.margin_percent, 1.00, "Sales order margin percentage should be 100.00") |
| 74 |
|
| 75 |
def test_margin_considering_product_qty(self): |
| 76 |
""" Test the margin and margin percentage when product with multiple quantity""" |
| 77 |
order = self.empty_order |
| 78 |
self.service_product.standard_price = 50.0 |
| 79 |
|
| 80 |
order.order_line = [ |
| 81 |
Command.create({ |
| 82 |
'price_unit': 100.0, |
| 83 |
'product_uom_qty': 3.0, |
| 84 |
'product_id': self.service_product.id, |
| 85 |
}), |
| 86 |
Command.create({ |
| 87 |
'price_unit': -50.0, |
| 88 |
'product_uom_qty': 1.0, |
| 89 |
'product_id': self.product.id, |
| 90 |
}), |
| 91 |
] |
| 92 |
|
| 93 |
|
| 94 |
order.action_confirm() |
| 95 |
|
| 96 |
self.assertEqual(order.order_line[0].margin, 150.00, "Sales order profit should be 150.00") |
| 97 |
self.assertEqual(order.order_line[0].margin_percent, 0.5, "Sales order margin should be 100%") |
| 98 |
self.assertEqual(order.order_line[1].margin, -50.00, "Sales order profit should be -50.00") |
| 99 |
self.assertEqual(order.order_line[1].margin_percent, 1.0, "Sales order margin should be 100%") |
| 100 |
|
| 101 |
self.assertEqual(order.margin, 100.00, "Sales order profit should be 100.00") |
| 102 |
self.assertEqual(order.margin_percent, 0.4, "Sales order margin should be 40%") |
| 103 |
|
| 104 |
def test_sale_margin_order_copy(self): |
| 105 |
"""When we copy a sales order, its margins should be update to meet the current costs""" |
| 106 |
order = self.empty_order |
| 107 |
self.pricelist.currency_id = self.env.company.currency_id |
| 108 |
|
| 109 |
self.product.standard_price = 500.0 |
| 110 |
order.order_line = [ |
| 111 |
Command.create({ |
| 112 |
'price_unit': 1000.0, |
| 113 |
'product_uom_qty': 10.0, |
| 114 |
'product_id': self.product.id, |
| 115 |
}), |
| 116 |
] |
| 117 |
self.assertAlmostEqual(500.0, order.order_line.purchase_price) |
| 118 |
self.assertAlmostEqual(5000.0, order.order_line.margin) |
| 119 |
self.assertAlmostEqual(.5, order.order_line.margin_percent) |
| 120 |
|
| 121 |
|
| 122 |
self.product.standard_price = 750.0 |
| 123 |
following_sale = order.copy() |
| 124 |
self.assertAlmostEqual(750.0, following_sale.order_line.purchase_price) |
| 125 |
self.assertAlmostEqual(2500.0, following_sale.order_line.margin) |
| 126 |
self.assertAlmostEqual(.25, following_sale.order_line.margin_percent) |