0

这篇文章应该比平时复杂一点。

我们为 account.invoice.line 创建了一个新字段:pack_operation。使用此字段,我们可以为 PDF 发票上的每一行打印序列号/批号(这部分效果很好)。

尝试编写域以为每个发票行选择 EXACT 和 ONLY 库存包操作经过了许多小时。

在下面的代码中,我们使用域 [('id','=', 31)] 来使我们的测试打印 PDF。

何写此域以确保 100% 我们将为每个发票行获得正确的库存包装操作?

我真的需要你的帮助...对我的大脑来说太复杂了。我们的代码:

class AccountInvoiceLine(models.Model):
_inherit = "account.invoice.line"
    pack_operation = fields.Many2one(comodel_name='stock.pack.operation', compute='compute_stock_pack_operation_id')

    def compute_stock_pack_operation_id(self):       
         stock_operation_obj = self.env['stock.pack.operation']
         stock_operation = stock_operation_obj.search(            [('id','=', 31)]            )
         self.pack_operation = stock_operation[0]




   EDIT#1
I know that you won't like my code. But, this one seems to work. I take any comments and improvements with pleasure.



    class AccountInvoiceLine(models.Model):
    _inherit = "account.invoice.line"
    pack_operation = fields.Many2one(comodel_name='stock.pack.operation', compute='compute_stock_pack_operation_id')@api.one
    def compute_stock_pack_operation_id(self):


        procurement_order_obj = self.env['procurement.order']
        stock_operation_obj = self.env['stock.pack.operation']

        all_picking_ids_for_this_invoice_line = []


        for saleorderline in self.sale_line_ids:

            for procurement in saleorderline.procurement_ids:

                for stockmove in procurement.move_ids:

                    if stockmove.picking_id.id not in all_picking_ids_for_this_invoice_line
                        all_picking_ids_for_this_invoice_line.append(stockmove.picking_id.id)


        all_picking_ids_for_this_invoice_line))

        stock_operation = stock_operation_obj.search(
            [ '&', 
                ('picking_id','in',all_picking_ids_for_this_invoice_line),
                ('product_id','=',self.product_id.id)
                ]
            )

        self.pack_operation = stock_operation[0]
4

1 回答 1

0

pack_operation字段是一个计算字段,默认意味着该字段不会保存在数据库中,除非您store=True在定义字段时进行设置。

所以,你可以在这里做的是改变:

pack_operation = fields.Many2one(comodel_name='stock.pack.operation', compute='compute_stock_pack_operation_id')

至:

pack_operation = fields.Many2one(comodel_name='stock.pack.operation', compute='compute_stock_pack_operation_id', store=True)

并尝试再次运行您的查询。

于 2017-02-10T14:16:23.320 回答