Example #1
0
        public bool RemoveMatrailofTreatment(int clinecID, int matrailID = 0, int treatmentID = 0)
        {
            bool check;

            MaterialRepository materialRepository = new MaterialRepository();
            int quantity = materialRepository.getQuanityOfMatrailTreatment(matrailID, treatmentID);
            int storgeID = 0;

            StorgeRepository  storgeRepository = new StorgeRepository();
            IEnumerable <int> storgeList       = storgeRepository.getClinecStorgesList(clinecID);

            if (storgeList.Count() > 0)
            {
                storgeID = storgeList.First();

                Warehouse warehouse = new Warehouse()
                {
                    ItemID = matrailID, StorageID = storgeID
                };
                WarehouseRepository warehouseRepository = new WarehouseRepository();
                Warehouse           Getwarehouse        = warehouseRepository.Getwarehouse(warehouse);

                Getwarehouse.Available += quantity;
                warehouseRepository.UpdateWarehouse(Getwarehouse);
            }
            check = materialRepository.removeTreatmentMatrail(matrailID, treatmentID);

            return(check);
        }
Example #2
0
        public bool SaveMatrailOfTreatment(IEnumerable <MatrailToSaveViewModel> matrailList, int treatmentID, int clinecID)
        {
            // substract form the warehouse
            StorgeRepository               storgeRepository   = new StorgeRepository();
            MaterialRepository             materialRepository = new MaterialRepository();
            IEnumerable <int>              ClinecstorgeIDList = storgeRepository.getClinecStorgesList(clinecID);
            List <StorageMatrailViewModel> storageMatrailViewModelList;

            int storgeID          = 0;
            int total             = 0;
            int oldQuantity       = 0;
            int quantityToWisdraw = 0;

            foreach (MatrailToSaveViewModel item in matrailList)
            {
                storageMatrailViewModelList = storgeRepository.getStrogesMatrailsQuantity(ClinecstorgeIDList, item.MatrailID);
                if (storageMatrailViewModelList.Count < 1)
                {
                    return(false); // there is no stoge for this clinec
                }
                // substract the old matrail in case he update the same matrail to avoid doblcate the delete form warehouse
                oldQuantity       = materialRepository.getQuanityOfMatrailTreatment(item.MatrailID, treatmentID);
                quantityToWisdraw = item.Quantity - oldQuantity;


                // test if it's gonna return null
                storgeID = storageMatrailViewModelList.Where(x => x.Quantity > item.Quantity).Select(x => x.StorageID).FirstOrDefault();
                if (storgeID != 0)
                {
                    storgeRepository.withdrawMatrailFromWarehouse(item.MatrailID, storgeID, quantityToWisdraw);
                    continue;
                }

                total = storageMatrailViewModelList.Sum(x => x.Quantity);

                if (total >= quantityToWisdraw)
                {
                    foreach (StorageMatrailViewModel storge in storageMatrailViewModelList)
                    {
                        if (quantityToWisdraw >= storge.Quantity)
                        {
                            storgeRepository.withdrawMatrailFromWarehouse(item.MatrailID, storge.StorageID, storge.Quantity);
                            quantityToWisdraw -= storge.Quantity;
                        }
                        else
                        {
                            storgeRepository.withdrawMatrailFromWarehouse(item.MatrailID, storge.StorageID, quantityToWisdraw);
                            quantityToWisdraw = 0;
                        }
                        if (quantityToWisdraw < 1)
                        {
                            break; //
                        }
                    }
                }
                else
                {
                    // if this run that mean it's will empty every warehouse and still not cover it
                    foreach (StorageMatrailViewModel storge in storageMatrailViewModelList)
                    {
                        storgeRepository.withdrawMatrailFromWarehouse(item.MatrailID, storge.StorageID, storge.Quantity);
                    }

                    // if we go this far this mean no enough matrail  retun false in this one
                    continue;
                }
            }

            bool check = materialRepository.SaveTreatmentMatrail(matrailList, treatmentID);

            return(check);
        }