Exemplo n.º 1
0
        public static void CalculateAvalaraTax(OpportunityMaint rg, CROpportunity order)
        {
            TaxSvc service = new TaxSvc();

            AvalaraMaint.SetupService(rg, service);

            AddressSvc addressService = new AddressSvc();

            AvalaraMaint.SetupService(rg, addressService);

            GetTaxRequest getRequest       = null;
            bool          isValidByDefault = true;

            if (order.IsTaxValid != true)
            {
                getRequest = BuildGetTaxRequest(rg, order);

                if (getRequest.Lines.Count > 0)
                {
                    isValidByDefault = false;
                }
                else
                {
                    getRequest = null;
                }
            }

            if (isValidByDefault)
            {
                PXDatabase.Update <CROpportunity>(
                    new PXDataFieldAssign("IsTaxValid", true),
                    new PXDataFieldRestrict("OpportunityID", PXDbType.NVarChar, CROpportunity.OpportunityIDLength, order.OpportunityID, PXComp.EQ)
                    );
                return;
            }

            GetTaxResult result = service.GetTax(getRequest);

            if (result.ResultCode == SeverityLevel.Success)
            {
                try
                {
                    ApplyAvalaraTax(rg, order, result);
                    PXDatabase.Update <CROpportunity>(
                        new PXDataFieldAssign("IsTaxValid", true),
                        new PXDataFieldRestrict("OpportunityID", PXDbType.NVarChar, CROpportunity.OpportunityIDLength, order.OpportunityID, PXComp.EQ)
                        );
                }
                catch (Exception ex)
                {
                    throw new PXException(ex, TX.Messages.FailedToApplyTaxes);
                }
            }
            else
            {
                LogMessages(result);

                throw new PXException(TX.Messages.FailedToGetTaxes);
            }
        }
Exemplo n.º 2
0
        public static void Process(List <CROpportunity> list, bool isMassProcess)
        {
            OpportunityMaint rg = PXGraph.CreateInstance <OpportunityMaint>();

            for (int i = 0; i < list.Count; i++)
            {
                try
                {
                    rg.Clear();
                    rg.Opportunity.Current = PXSelect <CROpportunity, Where <CROpportunity.opportunityID, Equal <Required <CROpportunity.opportunityID> > > > .Select(rg, list[i].OpportunityID);

                    CalculateAvalaraTax(rg, rg.Opportunity.Current);
                    PXProcessing <CROpportunity> .SetInfo(i, ActionsMessages.RecordProcessed);
                }
                catch (Exception e)
                {
                    if (isMassProcess)
                    {
                        PXProcessing <CROpportunity> .SetError(i, e is PXOuterException?e.Message + "\r\n" + String.Join("\r\n", ((PXOuterException)e).InnerMessages) : e.Message);
                    }
                    else
                    {
                        throw new PXMassProcessException(i, e);
                    }
                }
            }
        }
        protected override void ReverseDocumentUpdate(OpportunityMaint graph, CROpportunity entity)
        {
            var doc = Documents.Current;

            Documents.Cache.SetValue <Document.description>(doc, entity.Subject);
            Documents.Cache.SetValue <Document.qualificationDate>(doc, PXTimeZoneInfo.Now);
            Documents.Cache.SetValue <Document.convertedBy>(doc, PXAccess.GetUserID());
            graph.Caches <TMain>().Update(GetMain(doc));
        }
