在Odoo 14或Flectra 1.7中点击创建发票时,如何将销售订单中的数据传输到发票?

人气:165 发布:2022-10-16 标签: python odoo odoo-14 flectra

问题描述

我在Docker容器中运行Flectra。我要将sale.order中的自定义字段传输到account t.Invoice。

class SaleOrder(models.Model):
    _inherit = 'sale.order' 
    myField = fields.Integer(string='My Field', default=21, required = True)

    @api.multi
    def _prepare_invoice(self):
         res = super(SaleOrder, self)._prepare_invoice()
         # res.update({
         #     'myField': self.myField,
         # })
         res['myField'] = self.myField
         return res


class SaleInvoice(models.Model):
    _inherit = 'account.invoice' 
    myField = fields.Integer(string='My Field', default=21, required = True)

我尝试在不同的变体中覆盖_PREPARE_INVOICE和_CREATE_INVOICES,但都不起作用。根据我的理解,它们应该是有效的,但我是新来Odoo/Flectra的,所以如果有任何帮助,我会很高兴。

我使用的是Flectra 1.7(社区版),我认为它对应于Odoo 14。

推荐答案

尝试此操作:

def create_invoice(self):
    for rec in self:
        invoice = rec.env['account.move'].create({
            'move_type': 'out_invoice',
            # 'partner_id': self.partner.id, 
            'journal_id': 18, # say u forget to create journal 
            # 'currency_id': self.env.ref('base.USD').id, 
            'payment_reference': 'invoice to client',  

            'invoice_line_ids': [(0, 0, {
                'product_id': self.env['product.product'].create({'name': 'A Test Product'}),
                'quantity': 1,
                'price_unit': 40,
                'name': 'Your project product',
            })],
        })

1,006