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_incomereport : 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 string fullname;
    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 incomasters ", 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]["incomes"].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 List<Department> PopulateDropDownList6(string clids)
    {
        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 division_master where classid = '" + clids + "' ", 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]["division"].ToString(),

                            });
                        }
                    }
                }
            }
        }
        return objDept;
    }
    [WebMethod]
    public static List<Department> PopulateDropDownListyras()
    {
        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 *,date_format(fromdate,'%Y-%m-%d') as acdyear1,date_format(todate,'%Y-%m-%d') as acdyear2 from academic_year ", 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]["acdyear1"].ToString(),
                                year1 = dtnew.Rows[i]["acdyear2"].ToString(),
                            });
                        }
                    }
                }
            }
        }
        return objDept;
    }
    [WebMethod]

    public static object GetData(string pdate, string pdate1,string yearsid, string incomeid, string clsid, string divisions)
    {
        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 incomeentry.*,incomasters.incomes as incnm,student_detail.title,student_detail.fname,student_detail.mname,student_detail.lname,class_master.class as clsnm,division_master.division as divnm,date_format(incomeentry.paydate,'%d-%m-%Y') as incpaydt from  incomeentry inner join student_detail on  incomeentry.studid = student_detail.studid AND incomeentry.yreasid = student_detail.admyears AND incomeentry.classes = student_detail.classd  AND incomeentry.divsid = student_detail.division left join incomasters on incomeentry.incomeheads = incomasters.id  left join class_master on incomeentry.classes = class_master.id left join division_master ON incomeentry.divsid = division_master.id where incomeentry.paydate BETWEEN '" + date1.ToString("yyyy/MM/dd") + "' AND '" + date2.ToString("yyyy/MM/dd") + "' and incomeentry.incomeheads ='" + incomeid + "' and incomeentry.classes ='"+ 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
        {
            using (MySqlConnection con = new MySqlConnection(constring))
            {
                using (MySqlCommand cmd = new MySqlCommand("select incomeentry.*,incomasters.incomes as incnm,student_detail.title,student_detail.fname,student_detail.mname,student_detail.lname,class_master.class as clsnm,division_master.division as divnm,date_format(incomeentry.paydate,'%d-%m-%Y') as incpaydt from  incomeentry inner join student_detail on  incomeentry.studid = student_detail.studid AND incomeentry.yreasid = student_detail.admyears AND incomeentry.classes = student_detail.classd  AND incomeentry.divsid = student_detail.division left join incomasters on incomeentry.incomeheads = incomasters.id  left join class_master on incomeentry.classes = class_master.id left join division_master ON incomeentry.divsid = division_master.id where incomeentry.paydate 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 diviions, string yearsids)
    {
        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 incomeentry.*,incomasters.incomes as incnm,student_detail.title,student_detail.fname,student_detail.mname,student_detail.lname,class_master.class as clsnm,division_master.division as divnm,date_format(incomeentry.paydate,'%d-%m-%Y') as incpaydt from  incomeentry inner join student_detail on  incomeentry.studid = student_detail.studid AND incomeentry.yreasid = student_detail.admyears AND incomeentry.classes = student_detail.classd  AND incomeentry.divsid = student_detail.division left join incomasters on incomeentry.incomeheads = incomasters.id  left join class_master on incomeentry.classes = class_master.id left join division_master ON incomeentry.divsid = division_master.id where incomeentry.paydate BETWEEN '" + date1.ToString("yyyy/MM/dd") + "' AND '" + date2.ToString("yyyy/MM/dd") + "' and incomeentry.incomeheads ='" + incomeid + "' and incomeentry.classes ='"+ 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
        {
            using (MySqlConnection con = new MySqlConnection(constring))
            {
                using (MySqlCommand cmd = new MySqlCommand("select incomeentry.*,incomasters.incomes as incnm,student_detail.title,student_detail.fname,student_detail.mname,student_detail.lname,class_master.class as clsnm,division_master.division as divnm,date_format(incomeentry.paydate,'%d-%m-%Y') as incpaydt from  incomeentry inner join student_detail on  incomeentry.studid = student_detail.studid AND incomeentry.yreasid = student_detail.admyears AND incomeentry.classes = student_detail.classd  AND incomeentry.divsid = student_detail.division left join incomasters on incomeentry.incomeheads = incomasters.id  left join class_master on incomeentry.classes = class_master.id left join division_master ON incomeentry.divsid = division_master.id where incomeentry.paydate 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;
    }
}