Exemplo n.º 4
0
        public virtual IEnumerable ConvertToOpportunity(PXAdapter adapter)
        {
            bool           isOpportunityAutoNumberOn = this.IsNumberingAutonumbered(Setup.Current.OpportunityNumberingID);
            List <Contact> contacts = new List <Contact>(adapter.Get().Cast <Contact>());

            foreach (Contact lead in contacts)
            {
                OpportunityMaint opportunityMaint = PXGraph.CreateInstance <OpportunityMaint>();
                CROpportunity    opportunity      = (CROpportunity)opportunityMaint.Opportunity.Cache.CreateInstance();

                if (!isOpportunityAutoNumberOn)
                {
                    if (OpportunityInfo.AskExt() != WebDialogResult.OK || !OpportunityInfo.VerifyRequired())
                    {
                        return(contacts);
                    }
                    CROpportunity existing = PXSelect <CROpportunity, Where <CROpportunity.opportunityID, Equal <Required <CROpportunity.opportunityID> > > > .SelectSingleBound(this, null, OpportunityInfo.Current.OpportunityID);

                    if (existing != null)
                    {
                        OpportunityInfo.Cache.RaiseExceptionHandling <OpportunityFilter.opportunityID>(OpportunityInfo.Current, OpportunityInfo.Current.OpportunityID, new PXSetPropertyException(Messages.OpportunityAlreadyExists, OpportunityInfo.Current.OpportunityID));
                        return(contacts);
                    }

                    object cd = OpportunityInfo.Current.OpportunityID;
                    opportunityMaint.Opportunity.Cache.RaiseFieldUpdating <CROpportunity.opportunityID>(null, ref cd);
                    opportunity.OpportunityID = (string)cd;
                }
                PXLongOperation.StartOperation(this, delegate()
                {
                    CRContactClass cls = PXSelect <CRContactClass, Where <CRContactClass.classID, Equal <Current <Contact.classID> > > > .SelectSingleBound(this, new object[] { lead });
                    if (cls != null && cls.OwnerToOpportunity == true)
                    {
                        opportunity.WorkgroupID = lead.WorkgroupID;
                        opportunity.OwnerID     = lead.OwnerID;
                    }

                    if (lead.BAccountID != null)
                    {
                        opportunity.BAccountID = lead.BAccountID;
                    }
                    opportunity.ContactID = lead.ContactID;
                    opportunity           = (CROpportunity)opportunityMaint.Opportunity.Cache.Insert(opportunity);

                    ContactMaint contactGraph = PXGraph.CreateInstance <ContactMaint>();
                    lead.ContactType          = ContactTypesAttribute.Person;
                    contactGraph.Contact.Update(lead);
                    contactGraph.Save.Press();

                    opportunityMaint.Opportunity.Search <CROpportunity.opportunityID>(opportunity.OpportunityID);

                    throw new PXRedirectRequiredException(opportunityMaint, "Opportunity", true);
                });
            }
            return(contacts);
        }
Exemplo n.º 5
0
        protected static IAddressBase GetFromAddress(OpportunityMaint rg)
        {
            PXSelectBase <Branch> select = new PXSelectJoin
                                           <Branch, InnerJoin <BAccount, On <BAccount.bAccountID, Equal <Branch.bAccountID> >,
                                                               InnerJoin <Address, On <Address.addressID, Equal <BAccount.defAddressID> > > >,
                                            Where <Branch.branchID, Equal <Required <Branch.branchID> > > >(rg);

            foreach (PXResult <Branch, BAccount, Address> res in select.Select(rg.Accessinfo.BranchID))
            {
                return((Address)res);
            }

            return(null);
        }
Exemplo n.º 6
0
        protected static IAddressBase GetToAddress(OpportunityMaint rg, CROpportunity order)
        {
            Address shipAddress = null;

            Location loc = (Location)PXSelect <Location,
                                               Where <Location.bAccountID, Equal <Required <Location.bAccountID> >, And <Location.locationID, Equal <Required <Location.locationID> > > > > .
                           Select(rg, order.BAccountID, order.LocationID);

            if (loc != null)
            {
                shipAddress = PXSelect <Address, Where <Address.addressID, Equal <Required <Address.addressID> > > > .Select(rg, loc.DefAddressID);
            }

            return(shipAddress);
        }
