Skip to content

Commit

Permalink
fix(patch): patch duplicate bins
Browse files Browse the repository at this point in the history
  • Loading branch information
s-aga-r authored and ankush committed Jan 30, 2022
1 parent a1e7771 commit 7ff3ca2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions erpnext/patches.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[pre_model_sync]
erpnext.patches.v12_0.update_is_cancelled_field
erpnext.patches.v13_0.add_bin_unique_constraint
erpnext.patches.v11_0.rename_production_order_to_work_order
erpnext.patches.v11_0.refactor_naming_series
erpnext.patches.v11_0.refactor_autoname_naming
Expand Down
45 changes: 45 additions & 0 deletions erpnext/patches/v13_0/add_bin_unique_constraint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import frappe

from erpnext.stock.stock_balance import (
get_balance_qty_from_sle,
get_indented_qty,
get_ordered_qty,
get_planned_qty,
get_reserved_qty,
update_bin_qty,
)


def execute():

duplicate_rows = frappe.db.sql("""
SELECT
item_code, warehouse
FROM
tabBin
GROUP BY
item_code, warehouse
HAVING
COUNT(*) > 1
""", as_dict=1)

for row in duplicate_rows:
bins = frappe.get_list("Bin",
filters={"item_code": row.item_code,
"warehouse": row.warehouse},
fields=["name"],
order_by="creation",
)

for x in range(len(bins) - 1):
frappe.delete_doc("Bin", bins[x].name)

qty_dict = {
"reserved_qty": get_reserved_qty(row.item_code, row.warehouse),
"indented_qty": get_indented_qty(row.item_code, row.warehouse),
"ordered_qty": get_ordered_qty(row.item_code, row.warehouse),
"planned_qty": get_planned_qty(row.item_code, row.warehouse),
"actual_qty": get_balance_qty_from_sle(row.item_code, row.warehouse)
}

update_bin_qty(row.item_code, row.warehouse, qty_dict)

0 comments on commit 7ff3ca2

Please sign in to comment.