/// <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);
        }
示例#2
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);
        }