Exemplo n.º 7
0
    protected void edContactID_EditRecord(object sender, PX.Web.UI.PXNavigateEventArgs e)
    {
        OpportunityMaint oppmaint = this.ds.DataGraph as OpportunityMaint;

        if (oppmaint != null)
        {
            CROpportunity currentopportunity = this.ds.DataGraph.Views[this.ds.DataGraph.PrimaryView].Cache.Current as CROpportunity;
            if (currentopportunity.ContactID == null && currentopportunity.BAccountID != null)
            {
                {
                    try
                    {
                        oppmaint.addNewContact.Press();
                    }
                    catch (PX.Data.PXRedirectRequiredException e1)
                    {
                        PX.Web.UI.PXBaseDataSource ds = this.ds as PX.Web.UI.PXBaseDataSource;
                        PX.Web.UI.PXBaseDataSource.RedirectHelper helper = new PX.Web.UI.PXBaseDataSource.RedirectHelper(ds);
                        helper.TryRedirect(e1);
                    }
                }
            }
        }
    }
        public virtual IEnumerable ConvertToOpportunity(PXAdapter adapter)
        {
            Save.Press();
            bool           isOpportunityAutoNumberOn = this.IsNumberingAutonumbered(Setup.Current.OpportunityNumberingID);
            List <Contact> contacts = new List <Contact>(adapter.Get().Cast <Contact>());

            foreach (Contact l in contacts)
            {
                OpportunityMaint opportunityMaint = CreateInstance <OpportunityMaint>();
                CROpportunity    opportunity      = (CROpportunity)opportunityMaint.Opportunity.Cache.Insert();

                if (!isOpportunityAutoNumberOn)
                {
                    if (OpportunityInfo.AskExt() != WebDialogResult.OK || !OpportunityInfo.VerifyRequired())
                    {
                        return(contacts);
                    }
                    CROpportunity existing = PXSelect <CROpportunity, Where <CROpportunity.opportunityID, Equal <Required <CROpportunity.opportunityID> > > > .SelectSingleBound(this, null, OpportunityInfo.Current.OpportunityID);

                    if (existing != null)
                    {
                        OpportunityInfo.Cache.RaiseExceptionHandling <OpportunityFilter.opportunityID>(OpportunityInfo.Current, OpportunityInfo.Current.OpportunityID, new PXSetPropertyException(Messages.OpportunityAlreadyExists, OpportunityInfo.Current.OpportunityID));
                        return(contacts);
                    }

                    object cd = OpportunityInfo.Current.OpportunityID;
                    opportunityMaint.Opportunity.Cache.RaiseFieldUpdating <CROpportunity.opportunityID>(null, ref cd);
                    opportunity.OpportunityID = (string)cd;
                }
                Contact lead = l;
                PXLongOperation.StartOperation(this, delegate()
                {
                    CRContactClass cls = PXSelect <CRContactClass, Where <CRContactClass.classID, Equal <Current <Contact.classID> > > > .SelectSingleBound(this, new object[] { lead });
                    if (cls != null && cls.OwnerToOpportunity == true)
                    {
                        opportunity.WorkgroupID = lead.WorkgroupID;
                        opportunity.OwnerID     = lead.OwnerID;
                    }
                    if (cls != null && cls.TargetOpportunityClassID != null)
                    {
                        opportunity.CROpportunityClassID = cls.TargetOpportunityClassID;
                    }

                    if (lead.BAccountID != null)
                    {
                        opportunity.BAccountID = lead.BAccountID;
                    }

                    if (lead.ParentBAccountID != null)
                    {
                        opportunity.ParentBAccountID = lead.ParentBAccountID;
                    }

                    if (lead.CampaignID != null)
                    {
                        opportunity.CampaignSourceID = lead.CampaignID;
                    }
                    opportunity.ContactID       = lead.ContactID;
                    opportunity.ConvertedLeadID = lead.ContactID;
                    opportunity.OpportunityName = string.IsNullOrEmpty(lead.FullName) ? lead.DisplayName : lead.FullName;
                    opportunity = (CROpportunity)opportunityMaint.Opportunity.Cache.Update(opportunity);

                    ContactMaint contactGraph = CreateInstance <ContactMaint>();
                    lead.ContactType          = ContactTypesAttribute.Person;
                    lead.QualificationDate    = PXTimeZoneInfo.Now;
                    lead.ConvertedBy          = Accessinfo.UserID;
                    lead = contactGraph.Contact.Update(lead);

                    opportunityMaint.Opportunity.Search <CROpportunity.opportunityID>(opportunity.OpportunityID);
                    lead = opportunityMaint.Leads.Update(lead);

                    // Copy Note text and Files references
                    CRSetup setup = PXSetupOptional <CRSetup> .Select(opportunityMaint);
                    PXNoteAttribute.CopyNoteAndFiles(opportunityMaint.Leads.Cache, lead, opportunityMaint.OpportunityCurrent.Cache, opportunity, setup);

                    throw new PXRedirectRequiredException(opportunityMaint, "Opportunity", true);
                });
            }
            return(contacts);
        }
        protected override CROpportunity CreateMaster(OpportunityMaint graph, OpportunityConversionOptions options)
        {
            var filter     = OpportunityInfo.Current;
            var document   = Documents.Current;
            var docContact = Contacts.Current ?? Contacts.SelectSingle();
            var docAddress = Addresses.Current ?? Addresses.SelectSingle();

            var opp = graph.Opportunity.Insert(new CROpportunity
            {
                Subject          = filter.Subject,
                CloseDate        = filter.CloseDate,
                ClassID          = filter.OpportunityClass,
                LeadID           = document.NoteID,
                Source           = document.Source,
                CampaignSourceID = document.CampaignID,
            });

            opp.ContactID  = document.RefContactID;
            opp.BAccountID = document.BAccountID;

            if (graph.OpportunityClass.SelectSingle()?.DefaultOwner == CRDefaultOwnerAttribute.Source)
            {
                opp.OwnerID     = document.OwnerID;
                opp.WorkgroupID = document.WorkgroupID;
            }

            opp = graph.Opportunity.Update(opp);

            // should be after contact and baccount changes
            if (options?.ForceOverrideContact is bool foc && foc)
            {
                graph.Opportunity.Cache.SetValueExt <CROpportunity.allowOverrideContactAddress>(opp, true);
            }

            if (opp.AllowOverrideContactAddress == true)
            {
                var address = graph.Opportunity_Address.SelectSingle();
                MapAddress(docAddress, address);
                graph.Opportunity_Address.Update(address);

                var contact = graph.Opportunity_Contact.SelectSingle();
                MapContact(docContact, contact);
                MapConsentable(docContact, contact);
                graph.Opportunity_Contact.Update(contact);

                var shipAddress = graph.Shipping_Address.SelectSingle();
                MapAddress(docAddress, shipAddress);
                graph.Shipping_Address.Update(shipAddress);

                var shipContact = graph.Shipping_Contact.SelectSingle();
                MapContact(docContact, shipContact);
                MapConsentable(docContact, shipContact);
                graph.Shipping_Contact.Update(shipContact);
            }

            FillAttributes(graph.Answers, opp);

            FillRelations(graph.Relations, opp);

            return(opp);
        }
