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 frmFactoreforoosh : Form
    {
        int intFlagplus = 0;
        string strDate = "", strNameID = "", strFactorID = "", strGeimatekharid = "", strGeimateforoosh = "", strGeimateKol = "", strTedad = "";
        
        public frmFactoreforoosh()
        {
            InitializeComponent();
        }


        public void cmbFil()
        {

            clsForCombo cmbP = new clsForCombo();
            comboBox1.DataSource = cmbP.dbConnection2(@"SELECT     FactorForushID, DtOfSodoor, MemBerID, ClosedBit FROM   FactForooshID WHERE     (ClosedBit = 1)");
            comboBox1.ValueMember = "FactorForushID";
            comboBox1.DisplayMember = "FactorForushID";

        }

        public void cmbFil2()
        {

            clsForCombo cmbP1 = new clsForCombo();
            comboBox2.DataSource = cmbP1.dbConnection2(@"SELECT     TOP 100 PERCENT GoodsID, Title  FROM  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     FactForooshID.FactorForushID, FactForooshID.DtOfSodoor, FactForooshID.MemBerID, FactForooshID.ClosedBit, Members.Name, 
                      Members.Name + '  ' + Members.Sname AS ex1
                      FROM    FactForooshID INNER JOIN
                      Members ON FactForooshID.MemBerID = Members.MemBerID
                      WHERE     (FactForooshID.ClosedBit = 1) AND (FactForooshID.FactorForushID = '" + 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     Goods.GoodsID, Goods.Title, Goods.PurchasePrice, Subjects.Subject, FactoreForoosh.Quantity1, FactoreForoosh.salePrice, 
                      FactoreForoosh.TotalPRICE, FactoreForoosh.BefitLoss
                       FROM   FactoreForoosh INNER JOIN
                      Goods ON FactoreForoosh.GoodsID = Goods.GoodsID INNER JOIN
                      Subjects ON Goods.TopicID = Subjects.SubId
                      WHERE     (FactoreForoosh.FactoreForooshID = '" + strFactorID + "')");

                dataGridView1.DataSource = dt;
                //strClear();
        }


        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 = "";
            sood.Text = "";
            textBox3.Text = "";
        }

        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         Goods  WHERE     (GoodsID = N'" + comboBox2.SelectedValue.ToString() + "') ORDER BY GoodsID");
            textBox3.Text = Convert.ToString(float.Parse(dt4.Rows[0][1].ToString()));
            strGeimatekharid = dt4.Rows[0][1].ToString();

        }

        public void dbIns()
        {
            FirstClass dbIns1 = new FirstClass();
            dbIns1.cmd.Parameters.Add("@FactoreForooshID" , 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("@salePrice", SqlDbType.Float).Value = float.Parse(textBox4.Text);
            dbIns1.cmd.Parameters.Add("@TotalPRICE", SqlDbType.Float).Value = float.Parse(textBox6.Text);
            dbIns1.cmd.Parameters.Add("@BefitLoss", SqlDbType.Float).Value = float.Parse(sood.Text);
            dbIns1.exeCommand("sp_FactoreForoosh_Insert");
        }

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

            dtTot = dbTot.dbOut(@"SELECT     SUM(TotalPRICE) AS KolleFactor, SUM(BefitLoss) AS kooleSood
                                    FROM         FactoreForoosh
                                    WHERE     (FactoreForooshID = '" + strFactorID + "')");

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

        public float mojoodiCheck()
        {
            float fltMizanemojoodiKala = 0;
            FirstClass mjCheck = new FirstClass();
            DataTable mjTedad = new DataTable();

            mjTedad = mjCheck.dbOut("SELECT     GoodsID, totalMojoodi FROM MizaneMojoodi WHERE (GoodsID = N'" + comboBox2.SelectedValue.ToString() + "')");

            fltMizanemojoodiKala = float.Parse(mjTedad.Rows[0][1].ToString());
            return fltMizanemojoodiKala;
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            float mojoodi101 = 0;
            mojoodi101 = mojoodiCheck();
            if (mojoodi101 - float.Parse(textBox5.Text) < 0)
            {
                MessageBox.Show("میزان موجودی کافی نیست" + "\n" + "میزان موجودی = " + mojoodi101.ToString());
            }
            else
            {

                if (textBox4.Text != "" && textBox5.Text != "" && textBox3.Text != "")
                {
                    dbIns();
                    grdFill();
                    strClear();
                }
                else
                {
                    MessageBox.Show("مقادیر تمامی فیلدها بایستی وارد شوند");
                }
            }
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            strGeimateforoosh =  textBox4.Text;
            if (textBox4.Text != "" && textBox5.Text != "" && textBox3.Text != "")
            {

                textBox6.Text = "";
                textBox6.Text = Convert.ToString(float.Parse(strGeimateforoosh) * float.Parse(strTedad));
                sood.Text = Convert.ToString((float.Parse(textBox4.Text) - float.Parse(textBox3.Text)) * float.Parse(strTedad));
                ttlSoodFact();
            }

        }

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

            if (textBox4.Text != "" && textBox5.Text != "" && textBox3.Text != "")
            {
                textBox6.Text = Convert.ToString(float.Parse(strGeimateforoosh) * float.Parse(strTedad));
                sood.Text = Convert.ToString((float.Parse(textBox4.Text) - float.Parse(textBox3.Text)) * float.Parse(strTedad));
                ttlSoodFact();
            }

        }

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

    }
}