コード例 #1
0
ファイル: ReceiveController.cs プロジェクト: robela/cats
        public virtual ActionResult _ReceivePartial(string grnNo)
        {
            var commodities = _commodityService.GetAllCommodity().DefaultIfEmpty().OrderBy(o => o.Name).ToList();
            var commodityGrades = _commodityGradeService.GetAllCommodityGrade().DefaultIfEmpty().OrderBy(o => o.Name).ToList();
            var transporters = _transporterService.GetAllTransporter().DefaultIfEmpty().OrderBy(o => o.Name).ToList();
            var commoditySources = _commoditySourceService.GetAllCommoditySource().DefaultIfEmpty().OrderBy(o => o.Name).ToList();
            var commodityTypes = _commodityTypeService.GetAllCommodityType().DefaultIfEmpty().OrderBy(o => o.Name).ToList();

            var programs = _programService.GetAllProgram().DefaultIfEmpty().OrderBy(o => o.Name).ToList();
            var donors = _donorService.GetAllDonor().DefaultIfEmpty().OrderBy(o => o.Name).ToList();
            var units = _unitService.GetAllUnit().OrderBy(o => o.Name).ToList();

            UserProfile user = _userProfileService.GetUser(User.Identity.Name);
            var hubs = _hubService.GetAllWithoutId(user.DefaultHub.HubID).DefaultIfEmpty().OrderBy(o => o.Name).ToList();

            Receive receive = Receive.GetReceiveByGRN(grnNo);
            if (receive != null && (receive.HubID == user.DefaultHub.HubID))
            {
                return PartialView("_ReceivePartial", ReceiveViewModel.GenerateReceiveModel(receive, commodities, commodityGrades, transporters, commodityTypes, commoditySources, programs, donors, hubs, user, units));
            }
            else if (receive != null && (receive.HubID != user.DefaultHub.HubID))
            {
                ViewBag.Message = string.Format("The selected GRN Number {0} doesn't exist in your default warehouse. Try changing your default warehouse.", grnNo);
            }

            ViewBag.Stacks = new SelectList(Enumerable.Empty<SelectListItem>());
            List<ReceiveDetailViewModel> ReceiveCommodities = new List<ReceiveDetailViewModel>();
            ViewBag.ReceiveCommodities = ReceiveCommodities;
            var viewmode = new ReceiveViewModel(commodities, commodityGrades, transporters, commodityTypes, commoditySources, programs, donors, hubs, user, units);
            // viewmode.GRN = grnNo;
            return PartialView("_ReceivePartial", viewmode);
        }
