Last active
October 18, 2021 09:53
-
-
Save gurneyalex/d9d2332d075900162e7af2466674f68f to your computer and use it in GitHub Desktop.
odoo issue #62139 - scheduled action to create inventory
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
""" | |
scheduled action 1 | |
""" | |
log('Cron: started fix stock level') | |
query = """ | |
WITH product AS | |
( | |
select distinct product_id from stock_move where state='done' and create_date > '2020-11-14 00:00:00' | |
), | |
location AS (SELECT id AS location_id, usage FROM stock_location), | |
error_move_lines AS (SELECT * FROM ( | |
SELECT sml.id, | |
ROW_NUMBER() OVER (PARTITION BY move_id) as row, | |
sml.qty_done | |
FROM stock_move_line sml | |
JOIN stock_move sm | |
ON sm.id = sml.move_id | |
WHERE sm.inventory_id IS NOT NULL | |
AND sm.state = 'done' | |
AND sm.product_uom_qty < ( | |
SELECT SUM(qty_done) | |
FROM stock_move_line sml2 | |
WHERE sml2.move_id = sm.id | |
GROUP BY sml2.move_id | |
) | |
) duplicates | |
WHERE duplicates.row > 1) | |
SELECT product_id, location_dest_id AS location_id, usage, package_id, result_package_id as result_package_id, SUM(qty_done) AS qty | |
FROM stock_move_line sml natural join product join location on (location_dest_id = location.location_id) | |
WHERE state = 'done' and sml.id not in (select id from error_move_lines) | |
GROUP BY product_id, location_dest_id, usage, package_id, result_package_id | |
UNION ALL | |
SELECT product_id, sml.location_id, usage, package_id, result_package_id, -SUM(qty_done) AS qty | |
FROM stock_move_line sml natural join product join location on (sml.location_id = location.location_id) | |
WHERE state = 'done' and sml.id not in (select id from error_move_lines) | |
GROUP BY product_id, sml.location_id, usage, package_id, result_package_id | |
""" | |
env.cr.execute(query) | |
quantities = {} | |
for product_id, location_id, usage, package_id, result_package_id, qty in env.cr.fetchall(): | |
if qty < 0: # when we remove, we may put in a pack | |
key = product_id, location_id, usage, package_id | |
else: | |
key = product_id, location_id, usage, result_package_id | |
if key not in quantities: | |
quantities[key] = 0. | |
quantities[key] += qty | |
inventory = env['stock.inventory'].create({'name': 'Fix stock cron %s' % datetime.datetime.now(), | |
'filter': 'partial', | |
'state': 'confirm',}) | |
inventory_lines = [] | |
for (product_id, location_id, usage, package_id), qty in quantities.items(): | |
if usage != 'internal': | |
continue | |
product = env['product.product'].browse(product_id) | |
package = env['stock.quant.package'].browse(package_id) | |
location = env['stock.location'].browse(location_id) | |
quants = env['stock.quant']._gather(product, location, package_id=package, strict=True) | |
actual_qty = sum(quants.mapped('quantity')) | |
delta = qty - actual_qty | |
if delta > 0: | |
#env['stock.quant']._update_available_quantity(product, location, delta, package_id=package) | |
vals = { | |
'inventory_id': inventory.id, | |
'product_id': product_id, | |
'package_id': package_id, | |
'location_id': location_id, | |
'product_uom_id': product.uom_id.id, | |
'product_qty': qty, | |
} | |
inventory_lines.append(vals) | |
#log("Product %s in location %s (package: %s): updated qty from %.1f to %.1f" % (product.display_name, location.display_name, package.name, actual_qty, qty)) | |
if inventory_lines: | |
env['stock.inventory.line'].create(inventory_lines) | |
else: | |
inventory.unlink() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment