using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI.WebControls.WebParts;
using System.Data.OracleClient;
using System.Xml;

/// <summary>
/// Summary description for T24Settlement
/// </summary>
/// 

public class Gl_Settlement_Entries
{
 public string 	SETTLE_DATE	;	
 public string 	CREDIT_ACCOUNT;
 public string  DEBIT_ACCOUNT;
 public string	AMOUNT;
 public string  NARRATIVE;
 public string  FILE_NUMBER;
 public string  RE_CODE;
 public string  POSTED_DATE;

}
public class T24SettlementCls
{

     public static IList<Gl_Settlement_Entries> load_T24_Settlement_Enties(string Settle_Date,string DEBIT_ACCOUNT,string CREDIT_ACCOUNT,string RE_CODE, int startIndex, int count,string jtSorting,out int FilterListCount)
     {
        string DBConnection = Helper.dbConnection();

        OracleConnection oracon = new OracleConnection(DBConnection);
        oracon.Open();

        OracleCommand oracom = new OracleCommand();
        oracom.Connection = oracon;

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "Generate_T24_Settlement.Get_T24_Settlement_Entries";
  
        oracom.Parameters.Add("P_Settlement_Date", OracleType.VarChar).Direction = ParameterDirection.Input;
        oracom.Parameters.Add("P_Settlement_Entries", OracleType.Cursor).Direction = ParameterDirection.Output;


        oracom.Parameters["P_Settlement_Date"].Value = Settle_Date; //20140623

        OracleDataAdapter da = new OracleDataAdapter(oracom);
        DataSet ds = new DataSet();
        da.Fill(ds);
        oracon.Close();

        IList<Gl_Settlement_Entries> DataList = new List<Gl_Settlement_Entries>();

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                Gl_Settlement_Entries Gl_Settlement_Entries = new Gl_Settlement_Entries();

                Gl_Settlement_Entries.AMOUNT = (dr["AMOUNT"] as string) ?? string.Empty;
               
                if(!String.IsNullOrWhiteSpace(Gl_Settlement_Entries.AMOUNT))
                    Gl_Settlement_Entries.AMOUNT = Convert.ToString(Convert.ToDecimal(Gl_Settlement_Entries.AMOUNT) / 1000);

                Gl_Settlement_Entries.CREDIT_ACCOUNT = (dr["CREDIT_ACCOUNT"] as string) ?? string.Empty;
                Gl_Settlement_Entries.DEBIT_ACCOUNT = (dr["DEBIT_ACCOUNT"] as string) ?? string.Empty;
                Gl_Settlement_Entries.SETTLE_DATE = (dr["SETTLE_DATE"] as string) ?? string.Empty;
                Gl_Settlement_Entries.FILE_NUMBER = (dr["FILE_NUMBER"] as string) ?? string.Empty;
                Gl_Settlement_Entries.NARRATIVE = (dr["NARRATIVE"] as string) ?? string.Empty;
                Gl_Settlement_Entries.POSTED_DATE = (dr["POSTED_DATE"] as string) ?? string.Empty;
                Gl_Settlement_Entries.RE_CODE = (dr["RE_CODE"] as string) ?? string.Empty;
                //;Convert.ToInt32(dr["USER_ID"]);//Always country

                DataList.Add(Gl_Settlement_Entries);
            }

          IEnumerable<Gl_Settlement_Entries> query = DataList;

         if(!String.IsNullOrWhiteSpace(RE_CODE))
             query = query = query.Where(p => p.RE_CODE == RE_CODE);

         if (!String.IsNullOrWhiteSpace(DEBIT_ACCOUNT))
             query = query.Where(p => p.DEBIT_ACCOUNT == DEBIT_ACCOUNT);
         if (!String.IsNullOrWhiteSpace(CREDIT_ACCOUNT))
             query = query.Where(p => p.CREDIT_ACCOUNT == CREDIT_ACCOUNT);

         if (!String.IsNullOrWhiteSpace(jtSorting))
         {
             switch (jtSorting)
             {
                 case "AMOUNT ASC":
                     query = query.OrderBy(S => S.AMOUNT);
                     break;
                 case "AMOUNT DESC":
                     query = query.OrderByDescending(S => S.AMOUNT);
                     break;
                 case "DEBIT_ACCOUNT ASC":
                     query = query.OrderBy(S => S.DEBIT_ACCOUNT);
                     break;
                 case "DEBIT_ACCOUNT DESC":
                     query = query.OrderByDescending(S => S.DEBIT_ACCOUNT);
                     break;
                 case "CREDIT_ACCOUNT ASC":
                     query = query.OrderBy(S => S.CREDIT_ACCOUNT);
                     break;
                 case "CREDIT_ACCOUNT DESC":
                     query = query.OrderByDescending(S => S.CREDIT_ACCOUNT);
                     break;
                 default:
                     break;
                
             }
         }
        FilterListCount = query.ToList().Count;
           return count > 0
                       ? query.Skip(startIndex).Take(count).ToList() //Paging
                       : query.ToList(); //No paging

        }// function End
}//Clas end
//////////////
