0
votes

In purchase requisition form i have added 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')

With this, i have added delivery location field on Purchase Order form. This delivery location will be used for receiving purchased item in different wirehouse location. Functionality is working fine.

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

    delivery_location = fields.Many2one(
        'stock.location', 'Destination', domain=[('usage', 'in',
                                                  ['internal', 'transit'])])  

Problem is, When i click on New Quotation button from Purchase Requisition form it's arrive in Purchase Order[Quotation] form with all other value than delivery_location. So far i know, to populate default value in form odoo use context field in form view. So, I want delivery_location value as well as other form value in Purchase Order [Quotation] form.

1

1 Answers

1
votes

You have to overwrite the _onchange_requisition_id method of purchase.order model this way:

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

Note that I'm only adding the line 'delivery_location': line.delivery_location,, almost at the end of the method, but in this case it would be a mess to use super to modify this. So you'd better overwrite the whole method as I'm doing here.