Created
September 5, 2019 08:15
-
-
Save gurneyalex/8f35bdc97c7b2ffee2b8ebfa430ed626 to your computer and use it in GitHub Desktop.
Odoo logistics load script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# Copyright 2017 Camptocamp SA | |
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) | |
import datetime | |
import random | |
from freezegun import freeze_time | |
import time | |
def create_products(ctx, nb_products=15000): | |
vals = [] | |
for i in range(nb_products): | |
vals.append( | |
{'name': 'product %06d' % i, | |
'default_code': 'p%06d' % i, | |
'type': 'product', | |
'list_price': (i % 1000) * 1.5 + 10, | |
} | |
) | |
ctx.env['product.product'].create( | |
vals | |
) | |
def move_stuff(ctx, nb_days=100): | |
date = datetime.date.today() - datetime.timedelta(days=nb_days) | |
vendor = ctx.env['res.partner'].create( | |
{'name': 'Perf Vendor', 'supplier': 1} | |
) | |
customer = ctx.env['res.partner'].create( | |
{'name': 'Perf Customer'} | |
) | |
for i in range(1, nb_days): | |
t0 = time.time() | |
print(date.strftime("%Y-%m-%d")) | |
products = ctx.env['product.product'].search( | |
[('type', '=', 'product'), ('tracking', '=', 'none'), | |
] | |
).sorted('qty_available')[:100] | |
with freeze_time(date.strftime('%Y-%m-%d 08:00:00')): | |
po = ctx.env['purchase.order'].create( | |
{'partner_id': vendor.id, | |
'order_line': [ | |
(0, 0, {'name': p.name, | |
'product_id': p.id, | |
'product_qty': random.randint(50, 100), | |
'product_uom': p.uom_id.id, | |
'date_planned': date.strftime('%Y-%m-%d 15:00:00'), | |
'price_unit': p.list_price * random.uniform(0.4, 1.1)} | |
) | |
for p in products], | |
} | |
) | |
po.button_confirm() | |
ctx.env.cr.commit() | |
sales = ctx.env['sale.order'] | |
with freeze_time(date.strftime('%Y-%m-%d 12:00:00')): | |
product_ids = ctx.env['product.product'].search( | |
[('type', '=', 'product'), ('tracking', '=', 'none'), | |
#('virtual_available', '>', 0) | |
] | |
).ids | |
for j in range(50): | |
products = ctx.env['product.product'].browse(random.sample(product_ids, 20)) | |
vals = {'partner_id': customer.id, | |
'order_line': [ | |
(0, 0, {'name': p.name, | |
'product_id': p.id, | |
'product_uom_qty': random.randint(1, min(10, p.virtual_available)), | |
'product_uom': 1, | |
'price_unit': p.list_price} | |
) | |
for p in products if p.virtual_available > 0], | |
} | |
if not vals['order_line']: | |
continue | |
so = ctx.env['sale.order'].create(vals) | |
so.action_confirm() | |
ctx.env.cr.commit() | |
sales |= so | |
with freeze_time(date.strftime('%Y-%m-%d 14:00:00')): | |
action_done(po.picking_ids) | |
with freeze_time(date.strftime('%Y-%m-%d 16:00:00')): | |
action_done(sales.mapped('picking_ids')) | |
date += datetime.timedelta(days=1) | |
t1 = time.time() | |
print(' %.2fs' % (t1-t0,)) | |
def action_done(picking): | |
for move in picking.mapped('move_lines'): | |
for move_line in move.move_line_ids: | |
move_line.qty_done = move_line.product_uom_qty | |
picking.action_done() | |
# self.env['product.category'].search([]).write({'property_cost_method': 'average'}) | |
# create_products(self.with_context(tracking_disable=1), 15500) | |
# self.env.cr.commit() | |
move_stuff(self.with_context(tracking_disable=1), 3*365) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment