private void SaveOrder()
        {
            var Vaccines = new Dictionary <Int32, Double>();

            foreach (DataGridViewRow vRow in this.dgvVaccine.Rows)
            {
                var StockID = Convert.ToInt32(vRow.Cells["colVaccineStockID"].Value);
                var Unit    = Convert.ToDouble(vRow.Cells["colVaccineUnit"].Value);
                if (Vaccines.ContainsKey(StockID))
                {
                    Vaccines[StockID] += Unit;
                }
                else
                {
                    Vaccines.Add(StockID, Unit);
                }
            }

            var Msg = new StringBuilder();

            foreach (KeyValuePair <Int32, Double> Vaccine in Vaccines)
            {
                Double?CurrentUnit = this.stockTableAdapter.QueryCurrentUnit(Vaccine.Key);
                if (CurrentUnit.Value < Vaccine.Value)
                {
                    Msg.Append(String.Format("{0} จำนวนปัจจุบัน:{1} จำนวนที่ต้องการ:{2}\n", stockTableAdapter.GetDataByPK(Vaccine.Key)[0].Name, CurrentUnit, Vaccine.Value));
                }
            }

            if (Msg.Length != 0)
            {
                MessageBox.Show(String.Format("พัสดุไม่พอ \n{0}\n กรุณาลองใหม่อีกครั้ง", Msg), "ข้อผิดผลาด", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }


            if (CustomerPID == null)
            {
                CustomerPID = (String)this.customerTableAdapter.InsertQuery(
                    this.tbxCustomerPID.Text,
                    this.Prename,
                    this.tbxfName.Text,
                    this.tbxlName.Text,
                    this.Company,
                    this.tbxAddress.Text,
                    this.tbxStreed.Text,
                    this.tbxPhone.Text,
                    this.tbxMobile.Text,
                    this.City,
                    this.District,
                    this.Province,
                    this.PostCode);

                if (CustomerPID != null)
                {
                    UserLogTableAdapter logTableAdapter = new UserLogTableAdapter();
                    logTableAdapter.Insert(TypeUtil.Customer, CustomerPID, MainForm.User.UserID, 0, DateTime.Now);
                }
                else
                {
                    lblFinishHeader.Text      = "ข้อผิดผลาด";
                    lblFinishDescription.Text = "พบข้อผิดผลาดในการบันทึกข้อมูลลูกค้า กรุณาลองใหม่อีกครั้ง";
                    Index = 3;
                    initialWizard();
                    return;
                }
            }

            var PaymentStatus = Convert.ToByte(cbxPaymentStatus.SelectedValue);
            var PaymentType   = Convert.ToByte(cbxPaymentType.SelectedValue);


            int OrderInID = (int)this.orderInTableAdapter.InsertQuery(
                this.OrderBy,
                this.CustomerPID,
                this.OrderBy,
                this.dtpOrderDate.Value,
                null,
                this.TransportType,
                Convert.ToDouble(tbxTransportCost.Text),
                this.tbxTransportNO.Text,
                this.tbxTransportIssueBy.Text,
                Convert.ToDouble(this.tbxUnit.Text),
                Convert.ToDouble(this.tbxAmountPerUnit.Text),
                Convert.ToDouble(this.tbxAmount.Text),
                PaymentStatus,
                PaymentStatus == 0 ? new Byte?() : PaymentType,
                PaymentStatus == 0 ? new Int32?() : Convert.ToByte(this.cbxPaymentBy.SelectedValue),
                PaymentStatus == 0 ? new DateTime?() : dtpPaymentDate.Value,
                cbxBankNO.SelectedValue == null ? new Int32?() : Convert.ToInt32(cbxBankNO.SelectedValue));



            var stockRemoveListTableAdapter = new StockRemoveListTableAdapter();
            var stockRemoveTableAdapter     = new StockRemoveTableAdapter();
            var vaccineTableAdapter         = new VaccineTableAdapter();

            if (OrderInID > 0)
            {
                MessageBox.Show(OrderInID.ToString());
                var log = new UserLogTableAdapter();
                log.Insert(TypeUtil.OrderIn, OrderInID.ToString(), MainForm.User.UserID, 0, DateTime.Now);


                var CowTableAdapter = new CowTableAdapter();
                var CowCount        = 0;
                foreach (DataGridViewRow row in dgvCow.Rows)
                {
                    String CowNO  = String.Format("{0:000}-{1:000}", OrderInID, Convert.ToInt32(row.Cells["colCowID"].Value));
                    Double Amount = Convert.ToDouble(row.Cells["colCowAmount"].Value);

                    CowNO = (String)CowTableAdapter.InsertQuery(
                        CowNO,
                        Convert.ToInt32(row.Cells["colCowCorral"].Value),
                        Convert.ToInt32(row.Cells["colCowGene"].Value),
                        Convert.ToByte(row.Cells["colCowSex"].Value),
                        String.Empty,
                        OrderInID,
                        Amount,
                        new Int32?(),
                        new Double?());


                    if (CowNO != null)
                    {
                        log.Insert(TypeUtil.Cow, CowNO, MainForm.User.UserID, TypeUtil.EVENT_BUY, DateTime.Now);


                        this.corralTableAdapter.UpdateAddCurrent(1, Convert.ToInt32(row.Cells["colCowCorral"].Value));
                        foreach (DataGridViewRow vRow in this.dgvVaccine.Rows)
                        {
                            int VaccineID = (int)vaccineTableAdapter.InsertQuery((Int32)vRow.Cells["colVaccineByID"].Value, CowNO, (Int32)vRow.Cells["colVaccineStockID"].Value, (Double)vRow.Cells["colVaccineUnit"].Value, (DateTime)vRow.Cells["colVaccineDate"].Value);
                            if (VaccineID > 0)
                            {
                                log.Insert(TypeUtil.Vaccine, VaccineID.ToString(), MainForm.User.UserID, 0, DateTime.Now);

                                this.stockTableAdapter.UpdateRemoveUnit((Double)vRow.Cells["colVaccineUnit"].Value, (Int32)vRow.Cells["colVaccineStockID"].Value);
                            }
                        }
                        CowCount++;
                    }
                }



                var StockRemoveID = 0;
                if (Vaccines.Count > 0)
                {
                    StockRemoveID = (int)stockRemoveTableAdapter.InsertQuery(
                        OrderBy,
                        this.dtpOrderDate.Value,
                        1,
                        OrderInID.ToString());//1 เบิกการฉีดวัคซีน
                    log.Insert(TypeUtil.StockRemove, StockRemoveID.ToString(), MainForm.User.UserID, 0, DateTime.Now);
                }

                foreach (KeyValuePair <Int32, Double> Vaccine in Vaccines)
                {
                    stockRemoveListTableAdapter.Insert(StockRemoveID, Vaccine.Key, Vaccine.Value, null);
                }

                lblFinishHeader.Text      = "เสร็จสมบูรณ์";
                lblFinishDescription.Text = "บันทึกการซื้อเสร็จสมบูรณ์";
            }
            else
            {
                lblFinishHeader.Text      = "ข้อผิดผลาด";
                lblFinishDescription.Text = "พบข้อผิดผลาดในการบันทึกข้อมูลการซื้อ";
                Index = 4;
                initialWizard();
                return;
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!ValidateChildren())
            {
                return;
            }


            var Stocks = new Dictionary <Int32, Double>();

            foreach (DataGridViewRow row in dgvAdd.Rows)
            {
                var StockID = Convert.ToInt32(row.Cells["StockID"].Value);
                var Unit    = Convert.ToDouble(row.Cells["Unit"].Value);
                if (Stocks.ContainsKey(StockID))
                {
                    Stocks[StockID] += Unit;
                }
                else
                {
                    Stocks.Add(StockID, Unit);
                }
            }

            var Msg = new StringBuilder();

            foreach (KeyValuePair <Int32, Double> Stock in Stocks)
            {
                var CurrentUnit = this.stockTableAdapter.QueryCurrentUnit(Stock.Key);
                if (CurrentUnit < Stock.Value)
                {
                    Msg.Append(String.Format("{0} จำนวนปัจจุบัน:{1} จำนวนที่ต้องการ:{2}\n", stockTableAdapter.GetDataByPK(Stock.Key)[0].Name, CurrentUnit, Stock.Value));
                }
            }

            if (Msg.Length != 0)
            {
                MessageBox.Show(String.Format("พัสดุไม่พอ \n{0}\n กรุณาลองใหม่อีกครั้ง", Msg), "ข้อผิดผลาด", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (DiseaseHistoryID == 0)
            {
                DiseaseHistoryID = (int)this.diseaseHistoryTableAdapter.InsertQuery(
                    Convert.ToInt32(this.cbxBy.SelectedValue),
                    CowNO,
                    this.dtpDate.Value,
                    this.tbxSymptom.Text,
                    this.tbxTreatment.Text);

                if (DiseaseHistoryID > 0)
                {
                    var log = new UserLogTableAdapter();
                    log.Insert(TypeUtil.DiseaseHistory, DiseaseHistoryID.ToString(), MainForm.User.UserID, 0, DateTime.Now);


                    var StockRemoveID     = 0;
                    var stockRemoveResult = 0;
                    if (dgvAdd.Rows.Count > 0)
                    {
                        var stockRemoveTableAdapter = new StockRemoveTableAdapter();
                        StockRemoveID = (int)stockRemoveTableAdapter.InsertQuery(
                            Convert.ToInt32(dgvAdd.Rows[0].Cells["ByID"].Value),
                            (DateTime)dgvAdd.Rows[0].Cells["Date"].Value,
                            2,
                            CowNO);

                        userLogTableAdapter.Insert(TypeUtil.StockRemove, StockRemoveID.ToString(), MainForm.User.UserID, 0, DateTime.Now);
                    }

                    var stockRemoveListTableAdapter = new StockRemoveListTableAdapter();
                    foreach (DataGridViewRow row in dgvAdd.Rows)
                    {
                        int DiseaseMedicine = (int)diseaseMedicineTableAdapter.InsertQuery(
                            Convert.ToInt32(row.Cells["ByID"].Value),
                            DiseaseHistoryID,
                            Convert.ToInt32(row.Cells["StockID"].Value),
                            Convert.ToDouble(row.Cells["Unit"].Value),
                            (DateTime)row.Cells["Date"].Value);
                        if (DiseaseMedicine > 0)
                        {
                            log.Insert(TypeUtil.DiseaseMedicine, DiseaseMedicine.ToString(), MainForm.User.UserID, 0, DateTime.Now);

                            this.stockTableAdapter.UpdateRemoveUnit(Convert.ToDouble(row.Cells["Unit"].Value), Convert.ToInt32(row.Cells["StockID"].Value));
                            if (stockRemoveResult == 1)
                            {
                                stockRemoveListTableAdapter.Insert(StockRemoveID, Convert.ToInt32(row.Cells["StockID"].Value), Convert.ToDouble(row.Cells["Unit"].Value), null);
                            }
                        }
                    }
                    DialogResult = DialogUtil.AddSuccess();
                }
                else
                {
                    DialogResult = DialogUtil.AddError();
                }
            }
            else
            {
                var StockRemoveID = 0;
                if (dgvAdd.Rows.Count > 0)
                {
                    var stockRemoveTableAdapter = new StockRemoveTableAdapter();
                    StockRemoveID = (int)stockRemoveTableAdapter.InsertQuery(
                        Convert.ToInt32(dgvAdd.Rows[0].Cells["ByID"].Value),
                        (DateTime)dgvAdd.Rows[0].Cells["Date"].Value,
                        2,
                        CowNO);

                    userLogTableAdapter.Insert(TypeUtil.StockRemove, StockRemoveID.ToString(), MainForm.User.UserID, 0, DateTime.Now);
                }

                var stockRemoveListTableAdapter = new StockRemoveListTableAdapter();
                foreach (DataGridViewRow row in dgvAdd.Rows)
                {
                    int DiseaseMedicine = (int)diseaseMedicineTableAdapter.InsertQuery(
                        Convert.ToInt32(row.Cells["ByID"].Value),
                        DiseaseHistoryID,
                        Convert.ToInt32(row.Cells["StockID"].Value),
                        Convert.ToDouble(row.Cells["Unit"].Value),
                        (DateTime)row.Cells["Date"].Value);
                    if (DiseaseMedicine < 0)
                    {
                        stockTableAdapter.UpdateRemoveUnit(Convert.ToDouble(row.Cells["Unit"].Value), Convert.ToInt32(row.Cells["StockID"].Value));
                        if (StockRemoveID > 0)
                        {
                            stockRemoveListTableAdapter.Insert(StockRemoveID, Convert.ToInt32(row.Cells["StockID"].Value), Convert.ToDouble(row.Cells["Unit"].Value), null);
                        }
                    }
                }

                var log = new UserLogTableAdapter();
                log.Insert(TypeUtil.DiseaseHistory, DiseaseHistoryID.ToString(), MainForm.User.UserID, 1, DateTime.Now);

                DialogResult = DialogUtil.SaveSuccess();
            }
        }