Exemplo n.º 1
0
        public AllocatedModelCollection getAllAllocated()
        // Now Allocation logic runs here
        {
            //{ PENDING, APPROVED, REJECTED, DISBURSED, PART_DISBURSED, CANCELLED, UPDATED });
            List <Request> efRequests = context.Requests
                                        .Where(x => x.current_status == RequestStatus.APPROVED ||
                                               x.current_status == RequestStatus.PART_DISBURSED
                                               ).ToList();

            if (efRequests.Count == 0)
            {
                //throw new ItemNotFoundException("No records exist");
                return(new AllocatedModelCollection(new List <AllocatedModel>()));
            }

            List <AllocatedModel> results = new List <AllocatedModel>();

            foreach (var efRequest in efRequests)
            {
                AllocatedModel alloc = findLatestAllocatedByRequestId(efRequest.request_id);
                if (alloc == null)
                {
                    continue;                // SKIP
                }
                results.Add(alloc);
            }

            return(new AllocatedModelCollection(results));
        }
Exemplo n.º 2
0
        public AllocatedModelCollection getAllAllocatedFromDepartment(string deptCode)
        {
            //{ PENDING, APPROVED, REJECTED, DISBURSED, PART_DISBURSED, CANCELLED, UPDATED });
            List <Request> efRequests = context.Requests
                                        .Where(x =>
                                               (x.current_status == RequestStatus.APPROVED ||
                                                x.current_status == RequestStatus.PART_DISBURSED) &&
                                               x.dept_code == deptCode
                                               ).ToList();

            if (efRequests.Count == 0)
            {
                //throw new ItemNotFoundException();
                return(new AllocatedModelCollection(new List <AllocatedModel>()));
            }

            List <AllocatedModel> results = new List <AllocatedModel>();

            foreach (var efRequest in efRequests)
            {
                AllocatedModel alloc = findLatestAllocatedByRequestId(efRequest.request_id);
                if (alloc == null)
                {
                    continue;                // SKIP
                }
                results.Add(alloc);
            }

            return(new AllocatedModelCollection(results));
        }
Exemplo n.º 3
0
        public AllocatedModel findLatestAllocatedByRequestId(int requestId)
        {
            // This NOW ALLOCATES UPON FINDING
            // Get all allocated: depending on:
            // Determine there's any latest allocation
            // Find the difference with the previous allocation, to see how much to fulfill

            //{ PENDING, APPROVED, REJECTED, DISBURSED, PART_DISBURSED, CANCELLED, UPDATED });
            Request efRequest = context.Requests
                                .Where(x => x.request_id == requestId &&
                                       (x.current_status == RequestStatus.APPROVED ||
                                        x.current_status == RequestStatus.PART_DISBURSED)
                                       ).First();

            if (efRequest == null)
            {
                throw new ItemNotFoundException("No records exist");
            }

            IEnumerable <IGrouping <string, Request_Event> > events = efRequest.Request_Details.SelectMany(x => x.Request_Event).GroupBy(g => g.Request_Details.item_code).ToList();

            Dictionary <ItemModel, int> itemsToFulfill = new Dictionary <ItemModel, int>();

            foreach (IGrouping <string, Request_Event> eventItem in events)
            {
                Request_Event item = eventItem.Where(w => w.deleted != "Y").DefaultIfEmpty(null).FirstOrDefault();

                if (item == null)
                {
                    continue;
                }

                // If neither approved nor disbursed, skip it.
                if (item.status != EventStatus.APPROVED &&
                    item.status != EventStatus.DISBURSED &&
                    item.status != EventStatus.ALLOCATED)
                {
                    continue;
                }

                Stock_Inventory inv       = context.Stock_Inventory.Find(eventItem.Key);
                ItemModel       itemModel = new ItemModel(inv);

                // Already allocated, just add it
                if (item.status == EventStatus.ALLOCATED && item.allocated.HasValue)
                {
                    itemsToFulfill.Add(itemModel, item.allocated.Value);
                    continue;
                }

                // Fully allocated, can skip
                if (item.status == EventStatus.DISBURSED && item.not_allocated == 0)
                {
                    continue;
                }

                int qtyToAllocate;

                if (item.status == EventStatus.APPROVED)
                {
                    // Take the whole quantity
                    qtyToAllocate = item.quantity;
                }
                else
                {
                    // Disbursed. Take just the not-allocated
                    qtyToAllocate = item.not_allocated.HasValue ? item.not_allocated.Value : 0;
                }

                // if for some reason it's still zero, just skip.
                if (qtyToAllocate == 0)
                {
                    continue;
                }


                int canAllocateQty = 0;
                int availableQty   = itemModel.AvailableQuantity;

                if (availableQty > 0)
                {
                    // There is available stock
                    if (availableQty >= qtyToAllocate)
                    {
                        // Lots of stock
                        canAllocateQty = qtyToAllocate;
                    }
                    else
                    {
                        // Only some stock
                        canAllocateQty = availableQty;
                    }
                }
                else
                {
                    // Cannot allocate at all, cannot retrieve, SKIP
                    continue;
                }

                // At this point, there is some qtyToAllocate
                context.Request_Event.Find(item.request_event_id).allocated = canAllocateQty;

                if (item.status == EventStatus.DISBURSED)
                {
                    context.Request_Event.Find(item.request_event_id).quantity       = qtyToAllocate;
                    context.Request_Event.Find(item.request_event_id).not_allocated -= canAllocateQty;
                }
                else
                {
                    // Approved only
                    // it.quantity doesn't change
                    //context.Request_Event.Find(item.request_event_id).quantity = //doesn't change;
                    context.Request_Event.Find(item.request_event_id).not_allocated = item.quantity - canAllocateQty;
                }
                context.Request_Event.Find(item.request_event_id).status = EventStatus.ALLOCATED;

                itemsToFulfill.Add(itemModel, canAllocateQty);
            }

            if (itemsToFulfill.Count == 0)
            {
                // Nothing to allocate, or cannot allocate anything at all.
                return(null);
            }

            AllocatedModel alloc = new AllocatedModel(efRequest, itemsToFulfill);

            // Save changes to update Available Qty
            context.SaveChanges();
            return(alloc);
        }
