public ActionResult UpdateMailers(MailerIdentity mailer) { var checkExist = db.MM_Mailers.Where(p => p.MailerID == mailer.MailerID).FirstOrDefault(); if (checkExist == null) { return(Json(new ResultInfo() { error = 1, msg = "Sai mã" }, JsonRequestBehavior.AllowGet)); } checkExist.SenderID = mailer.SenderID; checkExist.SenderName = mailer.SenderName; checkExist.SenderPhone = mailer.SenderPhone; checkExist.SenderAddress = mailer.SenderAddress; checkExist.SenderProvinceID = mailer.SenderProvinceID; checkExist.SenderDistrictID = mailer.SenderDistrictID; checkExist.SenderWardID = mailer.SenderWardID; checkExist.RecieverName = mailer.RecieverName; checkExist.RecieverPhone = mailer.RecieverPhone; checkExist.RecieverAddress = mailer.RecieverAddress; checkExist.RecieverProvinceID = mailer.RecieverProvinceID; checkExist.RecieverDistrictID = mailer.RecieverDistrictID; checkExist.RecieverWardID = mailer.RecieverWardID; checkExist.Weight = mailer.Weight; checkExist.LengthSize = mailer.LengthSize; checkExist.HeightSize = mailer.HeightSize; checkExist.WidthSize = mailer.WidthSize; checkExist.COD = mailer.COD; checkExist.Price = mailer.PriceDefault; checkExist.PriceDefault = mailer.PriceDefault; checkExist.PriceService = mailer.PriceService; checkExist.Quantity = mailer.Quantity; checkExist.Amount = mailer.Amount; checkExist.MailerTypeID = mailer.MailerTypeID; checkExist.MerchandiseID = mailer.MerchandiseID; checkExist.MerchandiseValue = mailer.MerchandiseValue; checkExist.PaymentMethodID = mailer.PaymentMethodID; checkExist.MailerDescription = mailer.MailerDescription; checkExist.Notes = mailer.Notes; checkExist.LastUpdateDate = DateTime.Now; db.Entry(checkExist).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); var allServices = db.MM_MailerServices.Where(p => p.MailerID == checkExist.MailerID).ToList(); foreach (var item in allServices) { db.MM_MailerServices.Remove(item); } db.SaveChanges(); // save service if (mailer.Services != null) { foreach (var service in mailer.Services) { var checkService = db.BS_Services.Where(p => p.ServiceID == service.code && p.IsActive == true).FirstOrDefault(); if (checkService != null) { var mailerService = new MM_MailerServices() { MailerID = mailer.MailerID, CreationDate = DateTime.Now, SellingPrice = (decimal)service.price, PriceDefault = (decimal)checkService.Price, ServiceID = service.code }; db.MM_MailerServices.Add(mailerService); } } db.SaveChanges(); } return(Json(new { 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)); }
//kh: listDebit, loai: $scope.CongNo.TheoNgayChot, ngaychot: $scope.ChotNgay.Ngay, thangchot: $scope.ChotNgay.Thang, tungay: $scope.CongNo.TuNgay, denngay: $scope.CongNo.DenNgay, allcus: $scope.CongNo.AllCustomer public ActionResult create(List <string> kh, int ngaychot, bool allcus) { string sysFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern; string customer = string.Empty; decimal?returncost = 0; //cap nhat chiet khau cho khach hang //exec AC_CustomerDebitVoucher_procUpdateDiscountMailer @FromDate='2018-11-28 00:00:00',@ToDate='2018-11-28 00:00:00',@PostOfficeID=N'CNTT',@PaymentMethodID=N'GN',@CustomerID=N'CNTT-2-CNTT, CNTT1-2-CNTT, HUNG-2-CNTT',@GroupByRep=0,@ByDebtDate=0 allcus = false; if (allcus == true) { var listcustomer = db.BS_CustomerGroups.Select(p => p.CustomerGroupCode).ToList(); if (listcustomer.Count == 0) { return(Json(new ResultInfo() { error = 1, msg = "Không có khách hàng thỏa điều kiện" }, JsonRequestBehavior.AllowGet)); } foreach (var item in listcustomer) { customer += item + ','; } customer = customer.Remove(customer.Length - 1); // bỏ đi dấu trừ cuối cùng } else if (allcus == false) { foreach (var item in kh) { customer += item + ','; } customer = customer.Remove(customer.Length - 1); // bỏ đi dấu trừ cuối cùng } var _tungay = new SqlParameter("@FromDate", "1900.01.01"); var _denngay = new SqlParameter("@ToDate", DateTime.Now.Date.ToString("yyyy-MM-dd")); var _post = new SqlParameter("@PostOfficeID", "BCQ3"); var _payment = new SqlParameter("@PaymentMethodID", "NGTT"); var _customerid = new SqlParameter("@CustomerID", customer); var _groupbyrep = new SqlParameter("@GroupByRep", false); var _bydebtdate = new SqlParameter("@ByDebtDate", true); var data = db.Database.SqlQuery <ReturnValue>("AC_CustomerDebitVoucher_procUpdateDiscountMailer @FromDate,@ToDate,@PostOfficeID,@PaymentMethodID,@CustomerID,@GroupByRep,@ByDebtDate", _tungay, _denngay, _post, _payment, _customerid, _groupbyrep, _bydebtdate).ToList(); //ket thuc gia trị maxid //lưu công nợ khách hàng //lay ra danh sach so CG ko co trong bang ke cong no truoc do va thoai thoi gian tìm kiem double totalamount = 0; double totalcod = 0; if (allcus == true) { var listcustomer = db.BS_CustomerGroups.Select(p => p.CustomerGroupCode).ToList(); foreach (var item in listcustomer) { var groupid = db.BS_CustomerGroups.Where(p => p.CustomerGroupCode == item).Select(p => p.CustomerGroupID).FirstOrDefault(); var listcus = db.BS_Customers.Where(p => p.CustomerGroupID == groupid).Select(p => p.CustomerCode).ToList(); string maxid = getMaxid(); foreach (var x in listcus) { var _cus = new SqlParameter("@CustID", x); var _fdate = new SqlParameter("@FromDate", "1900-01-01"); var _tdate = new SqlParameter("@ToDate", DateTime.Now.Date.ToString("yyyy-MM-dd")); var results = db.Database.SqlQuery <IdentityDebit>("get_mailerfordebit @CustID,@FromDate,@ToDate", _cus, _fdate, _tdate).ToList(); foreach (var item1 in results) { if (item1.IsReturn == true) { var checkext = db.MM_MailerServices.Where(p => p.MailerID == item1.MailerID).FirstOrDefault(); if (checkext == null) { returncost = item1.Price / 2; //nếu có chuyển hoàn. //thêm vào bảng dịch vụ cộng thêm MM_MailerServices ms = new MM_MailerServices(); ms.MailerID = item1.MailerID; ms.ServiceID = "CH"; ms.SellingPrice = decimal.Parse(returncost.ToString()); ms.PriceDefault = 0; ms.IsPercentage = true; ms.BfVATAmount = decimal.Parse((double.Parse(returncost.ToString()) * 0.9).ToString()); ms.VATAmount = decimal.Parse(returncost.ToString()) - decimal.Parse((double.Parse(returncost.ToString()) * 0.9).ToString()); ms.AfVATAmount = decimal.Parse(returncost.ToString()); ms.LastEditDate = DateTime.Now; ms.CreationDate = DateTime.Now; db.MM_MailerServices.Add(ms); //update thu khac vào bảng mm_mailers var checkmm = db.MM_Mailers.Where(p => p.MailerID == item1.MailerID).FirstOrDefault(); checkmm.PriceService = checkmm.PriceService + decimal.Parse(returncost.ToString()); db.Entry(checkmm).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } } string tttt = item1.MailerID; AC_CustomerDebitVoucherDetail ct = new AC_CustomerDebitVoucherDetail(); ct.DocumentID = maxid; ct.MailerID = item1.MailerID; ct.Amount = double.Parse((item1.Amount ?? 0).ToString()); ct.Price = decimal.Parse((double.Parse((item1.Price ?? 0).ToString()) / 1.1).ToString()); ct.PriceService = decimal.Parse((item1.PriceService ?? 0).ToString()) + decimal.Parse(returncost.ToString()); ct.DiscountPercent = decimal.Parse((item1.DiscountPercent ?? 0).ToString()); ct.Discount = decimal.Parse((item1.Discount ?? 0).ToString()); ct.VATpercent = decimal.Parse((item1.VATPercent ?? 0).ToString()); // ct.BfVATamount = decimal.Parse((item1.BfVATAmount ?? 0).ToString()); ct.BfVATamount = decimal.Round(decimal.Parse((item1.BfVATAmount ?? 0).ToString())); ct.VATamount = decimal.Round(decimal.Parse((item1.VATAmount ?? 0).ToString())); ct.AcceptDate = DateTime.Parse(item1.AcceptDate.ToString()); ct.Quantity = int.Parse(item1.Quantity.ToString()); ct.Weight = decimal.Parse(item1.Weight.ToString()); db.AC_CustomerDebitVoucherDetail.Add(ct); totalamount += double.Parse((item1.Amount ?? 0).ToString()); totalcod += double.Parse((item1.COD ?? 0).ToString()); db.SaveChanges(); } if (results.Count > 0) { //thông tin bảng master AC_CustomerDebitVoucher mt = new AC_CustomerDebitVoucher(); mt.DocumentID = maxid; mt.DocumentDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd")); mt.PostOfficeID = "BCQ3"; mt.CustomerGroupID = item; mt.StatusID = 0; mt.ToTalAmount = Math.Round(totalamount, 0); mt.CODTotal = totalcod; mt.DebtMonth = DateTime.Now.Date; mt.Description = ""; db.AC_CustomerDebitVoucher.Add(mt); db.SaveChanges(); } } } } else { foreach (var item in kh) { //lay ra danh sach ma khach hang thuoc nhom khach hang var groupid = db.BS_CustomerGroups.Where(p => p.CustomerGroupCode == item).Select(p => p.CustomerGroupID).FirstOrDefault(); var listcus = db.BS_Customers.Where(p => p.CustomerGroupID == groupid).Select(p => p.CustomerCode).ToList(); string maxid = getMaxid(); foreach (var x in listcus) { var _cus = new SqlParameter("@CustID", x); var _fdate = new SqlParameter("@FromDate", "1900-01-01"); var _tdate = new SqlParameter("@ToDate", DateTime.Now.Date.ToString("yyyy-MM-dd")); var results = db.Database.SqlQuery <IdentityDebit>("get_mailerfordebit @CustID,@FromDate,@ToDate", _cus, _fdate, _tdate).ToList(); foreach (var item1 in results) { if (item1.IsReturn == true) { var checkext = db.MM_MailerServices.Where(p => p.MailerID == item1.MailerID).FirstOrDefault(); if (checkext == null) { returncost = item1.Price / 2; //nếu có chuyển hoàn. //thêm vào bảng dịch vụ cộng thêm MM_MailerServices ms = new MM_MailerServices(); ms.MailerID = item1.MailerID; ms.ServiceID = "CH"; ms.SellingPrice = decimal.Parse(returncost.ToString()); ms.PriceDefault = 0; ms.IsPercentage = true; ms.BfVATAmount = decimal.Parse((double.Parse(returncost.ToString()) * 0.9).ToString()); ms.VATAmount = decimal.Parse(returncost.ToString()) - decimal.Parse((double.Parse(returncost.ToString()) * 0.9).ToString()); ms.AfVATAmount = decimal.Parse(returncost.ToString()); ms.LastEditDate = DateTime.Now; ms.CreationDate = DateTime.Now; db.MM_MailerServices.Add(ms); var checkmm = db.MM_Mailers.Where(p => p.MailerID == item1.MailerID).FirstOrDefault(); checkmm.PriceService = checkmm.PriceService + decimal.Parse(returncost.ToString()); db.Entry(checkmm).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } } string tttt = item1.MailerID; AC_CustomerDebitVoucherDetail ct = new AC_CustomerDebitVoucherDetail(); ct.DocumentID = maxid; ct.MailerID = item1.MailerID; ct.Amount = double.Parse((item1.Amount ?? 0).ToString()); ct.Price = decimal.Parse((double.Parse((item1.Price ?? 0).ToString()) / 1.1).ToString()); ct.PriceService = decimal.Parse((item1.PriceService ?? 0).ToString()); ct.DiscountPercent = decimal.Parse((item1.DiscountPercent ?? 0).ToString()); ct.Discount = decimal.Parse((item1.Discount ?? 0).ToString()); ct.VATpercent = decimal.Parse((item1.VATPercent ?? 0).ToString()); // ct.BfVATamount = decimal.Parse((item1.BfVATAmount ?? 0).ToString()); // ct.VATamount = decimal.Parse((item1.VATAmount ?? 0).ToString()); ct.BfVATamount = decimal.Round(decimal.Parse((item1.BfVATAmount ?? 0).ToString())); ct.VATamount = decimal.Round(decimal.Parse((item1.VATAmount ?? 0).ToString())); ct.AcceptDate = DateTime.Parse(item1.AcceptDate.ToString()); ct.Quantity = int.Parse(item1.Quantity.ToString()); ct.Weight = decimal.Parse(item1.Weight.ToString()); db.AC_CustomerDebitVoucherDetail.Add(ct); totalamount += double.Parse((item1.Amount ?? 0).ToString()); totalcod += double.Parse((item1.COD ?? 0).ToString()); db.SaveChanges(); } if (results.Count > 0) { //thông tin bảng master AC_CustomerDebitVoucher mt = new AC_CustomerDebitVoucher(); mt.DocumentID = maxid; mt.DocumentDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd")); mt.PostOfficeID = "BCQ3"; mt.CustomerGroupID = item; mt.StatusID = 0; mt.ToTalAmount = Math.Round(totalamount, 0); mt.CODTotal = totalcod; mt.DebtMonth = DateTime.Now.Date; mt.Description = ""; db.AC_CustomerDebitVoucher.Add(mt); db.SaveChanges(); } } } } return(Json(new ResultInfo() { error = 0, msg = "", data = sysFormat }, 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)); }
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); }