Skip to content

Commit

Permalink
[WIP] Toout fonctionne + tests aussi
Browse files Browse the repository at this point in the history
  • Loading branch information
quentinDupont committed Nov 4, 2019
1 parent 4597a59 commit eb02a51
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 49 deletions.
4 changes: 2 additions & 2 deletions stock_inventory_merge/demo/product_product.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
<record id="stockable_product" model="product.product">
<field name="name">My Stockable Product</field>
<field name="type">product</field>
<field name="uom_id" ref="product.product_uom_unit"/>
<field name="uom_po_id" ref="product.product_uom_unit"/>
<field name="uom_id" ref="uom.product_uom_unit"/>
<field name="uom_po_id" ref="uom.product_uom_unit"/>
<field name="categ_id" ref="product.product_category_5"/>
</record>

Expand Down
2 changes: 1 addition & 1 deletion stock_inventory_merge/demo/res_groups.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
<field name="users" eval="[(4, ref('base.user_admin'))]"/>
</record>

<record id="stock.group_locations" model="res.groups">
<record id="stock.group_stock_multi_locations" model="res.groups">
<field name="users" eval="[(4, ref('base.user_admin'))]"/>
</record>

Expand Down
12 changes: 6 additions & 6 deletions stock_inventory_merge/demo/stock_inventory.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

<record id="inventory_line_1_1" model="stock.inventory.line">
<field name="product_id" ref="stockable_product"/>
<field name="product_uom_id" ref="product.product_uom_unit"/>
<field name="product_uom_id" ref="uom.product_uom_unit"/>
<field name="inventory_id" ref="inventory_1"/>
<field name="product_qty">20</field>
<field name="location_id" ref="stock.stock_location_stock"/>
</record>

<record id="inventory_line_1_2" model="stock.inventory.line">
<field name="product_id" ref="stockable_product"/>
<field name="product_uom_id" ref="product.product_uom_dozen"/>
<field name="product_uom_id" ref="uom.product_uom_dozen"/>
<field name="inventory_id" ref="inventory_1"/>
<field name="product_qty">1</field>
<field name="location_id" ref="stock.stock_location_stock"/>
</record>

<record id="inventory_line_1_3" model="stock.inventory.line">
<field name="product_id" ref="product.product_product_24"/>
<field name="product_uom_id" ref="product.product_uom_unit"/>
<field name="product_uom_id" ref="uom.product_uom_unit"/>
<field name="inventory_id" ref="inventory_1"/>
<field name="product_qty">16.0</field>
<field name="location_id" ref="stock.stock_location_stock"/>
Expand All @@ -44,14 +44,14 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

<record id="inventory_line_2_1" model="stock.inventory.line">
<field name="product_id" ref="stockable_product"/>
<field name="product_uom_id" ref="product.product_uom_unit"/>
<field name="product_uom_id" ref="uom.product_uom_unit"/>
<field name="inventory_id" ref="inventory_2"/>
<field name="product_qty">30</field>
<field name="location_id" ref="stock.stock_location_stock"/>
</record>

<function model="stock.inventory" name="prepare_inventory">
<!-- <function model="stock.inventory" name="prepare_inventory">
<function eval="[[('id', 'in', [ref('inventory_1'), ref('inventory_2')])]]" model="stock.inventory" name="search"/>
</function>
</function> -->

</odoo>
20 changes: 11 additions & 9 deletions stock_inventory_merge/models/stock_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def _compute_duplicates_qty(self):

# Overload Section
@api.multi
def action_done(self):
def action_validate(self):
inventories = self.filtered(lambda x: x.duplicates_qty)
if inventories:
raise UserError(
Expand All @@ -45,7 +45,7 @@ def action_done(self):
% (", ".join([x.name for x in inventories]))
)
)
return super(StockInventory, self).action_done()
return super(StockInventory, self).action_validate()

# Action Section
@api.multi
Expand All @@ -68,7 +68,7 @@ def action_view_duplicates(self):
def complete_with_zero(self):
line_obj = self.env["stock.inventory.line"]
for inventory in self:
product_lines = inventory._get_inventory_lines(inventory)
product_lines = inventory._get_inventory_lines_values()
current_vals = inventory._get_inventory_line_vals()
for product_line in product_lines:
# Check if the line is in the inventory
Expand All @@ -77,16 +77,18 @@ def complete_with_zero(self):
if self._get_inventory_line_keys(
item
) == self._get_inventory_line_keys(product_line):
print("=> FOUND")
found = True
continue
if not found:
# Add the line, if inventory line was not found
product_line["product_qty"] = 0
product_line["inventory_id"] = self.id
line_obj.create(product_line)