Exemplo n.º 4
0
        public AllocatedModel findLatestAllocatedByRequestId(int requestId)
        {
            // Get all allocated: depending on:
            // Determine there's any latest allocation
            // Find the difference with the previous allocation, to see how much to fulfill

            //{ PENDING, APPROVED, REJECTED, DISBURSED, PART_DISBURSED, CANCELLED, UPDATED });
            Request efRequest = context.Requests
                                .Where(x => x.request_id == requestId &&
                                       (x.current_status == RequestStatus.APPROVED ||
                                        x.current_status == RequestStatus.PART_DISBURSED)
                                       ).First();

            if (efRequest == null)
            {
                throw new ItemNotFoundException("No records exist");
            }

            IEnumerable <IGrouping <string, Request_Event> > events = efRequest.Request_Details.SelectMany(x => x.Request_Event).GroupBy(g => g.Request_Details.item_code).ToList();

            Dictionary <ItemModel, int> itemsToFulfill = new Dictionary <ItemModel, int>();

            foreach (IGrouping <string, Request_Event> eventItem in events)
            {
                // Grouping:
                // A101
                // - Approved, 10
                // - Allocated, 9
                // A102
                // - Approved, 10
                List <Request_Event> latestAlloc = eventItem.Where(x => x.status == EventStatus.ALLOCATED).OrderBy(o => o.date_time).ToList();
                if (latestAlloc.Count == 0)
                {
                    continue;
                }

                // Get the latest allocated status's quantity
                int quantityToFulfil = latestAlloc.Last().quantity;

                //int quantityToFulfil = 0;

                //if (latestAlloc.Count > 1)
                //{
                //    Request_Event last = latestAlloc.Last();
                //    Request_Event secondLast = latestAlloc[latestAlloc.Count - 2];

                //    quantityToFulfil = last.quantity - secondLast.quantity;
                //} else
                //{
                //    quantityToFulfil = latestAlloc.Last().quantity;
                //}
                if (quantityToFulfil > 0)
                {
                    Stock_Inventory inv = context.Stock_Inventory.Find(eventItem.Key);
                    itemsToFulfill.Add(new ItemModel(inv), quantityToFulfil);
                }
            }

            if (itemsToFulfill.Count == 0)
            {
                return(null);
            }

            AllocatedModel alloc = new AllocatedModel(efRequest, itemsToFulfill);

            return(alloc);
        }