Пример #1
0
        public async Task <ActionResult> ImportSales(ImportSalesViewModel model)
        {
            AlertMessage alert = new AlertMessage();

            model = null;

            if (TempData["ImportSales"] != null)
            {
                model = TempData["ImportSales"] as ImportSalesViewModel;
            }
            else
            {
                alert.Text = StaticMessage.ERR_INVALID_INPUT;
            }

            if (model != null)
            {
                HierSalesBusiness business = new HierSalesBusiness(new SmtpService());
                business.SetUserAuth(ViewBag.UserAuth);

                alert = await business.ImportSales(model);
            }

            TempData["AlertMessage"] = alert;

            return(RedirectToAction("Index"));
        }
Пример #2
0
        public ActionResult PreviewImportSales(ImportSalesViewModel model)
        {
            AlertMessage alert = new AlertMessage();

            if (!ModelState.IsValid)
            {
                alert.Text = string.Join(System.Environment.NewLine, ModelState.Values
                                         .SelectMany(v => v.Errors)
                                         .Select(e => e.ErrorMessage));
            }
            else
            {
                HierSalesBusiness business = new HierSalesBusiness();
                business.SetUserAuth(ViewBag.UserAuth);

                alert = business.Preview(model);
            }

            TempData["AlertMessage"] = alert;

            if (alert.Data != null)
            {
                TempData["ImportSales"] = alert.Data;

                return(View(alert.Data));
            }
            else
            {
                return(RedirectToAction("Import"));
            }
        }
Пример #3
0
        public async Task <AlertMessage> ImportSales(ImportSalesViewModel model)
        {
            AlertMessage alert = new AlertMessage();

            _logger.Write("info", DateTime.Now, string.Format("Upload Master Hier Sales Total : {0}", model.ListHierSales.Count), _userAuth.Fullname);

            if (!IsAccessible(ModuleCode.MasterSales))
            {
                alert.Text = StaticMessage.ERR_ACCESS_DENIED;
                return(alert);
            }

            if (!IsEditable())
            {
                alert.Text = StaticMessage.ERR_ACCESS_DENIED;
                return(alert);
            }

            int month = 0;
            int year  = 0;

            try
            {
                string[] arr = model.FormattedValidDate.Split('-');

                month = Convert.ToInt16(arr[0]);
                year  = Convert.ToInt16(arr[1]);
            }
            catch (Exception ex)
            {
                alert.Text = StaticMessage.ERR_INVALID_INPUT;
                return(alert);
            }

            if (month == 0 || year == 0)
            {
                alert.Text = StaticMessage.ERR_INVALID_INPUT;
                return(alert);
            }

            IRepository <UploadHier2> repoUploadHier = _unitOfWork.GetRepository <UploadHier2>();

            UploadHier2 hier = null;

            foreach (var item in model.ListHierSales)
            {
                item.Remarks = null;

                hier = new UploadHier2()
                {
                    RayonCode    = item.RayonCode,
                    Plant        = item.PlantCode,
                    RayonType    = item.RayonType,
                    BUM          = item.BUMNik,
                    NSM          = item.NSMNik,
                    ASM          = item.ASMNik,
                    FSS          = item.FSSNik,
                    SLM          = item.SLMNik,
                    CreatedByNIK = _userAuth.NIK
                };

                try
                {
                    repoUploadHier.Insert(hier, true);
                }
                catch (Exception ex)
                {
                    item.Remarks = ex.Message;
                }
            }

            #region execute SP
            List <SqlParameter> listSqlParam = new List <SqlParameter>()
            {
                new SqlParameter("@inMonth", month),
                new SqlParameter("@inYear", year),
                new SqlParameter("@nik", _userAuth.NIK),
            };

            bool   isError    = false;
            string connString = GetConnectionString();

            Dictionary <int, dynamic> dc = null;

            try
            {
                //dc = SqlHelper.ExecuteProcedureWithReturnRecords(connString, "sp_uploadHier", listSqlParam
                dc = SqlHelper.ExecuteProcedureWithReturnRecords(connString, "sp_uploadHier_v2", listSqlParam);

                if (dc.ContainsKey(0))
                {
                    isError = true;
                }
            }
            catch (Exception ex)
            {
                _logger.Write("Error", DateTime.Now, ex.Message, _userAuth.Fullname, ex);
                isError = true;
            }
            #endregion

            if (isError)
            {
                // delete all records from table UploadML
                var condition = PredicateBuilder.True <UploadHier2>().And(x => x.CreatedByNIK == _userAuth.NIK);

                repoUploadHier.Delete(condition, true);
            }
            else if (dc != null)
            {
                dynamic val;

                DataTable dt = null;

                if (dc.TryGetValue(1, out val))
                {
                    dt = val as DataTable;
                }
            }

            _unitOfWork.Dispose();

            if (isError)
            {
                alert.Text = StaticMessage.ERR_SAVE_FAILED;
            }
            else
            {
                int numSuccess = model.ListHierSales.Count(x => string.IsNullOrEmpty(x.Remarks));

                alert.Status = 1;
                alert.Text   = string.Format(StaticMessage.SCS_IMPORT, numSuccess);

                await SendEmailUpload();
            }

            return(alert);
        }
