def fifo_counts(qstring): qs = COGSAssignment.objects.all() qs = COGSAssignmentSerializer.setup_eager_loading(qs) data = COGSAssignmentSerializer(qs, many=True).data order_keys = list(set(row['shipment_label'] for row in data)) item_keys = list(set(row['unit_label'] for row in data)) ship_cnts = inv_api.shipmentcounts({}) arrival_dates = dict((sl['order'], sl['arrival_date']) for sl in ship_cnts) tbl = [] for o in order_keys: row = {'order': o, 'arrival_date': arrival_dates.get(o)} for i in item_keys: row[i] = sum([x['quantity'] for x in data if x['unit_label'] == i and x['shipment_label'] == o]) tbl.append(row) return sorted(tbl, key=lambda x: x['arrival_date'])
def fifo_available(qstring): ship_cnts = inv_api.shipmentcounts({}) fifo_cnts = fifo_counts({}) orders = list(set([x['order'] for x in fifo_cnts] + [x['order'] for x in ship_cnts])) items = list(set(list(itertools.chain.from_iterable([d.keys() for d in ship_cnts])) + list(itertools.chain.from_iterable([d.keys() for d in fifo_cnts])))) items.remove('order') items.remove('arrival_date') arrival_dates = dict((sl['order'], sl['arrival_date']) for sl in ship_cnts) available = [] for order in orders: row = {'order': order, 'arrival_date': arrival_dates.get(order)} ship_cnt = next((x for x in ship_cnts if x['order'] == order), {}) fifo_cnt = next((x for x in fifo_cnts if x['order'] == order), {}) row.update(dict((item, ship_cnt.get(item, 0) - fifo_cnt.get(item, 0)) for item in items)) available.append(row) return sorted(available, key=lambda x: x['arrival_date'])