diff --git a/account_invoice_margin/__manifest__.py b/account_invoice_margin/__manifest__.py index 562b81e0..a8580707 100755 --- a/account_invoice_margin/__manifest__.py +++ b/account_invoice_margin/__manifest__.py @@ -1,7 +1,7 @@ { 'name': 'Invoice Margin', 'author': 'Hibou Corp. ', - 'version': '12.0.1.0.0', + 'version': '13.0.1.0.0', 'category': 'Accounting', 'sequence': 95, 'summary': 'Invoices include margin calculation.', diff --git a/account_invoice_margin/models/account_invoice.py b/account_invoice_margin/models/account_invoice.py index 295d68f7..6659ac70 100644 --- a/account_invoice_margin/models/account_invoice.py +++ b/account_invoice_margin/models/account_invoice.py @@ -2,53 +2,53 @@ from odoo import api, fields, models from odoo.addons import decimal_precision as dp -class AccountInvoiceLine(models.Model): - _inherit = "account.invoice.line" +class AccountMoveLine(models.Model): + _inherit = "account.move.line" margin = fields.Float(compute='_product_margin', digits=dp.get_precision('Product Price'), store=True) purchase_price = fields.Float(string='Cost', digits=dp.get_precision('Product Price')) - def _compute_margin(self, invoice_id, product_id, product_uom_id, sale_line_ids): + def _compute_margin(self, move_id, product_id, product_uom_id, sale_line_ids): # if sale_line_ids and don't re-browse for line in sale_line_ids: return line.purchase_price - frm_cur = invoice_id.company_currency_id - to_cur = invoice_id.currency_id + frm_cur = move_id.company_currency_id + to_cur = move_id.currency_id purchase_price = product_id.standard_price if product_uom_id != product_id.uom_id: purchase_price = product_id.uom_id._compute_price(purchase_price, product_uom_id) ctx = self.env.context.copy() - ctx['date'] = invoice_id.date if invoice_id.date else fields.Date.context_today(invoice_id) - price = frm_cur.with_context(ctx)._convert(purchase_price, to_cur, invoice_id.company_id, ctx['date'], round=False) + ctx['date'] = move_id.date if move_id.date else fields.Date.context_today(move_id) + price = frm_cur.with_context(ctx)._convert(purchase_price, to_cur, move_id.company_id, ctx['date'], round=False) return price - @api.onchange('product_id', 'uom_id') + @api.onchange('product_id', 'product_uom_id') def product_id_change_margin(self): - if not self.product_id or not self.uom_id: + if not self.product_id or not self.product_uom_id: return - self.purchase_price = self._compute_margin(self.invoice_id, self.product_id, self.uom_id, self.sale_line_ids) + self.purchase_price = self._compute_margin(self.move_id, self.product_id, self.product_uom_id, self.sale_line_ids) - @api.model + @api.model_create_multi def create(self, vals): - line = super(AccountInvoiceLine, self).create(vals) + line = super(AccountMoveLine, self).create(vals) line.product_id_change_margin() return line @api.depends('product_id', 'purchase_price', 'quantity', 'price_unit', 'price_subtotal') def _product_margin(self): for line in self: - currency = line.invoice_id.currency_id + currency = line.move_id.currency_id price = line.purchase_price if line.product_id and not price: - date = line.invoice_id.date if line.invoice_id.date else fields.Date.context_today(line.invoice_id) - from_cur = line.invoice_id.company_currency_id.with_context(date=date) + date = line.move_id.date if line.move_id.date else fields.Date.context_today(line.move_id) + from_cur = line.move_id.company_currency_id.with_context(date=date) price = from_cur._convert(line.product_id.standard_price, currency, line.company_id, date, round=False) line.margin = currency.round(line.price_subtotal - (price * line.quantity)) -class AccountInvoice(models.Model): - _inherit = "account.invoice" +class AccountMove(models.Model): + _inherit = "account.move" margin = fields.Monetary(compute='_product_margin', help="It gives profitability by calculating the difference between the Unit Price and the cost.", diff --git a/account_invoice_margin/tests/test_invoice_margin.py b/account_invoice_margin/tests/test_invoice_margin.py index 27945325..3bf5cad7 100644 --- a/account_invoice_margin/tests/test_invoice_margin.py +++ b/account_invoice_margin/tests/test_invoice_margin.py @@ -6,7 +6,7 @@ class TestInvoiceMargin(TestSaleMargin): def setUp(self): super(TestInvoiceMargin, self).setUp() - self.AccountInvoice = self.env['account.invoice'] + self.AccountMove = self.env['account.move'] def test_invoice_margin(self): """ Test the sale_margin module in Odoo. """ @@ -42,12 +42,12 @@ class TestInvoiceMargin(TestSaleMargin): sale_order_so11.order_line.write({'qty_delivered': 10.0}) # Invoice the sales order. - inv_id = sale_order_so11.action_invoice_create() - inv = self.AccountInvoice.browse(inv_id) + inv_id = sale_order_so11._create_invoices() + inv = self.AccountMove.browse(inv_id) self.assertEqual(inv.margin, sale_order_so11.margin) account = self.env['account.account'].search([('internal_type', '=', 'other')], limit=1) - inv = self.AccountInvoice.create({ + inv = self.AccountMove.create({ 'partner_id': self.partner_id, 'invoice_line_ids': [ (0, 0, { diff --git a/account_invoice_margin/views/account_invoice_views.xml b/account_invoice_margin/views/account_invoice_views.xml index cce0cdc3..e1495b0b 100644 --- a/account_invoice_margin/views/account_invoice_views.xml +++ b/account_invoice_margin/views/account_invoice_views.xml @@ -3,10 +3,10 @@ account.invoice.margin.view.form - account.invoice - + account.move + - +