예제 #1
0
        public ActionResult ExportCommisionList()
        {
            try
            {
                using (XLWorkbook wb = new XLWorkbook())
                {
                    List <CPProductImportCommisionListModel> lsImport = new List <CPProductImportCommisionListModel>();
                    object temp;
                    TheSession.TryGet(_strSectionCommisionList, out temp);
                    if (temp != null)
                    {
                        lsImport = (List <CPProductImportCommisionListModel>)temp;
                    }
                    if (lsImport.Any())
                    {
                        wb.Worksheets.Add(MapModelToNewTableCommision(lsImport), "CommisionListTemplate");

                        Response.Clear();
                        Response.Buffer      = true;
                        Response.Charset     = "";
                        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                        Response.AddHeader("content-disposition", $"attachment;filename={DateTime.Now:yyyyMMdd}_CounterPartyTemplate.xlsx");
                        using (MemoryStream myMemoryStream = new MemoryStream())
                        {
                            wb.SaveAs(myMemoryStream);
                            myMemoryStream.WriteTo(Response.OutputStream);
                            Response.End();
                            return(Content("Success"));
                        }
                    }
                    return(Content("No Data"));
                }
            }
            catch (Exception ex)
            {
                HDBH.Log.WriteLog.Error("ManagerController => ExportCommisionList", ex);
                return(Content("No Data"));
            }
        }
예제 #2
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" }));
        }