| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
|
| 3 |
from unittest import skip |
| 4 |
|
| 5 |
from odoo.exceptions import ValidationError |
| 6 |
from odoo.addons.stock.tests.common import TestStockCommon |
| 7 |
|
| 8 |
|
| 9 |
@skip('Temporary to fast merge new valuation') |
| 10 |
class TestAnalytics(TestStockCommon): |
| 11 |
|
| 12 |
@classmethod |
| 13 |
def setUpClass(cls): |
| 14 |
super().setUpClass() |
| 15 |
cls.plan1, cls.plan2 = cls.env['account.analytic.plan'].create([{'name': 'Plan 1'}, {'name': 'Plan 2'}]) |
| 16 |
cls.plan1_name = cls.plan1._column_name() |
| 17 |
cls.plan2_name = cls.plan2._column_name() |
| 18 |
cls.analytic_account1, cls.analytic_account2 = cls.env['account.analytic.account'].create([ |
| 19 |
{ |
| 20 |
'name': 'Account 1', |
| 21 |
'plan_id': cls.plan1.id, |
| 22 |
}, |
| 23 |
{ |
| 24 |
'name': 'Account 2', |
| 25 |
'plan_id': cls.plan2.id, |
| 26 |
}, |
| 27 |
]) |
| 28 |
cls.project = cls.env['project.project'].create({ |
| 29 |
'name': 'Project', |
| 30 |
cls.plan1_name: cls.analytic_account1.id, |
| 31 |
cls.plan2_name: cls.analytic_account2.id, |
| 32 |
}) |
| 33 |
# Remove the analytic account auto-generated when creating a timesheetable project if it exists |
| 34 |
cls.project.account_id = False |
| 35 |
cls.product1, cls.product2 = cls.env['product.product'].create([ |
| 36 |
{ |
| 37 |
'name': 'product1', |
| 38 |
'standard_price': 100.0, |
| 39 |
}, |
| 40 |
{ |
| 41 |
'name': 'product2', |
| 42 |
'standard_price': 200.0, |
| 43 |
}, |
| 44 |
]) |
| 45 |
|
| 46 |
def test_analytic_lines_generation_delivery(self): |
| 47 |
picking_out = self.PickingObj.create({ |
| 48 |
'picking_type_id': self.picking_type_out.id, |
| 49 |
'location_id': self.stock_location.id, |
| 50 |
'location_dest_id': self.customer_location.id, |
| 51 |
'project_id': self.project.id, |
| 52 |
}) |
| 53 |
picking_out.picking_type_id.analytic_costs = True |
| 54 |
move_values = { |
| 55 |
'product_uom': self.uom_unit.id, |
| 56 |
'picking_id': picking_out.id, |
| 57 |
'location_id': self.stock_location.id, |
| 58 |
'location_dest_id': self.customer_location.id, |
| 59 |
} |
| 60 |
self.MoveObj.create([ |
| 61 |
{ |
| 62 |
**move_values, |
| 63 |
'product_id': self.product1.id, |
| 64 |
'product_uom_qty': 3, |
| 65 |
}, |
| 66 |
{ |
| 67 |
**move_values, |
| 68 |
'product_id': self.product2.id, |
| 69 |
'product_uom_qty': 5, |
| 70 |
}, |
| 71 |
]) |
| 72 |
picking_out.action_confirm() |
| 73 |
picking_out.button_validate() |
| 74 |
|
| 75 |
analytic_lines = picking_out.move_ids.analytic_account_line_ids |
| 76 |
|
| 77 |
analytic_line1 = analytic_lines.filtered(lambda a: a.product_id == self.product1) |
| 78 |
self.assertEqual(analytic_line1.amount, -300.0) |
| 79 |
self.assertEqual(analytic_line1[self.plan1_name], self.analytic_account1) |
| 80 |
self.assertEqual(analytic_line1[self.plan2_name], self.analytic_account2) |
| 81 |
|
| 82 |
analytic_line2 = analytic_lines.filtered(lambda a: a.product_id == self.product2) |
| 83 |
self.assertEqual(analytic_line2.amount, -1000.0) |
| 84 |
self.assertEqual(analytic_line2[self.plan1_name], self.analytic_account1) |
| 85 |
self.assertEqual(analytic_line2[self.plan2_name], self.analytic_account2) |
| 86 |
|
| 87 |
def test_analytic_lines_generation_receipt(self): |
| 88 |
""" |
| 89 |
In this module, the project profitability should be computed while checking the AAL data from the pickings. |
| 90 |
When the 'analytic costs' option from delivery order is enabled, it is expected for picking to generate |
| 91 |
an aal for the move line created. These aals should be taken into account when computing the 'project |
| 92 |
profitability' right side panel and displayed under the 'costs -> materials' section. |
| 93 |
""" |
| 94 |
picking_in = self.PickingObj.create({ |
| 95 |
'picking_type_id': self.picking_type_in.id, |
| 96 |
'location_id': self.supplier_location.id, |
| 97 |
'location_dest_id': self.stock_location.id, |
| 98 |
'project_id': self.project.id, |
| 99 |
}) |
| 100 |
picking_in.picking_type_id.analytic_costs = True |
| 101 |
move_values = { |
| 102 |
'product_uom': self.uom_unit.id, |
| 103 |
'picking_id': picking_in.id, |
| 104 |
'location_id': self.supplier_location.id, |
| 105 |
'location_dest_id': self.stock_location.id, |
| 106 |
} |
| 107 |
self.MoveObj.create([ |
| 108 |
{ |
| 109 |
**move_values, |
| 110 |
'product_id': self.product1.id, |
| 111 |
'product_uom_qty': 3, |
| 112 |
}, |
| 113 |
{ |
| 114 |
**move_values, |
| 115 |
'product_id': self.product2.id, |
| 116 |
'product_uom_qty': 5, |
| 117 |
}, |
| 118 |
]) |
| 119 |
picking_in.action_confirm() |
| 120 |
picking_in.button_validate() |
| 121 |
|
| 122 |
analytic_lines = picking_in.move_ids.analytic_account_line_ids |
| 123 |
|
| 124 |
analytic_line1 = analytic_lines.filtered(lambda a: a.product_id == self.product1) |
| 125 |
self.assertEqual(analytic_line1.amount, 300.0) |
| 126 |
self.assertEqual(analytic_line1[self.plan1_name], self.analytic_account1) |
| 127 |
self.assertEqual(analytic_line1[self.plan2_name], self.analytic_account2) |
| 128 |
|
| 129 |
analytic_line2 = analytic_lines.filtered(lambda a: a.product_id == self.product2) |
| 130 |
self.assertEqual(analytic_line2.amount, 1000.0) |
| 131 |
self.assertEqual(analytic_line2[self.plan1_name], self.analytic_account1) |
| 132 |
self.assertEqual(analytic_line2[self.plan2_name], self.analytic_account2) |
| 133 |
|
| 134 |
self.assertDictEqual( |
| 135 |
self.project._get_profitability_items(False), |
| 136 |
{ |
| 137 |
'revenues': {'data': [], 'total': {'invoiced': 0.0, 'to_invoice': 0.0}}, |
| 138 |
'costs': { |
| 139 |
'data': [{'id': 'other_costs', 'sequence': 15, 'billed': 1300.0, 'to_bill': 0.0}], |
| 140 |
'total': {'billed': 1300.0, 'to_bill': 0.0} |
| 141 |
} |
| 142 |
} |
| 143 |
) |
| 144 |
|
| 145 |
def test_mandatory_analytic_plan_picking(self): |
| 146 |
self.env['account.analytic.applicability'].create({ |
| 147 |
'business_domain': 'stock_picking', |
| 148 |
'analytic_plan_id': self.plan1.id, |
| 149 |
'applicability': 'mandatory', |
| 150 |
}) |
| 151 |
picking_in = self.PickingObj.create({ |
| 152 |
'picking_type_id': self.picking_type_in.id, |
| 153 |
'location_id': self.supplier_location.id, |
| 154 |
'location_dest_id': self.stock_location.id, |
| 155 |
'project_id': self.project.id, |
| 156 |
}) |
| 157 |
picking_in.picking_type_id.analytic_costs = True |
| 158 |
self.project[self.plan1_name] = False # Remove the mandatory plan from the project linked to the picking |
| 159 |
self.MoveObj.create({ |
| 160 |
'product_uom': self.uom_unit.id, |
| 161 |
'picking_id': picking_in.id, |
| 162 |
'location_id': self.supplier_location.id, |
| 163 |
'location_dest_id': self.stock_location.id, |
| 164 |
'product_id': self.product2.id, |
| 165 |
'product_uom_qty': 5, |
| 166 |
}) |
| 167 |
picking_in.action_confirm() |
| 168 |
with self.assertRaises(ValidationError): |
| 169 |
picking_in.button_validate() # A missing mandatory plan is required on the project linked to the picking |