public ActionResult CancelMailers(List <string> mailers, string reason) { foreach (var item in mailers) { var findMailer = db.MM_Mailers.Find(item); if (findMailer != null && findMailer.CurrentStatusID == 0) { // moi tao moi dc huy findMailer.CurrentStatusID = 10; findMailer.LastUpdateDate = DateTime.Now; findMailer.StatusNotes = reason; db.Entry(findMailer).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); HandleHistory.AddTracking(10, item, findMailer.CurrentPostOfficeID, "Hủy đơn với lý do " + reason); } } return(Json(new ResultInfo() { error = 0, msg = "" }, JsonRequestBehavior.AllowGet)); }
public ActionResult AddMailers(List <string> mailers, string postId) { List <string> listAdds = new List <string>(); foreach (var item in mailers) { var find = db.MM_Mailers.Where(p => p.MailerID == item && p.PostOfficeAcceptID == postId).FirstOrDefault(); if (find != null && (find.CurrentStatusID == 0 || find.CurrentStatusID == 8)) { find.CurrentStatusID = 2; // nhap kho find.IsPostAccept = true; find.LastUpdateDate = DateTime.Now; db.Entry(find).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); HandleHistory.AddTracking(2, item, find.CurrentPostOfficeID, "Đã nhận hàng và lưu kho"); listAdds.Add(item); } } return(Json(new ResultInfo() { error = 0, data = listAdds }, JsonRequestBehavior.AllowGet)); }
public ActionResult UpdateTakeDetails(string documentID, List <string> mailers) { var checkDocument = db.MM_TakeMailers.Find(documentID); if (checkDocument == null) { return(Json(new ResultInfo() { error = 1, msg = "Sai thông tin" }, JsonRequestBehavior.AllowGet)); } foreach (var item in mailers) { var checkMailer = db.MM_Mailers.Find(item); if (checkMailer == null) { continue; } var findDetail = db.MM_TakeDetails.Where(p => p.DocumentID == documentID && p.MailerID == item).FirstOrDefault(); if (findDetail == null) { continue; } findDetail.StatusID = 8; findDetail.TimeTake = DateTime.Now; db.Entry(findDetail).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); checkMailer.CurrentStatusID = 8; checkMailer.LastUpdateDate = DateTime.Now; db.Entry(checkMailer).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); HandleHistory.AddTracking(8, item, checkMailer.CurrentPostOfficeID, "Đã lấy hàng, đang giao về kho"); } var checkCount = db.TAKEMAILER_GETDETAILs(documentID).Where(p => p.CurrentStatusID == 7).ToList(); if (checkCount.Count() == 0) { checkDocument.StatusID = 8; db.Entry(checkDocument).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } return(Json(new ResultInfo() { error = 0 }, JsonRequestBehavior.AllowGet)); }
public ActionResult ConfirmDeliveyMailer(MailerDeliveryConfirmInfo detail) { var findDetail = db.MM_MailerDeliveryDetail.Where(p => p.Id == detail.DetailId).FirstOrDefault(); if (findDetail != null) { var mailerInfo = db.MM_Mailers.Find(findDetail.MailerID); var findDocument = db.MM_MailerDelivery.Where(p => p.DocumentID == detail.DocumentID).FirstOrDefault(); if (findDocument == null) { return(Json(new ResultInfo() { error = 1, msg = "Sai thông tin" }, JsonRequestBehavior.AllowGet)); } findDetail.DeliveryStatus = detail.DeliveryStatus; mailerInfo.CurrentStatusID = detail.DeliveryStatus; var deliveryDate = DateTime.ParseExact(detail.DeliveryDate + " " + detail.DeliveryTime, "dd/M/yyyy HH:mm", null); if (deliveryDate == null) { deliveryDate = DateTime.Now; } if (detail.DeliveryStatus == 5) { findDetail.DeliveryTo = ""; findDetail.DeliveryNotes = detail.DeliveryNotes; findDetail.ReturnReasonID = detail.ReturnReasonID; findDetail.DeliveryDate = deliveryDate; mailerInfo.DeliveryTo = ""; mailerInfo.DeliveryDate = deliveryDate; mailerInfo.DeliveryNotes = detail.DeliveryNotes; mailerInfo.IsReturn = true; HandleHistory.AddTracking(5, detail.MailerID, mailerInfo.CurrentPostOfficeID, "Trả lại hàng, vì lý do " + detail.DeliveryNotes); } else if (detail.DeliveryStatus == 6) { findDetail.DeliveryTo = ""; findDetail.DeliveryNotes = detail.DeliveryNotes; findDetail.DeliveryDate = deliveryDate; mailerInfo.DeliveryTo = ""; mailerInfo.DeliveryDate = deliveryDate; mailerInfo.DeliveryNotes = detail.DeliveryNotes; HandleHistory.AddTracking(6, detail.MailerID, mailerInfo.CurrentPostOfficeID, "Chưa phát được vì " + detail.DeliveryNotes); } else if (detail.DeliveryStatus == 4) { findDetail.DeliveryTo = detail.DeliveryTo; findDetail.ReturnReasonID = null; findDetail.DeliveryNotes = "Đã phát"; findDetail.DeliveryDate = deliveryDate; mailerInfo.DeliveryTo = detail.DeliveryTo; mailerInfo.DeliveryDate = deliveryDate; mailerInfo.DeliveryNotes = "Đã phát"; mailerInfo.CurrentStatusID = 4; if (mailerInfo.IsReturn == true) { findDetail.DeliveryNotes = "Đã hoàn - Người gửi thanh toán cước"; mailerInfo.DeliveryNotes = "Đã hoàn - Người gửi thanh toán cước"; findDetail.DeliveryStatus = 11; mailerInfo.CurrentStatusID = 11; mailerInfo.PaymentMethodID = "NGTT"; HandleHistory.AddTracking(11, detail.MailerID, mailerInfo.CurrentPostOfficeID, "Ngày hoàn " + deliveryDate.ToString("dd/MM/yyyy") + " lúc " + deliveryDate.ToString("HH:mm") + ", người nhận: " + detail.DeliveryTo); } else { HandleHistory.AddTracking(4, detail.MailerID, mailerInfo.CurrentPostOfficeID, "Ngày phát " + deliveryDate.ToString("dd/MM/yyyy") + " lúc " + deliveryDate.ToString("HH:mm") + ", người nhận: " + detail.DeliveryTo); // save nhung don co thu tien COD if (mailerInfo.PaymentMethodID == "NNTT") { var saveCoDDebit = new EmpployeeDebitCOD() { Id = Guid.NewGuid().ToString(), AccountantConfirm = 0, COD = Convert.ToDouble(mailerInfo.COD) + Convert.ToDouble(mailerInfo.Amount), Describe = "Thu: Cước + COD", ConfirmDate = DateTime.Now, CreateDate = DateTime.Now, DocumentID = detail.DocumentID, EmployeeID = findDocument.EmployeeID, MailerID = mailerInfo.MailerID }; db.EmpployeeDebitCODs.Add(saveCoDDebit); } else { if (mailerInfo.COD > 0) { var saveCoDDebit = new EmpployeeDebitCOD() { Id = Guid.NewGuid().ToString(), AccountantConfirm = 0, Describe = "Thu: COD", COD = Convert.ToDouble(mailerInfo.COD), ConfirmDate = DateTime.Now, CreateDate = DateTime.Now, DocumentID = detail.DocumentID, EmployeeID = findDocument.EmployeeID, MailerID = mailerInfo.MailerID }; db.EmpployeeDebitCODs.Add(saveCoDDebit); } } } } mailerInfo.IsPostAccept = false; db.Entry(mailerInfo).State = System.Data.Entity.EntityState.Modified; db.Entry(findDetail).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); return(Json(new ResultInfo() { error = 0, msg = "" }, JsonRequestBehavior.AllowGet)); } return(Json(new ResultInfo() { error = 1, msg = "Sai thông tin" }, JsonRequestBehavior.AllowGet)); }
public ActionResult CreateFromRoutes(List <EmployeeAutoRouteInfo> routes, string postId) { DateTime date = DateTime.Now; foreach (var item in routes) { if (item.Mailers.Count() == 0) { continue; } var documentCod = postId + date.ToString("ddMMyyyy"); // var findDocument = db.MM_MailerDelivery.Where(p => p.DocumentCode == documentCod && p.EmployeeID == item.EmployeeID).FirstOrDefault(); var findEmployee = db.BS_Employees.Find(item.EmployeeID); if (findDocument == null) { findDocument = new MM_MailerDelivery() { DocumentID = Guid.NewGuid().ToString(), DocumentCode = documentCod, CreateDate = date, DocumentDate = date, EmployeeID = item.EmployeeID, LastEditDate = DateTime.Now, Notes = "", NumberPlate = "", Quantity = 0, StatusID = 0, Weight = 0, PostID = postId }; db.MM_MailerDelivery.Add(findDocument); db.SaveChanges(); } // add to detail foreach (var mailer in item.Mailers) { var checkMailer = db.MM_Mailers.Where(p => p.MailerID == mailer.MailerID && (p.CurrentStatusID == 2 || p.CurrentStatusID == 6 || p.CurrentStatusID == 5)).FirstOrDefault(); if (checkMailer != null) { if (checkMailer.CurrentStatusID == 6 || checkMailer.CurrentStatusID == 5) { if (checkMailer.IsPostAccept == false) { continue; } } // dang phat var check = db.MM_MailerDeliveryDetail.Where(p => p.MailerID == mailer.MailerID && p.DocumentID == findDocument.DocumentID && p.DeliveryStatus == 3).FirstOrDefault(); if (check != null) { continue; } var insData = new MM_MailerDeliveryDetail() { Id = Guid.NewGuid().ToString(), DocumentID = findDocument.DocumentID, MailerID = checkMailer.MailerID, CreationDate = DateTime.Now, DeliveryStatus = 3, }; db.MM_MailerDeliveryDetail.Add(insData); checkMailer.CurrentStatusID = 3; checkMailer.LastUpdateDate = DateTime.Now; checkMailer.IsPostAccept = false; db.Entry(checkMailer).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); HandleHistory.AddTracking(3, checkMailer.MailerID, checkMailer.CurrentPostOfficeID, "Nhân viên " + findEmployee.EmployeeName + "(" + findEmployee.Phone + ") , đang đi phát hàng"); } } MailerHandle.SendNotifi("Phát hàng", "Có đơn phát mới phát", findEmployee.UserLogin); } return(Json(new { }, JsonRequestBehavior.AllowGet)); }
public ActionResult AddMailer(string mailerId, string documentId) { var mailer = db.MM_Mailers.Find(mailerId); if (mailer == null) { return(Json(new ResultInfo() { error = 1, msg = "Sai thông tin" }, JsonRequestBehavior.AllowGet)); } if (mailer.CurrentStatusID != 2 && mailer.CurrentStatusID != 6 && mailer.CurrentStatusID != 5) { return(Json(new ResultInfo() { error = 1, msg = "Mã hàng không thể phân phát" }, JsonRequestBehavior.AllowGet)); } if (mailer.CurrentStatusID == 6 || mailer.CurrentStatusID == 5) { if (mailer.IsPostAccept == false) { return(Json(new ResultInfo() { error = 1, msg = "Mã hàng không thể phân phát vì bưu cục chưa xác nhận nhận hàng" }, JsonRequestBehavior.AllowGet)); } } var delivery = db.MM_MailerDelivery.Find(documentId); if (delivery == null || (delivery.DocumentDate.Value.ToString("ddMMyyyy") != DateTime.Now.ToString("ddMMyyyy"))) { return(Json(new ResultInfo() { error = 1, msg = "Không thể phân phát" }, JsonRequestBehavior.AllowGet)); } try { // dang phat var check = db.MM_MailerDeliveryDetail.Where(p => p.MailerID == mailerId && p.DocumentID == documentId && p.DeliveryStatus == 3).FirstOrDefault(); if (check != null) { throw new Exception("Không thể phân phát"); } var insData = new MM_MailerDeliveryDetail() { Id = Guid.NewGuid().ToString(), DocumentID = documentId, MailerID = mailerId, CreationDate = DateTime.Now, DeliveryStatus = 3 }; db.MM_MailerDeliveryDetail.Add(insData); db.SaveChanges(); mailer.CurrentStatusID = 3; mailer.IsPostAccept = false; mailer.LastUpdateDate = DateTime.Now; db.Entry(mailer).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); // luu tracking var employee = db.BS_Employees.Where(p => p.EmployeeID == delivery.EmployeeID).FirstOrDefault(); HandleHistory.AddTracking(3, mailerId, mailer.CurrentPostOfficeID, "Nhân viên " + employee.EmployeeName + "(" + employee.Phone + ") , đang đi phát hàng"); var data = db.MAILERDELIVERY_GETMAILER_BY_ID(insData.Id).FirstOrDefault(); MailerHandle.SendNotifi("Phát hàng", "Có 1 đơn phát hàng mới: " + mailer.MailerID, employee.UserLogin); return(Json(new ResultInfo() { error = 0, msg = "", data = data }, JsonRequestBehavior.AllowGet)); } catch (Exception e) { return(Json(new ResultInfo() { error = 1, msg = "Lỗi cập nhật" }, JsonRequestBehavior.AllowGet)); } }
public ActionResult AddListMailer(List <string> mailers, string documentId) { var delivery = db.MM_MailerDelivery.Find(documentId); var employee = db.BS_Employees.Where(p => p.EmployeeID == delivery.EmployeeID).FirstOrDefault(); if (delivery == null || (delivery.DocumentDate.Value.ToString("ddMMyyyy") != DateTime.Now.ToString("ddMMyyyy"))) { return(Json(new ResultInfo() { error = 1, msg = "Không thể phân phát" }, JsonRequestBehavior.AllowGet)); } foreach (var item in mailers) { var mailer = db.MM_Mailers.Find(item); if (mailer == null) { continue; } if (mailer.CurrentStatusID != 2 && mailer.CurrentStatusID != 6 && mailer.CurrentStatusID != 5) { continue; } // dang phat var check = db.MM_MailerDeliveryDetail.Where(p => p.MailerID == item && p.DocumentID == documentId && p.DeliveryStatus == 3).FirstOrDefault(); if (check != null) { throw new Exception("Không thể phân phát"); } var insData = new MM_MailerDeliveryDetail() { Id = Guid.NewGuid().ToString(), DocumentID = documentId, MailerID = item, CreationDate = DateTime.Now, DeliveryStatus = 3, }; db.MM_MailerDeliveryDetail.Add(insData); db.SaveChanges(); mailer.CurrentStatusID = 3; mailer.LastUpdateDate = DateTime.Now; db.Entry(mailer).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); HandleHistory.AddTracking(3, item, mailer.CurrentPostOfficeID, "Nhân viên " + employee.EmployeeName + "(" + employee.Phone + ") , đang đi phát hàng"); } return(Json(new ResultInfo() { error = 0, msg = "" }, JsonRequestBehavior.AllowGet)); }
public ActionResult CreateTakeMailer(string postId, string cusCode, string cusName, string cusAddress, string cusPhone, string content, string employeeId, List <string> mailers) { if (mailers == null || mailers.Count() == 0) { return(Json(new ResultInfo() { error = 1, msg = "Không có đơn nào để lấy" }, JsonRequestBehavior.AllowGet)); } var findEmployee = db.BS_Employees.Where(p => p.EmployeeID == employeeId).FirstOrDefault(); if (findEmployee == null) { return(Json(new ResultInfo() { error = 1, msg = "Sai thông tin nhân viên lấy hàng" }, JsonRequestBehavior.AllowGet)); } // tim ma document co trung id var documentCode = DateTime.Now.ToString("ddMMyyyy"); var findDocument = db.MM_TakeMailers.Where(p => p.EmployeeID == findEmployee.EmployeeID && p.CustomerID == cusCode && p.DocumentCode == documentCode).FirstOrDefault(); if (findDocument == null) { findDocument = new MM_TakeMailers() { DocumentID = Guid.NewGuid().ToString(), Content = content, CreateTime = DateTime.Now, CustomerAddress = cusAddress, CustomerID = cusCode, CustomerName = cusName, CustomerPhone = cusPhone, UserCreate = EmployeeInfo.user, EmployeeID = employeeId, StatusID = 7, PostID = postId, DocumentCode = DateTime.Now.ToString("ddMMyyyy") }; db.MM_TakeMailers.Add(findDocument); db.SaveChanges(); } else { findDocument.StatusID = 7; findDocument.Content = content; db.Entry(findDocument).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } foreach (var item in mailers) { var findMailer = db.MM_Mailers.Find(item); if (findMailer != null && findMailer.CurrentStatusID == 0) { var deltail = new MM_TakeDetails() { DocumentID = findDocument.DocumentID, MailerID = item, StatusID = 7, TimeTake = null }; db.MM_TakeDetails.Add(deltail); db.SaveChanges(); findMailer.CurrentStatusID = 7; findMailer.LastUpdateDate = DateTime.Now; db.Entry(findMailer).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); // luu tracking HandleHistory.AddTracking(7, item, postId, "Giao nhân viên " + findEmployee.EmployeeName + " số điện thoại " + findEmployee.Phone + " , đi lấy hàng"); } } MailerHandle.SendNotifi("Lấy hàng", "Lấy hàng tại " + findDocument.CustomerName, findEmployee.UserLogin); return(Json(new ResultInfo() { error = 0 }, 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" }; 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 InsertMailers(List <MailerIdentity> mailers, string postId) { if (mailers == null) { return(Json(new { error = 1, msg = "Hoàn thành" }, JsonRequestBehavior.AllowGet)); } if (mailers.Count() > 300) { return(Json(new { error = 1, msg = "Để đảm bảo hệ thống chỉ update 300/1 lần" }, JsonRequestBehavior.AllowGet)); } var checkPost = db.BS_PostOffices.Find(postId); if (checkPost == null) { return(Json(new { error = 1, msg = "chọn bưu cục" }, JsonRequestBehavior.AllowGet)); } List <MailerIdentity> insertFail = new List <MailerIdentity>(); foreach (var item in mailers) { // checkMailer if (String.IsNullOrEmpty(item.MailerID)) { insertFail.Add(item); continue; } var checkExist = db.MM_Mailers.Where(p => p.MailerID == item.MailerID).FirstOrDefault(); if (checkExist != null) { insertFail.Add(item); continue; } var checkSender = db.BS_Customers.Where(p => p.CustomerCode == item.SenderID).FirstOrDefault(); if (checkSender == null) { insertFail.Add(item); continue; } decimal?price = 0; if (item.COD > 0) { var findDitrict = db.BS_Districts.Where(p => p.DistrictID == item.RecieverDistrictID).FirstOrDefault(); int?vsvx = findDitrict == null ? 1 : (findDitrict.VSVS == true ? 1 : 0); price = db.CalPriceCOD(item.Weight, checkSender.CustomerID, item.RecieverProvinceID, "CD", postId, DateTime.Now.ToString("yyyy-MM-dd"), vsvx, item.MailerTypeID == "ST" ? "CODTK" : "CODN").FirstOrDefault(); } else { price = db.CalPrice(item.Weight, checkSender.CustomerID, item.RecieverProvinceID, item.MailerTypeID, postId, DateTime.Now.ToString("yyyy-MM-dd")).FirstOrDefault(); } var codPrice = 0; // theem var mailerIns = new MM_Mailers() { MailerID = item.MailerID, AcceptTime = DateTime.Now, AcceptDate = DateTime.Now, COD = item.COD, CreationDate = DateTime.Now, CurrentStatusID = 0, HeightSize = item.HeightSize, Weight = item.Weight, LengthSize = item.LengthSize, WidthSize = item.WidthSize, Quantity = item.Quantity, PostOfficeAcceptID = postId, CurrentPostOfficeID = postId, EmployeeAcceptID = EmployeeInfo.employeeId, MailerDescription = item.MailerDescription, MailerTypeID = item.MailerTypeID, MerchandiseValue = item.MerchandiseValue, MerchandiseID = item.MerchandiseID, PriceDefault = price, Price = price, PriceService = item.PriceService, Amount = price + codPrice + item.PriceService, PriceCoD = codPrice, Notes = item.Notes, PaymentMethodID = item.PaymentMethodID, RecieverAddress = item.RecieverAddress, RecieverName = item.RecieverName, RecieverPhone = item.RecieverPhone, RecieverDistrictID = item.RecieverDistrictID, RecieverWardID = item.RecieverWardID, RecieverProvinceID = item.RecieverProvinceID, SenderID = item.SenderID, SenderAddress = item.SenderAddress, SenderDistrictID = item.SenderDistrictID, SenderName = item.SenderName, SenderPhone = item.SenderPhone, SenderProvinceID = item.SenderProvinceID, SenderWardID = item.SenderWardID, PaidCoD = 0, CreateType = 0, VATPercent = 10, IsReturn = false, IsPayment = 0, IsPostAccept = false }; try { // db.MM_Mailers.Add(mailerIns); db.SaveChanges(); // add addressTemp var findAddressTemp = db.AddressTemps.Where(p => p.Phone == mailerIns.RecieverPhone).FirstOrDefault(); if (findAddressTemp != null) { findAddressTemp.AddressInfo = mailerIns.RecieverAddress; findAddressTemp.DistrictId = mailerIns.RecieverDistrictID; findAddressTemp.ProvinceId = mailerIns.RecieverProvinceID; findAddressTemp.WardId = mailerIns.RecieverWardID; findAddressTemp.Name = mailerIns.RecieverName; db.Entry(findAddressTemp).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } else { var insAddressInfo = new AddressTemp() { Id = Guid.NewGuid().ToString(), Name = mailerIns.RecieverName, Phone = mailerIns.RecieverPhone, AddressInfo = mailerIns.RecieverAddress, DistrictId = mailerIns.RecieverDistrictID, ProvinceId = mailerIns.RecieverProvinceID, WardId = mailerIns.RecieverWardID }; db.AddressTemps.Add(insAddressInfo); db.SaveChanges(); } // save service if (item.Services != null) { decimal?totalPriceService = 0; foreach (var service in item.Services) { var checkService = db.BS_Services.Where(p => p.ServiceID == service.code && p.IsActive == true).FirstOrDefault(); if (checkService != null && service.choose == true) { var servicePrice = service.price; if (service.percent == true) { servicePrice = (servicePrice * mailerIns.Price) / 100; } totalPriceService = totalPriceService + servicePrice; var mailerService = new MM_MailerServices() { MailerID = item.MailerID, CreationDate = DateTime.Now, SellingPrice = (decimal)servicePrice, PriceDefault = (decimal)checkService.Price, ServiceID = service.code }; db.MM_MailerServices.Add(mailerService); } } db.SaveChanges(); mailerIns.PriceService = totalPriceService; mailerIns.Amount = mailerIns.Price + mailerIns.PriceCoD + mailerIns.PriceService; db.Entry(mailerIns).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } // luu tracking HandleHistory.AddTracking(0, item.MailerID, postId, "Nhận thông tin đơn hàng"); } catch { insertFail.Add(item); continue; } } return(Json(new { error = 0, data = insertFail }, JsonRequestBehavior.AllowGet)); }