我需要计算amount_total
州的每个销售订单中的所有draft
。
我可以通过以下方式阅读此域中的所有销售:
sale_order = fields.Many2one('sale.order', domain=[('state', '=', 'draft')], string="Sales Planned")
amount_total = fields.Float("Total Planned Sales", related="sale_order.amount_total")
但是通过这种方式,我一次只能搜索一个销售订单,然后amount_total
只显示我已经选择的销售订单中的总计。
那么,如何阅读draft
州的所有销售订单,并在此基础上计算所有amount_total
?
有_amount_all
函数,但我想它需要以某种方式被覆盖。
修改
这就是我班级现在的样子:
class account_budget_bsi_line(models.Model):
_name = 'account.budget.bsi.line'
_inherit = ['sale.order', 'stock.location', 'stock.move', 'purchase.order',]
@api.one
def _compute_sales_bsi(self):
self.sales_bsi = self.env['sale.order'].search([('state', '=', 'draft')])
account_budget_bsi_id = fields.Many2one('account.budget.bsi', ondelete='cascade', select=True, required=True)
opening_stock = fields.Float(string="Opening stock")
sale_order = fields.Many2one('sale.order', domain=[('state', '=', 'draft')], string="Sales Planned")
#amount_total = fields.Float("Total Planned Sales", related="sale_order.amount_total")
amount_total = fields.Float("Total Planned Sales", compute="_compute_amount_total")
sales_bsi = fields.One2many('sale.order', compute="_compute_sales_bsi", string="Sales Test")
#sales_state = fields.Selection(related='sale_order.state', string="Sales Planned")
interauxiliary_transfers = fields.Many2one('stock.move', string="Interauxiliary transfers")
interauxiliary_receipts = fields.Many2one('stock.move', string="Interauxiliary receipts")
auxiliary_receipts = fields.Many2one('stock.move', string="Auxiliary receipts")
prod_purchased = fields.Many2one('stock.move', string="Products Purchased")
closing_stock = fields.Many2one('stock.move', string="Closing stock")
@api.multi
@api.depends('sale_order')
def _compute_amount_total(self):
draft_orders = self.env['sale.order'].search([('state', '=', 'draft')])
amount_total = sum(draft_orders.mapped('amount_total')
for record in self:
record.amount_total = amount_total
答案 0 :(得分:2)
你没有提到你正在创建这些字段的模型,所以我在这里猜一点。
为什么不让amount_total
成为computed field?
amount_total = fields.Float("Total Planned Sales", compute="_compute_amount_total")
@api.multi
@api.depends('fields_depended_on')
def _compute_amount_total(self):
draft_orders = self.env['sale.order'].search([('state', '=', 'draft')])
amount_total = sum(draft_orders.mapped('amount_total'))
for record in self:
record.amount_total = amount_total