1
votes

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?

1

1 Answers

0
votes

It turns out that there is no need for linking Sale Order with a Website.

First of all you have to make sure that Portal Users have correct access rights given. Without eCommerce module installed Portal Users by default won't have access to for example products. You need to write your own access rights or install website_sale module.

Also users won't be able to see orders that are in the Draft state. Either sending them or confirming will let users to see them in their documents.

And lastly if you want you can link order to a website by setting a website_id field:

order_data = {
    ...
    'website_id': http.request.website.id,
}

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

But it does not matter in the context of this question.