refactor: use defaultdict and enumeration

This commit is contained in:
Ankush Menat 2021-10-27 10:36:06 +05:30 committed by Ankush Menat
parent f2d136e574
commit 6942900555

View File

@ -2,10 +2,8 @@
# Copyright (c) 2019, Frappe Technologies Pvt. Ltd. and contributors
# For license information, please see license.txt
from __future__ import unicode_literals
import json
from collections import OrderedDict
from collections import OrderedDict, defaultdict
import frappe
from frappe import _
@ -126,28 +124,30 @@ class PickList(Document):
self.group_similar_items()
def group_similar_items(self):
group_item_qty = {}
group_picked_qty = {}
count = 0
group_item_qty = defaultdict(float)
group_picked_qty = defaultdict(float)
for item in self.locations:
group_item_qty[(item.item_code, item.warehouse)] = group_item_qty.get((item.item_code,item.warehouse), 0) + item.qty
group_picked_qty[(item.item_code, item.warehouse)] = group_picked_qty.get((item.item_code,item.warehouse), 0) + item.picked_qty
group_item_qty[(item.item_code, item.warehouse)] += item.qty
group_picked_qty[(item.item_code, item.warehouse)] += item.picked_qty
duplicate_list = []
for item in self.locations:
if (item.item_code, item.warehouse) in group_item_qty:
count += 1
item.qty = group_item_qty[(item.item_code, item.warehouse)]
item.picked_qty = group_picked_qty[(item.item_code, item.warehouse)]
item.stock_qty = group_item_qty[(item.item_code, item.warehouse)]
item.idx = count
del group_item_qty[(item.item_code, item.warehouse)]
else:
duplicate_list.append(item)
for item in duplicate_list:
self.remove(item)
for idx, item in enumerate(self.locations, start=1):
item.idx = idx
def validate_item_locations(pick_list):
if not pick_list.locations:
frappe.throw(_("Add items in the Item Locations table"))