Esempio n. 1
0
 /**
  *  Get Tax
  *	@return tax
  */
 public MTax GetTax()
 {
     if (_tax == null)
     {
         _tax = MTax.Get(GetCtx(), GetC_Tax_ID());
     }
     return(_tax);
 }
        /// <summary>
        /// Calculate/Set Tax Amt from Order Lines
        /// </summary>
        /// <returns>true if aclculated</returns>
        public bool CalculateTaxFromLines()
        {
            Decimal taxBaseAmt = Env.ZERO;
            Decimal taxAmt     = Env.ZERO;
            //
            bool documentLevel = GetTax().IsDocumentLevel();
            MTax tax           = GetTax();

            // Calculate Tax on TaxAble Amount
            String      sql = "SELECT TaxAbleAmt FROM C_OrderLine WHERE C_Order_ID=" + GetC_Order_ID() + " AND C_Tax_ID=" + GetC_Tax_ID();
            IDataReader idr = null;

            try
            {
                idr = DataBase.DB.ExecuteReader(sql, null, Get_TrxName());
                while (idr.Read())
                {
                    Decimal baseAmt = Utility.Util.GetValueOfDecimal(idr[0]);
                    taxBaseAmt = Decimal.Add(taxBaseAmt, baseAmt);
                    //
                    if (!documentLevel)         // calculate line tax
                    {
                        taxAmt = Decimal.Add(taxAmt, tax.CalculateTax(baseAmt, false, GetPrecision()));
                    }
                }
                idr.Close();
            }
            catch (Exception e)
            {
                if (idr != null)
                {
                    idr.Close();
                }
                log.Log(Level.SEVERE, "CalculateTaxFromLines", e);
                log.Log(Level.SEVERE, sql, e);
                taxBaseAmt = Utility.Util.GetValueOfDecimal(null);
            }

            //	Calculate Tax
            if (documentLevel)          //	document level
            {
                taxAmt = tax.CalculateTax(taxBaseAmt, false, GetPrecision());
            }
            SetTaxAmt(taxAmt);

            //	Set Base
            //if (IsTaxIncluded())
            //    SetTaxBaseAmt(Decimal.Subtract(taxBaseAmt, taxAmt));
            //else
            SetTaxBaseAmt(taxBaseAmt);
            //log.fine(toString());
            return(true);
        }
        /// <summary>
        /// Set Amount (Callout)
        /// </summary>
        /// <param name="windowNo">window</param>
        /// <param name="columnName">changed column</param>
        private void SetAmt(int windowNo, String columnName)
        {
            //	get values
            Decimal qtyEntered   = GetQtyEntered();
            Decimal priceEntered = GetPriceEntered();

            log.Fine("QtyEntered=" + qtyEntered + ", PriceEntered=" + priceEntered);
            //if (qtyEntered == null)
            //{
            //    qtyEntered = Env.ZERO;
            //}
            //if (priceEntered == null)
            //{
            //    priceEntered = Env.ZERO;
            //}

            //	Line Net Amt
            Decimal lineNetAmt   = Decimal.Multiply(qtyEntered, priceEntered);
            int     stdPrecision = GetCtx().GetStdPrecision();

            if (Env.Scale(lineNetAmt) > stdPrecision)
            {
                lineNetAmt = Decimal.Round(lineNetAmt, stdPrecision, MidpointRounding.AwayFromZero);
            }
            //	Calculate Tax Amount
            Boolean isTaxIncluded = IsTaxIncluded();
            Decimal?taxAmt        = null;

            if (columnName.Equals("TaxAmt"))
            {
                taxAmt = GetTaxAmt();
            }
            else
            {
                int C_Tax_ID = GetC_Tax_ID();
                if (C_Tax_ID != 0)
                {
                    MTax tax = new MTax(GetCtx(), C_Tax_ID, null);
                    taxAmt = tax.CalculateTax(lineNetAmt, isTaxIncluded, stdPrecision);
                    SetTaxAmt(Convert.ToDecimal(taxAmt));
                }
            }
            if (isTaxIncluded)
            {
                SetLineTotalAmt(lineNetAmt);
                SetLineNetAmt(Decimal.Subtract(lineNetAmt, Convert.ToDecimal(taxAmt)));
            }
            else
            {
                SetLineNetAmt(lineNetAmt);
                SetLineTotalAmt(Decimal.Subtract(lineNetAmt, Convert.ToDecimal(taxAmt)));
            }
        }
        /// <summary>
        /// Get All
        /// </summary>
        /// <param name="ctx">context</param>
        /// <returns>array list</returns>
        public static MTax[] GetAll(Ctx ctx)
        {
            int AD_Client_ID = ctx.GetAD_Client_ID();
            int key          = AD_Client_ID;

            MTax[] retValue = (MTax[])_cacheAll[key];
            if (retValue != null)
            {
                return(retValue);
            }

            //	Create it
            String sql = "SELECT * FROM C_Tax WHERE AD_Client_ID=@AD_Client_ID"
                         + " ORDER BY C_Country_ID, C_Region_ID, To_Country_ID, To_Region_ID";
            List <MTax> list = new List <MTax>();
            //PreparedStatement pstmt = null;
            DataSet ds;

            try
            {
                SqlParameter[] param = new SqlParameter[1];
                param[0] = new SqlParameter("@AD_Client_ID", AD_Client_ID);
                ds       = new DataSet();
                ds       = DataBase.DB.ExecuteDataset(sql, param);

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    MTax tax = new MTax(ctx, dr, null);
                    _cache.Add(tax.GetC_Tax_ID(), tax);
                    list.Add(tax);
                }
                ds = null;
                //pstmt.close ();
                //pstmt = null;
            }
            catch (Exception e)
            {
                _log.Log(Level.SEVERE, sql, e);
            }
            finally
            {
                ds = null;
            }

            //	Create Array
            retValue = new MTax[list.Count];
            retValue = list.ToArray();
            //
            _cacheAll.Add(key, retValue);
            return(retValue);
        }
        /// <summary>
        /// Get Tax from Cache
        /// </summary>
        /// <param name="ctx">context</param>
        /// <param name="C_Tax_ID">id</param>
        /// <returns>MTax</returns>
        public static MTax Get(Ctx ctx, int C_Tax_ID)
        {
            int  key      = C_Tax_ID;
            MTax retValue = (MTax)_cache[key];

            if (retValue != null)
            {
                return(retValue);
            }
            retValue = new MTax(ctx, C_Tax_ID, null);
            if (retValue.Get_ID() != 0)
            {
                _cache.Add(key, retValue);
            }
            return(retValue);
        }
