using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Web.Services;
using System.Web.Script;
using System.Web.Script.Services;
using Newtonsoft.Json;
using System.Web.Script.Serialization;
public partial class module_expensereport : System.Web.UI.Page
{
    public static string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    public static MySqlConnection con = new MySqlConnection(constring);
    public static DateTime date1; public static DateTime date2;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public class Department
    {
        public int DeptId { get; set; }
        public string DeptName { get; set; }
        public string studids { get; set; }
        public string DeptIds { get; set; }
        public string Initial { get; set; }
        public string year1 { get; set; }
    }
    [WebMethod]
    public static List<Department> PopulateDropDownList4()
    {
        DataTable dtnew = new DataTable();
        DataTable dtnew1 = new DataTable();
        List<Department> objDept = new List<Department>();

        using (MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString))
        {
            using (MySqlCommand cmdnew = new MySqlCommand("SELECT * from expensesasters ", con))
            {
                if (con.State != ConnectionState.Open) { con.Open(); }
                using (MySqlDataAdapter danew = new MySqlDataAdapter(cmdnew))
                {
                    danew.Fill(dtnew);
                    con.Close();
                    if (dtnew.Rows.Count > 0)
                    {
                        for (int i = 0; i < dtnew.Rows.Count; i++)
                        {
                            objDept.Add(new Department
                            {
                                DeptId = Convert.ToInt32(dtnew.Rows[i]["id"]),
                                DeptName = dtnew.Rows[i]["expenses"].ToString(),

                            });
                        }
                    }
                }
            }
        }
        return objDept;
    }
    [WebMethod]
    public static List<Department> PopulateDropDownList5()
    {
        DataTable dtnew = new DataTable();
        DataTable dtnew1 = new DataTable();
        List<Department> objDept = new List<Department>();

        using (MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString))
        {
            using (MySqlCommand cmdnew = new MySqlCommand("SELECT * from class_master ", con))
            {
                if (con.State != ConnectionState.Open) { con.Open(); }
                using (MySqlDataAdapter danew = new MySqlDataAdapter(cmdnew))
                {
                    danew.Fill(dtnew);
                    con.Close();
                    if (dtnew.Rows.Count > 0)
                    {
                        for (int i = 0; i < dtnew.Rows.Count; i++)
                        {
                            objDept.Add(new Department
                            {
                                DeptId = Convert.ToInt32(dtnew.Rows[i]["id"]),
                                DeptName = dtnew.Rows[i]["class"].ToString(),

                            });
                        }
                    }
                }
            }
        }
        return objDept;
    }
    [WebMethod]

    public static object GetData(string pdate, string pdate1, string incomeid, string clsid)
    {
        string data = "";
        if (pdate != "")
        {
            date1 = DateTime.Parse(pdate);
        }
        if (pdate1 != "")
        {
            date2 = DateTime.Parse(pdate1);
        }

        if (incomeid == "0" && clsid != "0")
        {
            using (MySqlConnection con = new MySqlConnection(constring))
            {
                using (MySqlCommand cmd = new MySqlCommand("select expenses_setup.*,expensesasters.expenses as expnm,date_format(expenses_setup.paydate,'%d-%m-%Y') as incpaydt,class_master.class as clsnm from  expenses_setup left join expensesasters on expenses_setup.exepnses = expensesasters.id left join class_master on expenses_setup.clsid = class_master.id  where expenses_setup.today between '" + date1.ToString("yyyy/MM/dd") + "' and '" + date2.ToString("yyyy/MM/dd") + "' and expenses_setup.clsid = '"+ clsid +"'", con))
                {
                    if (con.State != ConnectionState.Open) { con.Open(); }
                    DataSet ds = new DataSet();
                    using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
                    {
                        da.Fill(ds);
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            data = JsonConvert.SerializeObject(ds.Tables[0]);
                        }
                        con.Close();
                        con.Dispose();
                        cmd.Dispose();
                    }
                }
            }
        }
       else if (incomeid != "0" && clsid != "0")
        {
            using (MySqlConnection con = new MySqlConnection(constring))
            {
                using (MySqlCommand cmd = new MySqlCommand("select expenses_setup.*,expensesasters.expenses as expnm,date_format(expenses_setup.paydate,'%d-%m-%Y') as incpaydt,class_master.class as clsnm from  expenses_setup left join expensesasters on expenses_setup.exepnses = expensesasters.id left join class_master on expenses_setup.clsid = class_master.id where expenses_setup.today between '" + date1.ToString("yyyy/MM/dd") + "' and '" + date2.ToString("yyyy/MM/dd") + "' and expenses_setup.exepnses ='" + incomeid + "' and expenses_setup.clsid = '" + clsid + "'", con))
                {
                    if (con.State != ConnectionState.Open) { con.Open(); }
                    DataSet ds = new DataSet();
                    using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
                    {
                        da.Fill(ds);
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            data = JsonConvert.SerializeObject(ds.Tables[0]);
                        }
                        con.Close();
                        con.Dispose();
                        cmd.Dispose();
                    }
                }
            } 
       }
           else if (incomeid == "0" && clsid == "0")
       {
                 using (MySqlConnection con = new MySqlConnection(constring))
            {
                using (MySqlCommand cmd = new MySqlCommand("select expenses_setup.*,expensesasters.expenses as expnm,date_format(expenses_setup.paydate,'%d-%m-%Y') as incpaydt,class_master.class as clsnm from  expenses_setup left join expensesasters on expenses_setup.exepnses = expensesasters.id left join class_master on expenses_setup.clsid = class_master.id  where expenses_setup.today between '" + date1.ToString("yyyy/MM/dd") + "' and '" + date2.ToString("yyyy/MM/dd") + "'", con))
                {
                    if (con.State != ConnectionState.Open) { con.Open(); }
                    DataSet ds = new DataSet();
                    using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
                    {
                        da.Fill(ds);
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            data = JsonConvert.SerializeObject(ds.Tables[0]);
                        }
                        con.Close();
                        con.Dispose();
                        cmd.Dispose();
                    }
                }
            }
           }
      
        return data;
    }
    [WebMethod]

    public static object GetDataExports(string pdate, string pdate1, string incomeid,string clsid)
    {
        string data = "";
        if (pdate != "")
        {
            date1 = DateTime.Parse(pdate);
        }
        if (pdate1 != "")
        {
            date2 = DateTime.Parse(pdate1);
        }
        if (clsid != "0" && incomeid == "0")
        {
            using (MySqlConnection con = new MySqlConnection(constring))
            {
                using (MySqlCommand cmd = new MySqlCommand("select expenses_setup.*,expensesasters.expenses as expnm,date_format(expenses_setup.paydate,'%d-%m-%Y') as incpaydt,class_master.class as clsnm from  expenses_setup left join expensesasters on expenses_setup.exepnses = expensesasters.id left join class_master on expenses_setup.clsid = class_master.id  where expenses_setup.today between '" + date1.ToString("yyyy/MM/dd") + "' and '" + date2.ToString("yyyy/MM/dd") + "' and expenses_setup.clsid = '" + clsid + "'", con))
                {
                    if (con.State != ConnectionState.Open) { con.Open(); }
                    DataSet ds = new DataSet();
                    using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
                    {
                        da.Fill(ds);
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            data = JsonConvert.SerializeObject(ds.Tables[0]);
                        }
                        con.Close();
                        con.Dispose();
                        cmd.Dispose();
                    }
                }
            }
        }
        else if (incomeid != "0" && clsid != "0") 
        {
            using (MySqlConnection con = new MySqlConnection(constring))
            {
                using (MySqlCommand cmd = new MySqlCommand("select expenses_setup.*,expensesasters.expenses as expnm,date_format(expenses_setup.paydate,'%d-%m-%Y') as incpaydt,class_master.class as clsnm from  expenses_setup left join expensesasters on expenses_setup.exepnses = expensesasters.id left join class_master on expenses_setup.clsid = class_master.id where expenses_setup.today between '" + date1.ToString("yyyy/MM/dd") + "' and '" + date2.ToString("yyyy/MM/dd") + "' and expenses_setup.exepnses ='" + incomeid + "' and expenses_setup.clsid = '" + clsid + "'", con))
                {
                    if (con.State != ConnectionState.Open) { con.Open(); }
                    DataSet ds = new DataSet();
                    using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
                    {
                        da.Fill(ds);
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            data = JsonConvert.SerializeObject(ds.Tables[0]);
                        }
                        con.Close();
                        con.Dispose();
                        cmd.Dispose();
                    }
                }
            }
        }
        else if (incomeid == "0" && clsid == "0")
        {
            using (MySqlConnection con = new MySqlConnection(constring))
            {
                using (MySqlCommand cmd = new MySqlCommand("select expenses_setup.*,expensesasters.expenses as expnm,date_format(expenses_setup.paydate,'%d-%m-%Y') as incpaydt,class_master.class as clsnm from  expenses_setup left join expensesasters on expenses_setup.exepnses = expensesasters.id left join class_master on expenses_setup.clsid = class_master.id where expenses_setup.today between '" + date1.ToString("yyyy/MM/dd") + "' and '" + date2.ToString("yyyy/MM/dd") + "'", con))
                {
                    if (con.State != ConnectionState.Open) { con.Open(); }
                    DataSet ds = new DataSet();
                    using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
                    {
                        da.Fill(ds);
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            data = JsonConvert.SerializeObject(ds.Tables[0]);
                        }
                        con.Close();
                        con.Dispose();
                        cmd.Dispose();
                    }
                }
            }
        }
        return data;
    }
}