Skip to content

Commit

Permalink
[MIG] account_invoice_supplierinfo_update_standard_price: Migration t…
Browse files Browse the repository at this point in the history
…o 16.0 (from 12.0)
  • Loading branch information
legalsylvain committed Jan 31, 2025
1 parent 938806a commit f48db32
Show file tree
Hide file tree
Showing 13 changed files with 231 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"summary": "In the supplier invoice, automatically update all products "
"whose standard price on the line is different from "
" the product standard price",
"version": "12.0.1.1.3",
"version": "16.0.1.0.0",
"category": "Accounting & Finance",
"author": "GRAP",
"website": "https://github.com/grap/grap-odoo-business",
Expand All @@ -17,11 +17,11 @@
],
"data": [
"wizard/wizard_update_invoice_supplierinfo.xml",
"views/view_account_invoice.xml",
"views/view_account_move.xml",
"views/view_product_template.xml",
],
"images": [
"static/description/wizard_form.png",
"demo": [
"demo/product_product.xml",
],
"installable": True,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2018 - Today: GRAP (http://www.grap.coop)
@author: Sylvain LE GAL (https://twitter.com/legalsylvain)
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
-->
<odoo>

<record id="transport_costs_product" model="product.product">
<field name="name">Demo Transport costs</field>
<field name="categ_id" ref="product.product_category_all"/>
<field name="uom_id" ref="uom.product_uom_unit"/>
<field name="is_impact_standard_price" eval="True"/>
</record>

</odoo>
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from . import account_invoice
from . import account_invoice_line
from . import account_move
from . import account_move_line
from . import product_template

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,30 @@

from odoo import api, fields, models

import odoo.addons.decimal_precision as dp


class AccountInvoice(models.Model):
_inherit = "account.invoice"
class AccountMove(models.Model):
_inherit = "account.move"

product_expense_total = fields.Float(
string="Product Expenses Total",
compute="_compute_expense_total",
digits=dp.get_precision("Product Price"),
multi="expense_total",
digits="Product Price",
store=True,
)

distributed_expense_total = fields.Float(
string="Distributed Expenses Total",
compute="_compute_expense_total",
digits=dp.get_precision("Product Price"),
multi="expense_total",
digits="Product Price",
store=True,
)

@api.multi
@api.depends(
"invoice_line_ids.product_id.is_impact_standard_price",
"invoice_line_ids.price_subtotal",
)
def _compute_expense_total(self):
for invoice in self.filtered(lambda x: x.type == "in_invoice"):
for invoice in self.filtered(lambda x: x.move_type == "in_invoice"):
invoice.update(
{
"product_expense_total": sum(
Expand All @@ -50,7 +45,6 @@ def _compute_expense_total(self):
}
)

@api.multi
def _get_update_supplierinfo_lines(self):
ProductProduct = self.env["product.product"]
res = super()._get_update_supplierinfo_lines()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Copyright (C) 2018-Today: GRAP (http://www.grap.coop)
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import _, models
from odoo.exceptions import Warning as UserError
from odoo.tools.float_utils import float_compare


class AccountMoveLine(models.Model):
_inherit = "account.move.line"

def _get_standard_price(self):
self.ensure_one()
if not self.product_id:
return 0

if self.move_id.product_expense_total == 0:
raise UserError(
_("We can't check prices for a invoice whose total is null")
)
line_shared_cost = self.move_id.distributed_expense_total * (
self.price_subtotal / self.move_id.product_expense_total
)

if self.quantity:
line_shared_cost_per_unit = line_shared_cost / self.quantity
else:
line_shared_cost_per_unit = 0
uom = self.product_uom_id or self.product_id.uom_id
return self.move_id.currency_id.round(
uom._compute_price(
line_shared_cost_per_unit
+ (
self.price_unit
* (1 - self.discount1 / 100)
* (1 - self.discount2 / 100)
* (1 - self.discount3 / 100)
),
self.product_id.uom_id,
)
)

def _is_correct_price(self, supplierinfo):
self.ensure_one()
DecimalPrecision = self.env["decimal.precision"]
res = super()._is_correct_price(supplierinfo)
if not self.product_id:
return res

return res and not float_compare(
self.product_id.standard_price,
self._get_standard_price(),
precision_digits=DecimalPrecision.precision_get("Product Price"),
)

def _prepare_supplier_wizard_line(self, supplierinfo):
res = super()._prepare_supplier_wizard_line(supplierinfo)
if self.product_id:
res.update(
{
"current_standard_price": self.product_id.standard_price,
"new_standard_price": self._get_standard_price(),
}
)
return res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_module
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Copyright 2018 - Today: GRAP (http://www.grap.coop)
# Copyright Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo.tests import Form, tagged

from odoo.addons.account_invoice_supplierinfo_update_triple_discount.tests import (
test_module,
)


@tagged("post_install", "-at_install")
class TestModuleStandardPrice(test_module.TestModuleTripleDiscount):
@classmethod
def setUpClass(cls, chart_template_ref=None):
super().setUpClass(chart_template_ref=chart_template_ref)

cls.product_d = cls.env["product.product"].create(
{
"name": "product_d",
"uom_id": cls.env.ref("uom.product_uom_unit").id,
"uom_po_id": cls.env.ref("uom.product_uom_unit").id,
"lst_price": 10.0,
"standard_price": 0.0,
"property_account_income_id": cls.copy_account(
cls.company_data["default_account_revenue"]
).id,
"property_account_expense_id": cls.copy_account(
cls.company_data["default_account_expense"]
).id,
"taxes_id": [],
"supplier_taxes_id": [],
"is_impact_standard_price": True,
}
)

def _add_impact_standard_price_line(self):
with Form(self.invoice) as invoice_form:
with invoice_form.invoice_line_ids.new() as line_form:
line_form.product_id = self.product_d
line_form.quantity = 15
line_form.price_unit = 1
line_form.tax_ids.clear()
invoice_form.save()

self.line_d = self.invoice.invoice_line_ids.filtered(
lambda x: x.product_id == self.product_d
)

# configuration
# line_a (4000 $)
# - quantity: 10 // price_unit: 400 // uom_id: uom_unit

# line_b (5.6 $)
# - quantity: 1 // price_unit: 10 // uom_id: uom_dozen
# - discount1: 10.0 // discount2: 20.0 // discount3: 30.0

# line_without_product (35 $)
# - quantity: 1 // price_unit: 35

# line_d (15 $)
# - quantity: 15 // price_unit: 1

# PRODUCT TOTAL: 4000 + 5.6 + 35 = 4040.60

def test_invoice_new_field_computation(self):
self.assertAlmostEqual(self.invoice.product_expense_total, 4000 + 5.6 + 35)
self.assertAlmostEqual(self.invoice.distributed_expense_total, 0)
self._add_impact_standard_price_line()
self.assertAlmostEqual(self.invoice.distributed_expense_total, 15)

def test_supplierinfo_update_standard_price_without_distributed(self):
vals_wizard = self.invoice.check_supplierinfo().get("context", {})

line_ids = vals_wizard.get("default_line_ids", {})

self.assertEqual(len(line_ids), 2)
self.assertAlmostEqual(line_ids[0][2]["new_standard_price"], 4000.0 / 10)
self.assertAlmostEqual(line_ids[1][2]["new_standard_price"], 5.6)

# Create and launch update process
wizard = self.WizardUpdateSupplierinfo.create(
{"line_ids": line_ids, "invoice_id": self.invoice.id}
)

wizard.update_supplierinfo()

self.assertAlmostEqual(self.product_a.standard_price, 4000.0 / 10)
self.assertAlmostEqual(self.product_b.standard_price, 5.6)

def test_supplierinfo_update_standard_price_with_distributed(self):
self._add_impact_standard_price_line()

vals_wizard = self.invoice.check_supplierinfo().get("context", {})

line_ids = vals_wizard.get("default_line_ids", {})

self.assertEqual(len(line_ids), 2)
self.assertAlmostEqual(
line_ids[0][2]["new_standard_price"],
self.invoice.currency_id.round((4000.0 + 15 * 4000 / 4040.60) / 10),
)
self.assertAlmostEqual(
line_ids[1][2]["new_standard_price"],
self.invoice.currency_id.round(5.6 + 15 * 5.6 / 4040.60),
)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2018 - Today: GRAP (http://www.grap.coop)
@author: Sylvain LE GAL (https://twitter.com/legalsylvain)
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
-->
<odoo>

<record id="view_account_move_form" model="ir.ui.view">
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_move_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='tax_totals']" position="before">
<group class="oe_subtotal_footer oe_right" attrs="{'invisible': [
'|',
('move_type', 'not in', ('in_invoice', 'in_refund', 'in_receipt')),
('distributed_expense_total', '=', 0),
]}">
<field name="product_expense_total" />
<field name="distributed_expense_total" />
</group>

</xpath>
</field>
</record>

</odoo>
Loading

0 comments on commit f48db32

Please sign in to comment.