コード例 #2
0
ファイル: ReceiveController.cs プロジェクト: robela/cats
        //
        // GET: /Receive/Create
        public virtual ActionResult Create(string receiveId)
        {
            UserProfile user = _userProfileService.GetUser(User.Identity.Name);

            var commodities = _commodityService.GetAllCommodity().DefaultIfEmpty().OrderBy(o => o.Name).ToList();
            var commodityGrades = _commodityGradeService.GetAllCommodityGrade().DefaultIfEmpty().OrderBy(o => o.Name).ToList();
            var transporters = _transporterService.GetAllTransporter().DefaultIfEmpty().OrderBy(o => o.Name).ToList();
            var commoditySources = _commoditySourceService.GetAllCommoditySource().DefaultIfEmpty().OrderBy(o => o.Name).ToList();
            var commodityTypes = _commodityTypeService.GetAllCommodityType().DefaultIfEmpty().OrderBy(o => o.Name).ToList();

            var programs = _programService.GetAllProgram().DefaultIfEmpty().OrderBy(o => o.Name).ToList();
            var donors = _donorService.GetAllDonor().DefaultIfEmpty().OrderBy(o => o.Name).ToList();
            var units = _unitService.GetAllUnit().OrderBy(o => o.Name).ToList();

            var hubs = _hubService.GetAllWithoutId(user.DefaultHub.HubID).DefaultIfEmpty().OrderBy(o => o.Name).ToList();

            if (receiveId != "" && receiveId != null)
            {
                Receive receive = _receiveService.FindById(Guid.Parse(receiveId));
                var stacks = new List<AdminUnitItem>();
                if (receive != null && (receive.HubID == user.DefaultHub.HubID))
                {
                    var rViewModel = ReceiveViewModel.GenerateReceiveModel(receive, commodities, commodityGrades, transporters,
                                                            commodityTypes, commoditySources, programs, donors, hubs, user, units);

                    //TODO:=====================================Refactored from viewmodel needs refactor============================

                    if (rViewModel.StoreID != 0)
                    {
                        Store store = _storeService.FindById(rViewModel.StoreID);

                        foreach (var i in store.Stacks)
                        {
                            stacks.Add(new AdminUnitItem { Name = i.ToString(), Id = i });
                        }

                    }
                    rViewModel.Stacks = stacks;
                    //===============================================================================================================

                    return View("Create", rViewModel);
                }
                else if (receive != null && (receive.HubID != user.DefaultHub.HubID))
                {
                    ViewBag.Message =
                        string.Format(
                            "The selected GRN Number {0} doesn't exist in your default warehouse. Try changing your default warehouse.",
                            receive.GRN);
                }
            }

            // Hack
            ViewBag.Stacks = new SelectList(Enumerable.Empty<SelectListItem>());
            List<ReceiveDetailViewModel> receiveCommodities = new List<ReceiveDetailViewModel>();
            ViewBag.ReceiveCommodities = receiveCommodities;
            //TODO:Stacks shuld be sent basend storeID
            var receiveViewModel = new ReceiveViewModel(commodities, commodityGrades, transporters, commodityTypes, commoditySources, programs, donors, hubs, user, units);
            if (Request["type"] != null)
            {
                receiveViewModel.CommoditySourceID = Convert.ToInt32(Request["type"]);
            }
            else
            {
                receiveViewModel.CommoditySourceID = 1;
            }

            //receiveViewModel.SINumber = Request["SINumber"];
            if (Request["ReceiptAllocationID"] != null)
            {

                Guid receiptAllocationID = Guid.Parse(Request["ReceiptAllocationID"]);
                //Added (p.HubID == user.DefaultHub.HubID) check to load allocation only for the current user's defalt Hub
                //get populate using allocation by current hub,comsource and and si number,and not closed
                //TODO use the merge function to have swap,repay,..and others

                //var rAllocation = repository.ReceiptAllocation.GetAllByTypeMerged(receiveViewModel.CommoditySourceID)
                //                  .FirstOrDefault(p => p.SINumber == receiveViewModel.SINumber && p.HubID == user.DefaultHub.HubID
                //                  && p.IsClosed == false);

                //TODO we can load all the sub-commodites here
                //var rAllocation = repository.ReceiptAllocation.FindBySINumber(receiveViewModel.SINumber).FirstOrDefault(
                //    p => p.CommoditySourceID == receiveViewModel.CommoditySourceID && p.HubID == user.DefaultHub.HubID && p.IsClosed == false);
                var rAllocation = _receiptAllocationService.FindById(receiptAllocationID);

                if (rAllocation != null)
                {

                    //Only for loading Waybill no
                    var shippingInstruction = _shippingInstructionService.FindBy(t => t.Value == rAllocation.SINumber).FirstOrDefault();
                    var gCertificate = new Cats.Models.Hubs.GiftCertificate();
                    if(shippingInstruction!=null)
                        gCertificate = _giftCertificateService.FindBySINumber(shippingInstruction.ShippingInstructionID);
                    if (gCertificate != null)
                    {
                        var giftCertificateDetail = gCertificate.GiftCertificateDetails.FirstOrDefault();
                        if (giftCertificateDetail != null)
                        {
                            receiveViewModel.WayBillNo = giftCertificateDetail.BillOfLoading;

                        }
                        receiveViewModel.PortName = gCertificate.PortName;
                        receiveViewModel.VesselName = gCertificate.Vessel;
                    }

                    if (rAllocation.HubID == user.DefaultHub.HubID)
                    {
                        //if the allocation is for the current hub allocation
                        receiveViewModel.ReceiptAllocationID = rAllocation.ReceiptAllocationID;
                        receiveViewModel.SINumber = rAllocation.SINumber;
                        receiveViewModel.ProjectNumber = rAllocation.ProjectNumber;
                        receiveViewModel.ProgramID = rAllocation.ProgramID;
                        receiveViewModel.CommodityTypeID = rAllocation.Commodity.CommodityTypeID;

                        receiveViewModel.ResponsibleDonorID = rAllocation.DonorID;
                        //receiveViewModel.CommoditySourceID = rAllocation.DonorID;
                        receiveViewModel.SupplierName = rAllocation.SupplierName;
                        receiveViewModel.SourceHubID = rAllocation.SourceHubID;
                        receiveViewModel.PurchaseOrder = rAllocation.PurchaseOrder;

                        if (rAllocation.Commodity.ParentID != null)
                        {
                            //TODO if we were to load multiple commoditites don't forget to derecement the allocation id in the neative direction
                            receiveCommodities.Add(new ReceiveDetailViewModel() { ReceiveDetailID = null, ReceiveDetailCounter = -1, CommodityID = rAllocation.CommodityID });
                            receiveViewModel.JSONPrev = JsonConvert.SerializeObject(receiveCommodities);
                        }
                    }
                    else
                    {
                        ViewBag.Message =
                            string.Format(
                                "The selected Receipt Allocation {0} doesn't exist in your default warehouse. Try changing your default warehouse.",
                                rAllocation.ReceiptAllocationID);
                    }
                }

            }
            return View("Create", receiveViewModel);
        }
