计算从一个模型到另一个模型的字段数量 - Odoo v8

Compute fields quantities from one model to another - Odoo v8

考虑这四个模型:

class bsi_production_order(models.Model):
    _name = 'bsi.production.order'

    name = fields.Char('Reference', required=True, index=True, copy=False, readonly='True', default='New')
    date = fields.Date(string="Production Date")
    production_type = fields.Selection([
        ('budgeted','Budgeted'),
        ('nonbudgeted','Non Budgeted'),
        ('direct','Direct Order'),
    ], string='Type of Order', index=True,  
    track_visibility='onchange', copy=False,
    help=" ")
    notes = fields.Text(string="Notes")
    order_lines = fields.One2many('bsi.production.order.lines', 'production_order', states={'finished': [('readonly', True)], 'cancel': [('readonly', True)]}, string="Order lines", copy=True)
    print_orders = fields.One2many('bsi.print.order', 'production_orders', string="Print Orders")

class bsi_production_order_lines(models.Model):
    _name = 'bsi.production.order.lines'

    production_order = fields.Many2one('bsi.production.order', string="Production Orders")
    isbn = fields.Many2one('product.product', string="ISBN", domain="[('is_isbn', '=', True)]")
    qty = fields.Float(string="Quantity")
    consumed_qty = fields.Float(string="Consumed quantity")
    remaining_qty = fields.Float(string="Remaining quantity", compute="_remaining_func")

    @api.onchange('qty', 'consumed_qty')
    def _remaining_func(self):
        if self.qty or self.consumed_qty:
            self.remaining_qty = self.qty +(-self.consumed_qty)

class bsi_print_order(models.Model):
    _name = 'bsi.print.order'

    name = fields.Char('Reference', required=True, index=True, copy=False, readonly='True', default='New')
    date = fields.Date(string="Print Date")
    origin = fields.Char(string="Origin")
    production_orders = fields.Many2one('bsi.production.order', ondelete='cascade', string="Production Order")
    order_lines = fields.One2many('bsi.print.order.lines', 'print_order', string="Order lines")

class bsi_print_order_lines(models.Model):
    _name = 'bsi.print.order.lines'

    print_order = fields.Many2one('bsi.print.order', string="Print Order")
    production_orders = fields.Many2one('bsi.production.order', ondelete='cascade', string="Production Order")
    isbn = fields.Many2one('product.product', string="ISBN", domain="[('is_isbn', '=', True)]")
    qty = fields.Integer(string="Quantity")
    consumed_qty = fields.Integer(string="Quantity consumed")
    remaining_qty = fields.Float(string="Remaining quantity", compute="_remaining_func")

    @api.onchange('qty', 'consumed_qty')
    def _remaining_func(self):
        if self.consumed_qty or self.qty:
            self.remaining_qty = self.qty +(-self.consumed_qty)

因此,生产订单有生产订单行,打印订单也有它的订单行(One2many order_lines 字段)

两者都有方法,都叫_remaining_func_.

这些对于 remaining_qty 字段是可以的,但是 consumed_qty 应该在 production.order 和 print.order 之间相互关联。

因此,例如,如果 bsi.production.order.lines 上的 qty 是 10,(还有其他方法可以从生产订单创建 bsi.print.order),并且 bsi.print.order我把 qty 的值设置为 5,原来的 10 在 bsi.production.order.line 上应该是 5,我认为用类似 _remaining_func_ 的方法我可以实现这个,但我有点困惑如何在两个模型之间执行此操作。

有什么想法吗?

如果需要进一步解释,请告诉我。

除非bsi.production.orderbsi.print.order之间的关系是1:1,否则您想要的是不可能管理的,但在您的情况下,生产订单似乎可以有许多打印订单。我举个例子:

您可以在 bsi.print.order.line 中创建一个指向 bsi.production.order.line:

Many2one 字段
class bsi_print_order_lines(models.Model):
    _name = 'bsi.print.order.lines'

    po_line_related = fields.Many2one('bsi.production.order.lines', ondelete='cascade', string="Production Order Line Related")

并且每次创建打印线时,您都可以轻松创建相关的生产线(您拥有所需的所有数据):

@api.model
def create(self, vals):
    print_line = super(bsi_print_order_lines, self).create(vals)
    po_line_vals = {
        'production_order': print_line.print_order.production_orders.id,
        'isbn': print_line.isbn,
        'qty': print_line.qty,
        'consumed_qty': print_line.consumed_qty,
        'remaining_qty': print_line.remaining_qty,
    }
    po_line = self.env['bsi.production.order.lines'].create(po_line_vals)
    return print_line

但你必须反过来做同样的事情(这次覆盖 bsi.production.order.lines ORM create 方法),你会发现问题所在:

@api.model
def create(self, vals):
    po_line = super(bsi_production_order_lines, self).create(vals)
    print_line_vals = {
        'production_orders': po_line.production_order.id,
        'po_line_related': po_line.id,
        'isbn': po_line.isbn,
        'qty': po_line.qty,
        'consumed_qty': po_line.consumed_qty,
        'remaining_qty': po_line.remaining_qty,
        'print_order': '???????'  # You cannot know which print order you have to write here since a production order can have several ones...
    }
    print_line = self.env['bsi.print.order.lines'].create(print_line_vals)
    return po_line

如果 bsi.production.orderbsi.print.order 之间的关系是 1:1,您可以使用 search 获得打印订单(因为您可以确定它会 return只有一条记录):

@api.model
def create(self, vals):
    po_line = super(bsi_production_order_lines, self).create(vals)
    print_order = self.env['bsi.print.order'].search([
        ('production_orders', '=', po_line.production_order.id)
    ]).ensure_one()
    print_line_vals = {
        'production_orders': po_line.production_order.id,
        'po_line_related': po_line.id,
        'isbn': po_line.isbn,
        'qty': po_line.qty,
        'consumed_qty': po_line.consumed_qty,
        'remaining_qty': po_line.remaining_qty,
        'print_order': print_order.id,
    }
    print_line = self.env['bsi.print.order.lines'].create(print_line_vals)
    return po_line

这样你的生产线和打印线就会相关联,你也必须覆盖 writeunlink 方法来控制何时修改或删除一条线,做它的 "twin" 也一样(由于名为 po_line_related 的新 Many2one 字段,很容易找到它)。

当然这不是一个很好的解决方案,但我认为它是您的实体关系图的唯一解决方案(使用 Odoo API)。