bemade_margin_vendor_pricelist: fix broken views and rename fields

This commit is contained in:
Marc Durepos 2024-05-07 09:44:36 -04:00
parent d6a0bfd1b9
commit 8dbe3e2de1
4 changed files with 35 additions and 35 deletions

View file

@ -18,7 +18,7 @@
# #
{ {
'name': 'Sales Margin on Vendor Price', 'name': 'Sales Margin on Vendor Price',
'version': '17.0.0.0.2', 'version': '17.0.0.0.3',
'summary': 'Enables calculation of sales margins based on vendor pricelists.', 'summary': 'Enables calculation of sales margins based on vendor pricelists.',
'description': """Adds a new method for calculating """, 'description': """Adds a new method for calculating """,
'author': 'Bemade Inc.', 'author': 'Bemade Inc.',

View file

@ -5,19 +5,19 @@ from odoo.tools.float_utils import float_is_zero, float_compare
class SaleOrder(models.Model): class SaleOrder(models.Model):
_inherit = 'sale.order' _inherit = 'sale.order'
margin_actual = fields.Monetary("Our Margin", compute='_compute_margin_actual', store=False) gross_profit = fields.Monetary("Our Margin", compute='_compute_margin_actual', store=False)
margin_percent_actual = fields.Float( gross_profit_percent = fields.Float(
string='Our Margin (%)', string='Gross Profit (%)',
compute='_compute_margin_actual', compute='_compute_margin_actual',
store=False, store=False,
group_operator='avg' group_operator='avg'
) )
@api.depends('order_line.margin_actual', 'amount_untaxed') @api.depends('order_line.gross_profit', 'amount_untaxed')
def _compute_margin_actual(self): def _compute_margin_actual(self):
for order in self: for order in self:
order.margin_actual = sum(order.order_line.mapped('margin_actual')) order.gross_profit = sum(order.order_line.mapped('gross_profit'))
order.margin_percent_actual = order.amount_untaxed and order.margin_actual / order.amount_untaxed order.gross_profit_percent = order.amount_untaxed and order.gross_profit / order.amount_untaxed

View file

@ -12,36 +12,36 @@ class SaleOrderLine(models.Model):
digits='Product Price' digits='Product Price'
) )
margin_percent_vendor = fields.Float( gross_profit_percent_vendor = fields.Float(
string='Margin (%) on Vendor Price', string='GP (%) on Vendor Price',
groups='base.group_user', groups='base.group_user',
group_operator='avg', group_operator='avg',
compute='_compute_margin_vendor' compute='_compute_gp_vendor'
) )
margin_vendor = fields.Float( gross_profit_vendor = fields.Float(
string='Margin on Vendor Price', string='GP on Vendor Price',
groups='base.group_user', groups='base.group_user',
digits='Product Price', digits='Product Price',
compute='_compute_margin_vendor' compute='_compute_gp_vendor'
) )
purchase_price_actual = fields.Float( purchase_price_actual = fields.Float(
compute="_compute_actual_margins", compute="_compute_actual_gp",
digits='Product Price', digits='Product Price',
groups="base.group_user", groups="base.group_user",
string="Purchase Price" string="Purchase Price"
) )
margin_actual = fields.Float( gross_profit = fields.Float(
compute="_compute_actual_margins", compute="_compute_actual_gp",
digits='Product Price', digits='Product Price',
groups="base.group_user", groups="base.group_user",
string="Our Margin" string="Our Margin"
) )
margin_percent_actual = fields.Float( gross_profit_percent = fields.Float(
compute="_compute_actual_margins", compute="_compute_actual_gp",
groups="base.group_user", groups="base.group_user",
string="Our Margin (%)" string="Our Margin (%)"
) )
@ -54,8 +54,8 @@ class SaleOrderLine(models.Model):
'move_ids.state', 'move_ids.state',
'qty_to_deliver' 'qty_to_deliver'
) )
def _compute_actual_margins(self): def _compute_actual_gp(self):
""" We want to use the margin based on average inventory valuation when the """ We want to use the gross profit based on average inventory valuation when the
sale order line will be completely fulfilled (or has been fulfilled) from stock. sale order line will be completely fulfilled (or has been fulfilled) from stock.
For product not yet in stock we want to use the vendor price. We can also have For product not yet in stock we want to use the vendor price. We can also have
blended calculations (partly on vendor price, partly on stock valuation). This blended calculations (partly on vendor price, partly on stock valuation). This
@ -65,8 +65,8 @@ class SaleOrderLine(models.Model):
""" """
non_product_lines = self.filtered(lambda r: not r.product_id) non_product_lines = self.filtered(lambda r: not r.product_id)
non_product_lines.purchase_price_actual = 0.0 non_product_lines.purchase_price_actual = 0.0
non_product_lines.margin_actual = 0.0 non_product_lines.gross_profit = 0.0
non_product_lines.margin_percent_actual = 0.0 non_product_lines.gross_profit_percent = 0.0
for line in self - non_product_lines: for line in self - non_product_lines:
stock_missing = line._determine_missing_stock() stock_missing = line._determine_missing_stock()
if float_is_zero(stock_missing, precision_rounding=line.product_uom.rounding): if float_is_zero(stock_missing, precision_rounding=line.product_uom.rounding):
@ -83,9 +83,9 @@ class SaleOrderLine(models.Model):
(stock_missing * line.purchase_price_vendor (stock_missing * line.purchase_price_vendor
+ qty_from_stock * line.purchase_price) \ + qty_from_stock * line.purchase_price) \
/ line.product_uom_qty / line.product_uom_qty
line.margin_actual = line.price_subtotal - ( line.gross_profit = line.price_subtotal - (
line.purchase_price_actual * line.product_uom_qty) line.purchase_price_actual * line.product_uom_qty)
line.margin_percent_actual = line.price_subtotal and line.margin_actual / line.price_subtotal line.gross_profit_percent = line.price_subtotal and line.gross_profit / line.price_subtotal
def _determine_missing_stock(self) -> float: def _determine_missing_stock(self) -> float:
""" Compute how much stock is missing to meet an order line's demand. In the """ Compute how much stock is missing to meet an order line's demand. In the
@ -146,13 +146,13 @@ class SaleOrderLine(models.Model):
) if to_cur and suppinfo.price else suppinfo.price ) if to_cur and suppinfo.price else suppinfo.price
@api.depends('purchase_price_vendor') @api.depends('purchase_price_vendor')
def _compute_margin_vendor(self): def _compute_gp_vendor(self):
for line in self: for line in self:
if not line.price_unit or float_is_zero(line.price_unit): if not line.price_unit or float_is_zero(line.price_unit):
line.margin_vendor = 0 line.gross_profit_vendor = 0
line.margin_percent_vendor = 0 line.gross_profit_percent_vendor = 0
continue continue
unit_margin = line.price_unit - line.purchase_price_vendor unit_gp = line.price_unit - line.purchase_price_vendor
line.margin_percent_vendor = unit_margin / line.price_unit line.gross_profit_percent_vendor = unit_gp / line.price_unit
line.margin_vendor = unit_margin * line.product_uom_qty line.gross_profit_vendor = unit_gp * line.product_uom_qty

