def producer_plans_table(from_date, to_date, producer): plans = ProductPlan.objects.filter(member=producer) rows = {} for plan in plans: wkdate = from_date product = plan.product.supply_demand_product() row = [] while wkdate <= to_date: row.append(Decimal("0")) wkdate = wkdate + datetime.timedelta(days=7) row.insert(0, product) rows.setdefault(product, row) wkdate = from_date week = 0 while wkdate <= to_date: if plan.from_date <= wkdate and plan.to_date >= wkdate: rows[product][week + 1] += plan.quantity wkdate = wkdate + datetime.timedelta(days=7) week += 1 label = "Product/Weeks" columns = [label] wkdate = from_date while wkdate <= to_date: columns.append(wkdate) wkdate = wkdate + datetime.timedelta(days=7) rows = rows.values() rows.sort(lambda x, y: cmp(x[0].short_name, y[0].short_name)) sdtable = SupplyDemandTable(columns, rows) return sdtable
def supply_demand_table(from_date, to_date, member): plans = ProductPlan.objects.all() cps = ProducerProduct.objects.filter( inventoried=False, default_avail_qty__gt=0, ) constants = {} for cp in cps: constants.setdefault(cp.product, Decimal("0")) constants[cp.product] += cp.default_avail_qty pps = ProducerProduct.objects.filter( producer=member).values_list("product_id") pps = set(id[0] for id in pps) rows = {} for plan in plans: wkdate = from_date product = plan.product.supply_demand_product() if product.id in pps: constant = Decimal('0') cp = constants.get(product) if cp: constant = cp row = [] while wkdate <= to_date: row.append(constant) wkdate = wkdate + datetime.timedelta(days=7) row.insert(0, product) rows.setdefault(product, row) wkdate = from_date week = 0 while wkdate <= to_date: if plan.from_date <= wkdate and plan.to_date >= wkdate: if plan.role == "producer": rows[product][week + 1] += plan.quantity else: rows[product][week + 1] -= plan.quantity wkdate = wkdate + datetime.timedelta(days=7) week += 1 label = "Product/Weeks" columns = [label] wkdate = from_date while wkdate <= to_date: columns.append(wkdate) wkdate = wkdate + datetime.timedelta(days=7) rows = rows.values() rows.sort(lambda x, y: cmp(x[0].short_name, y[0].short_name)) sdtable = SupplyDemandTable(columns, rows) return sdtable
def producer_suppliable_demand(from_date, to_date, producer): customer_plans = ProductPlan.objects.filter(role="consumer") producer_plans = ProductPlan.objects.filter(member=producer) rows = {} for plan in producer_plans: wkdate = from_date row = [] while wkdate <= to_date: row.append(SuppliableDemandCell(Decimal("0"), Decimal("0"))) wkdate = wkdate + datetime.timedelta(days=7) product = plan.product.supply_demand_product() row.insert(0, product) rows.setdefault(product, row) wkdate = from_date week = 0 while wkdate <= to_date: if plan.from_date <= wkdate and plan.to_date >= wkdate: rows[product][week + 1].supply += plan.quantity wkdate = wkdate + datetime.timedelta(days=7) week += 1 pps = ProducerProduct.objects.filter( producer=producer).values_list("product_id") pps = set(id[0] for id in pps) for plan in customer_plans: wkdate = from_date product = plan.product.supply_demand_product() if product.id in pps: week = 0 while wkdate <= to_date: if plan.from_date <= wkdate and plan.to_date >= wkdate: rows[product][week + 1].demand += plan.quantity wkdate = wkdate + datetime.timedelta(days=7) week += 1 rows = rows.values() fee = producer_fee() for row in rows: for x in range(1, len(row)): sd = row[x].suppliable() if sd >= 0: income = sd * row[0].price row[x] = income - (income * fee) else: row[x] = Decimal("0") income_rows = [] for row in rows: total = Decimal("0") for x in range(1, len(row)): total += row[x] row[x] = row[x].quantize(Decimal('.1'), rounding=ROUND_UP) if total: row.append(total.quantize(Decimal('1.'), rounding=ROUND_UP)) income_rows.append(row) label = "Item\Weeks" columns = [label] wkdate = from_date while wkdate <= to_date: columns.append(wkdate) wkdate = wkdate + datetime.timedelta(days=7) columns.append("Total") income_rows.sort(lambda x, y: cmp(x[0].long_name, y[0].short_name)) sdtable = SupplyDemandTable(columns, income_rows) return sdtable