GenerateDipatch() public method

Generates the dipatch.
public GenerateDipatch ( UserProfile user ) : Dispatch
user UserProfile
return Dispatch
Ejemplo n.º 1
0
        public ActionResult Create(DispatchModel dispatchModel)
        {
            var user = _userProfileService.GetUser(User.Identity.Name);

            var insertCommodities = new List<DispatchDetailModel>();
            var updateCommodities = new List<DispatchDetailModel>();
            var prevCommodities = new List<DispatchDetailModel>();
            if (dispatchModel.JSONPrev != null)
            {
                prevCommodities = GetSelectedCommodities(dispatchModel.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 dispatchDetailAllViewModels in prevCommodities)
                {
                    if (dispatchDetailAllViewModels.Id != null)
                    {
                        count--;
                        dispatchDetailAllViewModels.DispatchDetailCounter = count;
                        insertCommodities.Add(dispatchDetailAllViewModels);
                    }
                    else
                    {
                        updateCommodities.Add(dispatchDetailAllViewModels);
                    }
                }

                ViewBag.ReceiveDetails = prevCommodities;
                ViewBag.SelectedCommodities = prevCommodities;
                dispatchModel.DispatchDetails = prevCommodities;

                //this check need's to be revisited
                if (!prevCommodities.Any())
                {
                    ModelState.AddModelError("DispatchDetails", @"Please add atleast one commodity to save this Dispatch");
                }
                string errorMessage = null;
                foreach (var dispatchDetailViewModel in prevCommodities)
                {
                    var validationContext = new ValidationContext(dispatchDetailViewModel, null, null);
                    var validationResults = dispatchDetailViewModel.Validate(validationContext);
                    errorMessage = validationResults.Aggregate(errorMessage, (current, v) => string.Format("{0}, {1}", current, v.ErrorMessage));
                    var comms = _commodityService.FindById(dispatchDetailViewModel.CommodityID);
                    var commType = _commodityTypeService.FindById(dispatchModel.CommodityTypeID);
                    if (dispatchModel.CommodityTypeID != comms.CommodityTypeID)
                        ModelState.AddModelError("DispatchDetails", comms.Name + @" is not of type " + commType.Name);
                }
                if (errorMessage != null)
                {
                    ModelState.AddModelError("DispatchDetails", errorMessage);
                }
            }
            else
            {
                ModelState.AddModelError("DispatchDetails", @"Please add atleast one commodity to save this Dispatch");
            }
            if (dispatchModel.Type != 1)
            {
                ModelState.Remove("FDPID");
                ModelState.Remove("RegionID");
                ModelState.Remove("WoredaID");
                ModelState.Remove("ZoneID");
                ModelState.Remove("BidNumber");
                dispatchModel.BidNumber = "00000";
                //NOT really needed
                ModelState.Remove("Year");
                ModelState.Remove("Month");
            }
            else
            {
                ModelState.Remove("ToHubID");
            }

            if (ModelState.IsValid && user != null)
            {

                if (dispatchModel.ChangeStoreManPermanently)
                {
                    var storeTobeChanged = _storeService.FindById(dispatchModel.StoreID);
                    if (storeTobeChanged != null && dispatchModel.ChangeStoreManPermanently)
                        storeTobeChanged.StoreManName = dispatchModel.DispatchedByStoreMan;
                }
                var dispatch = dispatchModel.GenerateDipatch(user);
                //if (dispatch.DispatchID == null )
                if (dispatchModel.DispatchID == null)
                {

                    dispatchModel.DispatchDetails = prevCommodities;
                    foreach (var gridCommodities in prevCommodities)
                    {
                        if (user.PreferedWeightMeasurment.Equals("qn"))
                        {
                            gridCommodities.DispatchedQuantityMT /= 10;
                            gridCommodities.RequestedQuantityMT /= 10;
                        }
                    }
                    //InsertDispatch(dispatchModel, user);
                    _transactionService.SaveDispatchTransaction(dispatchModel, user);
                }
                else
                {

                    // List<Models.DispatchDetailModel> insertCommodities = GetSelectedCommodities(dispatchModel.JSONInsertedCommodities);
                    var deletedCommodities = GetSelectedCommodities(dispatchModel.JSONDeletedCommodities);
                    // List<Models.DispatchDetailModel> updateCommodities = GetSelectedCommodities(dispatchModel.JSONUpdatedCommodities);
                    dispatch.HubID = user.DefaultHub.Value;
                    dispatch.Update(GenerateDispatchDetail(insertCommodities),
                        GenerateDispatchDetail(updateCommodities),
                        GenerateDispatchDetail(deletedCommodities));

                }

                return RedirectToAction("Index");
            }
            //List<Models.DispatchDetailModel> details = GetSelectedCommodities(dispatchModel.JSONInsertedCommodities);
            //Session["SELCOM"] = details;

            //UserProfile user =UserProfile.GetUser(User.Identity.Name);
            PrepareCreate(dispatchModel.Type);

            if (dispatchModel.FDPID != null)
            {
                PrepareFDPForEdit(dispatchModel.FDPID);
                dispatchModel.WoredaID = _fdpService.FindById(dispatchModel.FDPID.Value).AdminUnitID;
            } //PrepareEdit(dispatchModel.GenerateDipatch(), user,dispatchModel.Type);
            return View(dispatchModel);
        }
