public void Post(Document doc) { TXAvalaraSetup avalaraSetup = PXSelect <TXAvalaraSetup> .Select(this); if (avalaraSetup == null) { throw new PXException(Messages.AvalaraSetupNotConfigured); } CommitTaxRequest request = new CommitTaxRequest(); request.CompanyCode = AvalaraMaint.CompanyCodeFromBranch(this, doc.BranchID); request.DocCode = string.Format("{0}.{1}.{2}", doc.Module, doc.DocType, doc.RefNbr); if (doc.Module == "AP") { if (doc.DocType == AP.APDocType.Refund) { request.DocType = DocumentType.ReturnInvoice; } else { request.DocType = DocumentType.PurchaseInvoice; } } else if (doc.Module == "AR") { if (doc.DocType == AR.ARDocType.CreditMemo) { request.DocType = DocumentType.ReturnInvoice; } else { request.DocType = DocumentType.SalesInvoice; } } else if (doc.Module == "CA") { if (doc.DrCr == CA.CADrCr.CADebit) { request.DocType = DocumentType.SalesInvoice; } else { request.DocType = DocumentType.PurchaseInvoice; } } else { throw new PXException(PXMessages.LocalizeFormatNoPrefixNLA(Messages.InvalidModule, doc.Module)); } CommitTaxResult result = service.CommitTax(request); bool setPosted = false; if (result.ResultCode == SeverityLevel.Success) { setPosted = true; } else { //Avalara retuned an error - The given document is already marked as posted on the avalara side. //Just fix the discrepency by setting the IsTaxPosted=1 in the acumatica document. Do not return this as an error to the user. if (result.ResultCode == SeverityLevel.Error && result.Messages.Count == 1 && result.Messages[0].Details == "Expected Posted") { setPosted = true; } } if (setPosted) { if (doc.Module == "AP") { PXDatabase.Update <AP.APRegister>( new PXDataFieldAssign("IsTaxPosted", true), new PXDataFieldRestrict("DocType", PXDbType.Char, 3, doc.DocType, PXComp.EQ), new PXDataFieldRestrict("RefNbr", PXDbType.NVarChar, 15, doc.RefNbr, PXComp.EQ) ); } else if (doc.Module == "AR") { PXDatabase.Update <AR.ARRegister>( new PXDataFieldAssign("IsTaxPosted", true), new PXDataFieldRestrict("DocType", PXDbType.Char, 3, doc.DocType, PXComp.EQ), new PXDataFieldRestrict("RefNbr", PXDbType.NVarChar, 15, doc.RefNbr, PXComp.EQ) ); } else if (doc.Module == "CA") { PXDatabase.Update <CA.CAAdj>( new PXDataFieldAssign("IsTaxPosted", true), new PXDataFieldRestrict("AdjRefNbr", PXDbType.NVarChar, 15, doc.RefNbr, PXComp.EQ) ); } } else { StringBuilder sb = new StringBuilder(); foreach (Avalara.AvaTax.Adapter.Message msg in result.Messages) { sb.AppendLine(msg.Name + ": " + msg.Details); } throw new PXException(sb.ToString()); } }
public ExternalTaxPostProcess() { service = new TaxSvc(); AvalaraMaint.SetupService(this, service); }