public ActionResult GeneralCode(string postId) { MailerHandleCommon mailerHandle = new MailerHandleCommon(db); var code = mailerHandle.GeneralMailerCode(postId); return(Json(new { error = 0, code = code }, JsonRequestBehavior.AllowGet)); }
public BaseController() { RoleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(sdb)); UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(sdb)); conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString); MailerHandle = new MailerHandleCommon(new MNPOSTEntities()); }
public ActionResult Create(CustomerInfo cus) { MailerHandleCommon handle = new MailerHandleCommon(db); var checkGroup = db.BS_CustomerGroups.Where(p => p.CustomerGroupCode == cus.CustomerGroupCode).FirstOrDefault(); if (checkGroup == null) { return(Json(new ResultInfo() { error = 1, msg = "Sai mã nhóm" }, JsonRequestBehavior.AllowGet)); } var code = handle.GeneralCusCode(checkGroup.CustomerGroupCode); var ins = new BS_Customers() { CustomerID = Guid.NewGuid().ToString(), CustomerName = cus.CustomerName, CountryID = "VN", Address = cus.Address, CreateDate = DateTime.Now, CustomerCode = code, CustomerGroupID = checkGroup.CustomerGroupID, Deputy = cus.Deputy, DistrictID = cus.DistrictID, Email = cus.Email, IsActive = true, Phone = cus.Phone, PostOfficeID = cus.PostOfficeID, ProvinceID = cus.ProvinceID, UserLogin = "", WardID = cus.WardID }; db.BS_Customers.Add(ins); db.SaveChanges(); return(Json(new ResultInfo() { error = 0, msg = "", data = checkGroup.CustomerGroupCode }, JsonRequestBehavior.AllowGet)); }
public ActionResult create(BS_CustomerGroups customergroup) { MailerHandleCommon handle = new MailerHandleCommon(db); customergroup.CreationDate = DateTime.Now; customergroup.CustomerGroupCode = handle.GeneralCusGroupCode(); customergroup.CustomerGroupID = Guid.NewGuid().ToString(); customergroup.IsActive = true; db.BS_CustomerGroups.Add(customergroup); db.SaveChanges(); return(Json(new ResultInfo() { error = 0, msg = "", data = customergroup.CustomerGroupCode }, JsonRequestBehavior.AllowGet)); }
public ResultInfo AddMailer() { ResultInfo result = new ResultInfo() { error = 0, msg = "Them moi thanh cong" }; try { var requestContent = Request.Content.ReadAsStringAsync().Result; var jsonserializer = new JavaScriptSerializer(); var paser = jsonserializer.Deserialize <MailerIdentity>(requestContent); var findCus = db.BS_Customers.Where(p => p.CustomerCode == paser.SenderID).FirstOrDefault(); if (findCus == null) { throw new Exception("Sai thông tin"); } if (String.IsNullOrEmpty(findCus.Address) || String.IsNullOrEmpty(findCus.ProvinceID) || String.IsNullOrEmpty(findCus.DistrictID) || String.IsNullOrEmpty(findCus.CustomerName)) { throw new Exception("Cập nhật lại thông tin cá nhân"); } MailerHandleCommon mailerHandle = new MailerHandleCommon(db); var code = mailerHandle.GeneralMailerCode(findCus.PostOfficeID); var price = db.CalPrice(paser.Weight, findCus.CustomerID, paser.RecieverProvinceID, paser.MailerTypeID, findCus.PostOfficeID, DateTime.Now.ToString("yyyy-MM-dd")).FirstOrDefault(); var codPrice = 0; // theem var mailerIns = new MM_Mailers() { MailerID = code, AcceptTime = DateTime.Now, AcceptDate = DateTime.Now, COD = paser.COD, CreationDate = DateTime.Now, CurrentStatusID = 0, HeightSize = paser.HeightSize, Weight = paser.Weight, LengthSize = paser.LengthSize, WidthSize = paser.WidthSize, Quantity = paser.Quantity, PostOfficeAcceptID = findCus.PostOfficeID, CurrentPostOfficeID = findCus.PostOfficeID, EmployeeAcceptID = "", MailerDescription = paser.MailerDescription, MailerTypeID = paser.MailerTypeID, MerchandiseValue = paser.MerchandiseValue, MerchandiseID = paser.MerchandiseID, PriceDefault = price, Price = price, PriceService = paser.PriceService, Amount = price + codPrice, PriceCoD = codPrice, Notes = paser.Notes, PaymentMethodID = paser.PaymentMethodID, RecieverAddress = paser.RecieverAddress, RecieverName = paser.RecieverName, RecieverPhone = paser.RecieverPhone, RecieverDistrictID = paser.RecieverDistrictID, RecieverWardID = paser.RecieverWardID, RecieverProvinceID = paser.RecieverProvinceID, SenderID = findCus.CustomerCode, SenderAddress = findCus.Address, SenderDistrictID = findCus.DistrictID, SenderName = findCus.CustomerName, SenderPhone = findCus.Phone, SenderProvinceID = findCus.ProvinceID, SenderWardID = findCus.WardID, PaidCoD = 0, CreateType = 1 }; // db.MM_Mailers.Add(mailerIns); db.SaveChanges(); } catch (Exception e) { result.error = 1; result.msg = e.Message; } return(result); }
public ResultInfo AddCustomer() { ResponseInfo result = new ResponseInfo() { error = 0, msg = "Them moi thanh cong" }; try { var requestContent = Request.Content.ReadAsStringAsync().Result; logger.Info(requestContent); var jsonserializer = new JavaScriptSerializer(); var paser = jsonserializer.Deserialize <AddCustomerRequest>(requestContent); MailerHandleCommon handle = new MailerHandleCommon(db); var groups = new BS_CustomerGroups() { IsActive = true, ConatctPhone = paser.phone, ContactEmail = paser.email, CreationDate = DateTime.Now, CustomerGroupID = Guid.NewGuid().ToString(), CustomerGroupCode = handle.GeneralCusGroupCode(), PaymentMethodID = "money", CustomerGroupName = paser.fullName }; db.BS_CustomerGroups.Add(groups); db.SaveChanges(); // customer var code = handle.GeneralCusCode(groups.CustomerGroupCode); var ins = new BS_Customers() { CustomerID = Guid.NewGuid().ToString(), CustomerName = paser.fullName, CountryID = "VN", Address = "", CreateDate = DateTime.Now, CustomerCode = code, CustomerGroupID = groups.CustomerGroupID, Deputy = paser.fullName, DistrictID = "", Email = paser.email, IsActive = true, Phone = paser.phone, PostOfficeID = "BCQ3", ProvinceID = "", ClientUser = paser.clientUser, WardID = "" }; db.BS_Customers.Add(ins); db.SaveChanges(); result.data = code; } catch (Exception e) { result.error = 1; result.msg = e.Message; } return(result); }
public ActionResult InsertByExcel(HttpPostedFileBase files, string senderID, string senderAddress, string senderName, string senderPhone, string senderProvince, string senderDistrict, string postId) { MailerHandleCommon mailerHandle = new MailerHandleCommon(db); // List<MailerIdentity> mailers = new List<MailerIdentity>(); var result = new ResultInfo() { error = 0, msg = "Đã tải" }; string path = ""; try { // var findVSVX = db.BS_Services.Where(p => p.ServiceID == "VSVX").FirstOrDefault(); var allService = db.BS_Services.Select(p => new ItemPriceCommon() { code = p.ServiceID, name = p.ServiceName, price = p.Price, choose = false, percent = p.IsPercent }).ToList(); // check sender var checkSender = db.BS_Customers.Where(p => p.CustomerCode == senderID).FirstOrDefault(); if (checkSender == null) { throw new Exception("Sai thông tin người gửi"); } var checkSendProvince = db.BS_Provinces.Find(senderProvince); if (checkSendProvince == null) { throw new Exception("Sai thông tin tỉnh thành"); } var checkSendDistrict = db.BS_Districts.Find(senderDistrict); if (checkSendDistrict == null) { throw new Exception("Sai thông tin quận huyện "); } if (files == null || files.ContentLength <= 0) { throw new Exception("Thiếu file Excel"); } string extension = System.IO.Path.GetExtension(files.FileName); if (extension.Equals(".xlsx") || extension.Equals(".xls")) { string fileSave = "mailersupload" + DateTime.Now.ToString("ddMMyyyyhhmmss") + extension; path = Server.MapPath("~/Temps/" + fileSave); if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } files.SaveAs(path); FileInfo newFile = new FileInfo(path); var package = new ExcelPackage(newFile); ExcelWorksheet sheet = package.Workbook.Worksheets[1]; int totalRows = sheet.Dimension.End.Row; int totalCols = sheet.Dimension.End.Column; // int mailerCodeIdx = -1; int receiverIdx = -1; int receiPhoneIdx = -1; int receiAddressIdx = -1; int receiProvinceIdx = -1; int receiDistrictIdx = -1; int mailerTypeIdx = -1; int payTypeIdx = -1; int codIdx = -1; int merchandiseIdx = -1; int weigthIdx = -1; int quantityIdx = -1; int notesIdx = -1; int desIdx = -1; int vsvxIdx = -1; // lay index col tren excel for (int i = 0; i < totalCols; i++) { var colValue = Convert.ToString(sheet.Cells[1, i + 1].Value).Trim(); Regex regex = new Regex(@"\((.*?)\)"); Match match = regex.Match(colValue); if (match.Success) { string key = match.Groups[1].Value; switch (key) { case "1": mailerCodeIdx = i + 1; break; case "2": receiverIdx = i + 1; break; case "3": receiPhoneIdx = i + 1; break; case "4": receiAddressIdx = i + 1; break; case "5": receiProvinceIdx = i + 1; break; case "6": receiDistrictIdx = i + 1; break; case "8": mailerTypeIdx = i + 1; break; case "9": payTypeIdx = i + 1; break; case "10": codIdx = i + 1; break; case "11": merchandiseIdx = i + 1; break; case "12": weigthIdx = i + 1; break; case "13": quantityIdx = i + 1; break; case "17": notesIdx = i + 1; break; case "18": desIdx = i + 1; break; case "14": vsvxIdx = i + 1; break; } } } // check cac gia tri can if (receiverIdx == -1 || receiAddressIdx == -1 || receiPhoneIdx == -1 || receiProvinceIdx == -1 || weigthIdx == -1) { throw new Exception("Thiếu các cột cần thiết"); } for (int i = 2; i <= totalRows; i++) { string mailerId = mailerCodeIdx == -1 ? mailerHandle.GeneralMailerCode(postId) : Convert.ToString(sheet.Cells[i, mailerCodeIdx].Value); if (String.IsNullOrEmpty(mailerId)) { mailerId = mailerHandle.GeneralMailerCode(postId); } // string receiverPhone = Convert.ToString(sheet.Cells[i, receiPhoneIdx].Value); if (String.IsNullOrEmpty(receiverPhone)) { throw new Exception("Dòng " + (i) + " cột " + receiPhoneIdx + " : thiếu thông tin"); } // string receiver = Convert.ToString(sheet.Cells[i, receiverIdx].Value); if (String.IsNullOrEmpty(receiver)) { throw new Exception("Dòng " + (i) + " cột " + receiverIdx + " : thiếu thông tin"); } // string receiverAddress = Convert.ToString(sheet.Cells[i, receiAddressIdx].Value); if (String.IsNullOrEmpty(receiverAddress)) { throw new Exception("Dòng " + (i) + " cột " + receiAddressIdx + " : thiếu thông tin"); } // string receiverProvince = receiProvinceIdx == -1 ? "" : Convert.ToString(sheet.Cells[i, receiProvinceIdx].Value); var checkProvince = db.BS_Provinces.Where(p => p.ProvinceCode == receiverProvince).FirstOrDefault(); // string receiverDistrict = receiDistrictIdx == -1 ? "" : Convert.ToString(sheet.Cells[i, receiDistrictIdx].Value); var receiverDistrictSplit = receiverDistrict.Split('-'); var checkDistrict = db.BS_Districts.Find(receiverDistrictSplit[0]); string mailerType = Convert.ToString(sheet.Cells[i, mailerTypeIdx].Value); var checkMailerType = db.BS_ServiceTypes.Find(mailerType); // var mailerPay = payTypeIdx == -1 ? "NGTT" : Convert.ToString(sheet.Cells[i, payTypeIdx].Value); if (payTypeIdx != -1) { var checkMailerPay = db.CDatas.Where(p => p.Code == mailerPay && p.CType == "MAILERPAY").FirstOrDefault(); mailerPay = checkMailerPay == null ? "NGTT" : checkMailerPay.Code; } // COD var codValue = sheet.Cells[i, codIdx].Value; decimal cod = 0; if (codValue != null) { var isCodeNumber = codIdx == -1 ? false : Regex.IsMatch(codValue.ToString(), @"^\d+$"); cod = isCodeNumber ? Convert.ToDecimal(codValue) : 0; } // hang hoa var merchandisType = Convert.ToString(sheet.Cells[i, merchandiseIdx].Value); var checkMerchandisType = db.CDatas.Where(p => p.Code == merchandisType && p.CType == "GOODTYPE").FirstOrDefault(); if (checkMerchandisType == null) { throw new Exception("Dòng " + (i) + " cột " + merchandiseIdx + " : sai thông tin"); } // trong luong var weightValue = sheet.Cells[i, weigthIdx].Value; double weight = 0; if (weightValue == null) { throw new Exception("Dòng " + (i) + " cột " + weigthIdx + " : sai thông tin"); } else { var isWeightNumber = Regex.IsMatch(weightValue.ToString(), @"^\d+$"); weight = isWeightNumber ? Convert.ToDouble(sheet.Cells[i, weigthIdx].Value) : 0; } // so luong var quantityValue = sheet.Cells[i, quantityIdx].Value; var isQuantityNumber = quantityIdx == -1 ? false : Regex.IsMatch(quantityValue == null ? "0" : quantityValue.ToString(), @"^\d+$"); var quantity = isQuantityNumber ? Convert.ToInt32(quantityValue) : 0; // string notes = notesIdx == -1 ? "" : Convert.ToString(sheet.Cells[i, notesIdx].Value); // string describe = desIdx == -1 ? "" : Convert.ToString(sheet.Cells[i, desIdx].Value); string vsvs = vsvxIdx == -1 ? "N" : Convert.ToString(sheet.Cells[i, vsvxIdx].Value); decimal?price = 0; if (cod > 0) { price = db.CalPriceCOD(weight, senderID, checkProvince.ProvinceID, "CD", postId, DateTime.Now.ToString("yyyy-MM-dd"), vsvs == "N" ? 0 : 1, checkMailerType.ServiceID == "ST" ? "CODTK" : "CODN").FirstOrDefault(); } else { price = db.CalPrice(weight, senderID, checkProvince.ProvinceID, checkMailerType.ServiceID, postId, DateTime.Now.ToString("yyyy-MM-dd")).FirstOrDefault(); } var codPrice = 0; decimal?priceService = 0; // theem var mailerIns = new MM_Mailers() { MailerID = mailerId, AcceptTime = DateTime.Now, AcceptDate = DateTime.Now, COD = cod, CreationDate = DateTime.Now, CurrentStatusID = 0, HeightSize = 0, Weight = weight, LengthSize = 0, WidthSize = 0, Quantity = quantity, PostOfficeAcceptID = postId, CurrentPostOfficeID = postId, EmployeeAcceptID = EmployeeInfo.employeeId, MailerDescription = describe, MailerTypeID = checkMailerType != null ? checkMailerType.ServiceID : "", MerchandiseValue = cod, MerchandiseID = merchandisType, PriceDefault = price, Price = price, PriceService = priceService, Amount = price + codPrice + priceService, PriceCoD = codPrice, Notes = notes, PaymentMethodID = mailerPay, RecieverAddress = receiverAddress, RecieverName = receiver, RecieverPhone = receiverPhone, RecieverDistrictID = checkDistrict != null ? checkDistrict.DistrictID : "", RecieverWardID = "", RecieverProvinceID = checkProvince != null ? checkProvince.ProvinceID : "", SenderID = senderID, SenderAddress = senderAddress, SenderDistrictID = senderDistrict, SenderName = senderName, SenderPhone = senderPhone, SenderProvinceID = senderProvince, SenderWardID = "", PaidCoD = 0, CreateType = 0, VATPercent = 10, IsReturn = false, IsPayment = 0, IsPostAccept = false }; try { // db.MM_Mailers.Add(mailerIns); db.SaveChanges(); if (vsvs == "Y" && cod == 0) { // services.Where(p => p.code == "VSVX").FirstOrDefault().choose = true; var serviceVSVX = allService.Where(p => p.code == "VSVX").FirstOrDefault(); if (serviceVSVX.percent == true) { priceService = (price * serviceVSVX.price) / 100; } else { priceService = serviceVSVX.price; } var mailerService = new MM_MailerServices() { MailerID = mailerId, CreationDate = DateTime.Now, SellingPrice = (decimal)priceService, PriceDefault = (decimal)serviceVSVX.price, ServiceID = serviceVSVX.code }; db.MM_MailerServices.Add(mailerService); db.SaveChanges(); mailerIns.PriceService = priceService; mailerIns.Amount = mailerIns.Price + mailerIns.PriceCoD + mailerIns.PriceService; db.Entry(mailerIns).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } // luu tracking HandleHistory.AddTracking(0, mailerId, postId, "Nhận thông tin đơn hàng"); } catch (Exception e) { Console.WriteLine(e.Message); } } // xoa file temp package.Dispose(); if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } } // result.data = mailers; } catch (Exception e) { if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } result.error = 1; result.msg = e.Message; } return(Json(result, JsonRequestBehavior.AllowGet)); }
public ActionResult InsertByExcel(HttpPostedFileBase files, string senderID, string senderAddress, string senderName, string senderPhone, string senderProvince, string senderDistrict, string postId) { MailerHandleCommon mailerHandle = new MailerHandleCommon(db); List <MailerIdentity> mailers = new List <MailerIdentity>(); var result = new ResultInfo() { error = 0, msg = "Đã tải", data = mailers }; string path = ""; try { // check sender var checkSender = db.BS_Customers.Where(p => p.CustomerCode == senderID).FirstOrDefault(); if (checkSender == null) { throw new Exception("Sai thông tin người gửi"); } var checkSendProvince = db.BS_Provinces.Find(senderProvince); if (checkSendProvince == null) { throw new Exception("Sai thông tin tỉnh thành"); } var checkSendDistrict = db.BS_Districts.Find(senderDistrict); if (checkSendDistrict == null) { throw new Exception("Sai thông tin quận huyện "); } if (files == null || files.ContentLength <= 0) { throw new Exception("Thiếu file Excel"); } string extension = System.IO.Path.GetExtension(files.FileName); if (extension.Equals(".xlsx") || extension.Equals(".xls")) { string fileSave = "mailersupload" + DateTime.Now.ToString("ddMMyyyyhhmmss") + extension; path = Server.MapPath("~/Temps/" + fileSave); if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } files.SaveAs(path); FileInfo newFile = new FileInfo(path); var package = new ExcelPackage(newFile); ExcelWorksheet sheet = package.Workbook.Worksheets[1]; int totalRows = sheet.Dimension.End.Row; int totalCols = sheet.Dimension.End.Column; // int mailerCodeIdx = -1; int receiverIdx = -1; int receiPhoneIdx = -1; int receiAddressIdx = -1; int receiProvinceIdx = -1; int receiDistrictIdx = -1; int mailerTypeIdx = -1; int payTypeIdx = -1; int codIdx = -1; int merchandiseIdx = -1; int weigthIdx = -1; int quantityIdx = -1; int notesIdx = -1; int desIdx = -1; // lay index col tren excel for (int i = 0; i < totalCols; i++) { var colValue = Convert.ToString(sheet.Cells[1, i + 1].Value).Trim(); Regex regex = new Regex(@"\((.*?)\)"); Match match = regex.Match(colValue); if (match.Success) { string key = match.Groups[1].Value; switch (key) { case "1": mailerCodeIdx = i + 1; break; case "2": receiverIdx = i + 1; break; case "3": receiPhoneIdx = i + 1; break; case "4": receiAddressIdx = i + 1; break; case "5": receiProvinceIdx = i + 1; break; case "6": receiDistrictIdx = i + 1; break; case "8": mailerTypeIdx = i + 1; break; case "9": payTypeIdx = i + 1; break; case "10": codIdx = i + 1; break; case "11": merchandiseIdx = i + 1; break; case "12": weigthIdx = i + 1; break; case "13": quantityIdx = i + 1; break; case "17": notesIdx = i + 1; break; case "18": desIdx = i + 1; break; } } } // check cac gia tri can if (receiverIdx == -1 || receiAddressIdx == -1 || receiPhoneIdx == -1 || receiProvinceIdx == -1 || weigthIdx == -1) { throw new Exception("Thiếu các cột cần thiết"); } // for (int i = 2; i <= totalRows; i++) { string mailerId = mailerCodeIdx == -1 ? mailerHandle.GeneralMailerCode(postId) : Convert.ToString(sheet.Cells[i, mailerCodeIdx].Value); if (String.IsNullOrEmpty(mailerId)) { mailerId = mailerHandle.GeneralMailerCode(postId); } // string receiverPhone = Convert.ToString(sheet.Cells[i, receiPhoneIdx].Value); if (String.IsNullOrEmpty(receiverPhone)) { throw new Exception("Dòng " + (i) + " cột " + receiPhoneIdx + " : thiếu thông tin"); } // string receiver = Convert.ToString(sheet.Cells[i, receiverIdx].Value); if (String.IsNullOrEmpty(receiver)) { throw new Exception("Dòng " + (i) + " cột " + receiverIdx + " : thiếu thông tin"); } // string receiverAddress = Convert.ToString(sheet.Cells[i, receiAddressIdx].Value); if (String.IsNullOrEmpty(receiverAddress)) { throw new Exception("Dòng " + (i) + " cột " + receiAddressIdx + " : thiếu thông tin"); } // string receiverProvince = receiProvinceIdx == -1 ? "" : Convert.ToString(sheet.Cells[i, receiProvinceIdx].Value); var checkProvince = db.BS_Provinces.Where(p => p.ProvinceCode == receiverProvince).FirstOrDefault(); // string receiverDistrict = receiDistrictIdx == -1 ? "": Convert.ToString(sheet.Cells[i, receiDistrictIdx].Value); var receiverDistrictSplit = receiverDistrict.Split('-'); var checkDistrict = db.BS_Districts.Find(receiverDistrictSplit[0]); string mailerType = Convert.ToString(sheet.Cells[i, mailerTypeIdx].Value); var checkMailerType = db.BS_ServiceTypes.Find(mailerType); // var mailerPay = payTypeIdx == -1 ? "NGTT" : Convert.ToString(sheet.Cells[i, payTypeIdx].Value); if (payTypeIdx != -1) { var checkMailerPay = db.CDatas.Where(p => p.Code == mailerPay && p.CType == "MAILERPAY").FirstOrDefault(); mailerPay = checkMailerPay == null ? "NGTT" : checkMailerPay.Code; } // COD var codValue = sheet.Cells[i, codIdx].Value; decimal cod = 0; if (codValue != null) { var isCodeNumber = codIdx == -1 ? false : Regex.IsMatch(codValue.ToString(), @"^\d+$"); cod = isCodeNumber ? Convert.ToDecimal(codValue) : 0; } // hang hoa var merchandisType = Convert.ToString(sheet.Cells[i, merchandiseIdx].Value); var checkMerchandisType = db.CDatas.Where(p => p.Code == merchandisType && p.CType == "GOODTYPE").FirstOrDefault(); if (checkMerchandisType == null) { throw new Exception("Dòng " + (i) + " cột " + merchandiseIdx + " : sai thông tin"); } // trong luong var weightValue = sheet.Cells[i, weigthIdx].Value; double weight = 0; if (weightValue == null) { throw new Exception("Dòng " + (i) + " cột " + weigthIdx + " : sai thông tin"); } else { var isWeightNumber = Regex.IsMatch(weightValue.ToString(), @"^\d+$"); weight = isWeightNumber ? Convert.ToDouble(sheet.Cells[i, weigthIdx].Value) : 0; } // so luong var quantityValue = sheet.Cells[i, quantityIdx].Value; var isQuantityNumber = quantityIdx == -1 ? false : Regex.IsMatch(quantityValue == null ? "0" : quantityValue.ToString(), @"^\d+$"); var quantity = isQuantityNumber ? Convert.ToInt32(quantityValue) : 0; // string notes = notesIdx == -1 ? "" : Convert.ToString(sheet.Cells[i, notesIdx].Value); // string describe = desIdx == -1 ? "" : Convert.ToString(sheet.Cells[i, desIdx].Value); var price = db.CalPrice(weight, senderID, checkProvince.ProvinceID, checkMailerType.ServiceID, postId, DateTime.Now.ToString("yyyy-MM-dd")).FirstOrDefault(); var codPrice = 0; mailers.Add(new MailerIdentity() { MailerID = mailerId, COD = cod, PriceCoD = codPrice, HeightSize = 0, LengthSize = 0, MailerDescription = describe, MailerTypeID = checkMailerType != null ? checkMailerType.ServiceID: "", MerchandiseID = merchandisType, MerchandiseValue = cod, Notes = notes, PaymentMethodID = mailerPay, PriceDefault = price, Amount = price + codPrice, PriceService = 0, Quantity = quantity, RecieverAddress = receiverAddress, RecieverDistrictID = checkDistrict != null ? checkDistrict.DistrictID : "", RecieverName = receiver, RecieverPhone = receiverPhone, RecieverProvinceID = checkProvince != null ? checkProvince.ProvinceID : "", Weight = weight, WidthSize = 0, SenderID = senderID, SenderAddress = senderAddress, SenderDistrictID = senderDistrict, SenderName = senderName, SenderPhone = senderPhone, SenderProvinceID = senderProvince }); } // xoa file temp package.Dispose(); if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } } result.data = mailers; } catch (Exception e) { if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } result.error = 1; result.msg = e.Message; } return(Json(result, JsonRequestBehavior.AllowGet)); }
protected void btnxem_Click(object sender, EventArgs e) { if (FileUploadControl.HasFile) { try { string filename = Path.GetFileName(FileUploadControl.FileName); MailerHandleCommon mailerHandle = new MailerHandleCommon(db); mailers = new List <MailerRpt>(); // var findVSVX = db.BS_Services.Where(p => p.ServiceID == "VSVX").FirstOrDefault(); var allService = db.BS_Services.Select(p => new ItemPriceCommon() { code = p.ServiceID, name = p.ServiceName, price = p.Price, choose = false, percent = p.IsPercent }).ToList(); // check sender var checkSender = db.BS_Customers.Where(p => p.CustomerCode == cbsender.SelectedItem.Value).FirstOrDefault(); if (checkSender == null) { throw new Exception("Sai thông tin người gửi"); } var checkSendProvince = db.BS_Provinces.Find(cbProvince.SelectedItem.Value); if (checkSendProvince == null) { throw new Exception("Sai thông tin tỉnh thành"); } var checkSendDistrict = db.BS_Districts.Find(cbDistrict.SelectedItem.Value); if (checkSendDistrict == null) { throw new Exception("Sai thông tin quận huyện "); } string extension = System.IO.Path.GetExtension(filename); if (extension.Equals(".xlsx") || extension.Equals(".xls")) { string fileSave = "mailersupload" + DateTime.Now.ToString("ddMMyyyyhhmmss") + extension; string path = Server.MapPath("~/Temps/" + fileSave); if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } FileUploadControl.SaveAs(path); FileInfo newFile = new FileInfo(path); var package = new ExcelPackage(newFile); ExcelWorksheet sheet = package.Workbook.Worksheets[1]; int totalRows = sheet.Dimension.End.Row; int totalCols = sheet.Dimension.End.Column; // int mailerCodeIdx = -1; int receiverIdx = -1; int receiPhoneIdx = -1; int receiAddressIdx = -1; int receiProvinceIdx = -1; int receiDistrictIdx = -1; int mailerTypeIdx = -1; int payTypeIdx = -1; int codIdx = -1; int merchandiseIdx = -1; int weigthIdx = -1; int quantityIdx = -1; int notesIdx = -1; int desIdx = -1; int vsvxIdx = -1; // lay index col tren excel for (int i = 0; i < totalCols; i++) { var colValue = Convert.ToString(sheet.Cells[1, i + 1].Value).Trim(); Regex regex = new Regex(@"\((.*?)\)"); Match match = regex.Match(colValue); if (match.Success) { string key = match.Groups[1].Value; switch (key) { case "1": mailerCodeIdx = i + 1; break; case "2": receiverIdx = i + 1; break; case "3": receiPhoneIdx = i + 1; break; case "4": receiAddressIdx = i + 1; break; case "5": receiProvinceIdx = i + 1; break; case "6": receiDistrictIdx = i + 1; break; case "8": mailerTypeIdx = i + 1; break; case "9": payTypeIdx = i + 1; break; case "10": codIdx = i + 1; break; case "11": merchandiseIdx = i + 1; break; case "12": weigthIdx = i + 1; break; case "13": quantityIdx = i + 1; break; case "17": notesIdx = i + 1; break; case "18": desIdx = i + 1; break; case "14": vsvxIdx = i + 1; break; } } } // check cac gia tri can if (receiverIdx == -1 || receiAddressIdx == -1 || receiPhoneIdx == -1 || receiProvinceIdx == -1 || weigthIdx == -1) { throw new Exception("Thiếu các cột cần thiết"); } // for (int i = 2; i <= totalRows; i++) { string mailerId = mailerCodeIdx == -1 ? mailerHandle.GeneralMailerCode(searchText) : Convert.ToString(sheet.Cells[i, mailerCodeIdx].Value); if (String.IsNullOrEmpty(mailerId)) { mailerId = mailerHandle.GeneralMailerCode(searchText); } // string receiverPhone = Convert.ToString(sheet.Cells[i, receiPhoneIdx].Value); if (String.IsNullOrEmpty(receiverPhone)) { throw new Exception("Dòng " + (i) + " cột " + receiPhoneIdx + " : thiếu thông tin"); } // string receiver = Convert.ToString(sheet.Cells[i, receiverIdx].Value); if (String.IsNullOrEmpty(receiver)) { throw new Exception("Dòng " + (i) + " cột " + receiverIdx + " : thiếu thông tin"); } // string receiverAddress = Convert.ToString(sheet.Cells[i, receiAddressIdx].Value); if (String.IsNullOrEmpty(receiverAddress)) { throw new Exception("Dòng " + (i) + " cột " + receiAddressIdx + " : thiếu thông tin"); } // string receiverProvince = receiProvinceIdx == -1 ? "" : Convert.ToString(sheet.Cells[i, receiProvinceIdx].Value); var checkProvince = db.BS_Provinces.Where(p => p.ProvinceCode == receiverProvince).FirstOrDefault(); // string receiverDistrict = receiDistrictIdx == -1 ? "" : Convert.ToString(sheet.Cells[i, receiDistrictIdx].Value); var receiverDistrictSplit = receiverDistrict.Split('-'); var checkDistrict = db.BS_Districts.Find(receiverDistrictSplit[0]); string mailerType = Convert.ToString(sheet.Cells[i, mailerTypeIdx].Value); var checkMailerType = db.BS_ServiceTypes.Find(mailerType); // var mailerPay = payTypeIdx == -1 ? "NGTT" : Convert.ToString(sheet.Cells[i, payTypeIdx].Value); if (payTypeIdx != -1) { var checkMailerPay = db.CDatas.Where(p => p.Code == mailerPay && p.CType == "MAILERPAY").FirstOrDefault(); mailerPay = checkMailerPay == null ? "NGTT" : checkMailerPay.Code; } // COD var codValue = sheet.Cells[i, codIdx].Value; decimal cod = 0; if (codValue != null) { var isCodeNumber = codIdx == -1 ? false : Regex.IsMatch(codValue.ToString(), @"^\d+$"); cod = isCodeNumber ? Convert.ToDecimal(codValue) : 0; } // hang hoa var merchandisType = Convert.ToString(sheet.Cells[i, merchandiseIdx].Value); var checkMerchandisType = db.CDatas.Where(p => p.Code == merchandisType && p.CType == "GOODTYPE").FirstOrDefault(); if (checkMerchandisType == null) { throw new Exception("Dòng " + (i) + " cột " + merchandiseIdx + " : sai thông tin"); } // trong luong var weightValue = sheet.Cells[i, weigthIdx].Value; double weight = 0; if (weightValue == null) { throw new Exception("Dòng " + (i) + " cột " + weigthIdx + " : sai thông tin"); } else { var isWeightNumber = Regex.IsMatch(weightValue.ToString(), @"^\d+$"); weight = isWeightNumber ? Convert.ToDouble(sheet.Cells[i, weigthIdx].Value) : 0; } // so luong var quantityValue = sheet.Cells[i, quantityIdx].Value; var isQuantityNumber = quantityIdx == -1 ? false : Regex.IsMatch(quantityValue == null ? "0" : quantityValue.ToString(), @"^\d+$"); var quantity = isQuantityNumber ? Convert.ToInt32(quantityValue) : 0; // string notes = notesIdx == -1 ? "" : Convert.ToString(sheet.Cells[i, notesIdx].Value); // string describe = desIdx == -1 ? "" : Convert.ToString(sheet.Cells[i, desIdx].Value); string vsvs = vsvxIdx == -1 ? "N" : Convert.ToString(sheet.Cells[i, vsvxIdx].Value); decimal?price = 0; if (cod > 0) { price = db.CalPriceCOD(weight, checkSender.CustomerCode, checkProvince.ProvinceID, "CD", searchText, DateTime.Now.ToString("yyyy-MM-dd"), vsvs == "N" ? 0 : 1, checkMailerType.ServiceID == "ST" ? "CODTK" : "CODN").FirstOrDefault(); } else { price = db.CalPrice(weight, checkSender.CustomerCode, checkProvince.ProvinceID, checkMailerType.ServiceID, searchText, DateTime.Now.ToString("yyyy-MM-dd")).FirstOrDefault(); } var codPrice = 0; var services = new List <ItemPriceCommon>(allService); decimal?priceService = 0; if (vsvs == "Y" && cod == 0) { services.Where(p => p.code == "VSVX").FirstOrDefault().choose = true; var serviceVSVX = services.Where(p => p.code == "VSVX").FirstOrDefault(); if (serviceVSVX.percent == true) { priceService = (price * serviceVSVX.price) / 100; } else { priceService = serviceVSVX.price; } } mailers.Add(new MailerRpt() { MailerID = mailerId, SenderID = checkSender.CustomerCode, SenderAddress = "Địa chỉ: " + txtAddress.Text, SenderDistrictID = cbDistrict.SelectedValue, SenderDistrictName = cbDistrict.SelectedItem.Text, SenderProvinceName = cbProvince.SelectedItem.Text, ReceiverDistrictName = checkDistrict != null ? checkDistrict.DistrictName : "", ReceiverProvinceName = checkProvince != null ? checkProvince.ProvinceName : "", SenderName = checkSender.CustomerName, SenderPhone = txtPhone.Text, SenderProvinceID = cbProvince.SelectedValue, RecieverAddress = "Địa chỉ: " + receiverAddress, RecieverDistrictID = checkDistrict != null ? checkDistrict.DistrictID : "", RecieverName = receiver, RecieverPhone = receiverPhone, RecieverProvinceID = checkProvince != null ? checkProvince.ProvinceID : "", PostOfficeName = searchText, COD = cod.ToString("C", MNPOST.Utils.Cultures.VietNam), Weight = weight.ToString(), Quantity = quantity.ToString(), Amount = (price + priceService).Value.ToString("C", MNPOST.Utils.Cultures.VietNam), Price = price.Value.ToString("C", MNPOST.Utils.Cultures.VietNam), ServicePrice = priceService.Value.ToString("C", MNPOST.Utils.Cultures.VietNam) }); } // xoa file temp package.Dispose(); if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } LoadReport(mailers); } } catch (Exception ex) { } } else { ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Không có file để upload');", true); } }
public ResultInfo AddMailer() { ResultInfo result = new ResultInfo() { error = 0, msg = "Them moi thanh cong" }; try { var requestContent = Request.Content.ReadAsStringAsync().Result; var jsonserializer = new JavaScriptSerializer(); var paser = jsonserializer.Deserialize <MailerIdentity>(requestContent); var findCus = db.BS_Customers.Where(p => p.CustomerCode == paser.SenderID).FirstOrDefault(); if (findCus == null) { throw new Exception("Sai thông tin"); } if (String.IsNullOrEmpty(findCus.Address) || String.IsNullOrEmpty(findCus.ProvinceID) || String.IsNullOrEmpty(findCus.DistrictID) || String.IsNullOrEmpty(findCus.CustomerName)) { throw new Exception("Cập nhật lại thông tin cá nhân"); } MailerHandleCommon mailerHandle = new MailerHandleCommon(db); var code = mailerHandle.GeneralMailerCode(findCus.PostOfficeID); // var price = db.CalPrice(paser.Weight, findCus.CustomerID, paser.RecieverProvinceID, paser.MailerTypeID, findCus.PostOfficeID, DateTime.Now.ToString("yyyy-MM-dd")).FirstOrDefault(); var codPrice = 0; decimal?price = 0; if (paser.COD > 0) { var findDitrict = db.BS_Districts.Where(p => p.DistrictID == paser.RecieverDistrictID).FirstOrDefault(); int?vsvx = findDitrict == null ? 1 : (findDitrict.VSVS == true ? 1 : 0); price = db.CalPriceCOD(paser.Weight, findCus.CustomerID, paser.RecieverProvinceID, "CD", findCus.PostOfficeID, DateTime.Now.ToString("yyyy-MM-dd"), vsvx, paser.MailerTypeID == "ST" ? "CODTK" : "CODN").FirstOrDefault(); } else { price = db.CalPrice(paser.Weight, findCus.CustomerID, paser.RecieverProvinceID, paser.MailerTypeID, findCus.PostOfficeID, DateTime.Now.ToString("yyyy-MM-dd")).FirstOrDefault(); } // var checkRece = db.BS_Districts.Where(p => p.DistrictID == paser.RecieverDistrictID).FirstOrDefault(); decimal?priceService = 0; if (checkRece != null) { if (checkRece.VSVS == true) { var findService = db.BS_Services.Where(p => p.ServiceID == "VSVX").FirstOrDefault(); if (findService.IsPercent == true) { priceService = (price * findService.Price) / 100; } else { priceService = findService.Price; } var mailerService = new MM_MailerServices() { MailerID = code, CreationDate = DateTime.Now, SellingPrice = (decimal)priceService, PriceDefault = (decimal)priceService, ServiceID = "VSVX" }; db.MM_MailerServices.Add(mailerService); } } // theem var mailerIns = new MM_Mailers() { MailerID = code, AcceptTime = DateTime.Now, AcceptDate = DateTime.Now, COD = paser.COD, CreationDate = DateTime.Now, CurrentStatusID = 0, HeightSize = paser.HeightSize, Weight = paser.Weight, LengthSize = paser.LengthSize, WidthSize = paser.WidthSize, Quantity = paser.Quantity, PostOfficeAcceptID = findCus.PostOfficeID, CurrentPostOfficeID = findCus.PostOfficeID, EmployeeAcceptID = "", MailerDescription = paser.MailerDescription, MailerTypeID = paser.MailerTypeID, MerchandiseValue = paser.MerchandiseValue, MerchandiseID = paser.MerchandiseID, PriceDefault = price, Price = price, PriceService = priceService, Amount = price + codPrice + priceService, PriceCoD = codPrice, Notes = paser.Notes, PaymentMethodID = paser.PaymentMethodID, RecieverAddress = paser.RecieverAddress, RecieverName = paser.RecieverName, RecieverPhone = paser.RecieverPhone, RecieverDistrictID = paser.RecieverDistrictID, RecieverWardID = paser.RecieverWardID, RecieverProvinceID = paser.RecieverProvinceID, SenderID = findCus.CustomerCode, SenderAddress = findCus.Address, SenderDistrictID = findCus.DistrictID, SenderName = findCus.CustomerName, SenderPhone = findCus.Phone, SenderProvinceID = findCus.ProvinceID, SenderWardID = findCus.WardID, PaidCoD = 0, CreateType = 1, IsReturn = false, IsPayment = 0 }; // db.MM_Mailers.Add(mailerIns); db.SaveChanges(); } catch (Exception e) { result.error = 1; result.msg = e.Message; } return(result); }