计划(分配)数量,搜索

时间:2019-06-20 13:10:00

标签: odoo odoo-10

我用我的方法计算了2个字段。计算分配的产品数量。

class ProductProduct(models.Model):
    _inherit = 'product.product'

    planned_qty_cust = fields.Float(compute='_calculate_planned_qty', string='Planned Qty Customer', )
    planned_qty_supl = fields.Float(compute='_calculate_planned_qty', string='Planned Qty Suplier', )


    @api.multi
    def _calculate_planned_qty(self):
        for product in self:
            stock_move_obj = self.env['stock.move']
            domain = [('product_id', '=', product.id),
                                                 ('state', 'not in', ['cancel', 'done','draft']),
                                                 ('location_dest_id.usage', '=', 'customer'),
                                                 ]
            stock_moves_cust = stock_move_obj.search(domain)
            qty = sum(stock_moves_cust.mapped('product_uom_qty'))
            product.planned_qty_cust = qty
            domain2 = [('product_id', '=', product.id),
                      ('state', 'not in', ['cancel', 'done', 'draft']),
                      ('location_dest_id.usage', '=', 'supplier'),
                      ]
            stock_moves_cust = stock_move_obj.search(domain2)
            qty = sum(stock_moves_cust.mapped('product_uom_qty'))
            product.planned_qty_supl = qty

问题是我需要对这些字段进行过滤,可能我需要创建搜索功能,但这有点复杂。

也许有人可以帮助我解决这种方法的外观,或者也许已经有一个用于这种类型的计算的模块?

1 个答案:

答案 0 :(得分:0)

这就是我设法解决此问题的方式

    planned_qty_cust = fields.Float(
    compute='_calculate_planned_qty',
    string='Planned Qty Customer',
    search='_search_name_qty_cust',
)
planned_qty_supl = fields.Float(
    compute='_calculate_planned_qty', string='Planned Qty Suplier',
    search='_search_name_qty_sup',
)

def _calc_planned_qty(self, domain, operator, value):
    move_data = self.env['stock.move'].read_group(
        [
            ('product_uom_qty', operator, value),
            ('state', 'not in', ['cancel', 'done', 'draft']),
            domain,
        ],
        ['product_id'],
        ['product_id'],
    )
    res = [data['product_id'][0] for data in move_data]
    return res

def _search_name_qty_cust(self, operator, value):
    domain = ('location_dest_id.usage', '=', 'customer')
    res = self._calc_planned_qty(domain,operator,value)
    return [('id', 'in', res)]

def _search_name_qty_sup(self, operator, value):
    domain = ('location_dest_id.usage', '=', 'supplier')
    res = self._calc_planned_qty(domain, operator, value)
    return [('id', 'in', res)]