using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace accounting.reports
{
    public partial class ProfitAndLossReport : Form
    {
        public ProfitAndLossReport()
        {
            InitializeComponent();
        }
        string id ="";
        Int64 amount = 0;

        Int64 grossProfit = 0;
        Int64 OperatingProfit=0;
        Int64 ProfitBeforeTax=0;
        Int64 netProfit = 0;

        string h1 = "";
        string h2 = "";
        private void button9_Click(object sender, EventArgs e)
        {
            Close();
        }
  
        private void ProfitAndLossReport_Load(object sender, EventArgs e)
        {
            comboBox1.Text = FinancialYear.beginHistoryOfFinancialYear.Substring(8, 2);
            comboBox2.Text = FinancialYear.beginHistoryOfFinancialYear.Substring(5, 2);
            comboBox3.Text = FinancialYear.beginHistoryOfFinancialYear.Substring(0, 4);

            comboBox6.Text = historys.days(historys.day);
            comboBox7.Text = historys.months(historys.month);
            comboBox8.Text = historys.year;

            h1 = comboBox3.Text + "/" + comboBox2.Text + "/" + comboBox1.Text;
            h2 = comboBox8.Text + "/" + comboBox7.Text + "/" + comboBox6.Text;

            string str = "SELECT dbo.profitAndLoss.id, dbo.profitAndLoss.title, SUM(dbo.detailDocumentAccounting.credit) - SUM(dbo.detailDocumentAccounting.debit) AS remaning FROM dbo.profitAndLoss INNER JOIN dbo.detailProfitAndLoss ON dbo.profitAndLoss.id = dbo.detailProfitAndLoss.code INNER JOIN dbo.detailDocumentAccounting ON dbo.detailProfitAndLoss.codingAccounting = dbo.detailDocumentAccounting.delegateCode AND dbo.detailDocumentAccounting.history BETWEEN '"+h1+"' AND '"+h2+"'  GROUP BY dbo.profitAndLoss.id,dbo.profitAndLoss.title";
            dataGridView2.DataSource = data.dAdapter("ProfitAndLossReport__", str);

            dataGridView1.DataSource = data.dAdapter("profitAndLossReport", "select * from profitAndLoss order by id");
            grid();
            
            foreach (DataGridViewColumn column in dataGridView1.Columns)
            {
                column.SortMode = DataGridViewColumnSortMode.NotSortable;
            }

        }

        public void calculate()
        {
           
            grossProfit = 0;
            grossProfit +=Convert.ToInt64(dataGridView1.Rows[0].Cells["one"].Value);
            grossProfit += Convert.ToInt64(dataGridView1.Rows[1].Cells["one"].Value);
            dataGridView1.Rows[2].Cells["one"].Value= grossProfit;

            if(grossProfit<0)
            { 
                dataGridView1.Rows[2].Cells["one"].Style.ForeColor = Color.Red; 
            }
            else
            {
                dataGridView1.Rows[2].Cells["one"].Style.ForeColor = Color.Black;
            }
            dataGridView1.Rows[2].DefaultCellStyle.Font = new Font("Tahoma", 8, FontStyle.Bold);
            //---------------------------------------------------------------------------------------------------------
            OperatingProfit = grossProfit;
            OperatingProfit += Convert.ToInt64(dataGridView1.Rows[3].Cells["one"].Value);
            OperatingProfit += Convert.ToInt64(dataGridView1.Rows[4].Cells["one"].Value);
            OperatingProfit += Convert.ToInt64(dataGridView1.Rows[5].Cells["one"].Value);
            dataGridView1.Rows[6].Cells["one"].Value = OperatingProfit;

            if (OperatingProfit < 0)
            {
                dataGridView1.Rows[6].Cells["one"].Style.ForeColor = Color.Red;
            }
            else
            {
                dataGridView1.Rows[6].Cells["one"].Style.ForeColor = Color.Black;
            }
            dataGridView1.Rows[6].DefaultCellStyle.Font = new Font("Tahoma", 8, FontStyle.Bold);
            //---------------------------------------------------------------------------------------------------------
            ProfitBeforeTax = OperatingProfit;
            ProfitBeforeTax += Convert.ToInt64(dataGridView1.Rows[7].Cells["one"].Value);
            ProfitBeforeTax += Convert.ToInt64(dataGridView1.Rows[8].Cells["one"].Value);
            dataGridView1.Rows[9].Cells["one"].Value = ProfitBeforeTax;

            if (ProfitBeforeTax < 0)
            {
                dataGridView1.Rows[9].Cells["one"].Style.ForeColor = Color.Red;
            }
            else
            {
                dataGridView1.Rows[9].Cells["one"].Style.ForeColor = Color.Black;
            }
            dataGridView1.Rows[9].DefaultCellStyle.Font = new Font("Tahoma", 8, FontStyle.Bold);
            //---------------------------------------------------------------------------------------------------------
            netProfit = ProfitBeforeTax;
            netProfit += Convert.ToInt64(dataGridView1.Rows[10].Cells["one"].Value);
            dataGridView1.Rows[11].Cells["one"].Value = netProfit;

            if (netProfit < 0)
            {
                dataGridView1.Rows[11].Cells["one"].Style.ForeColor = Color.Red;

            }
            else
            {
                dataGridView1.Rows[11].Cells["one"].Style.ForeColor = Color.Black;
            }
            dataGridView1.Rows[11].DefaultCellStyle.Font = new Font("Tahoma", 8, FontStyle.Bold);

        }
        public void grid()
        {

            dataGridView1.Columns["id"].HeaderText = "کد";
            dataGridView1.Columns["title"].HeaderText = "عنوان";
            dataGridView1.Columns["one"].HeaderText = "ریال";



            dataGridView1.Columns["id"].Width = 70;
            dataGridView1.Columns["title"].Width = 360;
            dataGridView1.Columns["one"].Width = 150;




            dataGridView1.Columns["two"].Visible = false;
            dataGridView1.Columns["status"].Visible = false;
            dataGridView1.Columns["storeCode"].Visible = false;

            dataGridView1.Columns["one"].DefaultCellStyle.Format = "N0";
            dataGridView1.Columns["two"].DefaultCellStyle.Format = "N0";




        }

        private void ProfitAndLossReport_Shown(object sender, EventArgs e)
        {
            for(int i=0; i<dataGridView1.RowCount; i++)
            {
                id = dataGridView1.Rows[i].Cells["id"].Value.ToString();
                amount = 0;
                for(int j=0; j<dataGridView2.RowCount; j++)
                {
                    if(dataGridView2.Rows[j].Cells["id"].Value.ToString()==id)
                    {
                        amount = Int64.Parse(dataGridView2.Rows[j].Cells["remaning"].Value.ToString());
                        break;
                    }
                    else
                    {
                        amount = 0;
                    }


                }
                dataGridView1.Rows[i].Cells["one"].Value = amount;

            }

            calculate();
        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                detailProfitAndLossReport mn = new detailProfitAndLossReport();
                mn.id = dataGridView1.Rows[e.RowIndex].Cells["id"].Value.ToString();
                mn.ShowDialog();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //if (versionControl.demostatus == false)
            //{
                profitAndLoss.SetParameterValue("storeName", store.storeName);
                profitAndLoss.SetParameterValue("h1", comboBox3.Text + "/" + comboBox2.Text + "/" + comboBox1.Text);
                profitAndLoss.SetParameterValue("h2", comboBox8.Text + "/" + comboBox7.Text + "/" + comboBox6.Text);
                //factorReport.SetParameterValue("image", null);
                //factorReport.SetParameterValue("phone", store.phone);
                //factorReport.SetParameterValue("fax", store.fax);
                //factorReport.SetParameterValue("mobile", store.mobile);
                //factorReport.SetParameterValue("address", store.address);
                //factorReport.SetParameterValue("storeSite", store.site);
                //factorReport.SetParameterValue("info", info);
                //factorReport.SetParameterValue("accountside", textBox2.Text.Trim());
                //factorReport.SetParameterValue("accountsideMaliCode", data.dAdapterEnter("accountsideMaliCode", "accountSide", "id", "maliCode", textBox1.Text.Trim()));
                //factorReport.SetParameterValue("accountsideAddriss", data.dAdapterEnter("accountsideAddriss", "accountSide", "id", "addriss", textBox1.Text.Trim()));
                //factorReport.SetParameterValue("storeSide", store.manager);
                // factorReport.SetParameterValue("image",Convert.ToByte(pictureBox5.Image));



                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
                        new DataColumn("title", typeof(string)),
                        new DataColumn("one",typeof(Int64)) });
                for (int i = 0; i < dataGridView1.RowCount; i++)
                {

                    dt.Rows.Add(i, dataGridView1.Rows[i].Cells["title"].Value.ToString(), Convert.ToInt64(dataGridView1.Rows[i].Cells["one"].Value.ToString()));
                }

                profitAndLoss.RegisterData(dt, "profitAndLoss");
                if (setting.showPreviewPrint)
                {
                    profitAndLoss.Show();
                }
                else
                {
                    profitAndLoss.PrintSettings.ShowDialog = false;
                    profitAndLoss.PrintSettings.Copies = 1;
                    profitAndLoss.Print();
                }

            //}
            //else
            //{
            //    MessageBox.Show(".در نسخه آموزش ( دمو ) امکان فوق مقدور نیست ", "پیام برنامه", MessageBoxButtons.OK, MessageBoxIcon.Hand);

            //}

        }

        private void button8_Click(object sender, EventArgs e)
        {
            h1 = comboBox3.Text + "/" + comboBox2.Text + "/" + comboBox1.Text;
            h2 = comboBox8.Text + "/" + comboBox7.Text + "/" + comboBox6.Text;

            string str = "SELECT dbo.profitAndLoss.id, dbo.profitAndLoss.title, SUM(dbo.detailDocumentAccounting.credit) - SUM(dbo.detailDocumentAccounting.debit) AS remaning FROM dbo.profitAndLoss INNER JOIN dbo.detailProfitAndLoss ON dbo.profitAndLoss.id = dbo.detailProfitAndLoss.code INNER JOIN dbo.detailDocumentAccounting ON dbo.detailProfitAndLoss.codingAccounting = dbo.detailDocumentAccounting.delegateCode AND dbo.detailDocumentAccounting.history BETWEEN '" + h1 + "' AND '" + h2 + "'  GROUP BY dbo.profitAndLoss.id,dbo.profitAndLoss.title";
            dataGridView2.DataSource = data.dAdapter("ProfitAndLossReport__", str);

            dataGridView1.DataSource = data.dAdapter("profitAndLossReport", "select * from profitAndLoss order by id ");

            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                id = dataGridView1.Rows[i].Cells["id"].Value.ToString();
                amount = 0;
                for (int j = 0; j < dataGridView2.RowCount; j++)
                {
                    if (dataGridView2.Rows[j].Cells["id"].Value.ToString() == id)
                    {
                        amount = Int64.Parse(dataGridView2.Rows[j].Cells["remaning"].Value.ToString());
                        break;
                    }
                    else
                    {
                        amount = 0;
                    }


                }
                dataGridView1.Rows[i].Cells["one"].Value = amount;

            }

            calculate();

        }

        private void button10_Click(object sender, EventArgs e)
        {
            comboBox1.Text = "01";
            comboBox2.Text = "01";
            comboBox3.Text = historys.year;

            comboBox6.Text = historys.days(historys.day);
            comboBox7.Text = historys.months(historys.month);
            comboBox8.Text = historys.year;

            h1 = comboBox3.Text + "/" + comboBox2.Text + "/" + comboBox1.Text;
            h2 = comboBox8.Text + "/" + comboBox7.Text + "/" + comboBox6.Text;

            h1 = comboBox3.Text + "/" + comboBox2.Text + "/" + comboBox1.Text;
            h2 = comboBox8.Text + "/" + comboBox7.Text + "/" + comboBox6.Text;

            string str = "SELECT dbo.profitAndLoss.id, dbo.profitAndLoss.title, SUM(dbo.detailDocumentAccounting.credit) - SUM(dbo.detailDocumentAccounting.debit) AS remaning FROM dbo.profitAndLoss INNER JOIN dbo.detailProfitAndLoss ON dbo.profitAndLoss.id = dbo.detailProfitAndLoss.code INNER JOIN dbo.detailDocumentAccounting ON dbo.detailProfitAndLoss.codingAccounting = dbo.detailDocumentAccounting.delegateCode AND dbo.detailDocumentAccounting.history BETWEEN '" + h1 + "' AND '" + h2 + "'  GROUP BY dbo.profitAndLoss.id,dbo.profitAndLoss.title";
            dataGridView2.DataSource = data.dAdapter("ProfitAndLossReport__", str);

            dataGridView1.DataSource = data.dAdapter("profitAndLossReport", "select * from profitAndLoss order by id");

            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                id = dataGridView1.Rows[i].Cells["id"].Value.ToString();
                amount = 0;
                for (int j = 0; j < dataGridView2.RowCount; j++)
                {
                    if (dataGridView2.Rows[j].Cells["id"].Value.ToString() == id)
                    {
                        amount = Int64.Parse(dataGridView2.Rows[j].Cells["remaning"].Value.ToString());
                        break;
                    }
                    else
                    {
                        amount = 0;
                    }


                }
                dataGridView1.Rows[i].Cells["one"].Value = amount;

            }

            calculate();
        }

        private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            
        }
    }
}
