From 9afc2668db5235be69070e1a46b3320ea5626dfa Mon Sep 17 00:00:00 2001 From: Nicola Malcontenti Date: Tue, 1 Mar 2016 09:31:22 +0100 Subject: [PATCH 1/3] [MIG] Mig mrp_discount to v10 and added discount on fees lines --- mrp_repair_discount/README.rst | 55 +++++++++++++++++++ mrp_repair_discount/__init__.py | 5 ++ mrp_repair_discount/__openerp__.py | 18 ++++++ mrp_repair_discount/models/__init__.py | 5 ++ mrp_repair_discount/models/mrp_repair.py | 24 ++++++++ mrp_repair_discount/views/mrp_repair_view.xml | 15 +++++ 6 files changed, 122 insertions(+) create mode 100644 mrp_repair_discount/README.rst create mode 100644 mrp_repair_discount/__init__.py create mode 100644 mrp_repair_discount/__openerp__.py create mode 100644 mrp_repair_discount/models/__init__.py create mode 100644 mrp_repair_discount/models/mrp_repair.py create mode 100644 mrp_repair_discount/views/mrp_repair_view.xml diff --git a/mrp_repair_discount/README.rst b/mrp_repair_discount/README.rst new file mode 100644 index 000000000..64b0b05eb --- /dev/null +++ b/mrp_repair_discount/README.rst @@ -0,0 +1,55 @@ +... image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 + +=================== +MRP Repair Discount +=================== + +This module extends the functionality of mrp repair adding +new field discount on operation's lines + +.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas + :alt: Try me on Runbot + :target: https://runbot.odoo-community.org/runbot/129/8.0 + + +Bug Tracker +=========== + +Bugs are tracked on `GitHub 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 +`_. + +Credits +======= + +Images +------ + +* Odoo Community Association: `Icon `_. + +Contributors +------------ + +* Nicola Malcontenti + +Maintainer +---------- + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +This module is maintained by the OCA. + +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. + +To contribute to this module, please visit https://odoo-community.org. \ No newline at end of file diff --git a/mrp_repair_discount/__init__.py b/mrp_repair_discount/__init__.py new file mode 100644 index 000000000..421162bb0 --- /dev/null +++ b/mrp_repair_discount/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# © 2015 Nicola Malcontenti - Agile Business Group +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from . import models diff --git a/mrp_repair_discount/__openerp__.py b/mrp_repair_discount/__openerp__.py new file mode 100644 index 000000000..c5494d5df --- /dev/null +++ b/mrp_repair_discount/__openerp__.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +# © 2015 Nicola Malcontenti - Agile Business Group +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +{ + "name": "MRP Repair Discount", + "version": "8.0.1.0.0", + "category": "Manufactoring", + "license": "AGPL-3", + "author": "Agile Business Group, Odoo Community Association (OCA)", + "website": "http://www.agilebg.com", + "contributors": [ + "Nicola Malcontenti ", + ], + 'depends': ['mrp_repair'], + "data": ["views/mrp_repair_view.xml"], + "installable": True, +} diff --git a/mrp_repair_discount/models/__init__.py b/mrp_repair_discount/models/__init__.py new file mode 100644 index 000000000..a84a56b6f --- /dev/null +++ b/mrp_repair_discount/models/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# © 2015 Nicola Malcontenti - Agile Business Group +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from . import mrp_repair diff --git a/mrp_repair_discount/models/mrp_repair.py b/mrp_repair_discount/models/mrp_repair.py new file mode 100644 index 000000000..49e9c7395 --- /dev/null +++ b/mrp_repair_discount/models/mrp_repair.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +# © 2015 Nicola Malcontenti - Agile Business Group +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from openerp import fields, models +from openerp.osv import orm + + +class MrpRepairLine(models.Model): + _inherit = 'mrp.repair.line' + + discount = fields.Float(string='Discount (%)') + + +class MrpRepair(orm.Model): + _inherit = 'mrp.repair' + + def action_invoice_create(self, cr, uid, ids, group=False, context=None): + res = super(MrpRepair, self).action_invoice_create( + cr, uid, ids, group, context) + repair_obj = self.browse(cr, uid, ids, context=context) + for op in repair_obj.operations: + op.invoice_line_id.discount = op.discount + return res diff --git a/mrp_repair_discount/views/mrp_repair_view.xml b/mrp_repair_discount/views/mrp_repair_view.xml new file mode 100644 index 000000000..13d5c7365 --- /dev/null +++ b/mrp_repair_discount/views/mrp_repair_view.xml @@ -0,0 +1,15 @@ + + + + + mrp.repair.discount.form + mrp.repair + + + + + + + + + From 7fb6bdaf5889e786bbb0b207b0d2b3cf712cc00f Mon Sep 17 00:00:00 2001 From: "Pedro M. Baeza" Date: Fri, 14 Oct 2016 01:21:24 +0200 Subject: [PATCH 2/3] mrp_discount: New API + tests --- mrp_repair_discount/README.rst | 3 +- mrp_repair_discount/__openerp__.py | 5 +- mrp_repair_discount/models/mrp_repair.py | 38 +++++++++--- mrp_repair_discount/tests/__init__.py | 4 ++ .../tests/test_mrp_repair_discount.py | 62 +++++++++++++++++++ 5 files changed, 101 insertions(+), 11 deletions(-) create mode 100644 mrp_repair_discount/tests/__init__.py create mode 100644 mrp_repair_discount/tests/test_mrp_repair_discount.py diff --git a/mrp_repair_discount/README.rst b/mrp_repair_discount/README.rst index 64b0b05eb..db58bc437 100644 --- a/mrp_repair_discount/README.rst +++ b/mrp_repair_discount/README.rst @@ -38,6 +38,7 @@ Contributors ------------ * Nicola Malcontenti +* Pedro M. Baeza Maintainer ---------- @@ -52,4 +53,4 @@ 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. -To contribute to this module, please visit https://odoo-community.org. \ No newline at end of file +To contribute to this module, please visit https://odoo-community.org. diff --git a/mrp_repair_discount/__openerp__.py b/mrp_repair_discount/__openerp__.py index c5494d5df..527adf515 100644 --- a/mrp_repair_discount/__openerp__.py +++ b/mrp_repair_discount/__openerp__.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # © 2015 Nicola Malcontenti - Agile Business Group +# © 2016 Pedro M. Baeza # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). { @@ -7,7 +8,9 @@ "version": "8.0.1.0.0", "category": "Manufactoring", "license": "AGPL-3", - "author": "Agile Business Group, Odoo Community Association (OCA)", + "author": "Agile Business Group, " + "Tecnativa, " + "Odoo Community Association (OCA)", "website": "http://www.agilebg.com", "contributors": [ "Nicola Malcontenti ", diff --git a/mrp_repair_discount/models/mrp_repair.py b/mrp_repair_discount/models/mrp_repair.py index 49e9c7395..90961d3e9 100644 --- a/mrp_repair_discount/models/mrp_repair.py +++ b/mrp_repair_discount/models/mrp_repair.py @@ -1,9 +1,11 @@ # -*- coding: utf-8 -*- # © 2015 Nicola Malcontenti - Agile Business Group +# © 2016 Pedro M. Baeza # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from openerp import fields, models -from openerp.osv import orm +from openerp import api, fields, models +from openerp.osv import fields as old_fields +import openerp.addons.decimal_precision as dp class MrpRepairLine(models.Model): @@ -11,14 +13,32 @@ class MrpRepairLine(models.Model): discount = fields.Float(string='Discount (%)') + @api.multi + def _amount_line(self, field_name, arg): + res = super(MrpRepairLine, self)._amount_line(field_name, arg) + for line in self.filtered('discount'): + price = res[line.id] * (1 - (line.discount or 0.0) / 100.0) + res[line.id] = price + return res -class MrpRepair(orm.Model): + _columns = { + # Must be defined in old API so that we can call super in the compute + 'price_subtotal': old_fields.function( + _amount_line, string='Subtotal', + digits_compute=dp.get_precision('Account')), + } + + +class MrpRepair(models.Model): _inherit = 'mrp.repair' - def action_invoice_create(self, cr, uid, ids, group=False, context=None): - res = super(MrpRepair, self).action_invoice_create( - cr, uid, ids, group, context) - repair_obj = self.browse(cr, uid, ids, context=context) - for op in repair_obj.operations: - op.invoice_line_id.discount = op.discount + @api.multi + def action_invoice_create(self, group=False): + res = super(MrpRepair, self).action_invoice_create(group) + for repair in self: + operations = repair.operations.filtered('to_invoice') + for op in operations: + op.invoice_line_id.discount = op.discount + if operations: + repair.invoice_id.button_reset_taxes() return res diff --git a/mrp_repair_discount/tests/__init__.py b/mrp_repair_discount/tests/__init__.py new file mode 100644 index 000000000..eb17b9cae --- /dev/null +++ b/mrp_repair_discount/tests/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from . import test_mrp_repair_discount diff --git a/mrp_repair_discount/tests/test_mrp_repair_discount.py b/mrp_repair_discount/tests/test_mrp_repair_discount.py new file mode 100644 index 000000000..96346804c --- /dev/null +++ b/mrp_repair_discount/tests/test_mrp_repair_discount.py @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- +# © 2016 Pedro M. Baeza +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from openerp.tests import common + + +class TestMrpRepairDiscount(common.SavepointCase): + @classmethod + def setUpClass(cls): + super(TestMrpRepairDiscount, cls).setUpClass() + cls.product = cls.env['product.product'].create({ + 'name': 'Test product', + 'standard_price': 10, + 'list_price': 20, + }) + cls.partner = cls.env['res.partner'].create({ + 'name': 'Test partner', + }) + cls.location = cls.env['stock.location'].create({ + 'name': 'Test location', + }) + cls.repair = cls.env['mrp.repair'].create({ + 'product_id': cls.product.id, + 'partner_id': cls.partner.id, + 'partner_invoice_id': cls.partner.id, + 'product_uom': cls.product.uom_id.id, + 'location_id': cls.location.id, + 'location_dest_id': cls.location.id, + 'invoice_method': 'b4repair', + }) + cls.repair_line = cls.env['mrp.repair.line'].create({ + 'repair_id': cls.repair.id, + 'type': 'add', + 'product_id': cls.product.id, + 'product_uom': cls.product.uom_id.id, + 'name': 'Test line', + 'location_id': cls.repair.location_id.id, + 'location_dest_id': cls.repair.location_dest_id.id, + 'product_uom_qty': 1, + 'price_unit': 20, + 'discount': 50, + 'to_invoice': True, + }) + + def test_discount(self): + self.assertAlmostEqual( + self.repair_line.price_subtotal, 10, + self.repair_line._columns['price_subtotal'].digits[1]) + self.assertAlmostEqual( + self.repair.amount_total, 10, + self.repair._columns['amount_total'].digits[1]) + + def test_invoice_create(self): + self.repair.state = '2binvoiced' + res = self.repair.action_invoice_create() + invoice = self.env['account.invoice'].browse(res.values()[0]) + invoice_line = invoice.invoice_line[0] + self.assertEqual(invoice_line.discount, 50) + self.assertAlmostEqual( + invoice_line.price_subtotal, 10, + invoice_line._columns['price_subtotal'].digits[1]) From 1ac9e411f3eed29b083c7dfb5ab842a9c0ae519f Mon Sep 17 00:00:00 2001 From: Alex Comba Date: Fri, 17 Feb 2017 15:46:59 +0100 Subject: [PATCH 3/3] mrp_repair_discount: fix wrong AGPL badge image in README [MIG] Mig mrp_discount to v10 and added discount on fees lines --- mrp_repair_discount/README.rst | 22 ++-- mrp_repair_discount/__init__.py | 2 +- .../{__openerp__.py => __manifest__.py} | 11 +- mrp_repair_discount/models/__init__.py | 2 +- mrp_repair_discount/models/mrp_repair.py | 120 ++++++++++++++---- .../tests/test_mrp_repair_discount.py | 15 +-- mrp_repair_discount/views/mrp_repair_view.xml | 13 +- 7 files changed, 124 insertions(+), 61 deletions(-) rename mrp_repair_discount/{__openerp__.py => __manifest__.py} (61%) diff --git a/mrp_repair_discount/README.rst b/mrp_repair_discount/README.rst index db58bc437..af6861745 100644 --- a/mrp_repair_discount/README.rst +++ b/mrp_repair_discount/README.rst @@ -1,4 +1,4 @@ -... image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html :alt: License: AGPL-3 @@ -7,12 +7,14 @@ MRP Repair Discount =================== This module extends the functionality of mrp repair adding -new field discount on operation's lines +new field discount on operations lines + +Usage +===== .. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas :alt: Try me on Runbot - :target: https://runbot.odoo-community.org/runbot/129/8.0 - + :target: https://runbot.odoo-community.org/runbot/129/10.0 Bug Tracker =========== @@ -20,25 +22,17 @@ Bug Tracker Bugs are tracked on `GitHub 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 -`_. +help us smash it by providing detailed and welcomed feedback. Credits ======= -Images ------- - -* Odoo Community Association: `Icon `_. - Contributors ------------ * Nicola Malcontenti * Pedro M. Baeza +* Lorenzo Battistini Maintainer ---------- diff --git a/mrp_repair_discount/__init__.py b/mrp_repair_discount/__init__.py index 421162bb0..df4a96296 100644 --- a/mrp_repair_discount/__init__.py +++ b/mrp_repair_discount/__init__.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# © 2015 Nicola Malcontenti - Agile Business Group +# Copyright 2015 Nicola Malcontenti - Agile Business Group # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from . import models diff --git a/mrp_repair_discount/__openerp__.py b/mrp_repair_discount/__manifest__.py similarity index 61% rename from mrp_repair_discount/__openerp__.py rename to mrp_repair_discount/__manifest__.py index 527adf515..01e764519 100644 --- a/mrp_repair_discount/__openerp__.py +++ b/mrp_repair_discount/__manifest__.py @@ -1,20 +1,17 @@ # -*- coding: utf-8 -*- -# © 2015 Nicola Malcontenti - Agile Business Group -# © 2016 Pedro M. Baeza +# Copyright 2015 Nicola Malcontenti - Agile Business Group +# Copyright 2016 Pedro M. Baeza # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). { "name": "MRP Repair Discount", - "version": "8.0.1.0.0", - "category": "Manufactoring", + "version": "10.0.1.0.0", + "category": "Manufacturing", "license": "AGPL-3", "author": "Agile Business Group, " "Tecnativa, " "Odoo Community Association (OCA)", "website": "http://www.agilebg.com", - "contributors": [ - "Nicola Malcontenti ", - ], 'depends': ['mrp_repair'], "data": ["views/mrp_repair_view.xml"], "installable": True, diff --git a/mrp_repair_discount/models/__init__.py b/mrp_repair_discount/models/__init__.py index a84a56b6f..f579834b0 100644 --- a/mrp_repair_discount/models/__init__.py +++ b/mrp_repair_discount/models/__init__.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# © 2015 Nicola Malcontenti - Agile Business Group +# Copyright 2015 Nicola Malcontenti - Agile Business Group # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from . import mrp_repair diff --git a/mrp_repair_discount/models/mrp_repair.py b/mrp_repair_discount/models/mrp_repair.py index 90961d3e9..91160ca48 100644 --- a/mrp_repair_discount/models/mrp_repair.py +++ b/mrp_repair_discount/models/mrp_repair.py @@ -1,32 +1,66 @@ # -*- coding: utf-8 -*- -# © 2015 Nicola Malcontenti - Agile Business Group -# © 2016 Pedro M. Baeza +# Copyright 2015 Nicola Malcontenti - Agile Business Group +# Copyright 2016 Pedro M. Baeza # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from openerp import api, fields, models -from openerp.osv import fields as old_fields +from odoo import api, fields, models import openerp.addons.decimal_precision as dp +class RepairFee(models.Model): + _inherit = 'mrp.repair.fee' + + @api.one + @api.depends( + 'to_invoice', + 'price_unit', + 'repair_id', + 'product_uom_qty', + 'product_id') + def _compute_price_subtotal(self): + if not self.to_invoice: + self.price_subtotal = 0.0 + else: + taxes = self.env['account.tax'].compute_all( + self.price_unit, self.repair_id.pricelist_id.currency_id, + self.product_uom_qty, self.product_id, + self.repair_id.partner_id) + self.price_subtotal = ( + taxes['total_excluded'] * (1 - (self.discount or 0.0) / 100.0)) + + discount = fields.Float(string='Discount (%)') + price_subtotal = fields.Float( + 'Subtotal', + compute='_compute_price_subtotal', + digits=dp.get_precision('Account')) + + class MrpRepairLine(models.Model): _inherit = 'mrp.repair.line' + @api.one + @api.depends( + 'to_invoice', + 'price_unit', + 'repair_id', + 'product_uom_qty', + 'product_id') + def _compute_price_subtotal(self): + if not self.to_invoice: + self.price_subtotal = 0.0 + else: + taxes = self.env['account.tax'].compute_all( + self.price_unit, self.repair_id.pricelist_id.currency_id, + self.product_uom_qty, self.product_id, + self.repair_id.partner_id) + self.price_subtotal = ( + taxes['total_excluded'] * (1 - (self.discount or 0.0) / 100.0)) + discount = fields.Float(string='Discount (%)') - - @api.multi - def _amount_line(self, field_name, arg): - res = super(MrpRepairLine, self)._amount_line(field_name, arg) - for line in self.filtered('discount'): - price = res[line.id] * (1 - (line.discount or 0.0) / 100.0) - res[line.id] = price - return res - - _columns = { - # Must be defined in old API so that we can call super in the compute - 'price_subtotal': old_fields.function( - _amount_line, string='Subtotal', - digits_compute=dp.get_precision('Account')), - } + price_subtotal = fields.Float( + 'Subtotal', + compute='_compute_price_subtotal', + digits=dp.get_precision('Account')) class MrpRepair(models.Model): @@ -37,8 +71,48 @@ class MrpRepair(models.Model): res = super(MrpRepair, self).action_invoice_create(group) for repair in self: operations = repair.operations.filtered('to_invoice') - for op in operations: - op.invoice_line_id.discount = op.discount - if operations: - repair.invoice_id.button_reset_taxes() + fees_lines = repair.fees_lines.filtered('to_invoice') + if repair.invoice_method != 'none': + for op in operations: + op.invoice_line_id.discount = op.discount + if operations: + repair.invoice_id.compute_taxes() + for fl in fees_lines: + fl.invoice_line_id.discount = fl.discount + if fees_lines: + repair.invoice_id.compute_taxes() return res + + def _calc_line_base_price(self, line): + return line.price_unit * (1 - (line.discount or 0.0) / 100.0) + + @api.multi + @api.depends('operations', 'fees_lines') + def _compute_tax(self): + for repair in self: + val = 0.0 + cur = repair.pricelist_id.currency_id + for line in repair.operations: + if line.to_invoice: + tax_calculate = line.tax_id.compute_all( + self._calc_line_base_price(line), + self.pricelist_id.currency_id, + line.product_uom_qty, + line.product_id, + repair.partner_id) + for c in tax_calculate['taxes']: + val += c['amount'] + for line in repair.fees_lines: + if line.to_invoice: + tax_calculate = line.tax_id.compute_all( + self._calc_line_base_price(line), + self.pricelist_id.currency_id, + line.product_uom_qty, + line.product_id, + repair.partner_id) + for c in tax_calculate['taxes']: + val += c['amount'] + repair.amount_tax = cur.round(val) + + amount_tax = fields.Float( + string='Taxes', compute='_compute_tax') diff --git a/mrp_repair_discount/tests/test_mrp_repair_discount.py b/mrp_repair_discount/tests/test_mrp_repair_discount.py index 96346804c..8cf3ed735 100644 --- a/mrp_repair_discount/tests/test_mrp_repair_discount.py +++ b/mrp_repair_discount/tests/test_mrp_repair_discount.py @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- -# © 2016 Pedro M. Baeza +# Copyright 2016 Pedro M. Baeza # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from openerp.tests import common +from odoo.tests import common class TestMrpRepairDiscount(common.SavepointCase): @@ -45,18 +45,15 @@ class TestMrpRepairDiscount(common.SavepointCase): def test_discount(self): self.assertAlmostEqual( - self.repair_line.price_subtotal, 10, - self.repair_line._columns['price_subtotal'].digits[1]) + self.repair_line.price_subtotal, 10) self.assertAlmostEqual( - self.repair.amount_total, 10, - self.repair._columns['amount_total'].digits[1]) + self.repair.amount_total, 10) def test_invoice_create(self): self.repair.state = '2binvoiced' res = self.repair.action_invoice_create() invoice = self.env['account.invoice'].browse(res.values()[0]) - invoice_line = invoice.invoice_line[0] + invoice_line = invoice.invoice_line_ids[0] self.assertEqual(invoice_line.discount, 50) self.assertAlmostEqual( - invoice_line.price_subtotal, 10, - invoice_line._columns['price_subtotal'].digits[1]) + invoice_line.price_subtotal, 10) diff --git a/mrp_repair_discount/views/mrp_repair_view.xml b/mrp_repair_discount/views/mrp_repair_view.xml index 13d5c7365..72a8f92bc 100644 --- a/mrp_repair_discount/views/mrp_repair_view.xml +++ b/mrp_repair_discount/views/mrp_repair_view.xml @@ -1,7 +1,6 @@ - - - + + mrp.repair.discount.form mrp.repair @@ -9,7 +8,9 @@ + + + - - - + +