@api.multi
def action_merge_duplicated_line(self):
uom_obj = self.env["product.uom"]
uom_obj = self.env["uom.uom"]
line_obj = self.env["stock.inventory.line"]
for inventory in self:
line_group_ids = inventory._get_duplicated_line_ids()
Expand All @@ -95,7 +97,7 @@ def action_merge_duplicated_line(self):
keeped_line_id = False
default_uom_id = False
for line_data in line_obj.search_read(
[("id", "in", line_ids)], ["product_qty", "product_uom_id"]
[("id", "in", line_ids)], ["product_uom_id", "product_qty"]
):
if not keeped_line_id:
keeped_line_id = line_data["id"]
Expand All @@ -106,10 +108,10 @@ def action_merge_duplicated_line(self):
if default_uom_id == line_data["product_uom_id"][0]:
sum_quantity += line_data["product_qty"]
else:
sum_quantity += uom_obj._compute_qty(
line_data["product_uom_id"][0],
uom_id = line_data["product_uom_id"][0]
sum_quantity += uom_obj.browse(uom_id)._compute_quantity(
line_data["product_qty"],
to_uom_id=default_uom_id,
uom_obj.browse(default_uom_id),
)

# Update the first line with the sumed quantity
Expand All @@ -133,7 +135,7 @@ def _get_duplicated_line_ids(self):
check_dict[key].append(line_val["id"])
else:
check_dict[key] = [line_val["id"]]
for k, v in check_dict.iteritems():
for k, v in check_dict.items():
if len(v) > 1:
duplicates_group_ids.append(v)
return duplicates_group_ids
Expand Down
6 changes: 6 additions & 0 deletions stock_inventory_merge/models/stock_inventory_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ def create(self, vals):
StockInventoryLine, self.with_context(do_not_check_duplicates=True)
).create(vals)

@api.multi
def write(self, vals):
return super(
StockInventoryLine, self.with_context(do_not_check_duplicates=True)
).write(vals)

@api.model
def search(self, domain, *args, **kwargs):
if self.env.context.get("do_not_check_duplicates", False):
Expand Down
2 changes: 1 addition & 1 deletion stock_inventory_merge/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from . import test_module
from . import test_stock_inventory_merge
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from odoo.exceptions import Warning as UserError


class TestModule(TransactionCase):
class TestStockInventoryMerge(TransactionCase):
def setUp(self):
super(TestModule, self).setUp()
super(TestStockInventoryMerge, self).setUp()
self.inventory_obj = self.env["stock.inventory"]
self.line_obj = self.env["stock.inventory.line"]
self.wizard_obj = self.env["wizard.stock.inventory.merge"]
Expand All @@ -29,7 +29,7 @@ def setUp(self):
# Test Section
def test_01_block_done_inventory(self):
with self.assertRaises(UserError):
self.inventory_1.action_done()
self.inventory_1.action_validate()

def test_02_merge_duplicated_lines(self):
to_merge_line_ids = [self.line_1_1.id, self.line_1_2.id]
Expand All @@ -39,7 +39,7 @@ def test_02_merge_duplicated_lines(self):
2,
"Merging duplicated lines should delete lines.",
)
lines = self.line_obj.search([("id", "in", to_merge_line_ids)])
lines = self.inventory_1.line_ids.search([("id", "in", to_merge_line_ids)])
self.assertEqual(
len(lines),
1,
Expand All @@ -53,14 +53,22 @@ def test_02_merge_duplicated_lines(self):

def test_03_merge_inventories(self):
inventory_name = "Test Merged Inventory"
to_merge_inventory_ids = [self.inventory_1.id, self.inventory_2.id]
to_merge_inventory = [self.inventory_1, self.inventory_2]
to_merge_inventory_ids = []

# Start inventories
for inventory in to_merge_inventory:
to_merge_inventory_ids.append(inventory.id)
inventory.action_start()

wizard = self.wizard_obj.with_context(
active_ids=to_merge_inventory_ids, active_model="stock.inventory",
).create({"name": inventory_name})
wizard.action_merge()

inventories = self.inventory_obj.search(
[("id", "in", to_merge_inventory_ids)]
)
);
self.assertEqual(
len(inventories),
2,
Expand All @@ -84,7 +92,7 @@ def test_04_fill_with_zero(self):
"location_id": self.stock_location.id,
}
)
new_inventory_1.prepare_inventory()
new_inventory_1.action_start()
new_inventory_2 = new_inventory_1.copy(default={"name": "TEST #2"})
self.line_obj.create(
{
Expand All @@ -93,7 +101,7 @@ def test_04_fill_with_zero(self):
"inventory_id": new_inventory_2.id,
}
)
new_inventory_2.prepare_inventory()
new_inventory_2.action_start()
new_inventory_1.complete_with_zero()
new_inventory_2.complete_with_zero()

