public ActionResult UploadCustomerData(UploadModel model) { try { ITransactionService tranSrv = IoC.Resolve <ITransactionService>(); EInvoiceContext Context = (EInvoiceContext)FXContext.Current; Company currentCom = Context.CurrentCompany; HttpPostedFileBase fileUpload = Request.Files["FilePath"]; if (fileUpload == null || fileUpload.ContentLength <= 0) { Messages.AddErrorFlashMessage("Chưa chọn file dữ liệu khách hàng."); return(RedirectToAction("Upload", new { TypeTran = model.TypeTrans })); } Transaction mTran = new Transaction(); byte[] bf = new byte[fileUpload.ContentLength]; fileUpload.InputStream.Read(bf, 0, fileUpload.ContentLength); if (!fileUpload.FileName.ToLower().Contains(".zip") && !fileUpload.FileName.ToLower().Contains(".xls")) { Messages.AddErrorFlashMessage("File upload phải là file .zip hoặc .xls !"); return(RedirectToAction("Upload", new { TypeTran = model.TypeTrans })); } mTran.Data = bf; mTran.ComID = currentCom.id; mTran.id = Guid.NewGuid(); mTran.InvPattern = model.Pattern; mTran.InvSerial = model.Serial; mTran.AccountName = HttpContext.User.Identity.Name; mTran.Status = TranSactionStatus.NewUpload; mTran.TypeTrans = model.TypeTrans; IPoolingService service = new PoolingService(Context); if (fileUpload.FileName.ToLower().Contains(".xls")) { //xu ly doc excell và tạo xml. IDataTranService dataTranSrv = currentCom.Config.Keys.Contains("IDataTranService") ? IoC.Resolve(Type.GetType(currentCom.Config["IDataTranService"])) as IDataTranService : new ExcelDefaultDataTranService(); string parseError = ""; string xmlData = dataTranSrv.ParseCustomer(mTran.Data, ref parseError); //update lai du lieu data xml if (null != xmlData) { mTran.Data = CompressFile(System.Text.Encoding.UTF8.GetBytes(xmlData)); } else { mTran.Status = TranSactionStatus.Failed; mTran.Messages = parseError; tranSrv.CreateNew(mTran); tranSrv.CommitChanges(); log.Info("Upload Transaction by: " + HttpContext.User.Identity.Name); Messages.AddErrorFlashMessage("Upload file khách hàng lên hệ thống không thành công, theo dõi kết quả qua mã giao dịch: " + mTran.id); return(RedirectToAction("Index", new { TypeTran = model.TypeTrans })); } } tranSrv.CreateNew(mTran); tranSrv.CommitChanges(); log.Info("Upload Customer by: " + HttpContext.User.Identity.Name + "--Begin call webservice"); service.importCus(mTran.id); Messages.AddFlashMessage("Upload file lên hệ thống thành công, theo dõi kết quả qua mã giao dịch: " + mTran.id); return(RedirectToAction("Index", new { TypeTran = model.TypeTrans })); } catch (Exception ex) { log.Error(ex); return(RedirectToAction("Upload", new { TypeTran = model.TypeTrans })); } }
public string UpdateCus(ApiPublish publish) { IuserService _userSvc = IoC.Resolve <IuserService>(); ICustomerService _cusSvc = IoC.Resolve <ICustomerService>(); IApplicationsService _appSvc = IoC.Resolve <IApplicationsService>(); IRegisterEmailService _emailSvc = IoC.Resolve <IRegisterEmailService>(); ILog log = LogManager.GetLogger(typeof(PublishController)); try { log.Info("updateCus DATA:" + publish.xmlData); //check valiadate xml XmlSchemaValidator validator = new XmlSchemaValidator(); //XMLCusData = convertSpecialCharacter(XMLCusData); if (!validator.ValidXmlDoc(publish.xmlData, "", AppDomain.CurrentDomain.BaseDirectory + @"XMLValidate\CustomerValidate.xsd")) { log.Error("updateCus ERR:3-UpdateCus " + validator.ValidationError); return("ERR:3");//du lieu dau vao khong hop le } if (publish.convert == 1) { publish.xmlData = DataHelper.convertTCVN3ToUnicode(publish.xmlData); } Applications _app = _appSvc.GetByName("EInvoice"); //Chu y fix cung phu hop voi services.config/IRBACMembershipProvider EInvoiceContext _Einvoicecontext = (EInvoiceContext)FXContext.Current; Company _currentCom = _Einvoicecontext.CurrentCompany; // get dữ liệu từ string xml XElement xelement = XElement.Parse(publish.xmlData); List <XElement> cusLst = (from item in xelement.Elements("Customer") select item).ToList(); List <string> codeLst = (from item in cusLst select item.Element("Code").Value).ToList(); string defaultPass = (_currentCom.Config.ContainsKey("SetDefaultCusPass")) ? _currentCom.Config["SetDefaultCusPass"] : "******"; string labelEmail = _currentCom.Config.Keys.Contains("LabelMail") ? _currentCom.Config["LabelMail"] : "*****@*****.**"; string portalLink = _currentCom.Config.Keys.Contains("PortalLink") ? _currentCom.Config["PortalLink"] : "http://hddt.v-invoice.vn"; _cusSvc.BeginTran(); StringBuilder msg = new StringBuilder(); foreach (var item in cusLst) { try { string code = item.Element("Code").Value.Trim(); var taxCode = Utils.formatTaxcode(item.Element("TaxCode").Value.Trim()); var cusDb = _cusSvc.Query.FirstOrDefault(x => x.Code == code && x.ComID == _currentCom.id); var userDb = _userSvc.Query.FirstOrDefault(x => x.username == code && x.GroupName == _currentCom.id.ToString()); // create if (cusDb == null) { if (userDb != null) { msg.AppendFormat("Ma KH: {0}. Khong the tao khach hang do user {0} da ton tai trong he thong.", code).AppendLine(); log.WarnFormat("Ma KH: {0}. Khong the tao khach hang do user {0} da ton tai trong he thong.", code); continue; } if (!String.IsNullOrWhiteSpace(taxCode) && _cusSvc.Query.Any(x => x.TaxCode == taxCode && x.ComID == _currentCom.id)) { msg.AppendFormat("Ma KH: {0}. Khong the tao khach hang do ma so thue {0} da ton tai trong he thong.", taxCode).AppendLine(); log.WarnFormat("Ma KH: {0}. Khong the tao khach hang do ma so thue {1} da ton tai trong he thong.", code, taxCode); continue; } var cus = new Customer { Code = code, AccountName = code, TaxCode = taxCode, Name = item.Element("Name").Value, Address = item.Element("Address").Value, BankAccountName = item.Element("BankAccountName").Value, BankName = item.Element("BankName").Value, BankNumber = item.Element("BankNumber").Value, Email = item.Element("Email").Value, Fax = item.Element("Fax").Value, Phone = item.Element("Phone").Value, ContactPerson = item.Element("ContactPerson").Value, RepresentPerson = item.Element("RepresentPerson").Value, CusType = String.IsNullOrWhiteSpace(item.Element("Name").Value) ? Int32.Parse(item.Element("Name").Value) : 0, DeliverMethod = 2, ComID = _currentCom.id }; string createCusErr; if (_cusSvc.CreateCus(cus, new Certificate(), _currentCom.id, out createCusErr)) { log.Info("updateCus Create Customer by: " + HttpContext.Current.User.Identity.Name + " Info-- TenKhachHang: " + cus.Name + " TaiKhoanKhachHang: " + cus.AccountName + " Email: " + cus.Email); // send Mail-- try { if (!string.IsNullOrEmpty(cus.Email)) { Dictionary <string, string> subjectParams = new Dictionary <string, string>(1); subjectParams.Add("$subject", ""); Dictionary <string, string> bodyParams = new Dictionary <string, string>(3); bodyParams.Add("$company", _currentCom.Name); bodyParams.Add("$cusname", cus.Name); bodyParams.Add("$username", cus.AccountName); bodyParams.Add("$password", defaultPass); bodyParams.Add("$portalLink", portalLink); _emailSvc.ProcessEmail(labelEmail, cus.Email, "RegisterCustomer", subjectParams, bodyParams); } } catch (Exception ex) { log.Warn("updateCus", ex); } } else { msg.AppendFormat("Ma KH: {0}. Khong the tao khach hang {0}. Thong bao loi: {1}", code, createCusErr).AppendLine(); log.Warn("updateCus Error: " + createCusErr); } } // update else { // update taxcode if (!String.IsNullOrWhiteSpace(taxCode) && cusDb.TaxCode != taxCode) { if (_cusSvc.Query.Any(x => x.TaxCode == taxCode && x.ComID == _currentCom.id)) { msg.AppendFormat("Ma KH: {0}. Khong the chinh sua thong tin khach hang {0} do ma so thue {1} da ton tai trong he thong.", code, taxCode).AppendLine(); continue; } cusDb.TaxCode = taxCode; } cusDb.Name = item.Element("Name").Value; cusDb.Address = item.Element("Address").Value; cusDb.BankAccountName = item.Element("BankAccountName").Value; cusDb.BankName = item.Element("BankName").Value; cusDb.BankNumber = item.Element("BankNumber").Value; cusDb.Email = item.Element("Email").Value; cusDb.Fax = item.Element("Fax").Value; cusDb.Phone = item.Element("Phone").Value; cusDb.ContactPerson = item.Element("ContactPerson").Value; cusDb.RepresentPerson = item.Element("RepresentPerson").Value; cusDb.CusType = String.IsNullOrWhiteSpace(item.Element("Name").Value) ? Int32.Parse(item.Element("Name").Value) : 0; } } catch (Exception ex) { _cusSvc.RolbackTran(); log.Error("Loi tao hoac cap nhat khach hang: " + item.Element("Code"), ex); return("ERR:2");//loi cap nhat khach hang le vao csdl } } _cusSvc.CommitTran(); return("OK:" + msg); } catch (Exception ex) { log.Error("updateCus: " + ex); throw ex; } }
public ActionResult UploadInvoiceCancelData(UploadModel model) { try { ITransactionService tranSrv = IoC.Resolve <ITransactionService>(); EInvoiceContext Context = (EInvoiceContext)FXContext.Current; Company currentCom = Context.CurrentCompany; HttpPostedFileBase fileUpload = Request.Files["FilePath"]; HttpPostedFileBase fileQDUpload = Request.Files["FileQDPath"]; if (fileUpload == null || fileUpload.ContentLength == 0) { Messages.AddErrorFlashMessage("Chưa chọn file dữ liệu hủy."); return(RedirectToAction("Upload", new { TypeTran = model.TypeTrans })); } if (fileQDUpload == null || fileQDUpload.ContentLength == 0) { Messages.AddErrorFlashMessage("Chưa chọn file quyết định hủy."); return(RedirectToAction("Upload", model)); } byte[] qdinhHuybuffer = new byte[fileQDUpload.ContentLength]; fileQDUpload.InputStream.Read(qdinhHuybuffer, 0, fileQDUpload.ContentLength); if (!fileQDUpload.FileName.ToLower().Contains(".jpg") && !fileQDUpload.FileName.ToLower().Contains(".png")) { Messages.AddErrorFlashMessage("File QĐ upload phải là file .jpg hoặc .png!"); return(RedirectToAction("Upload", new { TypeTran = model.TypeTrans })); } byte[] dulieuhuyBuffer = new byte[fileUpload.ContentLength]; fileUpload.InputStream.Read(dulieuhuyBuffer, 0, fileUpload.ContentLength); if (!fileUpload.FileName.ToLower().Contains(".zip") && !fileUpload.FileName.ToLower().Contains(".xls")) { Messages.AddErrorFlashMessage("File upload phải là file .zip hoặc .xls."); return(RedirectToAction("Upload", new { TypeTran = model.TypeTrans })); } Transaction mTran = new Transaction(); mTran.Data = dulieuhuyBuffer; mTran.ComID = currentCom.id; mTran.id = Guid.NewGuid(); mTran.InvPattern = model.Pattern; mTran.InvSerial = model.Serial; mTran.AccountName = HttpContext.User.Identity.Name; mTran.Status = TranSactionStatus.NewUpload; mTran.TypeTrans = model.TypeTrans; string fullPath = ""; //string filePath = WebConfigurationManager.AppSettings["InvUnReleaseDecisionDir"] string filePath = AppDomain.CurrentDomain.BaseDirectory + WebConfigurationManager.AppSettings["InvUnReleaseDecisionDir"]; if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } if (!filePath.EndsWith(@"\")) { filePath += @"\"; } fullPath = filePath + fileUpload.FileName; System.IO.File.Delete(fullPath); System.IO.File.WriteAllBytes(fullPath, qdinhHuybuffer); mTran.AttachedFile = fullPath; mTran.Reason = model.ReasonDel; if (fileUpload.FileName.ToLower().Contains(".xls")) { //xu ly doc excell và tạo xml. IDataTranService dataTranSrv = currentCom.Config.Keys.Contains("IDataTranService") ? IoC.Resolve(Type.GetType(currentCom.Config["IDataTranService"])) as IDataTranService : new ExcelDefaultDataTranService(); string parseError = ""; string xmlData = dataTranSrv.ParseIInvoiceCancel(mTran.Data, ref parseError); //update lai du lieu data xml if (null != xmlData) { mTran.Data = CompressFile(System.Text.Encoding.UTF8.GetBytes(xmlData)); } else { mTran.Status = TranSactionStatus.Failed; mTran.Messages = parseError; tranSrv.CreateNew(mTran); tranSrv.CommitChanges(); log.Info("Upload Transaction by: " + HttpContext.User.Identity.Name); Messages.AddErrorFlashMessage("Upload file khách hàng lên hệ thống không thành công, theo dõi kết quả qua mã giao dịch: " + mTran.id); return(RedirectToAction("Index", new { TypeTran = model.TypeTrans })); } } tranSrv.CreateNew(mTran); tranSrv.CommitChanges(); log.Info("Upload Transaction by: " + HttpContext.User.Identity.Name + "--Begin call webservice"); IPoolingService service = new PoolingService(Context); service.cancelInv(mTran.id); Messages.AddFlashMessage("Upload file lên hệ thống thành công, theo dõi kết quả qua mã giao dịch: " + mTran.id); return(RedirectToAction("Index", new { TypeTran = model.TypeTrans })); } catch (Exception ex) { log.Error(ex); return(RedirectToAction("Upload", new { TypeTran = model.TypeTrans })); } }