Skip to content

Commit

Permalink
Merge pull request #322 from Vauxoo/15.0
Browse files Browse the repository at this point in the history
Syncing from upstream Vauxoo/addons-vauxoo (15.0)
  • Loading branch information
bt-admin authored May 3, 2024
2 parents 9bcee00 + 3ed0c76 commit 91686e2
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 97 deletions.
2 changes: 1 addition & 1 deletion sale_margin_percentage/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Sale Order Gross Margin Percentage",
"version": "15.0.1.0.0",
"version": "15.0.1.0.1",
"author": "Vauxoo",
"category": "Sales/Sales",
"website": "https://www.vauxoo.com/",
Expand Down
17 changes: 0 additions & 17 deletions sale_margin_percentage/i18n/es.po
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,6 @@ msgstr "Coste"
msgid "Limit margin set in sales configuration"
msgstr "Límite de margen establecido en la configuración de ventas"

#. module: sale_margin_percentage
#: model_terms:ir.ui.view,arch_db:sale_margin_percentage.view_order_form
msgid "Margin %"
msgstr "Margen %"

#. module: sale_margin_percentage
#: model:ir.model.fields,field_description:sale_margin_percentage.field_sale_order__margin_percentage
#: model:ir.model.fields,field_description:sale_margin_percentage.field_sale_order_line__margin_percentage
msgid "Margin Percentage"
msgstr "Porcentaje de margen"

#. module: sale_margin_percentage
#: model:ir.model.fields,field_description:sale_margin_percentage.field_res_company__margin_threshold
#: model:ir.model.fields,field_description:sale_margin_percentage.field_res_config_settings__margin_threshold
Expand All @@ -73,12 +62,6 @@ msgstr "Límite de margen"
msgid "Margin Threshold for product in order lines"
msgstr "Límite de margen para productos en líneas de pedido"

#. module: sale_margin_percentage
#: model:ir.model.fields,help:sale_margin_percentage.field_sale_order__margin_percentage
#: model:ir.model.fields,help:sale_margin_percentage.field_sale_order_line__margin_percentage
msgid "Margin percentage compute based on price unit"
msgstr "Porcentaje de margen calculado según la unidad de precio"

#. module: sale_margin_percentage
#: model_terms:ir.ui.view,arch_db:sale_margin_percentage.res_config_settings_view_form
msgid "Minimun margin percentage allowed"
Expand Down
25 changes: 25 additions & 0 deletions sale_margin_percentage/migrations/15.0.1.0.1/post-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import logging

from odoo import SUPERUSER_ID, api

_logger = logging.getLogger(__name__)


def migrate(cr, version):
env = api.Environment(cr, SUPERUSER_ID, {})
update_margin_threshold(env)
recompute_margin_percent(env)


def update_margin_threshold(env):
companies = env["res.company"].search([])
for company in companies:
company.write({"margin_threshold": company.margin_threshold / 100.0})

_logger.info("The margin threshold for %s companies have been updated.", len(companies))


def recompute_margin_percent(env):
order_lines = env["sale.order.line"].search([])
order_lines._compute_margin()
_logger.info("The margin percentage of %s records have been recomputed.", len(order_lines))
2 changes: 1 addition & 1 deletion sale_margin_percentage/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from . import res_company
from . import res_config_settings
from . import sale_order
from . import sale_order_line
2 changes: 1 addition & 1 deletion sale_margin_percentage/models/res_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
class ResCompany(models.Model):
_inherit = "res.company"

margin_threshold = fields.Float(digits=(16, 2), help="Margin Threshold for product in order lines")
margin_threshold = fields.Float(help="Margin Threshold for product in order lines")
61 changes: 0 additions & 61 deletions sale_margin_percentage/models/sale_order.py

This file was deleted.

30 changes: 30 additions & 0 deletions sale_margin_percentage/models/sale_order_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from odoo import api, fields, models


class SaleOrderLine(models.Model):
_inherit = "sale.order.line"

margin_threshold = fields.Float(
default=lambda self: self.env.user.company_id.margin_threshold, help="Limit margin set in sales configuration"
)
purchase_price = fields.Float(readonly=True, help="Price purchase of product")

@api.depends("price_subtotal", "product_uom_qty", "purchase_price")
def _compute_margin(self):
res = super()._compute_margin()
for line in self:
currency = line.order_id.pricelist_id.currency_id
if not line.product_uom_qty:
line.margin_percent = 0.0
continue

if currency.is_zero(line.price_unit) or currency.is_zero(line.price_subtotal):
line.margin_percent = -1.0
continue

purchase_price = line.purchase_price or line.product_id.standard_price
if currency.is_zero(purchase_price):
line.margin_percent = 1.0
continue

