public ActionResult IndexC1(RequestRestoreMaterialViewModel parameters)
        {
            if (parameters == null)
            {
                parameters = new RequestRestoreMaterialViewModel();
                TryValidateModel(parameters);
            }

            if (!ModelState.IsValid)
            {
                var httpResponseViewModel = new MessageResponseViewModel();
                httpResponseViewModel.HttpStatusCode = HttpStatusCode.BadRequest;
                httpResponseViewModel.Message        = "Invalid parameters submitted to service";
                return(Json(httpResponseViewModel));
            }

            var restoreMaterialViewModel = new RestoreMaterialViewModel();

            restoreMaterialViewModel.MaterialCode = parameters.MaterialCode;
            restoreMaterialViewModel.MaterialDsp  = parameters.MaterialDsp;
            restoreMaterialViewModel.ShelfNo      = parameters.ShelfNo;
            restoreMaterialViewModel.PalletNo     = parameters.PalletNo;

            //var materialShelfStocks = _stockTakingOfMaterialDomain.GetMaterialShelfStocks(model.ShelfNo, model.MaterialCode);
            var items     = _stockTakingOfMaterialDomain.GetMaterialShelfStocks(parameters.ShelfNo, parameters.MaterialCode);
            var materials = new List <FinalStockTakingMaterialItem>();

            foreach (var item in items)
            {
                var result = Math.Abs(item.F33_Amount / item.F01_PackingUnit - 1);

                var material = new FinalStockTakingMaterialItem();
                if (result < 0.01)
                {
                    material.PackQuantity = 1;
                    material.Fraction     = 0.00;
                }
                else
                {
                    material.PackQuantity = Convert.ToInt32(item.F33_Amount / item.F01_PackingUnit);
                    material.Fraction     = item.F33_Amount -
                                            item.F01_PackingUnit * Convert.ToDouble(item.F33_Amount / item.F01_PackingUnit);
                }
                material.MaterialLotNo = item.F33_MaterialLotNo;
                material.PackUnit      = item.F01_PackingUnit;
                material.Total         = item.F33_Amount;
                restoreMaterialViewModel.GrandTotal += item.F33_Amount;

                materials.Add(material);
            }

            restoreMaterialViewModel.Materials = materials.ToArray();
            return(View(restoreMaterialViewModel));
        }
        public JsonResult ReStore(RestoreMaterialViewModel model)
        {
            if (model == null)
            {
                model = new RestoreMaterialViewModel();
                TryValidateModel(model);
            }

            if (!ModelState.IsValid)
            {
                return(Json(new { Success = false, Message = "Parameters are invalid" }));
            }

            //Get current terminalNo
            var terminalNo = _identityDomain.FindTerminalNo(HttpContext.User.Identity);

            #region Additional validations

            //If all of [Material Lot No.] fields are blank, the system will show error message using template MSG 8.
            if (model.Materials.Any(x => string.IsNullOrEmpty(x.MaterialLotNo)))
            {
                return(Json(new { Success = false, Message = MessageResource.MSG8_Material }, JsonRequestBehavior.AllowGet));
            }

            //There is at least one of [Material Lot No.] fields which are inputted and their corresponding [Pack Qty]
            //fields are not “0.0” (Valid item), if not, the system will show error message using template MSG 12.
            var hasValidItem = false;
            var validItems   = new List <string>();

            foreach (var item in model.Materials)
            {
                if (!string.IsNullOrEmpty(item.MaterialLotNo) && (item.PackQuantity != 0))
                {
                    hasValidItem = true;
                    validItems.Add(item.MaterialLotNo);
                }
            }

            if (!hasValidItem)
            {
                return(Json(new { Success = false, Message = MessageResource.MSG12_Material },
                            JsonRequestBehavior.AllowGet));
            }
            //If there any [Material Lot No.] of “Valid items” are duplicated, the system will show error message using template MSG 13.
            var duplicateItems = validItems.GroupBy(x => x).Where(group => group.Count() > 1).Select(group => group.Key);
            if (duplicateItems.Count() > 0)
            {
                return(Json(new { Success = false, Message = MessageResource.MSG13_Material },
                            JsonRequestBehavior.AllowGet));
            }

            //If [Unit] of corresponding Material Master DB record (based on [Material Code]) is not “K” and there any [Fraction]
            //is greater than or equal to corresponding [Pack Unit], the system will show error message using template MSG 14.
            var material = _materialDomain.GetById(model.MaterialCode);
            var unitOK   = true;
            if (!material.F01_Unit.Equals("K"))
            {
                unitOK = !model.Materials.Any(x => x.Fraction >= x.PackUnit);
            }

            if (!unitOK)
            {
                return(Json(new { Success = false, Message = MessageResource.MSG14_Material },
                            JsonRequestBehavior.AllowGet));
            }

            //ToDo: If there is no [tm05_conveyor] record whose [f05_terminalno] is current application terminal or [f05_strrtrsts]
            //is “9” (Error), the system will show error message using template MSG 15.
            //If there is no [tm14_device] record whose [f14_devicecode] is “ATW001” or [f14_devicestatus] is “1” (Offline),
            //[f14_devicestatus] is “2” (Error) or [f14_usepermitclass] is “1” (Prohibited), the system will show error message using template MSG 16.

            #endregion

            //var modelItem = Mapper.Map<StockTakingOfMaterialC1ViewModelItem>(model);
            _stockTakingOfMaterialDomain.RestoreMaterialStocks(model, terminalNo);
            return(Json(new { Success = true, Message = "Finish Re-store material stocks!" }));
        }
        public void RestoreMaterialStocks(RestoreMaterialViewModel model, string termianlNo)
        {
            #region Re-store material stocks

            //Delete [TX33_MtrShfStk] records whose [F33_PalletNo] is [f33_palletno] of selected [tx33_mtrshfstk] record
            //(the selected item in screen SC 20 – Stock-taking of Raw Material (TCRM081F))  and [F33_MaterialCode] is [Material Code] in current screen.
            Delete(model.PalletNo, model.MaterialCode);

            //For each item in the table listing whose [Material Lot No] is not blank and [Total] is greater than 0,
            //the system will create and save a new [TX33_MtrShfStk] record into table [TX33_MtrShfStk]

            foreach (var item in model.Materials)
            {
                if (!string.IsNullOrEmpty(item.MaterialLotNo) && (item.Total > 0.0))
                {
                    Create(model.PalletNo, model.MaterialCode, item.MaterialLotNo, item.Total);
                }
            }

            //Update material shelf status table (table [tx31_mtrshfsts]) whose [F31_ShelfRow] is 2 first characters of
            //[Shelf No], [F31_ShelfBay] is 2 next characters of [Shelf No] and [F31_ShelfLevel] is 2 last characters of [Shelf No]:
            string shelfNo = "";
            var    materialShelfStatusItem =
                _unitOfWork.MaterialShelfStatusRepository.GetAll()
                .FirstOrDefault(
                    m =>
                    m.F31_ShelfRow.Equals(model.ShelfNo.Substring(0, 2)) &&
                    m.F31_ShelfBay.Equals(model.ShelfNo.Substring(3, 2)) &&
                    m.F31_ShelfLevel.Equals(model.ShelfNo.Substring(6, 2)));
            if (materialShelfStatusItem != null)
            {
                materialShelfStatusItem.F31_ShelfStatus =
                    ((int)Constants.F31_ShelfStatus.ReservedForRetrieval).ToString();
                materialShelfStatusItem.F31_Amount       = model.GrandTotal;
                materialShelfStatusItem.F31_TerminalNo   = termianlNo;
                materialShelfStatusItem.F31_UpdateDate   = DateTime.Now;
                materialShelfStatusItem.F31_UpdateCount += 1;
                shelfNo = materialShelfStatusItem.F31_ShelfRow + materialShelfStatusItem.F31_ShelfBay
                          + materialShelfStatusItem.F31_ShelfLevel;
                materialShelfStatusItem.F31_StockTakingFlag = Constants.F31_ShelfStatus.EmptyShelf.ToString("D");
                _unitOfWork.MaterialShelfStatusRepository.Update(materialShelfStatusItem);
            }
            var isNoManage = false;
            //var f48_MtrWhsCmdNo = CreateOrUpdateTX48(ref isNoManage);
            var f48_MtrWhsCmdNo = _unitOfWork.NoManageRepository.CreateOrUpdateTX48(ref isNoManage,
                                                                                    Constants.GetColumnInNoManager.MtrWhsCmdNo);

            var asConvCode =
                _unitOfWork.ConveyorRepository.Get(i => i.F05_TerminalNo.Trim().Equals(termianlNo)).F05_ConveyorCode;

            InsertTX34(Constants.F34_CommandNo.StockTakingIn, f48_MtrWhsCmdNo,
                       Constants.CmdType.cmdType,
                       Constants.TX34_StrRtrType.Material.ToString("D"), Constants.TC_CMDSTS.TC_CMDSTS_0, model.PalletNo,
                       asConvCode, shelfNo, termianlNo,
                       Constants.PictureNo.TCRM082F);


            //Send message to C1.
            var msgId     = "0001";
            var pictureNo = Constants.PictureNo.TCRM082F;

            _notificationService.SendMessageToC1(new
            {
                msgId,
                termianlNo,
                pictureNo
            });
            _unitOfWork.Commit();

            #endregion
        }