Skip to Content
Menu
This question has been flagged
2 Replies
19652 Views

I am trying to create a sample order in sales order.In the sample order form, products are sold to customers as complimentary copy(books in my case) without charging any money so I have created a separate sub-menu in sales menu.Here I take only product and quantity as input. Sample order number will be the next number from the sales order(like SO360).So I am fetching sales order number as parent_id in my inherited module.I am not able to create sale order line(data in order lines tab)

    class SaleOrder(models.Model):

    _inherit = 'sale.order'


    is_sample = fields.Boolean(string="Sample Order", default=False)

    parent_id = fields.Many2one('sale.order', string="Parent Sales Order")

    sample_ids = fields.One2many('sale.order', 'parent_id', string="Sample Orders")


    @api.model

    @api.returns('sale.order')

    def create(self, vals):

        if vals.get('is_sample', False) and vals.get('name', '/') == '/':

            IrSeq = self.env['ir.sequence']

            ref = IrSeq.next_by_code('sale.order.sample.ref') or '/'

            parent = self.search([('id', '=', vals.get('parent_id'))])

            vals['name'] = parent.name + ref

            vals['user_id'] = parent.user_id.id


        return super(SaleOrder, self).create(vals)


class SampleOrderWizard(models.TransientModel):

    _name = 'sale.order.sample.wizard'

    _description = 'Sample Sale Order Wizard'


    def _get_parent(self):

        res = False

        if self.env.context \

                and 'active_id' in list(self.env.context.iterkeys()):

            res = self.env.context['active_id']

            

        return res


    def _get_new_sale_line(self, orig_sale, orig_sale_line):

        """Internal function to get the fields of the sale order line. Modules

        enhancing this one should add their own fields to the return value."""


        res = {

            'order_id': orig_sale.id,

            'product_id': orig_sale_line.product.id,

            'name': orig_sale_line.name,

            'sequence': orig_sale_line.sequence,

            'price_unit': orig_sale_line.price_unit,

            'product_uom': orig_sale_line.product_uom.id,

            'product_uom_qty': orig_sale_line.qty or 1,

            'product_uos_qty': orig_sale_line.qty or 1,

        }

        self.env['sale.order.line'].create(res)


        return res


    def _get_order_lines(self, sale):

        res = []

        line_env = self.env['sale.order.sample.wizard.line']

        res = self._get_new_sale_line(sale, line_env)


        for line in sale.order_line:

            wizard_line = False

            for wzline in self.wizard_lines:

                if wzline.product == line.product_id:

                    wizard_line = wzline

                    break


            if wizard_line:

                res.append(

                    (0, 0, self._get_new_sale_line(sale, line, wizard_line))

                )


        return res


    def _get_wizard_lines(self):

        res = []

        if self._get_parent():

            SaleOrder = self.env['sale.order']

            parent = SaleOrder.search([('id', '=', self._get_parent())])

            for line in parent.order_line:

                res.append((0, 0,

                            {

                                'product': line.product_id,

                                'qty': 1,

                            }))

        return res


   


    @api.one

    def create_order(self):


        sale_vals = {

            'user_id': self.order.user_id.id,

            'partner_id': self.order.partner_id.id,

            'parent_id': self.order.id,

            'date_order': self.order_date,

            'client_order_ref': self.order.client_order_ref,

            'company_id': self.order.company_id.id,

            'is_sample': True,

            'order_line': self._get_order_lines(self.order)

        }

        self.env['sale.order'].create(sale_vals)


        return {'type': 'ir.actions.act_window_close'}


    order = fields.Many2one('sale.order', default=_get_parent, readonly=True)

    wizard_lines = fields.One2many('sale.order.sample.wizard.line', 'wizard', default=_get_wizard_lines)

    order_date = fields.Datetime(default=fields.Datetime.now())



class SampleOrderWizardLine(models.TransientModel):


    _name = 'sale.order.sample.wizard.line'

    _description = 'Sample Order Wizard Line'


    wizard = fields.Many2one('sale.order.sample.wizard')

    product = fields.Many2one('product.product',

                              domain=[('sale_ok', '=', True)])

    qty = fields.Float(string="Quantity", default=1.0, digits_compute=dp.get_precision('Product UoS'))

Avatar
Discard
Best Answer

Existing order lines is a One2many relation to model sale.order.line, you can inherit it if you need to alter it
Or if you are trying to show another tree view below it you can add a One2many relation to that model in sale.order and from xml inherit sale order form view and add your one2many field

Avatar
Discard
Best Answer

\https://apps.odoo.com/apps/modules/13.0/pos_test_orders/

Avatar
Discard
Related Posts Replies Views Activity
1
Nov 22
1808
0
Jun 21
2827
0
Mar 21
1548
2
May 18
2884
3
May 18
4116