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

namespace Nano_System.Factorha
{
    public partial class frmFactoreKharid : Form
    {
        int intFlagplus = 0;
        string strDate = "", strNameID = "", strFactorID = "", strGeimatekharid = "", strGeimateforoosh = "", strGeimateKol = "", strTedad = "";

        public frmFactoreKharid()
        {
            InitializeComponent();
        }

        public void cmbFil()
        {
            clsForCombo cmbP = new clsForCombo();
            comboBox1.DataSource = cmbP.dbConnection2(@"SELECT     FactorKharidID, DtOfSodoor, MemBerID, ClosedBit FROM dbo.FactoreKharidID WHERE (ClosedBit = 1)");
            comboBox1.ValueMember = "FactorKharidID";
            comboBox1.DisplayMember = "FactorKharidID";
        }

        public void cmbFil2()
        {
            clsForCombo cmbP1 = new clsForCombo();
            comboBox2.DataSource = cmbP1.dbConnection2(@"SELECT     TOP 100 PERCENT GoodsID, Title  FROM  dbo.Goods  WHERE (TahYeNo = 1)  ORDER BY Title");
            comboBox2.ValueMember = "GoodsID";
            comboBox2.DisplayMember = "Title";
            
        }


        public void txFldAd(string FactorID)
        {
            FirstClass dbTxt = new FirstClass();
            DataTable dt88 = new DataTable();

            dt88 = dbTxt.dbOut(@"SELECT     dbo.FactoreKharidID.FactorKharidID, dbo.FactoreKharidID.DtOfSodoor, dbo.FactoreKharidID.MemBerID, dbo.FactoreKharidID.ClosedBit, 
                                dbo.Members.Name, dbo.Members.Name + '  ' + dbo.Members.Sname AS ex1 FROM dbo.Members INNER JOIN dbo.FactoreKharidID ON dbo.Members.MemBerID = dbo.FactoreKharidID.MemBerID
                                WHERE     (dbo.FactoreKharidID.ClosedBit = 1) AND (dbo.FactoreKharidID.FactorKharidID = '" + FactorID + "')");
            if (intFlagplus == 0)
            {
                intFlagplus = 1;
                strNameID = dt88.Rows[0][2].ToString();
                strDate = dt88.Rows[0][1].ToString();
                textBox1.Text = dt88.Rows[0][5].ToString();
                textBox2.Text = dt88.Rows[0][1].ToString();
            }
        }

        public void grdFill()
        {

                FirstClass grd1 = new FirstClass();
                DataTable dt = new DataTable();

                
                dt = grd1.dbOut(@"SELECT     dbo.Goods.GoodsID, dbo.Goods.Title,  dbo.Subjects.Subject, dbo.FactoreKharid.Quantity1, dbo.FactoreKharid.PurchPrice, 
                        dbo.FactoreKharid.TotalPRICE
                        FROM         dbo.FactoreKharid INNER JOIN
                                              dbo.Goods ON dbo.FactoreKharid.GoodsID = dbo.Goods.GoodsID INNER JOIN
                                              dbo.Subjects ON dbo.Goods.TopicID = dbo.Subjects.SubId
                        WHERE     (dbo.FactoreKharid.FactoreKaridID = '" + strFactorID + "')");

                dataGridView1.DataSource = dt;
        }


        private void comboBox1_DropDownClosed(object sender, EventArgs e)
        {
            try
            {
                object aa = null;
                aa = MessageBox.Show("آیا کد انتخاب شده صحیح است " + "\n" + " پس از تائید کد فاکتور قفل خواهد شد", "", MessageBoxButtons.OKCancel);

                if (string.Compare(aa.ToString(), "Ok") == 1)
                {
                    txFldAd(comboBox1.SelectedValue.ToString());
                    strFactorID = comboBox1.SelectedValue.ToString();
                    comboBox1.Enabled = false;
                    panel2.Enabled = true;
                    closFactID();
                }
            }
            catch { }
        }


        private void frmFactoreforoosh_Load(object sender, EventArgs e)
        {
            panel2.Enabled = false;
            cmbFil();
            cmbFil2();
            grdFill();

        }

        public void strClear()
        {
            textBox6.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
        }



        public void dbIns()
        {
            FirstClass dbIns1 = new FirstClass();
            dbIns1.cmd.Parameters.Add("@FactoreKaridID", SqlDbType.Int).Value = int.Parse(strFactorID);
            dbIns1.cmd.Parameters.Add("@GoodsID", SqlDbType.NVarChar).Value = comboBox2.SelectedValue.ToString();
            dbIns1.cmd.Parameters.Add("@Quantity1", SqlDbType.Float).Value = float.Parse(textBox5.Text);
            dbIns1.cmd.Parameters.Add("@PurchPrice", SqlDbType.Float).Value = float.Parse(textBox4.Text);
            dbIns1.cmd.Parameters.Add("@TotalPRICE", SqlDbType.Float).Value = float.Parse(textBox6.Text);
            dbIns1.exeCommand("sp_FactoreKharid_Insert");
        }

        public void ttlSoodFact()
        {
            FirstClass dbTot = new FirstClass();
            DataTable dtTot = new DataTable();

            dtTot = dbTot.dbOut(@"SELECT     SUM(TotalPRICE) AS KolleFactor
                                    FROM         dbo.FactoreKharid
                                    WHERE     (FactoreKaridID = '" + strFactorID + "')");

            textBox7.Text = dtTot.Rows[0][0].ToString();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox4.Text != "" && textBox5.Text != "")
            {
                dbIns();
                grdFill();
                strClear();
                ttlSoodFact();
            }
            else
            {
                MessageBox.Show("مقادیر تمامی فیلدها بایستی وارد شوند");
            }
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            strTedad = textBox5.Text;

            if (textBox4.Text != "" && textBox5.Text != "")
            {
                textBox6.Text = Convert.ToString(float.Parse(textBox4.Text) * float.Parse(textBox5.Text));
            }

        }

        public void closFactID()
        {
            FirstClass facClos = new FirstClass();
            facClos.cmd.Parameters.Add("@FactorKharidID", SqlDbType.Int).Value = int.Parse(strFactorID);
            facClos.cmd.Parameters.Add("@ClosedBit" , SqlDbType.Bit).Value = 0;
            facClos.exeCommand("sp_FactoreKharidID_Update");
         }

        private void comboBox2_DropDownClosed(object sender, EventArgs e)
        {
            strClear();
            FirstClass db4 = new FirstClass();
            DataTable dt4 = new DataTable();
            dt4 = db4.dbOut("SELECT     TOP 100 PERCENT GoodsID, PurchasePrice FROM         dbo.Goods  WHERE     (GoodsID = N'" + comboBox2.SelectedValue.ToString() + "') ORDER BY GoodsID");
            textBox4.Text = Convert.ToString(float.Parse(dt4.Rows[0][1].ToString()));
            strGeimatekharid = dt4.Rows[0][1].ToString();
        }

    }
}