Ejemplo n.º 2
0
        //used to return void
        /// <summary>
        /// Saves the dispatch transaction.
        /// </summary>
        /// <param name="dispatchModel">The dispatch model.</param>
        /// <param name="user">The user.</param>
        public bool SaveDispatchTransaction(DispatchModel dispatchModel, UserProfile user)
        {
            Dispatch dispatch = dispatchModel.GenerateDipatch(user);
            dispatch.DispatchID = Guid.NewGuid();
            dispatch.HubID = user.DefaultHub.Value;
            dispatch.UserProfileID = user.UserProfileID;
            dispatch.DispatchAllocationID = dispatchModel.DispatchAllocationID;
            dispatch.OtherDispatchAllocationID = dispatchModel.OtherDispatchAllocationID;
            CommodityType commType = _unitOfWork.CommodityTypeRepository.FindById(dispatchModel.CommodityTypeID);

            foreach (DispatchDetailModel detail in dispatchModel.DispatchDetails)
            {

                if (commType.CommodityTypeID == 2)//if it's a non food
                {
                    detail.DispatchedQuantityMT = 0;
                    detail.RequestedQuantityMT = 0;
                }

                TransactionGroup group = new TransactionGroup();
                group.TransactionGroupID = Guid.NewGuid();
                if (dispatchModel.Type == 1)
                {
                    Transaction transaction = GetGoodsOnHandFDPTransaction(dispatchModel, dispatch, detail);
                    group.Transactions.Add(transaction);

                    Transaction transaction2 = GetGoodsInTransitFDPTransaction(dispatchModel, dispatch, detail);
                    group.Transactions.Add(transaction2);

                    Transaction transaction3 = GetStatisticsFDPTransaction(dispatchModel, dispatch, detail);
                    group.Transactions.Add(transaction3);

                    Transaction transaction4 = GetCommitedToFDPTransaction(dispatchModel, dispatch, detail);
                    group.Transactions.Add(transaction4);
                }
                else
                {
                    Transaction transaction = GetGoodsOnHandHUBTransaction(dispatchModel, dispatch, detail);
                    group.Transactions.Add(transaction);

                    Transaction transaction2 = GetGoodInTransitHUBTransaction(dispatchModel, dispatch, detail);
                    group.Transactions.Add(transaction2);

                    Transaction transaction3 = GetStatisticsHUBTransaction(dispatchModel, dispatch, detail);
                    group.Transactions.Add(transaction3);

                    Transaction transaction4 = GetCommittedToFDPHUBTransaction(dispatchModel, dispatch, detail);
                    group.Transactions.Add(transaction4);
                }

                DispatchDetail dispatchDetail = GenerateDispatchDetail(detail);
                dispatchDetail.DispatchDetailID = Guid.NewGuid();
                dispatchDetail.TransactionGroup = group;

                dispatch.DispatchDetails.Add(dispatchDetail);

            }
            // Try to save this transaction
            //    db.Database.Connection.Open();
            //  DbTransaction dbTransaction = db.Database.Connection.BeginTransaction();
            try
            {
                _unitOfWork.DispatchRepository.Add(dispatch);
                _unitOfWork.Save();
                return true;
                //repository.Dispatch.Add(dispatch);
                //dbTransaction.Commit();
            }
            catch (Exception exp)
            {
                // dbTransaction.Rollback();
                //TODO: Save the detail of this exception somewhere
                throw new Exception("The Dispatch Transaction Cannot be saved. <br />Detail Message :" + exp.Message);
            }

            if (dispatch.Type == 1)
            {
                string sms = dispatch.GetSMSText();
                //SMS.SendSMS(dispatch.FDPID.Value, sms);
            }
            return false;
        }