protected void ApplyTax(PMQuote quote, GetTaxResult result)
        {
            TaxZone taxZone = null;

            AP.Vendor vendor = null;

            if (result.TaxSummary.Length > 0)
            {
                taxZone = (TaxZone)PXSetup <TaxZone, Where <TaxZone.taxZoneID, Equal <Required <PMQuote.taxZoneID> > > > .Select(Base, quote.TaxZoneID);

                vendor = (VendorMaster)PXSelectReadonly <VendorMaster, Where <VendorMaster.bAccountID, Equal <Required <VendorMaster.bAccountID> > > > .Select(Base, taxZone.TaxVendorID);

                if (vendor == null)
                {
                    throw new PXException(CR.Messages.ExternalTaxVendorNotFound);
                }
            }

            //Clear all existing Tax transactions:
            foreach (PXResult <CRTaxTran, Tax> res in Base.Taxes.View.SelectMultiBound(new object[] { quote }))
            {
                CRTaxTran taxTran = (CRTaxTran)res;
                Base.Taxes.Delete(taxTran);
            }

            Base.Views.Caches.Add(typeof(Tax));

            for (int i = 0; i < result.TaxSummary.Length; i++)
            {
                string taxID = GetTaxID(result.TaxSummary[i]);

                //Insert Tax if not exists - just for the selectors sake
                Tax tx = PXSelect <Tax, Where <Tax.taxID, Equal <Required <Tax.taxID> > > > .Select(Base, taxID);

                if (tx == null)
                {
                    tx                   = new Tax();
                    tx.TaxID             = taxID;
                    tx.Descr             = PXMessages.LocalizeFormatNoPrefixNLA(TX.Messages.ExternalTaxProviderTaxId, taxID);
                    tx.TaxType           = CSTaxType.Sales;
                    tx.TaxCalcType       = CSTaxCalcType.Doc;
                    tx.TaxCalcLevel      = result.TaxSummary[i].TaxCalculationLevel.ToCSTaxCalcLevel();
                    tx.TaxApplyTermsDisc = CSTaxTermsDiscount.ToTaxableAmount;
                    tx.SalesTaxAcctID    = vendor.SalesTaxAcctID;
                    tx.SalesTaxSubID     = vendor.SalesTaxSubID;
                    tx.ExpenseAccountID  = vendor.TaxExpenseAcctID;
                    tx.ExpenseSubID      = vendor.TaxExpenseSubID;
                    tx.TaxVendorID       = taxZone.TaxVendorID;
                    tx.IsExternal        = true;

                    Base.Caches[typeof(Tax)].Insert(tx);
                }

                CRTaxTran tax = new CRTaxTran();
                tax.QuoteID        = quote.QuoteID;
                tax.TaxID          = taxID;
                tax.CuryTaxAmt     = result.TaxSummary[i].TaxAmount;
                tax.CuryTaxableAmt = result.TaxSummary[i].TaxableAmount;
                tax.TaxRate        = Convert.ToDecimal(result.TaxSummary[i].Rate) * 100;

                Base.Taxes.Insert(tax);
            }

            Base.Quote.SetValueExt <PMQuote.curyTaxTotal>(quote, result.TotalTaxAmount);

            decimal?СuryProductsAmount =
                quote.ManualTotalEntry == true
                                ? quote.CuryAmount - quote.CuryDiscTot
                                : quote.CuryLineTotal - quote.CuryDiscTot + quote.CuryTaxTotal;

            Base.Quote.SetValueExt <PMQuote.curyProductsAmount>(quote, СuryProductsAmount ?? 0m);
        }
        protected void ApplyTax(PMQuote quote, GetTaxResult result)
        {
            TaxZone taxZone = null;

            AP.Vendor vendor = null;

            if (result.TaxSummary.Length > 0)
            {
                taxZone = (TaxZone)PXSetup <TaxZone> .Where <TaxZone.taxZoneID.IsEqual <@P.AsString> > .Select(Base, quote.TaxZoneID);

                vendor = (VendorMaster)PXSelectReadonly <VendorMaster, Where <VendorMaster.bAccountID, Equal <Required <VendorMaster.bAccountID> > > > .Select(Base, taxZone.TaxVendorID);

                if (vendor == null)
                {
                    throw new PXException(CR.Messages.ExternalTaxVendorNotFound);
                }
            }

            //Clear all existing Tax transactions:
            foreach (PXResult <CRTaxTran, Tax> res in Base.Taxes.View.SelectMultiBound(new object[] { quote }))
            {
                CRTaxTran taxTran = (CRTaxTran)res;
                Base.Taxes.Delete(taxTran);
            }

            Base.Views.Caches.Add(typeof(Tax));

            for (int i = 0; i < result.TaxSummary.Length; i++)
            {
                string taxID = result.TaxSummary[i].TaxName;
                if (string.IsNullOrEmpty(taxID))
                {
                    taxID = result.TaxSummary[i].JurisCode;
                }

                if (string.IsNullOrEmpty(taxID))
                {
                    PXTrace.WriteInformation(Messages.EmptyValuesFromExternalTaxProvider);
                    continue;
                }

                CreateTax(Base, taxZone, vendor, result.TaxSummary[i], taxID);

                CRTaxTran tax = new CRTaxTran();
                tax.QuoteID        = quote.QuoteID;
                tax.TaxID          = taxID;
                tax.CuryTaxAmt     = result.TaxSummary[i].TaxAmount;
                tax.CuryTaxableAmt = result.TaxSummary[i].TaxableAmount;
                tax.TaxRate        = Convert.ToDecimal(result.TaxSummary[i].Rate) * 100;

                Base.Taxes.Insert(tax);
            }

            Base.Quote.SetValueExt <PMQuote.curyTaxTotal>(quote, result.TotalTaxAmount);

            decimal?СuryProductsAmount =
                quote.ManualTotalEntry == true
                                ? quote.CuryAmount - quote.CuryDiscTot
                                : quote.CuryLineTotal - quote.CuryDiscTot + quote.CuryTaxTotal;

            Base.Quote.SetValueExt <PMQuote.curyProductsAmount>(quote, СuryProductsAmount ?? 0m);
        }