Help

0

Create Sale Order linked with Website using Python.

Avatar
Andrzej

I writing a custom Website for making Sale Orders. This website is available to portal users after they login. I have a controller ready for creating Sale Order but it does not work as I would like it to.

This is how I'm creating new orders:

    @http.route('/api/create_order', type='json', auth='user', website=True)
    def create_order(self, **kw):
        uid = http.request.env.context.get('uid')
        partner_id = http.request.env['res.users'].search([('id','=',uid)]).partner_id.id
        
        order_products = kw.get('order_products', [])
        order_line = []

        for product in order_products:
            order_line.append(
                (0, 0, {
                    'product_id': http.request.env['product.product'].search([('product_tmpl_id','=',product['product_id'])])[0].id,
                    'product_uom_qty': product['amount'],
                }))

        order_data = {
            'name': 'Test Sale Order',
            'partner_id': partner_id,
            'order_line': order_line,
        }

        result_insert_record = http.request.env['sale.order'].with_user(SUPERUSER_ID).create(order_data)
        return result_insert_record

As you can see I'm using with_user(SUPERUSER_ID).create(). Because portal user does not have permission to create a Sale Order directly. And when I'm using sudo().create() this portal users is also assigned as Salesperson (as well as a Customer) to his own order.

Also he does not see them inside his website account - and he should. He should also receive an confirmation email after creating such order.

And when someone makes order through the Shop inside the Website there is no Salesperson assigned and instead there is a Website linked with that Sale Order.

So how do I create this Sale Order so that it is linked with the Website, user sees it inside his portal and receives an email after creation? Do I need to pass some special parameter or use different function?

アバター
Discard