Esempio n. 6
0
        /**************************************************************************
         *  Calculate/Set Tax Base Amt from Invoice Lines
         *  @return true if tax calculated
         */
        public bool CalculateTaxFromLines()
        {
            Decimal?taxBaseAmt = Env.ZERO;
            Decimal taxAmt     = Env.ZERO;
            //
            bool documentLevel = GetTax().IsDocumentLevel();
            MTax tax           = GetTax();
            //
            String sql = "SELECT il.LineNetAmt, COALESCE(il.TaxAmt,0), i.IsSOTrx "
                         + "FROM C_InvoiceLine il"
                         + " INNER JOIN C_Invoice i ON (il.C_Invoice_ID=i.C_Invoice_ID) "
                         + "WHERE il.C_Invoice_ID=" + GetC_Invoice_ID() + " AND il.C_Tax_ID=" + GetC_Tax_ID();
            IDataReader idr = null;

            try
            {
                idr = DataBase.DB.ExecuteReader(sql, null, Get_TrxName());
                while (idr.Read())
                {
                    //	BaseAmt
                    Decimal baseAmt = Utility.Util.GetValueOfDecimal(idr[0]);
                    taxBaseAmt = Decimal.Add((Decimal)taxBaseAmt, baseAmt);
                    //	TaxAmt
                    Decimal amt = Utility.Util.GetValueOfDecimal(idr[1]);
                    //if (amt == null)
                    //    amt = Env.ZERO;
                    bool isSOTrx = "Y".Equals(idr[2].ToString());
                    //
                    if (documentLevel || Env.Signum(baseAmt) == 0)
                    {
                        amt = Env.ZERO;
                    }
                    else if (Env.Signum(amt) != 0 && !isSOTrx)  //	manually entered
                    {
                        ;
                    }
                    else        // calculate line tax
                    {
                        amt = tax.CalculateTax(baseAmt, IsTaxIncluded(), GetPrecision());
                    }
                    //
                    taxAmt = Decimal.Add(taxAmt, amt);
                }
                idr.Close();
            }
            catch (Exception e)
            {
                if (idr != null)
                {
                    idr.Close();
                }
                log.Log(Level.SEVERE, "setTaxBaseAmt", e);
                taxBaseAmt = null;
            }
            if (taxBaseAmt == null)
            {
                return(false);
            }

            //	Calculate Tax
            if (documentLevel || Env.Signum(taxAmt) == 0)
            {
                taxAmt = tax.CalculateTax((Decimal)taxBaseAmt, IsTaxIncluded(), GetPrecision());
            }
            SetTaxAmt(taxAmt);

            //	Set Base
            if (IsTaxIncluded())
            {
                SetTaxBaseAmt(Decimal.Subtract((Decimal)taxBaseAmt, taxAmt));
            }
            else
            {
                SetTaxBaseAmt((Decimal)taxBaseAmt);
            }
            return(true);
        }
