[REF] stock: merge quants

In a situation where one transaction is updating a quant via
`_update_available_quantity`and another concurrent one calls
this function with the same argument, we’ll create a new quant
in order for these transactions to not rollback.

This commit adds a method to deduplicate these quants.
This commit is contained in:
amoyaux
2017-09-19 15:55:53 +02:00
parent 8514df36e9
commit d547f63cb7
2 changed files with 39 additions and 1 deletions
+28
View File
@@ -263,6 +263,34 @@ class StockQuant(models.Model):
break
return reserved_quants
@api.model
def _merge_quants(self):
""" In a situation where one transaction is updating a quant via
`_update_available_quantity` and another concurrent one calls this function with the same
argument, we’ll create a new quant in order for these transactions to not rollback. This
method will find and deduplicate these quants.
"""
query = """WITH
dupes AS (
SELECT min(id) as to_update_quant_id,
(array_agg(id ORDER BY id))[2:array_length(array_agg(id), 1)] as to_delete_quant_ids,
SUM(reserved_quantity) as reserved_quantity,
SUM(quantity) as quantity
FROM stock_quant
GROUP BY product_id, company_id, location_id, lot_id, package_id, owner_id, in_date
HAVING count(id) > 1
),
_up AS (
UPDATE stock_quant q
SET quantity = d.quantity,
reserved_quantity = d.reserved_quantity
FROM dupes d
WHERE d.to_update_quant_id = q.id
)
DELETE FROM stock_quant WHERE id in (SELECT unnest(to_delete_quant_ids) from dupes)
"""
self.env.cr.execute(query)
class QuantPackage(models.Model):
""" Packages containing quants and/or other packages """
+11 -1
View File
@@ -139,7 +139,17 @@
<field name="res_model">stock.quant</field>
</record>
<menuitem id="menu_quants" name="Inventory" parent="stock.menu_warehouse_report" sequence="100" action="quantsact"/>
<record model="ir.actions.server" id="quant_merge_action"> <!-- Menu + python -->
<field name="name">Merge duplicate quants</field>
<field name="model_id" ref="stock.model_stock_quant"/>
<field name="state">code</field>
<field name="code">
env['stock.quant']._merge_quants()
action = env.ref('stock.quantsact').read()[0]
</field>
</record>
<menuitem id="menu_quants" name="Inventory" parent="stock.menu_warehouse_report" sequence="100" action="stock.quant_merge_action"/>
<record id="quant_package_search_view" model="ir.ui.view">
<field name="name">stock.quant.package.search</field>