[10.0][ADD] mrp_bom_comparison (#277)

Compare two Bill of Materials to view the differences.
This commit is contained in:
Sébastien Alix
2018-08-07 13:25:35 +02:00
committed by Pedro M. Baeza
parent 38219b4458
commit 0c7a4230c7
16 changed files with 839 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
============================
Bill of Materials comparison
============================
.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
This module allows you to compare two Bill of Materials to view the differences.
**Table of contents**
.. contents::
:local:
Bug Tracker
===========
Bugs are tracked on `GitHub Issues <https://github.com/OCA/{{ repo_name }}/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed feedback.
Do not contact contributors directly about support or help with technical issues.
Credits
=======
Authors
~~~~~~~
Maintainers
~~~~~~~~~~~
This module is maintained by the OCA.
.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org
OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.
Current `maintainer{% if manifest.maintainers|length > 1 %}s{% endif %} <https://odoo-community.org/page/maintainer-role>`_:
This module is part of the `OCA/manufacture <https://github.com/OCA/manufacture/tree/10.0/mrp_bom_comparison>`_ project on GitHub.
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

View File

@@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
from . import wizards

View File

@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
# Copyright 2018 ABF OSIELL <http://osiell.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
"name": "Bill of Materials comparison",
"summary": "Compare two Bill of Materials to view the differences",
"version": "10.0.1.0.0",
"category": "Manufacturing",
"website": "https://github.com/OCA/manufacture",
"author": "ABF OSIELL, Odoo Community Association (OCA)",
"license": "AGPL-3",
"application": False,
"installable": True,
"depends": [
"mrp",
"report",
],
"data": [
"wizards/mrp_bom_comparison.xml",
"reports/mrp_bom_comparison.xml",
"reports.xml",
],
}

View File

@@ -0,0 +1,210 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * mrp_bom_comparison
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 10.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-05-30 12:39+0000\n"
"PO-Revision-Date: 2018-05-30 12:39+0000\n"
"Last-Translator: <>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"
#. module: mrp_bom_comparison
#: model:ir.ui.view,arch_db:mrp_bom_comparison.report_mrp_bom_comparison
msgid "<br/>Products added to BoM v2"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.ui.view,arch_db:mrp_bom_comparison.report_mrp_bom_comparison
msgid "<br/>Products removed from BoM v2"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.ui.view,arch_db:mrp_bom_comparison.report_mrp_bom_comparison
msgid "<br/>Products updated"
msgstr ""
#. module: mrp_bom_comparison
#: selection:wizard.mrp.bom.comparison.line,state:0
msgid "Added"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.actions.report.xml,name:mrp_bom_comparison.action_report_mrp_bom_comparison
msgid "BoM Differences"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.model,name:mrp_bom_comparison.model_wizard_mrp_bom_comparison_line
msgid "BoM line difference"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.model.fields,field_description:mrp_bom_comparison.field_wizard_mrp_bom_comparison_bom1_id
#: model:ir.ui.view,arch_db:mrp_bom_comparison.report_mrp_bom_comparison
msgid "BoM v1"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.model.fields,field_description:mrp_bom_comparison.field_wizard_mrp_bom_comparison_bom2_id
#: model:ir.ui.view,arch_db:mrp_bom_comparison.report_mrp_bom_comparison
msgid "BoM v2"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.ui.view,arch_db:mrp_bom_comparison.view_wizard_bom_comparison_form
msgid "Cancel"
msgstr ""
#. module: mrp_bom_comparison
#: selection:wizard.mrp.bom.comparison.line,state:0
msgid "Changed"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.ui.view,arch_db:mrp_bom_comparison.view_wizard_bom_comparison_form
msgid "Compare"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.actions.act_window,name:mrp_bom_comparison.act_wizard_mrp_bom_comparison
#: model:ir.ui.view,arch_db:mrp_bom_comparison.view_wizard_bom_comparison_form
msgid "Compare Bill of Materials"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.model,name:mrp_bom_comparison.model_wizard_mrp_bom_comparison
msgid "Compare two BoM"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.model.fields,field_description:mrp_bom_comparison.field_wizard_mrp_bom_comparison_create_uid
#: model:ir.model.fields,field_description:mrp_bom_comparison.field_wizard_mrp_bom_comparison_line_create_uid
msgid "Created by"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.model.fields,field_description:mrp_bom_comparison.field_wizard_mrp_bom_comparison_create_date
#: model:ir.model.fields,field_description:mrp_bom_comparison.field_wizard_mrp_bom_comparison_line_create_date
msgid "Created on"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.ui.view,arch_db:mrp_bom_comparison.report_mrp_bom_comparison
msgid "Designation"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.model.fields,field_description:mrp_bom_comparison.field_wizard_mrp_bom_comparison_line_ids
msgid "Differences"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.model.fields,field_description:mrp_bom_comparison.field_wizard_mrp_bom_comparison_display_name
#: model:ir.model.fields,field_description:mrp_bom_comparison.field_wizard_mrp_bom_comparison_line_display_name
msgid "Display Name"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.model.fields,field_description:mrp_bom_comparison.field_wizard_mrp_bom_comparison_id
#: model:ir.model.fields,field_description:mrp_bom_comparison.field_wizard_mrp_bom_comparison_line_id
msgid "ID"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.model.fields,field_description:mrp_bom_comparison.field_wizard_mrp_bom_comparison___last_update
#: model:ir.model.fields,field_description:mrp_bom_comparison.field_wizard_mrp_bom_comparison_line___last_update
msgid "Last Modified on"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.model.fields,field_description:mrp_bom_comparison.field_wizard_mrp_bom_comparison_line_write_uid
#: model:ir.model.fields,field_description:mrp_bom_comparison.field_wizard_mrp_bom_comparison_write_uid
msgid "Last Updated by"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.model.fields,field_description:mrp_bom_comparison.field_wizard_mrp_bom_comparison_line_write_date
#: model:ir.model.fields,field_description:mrp_bom_comparison.field_wizard_mrp_bom_comparison_write_date
msgid "Last Updated on"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.ui.view,arch_db:mrp_bom_comparison.report_mrp_bom_comparison
msgid "NO DIFFERENCE DETECTED"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.model.fields,field_description:mrp_bom_comparison.field_wizard_mrp_bom_comparison_line_product_id
msgid "Product"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.model.fields,field_description:mrp_bom_comparison.field_wizard_mrp_bom_comparison_line_added_ids
msgid "Products added"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.model.fields,field_description:mrp_bom_comparison.field_wizard_mrp_bom_comparison_line_removed_ids
msgid "Products removed"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.model.fields,field_description:mrp_bom_comparison.field_wizard_mrp_bom_comparison_line_changed_ids
msgid "Products updated"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.model.fields,field_description:mrp_bom_comparison.field_wizard_mrp_bom_comparison_line_diff_qty
#: model:ir.ui.view,arch_db:mrp_bom_comparison.report_mrp_bom_comparison
msgid "Qty gap"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.ui.view,arch_db:mrp_bom_comparison.report_mrp_bom_comparison
msgid "Ref."
msgstr ""
#. module: mrp_bom_comparison
#: selection:wizard.mrp.bom.comparison.line,state:0
msgid "Removed"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.model.fields,field_description:mrp_bom_comparison.field_wizard_mrp_bom_comparison_line_state
msgid "State"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.ui.view,arch_db:mrp_bom_comparison.report_mrp_bom_comparison
msgid "TOTAL"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.model.fields,field_description:mrp_bom_comparison.field_wizard_mrp_bom_comparison_total_qty
msgid "Total qty"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.model.fields,field_description:mrp_bom_comparison.field_wizard_mrp_bom_comparison_line_wiz_id
msgid "Wizard"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.model.fields,field_description:mrp_bom_comparison.field_wizard_mrp_bom_comparison_line_bom1_qty
#: model:ir.ui.view,arch_db:mrp_bom_comparison.report_mrp_bom_comparison
msgid "v1-Qty"
msgstr ""
#. module: mrp_bom_comparison
#: model:ir.model.fields,field_description:mrp_bom_comparison.field_wizard_mrp_bom_comparison_line_bom2_qty
#: model:ir.ui.view,arch_db:mrp_bom_comparison.report_mrp_bom_comparison
msgid "v2-Qty"
msgstr ""