Esempio n. 7
0
        /// <summary>
        /// Calculate/Set Surcharge Tax Amt from Invoice Lines
        /// </summary>
        /// <returns>true if calculated</returns>
        public bool CalculateSurchargeFromLines()
        {
            Decimal taxBaseAmt = Env.ZERO;
            Decimal surTaxAmt  = Env.ZERO;
            //
            MTax surTax        = new MTax(GetCtx(), GetC_Tax_ID(), Get_TrxName());
            bool documentLevel = surTax.IsDocumentLevel();
            //
            String sql = "SELECT il.TaxBaseAmt, COALESCE(il.TaxAmt,0), i.IsSOTrx  , i.C_Currency_ID , i.DateAcct , i.C_ConversionType_ID, tax.SurchargeType "
                         + "FROM C_InvoiceLine il"
                         + " INNER JOIN C_Invoice i ON (il.C_Invoice_ID=i.C_Invoice_ID) "
                         + " INNER JOIN C_Tax tax ON il.C_Tax_ID=tax.C_Tax_ID "
                         + "WHERE il.C_Invoice_ID=" + GetC_Invoice_ID() + " AND tax.Surcharge_Tax_ID=" + GetC_Tax_ID();
            IDataReader idr                 = null;
            int         c_Currency_ID       = 0;
            DateTime?   dateAcct            = null;
            int         c_ConversionType_ID = 0;

            try
            {
                idr = DataBase.DB.ExecuteReader(sql, null, Get_TrxName());
                while (idr.Read())
                {
                    //Get References from invoiice header
                    c_Currency_ID       = Utility.Util.GetValueOfInt(idr[3]);
                    dateAcct            = Utility.Util.GetValueOfDateTime(idr[4]);
                    c_ConversionType_ID = Utility.Util.GetValueOfInt(idr[5]);
                    //	BaseAmt
                    Decimal baseAmt = Utility.Util.GetValueOfDecimal(idr[0]);
                    //	TaxAmt
                    Decimal taxAmt        = Utility.Util.GetValueOfDecimal(idr[1]);
                    string  surchargeType = Util.GetValueOfString(idr[6]);

                    // for Surcharge Calculation type - Line Amount + Tax Amount
                    if (surchargeType.Equals(MTax.SURCHARGETYPE_LineAmountPlusTax))
                    {
                        baseAmt    = Decimal.Add(baseAmt, taxAmt);
                        taxBaseAmt = Decimal.Add(taxBaseAmt, baseAmt);
                    }
                    // for Surcharge Calculation type - Line Amount
                    else if (surchargeType.Equals(MTax.SURCHARGETYPE_LineAmount))
                    {
                        taxBaseAmt = Decimal.Add(taxBaseAmt, baseAmt);
                    }
                    // for Surcharge Calculation type - Tax Amount
                    else
                    {
                        baseAmt    = taxAmt;
                        taxBaseAmt = Decimal.Add(taxBaseAmt, baseAmt);
                    }

                    taxAmt = Env.ZERO;

                    bool isSOTrx = "Y".Equals(idr[2].ToString());
                    //
                    if (documentLevel || Env.Signum(baseAmt) == 0)
                    {
                    }
                    else if (Env.Signum(taxAmt) != 0 && !isSOTrx)       //	manually entered
                    {
                        ;
                    }
                    else        // calculate line tax
                    {
                        taxAmt = surTax.CalculateTax(baseAmt, false, GetPrecision());
                    }
                    //
                    surTaxAmt = Decimal.Add(surTaxAmt, taxAmt);
                }
                idr.Close();
            }
            catch (Exception e)
            {
                if (idr != null)
                {
                    idr.Close();
                }
                log.Log(Level.SEVERE, "setTaxBaseAmt", e);
                taxBaseAmt = Util.GetValueOfDecimal(null);
            }

            //	Calculate Tax
            if (documentLevel || Env.Signum(surTaxAmt) == 0)
            {
                surTaxAmt = surTax.CalculateTax(taxBaseAmt, false, GetPrecision());
            }
            SetTaxAmt(surTaxAmt);

            // set Tax Amount in base currency
            if (Get_ColumnIndex("TaxBaseCurrencyAmt") >= 0)
            {
                decimal taxAmtBaseCurrency        = GetTaxAmt();
                int     primaryAcctSchemaCurrency = GetCtx().GetContextAsInt("$C_Currency_ID");
                if (c_Currency_ID != primaryAcctSchemaCurrency)
                {
                    taxAmtBaseCurrency = MConversionRate.Convert(GetCtx(), GetTaxAmt(), primaryAcctSchemaCurrency, c_Currency_ID,
                                                                 dateAcct, c_ConversionType_ID, GetAD_Client_ID(), GetAD_Org_ID());
                }
                SetTaxBaseCurrencyAmt(taxAmtBaseCurrency);
            }

            //	Set Base
            SetTaxBaseAmt(taxBaseAmt);
            return(true);
        }
