2022-01-30 12:40:43 +00:00
|
|
|
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,
|
|
|
|
)
|
2022-01-31 08:40:42 +00:00
|
|
|
from erpnext.stock.utils import get_bin
|
2022-01-30 12:40:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
def execute():
|
2022-01-30 12:46:14 +00:00
|
|
|
delete_broken_bins()
|
|
|
|
delete_and_patch_duplicate_bins()
|
2022-01-30 12:40:43 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2022-01-30 12:46:14 +00:00
|
|
|
def delete_broken_bins():
|
|
|
|
# delete useless bins
|
|
|
|
frappe.db.sql("delete from `tabBin` where item_code is null or warehouse is null")
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2022-01-30 12:46:14 +00:00
|
|
|
def delete_and_patch_duplicate_bins():
|
|
|
|
|
|
|
|
duplicate_bins = frappe.db.sql(
|
|
|
|
"""
|
2022-01-30 12:40:43 +00:00
|
|
|
SELECT
|
2022-01-30 12:46:14 +00:00
|
|
|
item_code, warehouse, count(*) as bin_count
|
2022-01-30 12:40:43 +00:00
|
|
|
FROM
|
|
|
|
tabBin
|
|
|
|
GROUP BY
|
|
|
|
item_code, warehouse
|
|
|
|
HAVING
|
2022-01-30 12:46:14 +00:00
|
|
|
bin_count > 1
|
2022-01-30 12:40:43 +00:00
|
|
|
""",
|
|
|
|
as_dict=1,
|
|
|
|
)
|
|
|
|
|
2022-01-30 12:46:14 +00:00
|
|
|
for duplicate_bin in duplicate_bins:
|
2022-01-31 08:40:42 +00:00
|
|
|
item_code = duplicate_bin.item_code
|
|
|
|
warehouse = duplicate_bin.warehouse
|
2022-01-30 12:46:14 +00:00
|
|
|
existing_bins = frappe.get_list(
|
|
|
|
"Bin",
|
2022-01-31 08:40:42 +00:00
|
|
|
filters={"item_code": item_code, "warehouse": warehouse},
|
2022-01-30 12:46:14 +00:00
|
|
|
fields=["name"],
|
|
|
|
order_by="creation",
|
|
|
|
)
|
|
|
|
|
|
|
|
# keep last one
|
|
|
|
existing_bins.pop()
|
2022-01-30 12:40:43 +00:00
|
|
|
|
2022-01-30 12:46:14 +00:00
|
|
|
for broken_bin in existing_bins:
|
|
|
|
frappe.delete_doc("Bin", broken_bin.name)
|
2022-01-30 12:40:43 +00:00
|
|
|
|
|
|
|
qty_dict = {
|
2022-01-31 08:40:42 +00:00
|
|
|
"reserved_qty": get_reserved_qty(item_code, warehouse),
|
|
|
|
"indented_qty": get_indented_qty(item_code, warehouse),
|
|
|
|
"ordered_qty": get_ordered_qty(item_code, warehouse),
|
|
|
|
"planned_qty": get_planned_qty(item_code, warehouse),
|
|
|
|
"actual_qty": get_balance_qty_from_sle(item_code, warehouse),
|
2022-01-30 12:40:43 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 08:40:42 +00:00
|
|
|
bin = get_bin(item_code, warehouse)
|
|
|
|
bin.update(qty_dict)
|
|
|
|
bin.update_reserved_qty_for_production()
|
|
|
|
bin.update_reserved_qty_for_sub_contracting()
|
|
|
|
bin.db_update()
|