public async Task <IEnumerable <Applications> > GetByName(string name)
 {
     return(await _applicationService.GetByName(name));
 }
Ejemplo n.º 2
0
        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;
            }
        }