public virtual ARInvoice CommitExternalTax(ARInvoice doc) { if (doc != null && doc.IsTaxValid == true && doc.NonTaxable == false && IsExternalTax(doc.TaxZoneID) && doc.InstallmentNbr == null) { if (TaxPluginMaint.IsActive(Base, doc.TaxZoneID)) { var service = ExternalTax.TaxProviderFactory(Base, doc.TaxZoneID); CommitTaxRequest request = new CommitTaxRequest(); request.CompanyCode = ExternalTax.CompanyCodeFromBranch(Base, doc.TaxZoneID, doc.BranchID); request.DocCode = string.Format("AR.{0}.{1}", doc.DocType, doc.RefNbr); if (doc.DocType == ARDocType.CreditMemo) { request.DocType = TaxDocumentType.ReturnInvoice; } else { request.DocType = TaxDocumentType.SalesInvoice; } CommitTaxResult result = service.CommitTax(request); if (result.IsSuccess) { doc.IsTaxPosted = true; } else { //Avalara retuned an error - The given document is already marked as posted on the avalara side. if (!result.IsSuccess && result.Messages.Any(t => t.Contains("Expected Posted"))) { //ignore this error - everything is cool } else { //show as warning. StringBuilder sb = new StringBuilder(); foreach (var msg in result.Messages) { sb.AppendLine(msg); } if (sb.Length > 0) { doc.WarningMessage = PXMessages.LocalizeFormatNoPrefixNLA(Messages.PostingToExternalTaxProviderFailed, sb.ToString()); } } } } } return(doc); }
public virtual void CancelTax(ARInvoice invoice, VoidReasonCode code) { string taxZoneID = ARInvoice.PK.Find(Base, invoice)?.TaxZoneID ?? invoice.TaxZoneID; var request = new VoidTaxRequest(); request.CompanyCode = CompanyCodeFromBranch(taxZoneID, invoice.BranchID); request.Code = code; request.DocCode = $"AR.{invoice.DocType}.{invoice.RefNbr}"; request.DocType = GetTaxDocumentType(invoice); var service = ExternalTax.TaxProviderFactory(Base, taxZoneID); var result = service.VoidTax(request); bool raiseError = false; if (!result.IsSuccess) { LogMessages(result); if (result.Messages.Any(t => t.Contains("DocumentNotFoundError") || t.Contains("The tax document could not be found."))) { //just ignore this error. There is no document to delete in avalara. } else { raiseError = true; } } if (raiseError) { throw new PXException(TX.Messages.FailedToDeleteFromExternalTaxProvider); } else { invoice.IsTaxSaved = false; invoice.IsTaxValid = false; if (Base.Document.Cache.GetStatus(invoice) == PXEntryStatus.Notchanged) { Base.Document.Cache.SetStatus(invoice, PXEntryStatus.Updated); } } }