Ejemplo n.º 1
0
        private void button1_Click(object sender, EventArgs e)
        {
            bool  check1 = true;
            bool  check2 = true;
            bool  check3 = true;
            float a      = 0;

            if (!batch)
            {
                check1 = true;
            }
            if (batch && textBox1.Text.Trim() == "" || textBox1.Text.Trim() == "True")
            {
                MessageBox.Show("Bạn cần điền Batch Name");
                check1 = false;
            }
            if (batch && textBox2.Text.Trim() == "")
            {
                MessageBox.Show("Bạn cần điền Amount");
                check2 = false;
            }

            if (batch && textBox2.Text.Trim() != "")
            {
                try
                {
                    a = float.Parse(textBox2.Text);
                    if (a < 0)
                    {
                        MessageBox.Show("Amount là số dương");
                        check2 = false;
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Amount là chữ số");
                    check2 = false;
                }
            }

            if (batch && textBox1.Text.Trim() != "")
            {
                DataTable dt = bllss4.getBatchNumber(textBox1.Text);
                int       numberOfRecords = dt.Rows.Count;
                if (numberOfRecords != 0)
                {
                    MessageBox.Show("Batch number trùng vui lòng nhập lại");
                    check3 = false;
                }
                else
                {
                    check3 = true;
                }
            }

            if (check1 == true && check2 == true && check3 == true)
            {
                string part   = comboBox3.Text;
                string batch  = textBox1.Text;
                float  amount = float.Parse(textBox2.Text);
                float  sum    = amount;

                DataTable dt   = bllss4.getMiniAmount(part);
                float     mini = int.Parse(dt.Rows[0][0].ToString());

                foreach (DataGridViewRow row1 in dataGridView1.Rows)
                {
                    if ((String)row1.Cells["PartName"].Value == part)
                    {
                        sum += float.Parse(row1.Cells["Amount"].Value.ToString());
                    }
                }
                int i = 1;
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if (sum <= mini)
                    {
                        int rowCount = dataGridView1.Rows.Count - 1;
                        if ((String)row.Cells["PartName"].Value == part && (String)row.Cells["BatchNumber"].Value == batch)
                        {
                            dataGridView1.Rows.Add(comboBox3.Text, textBox1.Text, sum);
                            dataGridView1.Rows.RemoveAt(row.Index);
                            break;
                        }

                        if (i > rowCount)
                        {
                            dataGridView1.Rows.Add(comboBox3.Text, textBox1.Text, textBox2.Text);
                            break;
                        }
                        i++;
                    }
                    else
                    {
                        sum = sum - amount;
                        MessageBox.Show("MiniAmount hiện tại còn: " + (mini - sum));
                        break;
                    }
                }
            }
        }