Esempio n. 8
0
        /**************************************************************************
         *  Calculate/Set Tax Base Amt from Invoice Lines
         *  @return true if tax calculated
         */
        public bool CalculateTaxFromLines()
        {
            Decimal?taxBaseAmt = Env.ZERO;
            Decimal taxAmt     = Env.ZERO;
            //
            bool documentLevel = GetTax().IsDocumentLevel();
            MTax tax           = GetTax();
            // Calculate Tax on TaxAble Amount
            String sql = "SELECT il.TaxBaseAmt, COALESCE(il.TaxAmt,0), i.IsSOTrx  , i.C_Currency_ID , i.DateAcct , i.C_ConversionType_ID "
                         + "FROM C_InvoiceLine il"
                         + " INNER JOIN C_Invoice i ON (il.C_Invoice_ID=i.C_Invoice_ID) "
                         + "WHERE il.C_Invoice_ID=" + GetC_Invoice_ID() + " AND il.C_Tax_ID=" + GetC_Tax_ID();
            IDataReader idr                 = null;
            int         c_Currency_ID       = 0;
            DateTime?   dateAcct            = null;
            int         c_ConversionType_ID = 0;

            try
            {
                idr = DataBase.DB.ExecuteReader(sql, null, Get_TrxName());
                while (idr.Read())
                {
                    //Get References from invoiice header
                    c_Currency_ID       = Utility.Util.GetValueOfInt(idr[3]);
                    dateAcct            = Utility.Util.GetValueOfDateTime(idr[4]);
                    c_ConversionType_ID = Utility.Util.GetValueOfInt(idr[5]);
                    //	BaseAmt
                    Decimal baseAmt = Utility.Util.GetValueOfDecimal(idr[0]);
                    taxBaseAmt = Decimal.Add((Decimal)taxBaseAmt, baseAmt);
                    //	TaxAmt
                    Decimal amt = Utility.Util.GetValueOfDecimal(idr[1]);
                    //if (amt == null)
                    //    amt = Env.ZERO;
                    bool isSOTrx = "Y".Equals(idr[2].ToString());
                    //
                    if (documentLevel || Env.Signum(baseAmt) == 0)
                    {
                        amt = Env.ZERO;
                    }
                    else if (Env.Signum(amt) != 0 && !isSOTrx)  //	manually entered
                    {
                        ;
                    }
                    else        // calculate line tax
                    {
                        amt = tax.CalculateTax(baseAmt, false, GetPrecision());
                    }
                    //
                    taxAmt = Decimal.Add(taxAmt, amt);
                }
                idr.Close();
            }
            catch (Exception e)
            {
                if (idr != null)
                {
                    idr.Close();
                }
                log.Log(Level.SEVERE, "setTaxBaseAmt", e);
                log.Log(Level.SEVERE, sql, e);
                taxBaseAmt = null;
            }
            if (taxBaseAmt == null)
            {
                return(false);
            }

            //	Calculate Tax
            if (documentLevel || Env.Signum(taxAmt) == 0)
            {
                taxAmt = tax.CalculateTax((Decimal)taxBaseAmt, false, GetPrecision());
            }
            SetTaxAmt(taxAmt);

            // set Tax Amount in base currency
            if (Get_ColumnIndex("TaxBaseCurrencyAmt") >= 0)
            {
                decimal taxAmtBaseCurrency        = GetTaxAmt();
                int     primaryAcctSchemaCurrency = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT C_Currency_ID FROM C_AcctSchema WHERE C_AcctSchema_ID = 
                                            (SELECT c_acctschema1_id FROM ad_clientinfo WHERE ad_client_id = " + GetAD_Client_ID() + ")", null, Get_Trx()));
                if (c_Currency_ID != primaryAcctSchemaCurrency)
                {
                    taxAmtBaseCurrency = MConversionRate.Convert(GetCtx(), GetTaxAmt(), primaryAcctSchemaCurrency, c_Currency_ID,
                                                                 dateAcct, c_ConversionType_ID, GetAD_Client_ID(), GetAD_Org_ID());
                }
                SetTaxBaseCurrencyAmt(taxAmtBaseCurrency);
            }

            //	Set Base
            //if (IsTaxIncluded())
            //    SetTaxBaseAmt(Decimal.Subtract((Decimal)taxBaseAmt, taxAmt));
            //else
            SetTaxBaseAmt((Decimal)taxBaseAmt);
            return(true);
        }
        /// <summary>
        /// Calculate Tax and Surcharge
        /// </summary>
        /// <param name="amount"></param>
        /// <param name="taxIncluded">if true tax is calculated from gross otherwise from net </param>
        /// <param name="scale"></param>
        /// <returns>tax amount</returns>
        public Decimal CalculateSurcharge(Decimal amount, Boolean taxIncluded, int scale, out Decimal SurchargeAmt)
        {
            //	Null Tax
            SurchargeAmt = Env.ZERO;
            Decimal TaxAmt = Env.ZERO;

            if (IsZeroTax())
            {
                return(Env.ZERO);
            }

            Decimal multiplier = Env.ZERO;
            Decimal taxBase    = amount;
            Decimal taxRate    = GetRate();
            MTax    surTax     = MTax.Get(GetCtx(), GetSurcharge_Tax_ID());
            Decimal surRate    = surTax.GetRate();

            // for Surcharge Calculation type - Line Amount + Tax Amount
            if (GetSurchargeType() == MTax.SURCHARGETYPE_LineAmountPlusTax)
            {
                if (!taxIncluded)   //	$100 * 6 / 100 == $6 == $100 * 0.06
                {
                    // Calculate Tax Amount on Line Amount
                    multiplier = Decimal.Round(Decimal.Divide(taxRate, 100), 12, MidpointRounding.AwayFromZero);
                    TaxAmt     = Decimal.Multiply(amount, multiplier);

                    // Calculate Surcharge Tax Amount on Line Amount + Tax Amount
                    amount       = amount + TaxAmt;
                    multiplier   = Decimal.Round(Decimal.Divide(surRate, 100), 12, MidpointRounding.AwayFromZero);
                    SurchargeAmt = Decimal.Multiply(amount, multiplier);

                    TaxAmt       = Decimal.Round(TaxAmt, scale, MidpointRounding.AwayFromZero);
                    SurchargeAmt = Decimal.Round(SurchargeAmt, scale, MidpointRounding.AwayFromZero);
                }
                else            //	$106 - ($106 / (100+6)/100) == $6 == $106 - ($106/1.06)
                {
                    multiplier = Decimal.Round(Decimal.Divide(surRate, 100), 12, MidpointRounding.AwayFromZero);
                    multiplier = Decimal.Add(multiplier, Env.ONE);

                    taxBase = Decimal.Divide(amount, multiplier);
                    taxBase = Decimal.Round(taxBase, 12, MidpointRounding.AwayFromZero);
                    amount  = taxBase;

                    multiplier = Decimal.Round(Decimal.Divide(taxRate, 100), 12, MidpointRounding.AwayFromZero);
                    multiplier = Decimal.Add(multiplier, Env.ONE);

                    taxBase = Decimal.Divide(amount, multiplier);

                    TaxAmt       = CalculateTax(taxBase, false, scale);
                    SurchargeAmt = surTax.CalculateTax(Decimal.Add(taxBase, TaxAmt), false, scale);
                }
            }
            // for Surcharge Calculation type - Line Amount
            else if (GetSurchargeType() == MTax.SURCHARGETYPE_LineAmount)
            {
                if (!taxIncluded)   //	$100 * 6 / 100 == $6 == $100 * 0.06
                {
                    // Calculate Tax Amount on Line Amount
                    multiplier = Decimal.Round(Decimal.Divide(taxRate, 100), 12, MidpointRounding.AwayFromZero);
                    TaxAmt     = Decimal.Multiply(amount, multiplier);
                    TaxAmt     = Decimal.Round(TaxAmt, scale, MidpointRounding.AwayFromZero);

                    // Calculate Surcharge Tax Amount on Line Amount
                    multiplier   = Decimal.Round(Decimal.Divide(surRate, 100), 12, MidpointRounding.AwayFromZero);
                    SurchargeAmt = Decimal.Multiply(amount, multiplier);
                    SurchargeAmt = Decimal.Round(SurchargeAmt, scale, MidpointRounding.AwayFromZero);
                }
                else            //	$106 - ($106 / (100+6)/100) == $6 == $106 - ($106/1.06)
                {
                    multiplier = Decimal.Round(Decimal.Divide(Decimal.Add(taxRate, surRate), 100), 12, MidpointRounding.AwayFromZero);
                    multiplier = Decimal.Add(multiplier, Env.ONE);

                    taxBase = Decimal.Divide(amount, multiplier);
                    taxBase = Decimal.Round(taxBase, 12, MidpointRounding.AwayFromZero);

                    TaxAmt       = CalculateTax(taxBase, false, scale);
                    SurchargeAmt = surTax.CalculateTax(taxBase, false, scale);
                }
            }
            // for Surcharge Calculation type - Tax Amount
            else
            {
                if (!taxIncluded)   //	$100 * 6 / 100 == $6 == $100 * 0.06
                {
                    // Calculate Tax Amount on Line Amount
                    multiplier = Decimal.Round(Decimal.Divide(taxRate, 100), 12, MidpointRounding.AwayFromZero);
                    TaxAmt     = Decimal.Multiply(amount, multiplier);

                    // Calculate Surcharge Tax Amount on Line Amount + Tax Amount
                    multiplier   = Decimal.Round(Decimal.Divide(surRate, 100), 12, MidpointRounding.AwayFromZero);
                    SurchargeAmt = Decimal.Multiply(TaxAmt, multiplier);

                    TaxAmt       = Decimal.Round(TaxAmt, scale, MidpointRounding.AwayFromZero);
                    SurchargeAmt = Decimal.Round(SurchargeAmt, scale, MidpointRounding.AwayFromZero);
                }
                else            //	$106 - ($106 / (100+6)/100) == $6 == $106 - ($106/1.06)
                {
                    multiplier = Decimal.Round(Decimal.Divide(surRate, 100), 12, MidpointRounding.AwayFromZero);
                    multiplier = Decimal.Multiply(taxRate, multiplier);
                    multiplier = Decimal.Add(taxRate, multiplier);
                    multiplier = Decimal.Round(Decimal.Divide(multiplier, 100), 12, MidpointRounding.AwayFromZero);
                    multiplier = Decimal.Add(multiplier, Env.ONE);

                    taxBase = Decimal.Divide(amount, multiplier);
                    taxBase = Decimal.Round(taxBase, 12, MidpointRounding.AwayFromZero);

                    TaxAmt       = CalculateTax(taxBase, false, scale);
                    SurchargeAmt = surTax.CalculateTax(TaxAmt, false, scale);
                }
            }
            return(TaxAmt);
        }
        /// <summary>
        /// Calculate/Set Surcharge Tax Amt from Order Lines
        /// </summary>
        /// <returns>true if calculated</returns>
        public bool CalculateSurchargeFromLines()
        {
            Decimal taxBaseAmt = Env.ZERO;
            Decimal surTaxAmt  = Env.ZERO;
            //
            MTax surTax        = new MTax(GetCtx(), GetC_Tax_ID(), Get_TrxName());
            bool documentLevel = surTax.IsDocumentLevel();

            //
            String sql = "SELECT ol.TaxAbleAmt, ol.TaxAmt, tax.SurchargeType FROM C_OrderLine ol"
                         + " INNER JOIN C_Tax tax ON ol.C_Tax_ID=tax.C_Tax_ID WHERE ol.C_Order_ID=" + GetC_Order_ID()
                         + " AND tax.Surcharge_Tax_ID=" + GetC_Tax_ID();
            IDataReader idr = null;

            try
            {
                idr = DataBase.DB.ExecuteReader(sql, null, Get_TrxName());
                while (idr.Read())
                {
                    Decimal baseAmt       = Util.GetValueOfDecimal(idr[0]);
                    Decimal taxAmt        = Util.GetValueOfDecimal(idr[1]);
                    string  surchargeType = Util.GetValueOfString(idr[2]);

                    // for Surcharge Calculation type - Line Amount + Tax Amount
                    if (surchargeType.Equals(MTax.SURCHARGETYPE_LineAmountPlusTax))
                    {
                        baseAmt    = Decimal.Add(baseAmt, taxAmt);
                        taxBaseAmt = Decimal.Add(taxBaseAmt, baseAmt);
                    }
                    // for Surcharge Calculation type - Line Amount
                    else if (surchargeType.Equals(MTax.SURCHARGETYPE_LineAmount))
                    {
                        taxBaseAmt = Decimal.Add(taxBaseAmt, baseAmt);
                    }
                    // for Surcharge Calculation type - Tax Amount
                    else
                    {
                        baseAmt    = taxAmt;
                        taxBaseAmt = Decimal.Add(taxBaseAmt, baseAmt);
                    }
                    //
                    if (!documentLevel)         // calculate Surcharge tax
                    {
                        surTaxAmt = Decimal.Add(surTaxAmt, surTax.CalculateTax(baseAmt, false, GetPrecision()));
                    }
                }
                idr.Close();
            }
            catch (Exception e)
            {
                if (idr != null)
                {
                    idr.Close();
                }
                log.Log(Level.SEVERE, "CalculateSurchargeFromLines", e);
                taxBaseAmt = Utility.Util.GetValueOfDecimal(null);
            }

            //	Calculate Tax
            if (documentLevel)      //	document level
            {
                surTaxAmt = surTax.CalculateTax(taxBaseAmt, false, GetPrecision());
            }
            SetTaxAmt(surTaxAmt);
            SetTaxBaseAmt(taxBaseAmt);
            return(true);
        }
