private static void PopulateCFs(Company1C company1C, int amo_acc, Company company) { foreach (var p in company1C.GetType().GetProperties()) { if (FieldLists.Companies[amo_acc].ContainsKey(p.Name) && p.GetValue(company1C) is not null) { try { if ((string)p.GetValue(company1C) == "") { continue; } } catch { } company.custom_fields_values.Add(new Custom_fields_value() { field_id = FieldLists.Companies[amo_acc][p.Name], values = new Custom_fields_value.Values[] { new Custom_fields_value.Values() { value = p.GetValue(company1C) } } }); } } }
internal Guid AddCompany(Company1C company) { if (string.IsNullOrEmpty(company.email) && string.IsNullOrEmpty(company.phone) && string.IsNullOrEmpty(company.name) && string.IsNullOrEmpty(company.INN)) { throw new Exception("Unable to add company to 1C: no phone or email."); } company.company_id_1C = null; string method = "EditPartner"; string content = JsonConvert.SerializeObject(company, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include });; Request1C request = new("POST", method, content, _cred1C); Result result = new(); var response = request.GetResponse(); try { JsonConvert.PopulateObject(WebUtility.UrlDecode(response), result); } catch (Exception e) { throw new Exception($"Unable to process response from 1C: {e.Message}, Response: {response}"); } return(result.company_id_1C); }
private static int CreateCompanyInAmo(Company1C company1C, IAmoRepo <Company> compRepo, int amo_acc, RecentlyUpdatedEntityFilter filter) { Company company = new() { name = company1C.name, custom_fields_values = new() }; AddUIDToEntity(company1C, amo_acc, company); PopulateCFs(company1C, amo_acc, company); try { var result = compRepo.AddNew(company).ToList(); result.ForEach(x => filter.AddEntity(x.id)); if (result.Any()) { return(result.First().id); } else { throw new Exception("Amo returned no company Ids."); } } catch (Exception e) { throw new Exception($"Unable to create company in amo: {e.Message}"); } }
public CreateOrUpdateAmoCompany(Company1C company1C, Amo amo, Log log, RecentlyUpdatedEntityFilter filter) { _amo = amo; _log = log; _company1C = company1C; _compRepo = _amo.GetAccountById(19453687).GetRepo <Company>(); _amo_acc = 19453687; _filter = filter; }
private static void AddUIDToEntity(Company1C company1C, int amo_acc, Company company) { company.custom_fields_values.Add(new Custom_fields_value() { field_id = FieldLists.Companies[amo_acc]["company_id_1C"], values = new Custom_fields_value.Values[] { new Custom_fields_value.Values() { value = company1C.company_id_1C.Value.ToString("D") } } }); }
private static void UpdateCompanyIn1C(Company company, Guid company_id_1C, int amo_acc, CompanyRepository repo1C) { Company1C company1C = repo1C.GetCompany(company_id_1C); if (company1C == default) { throw new Exception($"Unable to update company in 1C. 1C returned no company {company_id_1C}."); } PopulateCFs(company, amo_acc, company1C); repo1C.UpdateCompany(company1C); }
internal Guid UpdateCompany(Company1C company) { if (company.company_id_1C is null || company.company_id_1C == default) { throw new Exception("Unable to update 1C client, no UID."); } string method = "EditPartner"; string content = JsonConvert.SerializeObject(company, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include });; Request1C request = new("POST", method, content, _cred1C); Result result = new(); var response = request.GetResponse(); try { JsonConvert.PopulateObject(WebUtility.UrlDecode(response), result); } catch (Exception e) { throw new Exception($"Unable to process response from 1C: {e.Message}, Response: {response}"); } return(result.company_id_1C); }
private static void PopulateCFs(Company company, int amo_acc, Company1C company1C) { if (company.custom_fields_values is not null) { foreach (var p in company1C.GetType().GetProperties()) { if (FieldLists.Companies[amo_acc].ContainsKey(p.Name) && company.custom_fields_values.Any(x => x.field_id == FieldLists.Companies[amo_acc][p.Name])) { var value = company.custom_fields_values.First(x => x.field_id == FieldLists.Companies[amo_acc][p.Name]).values[0].value; if (p.PropertyType == typeof(Guid?) && Guid.TryParse((string)value, out Guid guidValue)) { p.SetValue(company1C, guidValue); continue; } p.SetValue(company1C, value); } } } }
private static void UpdateCompanyInAmo(Company1C company1C, IAmoRepo <Company> compRepo, int company_id, int amo_acc, RecentlyUpdatedEntityFilter filter) { Company company = new() { id = company_id, //name = company1C.name, custom_fields_values = new() }; AddUIDToEntity(company1C, amo_acc, company); PopulateCFs(company1C, amo_acc, company); try { filter.AddEntity(company_id); compRepo.Save(company); } catch (Exception e) { throw new Exception($"Unable to update company {company_id} in amo: {e.Message}"); } }
internal Company1C GetCompany(Company1C company) => GetCompany((Guid)company.company_id_1C);
public List <Amo_id> Run() { if (_lead1C.amo_ids is null) { _lead1C.amo_ids = new(); } try { int amo_acc = 28395871; if (_lead1C.is_corporate) { amo_acc = 19453687; } if (_lead1C.organization == "ООО «Первый Профессиональный Институт Эстетики»") { amo_acc = 29490250; } var leadRepo = _amo.GetAccountById(amo_acc).GetRepo <Lead>(); #region Checking if lead already linked to entity an updating if possible if (_lead1C.amo_ids.Any(x => x.account_id == amo_acc)) { try { UpdateLeadInAmo(_lead1C, leadRepo, _lead1C.amo_ids.First().entity_id, amo_acc, _filter); _log.Add($"Updated lead {_lead1C.amo_ids.First().entity_id} in amo {amo_acc}."); return(_lead1C.amo_ids); } catch (Exception e) { _log.Add($"Unable to update existing lead {_lead1C.amo_ids.First().entity_id} in amo. Creating new."); } } #endregion #region Getting connected entitites ids Client1C client1C = new ClientRepository(_cred1C).GetClient((Guid)_lead1C.client_id_1C); Course1C course1C = new CourseRepository(_cred1C).GetCourse((Guid)_lead1C.product_id_1C); Company1C company1C = null; if (_lead1C.is_corporate) { company1C = new CompanyRepository(_cred1C).GetCompany((Guid)_lead1C.company_id_1C); } var contact_id = 0; var course_id = 0; var company_id = 0; if (client1C.amo_ids is not null && client1C.amo_ids.Any(x => x.account_id == amo_acc)) { contact_id = client1C.amo_ids.First(x => x.account_id == amo_acc).entity_id; } if (course1C.amo_ids is not null && course1C.amo_ids.Any(x => x.account_id == amo_acc)) { course_id = course1C.amo_ids.First(x => x.account_id == amo_acc).entity_id; } if (company1C is not null && company1C.amo_ids is not null && company1C.amo_ids.Any(x => x.account_id == amo_acc)) { company_id = company1C.amo_ids.First(x => x.account_id == amo_acc).entity_id; } #endregion #region Creating new lead var lead_id = CreateLeadInAmo(_lead1C, leadRepo, amo_acc, contact_id, course_id, company_id, _filter); _lead1C.amo_ids.Add(new() { account_id = amo_acc, entity_id = lead_id }); _log.Add($"Created lead {lead_id} in amo {amo_acc}."); #endregion } catch (Exception e) { _log.Add($"Unable to create or update lead {_lead1C.lead_id_1C} in amo: {e.Message}"); } return(_lead1C.amo_ids); }