private bool calcSimpleReceiptPlan()
        {
            Receipt receipt = Receipts[0];

            receipt.CriticalWeek     = Weeks[NumWeeks - 1].Week;
            receipt.CriticalFraction = 1;

            receipt.NodeReceiptPlans = new Dictionary <Tuple <int, int>, ReceiptComponents>();

            decimal totalSalesPlan = 0;

            foreach (NodeSalesPlan nodeSalesPlan in NodeSalesPlans)
            {
                totalSalesPlan += nodeSalesPlan.TotalNodeSales;
            }
            if (totalSalesPlan > 0)
            {
                foreach (NodeSalesPlan nodeSalesPlan in NodeSalesPlans)
                {
                    ReceiptComponents rc = new ReceiptComponents();
                    rc.Qty        = receipt.Qty * nodeSalesPlan.TotalNodeSales / totalSalesPlan;
                    rc.StoreCount = nodeSalesPlan.Plans[NumWeeks - 1].StoreCount;
                    receipt.NodeReceiptPlans.Add(new Tuple <int, int>(nodeSalesPlan.GradeId, nodeSalesPlan.ClimateId), rc);
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }
        private bool allocate(int qty, Receipt receipt, Dictionary <Tuple <int, int>, decimal> priorAllocation,
                              int earliestStartWeekIndex, decimal[] cumulativeReceiptNeeds, ref int criticalWeekIndex, bool debug = false)
        {
            SqlPipe sqlP = SqlContext.Pipe;
            int     step = 0;

            if (debug)
            {
                sqlP.Send(" allocate - -  Step " + step++);
            }

            decimal criticalFraction;
            bool    success = getCriticalWeekAndFraction(earliestStartWeekIndex, qty, cumulativeReceiptNeeds,
                                                         ref criticalWeekIndex, out criticalFraction);

            if (debug)
            {
                sqlP.Send(" allocate - -  Step " + step++);
            }
            if (success)
            {
                if (receipt != null)
                {
                    receipt.CriticalWeek     = Weeks[criticalWeekIndex].Week;
                    receipt.CriticalFraction = criticalFraction;
                }
                decimal totalQtyAllocated = 0;
                foreach (NodeSalesPlan nodeSalesPlan in NodeSalesPlans)
                {
                    decimal allocation = nodeSalesPlan.Allocation(earliestStartWeekIndex, criticalWeekIndex, criticalFraction);
                    if (allocation > 0)
                    {
                        Tuple <int, int> tuple = new Tuple <int, int>(nodeSalesPlan.GradeId, nodeSalesPlan.ClimateId);
                        decimal          priorAllocatedQty;
                        if (!priorAllocation.TryGetValue(tuple, out priorAllocatedQty))
                        {
                            priorAllocatedQty = 0;
                        }
                        if (allocation > priorAllocatedQty)
                        {
                            if (receipt != null)
                            {
                                ReceiptComponents rc = new ReceiptComponents();
                                rc.Qty        = allocation - priorAllocatedQty;
                                rc.StoreCount = nodeSalesPlan.Plans[criticalWeekIndex].StoreCount;
                                receipt.NodeReceiptPlans.Add(tuple, rc);
                            }
                            priorAllocation[tuple] = allocation;
                            totalQtyAllocated     += allocation;
                        }
                    }
                }
                if (Math.Abs(totalQtyAllocated - qty) > (decimal)0.01)
                {
                    if (debug)
                    {
                        sqlP.Send("Target Allocation Qty = " + qty + ", Allocated Qty = " + totalQtyAllocated);
                    }
                }
            }
            return(success);
        }