Exemplo n.º 10
0
        protected static void ApplyAvalaraTax(OpportunityMaint rg, CROpportunity order, GetTaxResult result)
        {
            var avalaraSetup = (TXAvalaraSetup)PXSetupOptional <TXAvalaraSetup> .Select(rg);

            TaxZone taxZone = (TaxZone)PXSetup <TaxZone, Where <TaxZone.taxZoneID, Equal <Required <CROpportunity.taxZoneID> > > > .Select(rg, order.TaxZoneID);

            AP.Vendor vendor = PXSelect <AP.Vendor, Where <AP.Vendor.bAccountID, Equal <Required <AP.Vendor.bAccountID> > > > .Select(rg, taxZone.TaxVendorID);

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

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

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

            for (int i = 0; i < result.TaxSummary.Count; i++)
            {
                string taxID = AvalaraMaint.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(rg, taxID);

                if (tx == null)
                {
                    tx       = new Tax();
                    tx.TaxID = taxID;
                    //tx.Descr = string.Format("Avalara {0} {1}%", taxID, Convert.ToDecimal(result.TaxSummary[i].Rate)*100);
                    tx.Descr             = PXMessages.LocalizeFormatNoPrefixNLA(TX.Messages.AvalaraTaxId, taxID);
                    tx.TaxType           = CSTaxType.Sales;
                    tx.TaxCalcType       = CSTaxCalcType.Doc;
                    tx.TaxCalcLevel      = avalaraSetup.IsInclusiveTax == true ? CSTaxCalcLevel.Inclusive : CSTaxCalcLevel.CalcOnItemAmt;
                    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;

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

                CRTaxTran tax = new CRTaxTran();
                tax.OpportunityID  = order.OpportunityID;
                tax.TaxID          = taxID;
                tax.CuryTaxAmt     = Math.Abs(result.TaxSummary[i].Tax);
                tax.CuryTaxableAmt = Math.Abs(result.TaxSummary[i].Taxable);
                tax.TaxRate        = Convert.ToDecimal(result.TaxSummary[i].Rate) * 100;

                rg.Taxes.Insert(tax);
            }

            rg.Opportunity.SetValueExt <CROpportunity.curyTaxTotal>(order, Math.Abs(result.TotalTax));

            try
            {
                rg.SkipAvalaraTaxProcessing = true;
                rg.Save.Press();
            }
            finally
            {
                rg.SkipAvalaraTaxProcessing = false;
            }
        }
Exemplo n.º 11
0
        protected static GetTaxRequest BuildGetTaxRequest(OpportunityMaint rg, CROpportunity order)
        {
            if (order == null)
            {
                throw new PXArgumentException(ErrorMessages.ArgumentNullException);
            }

            BAccount cust = (BAccount)PXSelect <BAccount,
                                                Where <BAccount.bAccountID, Equal <Required <BAccount.bAccountID> > > > .
                            Select(rg, order.BAccountID);

            Location loc = (Location)PXSelect <Location,
                                               Where <Location.bAccountID, Equal <Required <Location.bAccountID> >, And <Location.locationID, Equal <Required <Location.locationID> > > > > .
                           Select(rg, order.BAccountID, order.LocationID);

            IAddressBase addressFrom = GetFromAddress(rg);
            IAddressBase addressTo   = GetToAddress(rg, order);

            if (addressFrom == null)
            {
                throw new PXException(Messages.FailedGetFromAddressSO);
            }

            if (addressTo == null)
            {
                throw new PXException(Messages.FailedGetToAddressSO);
            }

            var avalaraSetup = (TXAvalaraSetup)PXSetupOptional <TXAvalaraSetup> .Select(rg);

            GetTaxRequest request = new GetTaxRequest();

            request.CompanyCode        = AvalaraMaint.CompanyCodeFromBranch(rg, rg.Accessinfo.BranchID);
            request.CurrencyCode       = order.CuryID;
            request.CustomerCode       = cust.AcctCD;
            request.OriginAddress      = AvalaraMaint.FromAddress(addressFrom);
            request.DestinationAddress = AvalaraMaint.FromAddress(addressTo);
            request.DetailLevel        = DetailLevel.Summary;
            request.DocCode            = string.Format("CR.{0}", order.OpportunityID);
            request.DocDate            = order.CloseDate.GetValueOrDefault();

            int mult = 1;

            if (!string.IsNullOrEmpty(loc.CAvalaraCustomerUsageType))
            {
                request.CustomerUsageType = loc.CAvalaraCustomerUsageType;
            }
            if (!string.IsNullOrEmpty(loc.CAvalaraExemptionNumber))
            {
                request.ExemptionNo = loc.CAvalaraExemptionNumber;
            }

            request.DocType = DocumentType.SalesOrder;

            PXSelectBase <CROpportunityProducts> select = new PXSelectJoin <CROpportunityProducts,
                                                                            LeftJoin <InventoryItem, On <InventoryItem.inventoryID, Equal <CROpportunityProducts.inventoryID> >,
                                                                                      LeftJoin <Account, On <Account.accountID, Equal <InventoryItem.salesAcctID> > > >,
                                                                            Where <CROpportunityProducts.cROpportunityID, Equal <Current <CROpportunity.opportunityID> > >,
                                                                            OrderBy <Asc <CROpportunityProducts.cROpportunityProductID> > >(rg);

            foreach (PXResult <CROpportunityProducts, InventoryItem, Account> res in select.View.SelectMultiBound(new object[] { order }))
            {
                CROpportunityProducts tran = (CROpportunityProducts)res;
                InventoryItem         item = (InventoryItem)res;
                Account salesAccount       = (Account)res;

                Line line = new Line();
                line.No                 = Convert.ToString(tran.CROpportunityProductID);
                line.Amount             = mult * tran.CuryAmount.GetValueOrDefault();
                line.Description        = tran.TransactionDescription;
                line.DestinationAddress = request.DestinationAddress;
                line.OriginAddress      = request.OriginAddress;
                line.ItemCode           = item.InventoryCD;
                line.Qty                = Math.Abs(Convert.ToDouble(tran.Qty.GetValueOrDefault()));
                line.Discounted         = request.Discount > 0;
                line.TaxIncluded        = avalaraSetup.IsInclusiveTax == true;

                if (avalaraSetup != null && avalaraSetup.SendRevenueAccount == true)
                {
                    line.RevAcct = salesAccount.AccountCD;
                }

                line.TaxCode = tran.TaxCategoryID;

                request.Lines.Add(line);
            }

            return(request);
        }
Exemplo n.º 12
0
        protected static void ApplyAvalaraTax(OpportunityMaint rg, CROpportunity order, GetTaxResult result)
        {
            var avalaraSetup = (TXAvalaraSetup)PXSetupOptional <TXAvalaraSetup> .Select(rg);

            TaxZone taxZone = (TaxZone)PXSetup <TaxZone, Where <TaxZone.taxZoneID, Equal <Required <CROpportunity.taxZoneID> > > > .Select(rg, order.TaxZoneID);

            AP.Vendor vendor = PXSelect <AP.Vendor, Where <AP.Vendor.bAccountID, Equal <Required <AP.Vendor.bAccountID> > > > .Select(rg, taxZone.TaxVendorID);

            if (vendor == null)
            {
                throw new PXException("Tax Vendor is required but not found for the External TaxZone.");
            }

            Dictionary <string, CRTaxTran> existingRows = new Dictionary <string, CRTaxTran>();

            foreach (PXResult <CRTaxTran, Tax> res in rg.Taxes.View.SelectMultiBound(new object[] { order }))
            {
                CRTaxTran taxTran = (CRTaxTran)res;
                existingRows.Add(taxTran.TaxID.Trim().ToUpperInvariant(), taxTran);
            }

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

            for (int i = 0; i < result.TaxSummary.Count; i++)
            {
                string taxID = result.TaxSummary[i].TaxName.ToUpperInvariant();

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

                if (tx == null)
                {
                    tx       = new Tax();
                    tx.TaxID = taxID;
                    //tx.Descr = string.Format("Avalara {0} {1}%", taxID, Convert.ToDecimal(result.TaxSummary[i].Rate)*100);
                    tx.Descr             = string.Format("Avalara {0}", taxID);
                    tx.TaxType           = CSTaxType.Sales;
                    tx.TaxCalcType       = CSTaxCalcType.Doc;
                    tx.TaxCalcLevel      = avalaraSetup.IsInclusiveTax == true ? CSTaxCalcLevel.Inclusive : CSTaxCalcLevel.CalcOnItemAmt;
                    tx.TaxApplyTermsDisc = CSTaxTermsDiscount.ToTaxableAmount;
                    tx.SalesTaxAcctID    = vendor.SalesTaxAcctID;
                    tx.SalesTaxSubID     = vendor.SalesTaxSubID;
                    tx.ExpenseAccountID  = vendor.TaxExpenseAcctID;
                    tx.ExpenseSubID      = vendor.TaxExpenseSubID;
                    tx.TaxVendorID       = taxZone.TaxVendorID;

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

                CRTaxTran existing = null;
                existingRows.TryGetValue(taxID, out existing);

                if (existing != null)
                {
                    existing.TaxAmt         = Math.Abs(result.TaxSummary[i].Tax);
                    existing.CuryTaxAmt     = Math.Abs(result.TaxSummary[i].Tax);
                    existing.TaxableAmt     = Math.Abs(result.TaxSummary[i].Taxable);
                    existing.CuryTaxableAmt = Math.Abs(result.TaxSummary[i].Taxable);
                    existing.TaxRate        = Convert.ToDecimal(result.TaxSummary[i].Rate);

                    rg.Taxes.Update(existing);
                    existingRows.Remove(existing.TaxID.Trim().ToUpperInvariant());
                }
                else
                {
                    CRTaxTran tax = new CRTaxTran();
                    tax.OpportunityID  = order.OpportunityID;
                    tax.TaxID          = taxID;
                    tax.TaxAmt         = Math.Abs(result.TaxSummary[i].Tax);
                    tax.CuryTaxAmt     = Math.Abs(result.TaxSummary[i].Tax);
                    tax.TaxableAmt     = Math.Abs(result.TaxSummary[i].Taxable);
                    tax.CuryTaxableAmt = Math.Abs(result.TaxSummary[i].Taxable);
                    tax.TaxRate        = Convert.ToDecimal(result.TaxSummary[i].Rate);

                    rg.Taxes.Insert(tax);
                }
            }

            foreach (CRTaxTran taxTran in existingRows.Values)
            {
                rg.Taxes.Delete(taxTran);
            }

            rg.Opportunity.SetValueExt <CROpportunity.curyTaxTotal>(order, Math.Abs(result.TotalTax));

            try
            {
                rg.SkipAvalaraTaxProcessing = true;
                rg.Save.Press();
            }
            finally
            {
                rg.SkipAvalaraTaxProcessing = false;
            }
        }