Skip to Content
Menu
This question has been flagged
3 Replies
3621 Views

Hello, I am migrating from an Old ERP to Odoo and just imported the previous sales we, in order to keep it all on Odoo. The thing is that all sales are Quotations and now I have to Confirm them all and generate invoices from them in order to keep the data. I made a script that allows me to select the sales I want to to confirm and them loop through them. The code I have is the following:

@api.multi
def confirm_sales(self):
sale_orders = self.env['sale.order'].browse(self._context.get('active_ids', []))
count = 0
for sale_order in sale_orders:
count = count + 1
_logger.info("Sale: %s/%s", count, len(sale_orders))
if sale_order.state == 'sale':
_logger.info("It's sales order already")
continue
sale_order.action_confirm()
self.env.cr.commit()

  The problem is that there over 13K sales to be confirmed and when I try to run it on odoo.sh, each iteration takes a lot time and the script simply stops and no errors are thrown. I suspect this occurs due the number of sales being confirmed, but I am not sure that is it.

Does anyone know a way I can run I can successfully run this script? A fast way would be great =P

Thanks!

Arthur

Avatar
Discard
Best Answer

Hi arthur,

First of all, 13K records will definitely take time. Be patient. 

Secondly,  if you run your script via XMLRPC, cron.. self._context.get('active_ids', []) will be empty as you have no active ids.. You need to call search function to search for records with state = 'draft' or state='sent'. 
These are the records you want to confirm. 

this would be : 

@api.multi
def confirm_sales(self):
    sale_orders = self.env['sale.order'].search([('state','in', ('draft', 'sent'))])
    count = 0
    for sale_order in sale_orders:
        count += 1
        _logger.info("Sale: %s/%s", count, len(sale_orders))
        sale_order.action_confirm()

Applying these changes would decrease probably the number of records that should be updated. 
Avatar
Discard
Best Answer

I had faced a similar problem like yours

Odoo ORM need huge resource,  for example, only for just 1 sale record to execute confirm action, it require dozens of transaction depends on how much order item (sale order line),, even hundreds

if i confirm more than 2000 records the process will be stopped with no errors,, (simmiliar with your problem)

In my case, It stopped caused by the webserver (nginx) has limit for request and waiting response from the server and few more things like buffer size and else that i need to configure up,,
 
So beacause i run out of time,, i decide to make automated action (ir.cron) to execute python code,, and set interval for 10 or 15 minute,

And on python code i set then command to find the outstanding  records (find records wich not executed before) and limited to hundreds (in my case, i set limit 1000 records / cron action to execute)


And 1 more,, you must optimize your code,, for saving times to execute your code,,

For example,  in my opinion why don't you filter the record first,, so no loops on records that no need to be executed...
Example:

sale_orders = self.env['sale.order'].search([('state','!=','sale')], (**another domain))
# then loops after filtering data
for sale_order in sale_orders:
    # Executed here


Hope Helping you, Just share my experience
Regards

Avatar
Discard
Best Answer

Hi arthur,

i think it's happen due to large number of records,

i can give you one hint from that you can do it.

You can add one flag field (Boolean) in sale.order using inheritance, that just confirm that this order confirmed.

in your script you have to update that flag field when your order confirm,

so when you browse all sale order then set domain that only flag=False records are capture.

don't forget to accept answer if it's helpful for you.


Thank you.

Avatar
Discard