Esempio n. 1
0
        private IEnumerable <SupplyInventoryListModel> GetComputedList()
        {
            try
            {
                using (var uow = new UnitOfWork(new DataContext()))
                {
                    var models        = new List <SupplyInventoryListModel>();
                    var objsMilk      = new List <MilkCollection>();
                    var objsInventory = uow.SupplyInventories.GetAllRecords().ToList();

                    //Get recorded months from inventory records to identify where the computation start
                    var months      = objsInventory.GroupBy(r => r.ActualDate.Month).Select(r => new { Date = r.FirstOrDefault().ActualDate }).OrderBy(r => r.Date);
                    var supplyTypes = uow.SupplyTypes.GetAllRecords().ToList();


                    foreach (var type in supplyTypes)
                    {
                        // store quantity total after loop
                        var beginningQuantity = 0.0;
                        var endingQuantity    = 0.0;

                        foreach (var month in months)
                        {
                            // beginning will same as ending;
                            beginningQuantity = endingQuantity;

                            var model           = new SupplyInventoryListModel();
                            var supplyInventory = objsInventory.FirstOrDefault(r => r.ActualDate.Month == month.Date.Month &&
                                                                               r.ActualDate.Year == month.Date.Year &&
                                                                               r.SupplyTypeID == type.SupplyTypeID);
                            if (supplyInventory != null)
                            {
                                // goes here if found previous inventory
                                model.ID          = supplyInventory.SupplyInventoryID;
                                model.Description = type.Description;
                                model.Date        = supplyInventory.ActualDate;
                                model.UnitPrice   = supplyInventory.SupplyType.UnitPrice;

                                model.BeginningQuantity = beginningQuantity;
                                model.BeginningTotal    = beginningQuantity * model.UnitPrice;

                                model.PurchaseQuantity = supplyInventory.PurchaseQuantity;
                                model.PurchaseTotal    = supplyInventory.PurchaseQuantity * model.UnitPrice;


                                model.WithdrawQuantity = supplyInventory.WithdrawQuantity;
                                model.WithdrawTotal    = supplyInventory.WithdrawQuantity * model.UnitPrice;

                                model.EndingQuantity = model.BeginningQuantity + model.PurchaseQuantity - model.WithdrawQuantity;
                                model.EndingTotal    = model.EndingQuantity * model.UnitPrice;

                                endingQuantity = model.EndingQuantity;

                                models.Add(model);
                            }
                            else
                            {
                                // goes here if previous inventory not found, starting of record loop
                                model.Description = type.Description;
                                model.Date        = month.Date;
                                model.UnitPrice   = type.UnitPrice;
                                // just compute for beginning and ending quantity to avoid confusion even record cant found.
                                model.BeginningQuantity = beginningQuantity;
                                model.BeginningTotal    = beginningQuantity * model.UnitPrice;

                                model.EndingQuantity = model.BeginningQuantity + model.PurchaseQuantity - model.WithdrawQuantity;
                                model.EndingTotal    = model.EndingQuantity * model.UnitPrice;

                                models.Add(model);
                            }
                        }
                    }

                    return(models);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Esempio n. 2
0
        public IEnumerable <SupplyInventoryListModel> GetComputedByMonth(DateTime dateTime, string criteria)
        {
            /**
             * Loop for every supply types
             * Inside current supply types, loop every months
             * inside active month, get supply inventory by using current supply type and current month
             * if value found, get endingquantity value then set as beginning quantity
             * compute for new ending quantity by beginning quantity + purchase quantity - widthdraw quantity
             * if  supply type for current month not found in inventory, just set previous ending quantiy as beginning quantity
             * no computation for purchase and withdraw
             *
             */

            using (var uow = new UnitOfWork(new DataContext()))
            {
                var models             = new List <SupplyInventoryListModel>();
                var objsMilk           = new List <MilkCollection>();
                var objsInventory      = uow.SupplyInventories.GetAllRecords().ToList();
                var objMilkCollections = uow.MilkCollections.GetAllRecords().ToList();
                //Get recorded months from inventory records to identify where the computation start
                var supplyMonths = uow.MilkCollections.GetAll().GroupBy(r => r.ActualDate.Month).Select(r => new { Date = r.FirstOrDefault().ActualDate }).OrderBy(r => r.Date).ToList();
                var supplyTypes  = uow.SupplyTypes.GetAllRecords().ToList();



                foreach (var type in supplyTypes)
                {
                    // store quantity total after loop
                    var beginningQuantity = 0.0;
                    var endingQuantity    = 0.0;

                    foreach (var sMonth in supplyMonths)
                    {
                        // beginning will same as ending;
                        beginningQuantity = endingQuantity;

                        var model           = new SupplyInventoryListModel();
                        var supplyInventory = objsInventory.FirstOrDefault(r => r.ActualDate.Month == sMonth.Date.Month &&
                                                                           r.ActualDate.Year == sMonth.Date.Year &&
                                                                           r.SupplyTypeID == type.SupplyTypeID);
                        if (supplyInventory != null)
                        {
                            // goes here if a supplyInventory found on current supplytype and current month
                            model.ID                     = supplyInventory.SupplyInventoryID;
                            model.Description            = type.SupplyClass.Description + " - " + type.Description;
                            model.SupplyClassDescription = type.SupplyClass.Description;
                            model.Date                   = supplyInventory.ActualDate;
                            model.UnitPrice              = supplyInventory.SupplyType.UnitPrice;

                            model.BeginningQuantity = beginningQuantity;
                            model.BeginningTotal    = beginningQuantity * model.UnitPrice;

                            // get purchase from milk collections for raw milk
                            if (type.SupplyClass.Description == "Raw Milk")
                            {
                                // get total milk purchase from farmers in milk collections
                                var totalRawMilk = uow.MilkCollections.GetAllByMonthV2(sMonth.Date, supplyTypeID: type.SupplyTypeID).Sum(x => x.Volume);
                                model.PurchaseQuantity = totalRawMilk;
                                model.PurchaseTotal    = model.PurchaseQuantity * model.UnitPrice;
                            }
                            else
                            {
                                // get purchase from supply inventory for non milk supplies
                                model.PurchaseQuantity = supplyInventory.PurchaseQuantity;
                                model.PurchaseTotal    = model.PurchaseQuantity * model.UnitPrice;
                            }


                            //Get Withdraw from production/sales using current supply type, current month and year
                            model.WithdrawQuantity = GetWithdrawV2(type.SupplyTypeID, sMonth.Date.Month, sMonth.Date.Year);
                            model.WithdrawTotal    = model.WithdrawQuantity * model.UnitPrice;

                            model.EndingQuantity = model.BeginningQuantity + model.PurchaseQuantity - model.WithdrawQuantity;
                            model.EndingTotal    = model.EndingQuantity * model.UnitPrice;

                            // set ending quantity to get the beginning of quantity for the next month. this is important!
                            endingQuantity = model.EndingQuantity;

                            models.Add(model);
                        }
                        else
                        {
                            //initial values
                            // goes here if a supplyInventory not found on current supply type and current month
                            model.Description            = type.SupplyClass.Description + " - " + type.Description;
                            model.SupplyClassDescription = type.SupplyClass.Description;
                            model.SupplyTypeID           = type.SupplyTypeID;
                            model.SupplyClassID          = type.SupplyClassID;
                            model.Date      = sMonth.Date;
                            model.UnitPrice = type.UnitPrice;
                            // just compute for beginning and ending quantity to avoid confusion even record cant found.
                            model.BeginningQuantity = beginningQuantity;
                            model.BeginningTotal    = beginningQuantity * model.UnitPrice;

                            // get purchase from milk collections for raw milk
                            if (type.SupplyClass.Description == "Raw Milk")
                            {
                                // get total milk purchase from milk collections
                                var totalRawMilk = uow.MilkCollections.GetAllByMonthV2(sMonth.Date, supplyTypeID: type.SupplyTypeID).Sum(x => x.Volume);
                                model.PurchaseQuantity = totalRawMilk;
                                model.PurchaseTotal    = model.PurchaseQuantity * model.UnitPrice;
                            }
                            else
                            {
                                // get purchase from supply inventory for non milk supplies
                                model.PurchaseQuantity = 0;
                                model.PurchaseTotal    = model.PurchaseQuantity * model.UnitPrice;
                            }

                            //Get Withdraw from production/sales using current supply type, current month and year
                            model.WithdrawQuantity = GetWithdrawV2(type.SupplyTypeID, sMonth.Date.Month, sMonth.Date.Year);
                            model.WithdrawTotal    = model.WithdrawQuantity * model.UnitPrice;

                            model.EndingQuantity = model.BeginningQuantity + model.PurchaseQuantity - model.WithdrawQuantity;
                            model.EndingTotal    = model.EndingQuantity * model.UnitPrice;

                            // set ending quantity to get the beginning of quantity for the next month. this is important!
                            endingQuantity = model.EndingQuantity;

                            models.Add(model);
                        }
                    }
                }

                return(models.Where(r => r.Date.Month == dateTime.Month && r.Date.Year == dateTime.Year && r.Description.Contains(criteria)));
            }
        }