View file

@ -8,14 +8,14 @@
<field name="arch" type="xml"> <field name="arch" type="xml">
<field name="margin" position="replace"> <field name="margin" position="replace">
<field name="margin" invisible="True"/> <field name="margin" invisible="True"/>
<field name="margin_actual" class="oe_inline"/> <field name="gross_profit" class="oe_inline"/>
</field> </field>
<label for="margin" position="replace"> <label for="margin" position="replace">
<label for="margin_actual" groups="base.group_user"/> <label for="gross_profit" groups="base.group_user"/>
</label> </label>
<field name="margin_percent" position="replace"> <field name="margin_percent" position="replace">
<field name="margin_percent" invisible="True"/> <field name="margin_percent" invisible="True"/>
<field name="margin_percent_actual" nolabel="1" class="oe_inline" <field name="gross_profit_percent" nolabel="1" class="oe_inline"
widget="percentage" groups="base.group_user"/> widget="percentage" groups="base.group_user"/>
</field> </field>
</field> </field>
@ -42,11 +42,11 @@
</field> </field>
<field name="margin" position="replace"> <field name="margin" position="replace">
<field name="margin" invisible="True"/> <field name="margin" invisible="True"/>
<field name="margin_actual" optional="hide"/> <field name="gross_profit" optional="hide"/>
</field> </field>
<field name="margin_percent" position="replace"> <field name="margin_percent" position="replace">
<field name="margin_percent" invisible="True"/> <field name="margin_percent" invisible="True"/>
<field name="margin_percent_actual" optional="hide" <field name="gross_profit_percent" optional="hide"
widget="percentage" groups="base.group_user"/> widget="percentage" groups="base.group_user"/>
</field> </field>
</field> </field>