/// <summary> /// convert foundry to view model /// </summary> /// <param name="foundry"></param> /// <returns></returns> public FoundryViewModel ConvertToView(PM00200_Foundry foundry) { FoundryViewModel model = new FoundryViewModel(); var _paymentTermRepository = new PaymentTermRepository(); var _shipmentTermRepository = new ShipmentTermRepository(); var _stateRepository = new StateRepository(); var _countryRepository = new CountryRepository(); var state = _stateRepository.GetState(foundry.STATE); var country = _countryRepository.GetCountry(foundry.COUNTRY); var paymentTerm = _paymentTermRepository.GetPaymentTerm(foundry.PYMTRMID); model.FoundryId = foundry.VENDORID; model.VendorId = foundry.VENDORID; model.ShortName = (!string.IsNullOrEmpty(foundry.VENDSHNM.Replace(" ", string.Empty))) ? foundry.VENDSHNM : "N/A"; model.ContactName = (!string.IsNullOrEmpty(foundry.VNDCNTCT.Replace(" ", string.Empty))) ? foundry.VNDCNTCT : "N/A"; model.ContactPhone = FormattingManager.FormatPhone(foundry.PHNUMBR1); model.FaxNumber = FormattingManager.FormatPhone(foundry.FAXNUMBR); model.Address1 = (!string.IsNullOrEmpty(foundry.ADDRESS1.Replace(" ", string.Empty))) ? foundry.ADDRESS1 : "N/A"; model.Address2 = (!string.IsNullOrEmpty(foundry.ADDRESS2.Replace(" ", string.Empty))) ? foundry.ADDRESS2 : "N/A"; model.Address3 = (!string.IsNullOrEmpty(foundry.ADDRESS3.Replace(" ", string.Empty))) ? foundry.ADDRESS3 : "N/A"; model.City = (!string.IsNullOrEmpty(foundry.CITY.Replace(" ", string.Empty))) ? foundry.CITY : "N/A"; model.StateName = (state != null && !string.IsNullOrEmpty(state.Name)) ? state.Name : "N/A"; model.PostalCode = (!string.IsNullOrEmpty(foundry.ZIPCODE.Replace(" ", string.Empty))) ? foundry.ZIPCODE : "N/A"; model.CountryName = (country != null && !string.IsNullOrEmpty(country.Name)) ? country.Name : "N/A"; model.PaymentTermDescription = (paymentTerm != null && !string.IsNullOrEmpty(paymentTerm.Description)) ? paymentTerm.Description : "N/A"; model.IsActive = foundry.VENDSTTS != 1 ? false : true; if (_paymentTermRepository != null) { _paymentTermRepository.Dispose(); _paymentTermRepository = null; } if (_shipmentTermRepository != null) { _shipmentTermRepository.Dispose(); _shipmentTermRepository = null; } if (_stateRepository != null) { _stateRepository.Dispose(); _stateRepository = null; } if (_countryRepository != null) { _countryRepository.Dispose(); _countryRepository = null; } return(model); }
/// <summary> /// convert foundry to list model /// </summary> /// <param name="foundry"></param> /// <returns></returns> public FoundryViewModel ConvertToListView(PM00200_Foundry foundry) { FoundryViewModel model = new FoundryViewModel(); model.FoundryId = foundry.VENDORID; model.VendorId = foundry.VENDORID; model.ShortName = (!string.IsNullOrEmpty(foundry.VENDSHNM.Replace(" ", string.Empty))) ? foundry.VENDSHNM : "N/A"; model.ContactName = (!string.IsNullOrEmpty(foundry.VNDCNTCT.Replace(" ", string.Empty))) ? foundry.VNDCNTCT : "N/A"; model.IsActive = foundry.VENDSTTS != 1 ? false : true; return(model); }
/// <summary> /// get foundry by id /// </summary> /// <param name="vendorId"></param> /// <returns></returns> public PM00200_Foundry GetFoundry(string vendorId) { var foundry = new PM00200_Foundry(); try { foundry = _dynamicsContext.PM00200_Foundry.FirstOrDefault(x => x.VENDORID.Replace(" ", string.Empty).ToLower() == vendorId.Replace(" ", string.Empty).ToLower()); } catch (Exception ex) { logger.ErrorFormat("Error getting foundry: {0} ", ex.ToString()); } return(foundry); }
/// <summary> /// serialize vendor /// </summary> /// <param name="filename"></param> /// <param name="foundry"></param> public static void SerializeVendorObject(string filename, PM00200_Foundry foundry) { try { // Instantiate an eConnectType schema object eConnectType eConnect = new eConnectType(); // Instantiate a PMVendorMasterType schema object PMVendorMasterType vendortype = new PMVendorMasterType(); // Instantiate a taUpdateCreateCustomerRcd XML node object taUpdateCreateVendorRcd vendor = new taUpdateCreateVendorRcd(); // Create an XML serializer object XmlSerializer serializer = new XmlSerializer(eConnect.GetType()); //Populate elements of the taUpdateCreateVendorRcd XML node object vendor.VENDORID = foundry.VENDORID; vendor.VENDNAME = foundry.VENDNAME; vendor.VENDSHNM = foundry.VENDSHNM; vendor.UseVendorClass = 1; vendor.VNDCLSID = "SUO"; // Populate the PMVendorMasterType schema with the taUpdateCreateVendorRcd XML node vendortype.taUpdateCreateVendorRcd = vendor; PMVendorMasterType[] vendorMaster = { vendortype }; // Populate the eConnectType object with the PMVendorMasterType schema object eConnect.PMVendorMasterType = vendorMaster; // Create objects to create file and write the customer XML to the file FileStream fs = new FileStream(filename, FileMode.Create); XmlTextWriter writer = new XmlTextWriter(fs, new UTF8Encoding()); // Serialize the eConnectType object to a file using the XmlTextWriter. serializer.Serialize(writer, eConnect); writer.Close(); } // catch any errors that occur and display them to the console catch (System.Exception ex) { Console.Write(ex.ToString()); } }
/// <summary> /// update foundry /// </summary> /// <param name="foundry"></param> /// <returns></returns> public OperationResult UpdateFoundry(PM00200_Foundry foundry) { var operationResult = new OperationResult(); var existingFoundry = _dynamicsContext.PM00200_Foundry.FirstOrDefault(x => x.VENDORID.Replace(" ", string.Empty) == foundry.VENDORID); if (existingFoundry != null) { logger.Debug("Foundry is being updated."); string sCustomerDocument; string sXsdSchema; string sConnectionString; using (eConnectMethods e = new eConnectMethods()) { try { // Instantiate a taUpdateCreateCustomerRcd XML node object taUpdateCreateVendorRcd vendor = new taUpdateCreateVendorRcd(); //Populate elements of the taUpdateCreateVendorRcd XML node object vendor.VENDORID = foundry.VENDORID; vendor.VENDNAME = foundry.VENDNAME; vendor.VENDSHNM = foundry.VENDSHNM; vendor.UseVendorClass = 0; vendor.UpdateIfExists = 1; // Instantiate a PMVendorMasterType schema object PMVendorMasterType vendortype = new PMVendorMasterType(); // Populate the PMVendorMasterType schema with the taUpdateCreateVendorRcd XML node vendortype.taUpdateCreateVendorRcd = vendor; PMVendorMasterType[] vendorMaster = { vendortype }; // Instantiate an eConnectType schema object eConnectType eConnect = new eConnectType(); // Instantiate a Memory Stream object MemoryStream memoryStream = new MemoryStream(); // Create an XML serializer object XmlSerializer serializer = new XmlSerializer(eConnect.GetType()); // Populate the eConnectType object with the PMVendorMasterType schema object eConnect.PMVendorMasterType = vendorMaster; // Serialize the eConnectType. serializer.Serialize(memoryStream, eConnect); // Reset the position of the memory stream to the start. memoryStream.Position = 0; // Create an XmlDocument from the serialized eConnectType in memory. XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(memoryStream); memoryStream.Close(); // Call eConnect to process the XmlDocument. e.UpdateEntity(_dynamicsConnection, xmlDocument.OuterXml); operationResult.Success = true; operationResult.Message = "Success"; } // The eConnectException class will catch eConnect business logic errors. // display the error message on the console catch (eConnectException exc) { Console.Write(exc.ToString()); operationResult.Success = false; operationResult.Message = "Error"; logger.ErrorFormat("Error while updating foundry: {0} ", exc.ToString()); } // Catch any system error that might occurr. // display the error message on the console catch (System.Exception ex) { Console.Write(ex.ToString()); operationResult.Success = false; operationResult.Message = "Error"; logger.ErrorFormat("Error while updating foundry: {0} ", ex.ToString()); } finally { // Call the Dispose method to release the resources // of the eConnectMethds object e.Dispose(); } } // end of using statement } else { operationResult.Success = false; operationResult.Message = "Unable to find selected foundry."; } return(operationResult); }