Пример #4
0
        public AlertMessage Preview(ImportSalesViewModel model)
        {
            AlertMessage alert = new AlertMessage();

            if (!IsAccessible(ModuleCode.MasterSales))
            {
                alert.Text = StaticMessage.ERR_ACCESS_DENIED;
                return(alert);
            }

            if (!IsEditable())
            {
                alert.Text = StaticMessage.ERR_ACCESS_DENIED;
                return(alert);
            }

            List <HierSalesViewModel> listVM = GetListHierSalesViewModel(model.InputFile);

            if (listVM == null)
            {
                alert.Text = StaticMessage.ERR_INVALID_INPUT;
                return(alert);
            }

            IRepository <FSS> repoFSS = _unitOfWork.GetRepository <FSS>();
            IRepository <SLM> repoSLM = _unitOfWork.GetRepository <SLM>();
            FSS fss = null;
            SLM slm = null;

            DateTime dtMaxValidTo = DateTime.ParseExact(AppConstant.DefaultValidTo, AppConstant.DefaultFormatDate, _cultureInfo);

            Dictionary <int, string> dcExistingFSS = new Dictionary <int, string>();
            Dictionary <int, string> dcExistingSLM = new Dictionary <int, string>();

            DateTime today = DateTime.UtcNow.ToUtcID().Date;

            List <string> listRemarks = null;

            foreach (var item in listVM)
            {
                if (!string.IsNullOrEmpty(item.Remarks))
                {
                    continue;
                }

                if (dcExistingFSS.ContainsKey(item.FSSNik) && dcExistingSLM.ContainsKey(item.SLMNik))
                {
                    continue;
                }

                listRemarks = new List <string>();

                if (item.FSSNik != 0 && !dcExistingFSS.ContainsKey(item.FSSNik))
                {
                    repoFSS.Condition = PredicateBuilder.True <FSS>().And(x => x.NIK == item.FSSNik);
                    repoFSS.OrderBy   = new SqlOrderBy("CreatedOn", SqlOrderType.Descending);

                    fss = repoFSS.Find().FirstOrDefault();

                    if (fss == null)
                    {
                        listRemarks.Add(string.Format(StaticMessage.ERR_FSS_NOT_FOUND, item.FSSNik));
                    }
                    else if (fss.ValidTo < today)
                    {
                        listRemarks.Add(string.Format(StaticMessage.ERR_FSS_NOT_ACTIVE, item.FSSNik));
                    }
                    else
                    {
                        dcExistingFSS[fss.NIK] = fss.FullName;
                    }
                }

                if (item.SLMNik != 0 && !dcExistingSLM.ContainsKey(item.SLMNik))
                {
                    repoSLM.Condition = PredicateBuilder.True <SLM>().And(x => x.NIK == item.SLMNik);
                    repoSLM.OrderBy   = new SqlOrderBy("CreatedOn", SqlOrderType.Descending);

                    slm = repoSLM.Find().FirstOrDefault();

                    if (slm == null)
                    {
                        listRemarks.Add(string.Format(StaticMessage.ERR_SLM_NOT_FOUND, item.SLMNik));
                    }
                    else if (slm.ValidTo < today)
                    {
                        listRemarks.Add(string.Format(StaticMessage.ERR_SLM_NOT_ACTIVE, item.SLMNik));
                    }
                    else
                    {
                        dcExistingSLM[slm.NIK] = slm.FullName;
                    }
                }

                if (listRemarks.Count > 0)
                {
                    item.Remarks = String.Join("; ", listRemarks.ToArray());
                }
            }

            int numError = listVM.Count(x => !string.IsNullOrEmpty(x.Remarks));

            if (numError > 0)
            {
                alert.Status = 0;
                alert.Text   = string.Format(StaticMessage.ERR_PREVIEW_CONTAINS_ERROR, numError);
            }

            model.ListHierSales = listVM;

            alert.Data = model;

            return(alert);
        }