Esempio n. 11
0
        /// <summary>
        /// Get Tax ID (Detail).
        /// If error return 0 and set error log (TaxNotFound)
        /// </summary>
        /// <param name="ctx">context</param>
        /// <param name="C_TaxCategory_ID">tax category</param>
        /// <param name="IsSOTrx">Sales Order Trx</param>
        /// <param name="shipDate">ship date (ignored)</param>
        /// <param name="shipFromC_Locction_ID">ship from (ignored)</param>
        /// <param name="shipToC_Location_ID">ship to (ignored)</param>
        /// <param name="billDate">invoice date</param>
        /// <param name="billFromC_Location_ID">invoice from</param>
        /// <param name="billToC_Location_ID">invoice to</param>
        /// <returns>C_Tax_ID</returns>
        protected static int Get(Ctx ctx, int C_TaxCategory_ID, bool IsSOTrx,
                                 DateTime?shipDate, int shipFromC_Locction_ID, int shipToC_Location_ID,
                                 DateTime?billDate, int billFromC_Location_ID, int billToC_Location_ID)
        {
            //	C_TaxCategory contains CommodityCode
            //	API to Tax Vendor comes here

            //if (CLogMgt.IsLevelFinest())
            {
                log.Info("(Detail) - Category=" + C_TaxCategory_ID
                         + ", SOTrx=" + IsSOTrx);
                log.Config("(Detail) - BillFrom=" + billFromC_Location_ID
                           + ", BillTo=" + billToC_Location_ID + ", BillDate=" + billDate);
            }

            MTax[]    taxes = MTax.GetAll(ctx);
            MLocation lFrom = new MLocation(ctx, billFromC_Location_ID, null);
            MLocation lTo   = new MLocation(ctx, billToC_Location_ID, null);

            log.Finer("From=" + lFrom);
            log.Finer("To=" + lTo);

            List <MTax> results = new List <MTax>();

            for (int i = 0; i < taxes.Length; i++)
            {
                MTax tax = taxes[i];
                if (tax.IsTaxExempt() ||
                    !tax.IsActive() ||
                    tax.GetC_TaxCategory_ID() != C_TaxCategory_ID ||
                    tax.GetParent_Tax_ID() != 0)        //	user parent tax
                {
                    continue;
                }
                if (IsSOTrx && MTax.SOPOTYPE_PurchaseTax.Equals(tax.GetSOPOType()))
                {
                    continue;
                }
                if (!IsSOTrx && MTax.SOPOTYPE_SalesTax.Equals(tax.GetSOPOType()))
                {
                    continue;
                }

                // if (CLogMgt.IsLevelFinest())
                {
                    log.Finest(tax.ToString());
                    log.Finest("From Country - " + (tax.GetC_Country_ID() == lFrom.GetC_Country_ID() ||
                                                    tax.GetC_Country_ID() == 0));
                    log.Finest("From Region - " + (tax.GetC_Region_ID() == lFrom.GetC_Region_ID() ||
                                                   tax.GetC_Region_ID() == 0));
                    log.Finest("To Country - " + (tax.GetTo_Country_ID() == lTo.GetC_Country_ID() ||
                                                  tax.GetTo_Country_ID() == 0));
                    log.Finest("To Region - " + (tax.GetTo_Region_ID() == lTo.GetC_Region_ID() ||
                                                 tax.GetTo_Region_ID() == 0));
                    //log.Finest("Date valid - " + (!tax.GetValidFrom().after(billDate)));
                    log.Finest("Date valid - " + (!(tax.GetValidFrom() > (billDate))));
                }

                //	From Country
                if ((tax.GetC_Country_ID() == lFrom.GetC_Country_ID() ||
                     tax.GetC_Country_ID() == 0)
                    //	From Region
                    && (tax.GetC_Region_ID() == lFrom.GetC_Region_ID() ||
                        tax.GetC_Region_ID() == 0)
                    //	To Country
                    && (tax.GetTo_Country_ID() == lTo.GetC_Country_ID() ||
                        tax.GetTo_Country_ID() == 0)
                    //	To Region
                    && (tax.GetTo_Region_ID() == lTo.GetC_Region_ID() ||
                        tax.GetTo_Region_ID() == 0)
                    //	Date
                    //&& !tax.GetValidFrom().after(billDate)
                    && tax.GetValidFrom() > (billDate)
                    )
                {
                    if (!tax.IsPostal())
                    {
                        results.Add(tax);
                        continue;
                    }
                    //
                    MTaxPostal[] postals = tax.GetPostals(false);
                    for (int j = 0; j < postals.Length; j++)
                    {
                        MTaxPostal postal = postals[j];
                        if (postal.IsActive()
                            //	Postal From is mandatory
                            && postal.GetPostal().StartsWith(lFrom.GetPostal())
                            //	Postal To is optional
                            && (postal.GetPostal_To() == null ||
                                postal.GetPostal_To().StartsWith(lTo.GetPostal()))
                            )
                        {
                            results.Add(tax);
                            continue;
                        }
                    } //	for all postals
                }
            }         //	for all taxes

            //	One Result
            if (results.Count == 1)
            {
                return(results[0].GetC_Tax_ID());
            }
            //	Multiple results - different valid from dates
            if (results.Count > 1)
            {
                MTax latest = null;
                for (int i = 0; i < results.Count; i++)
                {
                    MTax tax = results[i];
                    if (latest == null
                        //|| tax.GetValidFrom().after(latest.GetValidFrom()))
                        || tax.GetValidFrom() > (latest.GetValidFrom()))
                    {
                        latest = tax;
                    }
                }
                return(latest.GetC_Tax_ID());
            }

            //	Default Tax
            for (int i = 0; i < taxes.Length; i++)
            {
                MTax tax = taxes[i];
                if (!tax.IsDefault() || !tax.IsActive() ||
                    tax.GetParent_Tax_ID() != 0)        //	user parent tax
                {
                    continue;
                }
                if (IsSOTrx && MTax.SOPOTYPE_PurchaseTax.Equals(tax.GetSOPOType()))
                {
                    continue;
                }
                if (!IsSOTrx && MTax.SOPOTYPE_SalesTax.Equals(tax.GetSOPOType()))
                {
                    continue;
                }
                log.Fine("(default) - " + tax);
                return(tax.GetC_Tax_ID());
            }   //	for all taxes

            log.SaveError("TaxNotFound", "");
            return(0);
        }