示例#1
0
        public JsonResult ImportCommisionList(HttpPostedFileBase importCommisionList)
        {
            string fileName = "";
            object ls;
            object retList = null;

            try
            {
                var file = importCommisionList;
                if (file != null)
                {
                    string[] extFile = new string[] { "xlsx" };
                    fileName = string.Format("{0}\\{1}{2}", Server.MapPath("~/Content/Uploads"), DateTime.Now.ToString("yyyyMMddHHmmss"), System.IO.Path.GetExtension(file.FileName));
                    if (fileName.EndsWith(".xlsx", StringComparison.CurrentCultureIgnoreCase))
                    {
                        file.SaveAs(fileName);
                    }

                    Reader           reader = new Reader(fileName);
                    IDataTransformer podata = new DataTransformer();
                    ls = reader.GetData <CPProductImportTempCommisionListModel>(podata);
                    if (ls != null)
                    {
                        int idx = 1;
                        List <CPProductImportCommisionListModel> lsImport  = new List <CPProductImportCommisionListModel>();
                        List <CPProductImportProductModel>       lsProduct = new List <CPProductImportProductModel>();
                        object temp;
                        TheSession.TryGet(_strSectionProduct, out temp);
                        if (temp != null)
                        {
                            lsProduct = (List <CPProductImportProductModel>)temp;
                        }
                        if (lsProduct != null && lsProduct.Any())
                        {
                            foreach (var itx in ls as List <CPProductImportTempCommisionListModel> )
                            {
                                if (!string.IsNullOrEmpty(itx.productCode))
                                {
                                    string strProductCode = itx.productCode.FilterSpecial();
                                    string strMessage     = string.Empty;
                                    bool   isExists       = false;

                                    if (lsProduct != null && lsProduct.Any())
                                    {
                                        lsProduct = lsProduct.Where(x => string.IsNullOrEmpty(x.errorMessage)).ToList();
                                        isExists  = lsProduct.Any(x => x.productCode.ToUpper().Trim().Equals(strProductCode.ToUpper().Trim()));
                                    }
                                    else
                                    {
                                        strMessage += " Không tìm thấy danh sách mã sản phẩm";
                                    }
                                    if (!isExists)
                                    {
                                        strMessage += " Mã sản phẩm không tồn tại";
                                    }

                                    if (itx.effectedFromDate == null)
                                    {
                                        strMessage += " Thiếu trường hiệu lực từ ngày";
                                    }
                                    else
                                    {
                                        if (DateTime.Parse(itx.effectedFromDate.ToString()).Ticks < DateTime.Now.Date.Ticks)
                                        {
                                            strMessage += " Ngày hiệu lực từ ngày phải lớn hơn ngày hiện tại";
                                        }
                                    }
                                    if (itx.effectedToDate == null)
                                    {
                                        strMessage += " Thiếu trường hiệu lực đến ngày";
                                    }
                                    else
                                    {
                                        if (DateTime.Parse(itx.effectedToDate.ToString()).Ticks < DateTime.Now.Date.Ticks)
                                        {
                                            strMessage += " Ngày hiệu lực đến ngày phải lớn hơn ngày hiện tại";
                                        }
                                    }
                                    var item = new CPProductImportCommisionListModel
                                    {
                                        productCode      = strProductCode,
                                        productName      = itx.productName,
                                        effectedFromDate = itx.effectedFromDate != null?DateTime.Parse(itx.effectedFromDate.ToString()) : new DateTime(),
                                                               effectedToDate = itx.effectedToDate != null?DateTime.Parse(itx.effectedToDate.ToString()) : new DateTime(),
                                                                                    totalCommisRate   = (itx.agencyCommisRate ?? 0) + (itx.supportCommisRate ?? 0) + (itx.serviceCostRate ?? 0),
                                                                                    agencyCommisRate  = (itx.agencyCommisRate ?? 0),
                                                                                    supportCommisRate = (itx.supportCommisRate ?? 0),
                                                                                    serviceCostRate   = (itx.serviceCostRate ?? 0),
                                                                                    commisRate        = (itx.commisRate ?? 0),
                                                                                    errorMessage      = strMessage
                                    };
                                    item.remainRate = item.totalCommisRate - item.commisRate;
                                    item.SelectList = new List <SelectListItem>();
                                    item.SelectList = lsProduct;
                                    lsImport.Add(item);
                                    idx++;
                                }
                            }
                            retList = lsImport;
                            TheSession.TrySet(_strSectionCommisionList, retList);
                            return(Json(new { Code = 0, Message = RenderPartialViewToString("~/Areas/CPProduct/Views/Shared/_templateCommissionListImport.cshtml", retList) }));
                        }
                        else
                        {
                            return(Json(new { Code = -1, Message = "Không tìm thấy danh sách sản phẩm" }));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                HDBH.Log.WriteLog.Error("ManagerController => ImportCommisionList", ex);
            }
            finally
            {
                if (!string.IsNullOrWhiteSpace(fileName))
                {
                    System.IO.File.Delete(fileName);
                }
            }
            return(Json(new { Code = -1, Message = "Không tìm thấy danh sách sản phẩm" }));
        }
示例#2
0
        public JsonResult ImportProduct(HttpPostedFileBase importProduct)
        {
            string fileName = "";
            object ls;
            object retList = null;

            try
            {
                var file = importProduct;
                if (file != null)
                {
                    string[] extFile = new string[] { "xlsx" };
                    fileName = string.Format("{0}\\{1}{2}", Server.MapPath("~/Content/Uploads"), DateTime.Now.ToString("yyyyMMddHHmmss"), System.IO.Path.GetExtension(file.FileName));
                    if (fileName.EndsWith(".xlsx", StringComparison.CurrentCultureIgnoreCase))
                    {
                        file.SaveAs(fileName);
                    }

                    Reader           reader = new Reader(fileName);
                    IDataTransformer podata = new DataTransformer();
                    ls = reader.GetData <CPProductImportTempProductModel>(podata);
                    if (ls != null)
                    {
                        int idx = 1;
                        List <CPProductImportProductModel> lsImport = new List <CPProductImportProductModel>();
                        foreach (var itx in ls as List <CPProductImportTempProductModel> )
                        {
                            if (!string.IsNullOrEmpty(itx.productCode))
                            {
                                string strProductCode = itx.productCode.FilterSpecial();
                                string strProductName = itx.productName.FilterSpecial();
                                string strMessage     = string.Empty;

                                if (string.IsNullOrEmpty(strProductCode))
                                {
                                    strMessage += " Chưa nhập Product Code";
                                }
                                else
                                {
                                    if (HasSpecialChars(strProductCode))
                                    {
                                        strMessage += " Lỗi ký tự đặc biệt";
                                    }
                                    if (HasUnicode(strProductCode))
                                    {
                                        strMessage += " Lỗi ký unicode";
                                    }
                                    if (HasSpace(strProductCode))
                                    {
                                        strMessage += " Lỗi khoảng trắng";
                                    }
                                }

                                var item = new CPProductImportProductModel
                                {
                                    productCode  = strProductCode,
                                    productName  = strProductName,
                                    errorMessage = strMessage
                                };

                                lsImport.Add(item);
                                idx++;
                            }
                        }
                        retList = lsImport;
                    }
                    TheSession.TrySet(_strSectionProduct, retList);
                }
            }
            catch (Exception ex)
            {
                HDBH.Log.WriteLog.Error("ManagerController => ImportProduct", ex);
            }
            finally
            {
                if (!string.IsNullOrWhiteSpace(fileName))
                {
                    System.IO.File.Delete(fileName);
                }
            }
            return(Json(RenderPartialViewToString("~/Areas/CPProduct/Views/Shared/_templateProductImport.cshtml", retList)));
        }