return res
17 changes: 9 additions & 8 deletions sale_margin_percentage/tests/test_sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,37 +37,37 @@ def test_01_margin_cost_0(self):
"""Margin and Margin Percentage when cost is 0"""
sale = self.create_so(quantity=4, price=12)
self.assertEqual(sale.order_line.margin, 48)
self.assertEqual(sale.order_line.margin_percentage, 100)
self.assertEqual(sale.order_line.margin_percent, 1.0)

def test_02_margin_sale_price_0(self):
"""Margin and Margin Percentage when sale price is 0"""
sale = self.create_so(product=self.product_cost_15, quantity=4, price=0)
self.assertEqual(sale.order_line.margin, -60)
self.assertEqual(sale.order_line.margin_percentage, -100)
self.assertEqual(sale.order_line.margin_percent, -1.0)

def test_03_margin_sale_qty_0(self):
"""Margin and Margin Percentage when product qty is 0"""
sale = self.create_so(product=self.product_cost_10, quantity=0, price=15)
self.assertEqual(sale.order_line.margin, 0.0)
self.assertEqual(sale.order_line.margin_percentage, 0.0)
self.assertEqual(sale.order_line.margin_percent, 0.0)

def test_04_margin_positive(self):
"""Margin and Margin Percentage when purchase price < sale cost"""
sale = self.create_so(product=self.product_cost_10, quantity=4, price=12)
self.assertEqual(sale.order_line.margin, 8.0)
self.assertAlmostEqual(sale.order_line.margin_percentage, 16.66, places=1)
self.assertAlmostEqual(sale.order_line.margin_percent, 0.1666, places=3)

def test_05_margin_negative(self):
"""Margin and Margin Percentage when purchase price > sale cost"""
sale = self.create_so(product=self.product_cost_15, quantity=4, price=12)
self.assertEqual(sale.order_line.margin, -12)
self.assertAlmostEqual(sale.order_line.margin_percentage, -25, places=1)
self.assertAlmostEqual(sale.order_line.margin_percent, -0.25, places=3)

def test_06_margin_balanced(self):
"""Margin and Margin Percentage when purchase price = sale cost"""
sale = self.create_so(product=self.product_cost_15, quantity=4, price=15)
self.assertEqual(sale.order_line.margin, 0.0)
self.assertEqual(sale.order_line.margin_percentage, 0.0)
self.assertEqual(sale.order_line.margin_percent, 0.0)

def test_07_margin_total(self):
"""Margin Percentage of the sale order."""
Expand All @@ -81,7 +81,8 @@ def test_07_margin_total(self):
self.create_so_line(sale, product=self.product_cost_10, quantity=5, price=12)
self.assertEqual(sale.order_line.mapped("margin"), [48.0, -60.0, -12.0, 8.0, 0.0, 0.0, 32.0, 10.0])
self.assertEqual(
sale.order_line.mapped("margin_percentage"), [100.0, -100.0, -25.0, 16.67, 0.0, 0.0, 61.54, 16.67]
list(map(lambda x: round(x, 4), sale.order_line.mapped("margin_percent"))),
[1.0, -1.0, -0.25, 0.1667, 0.0, 0.0, 0.6154, 0.1667],
)
self.assertEqual(sale.margin, 26)
self.assertAlmostEqual(sale.margin_percentage, 8.55, places=1)
self.assertAlmostEqual(sale.margin_percent, 0.0855, places=3)
2 changes: 1 addition & 1 deletion sale_margin_percentage/views/res_config_settings_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Minimun margin percentage allowed
</div>
<div class="text-muted">
<field name="margin_threshold" />
<field name="margin_threshold" widget="percentage" />
</div>
</div>
</div>
Expand Down
9 changes: 2 additions & 7 deletions sale_margin_percentage/views/sale_order_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,17 @@
<xpath expr="//field[@name='margin']" position="attributes">
<attribute name="groups">base.group_system</attribute>
</xpath>
<xpath expr="//field[@name='margin']" position="after">
<field name="margin_percentage" class="oe_inline" />
</xpath>
<xpath expr="//field[@name='order_line']/form//field[@name='purchase_price']" position="after">
<field name="margin_threshold" invisible="1" />
<field name="margin_percentage" />
</xpath>
<xpath expr="//field[@name='order_line']/tree//field[@name='purchase_price']" position="after">
<field name="margin_threshold" invisible="1" />
<field name="margin_percentage" string="Margin %" />
</xpath>
<xpath expr="//field[@name='order_line']/tree" position="attributes">
<attribute name="decoration-danger">margin_percentage &lt;= 0.0</attribute>
<attribute name="decoration-danger">margin_percent &lt;= 0.0</attribute>
<attribute
name="decoration-warning"
>margin_percentage &lt;= margin_threshold and margin_percentage &gt; 0.0</attribute>
>margin_percent &lt;= margin_threshold and margin_percent &gt; 0.0</attribute>
</xpath>
</field>
</record>
Expand Down

0 comments on commit 91686e2

Please sign in to comment.