用其他模型形式值odoo 11.0填写表单字段

时间:2018-03-08 10:45:20

标签: python python-3.x odoo odoo-10

在采购申请表中,我添加了@ExtensionMethod

delivery location

有了这个,我在采购订单表单上添加了class PurchaseRequisitionInherit(models.Model): _inherit = "purchase.requisition.line" _description = "Purchase Requisition Line" product_id = fields.Many2one('product.product', string='Product', domain=get_master_purchase_product, required=True) delivery_location = fields.Many2one('stock.location', 'Warehouse Location') 字段。此delivery location将用于接收不同驿站位置的购买物品。功能正常。

delivery location

问题是,当我点击采购申请表单中的class PurchaseOrderLine(models.Model): _inherit = 'purchase.order.line' delivery_location = fields.Many2one( 'stock.location', 'Destination', domain=[('usage', 'in', ['internal', 'transit'])]) 按钮时,它会以所有其他形式到达采购订单 [报价]表单值比New Quotation。 到目前为止,我知道,要在delivery_location视图中使用context字段填写默认值。 所以,我想要采购订单 [报价]表单中的delivery_location值以及其他表单值。

1 个答案:

答案 0 :(得分:1)

您必须以这种方式覆盖_onchange_requisition_id模型的purchase.order方法:

class PurchaseOrder(models.Model):
    _inherit = 'purchase.order'

    @api.onchange('requisition_id')
    def _onchange_requisition_id(self):
        if not self.requisition_id:
            return

        requisition = self.requisition_id
        if self.partner_id:
            partner = self.partner_id
        else:
            partner = requisition.vendor_id
        payment_term = partner.property_supplier_payment_term_id
        currency = partner.property_purchase_currency_id or requisition.company_id.currency_id

        FiscalPosition = self.env['account.fiscal.position']
        fpos = FiscalPosition.get_fiscal_position(partner.id)
        fpos = FiscalPosition.browse(fpos)

        self.partner_id = partner.id
        self.fiscal_position_id = fpos.id
        self.payment_term_id = payment_term.id,
        self.company_id = requisition.company_id.id
        self.currency_id = currency.id
        self.origin = requisition.name
        self.partner_ref = requisition.name # to control vendor bill based on agreement reference
        self.notes = requisition.description
        self.date_order = requisition.date_end or fields.Datetime.now()
        self.picking_type_id = requisition.picking_type_id.id

        if requisition.type_id.line_copy != 'copy':
            return

        # Create PO lines if necessary
        order_lines = []
        for line in requisition.line_ids:
            # Compute name
            product_lang = line.product_id.with_context({
                'lang': partner.lang,
                'partner_id': partner.id,
            })
            name = product_lang.display_name
            if product_lang.description_purchase:
                name += '\n' + product_lang.description_purchase

            # Compute taxes
            if fpos:
                taxes_ids = fpos.map_tax(line.product_id.supplier_taxes_id.filtered(lambda tax: tax.company_id == requisition.company_id)).ids
            else:
                taxes_ids = line.product_id.supplier_taxes_id.filtered(lambda tax: tax.company_id == requisition.company_id).ids

            # Compute quantity and price_unit
            if line.product_uom_id != line.product_id.uom_po_id:
                product_qty = line.product_uom_id._compute_quantity(line.product_qty, line.product_id.uom_po_id)
                price_unit = line.product_uom_id._compute_price(line.price_unit, line.product_id.uom_po_id)
            else:
                product_qty = line.product_qty
                price_unit = line.price_unit

            if requisition.type_id.quantity_copy != 'copy':
                product_qty = 0

            # Compute price_unit in appropriate currency
            if requisition.company_id.currency_id != currency:
                price_unit = requisition.company_id.currency_id.compute(price_unit, currency)

            # Create PO line
            order_lines.append((0, 0, {
                'name': name,
                'product_id': line.product_id.id,
                'product_uom': line.product_id.uom_po_id.id,
                'product_qty': product_qty,
                'price_unit': price_unit,
                'taxes_id': [(6, 0, taxes_ids)],
                'date_planned': requisition.schedule_date or fields.Date.today(),
                'procurement_ids': [(6, 0, [requisition.procurement_id.id])] if requisition.procurement_id else False,
                'account_analytic_id': line.account_analytic_id.id,
                'delivery_location': line.delivery_location,
            }))
        self.order_line = order_lines

请注意,我只是在方法的末尾添加了行'delivery_location': line.delivery_location,,但在这种情况下使用super来修改它会很麻烦。所以你最好像我在这里一样覆盖整个方法。