View File

@@ -0,0 +1 @@
* Sébastien Alix <sebastien.alix@osiell.com> (https://osiell.com)

View File

@@ -0,0 +1 @@
This module allows you to compare two Bill of Materials to view the differences.

View File

@@ -0,0 +1,3 @@
If you want to compare two Bill of Materials not linked to the same product,
you can override the `_domain_bom_id` method from the
`wizard.mrp.bom.comparison` wizard.

View File

@@ -0,0 +1,9 @@
First, create two (or more) Bill of Materials on the same product.
Then, open the first Bill of Materials, and launch the wizard with `Action / Compare Bill of Materials`.
In the `BoM v2` field, select a BoM to compare against the current one,
and click on `Compare`. The result of the comparaison is returned as
a PDF report with three sections:
- products updated
- products added
- products removed

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2018 ABF OSIELL <http://osiell.com>
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>
<report id="action_report_mrp_bom_comparison"
name="mrp_bom_comparison.report_mrp_bom_comparison"
model="wizard.mrp.bom.comparison"
string="BoM Differences"
report_type="qweb-pdf"/>
</odoo>

View File

@@ -0,0 +1,98 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2018 ABF OSIELL <http://osiell.com>
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>
<template id="report_mrp_bom_comparison">
<t t-call="report.html_container">
<t t-foreach="docs" t-as="o">
<t t-call="report.internal_layout">
<div class="page">
<table class="table table-condensed">
<thead>
<tr>
<th>BoM v1</th>
<th>BoM v2</th>
</tr>
</thead>
<tbody>
<tr>
<td><span t-field="o.bom1_id"/></td>
<td><span t-field="o.bom2_id"/></td>
</tr>
</tbody>
</table>
<br/>
<table class="table table-condensed">
<thead>
<tr class="border-black">
<th>Ref.</th>
<th>Designation</th>
<th>v1-Qty</th>
<th>v2-Qty</th>
<th>Qty gap</th>
</tr>
</thead>
<tbody t-if="not o.line_changed_ids and not o.line_added_ids and not o.line_removed_ids">
<tr>
<td colspan="5" class="text-center">NO DIFFERENCE DETECTED</td>
</tr>
</tbody>
<tbody t-if="o.line_changed_ids">
<tr>
<th colspan="5"><br/>Products updated</th>
</tr>
<t t-foreach="o.line_changed_ids" t-as="line">
<tr>
<td class="text-warning"><span t-field="line.product_id.default_code"/></td>
<td><span t-field="line.product_id.name"/></td>
<td class="text-right"><span t-field="line.bom1_qty"/></td>
<td class="text-right"><span t-field="line.bom2_qty"/></td>
<td class="text-right text-warning"><span t-field="line.diff_qty"/></td>
</tr>
</t>
</tbody>
<tbody t-if="o.line_added_ids">
<tr>
<th colspan="5"><br/>Products added to BoM v2</th>
</tr>
<t t-foreach="o.line_added_ids" t-as="line">
<tr>
<td class="text-success"><span t-field="line.product_id.default_code"/></td>
<td><span t-field="line.product_id.name"/></td>
<td class="text-right"><span t-field="line.bom1_qty"/></td>
<td class="text-right"><span t-field="line.bom2_qty"/></td>
<td class="text-right text-success"><span t-field="line.diff_qty"/></td>
</tr>
</t>
</tbody>
<tbody t-if="o.line_removed_ids">
<tr>
<th colspan="5"><br/>Products removed from BoM v2</th>
</tr>
<t t-foreach="o.line_removed_ids" t-as="line">
<tr>
<td class="text-danger"><span t-field="line.product_id.default_code"/></td>
<td><span t-field="line.product_id.name"/></td>
<td class="text-right"><span t-field="line.bom1_qty"/></td>
<td class="text-right"><span t-field="line.bom2_qty"/></td>
<td class="text-right text-danger"><span t-field="line.diff_qty"/></td>
</tr>
</t>
</tbody>
<tfoot t-if="o.line_changed_ids or o.line_added_ids or o.line_removed_ids">
<tr class="no_border">
<th class="text-right" colspan="4">TOTAL</th>
<td class="text-right"><span t-field="o.total_qty"/></td>
</tr>
</tfoot>
</table>
</div>
</t>
</t>
</t>
</template>
</odoo>

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.2 KiB

View File

@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2018 ABF OSIELL <http://osiell.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from . import test_mrp_bom_comparison

View File

@@ -0,0 +1,141 @@
# -*- coding: utf-8 -*-
# Copyright 2018 ABF OSIELL <http://osiell.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo.addons.mrp.tests.common import TestMrpCommon
class TestMrpBomComparison(TestMrpCommon):
def setUp(self):
super(TestMrpBomComparison, self).setUp()
# bom_3 (product_6) -> bom_2 (product_5) -> bom_1 (product_4)
self.wiz_model = self.env['wizard.mrp.bom.comparison']
self.product_1.default_code = u"P1"
self.product_2.default_code = u"P2"
self.product_3.default_code = u"P3"
self.product_4.default_code = u"P4-BOM_1"
self.product_5.default_code = u"P5-BOM_2"
self.product_6.default_code = u"P6-BOM_3"
# Create a 'new_bom_1' from 'bom_1'
self.new_product_4 = self.product_4.copy(
{'default_code': u"P4-NEW_BOM_1"})
self.new_bom_1 = self.bom_1.copy({
'product_id': self.new_product_4.id,
'product_tmpl_id': self.new_product_4.product_tmpl_id.id,
})
# Change a component qty on 'new_bom_1':
# product_2: 2 => 1
self.new_bom_1.bom_line_ids.filtered(
lambda l: l.product_id == self.product_2).product_qty = 1
# Create a 'new_bom_2' from 'bom_2'
self.new_product_5 = self.product_5.copy(
{'default_code': u"P5-NEW_BOM_2"})
self.new_bom_2 = self.bom_2.copy({
'product_id': self.new_product_5.id,
'product_tmpl_id': self.new_product_5.product_tmpl_id.id,
})
self.new_bom_2.bom_line_ids.filtered(
lambda l: l.product_id == self.product_4).write(
{'product_id': self.new_product_4.id})
# Change a component qty on 'new_bom_2':
# product_3: 3 => 4
# new_product_4: 2 => 6
self.new_bom_2.bom_line_ids.filtered(
lambda l: l.product_id == self.product_3).product_qty = 4
self.new_bom_2.bom_line_ids.filtered(
lambda l: l.product_id == self.new_product_4).product_qty = 6
# Create a 'new_bom_3' from 'bom_3'
self.new_bom_3 = self.bom_3.copy()
self.new_bom_3.bom_line_ids.filtered(
lambda l: l.product_id == self.product_4).write(
{'product_id': self.new_product_4.id})
self.new_bom_3.bom_line_ids.filtered(
lambda l: l.product_id == self.product_5).write(
{'product_id': self.new_product_5.id})
# Change a component qty on 'new_bom_3':
# product_2: 12 => 10
self.new_bom_3.bom_line_ids.filtered(
lambda l: l.product_id == self.product_2).product_qty = 10
# Make a comparison between the original BoM and the new one
vals = {
'bom1_id': self.bom_3.id,
'bom2_id': self.new_bom_3.id,
}
self.wiz = self.wiz_model.create(vals)
self.wiz.run()
def test_line_changed_ids(self):
self.assertTrue(self.wiz.line_changed_ids)
self.assertEqual(len(self.wiz.line_changed_ids), 3)
changed_products = self.wiz.line_changed_ids.mapped('product_id')
self.assertIn(self.product_1, changed_products)
self.assertIn(self.product_2, changed_products)
self.assertIn(self.product_3, changed_products)
# product_1 qty:
# v1: 8*4/4 + 2*2*4/4 = 12
# v2: 8*4/4 + 2*6*4/4 = 20
# diff = +8
line_product_1 = self.wiz.line_changed_ids.filtered(
lambda l: l.product_id == self.product_1)
self.assertEqual(line_product_1.diff_qty, 8)
# product_2 qty:
# v1: 12 + 8*2/4 + 2*2*2/4 = 18
# v2: 10 + 8*1/4 + 2*6*1/4 = 15
# diff = -3
line_product_2 = self.wiz.line_changed_ids.filtered(
lambda l: l.product_id == self.product_2)
self.assertEqual(line_product_2.diff_qty, -3)
# product_3 qty:
# v1: 2*3 = 6
# v2: 2*4 = 8
# diff = +2
line_product_3 = self.wiz.line_changed_ids.filtered(
lambda l: l.product_id == self.product_3)
self.assertEqual(line_product_3.diff_qty, 2)
def test_line_added_ids(self):
self.assertTrue(self.wiz.line_added_ids)
self.assertEqual(len(self.wiz.line_added_ids), 2)
added_products = self.wiz.line_added_ids.mapped('product_id')
self.assertIn(self.new_product_4, added_products)
self.assertIn(self.new_product_5, added_products)
# new_product4 qty:
# v1: 0
# v2: 8 + 2*6 = 20
# diff = +20
line_new_product_4 = self.wiz.line_added_ids.filtered(
lambda l: l.product_id == self.new_product_4)
self.assertEqual(line_new_product_4.diff_qty, 20)
# new_product5 qty:
# v1: 0
# v2: 2
# diff = +2
line_new_product_5 = self.wiz.line_added_ids.filtered(
lambda l: l.product_id == self.new_product_5)
self.assertEqual(line_new_product_5.diff_qty, 2)
def test_line_removed_ids(self):
self.assertTrue(self.wiz.line_removed_ids)
self.assertEqual(len(self.wiz.line_removed_ids), 2)
removed_products = self.wiz.line_removed_ids.mapped('product_id')
self.assertIn(self.product_4, removed_products)
self.assertIn(self.product_5, removed_products)
# product4 qty:
# v1: 12
# v2: 0
# diff = -12
line_product_4 = self.wiz.line_removed_ids.filtered(
lambda l: l.product_id == self.product_4)
self.assertEqual(line_product_4.diff_qty, -12)
# product5 qty:
# v1: 2
# v2: 0
# diff = -2
line_product_5 = self.wiz.line_removed_ids.filtered(
lambda l: l.product_id == self.product_5)
self.assertEqual(line_product_5.diff_qty, -2)

View File

@@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
from . import mrp_bom_comparison

View File

@@ -0,0 +1,247 @@
# -*- coding: utf-8 -*-
# Copyright 2018 ABF OSIELL <http://osiell.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import logging
from odoo import api, fields, models
from odoo.addons import decimal_precision as dp
_logger = logging.getLogger(__name__)
class DictDiffer(object):
"""Calculate the difference between two dictionaries as:
(1) items added
(2) items removed
(3) keys same in both but changed values
(4) keys same in both and unchanged values
"""
def __init__(self, current_dict, past_dict):
self.current_dict, self.past_dict = current_dict, past_dict
self.set_current = set(current_dict.keys())
self.set_past = set(past_dict.keys())
self.intersect = self.set_current.intersection(self.set_past)
def added(self):
return self.set_current - self.intersect
def removed(self):
return self.set_past - self.intersect
def changed(self):
return set(o for o in self.intersect
if self.past_dict[o] != self.current_dict[o])
def unchanged(self):
return set(o for o in self.intersect
if self.past_dict[o] == self.current_dict[o])
class WizardMrpBomComparison(models.TransientModel):
_name = 'wizard.mrp.bom.comparison'
_description = "Compare two BoM"
@api.model
def _func_domain_bom_id(self):
return self._domain_bom_id()
@api.model
def _domain_bom_id(self):
"""Returns the domain used to select the BoMs to compare."""
bom_id = self.env.context.get('active_id', False)
bom = self.env['mrp.bom'].browse(bom_id)
return [('product_tmpl_id', '=', bom.product_tmpl_id.id)]
@api.multi
@api.depends('line_ids.diff_qty')
def _compute_total_qty(self):
for wiz in self:
wiz.total_qty = sum([
sum([line.diff_qty for line in wiz.line_changed_ids]),
sum([line.diff_qty for line in wiz.line_added_ids]),
sum([line.diff_qty for line in wiz.line_removed_ids]),
])
bom1_id = fields.Many2one(
'mrp.bom', u"BoM v1", required=True,
domain=_func_domain_bom_id)
bom2_id = fields.Many2one(
'mrp.bom', u"BoM v2", required=True,
domain=_func_domain_bom_id)
line_ids = fields.One2many(
'wizard.mrp.bom.comparison.line', 'wiz_id', u"Differences")
line_changed_ids = fields.One2many(
'wizard.mrp.bom.comparison.line', 'wiz_id', u"Products updated",
domain=[('state', '=', 'changed')])
line_added_ids = fields.One2many(
'wizard.mrp.bom.comparison.line', 'wiz_id', u"Products added",
domain=[('state', '=', 'added')])
line_removed_ids = fields.One2many(
'wizard.mrp.bom.comparison.line', 'wiz_id', u"Products removed",
domain=[('state', '=', 'removed')])
total_qty = fields.Float(
u"Total qty",
digits=dp.get_precision('Product Unit of Measure'),
compute='_compute_total_qty')
@api.model
def default_get(self, fields_list):
"""'default_get' method overridden."""
res = super(WizardMrpBomComparison, self).default_get(fields_list)
res['bom1_id'] = self.env.context.get('active_id', False)
return res
@api.model
def _get_bom_line_data(self, root_bom, bom_line, factor=1):
"""Return a dictionary representation of the `bom_line` record.
:return: a dictionary
"""
data = {
'product_id': bom_line.product_id.id,
'product_code': bom_line.product_id.default_code or '-',
'product_name': bom_line.product_id.name or '-',
'bom_qty': (
bom_line.product_qty / float(bom_line.bom_id.product_qty)
* factor),
}
if bom_line.bom_id == root_bom:
data['bom_qty'] = bom_line.product_qty * factor
return data
@api.model
def _merge_bom_line_data(self, bom1_line_data, bom2_line_data):
"""Merge two bom lines (dictionaries with same keys).
:return: a dictionary
"""
new_bom_line_data = bom1_line_data.copy()
new_bom_line_data['bom_qty'] += bom2_line_data['bom_qty']
return new_bom_line_data
@api.model
def _get_all_data(self, root_bom):
"""Get all BoM data composing the BoM identified by `bom_id`.
:return: a dictionary
"""
products = {}
def recurse(start_bom, products, factor=1):
for bom_line in start_bom.bom_line_ids:
if bom_line.product_id.bom_ids:
bom = self._get_bom_from_product(bom_line.product_id)
bom_factor = bom_line.product_qty * factor
recurse(bom, products, factor=bom_factor)
if not bom_line.product_id:
continue
p_id = bom_line.product_id.id
bom_line_data = self._get_bom_line_data(
root_bom, bom_line, factor)
# New product
if p_id not in products:
products[p_id] = bom_line_data
# Merge bom data with the existing one
else:
products[p_id] = self._merge_bom_line_data(
products[p_id], bom_line_data)
return products
recurse(root_bom, products)
return products
def _get_bom_from_product(self, product):
return product.bom_ids[0]
@api.multi
def run(self):
"""Make a comparison between two BoMs.
:return: a report
"""
self.ensure_one()
# Ensure that no line exists if we make 2 comparisons
# from the same wizard
self.line_ids.unlink()
_logger.info(
u"BoM comparison between '%s' and '%s'...",
self.bom1_id.product_tmpl_id.default_code,
self.bom2_id.product_tmpl_id.default_code)
comparison_line_model = self.env['wizard.mrp.bom.comparison.line']
# Get all data for each BoM
bom1_data = self._get_all_data(self.bom1_id)
bom2_data = self._get_all_data(self.bom2_id)
# Make the comparison between them
diff = DictDiffer(bom2_data, bom1_data)
# Iterate over data to generate lines to display on the report
for p_id in diff.changed():
v1 = bom1_data[p_id]
v2 = bom2_data[p_id]
vals = {
'wiz_id': self.id,
'product_id': p_id,
'bom1_qty': v1['bom_qty'],
'bom2_qty': v2['bom_qty'],
'diff_qty': v2['bom_qty'] - v1['bom_qty'],
'state': 'changed',
}
_logger.info(
u"\tProduct updated: %s (ID=%s) %s -> %s",
v1['product_code'], p_id,
v1['bom_qty'], v2['bom_qty'])
comparison_line_model.create(vals)
for p_id in diff.added():
v2 = bom2_data[p_id]
vals = {
'wiz_id': self.id,
'product_id': p_id,
'bom1_qty': 0.0,
'bom2_qty': v2['bom_qty'],
'diff_qty': v2['bom_qty'],
'state': 'added',
}
_logger.info(
u"\tProduct added: %s (ID=%s) -> %s",
v2['product_code'], p_id, vals['diff_qty'])
comparison_line_model.create(vals)
for p_id in diff.removed():
v1 = bom1_data[p_id]
vals = {
'wiz_id': self.id,
'product_id': p_id,
'bom1_qty': v1['bom_qty'],
'bom2_qty': 0.0,
'diff_qty': -v1['bom_qty'],
'state': 'removed',
}
_logger.info(
u"\tProduct removed: %s (ID=%s) -> %s",
v1['product_code'], p_id, vals['diff_qty'])
comparison_line_model.create(vals)
_logger.info(
u"BoM comparison between '%s' and '%s': printing report...",
self.bom1_id.product_tmpl_id.default_code,
self.bom2_id.product_tmpl_id.default_code)
# Return the report
return self.env['report'].get_action(
self, 'mrp_bom_comparison.report_mrp_bom_comparison')
class WizardMrpBomComparisonLine(models.TransientModel):
_name = 'wizard.mrp.bom.comparison.line'
_description = "BoM line difference"
wiz_id = fields.Many2one('wizard.mrp.bom.comparison', u"Wizard")
product_id = fields.Many2one('product.product', u"Product")
bom1_qty = fields.Float(
u"v1-Qty", digits=dp.get_precision('Product Unit of Measure'))
bom2_qty = fields.Float(
u"v2-Qty", digits=dp.get_precision('Product Unit of Measure'))
diff_qty = fields.Float(
u"Qty gap", digits=dp.get_precision('Product Unit of Measure'))
state = fields.Selection(
[('changed', u"Changed"),
('added', u"Added"),
('removed', u"Removed"),
],
u"State")

View File

@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2018 ABF OSIELL <http://osiell.com>
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>
<record id="view_wizard_bom_comparison_form" model="ir.ui.view">
<field name="name">Compare Bill of Materials</field>
<field name="model">wizard.mrp.bom.comparison</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Compare Bill of Materials">
<group col="4">
<field name="bom1_id"/>
<field name="bom2_id"/>
</group>
<footer>
<button name="run" type="object"
string="Compare" class="btn-primary"/>
<button special="cancel"
string="Cancel" class="btn-default"/>
</footer>
</form>
</field>
</record>
<act_window name="Compare Bill of Materials"
res_model="wizard.mrp.bom.comparison"
src_model="mrp.bom"
view_mode="form"
target="new"
id="act_wizard_mrp_bom_comparison"/>
</odoo>