コード例 #3
0
ファイル: ReceiveController.cs プロジェクト: robela/cats
        public virtual ActionResult Create(ReceiveViewModel receiveModels)
        {
            MembershipProvider membership = new MembershipProvider();
            UserProfile user = _userProfileService.GetUser(User.Identity.Name);

            var commodities = _commodityService.GetAllCommodity().DefaultIfEmpty().OrderBy(o => o.Name).ToList();
            var commodityGrades = _commodityGradeService.GetAllCommodityGrade().DefaultIfEmpty().OrderBy(o => o.Name).ToList();
            var transporters = _transporterService.GetAllTransporter().DefaultIfEmpty().OrderBy(o => o.Name).ToList();
            var commoditySources = _commoditySourceService.GetAllCommoditySource().DefaultIfEmpty().OrderBy(o => o.Name).ToList();
            var commodityTypes = _commodityTypeService.GetAllCommodityType().DefaultIfEmpty().OrderBy(o => o.Name).ToList();

            var programs = _programService.GetAllProgram().DefaultIfEmpty().OrderBy(o => o.Name).ToList();
            var donors = _donorService.GetAllDonor().DefaultIfEmpty().OrderBy(o => o.Name).ToList();

            var hubs = _hubService.GetAllWithoutId(user.DefaultHub.HubID).DefaultIfEmpty().OrderBy(o => o.Name).ToList();

            var units = _unitService.GetAllUnit().OrderBy(o => o.Name).ToList();
            var insertCommodities = new List<ReceiveDetailViewModel>();
            var updateCommodities = new List<ReceiveDetailViewModel>();
            var prevCommodities = new List<ReceiveDetailViewModel>();
            if (receiveModels.JSONPrev != null)
            {
                prevCommodities = GetSelectedCommodities(receiveModels.JSONPrev);

                //Even though they are updated they are not saved so move them in to the inserted at the end of a succcessful submit
                int count = 0;
                foreach (var receiveDetailAllViewModels in prevCommodities)
                {
                    if (receiveDetailAllViewModels.ReceiveDetailID == null)
                    {
                        count--;
                        receiveDetailAllViewModels.ReceiveDetailCounter = count;
                        insertCommodities.Add(receiveDetailAllViewModels);
                    }
                    else
                    {
                        receiveDetailAllViewModels.ReceiveDetailCounter = 1;
                        updateCommodities.Add(receiveDetailAllViewModels);
                    }
                }

                ViewBag.ReceiveDetails = prevCommodities;
                receiveModels.ReceiveDetails = prevCommodities;
                bool isValid = ModelState.IsValid;

                //this check need's to be revisited
                if (prevCommodities.Count() == 0)
                {
                    ModelState.AddModelError("ReceiveDetails", "Please add atleast one commodity to save this Reciept");
                }

                //TODO add check against the commodity type for each commodity
                string errorMessage = null;
                foreach (var receiveDetailViewModel in prevCommodities)
                {
                    var validationContext = new ValidationContext(receiveDetailViewModel, null, null);
                    IEnumerable<ValidationResult> validationResults = receiveDetailViewModel.Validate(validationContext);
                    foreach (var v in validationResults)
                    {
                        errorMessage = string.Format("{0}, {1}", errorMessage, v.ErrorMessage);
                    }
                    Commodity comms = _commodityService.FindById(receiveDetailViewModel.CommodityID);
                    CommodityType commType = _commodityTypeService.FindById(receiveModels.CommodityTypeID);
                    if (receiveModels.CommodityTypeID != comms.CommodityTypeID)
                        ModelState.AddModelError("ReceiveDetails", comms.Name + " is not of type " + commType.Name);
                }
                if (errorMessage != null)
                {
                    ModelState.AddModelError("ReceiveDetails", errorMessage);
                }
            }
            else
            {
                ModelState.AddModelError("ReceiveDetails", "Please add atleast one commodity to save this Reciept");
            }
            switch (receiveModels.CommoditySourceID)
            {
                case CommoditySource.Constants.DONATION:
                    ModelState.Remove("SourceHubID");
                    ModelState.Remove("SupplierName");
                    ModelState.Remove("PurchaseOrder");
                    break;
                case CommoditySource.Constants.LOCALPURCHASE:
                    //ModelState.Remove("DonorID");
                    ModelState.Remove("SourceHubID");
                    //ModelState.Remove("ResponsibleDonorID");
                    break;
                default:
                    ModelState.Remove("DonorID");
                    ModelState.Remove("ResponsibleDonorID");
                    ModelState.Remove("SupplierName");
                    ModelState.Remove("PurchaseOrder");
                    break;
            }

            if (ModelState.IsValid && user != null)
            {
                if (receiveModels.ChangeStoreManPermanently != null && receiveModels.ChangeStoreManPermanently == true)
                {
                    Store storeTobeChanged = _storeService.FindById(receiveModels.StoreID);
                    if (storeTobeChanged != null && receiveModels.ChangeStoreManPermanently == true)
                        storeTobeChanged.StoreManName = receiveModels.ReceivedByStoreMan;
                    //repository.Store.SaveChanges(storeTobeChanged);
                }

                Receive receive = receiveModels.GenerateReceive();
                //if (receive.ReceiveID == null )
                if (receiveModels.ReceiveID == null)
                {
                    //List<ReceiveDetailViewModel> commodities = GetSelectedCommodities(receiveModels.JSONInsertedCommodities);
                    receiveModels.ReceiveDetails = prevCommodities;
                    foreach (var gridCommodities in prevCommodities)
                    {
                        if (user.PreferedWeightMeasurment.Equals("qn"))
                        {
                            gridCommodities.ReceivedQuantityInMT /= 10;
                            gridCommodities.SentQuantityInMT /= 10;
                        }
                    }
                    _transactionService.SaveReceiptTransaction(receiveModels, user);
                }
                else
                {
                    //List<ReceiveDetailViewModel>
                    //insertCommodities = GetSelectedCommodities(receiveModels.JSONInsertedCommodities);
                    List<ReceiveDetailViewModel> deletedCommodities = GetSelectedCommodities(receiveModels.JSONDeletedCommodities);
                    // List<ReceiveDetailViewModel> updateCommodities = GetSelectedCommodities(receiveModels.JSONUpdatedCommodities);
                    receive.HubID = user.DefaultHub.HubID;
                    receive.UserProfileID = user.UserProfileID;
                    receive.Update(GenerateReceiveDetail(insertCommodities),
                        GenerateReceiveDetail(updateCommodities),
                        GenerateReceiveDetail(deletedCommodities));

                }

                return RedirectToAction("Index");
            }
            receiveModels.InitializeEditLists(commodities, commodityGrades, transporters, commodityTypes, commoditySources, programs, donors, hubs, user, units);
            if (receiveModels.ReceiveID != null)
            {
                receiveModels.IsEditMode = true;
            }
            return View(receiveModels);
        }
