Search code examples
odooodoo-10odoo-11

Fill up items from parent sale order to wizard


My goal is to get all items from parent quotation, to the wizard window.

I don't know i do this right way or not, but for now i can get all the products from quotation, and don't understand how to fill them into my wizard items line.

Quotation example

Wizard on that quotation

I remove the pieces of code which not matter.

    from odoo import fields, models, api
    import logging

    class back_to_back_order(models.Model):
        _name = "back.to.back.order"
        _description = "Back to Back Order"

        line_ids =  fields.One2many('back.to.back.order.line','back_order_id', 'Order Lines', required=True)

    def get_items_from_quotation(self, context):
            items = []
            quotation_id = context['id']
            current_quotation = self.env['sale.order'].search([('id','=',quotation_id)])
            if quotation_id == current_quotation.id:
                    for line in current_quotation.order_line:
                            item = {
                                    'product_id': line.product_id,
                                    'qty': line.product_uom_qty,
                                    'price': line.price_unit,
                                    'subtotal': line.price_unit*line.product_uom_qty
                            }
                            items.append(item)


    class back_to_back_order_line(models.Model):
        _name = "back.to.back.order.line"
        _description = "Back to Back Order"


        product_id = fields.Many2one('product.product', 'Product')
        back_order_id = fields.Many2one('back.to.back.order', 'Back Order')
        qty = fields.Float('Quantity')
        price = fields.Float('Unit Price')
        subtotal = fields.Float('Subtotal')

Solution

  • Firstly, if you are creating a wizard, then it's very likely you should be using models.TransientModel and not model.Model for your classes.

    class back_to_back_order(models.TransientModel):
        ...
    

    Wizard records are not meant to be persistent; they are automatically deleted from the database after a certain time. This is why they are called transient.


    You mentioned you are already able to get the Sales Order Lines within your wizard, but you aren't sure how to fill your Back to Back Order Lines with the data.

    One2many and Many2many use a special "commands" format to manipulate the set of records stored in/associated with the field.

    I originally found these commands on another answer but they are also covered in the Documentation.


    As for your specific application, you should be able to simply create your back.to.back.order.line records and they will be linked as long as you provide the back_order_id.

    @api.multi
    def get_items_from_quotation(self, context):
        self.ensure_one()
        b2b_line_obj = self.env['back.to.back.order.line']
        quotation = self.env['sale.order'].browse(context['id'])
        if quotation:
            back_order_id = self.id
            for line in quotation.order_line:
                b2b_line_obj.create({
                    'back_order_id': back_order_id,
                    'product_id': line.product_id.id,
                    'qty': line.product_uom_qty,
                    'price': line.price_unit,
                    'subtotal': line.price_unit * line.product_uom_qty,
                })