protected override TaxDetail CalculateTaxSum(PXCache sender, object taxrow, object row)
            {
                TaxRev taxrev = PXResult.Unwrap <TaxRev>(taxrow);
                Tax    tax    = PXResult.Unwrap <Tax>(taxrow);
                bool   propagateCustomRate = false;
                var    origTaxRate         = taxrev.TaxRate;

                if (taxrev.TaxID != null && tax != null)
                {
                    SOTaxAmazonExt taxExt = tax.GetExtension <SOTaxAmazonExt>();
                    if (taxExt.UsrAMPropagateTaxAmt == true)
                    {
                        SOTaxTran soTax = PXResult <SOTaxTran> .Current;
                        if (soTax != null && taxrev.TaxID == soTax.TaxID && soTax.CuryTaxableAmt.GetValueOrDefault() > 0)
                        {
                            var taxRate = soTax.CuryTaxAmt / soTax.CuryTaxableAmt * 100;
                            if (taxRate != origTaxRate && taxRate > 0)
                            {
                                taxrev.TaxRate      = taxRate;
                                propagateCustomRate = true;
                            }
                        }
                    }
                }
                TaxDetail result = base.CalculateTaxSum(sender, taxrow, row);

                if (result != null && propagateCustomRate)
                {
                    result.TaxRate = origTaxRate;
                    taxrev.TaxRate = origTaxRate;
                }
                return(result);
            }
        private void SetDocumentLevelDiscountandTaxData(PrepareAndImportOrdersParams orderParams)
        {
            if (taxtotal <= 0)
            {
                return;
            }
            SOOrder currentFin = (SOOrder)orderParams.objSOOrderEntry.Document.Current;

            if (currentFin != null && orderParams.objSOOrderEntry.sosetup.Current != null)
            {
                SOSetupAmazonExt objSOOSetupext = orderParams.objSOOrderEntry.sosetup.Current.GetExtension <SOSetupAmazonExt>();
                if (objSOOSetupext != null && !string.IsNullOrEmpty(objSOOSetupext.UsrAmazonTaxID))
                {
                    if (currentFin.TaxZoneID != objSOOSetupext.UsrAmazonTaxZoneID)
                    {
                        currentFin.OverrideTaxZone = true;
                        currentFin           = orderParams.objSOOrderEntry.CurrentDocument.Update(currentFin);
                        currentFin.TaxZoneID = objSOOSetupext.UsrAmazonTaxZoneID;
                        currentFin           = orderParams.objSOOrderEntry.CurrentDocument.Update(currentFin);
                    }
                    if (currentFin.TaxZoneID != objSOOSetupext.UsrAmazonTaxZoneID)
                    {
                        return;
                    }

                    SOTaxTran orderTax = (SOTaxTran)orderParams.objSOOrderEntry.Taxes.Select();
                    if (orderTax == null && !string.IsNullOrEmpty(objSOOSetupext.UsrAmazonTaxID))
                    {
                        orderTax           = (SOTaxTran)orderParams.objSOOrderEntry.Taxes.Cache.Insert();
                        orderTax.OrderType = orderParams.objSOOrderEntry.Document.Current.OrderType;
                        orderTax.OrderNbr  = orderParams.objSOOrderEntry.Document.Current.OrderNbr;
                        orderTax.LineNbr   = int.MaxValue;
                        orderTax.TaxID     = objSOOSetupext.UsrAmazonTaxID;
                        orderParams.objSOOrderEntry.Taxes.Cache.Update(orderTax);
                        orderTax.CuryTaxableAmt = taxableamount;
                        orderTax.CuryTaxAmt     = taxtotal;
                        orderTax.TaxRate        = taxRate;
                        orderParams.objSOOrderEntry.Taxes.Cache.Update(orderTax);
                        orderParams.objSOOrderEntry.Document.SetValueExt <SOOrder.curyTaxTotal>(orderParams.objSOOrderEntry.Document.Current, taxtotal);
                        orderTotal = orderParams.objSOOrderEntry.Document.Current.OrderTotal + orderTax.CuryTaxAmt;
                        orderParams.objSOOrderEntry.Document.SetValueExt <SOOrder.curyOrderTotal>(orderParams.objSOOrderEntry.Document.Current, orderTotal);
                        orderParams.objSOOrderEntry.Document.Cache.Update(orderParams.objSOOrderEntry.Document.Current);
                    }
                    else if (orderTax != null && !string.IsNullOrEmpty(objSOOSetupext.UsrAmazonTaxID))
                    {
                        orderTax.CuryTaxAmt = taxtotal;
                        orderParams.objSOOrderEntry.Taxes.Cache.Update(orderTax);
                        orderParams.objSOOrderEntry.Document.SetValueExt <SOOrder.curyTaxTotal>(orderParams.objSOOrderEntry.Document.Current, taxtotal);
                        orderTotal = orderParams.objSOOrderEntry.Document.Current.OrderTotal + orderTax.CuryTaxAmt;
                        orderParams.objSOOrderEntry.Document.SetValueExt <SOOrder.curyOrderTotal>(orderParams.objSOOrderEntry.Document.Current, orderTotal);
                        orderParams.objSOOrderEntry.Document.Cache.Update(orderParams.objSOOrderEntry.Document.Current);
                    }
                }
            }
        }
        protected override TaxDetail CalculateTaxSum(PXCache sender, object taxrow, object row)
        {
            TaxRev    taxrev = PXResult.Unwrap <TaxRev>(taxrow);
            Tax       tax    = PXResult.Unwrap <Tax>(taxrow);
            bool      propagateCustomRate = false;
            decimal?  origTaxRate         = taxrev.TaxRate;
            SOTaxTran soTax = PXResult <SOTaxTran> .Current;

            if (taxrev != null && taxrev.TaxID != null && tax != null)
            {
                KCTaxExt taxExt = tax.GetExtension <KCTaxExt>();
                if (taxExt.UsrKCPropagateTaxAmt == true)
                {
                    if (soTax != null && taxrev.TaxID == soTax.TaxID && soTax.CuryTaxableAmt.GetValueOrDefault() > 0)
                    {
                        decimal?taxRate = soTax.CuryTaxAmt / soTax.CuryTaxableAmt * 100;
                        if (taxRate != origTaxRate && taxRate > 0)
                        {
                            taxrev.TaxRate      = taxRate;
                            propagateCustomRate = true;
                        }
                    }
                }
            }

            bool compositeExists = false;

            foreach (var line in sender.Inserted)
            {
                ARTran tran = (ARTran)line;
                KCSOInvoiceEntryExt graphKCExt = sender.Graph.GetExtension <KCSOInvoiceEntryExt>();
                InventoryItem       product    = graphKCExt.KCInventoryItem.SelectSingle(tran.InventoryID);
                if (product == null)
                {
                    continue;
                }
                InventoryItemPCExt productPCExt = product.GetExtension <InventoryItemPCExt>();
                if (productPCExt == null)
                {
                    continue;
                }

                if (productPCExt.UsrKNCompositeType != null)
                {
                    compositeExists  = true;
                    tran.CuryTranAmt = 0;
                    tran.TranAmt     = 0;
                }
            }

            TaxDetail result = base.CalculateTaxSum(sender, taxrow, row);

            if ((compositeExists || result == null) && PXResult <SOOrder> .Current != null && soTax != null)
            {
                if (result != null)
                {
                    result = null;
                }
                bool?isFromCA = ((SOOrder)PXResult <SOOrder> .Current).GetExtension <KCSOOrderExt>().UsrKCSiteName?.EndsWith("FBA");
                if (isFromCA.GetValueOrDefault())
                {
                    decimal?taxableAmount = 0;
                    foreach (ARTran tran in sender.Inserted)
                    {
                        if (tran.LineType == KCConstants.LineTypeGI ||
                            tran.LineType == KCConstants.LineTypeGN)
                        {
                            taxableAmount += tran.TranAmt;
                        }
                    }
                    if (soTax.CuryTaxableAmt != soTax.TaxableAmt && soTax.CuryTaxableAmt == 0)
                    {
                        soTax.CuryTaxableAmt = soTax.TaxableAmt;
                    }
                    decimal?taxAmount = soTax.TaxAmt * taxableAmount / soTax.CuryTaxableAmt;

                    result = (TaxDetail)((PXResult)taxrow)[0];
                    PXCache pxcache2 = sender.Graph.Caches[this._TaxSumType];
                    pxcache2.SetValue(result, this._CuryOrigTaxableAmt, soTax.CuryTaxableAmt);
                    pxcache2.SetValue(result, this._CuryTaxableAmt, taxableAmount);
                    pxcache2.SetValue(result, this._CuryTaxAmt, taxAmount);
                }
            }

            if (propagateCustomRate && result != null)
            {
                result.TaxRate = origTaxRate;
            }
            return(result);
        }