Exemplo n.º 1
0
        public static CommitTaxResult Execute(bool inProduction, string strOCN, out string summary)
        {
            summary = "";
            TaxServiceWrapper taxSvcWrapper = new TaxServiceWrapper();
            TaxSvc            taxSvc        = taxSvcWrapper.GetTaxSvcInstance(inProduction);

            CommitTaxRequest commitTaxRequest = new CommitTaxRequest();

            // Required Parameters
            commitTaxRequest.DocCode     = strOCN;
            commitTaxRequest.DocType     = DocumentType.SalesInvoice;
            commitTaxRequest.CompanyCode = Properties.Settings.Default.CompanyCode;

            // Optional Parameters
            //commitTaxRequest.NewDocCode = "INV001";

            CommitTaxResult commitTaxResult = taxSvc.CommitTax(commitTaxRequest);

            if (!commitTaxResult.ResultCode.Equals(SeverityLevel.Success))
            {
                foreach (Message message in commitTaxResult.Messages)
                {
                    summary = message.Summary;
                }
            }

            return(commitTaxResult);
        }
        private void buttonCommitTax_Click(object sender, EventArgs e)
        {
            try
            {
                //##############################################################################
                //### 1st WE CREATE THE REQUEST OBJECT FOR DOCUMENT THAT SHOULD BE COMMITTED ###
                //##############################################################################
                CommitTaxRequest commitTaxRequest = new CommitTaxRequest();

                //###########################################################
                //### 2nd WE LOAD THE REQUEST-LEVEL DATA INTO THE REQUEST ###
                //###########################################################
                commitTaxRequest.CompanyCode = textCompanyCode.Text;
                commitTaxRequest.DocType     = (DocumentType)comboDocType.SelectedItem;
                commitTaxRequest.DocCode     = textDocCode.Text;
                commitTaxRequest.NewDocCode  = textNewDocCode.Text;

                //##############################################################################################
                //### 3rd WE INVOKE THE COMMITTAX() METHOD OF THE TAXSVC OBJECT AND GET BACK A RESULT OBJECT ###
                //##############################################################################################
                Util.PreMethodCall(this, lblStatus);

                TaxSvc taxSvc = new TaxSvc();
                ((formMain)this.Owner).SetConfig(taxSvc);                              //set the Url and Security configuration

                _commitTaxResult = taxSvc.CommitTax(commitTaxRequest);

                Util.PostMethodCall(this, lblStatus);

                //#####################################
                //### 4th WE READ THE RESULT OBJECT ###
                //#####################################
                lblResultCode.Text = _commitTaxResult.ResultCode.ToString();
                Util.SetMessageLabelText(lblResultMsg, _commitTaxResult);

                commitTaxRequest = null;
                taxSvc           = null;
            }
            catch (Exception ex)
            {
                Util.ShowError(ex);
            }
            finally
            {
                Util.PostMethodCall(this, lblStatus);
            }
        }
        public static void Test()
        {
            string accountNumber = ConfigurationManager.AppSettings["AvaTax:AccountNumber"];
            string licenseKey = ConfigurationManager.AppSettings["AvaTax:LicenseKey"];
            string serviceURL = ConfigurationManager.AppSettings["AvaTax:ServiceUrl"];

            TaxSvc taxSvc = new TaxSvc();

            // Header Level Parameters
            // Required Header Parameters
            taxSvc.Configuration.Security.Account = accountNumber;
            taxSvc.Configuration.Security.License = licenseKey;
            taxSvc.Configuration.Url = serviceURL;
            taxSvc.Configuration.ViaUrl = serviceURL;
            taxSvc.Profile.Client = "AvaTaxSample";

            // Optional Header Parameters
            taxSvc.Profile.Name = "Development";

            CommitTaxRequest commitTaxRequest = new CommitTaxRequest();

            // Required Parameters
            commitTaxRequest.DocCode = "INV001-1";
            commitTaxRequest.DocType = DocumentType.SalesInvoice;
            commitTaxRequest.CompanyCode = "APITrialCompany";

            // Optional Parameters
            commitTaxRequest.NewDocCode = "INV001";

            CommitTaxResult commitTaxResult = taxSvc.CommitTax(commitTaxRequest);

            Console.WriteLine("CommitTaxTest Result: " + commitTaxResult.ResultCode.ToString());
            if (!commitTaxResult.ResultCode.Equals(SeverityLevel.Success))
            {
                foreach (Message message in commitTaxResult.Messages)
                {
                    Console.WriteLine(message.Summary);
                }
            }
        }
            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());
                }
            }