Expand All @@ -105,7 +113,7 @@ def test_04_fill_with_zero(self):
)

# We confirm an inventory that set all product to 0
new_inventory_1.action_done()
new_inventory_1.action_validate()

new_inventory_3 = self.inventory_obj.create(
{
Expand All @@ -114,7 +122,7 @@ def test_04_fill_with_zero(self):
"location_id": self.stock_location.id,
}
)
new_inventory_3.prepare_inventory()
new_inventory_3.action_start()
new_inventory_3.complete_with_zero()
self.assertEqual(
len(new_inventory_3.line_ids),
Expand Down
18 changes: 9 additions & 9 deletions stock_inventory_merge/views/view_stock_inventory.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
<button name="complete_with_zero" type="object" string="Complete With Zero" attrs="{'invisible': ['|', ('duplicates_qty', '!=', 0), ('state', '!=', 'confirm')]}"/>
<button name="action_merge_duplicated_line" type="object" string="Merge Duplicates" attrs="{'invisible': ['|', ('duplicates_qty', '=', 0), ('state', '!=', 'confirm')]}" class="oe_highlight"/>
</button>
<xpath expr="//div[@class='oe_title']" position="after">
<div class="oe_right oe_button_box" name="buttons">
<button class="oe_inline oe_stat_button" type="object"
name="action_view_duplicates" icon="fa-copy"
attrs="{'invisible': [('duplicates_qty', '=', 0)]}">
<field string="Duplicates" name="duplicates_qty" widget="statinfo"/>
</button>
</div>
</xpath>

<button name="action_inventory_line_tree" position="before">
<button class="oe_inline oe_stat_button" type="object"
name="action_view_duplicates" icon="fa-copy"
attrs="{'invisible': [('duplicates_qty', '=', 0)]}">
<field string="Duplicates" name="duplicates_qty" widget="statinfo"/>
</button>
</button>

</field>
</record>

Expand Down
18 changes: 7 additions & 11 deletions stock_inventory_merge/views/view_stock_inventory_line.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,24 @@
<!--
Copyright (C) 2018 - Today: GRAP (http://www.grap.coop)
@author: Sylvain LE GAL (https://twitter.com/legalsylvain)
@author: Quentin DUPONT (https://twitter.com/pondupont)
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
-->
<odoo>

<record id="view_stock_inventory_line_tree" model="ir.ui.view">
<field name="model">stock.inventory.line</field>
<field name="arch" type="xml">
<tree editable="bottom" colors="blue: product_qty != theoretical_qty; red: theoretical_qty &lt; 0">
<field name="product_id" domain="[('type','=','product')]"
on_change="onchange_createline(location_id, product_id, product_uom_id, package_id, prod_lot_id, partner_id)"/>
<field name="product_uom_id" groups="product.group_uom" string="UoM"
on_change="onchange_createline(location_id, product_id, product_uom_id, package_id, prod_lot_id, partner_id)"/>
<field name="location_id" domain="[('id', 'child_of', parent.location_id)]" groups="stock.group_locations"
on_change="onchange_createline(location_id, product_id, product_uom_id, package_id, prod_lot_id, partner_id)"/>
<tree editable="bottom" decoration-success="product_qty != theoretical_qty" decoration-danger="theoretical_qty &lt; 0">
<field name="product_id" domain="[('type','=','product')]"/>
<field name="product_uom_id" groups="uom.group_uom" string="UoM"/>
<field name="location_id" domain="[('id', 'child_of', parent.location_id)]"
groups="stock.group_stock_multi_locations"/>
<field name="prod_lot_id" domain="[('product_id', '=', product_id)]"
on_change="onchange_createline(location_id, product_id, product_uom_id, package_id, prod_lot_id, partner_id)"
context="{'default_product_id': product_id}" groups="stock.group_production_lot"/>
groups="stock.group_production_lot"/>
<field name="package_id" domain="['|', ('location_id','=', False), ('location_id', '=', location_id)]"
on_change="onchange_createline(location_id, product_id, product_uom_id, package_id, prod_lot_id, partner_id)"
groups="stock.group_tracking_lot"/>
<field name="partner_id"
on_change="onchange_createline(location_id, product_id, product_uom_id, package_id, prod_lot_id, partner_id)"
groups="stock.group_tracking_owner"/>
<field name="theoretical_qty" readonly="1"/>
<field name="product_qty" string="Real Quantity"/>
Expand Down

0 comments on commit eb02a51

Please sign in to comment.