Я пользователь odoo 13 (не разработчик). Я пытаюсь создать пользовательский модуль для отображения процента маржи продукта в строке заказа на покупку. Я скопировал код модуля из надстройки Odoo sale_margin по умолчанию и адаптировал его для своего пользовательского модуля. В Заказе на поставку, когда я добавляю товар, редактирую количество или цену товара, у меня появляется ошибка: margin = ((price - line.price_unit) / price) * 100 ZeroDivisionError: деление с плавающей точкой на ноль
Пожалуйста, можете кто-нибудь поможет мне решить эту проблему? Ниже код, спасибо
from odoo import models, fields, api
import odoo.addons.decimal_precision as dp
class PurchaseOrderLine(models.Model):
_inherit = 'purchase.order.line'
margin = fields.Float(string='Margin (%)', compute='_product_margin', digits='Product Price', store=True)
sale_price = fields.Float(string='Price', digits='Product Price')
def _compute_margin(self, order_id, product_id, product_uom_id):
sale_price = product_id.lst_price
if product_uom_id != product_id.uom_id:
sale_price = product_id.uom_id._compute_price(sale_price, product_uom_id)
@api.model
def _get_sale_price(self, product, product_uom):
sale_price = product.lst_price
if product_uom != product.uom_id:
sale_price = product.uom_id._compute_price(sale_price, product_uom)
@api.onchange('product_id', 'product_uom')
def product_id_change_margin(self):
if not self.product_id or not self.product_uom:
return
self.sale_price = self._compute_margin(self.order_id, self.product_id, self.product_uom)
@api.model
def create(self, vals):
vals.update(self._prepare_add_missing_fields(vals))
# Calculation of the margin for programmatic creation of a PO line. It is therefore not
# necessary to call product_id_change_margin manually
if 'sale_price' not in vals and ('display_type' not in vals or not vals['display_type']):
order_id = self.env['sale.order'].browse(vals['order_id'])
product_id = self.env['product.product'].browse(vals['product_id'])
product_uom_id = self.env['uom.uom'].browse(vals['product_uom'])
vals['sale_price'] = self._compute_margin(order_id, product_id, product_uom_id)
return super(SaleOrderLine, self).create(vals)
@api.depends('product_id', 'sale_price', 'product_uom', 'price_unit', 'price_subtotal')
def _product_margin(self):
for line in self:
price = line.sale_price
margin = ((price - line.price_unit) / price) * 100
line.margin = margin