首页 > 解决方案 > 如何在odoo中更新列表时在方法内退出ZeroDivisionError?

问题描述

我正在尝试返回一个列表以在 pdf 报告中打印它,并且返回给了我正确的数据,但如果产品成本为零,我得到

won_perecnt = (total_won * 100) / total_cost

除以零ZeroDivisionError:浮点数

如果发生错误,我需要发出必须更改某些产品成本的警告。

这是代码

@api.multi
def product_summary(self,):
    product_summary_dict = {}
    data=[]
    if self.date_from and self.date_to:
        order_detail = self.env['pos.order'].search([('date_order', '>=',self.date_from),
                                                     ('date_order', '<=', self.date_to)])
        if order_detail:
            for each_order in order_detail:
                for each_order_line in each_order.lines:
                    if each_order_line.product_id.name in product_summary_dict:
                        product_qty = product_summary_dict[each_order_line.product_id.name]
                        product_qty += each_order_line.qty
                        total_sales = each_order_line.price_unit * product_qty
                        total_cost = each_order_line.product_id.standard_price*product_qty
                        total_won = total_sales - total_cost
                        won_perecnt= (total_won *100)/ total_cost
                        res1= {
                            "name": each_order_line.product_id.name,
                            "sold_qty": product_qty,
                            "price_unit": each_order_line.price_unit,
                            'total_sales': total_sales,
                            "total_cost": total_cost,
                            "total_won": total_won,
                            "won_perecnt": won_perecnt,

                        }
                        data.append(res1)
                    else:
                        product_qty = each_order_line.qty
                        total_sales = each_order_line.price_unit * product_qty
                        total_cost = each_order_line.product_id.standard_price * product_qty
                        total_won = total_sales - total_cost
                        won_perecnt= (total_won *100)/ total_cost
                        res2 = {
                            "name": each_order_line.product_id.name,
                            "sold_qty": product_qty,
                            "price_unit": each_order_line.price_unit,
                            'total_sales': total_sales,
                            "total_cost": total_cost,
                            "total_won": total_won,
                            "won_perecnt": won_perecnt,
                        }
                        data.append(res2)
                    product_summary_dict[each_order_line.product_id.name] = product_qty;
    if data:
        return data
    else:
        return {}

任何帮助将不胜感激

标签: pythonpython-3.xodoodivide-by-zero

解决方案


做这个改变:

try:
    won_percent = (total_won * 100) / total_cost
except ZeroDivisionError:
    won_percent = None

print(won_percent)

Nonetotal_cost为零时返回。

完整代码:

@api.multi
def product_summary(self):
    product_summary_dict = {}
    data=[]
    if self.date_from and self.date_to:
        order_detail = self.env['pos.order'].search([('date_order', '>=',self.date_from),
                                                     ('date_order', '<=', self.date_to)])
        if order_detail:
            for each_order in order_detail:
                for each_order_line in each_order.lines:
                    if each_order_line.product_id.name in product_summary_dict:
                        product_qty = product_summary_dict[each_order_line.product_id.name]
                        product_qty += each_order_line.qty
                        total_sales = each_order_line.price_unit * product_qty
                        total_cost = each_order_line.product_id.standard_price*product_qty
                        total_won = total_sales - total_cost
                        try:
                            won_perecnt = (total_won * 100) / total_cost
                        except ZeroDivisionError:
                            won_perecnt = None
                        res1 = {
                            "name": each_order_line.product_id.name,
                            "sold_qty": product_qty,
                            "price_unit": each_order_line.price_unit,
                            'total_sales': total_sales,
                            "total_cost": total_cost,
                            "total_won": total_won,
                            "won_perecnt": won_perecnt,
                        }
                        data.append(res1)
                    else:
                        product_qty = each_order_line.qty
                        total_sales = each_order_line.price_unit * product_qty
                        total_cost = each_order_line.product_id.standard_price * product_qty
                        total_won = total_sales - total_cost
                        try:
                            won_perecnt = (total_won * 100) / total_cost
                        except ZeroDivisionError:
                            won_perecnt = None
                        res2 = {
                            "name": each_order_line.product_id.name,
                            "sold_qty": product_qty,
                            "price_unit": each_order_line.price_unit,
                            'total_sales': total_sales,
                            "total_cost": total_cost,
                            "total_won": total_won,
                            "won_perecnt": won_perecnt,
                        }
                        data.append(res2)
                    product_summary_dict[each_order_line.product_id.name] = product_qty;
    return data if data else {}

推荐阅读