using HRM_MQ_Consumer_Service.Structure; using Simple.OData.Client; using System; using System.Collections; using System.Collections.Generic; using System.Dynamic; using System.Linq; using System.Threading.Tasks; namespace HRM_MQ_Consumer_Service.OData { public class QDocWrapper { private ODataClient _client; private Logger.Logger _logger; public QDocWrapper(Logger.Logger logger) { var oDataAccess = new ODataAccess(); _client = oDataAccess.Client; _logger = logger; } public async Task GetCompanyByBIN(string bin) { if (!string.IsNullOrEmpty(bin)) { var company = await _client .For("ICompanies") .Expand("HeadCompany") .Filter($"BINArmadoc eq '{bin}'") .FindEntryAsync(); if (company != null) { var result = new CounterParty { Id = (long)company["Id"], Name = company["Name"]?.ToString(), LegalAddress = company["LegalAddress"]?.ToString(), PostalAddress = company["PostalAddress"]?.ToString(), GeneralCompany = company["HeadCompany"]?.ToString(), Code = company["Code"]?.ToString(), Guid = company["ExternalId"]?.ToString(), Resident = !(bool)company["Nonresident"], LegalName = company["LegalName"]?.ToString(), Bin = company["BINArmadoc"]?.ToString() }; return result; } } return null; } public async Task GetCompanyByGuid(string guid) { if (!string.IsNullOrEmpty(guid)) { var company = await _client .For("ICompanies") .Expand("HeadCompany") .Filter($"ExternalId eq '{guid}'") .FindEntryAsync(); if (company != null) { var result = new CounterParty { Id = (long)company["Id"], Name = company["Name"]?.ToString(), LegalAddress = company["LegalAddress"]?.ToString(), PostalAddress = company["PostalAddress"]?.ToString(), GeneralCompany = company["HeadCompany"]?.ToString(), Code = company["Code"]?.ToString(), Guid = company["ExternalId"]?.ToString(), Resident = !(bool)company["Nonresident"], LegalName = company["LegalName"]?.ToString(), Bin = company["BINArmadoc"]?.ToString() }; return result; } } return null; } public async Task GetBusinessUnitByBin(string bin) { bool result = false; if (!string.IsNullOrEmpty(bin)) { var businessUnit = await _client .For("ICompanies") .Expand("HeadCompany") .Filter($"ExternalId eq '{bin}'") .FindEntryAsync(); if (businessUnit != null) result = true; } return result; } public async Task UpdateCounterparty(CounterParty counterparty) { try { if (counterparty == null) return; var newCounterparty = new { counterparty.Name, counterparty.LegalName, ExternalId = counterparty.Guid, Status = counterparty.Deleted == "true" ? "Closed" : "Active", counterparty.LegalAddress, counterparty.PostalAddress, Nonresident = counterparty.Resident != true ? true : false, BINArmadoc = counterparty.Bin != null ? counterparty.Bin : string.Empty, TIN = counterparty.Tin != null ? counterparty.Tin : string.Empty }; await _client .For("ICompanies") .Key(counterparty.UpdCounterparty.Id) .Set(newCounterparty) .UpdateEntryAsync(); _logger.Info($"{counterparty.Name} updated."); } catch (Exception ex) { _logger.Error($"{counterparty.Name} - {ex.Message}"); } } internal async Task CreateCounterparty(CounterParty counterparty) { try { if (counterparty == null) return; var newCounterparty = new { counterparty.Name, counterparty.LegalName, ExternalId = counterparty.Guid, Status = counterparty.Deleted == "true" ? "Active" : "Closed", counterparty.LegalAddress, counterparty.PostalAddress, Nonresident = counterparty.Resident != true ? true : false, BINArmadoc = counterparty.Resident == true ? counterparty.Bin : string.Empty, TIN = counterparty.Resident == false ? counterparty.Bin : string.Empty }; await _client .For("ICompanies") .Set(newCounterparty) .InsertEntryAsync(); _logger.Info($"{counterparty.Name} created."); } catch (Exception ex) { _logger.Error($"{counterparty.Name} - {ex.Message}"); } } internal async Task CreateCounterparty(CounterParty1C counterparty) { try { if (counterparty == null) return; var newCounterparty = new { counterparty.Name, counterparty.LegalName, ExternalId = counterparty.Guid, Nonresident = counterparty.NonResident, BINArmadoc = counterparty.NonResident != true ? counterparty.BIN : string.Empty, TIN = counterparty.NonResident != false ? counterparty.BIN : string.Empty }; await _client .For("ICompanies") .Set(newCounterparty) .InsertEntryAsync(); _logger.Info($"{counterparty.Name} created."); } catch (Exception ex) { _logger.Error($"{counterparty.Name} - {ex.Message}"); } } } }