コード例 #4
0
ファイル: ReceiveViewModel.cs プロジェクト: robela/cats
        /// <summary>
        /// Generates the receive model.
        /// </summary>
        /// <param name="receive">The receive.</param>
        /// <param name="user">The user.</param>
        /// <returns></returns>
        public static ReceiveViewModel GenerateReceiveModel(Receive receive, List<Commodity> commodities, List<CommodityGrade> commodityGrades, List<Transporter> transporters, List<CommodityType> commodityTypes,
            List<CommoditySource> commoditySources, List<Program> programs, List<Donor> donors, List<Hub> hubs, UserProfile user,List<Unit> units )
        {
            ReceiveViewModel model = new ReceiveViewModel();
            model._UserProfile = user;

            model.InitalizeViewModel(commodities, commodityGrades, transporters, commodityTypes,
             commoditySources, programs, donors, hubs, user,units);
            model.IsEditMode = true;
            model.ReceiveID = receive.ReceiveID;
            model.DriverName = receive.DriverName;
            model.GRN = receive.GRN;
            model.PlateNo_Prime = receive.PlateNo_Prime;
            model.PlateNo_Trailer = receive.PlateNo_Trailer;

            model.TransporterID = receive.TransporterID;
            model.HubID = receive.HubID;

            ReceiveDetail receiveDetail = receive.ReceiveDetails.FirstOrDefault();//p=>p.QuantityInMT>0);
            Transaction receiveDetailtransaction = null;
            if (receiveDetail != null)
                foreach (Transaction transaction in receiveDetail.TransactionGroup.Transactions)
                {
                    var negTransaction = receiveDetail.TransactionGroup.Transactions.FirstOrDefault(p => p.QuantityInMT < 0);
                    if (negTransaction != null)
                        model.SourceHubID = negTransaction.Account.EntityID;
                    receiveDetailtransaction = transaction;
                    break;
                }
            if (receiveDetailtransaction != null)
            {
                model.SINumber = receiveDetailtransaction.ShippingInstruction != null ? receiveDetailtransaction.ShippingInstruction.Value : "";

                model.ProjectNumber = receiveDetailtransaction.ProjectCode != null ? receiveDetailtransaction.ProjectCode.Value : "";

                model.ProgramID = receiveDetailtransaction.Program != null ? receiveDetailtransaction.Program.ProgramID : default(int);

                model.StoreID = receiveDetailtransaction.Store != null ? receiveDetailtransaction.Store.StoreID : default(int);

                model.StackNumber = receiveDetailtransaction.Stack.HasValue ? receiveDetailtransaction.Stack.Value : default(int);

            }
            else
            {
                model.SINumber = "";
                model.ProjectNumber = "";
                model.ProgramID = default(int);
                model.StoreID = default(int);
                model.StackNumber = default(int);
            }

            model.ReceiptDate = receive.ReceiptDate;
            model.WayBillNo = receive.WayBillNo;
            model.CommodityTypeID = receive.CommodityTypeID;
            model.ResponsibleDonorID = receive.ResponsibleDonorID;
            model.SourceDonorID = receive.SourceDonorID;
            model.CommoditySourceID = receive.CommoditySourceID;
            model.TicketNumber = receive.WeightBridgeTicketNumber;
            model.WeightBeforeUnloading = receive.WeightBeforeUnloading;
            model.WeightAfterUnloading = receive.WeightAfterUnloading;
            model.VesselName = receive.VesselName;
            model.PortName = receive.PortName;
            model.ReceiptAllocationID = receive.ReceiptAllocationID;
            model.PurchaseOrder = receive.PurchaseOrder;
            model.SupplierName = receive.SupplierName;

            model.Remark = receive.Remark;
            model.ReceivedByStoreMan = receive.ReceivedByStoreMan;

            model.ReceiveDetails =Cats.Models.Hubs.ReceiveDetailViewModel.GenerateReceiveDetailModels(receive.ReceiveDetails);
            return model;
        }