Skip to content

Instantly share code, notes, and snippets.

@smarenich
Created September 11, 2018 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smarenich/73708f42e7f93e9387b8d3982a867415 to your computer and use it in GitHub Desktop.
Save smarenich/73708f42e7f93e9387b8d3982a867415 to your computer and use it in GitHub Desktop.
#region Processing
public static void ProcessRecords(List<ARRegister> records)
{
PXGraph graph = new PXGraph();
Boolean anyFailed = false;
foreach (ARRegister rec in records)
{
PXProcessing.SetCurrentItem(rec);
try
{
#region Validation
ARRegister invoice = PXSelect<ARRegister,
Where<ARRegister.docType, Equal<Required<ARRegister.docType>>, And<ARRegister.refNbr, Equal<Required<ARRegister.refNbr>>>>>.Select(graph, rec.DocType, rec.RefNbr);
BAccount customer = (BAccount)PXSelectorAttribute.Select<ARRegister.customerID>(graph.Caches[typeof(ARRegister)], invoice);
CompanyLinkedEntity entity = null;
foreach (PXResult<CompanyLinkedEntity> r in PXSelect<CompanyLinkedEntity,
Where<CompanyLinkedEntity.customerID, Equal<Required<CompanyLinkedEntity.customerID>>,
And<Where<CompanyLinkedEntity.branchID, IsNull,
Or<CompanyLinkedEntity.branchID, Equal<Required<CompanyLinkedEntity.branchID>>>>>>>.Select(graph, invoice.CustomerID, invoice.BranchID))
{
if (entity != null)
throw new PXException(IBMessages.MultipleBranches, customer.AcctCD);
entity = (CompanyLinkedEntity)r;
}
if (entity == null) throw new PXException(IBMessages.NoInterCompany, customer.AcctCD);
InterCompany company = (InterCompany)PXSelect<InterCompany,
Where<InterCompany.interCompanyID, Equal<Required<InterCompany.interCompanyID>>>>.Select(graph, entity.InterCompanyID);
if (company == null) throw new PXException(IBMessages.NoInterCompany, customer.AcctCD);
if (entity == null || entity.CustomerID == null || entity.VendorID == null)
throw new PXException(IBMessages.NoInterCompany, customer.AcctCD);
BAccount vendor = (BAccount)PXSelectorAttribute.Select<CompanyLinkedEntity.vendorID>(graph.Caches[typeof(CompanyLinkedEntity)], entity);
if (vendor == null) throw new PXException(IBMessages.NoVendor, company.InterCompanyID);
#endregion
#region Processing
using (WS.DefaultSoapClient client = new WS.DefaultSoapClient(CreateBinding(company), CreateEndpoint(company)))
{
client.Login(company.LoginUser, company.Password, company.LoginCompany, String.IsNullOrWhiteSpace(company.LoginBranch) ? null : company.LoginBranch, null);
try
{
Tuple<String, String> result = new Tuple<String, String>(null, null);
if (invoice.DocType == ARDocType.CashSale)
{
result = CreateQuickCheck(client, graph, entity, invoice, vendor);
}
else
{
result = CreateAPInvoice(client, graph, entity, invoice, vendor);
}
if (result != null && result.Item1 != null && result.Item2 != null)
{
//Update Bill
graph.Caches[typeof(ARRegister)].SetValue<ARInvoiceExt.usrCompletedInterCompany>(invoice, true);
graph.Caches[typeof(ARRegister)].Update(invoice);
graph.Actions.PressSave();
graph.Caches[typeof(ARRegister)].SetValue<ARInvoiceExt.usrAPCompany>(invoice, company.InterCompanyID);
graph.Caches[typeof(ARRegister)].SetValue<ARInvoiceExt.usrAPDocType>(invoice, GetDocType(rec));
graph.Caches[typeof(ARRegister)].SetValue<ARInvoiceExt.usrAPDocNumber>(invoice, result.Item2);
graph.Caches[typeof(ARRegister)].Update(invoice);
graph.Caches[typeof(ARRegister)].PersistUpdated(invoice);
}
}
finally
{
client.Logout();
}
}
#endregion
PXProcessing.SetProcessed();
}
catch (Exception ex)
{
anyFailed = true;
PXProcessing.SetError(ex);
}
}
PXProcessing.SetCurrentItem(null);
if (anyFailed) throw new PXException(PX.Data.ErrorMessages.SeveralItemsFailed);
}
public static Tuple<String, String> CreateAPInvoice(WS.DefaultSoapClient client, PXGraph graph, CompanyLinkedEntity entity, ARRegister invoice, BAccount vendor)
{
WS.APInvoice inv = new WS.APInvoice
{
Type = new WS.StringValue { Value = GetDocType(invoice) },
Vendor = new WS.StringValue { Value = vendor.AcctCD.Trim() },
Date = new WS.DateTimeValue { Value = invoice.DocDate },
Currency = new WS.StringValue { Value = invoice.CuryID },
VendorRef = new WS.StringValue { Value = invoice.RefNbr },
Description = new WS.StringValue { Value = invoice.DocDesc },
DueDate = new WS.DateTimeValue { Value = invoice.DueDate },
CustomFields = new WS.CustomField[]
{
new WS.CustomStringField { Name = "UsrARDocType", Value = new WS.StringValue { Value = invoice.DocType } },
new WS.CustomStringField { Name = "UsrARDocNumber", Value = new WS.StringValue { Value = invoice.RefNbr } },
},
APInvoiceDetails = CreateAPInvoiceDetails(graph, invoice, entity),
};
try
{
inv = (WS.APInvoice)client.Put(inv);
if (inv == null || inv.ReferenceNbr == null || String.IsNullOrEmpty(inv.ReferenceNbr.Value))
throw new PXException(IBMessages.PutNoResult);
}
catch (Exception ex)
{
PXTrace.WriteError(ex);
//throw new PXException(IBMessages.PutError, ex);
throw;
}
return new Tuple<String, String>(inv.Type.Value, inv.ReferenceNbr.Value);
}
public static WS.APInvoiceDetails[] CreateAPInvoiceDetails(PXGraph graph, ARRegister invoice, CompanyLinkedEntity entity)
{
Dimension subsetup = PXSelect<Dimension, Where<Dimension.dimensionID, Equal<SubAccountAttribute.dimensionName>>>.Select(graph);
List<WS.APInvoiceDetails> invoicesDetails = new List<WS.APInvoiceDetails>();
foreach (ARTran artran in PXSelect<ARTran,
Where<ARTran.tranType, Equal<Required<ARTran.tranType>>, And<ARTran.refNbr, Equal<Required<ARTran.refNbr>>>>>.Select(graph, invoice.DocType, invoice.RefNbr))
{
InventoryItem item = (InventoryItem)PXSelectorAttribute.Select<ARTran.inventoryID>(graph.Caches[typeof(ARTran)], artran);
WS.APInvoiceDetails details = new WS.APInvoiceDetails();
if (item != null && item.InventoryCD != null)
details.InventoryID = new WS.StringValue { Value = item.InventoryCD };
details.Quantity = new WS.DecimalValue { Value = artran.Qty };
details.UnitCost = new WS.DecimalValue { Value = artran.CuryUnitPrice };
details.TransactionDescr = new WS.StringValue { Value = artran.TranDesc };
details.UOM = new WS.StringValue { Value = artran.UOM };
details.ExtCost = new WS.DecimalValue { Value = artran.CuryLineAmt };
var acctID = ReplaceAcct(graph, artran.AccountID, entity.InterCompanyID, invoice.BranchID);
Account account = (Account)PXSelect<Account,
Where<Account.accountID, Equal<Required<Account.accountID>>>>.Select(graph, acctID);
details.Account = new WS.StringValue { Value = account.AccountCD };
object sub = ReplaceSub(graph, SubAccountAttribute.GetSubaccount(graph, artran.SubID).SubCD, entity.InterCompanyID, invoice.BranchID);
if (sub is string && subsetup.Validate == true
&& ((Sub)PXSelect<Sub, Where<Sub.subCD, Equal<Required<Sub.subCD>>>>.Select(graph, (string)sub)) == null)
throw new PXException(IBMessages.NoSubAccount, (string)sub);
details.Subaccount = new WS.StringValue { Value = (string)sub };
invoicesDetails.Add(details);
}
return invoicesDetails.ToArray();
}
public static Tuple<String, String> CreateQuickCheck(WS.DefaultSoapClient client, PXGraph graph, CompanyLinkedEntity entity, ARRegister invoice, BAccount vendor)
{
WS.QuickCheck inv = new WS.QuickCheck
{
Type = new WS.StringValue { Value = GetDocType(invoice) },
Vendor = new WS.StringValue { Value = vendor.AcctCD.Trim() },
Date = new WS.DateTimeValue { Value = invoice.DocDate },
Currency = new WS.StringValue { Value = invoice.CuryID },
PaymentRef = new WS.StringValue { Value = invoice.RefNbr },
Description = new WS.StringValue { Value = invoice.DocDesc },
CustomFields = new WS.CustomField[]
{
new WS.CustomStringField { Name = "UsrARDocType", Value = new WS.StringValue { Value = invoice.DocType } },
new WS.CustomStringField { Name = "UsrARDocNumber", Value = new WS.StringValue { Value = invoice.RefNbr } },
},
QuickCheckDetails = CreateQuickCheckDetails(graph, invoice, entity),
};
try
{
inv = (WS.QuickCheck)client.Put(inv);
if (inv == null || inv.ReferenceNbr == null || String.IsNullOrEmpty(inv.ReferenceNbr.Value))
throw new PXException(IBMessages.PutNoResult);
}
catch (Exception ex)
{
PXTrace.WriteError(ex);
//throw new PXException(IBMessages.PutError, ex);
throw;
}
return new Tuple<String, String>(inv.Type.Value, inv.ReferenceNbr.Value);
}
public static WS.QuickCheckDetails[] CreateQuickCheckDetails(PXGraph graph, ARRegister invoice, CompanyLinkedEntity entity)
{
Dimension subsetup = PXSelect<Dimension, Where<Dimension.dimensionID, Equal<SubAccountAttribute.dimensionName>>>.Select(graph);
List<WS.QuickCheckDetails> invoicesDetails = new List<WS.QuickCheckDetails>();
foreach (ARTran artran in PXSelect<ARTran,
Where<ARTran.tranType, Equal<Required<ARTran.tranType>>, And<ARTran.refNbr, Equal<Required<ARTran.refNbr>>>>>.Select(graph, invoice.DocType, invoice.RefNbr))
{
InventoryItem item = (InventoryItem)PXSelectorAttribute.Select<ARTran.inventoryID>(graph.Caches[typeof(ARTran)], artran);
WS.QuickCheckDetails details = new WS.QuickCheckDetails();
if (item != null && item.InventoryCD != null)
details.InventoryID = new WS.StringValue { Value = item.InventoryCD };
details.Quantity = new WS.DecimalValue { Value = artran.Qty };
details.UnitCost = new WS.DecimalValue { Value = artran.CuryUnitPrice };
details.TransactionDescr = new WS.StringValue { Value = artran.TranDesc };
details.UOM = new WS.StringValue { Value = artran.UOM };
details.ExtCost = new WS.DecimalValue { Value = artran.CuryLineAmt };
var acctID = ReplaceAcct(graph, artran.AccountID, entity.InterCompanyID, invoice.BranchID);
Account account = (Account)PXSelect<Account,
Where<Account.accountID, Equal<Required<Account.accountID>>>>.Select(graph, acctID);
details.Account = new WS.StringValue { Value = account.AccountCD };
object sub = ReplaceSub(graph, SubAccountAttribute.GetSubaccount(graph, artran.SubID).SubCD, entity.InterCompanyID, invoice.BranchID);
if (sub is string && subsetup.Validate == true
&& ((Sub)PXSelect<Sub, Where<Sub.subCD, Equal<Required<Sub.subCD>>>>.Select(graph, (string)sub)) == null)
throw new PXException(IBMessages.NoSubAccount, (string)sub);
details.Subaccount = new WS.StringValue { Value = (string)sub };
invoicesDetails.Add(details);
}
return invoicesDetails.ToArray();
}
#endregion
#region Auxiliary
public static string GetDocType(ARRegister invoice)
{
switch (invoice.DocType ?? ARDocType.Invoice)
{
case ARDocType.Invoice:
return APDocType.Invoice;
case ARDocType.CreditMemo:
return APDocType.DebitAdj;
case ARDocType.DebitMemo:
return APDocType.CreditAdj;
case ARDocType.CashSale:
return APDocType.QuickCheck;
default:
return APDocType.Invoice;
}
}
public static System.ServiceModel.EndpointAddress CreateEndpoint(InterCompany company)
{
string url = company.URL;
if (url != null && url[url.Length - 1] != '/') url += "/";
string endpoint = company.Endpoint;
if (endpoint != null && endpoint[endpoint.Length - 1] != '/') endpoint += "/";
Uri uri = new Uri(new Uri(new Uri(new Uri(url), "entity/"), endpoint), company.Version);
return new System.ServiceModel.EndpointAddress(uri.ToString());
}
public static System.ServiceModel.Channels.Binding CreateBinding(InterCompany company)
{
if (company.URL.StartsWith("https://"))
{
return new System.ServiceModel.BasicHttpsBinding()
{
AllowCookies = true,
MaxReceivedMessageSize = 65555555,
SendTimeout = TimeSpan.FromMinutes(5),
ReceiveTimeout = TimeSpan.FromMinutes(5),
Security = new System.ServiceModel.BasicHttpsSecurity() { Mode = System.ServiceModel.BasicHttpsSecurityMode.Transport },
};
}
else
{
return new System.ServiceModel.BasicHttpBinding()
{
AllowCookies = true,
MaxReceivedMessageSize = 65555555,
SendTimeout = TimeSpan.FromMinutes(5),
ReceiveTimeout = TimeSpan.FromMinutes(5),
//Security = new System.ServiceModel.BasicHttpSecurity() { Mode = System.ServiceModel.BasicHttpSecurityMode.Transport },
};
}
}
public static int? ReplaceAcct(PXGraph graph, int? acct, string company, int? branch)
{
foreach (CompanyLinkedAccount linked in PXSelect<CompanyLinkedAccount,
Where<CompanyLinkedAccount.interCompanyID, Equal<Required<CompanyLinkedAccount.interCompanyID>>,
And<Where<CompanyLinkedAccount.branchID, IsNull,
Or<CompanyLinkedAccount.branchID, Equal<Required<CompanyLinkedAccount.branchID>>>>>>>.Select(graph, company, branch))
{
if (acct == linked.SourceAccount)
return linked.DestAccount;
}
return acct;
}
public static object ReplaceSub(PXGraph graph, string sub, string company, int? branch)
{
if (sub != null)
{
foreach (CompanyLinkedSegment linked in PXSelect<CompanyLinkedSegment,
Where<CompanyLinkedSegment.interCompanyID, Equal<Required<CompanyLinkedSegment.interCompanyID>>,
And<Where<CompanyLinkedSegment.branchID, IsNull,
Or<CompanyLinkedSegment.branchID, Equal<Required<CompanyLinkedSegment.branchID>>>>>>>.Select(graph, company, branch))
{
short index = 0;
foreach (Segment s in PXSelect<Segment, Where<Segment.dimensionID, Equal<SubAccountAttribute.dimensionName>>>.Select(graph))
{
if (s.SegmentID == linked.SegmentID)
{
sub = sub.Substring(0, index) + linked.SegmentValue + sub.Substring(index + s.Length.Value);
}
index += s.Length.Value;
}
}
return sub;
}
return 0;
}
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment