private void btnSave_Click(object sender, EventArgs e)
        {
            if (OrderOutID > 0)
            {
                this.Validate();
                this.deadViewBindingSource.EndEdit();
                this.fKCowOrderOut1BindingSource.EndEdit();
                Int32 result = this.deadViewTableAdapter.Update(this.farmManageDataSet.DeadView);

                if (result == 1)
                {
                    this.DialogResult = DialogUtil.SaveSuccess();
                }
                else
                {
                    this.DialogResult = DialogUtil.SaveError();
                }
            }
            else
            {
                OrderOutID = (int)this.deadViewTableAdapter.InsertQuery(
                    (byte)2,
                    Convert.ToInt32(this.cbxBy.SelectedValue),
                    this.dtpDate.Value,
                    this.tbxDescription.Text);

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

                    foreach (DataGridViewRow row in this.dgvCowAdd.Rows)
                    {
                        Int32  tCorralID = Convert.ToInt32(row.Cells["colCorral"].Value);
                        String tCowNO    = row.Cells["colCowNO"].Value.ToString();


                        int result = this.cowTableAdapter.UpdateCowOrderOut(OrderOutID, new Double?(), tCowNO);
                        if (result == 1)
                        {
                            log.Insert(TypeUtil.Cow, tCowNO, MainForm.User.UserID, TypeUtil.EVENT_DEAD, DateTime.Now);

                            this.corralTableAdapter.UpdateRemoveCurrent(1, tCorralID);
                        }
                    }

                    this.DialogResult = DialogUtil.AddSuccess();
                }
                else
                {
                    this.DialogResult = DialogUtil.AddError();
                }
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (AddStockID == 0)
            {
                if (dgvAddList.Rows.Count == 0)
                {
                    MessageBox.Show("ไม่มีรายการที่เพิ่มอย่างสมบูรณ์ กรุณาเพิ่มหรือป้อนข้อมูลให้ครบ");
                    return;
                }
                for (var i = 0; i < dgvAddList.Rows.Count - 1; i++)
                {
                    if (dgvAddList.Rows[i].Cells[0].Value == null || Convert.ToDouble(dgvAddList.Rows[i].Cells[1].Value) == 0 || Convert.ToDouble(dgvAddList.Rows[i].Cells[2].Value) == 0)
                    {
                        MessageBox.Show("ข้อมูลไม่ครบ กรุณาลองใหม่อีกครั้ง");
                        return;
                    }
                }
                int StockAddID = (int)stockAddTableAdapter.InsertQuery(
                    Convert.ToInt32(cbxBy.SelectedValue),
                    dtpDate.Value,
                    Convert.ToDouble(tbxTotal.Text));

                if (StockAddID > 0)
                {
                    var log = new UserLogTableAdapter();
                    log.Insert(TypeUtil.StockAdd, AddStockID.ToString(), MainForm.User.UserID, TypeUtil.EVENT_CREATE, DateTime.Now);


                    for (var i = 0; i < dgvAddList.Rows.Count; i++)
                    {
                        var stockAddListID = (int)stockAddListTableAdapter.InsertQuery(
                            StockAddID,
                            Convert.ToInt32(dgvAddList.Rows[i].Cells[0].Value),
                            Convert.ToDouble(dgvAddList.Rows[i].Cells[1].Value),
                            Convert.ToDouble(dgvAddList.Rows[i].Cells[2].Value),
                            Convert.ToDouble(dgvAddList.Rows[i].Cells[3].Value),
                            dgvAddList.Rows[i].Cells[4].Value == null ? string.Empty : dgvAddList.Rows[i].Cells[4].Value.ToString());

                        if (stockAddListID > 0)
                        {
                            log.Insert(TypeUtil.StockAddList, stockAddListID.ToString(), MainForm.User.UserID, TypeUtil.EVENT_CREATE, DateTime.Now);

                            stockTableAdapter.UpdateAddUnit(Convert.ToDouble(dgvAddList.Rows[i].Cells[1].Value), Convert.ToInt32(dgvAddList.Rows[i].Cells[0].Value));
                        }
                    }

                    DialogResult = DialogUtil.AddSuccess();
                }
                else
                {
                    DialogResult = DialogUtil.AddError();
                }
            }
        }
        private void btnChange_Click(object sender, EventArgs e)
        {
            if (!tbxPassword.Text.Trim().Equals(tbxConfirmPassword.Text.Trim()))
            {
                MessageBox.Show("รหัสผ่านไม่ตรงกัน, กรุณาลองใหม่อีกครั้ง", "รหัสผ่านไม่ตรงกัน", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else
            {
                if (tbxPassword.Text.Trim().Length < 6)
                {
                    MessageBox.Show("รหัสผ่านสั้นเกินไป, กรุณาลองใหม่อีกครั้ง", "รหัสผ่านสั้นเกินไป", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }

            if (ValidateChildren())
            {
                usersBindingSource.EndEdit();
                if (1 == usersTableAdapter.Update(farmManageDataSet.Users))
                {
                    var log = new UserLogTableAdapter();
                    log.Insert(TypeUtil.User, UserID.ToString(), MainForm.User.UserID, TypeUtil.EVENT_CHANGE_PASSWORD, DateTime.Now);

                    DialogResult = DialogUtil.SaveSuccess();
                }
                else
                {
                    DialogResult = DialogUtil.SaveError();
                }
            }
        }
Ejemplo n.º 4
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!ValidateChildren())
            {
                return;
            }

            String CustomerPID = (String)customerTableAdapter.InsertQuery(
                tbxPID.Text,
                Convert.ToByte(cbxPrename.SelectedValue),
                tbxfName.Text,
                tbxlName.Text,
                cbxCompany.SelectedValue == null ? new Int32?() : Convert.ToInt32(cbxCompany.SelectedValue),
                tbxAddress.Text,
                tbxStreed.Text,
                tbxPhone.Text,
                tbxMobile.Text,
                cbxCityID.SelectedValue == null ? new Int32?() : Convert.ToInt32(cbxCityID.SelectedValue),
                cbxDistrictID.SelectedValue == null ? new Int32?() : Convert.ToInt32(cbxDistrictID.SelectedValue),
                cbxProvinceID.SelectedValue == null ? new Int32?() : Convert.ToInt32(cbxProvinceID.SelectedValue),
                cbxPostCode.SelectedValue == null ? new Int32?() : Convert.ToInt32(cbxPostCode.SelectedValue));

            if (CustomerPID != null)
            {
                UserLogTableAdapter logTableAdapter = new UserLogTableAdapter();
                logTableAdapter.Insert(TypeUtil.Customer, CustomerPID, MainForm.User.UserID, TypeUtil.EVENT_CREATE, DateTime.Now);

                DialogResult = DialogUtil.AddSuccess();
            }
            else
            {
                DialogResult = DialogUtil.AddError();
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            gxbDetail.Enabled = false;
            btnSave.Enabled   = false;
            if (Validate())
            {
                usersBindingSource.EndEdit();
                if (1 == usersTableAdapter.Update(farmManageDataSet.Users))
                {
                    var log = new UserLogTableAdapter();
                    log.Insert(TypeUtil.User, UserID.ToString(), MainForm.User.UserID, TypeUtil.EVENT_MODIFY, DateTime.Now);

                    DialogResult = DialogUtil.SaveSuccess();
                }
                else
                {
                    DialogResult = DialogUtil.SaveError();
                }
            }
            else
            {
                gxbDetail.Enabled = true;
                btnSave.Enabled   = true;
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!ValidateChildren())
            {
                return;
            }

            if (Option)
            {
                OptionByID        = Convert.ToInt32(cbxBy.SelectedValue);
                OptionStockID     = Convert.ToInt32(cbxStock.SelectedValue);
                OptionUnit        = Convert.ToDouble(tbxUnit.Text);
                OptionDate        = this.dtpDate.Value;
                this.DialogResult = System.Windows.Forms.DialogResult.Yes;
            }
            else
            {
                var Msg         = new StringBuilder();
                var CurrentUnit = this.stockTableAdapter.QueryCurrentUnit(Convert.ToInt32(cbxStock.SelectedValue));
                if (CurrentUnit < Convert.ToDouble(tbxUnit.Text))
                {
                    Msg.Append(String.Format("{0} จำนวนปัจจุบัน:{1} จำนวนที่ต้องการ:{2}\n", stockTableAdapter.GetDataByPK(Convert.ToInt32(cbxStock.SelectedValue))[0].Name, CurrentUnit, Convert.ToDouble(tbxUnit.Text)));
                }

                if (Msg.Length != 0)
                {
                    MessageBox.Show(String.Format("พัสดุไม่พอ \n{0}\n กรุณาลองใหม่อีกครั้ง", Msg), "ข้อผิดผลาด", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else
                {
                    int VaccineID = (int)this.vaccineTableAdapter.InsertQuery(
                        Convert.ToInt32(cbxBy.SelectedValue),
                        cbxCow.SelectedValue.ToString(),
                        Convert.ToInt32(cbxStock.SelectedValue),
                        Convert.ToDouble(tbxUnit.Text),
                        dtpDate.Value);


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

                        this.stockTableAdapter.UpdateRemoveUnit(Convert.ToDouble(tbxUnit.Text), Convert.ToInt32(cbxStock.SelectedValue));
                        this.DialogResult = DialogUtil.AddSuccess();
                    }
                    else
                    {
                        this.DialogResult = DialogUtil.AddError();
                    }
                }
            }
        }
Ejemplo n.º 7
0
        private void initalLogin()
        {
            pnlContent.Enabled = false;
            btnLogin.Enabled   = false;

            var UsersTableAdapter = new UsersTableAdapter();
            var users             = UsersTableAdapter.GetDataByUserLogIn(cbxUsername.Text.Trim().ToLower(), tbxPassword.Text.Trim().ToLower());

            if (users.Count == 0)
            {
                this.pnlContent.Enabled = true;
                this.btnLogin.Enabled   = true;
                this.tbxPassword.Text   = String.Empty;
                MessageBox.Show("ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง", "ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                var list = Properties.Settings.Default.fUsernameList;

                var lists = list.Split('|');
                if (lists.Length > 0 && list.Trim().Length != 0)
                {
                    var result = string.Empty;
                    for (var i = 0; i < lists.Length; i++)
                    {
                        if (!cbxUsername.Text.Trim().Equals(lists[i]))
                        {
                            if (i == 0)
                            {
                                result += lists[i];
                            }
                            else
                            {
                                result += "|" + lists[i];
                            }
                        }
                    }

                    result = String.Format("{0}|{1}", cbxUsername.Text.Trim(), result);
                    Properties.Settings.Default.fUsernameList = result;
                    Properties.Settings.Default.Save();
                }
                else
                {
                    Properties.Settings.Default.fUsernameList = cbxUsername.Text;
                    Properties.Settings.Default.Save();
                }
                var log = new UserLogTableAdapter();
                log.Insert(TypeUtil.User, users[0].UserID.ToString(), users[0].UserID, TypeUtil.EVENT_USER_LOGIN, DateTime.Now);

                MainForm.User = users[0];
                DialogResult  = System.Windows.Forms.DialogResult.Yes;
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            customerBindingSource.EndEdit();
            customerTableAdapter.Update(farmManageDataSet.Customer);
            orderInBindingSource.EndEdit();
            orderInTableAdapter.Update(farmManageDataSet.OrderIn);

            var userLogTableAdapter = new UserLogTableAdapter();

            userLogTableAdapter.Insert(TypeUtil.OrderIn, OrderInID.ToString(), MainForm.User.UserID, 1, DateTime.Now);

            DialogResult = DialogUtil.SaveSuccess();
        }
Ejemplo n.º 9
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            Convert.ToByte(cbxOrderType.SelectedValue);
            Convert.ToByte(cbxPaymentStatus.SelectedValue);

            orderOutBindingSource.EndEdit();
            orderOutTableAdapter.Update(farmManageDataSet.OrderOut);

            var log = new UserLogTableAdapter();

            log.Insert(TypeUtil.OrderOut, OrderOutID.ToString(), MainForm.User.UserID, 1, DateTime.Now);

            DialogResult = DialogUtil.SaveSuccess();
        }
 private void btnSave_Click(object sender, EventArgs e)
 {
     if (!ValidateChildren())
     {
         stockBindingSource.EndEdit();
         int result = stockTableAdapter.Update(farmManageDataSet.Stock);
         if (result == 1)
         {
             var log = new UserLogTableAdapter();
             log.Insert(TypeUtil.Stock, StockID.ToString(), MainForm.User.UserID, TypeUtil.EVENT_MODIFY, DateTime.Now);
         }
         DialogResult = DialogUtil.SaveSuccess();
     }
 }
Ejemplo n.º 11
0
 private void btnSave_Click(object sender, EventArgs e)
 {
     if (ValidateChildren())
     {
         tcMain.Enabled  = false;
         btnSave.Enabled = false;
         customerBindingSource.EndEdit();
         if (1 == customerTableAdapter.Update(farmManageDataSet.Customer))
         {
             var log = new UserLogTableAdapter();
             log.Insert(TypeUtil.Customer, CustomerPID, MainForm.User.UserID, TypeUtil.EVENT_MODIFY, DateTime.Now);
         }
         DialogResult = DialogUtil.SaveSuccess();
     }
 }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!this.ValidateChildren())
            {
                return;
            }

            if (CorralID > 0)
            {
                this.Validate();
                this.corralBindingSource.EndEdit();
                var ResultId = this.corralTableAdapter.Update(farmManageDataSet.Corral);
                if (ResultId == 1)
                {
                    UserLogTableAdapter logTableAdapter = new UserLogTableAdapter();
                    logTableAdapter.Insert(TypeUtil.Corral, CorralID.ToString(), MainForm.User.UserID, TypeUtil.EVENT_MODIFY, DateTime.Now);

                    DialogUtil.SaveSuccess();
                    this.DialogResult = DialogResult.Yes;
                }
                else
                {
                    DialogUtil.SaveError();
                    this.DialogResult = DialogResult.No;
                }
            }
            else
            {
                CorralID = (int)this.corralTableAdapter.InsertQuery(tbxName.Text.Trim(), Convert.ToInt32(tbxMax.Text), Convert.ToInt32(tbxCurrent.Text), tbxDescription.Text.Trim());
                if (CorralID > 0)
                {
                    UserLogTableAdapter logTableAdapter = new UserLogTableAdapter();
                    logTableAdapter.Insert(TypeUtil.Corral, CorralID.ToString(), MainForm.User.UserID, TypeUtil.EVENT_MODIFY, DateTime.Now);

                    DialogUtil.AddSuccess();
                    this.DialogResult = DialogResult.Yes;
                }
                else
                {
                    DialogUtil.AddError();
                    this.DialogResult = DialogResult.No;
                }
            }
        }
Ejemplo n.º 13
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!this.ValidateChildren())
            {
                return;
            }

            if (StockID != 0)
            {
                this.Validate();
                this.stockBindingSource.EndEdit();
                if (1 == stockTableAdapter.Update(farmManageDataSet.Stock))
                {
                    var log = new UserLogTableAdapter();
                    log.Insert(TypeUtil.Stock, StockID.ToString(), MainForm.User.UserID, TypeUtil.EVENT_CREATE, DateTime.Now);


                    DialogResult = DialogUtil.SaveSuccess();
                }
                else
                {
                    DialogResult = DialogUtil.SaveError();
                }
            }
            else
            {
                StockID = (int)stockTableAdapter.InsertQuery(tbxSuppliesName.Text, Convert.ToDouble(tbxUnit.Text), Convert.ToByte(cbxSuppliesType.SelectedValue), tbxDescription.Text);

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

                    DialogResult = DialogUtil.AddSuccess();
                }
                else
                {
                    DialogResult = DialogUtil.AddError();
                }
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!this.ValidateChildren())
            {
                return;
            }

            CompanyTableAdapter companyTableAdapter = new CompanyTableAdapter();

            CompanyID = (int)companyTableAdapter.InsertQuery(this.tbxName.Text.Trim());
            if (CompanyID > 0)
            {
                UserLogTableAdapter logTableAdapter = new UserLogTableAdapter();
                logTableAdapter.Insert(TypeUtil.Gene, CompanyID.ToString(), MainForm.User.UserID, TypeUtil.EVENT_CREATE, DateTime.Now);

                this.DialogResult = DialogUtil.AddSuccess();
            }
            else
            {
                this.DialogResult = DialogUtil.AddError();
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!this.ValidateChildren())
            {
                return;
            }


            if (GeneID != 0)
            {
                this.geneBindingSource.EndEdit();
                if (1 == this.geneTableAdapter.Update(this.farmManageDataSet.Gene))
                {
                    UserLogTableAdapter logTableAdapter = new UserLogTableAdapter();
                    logTableAdapter.Insert(TypeUtil.Gene, GeneID.ToString(), MainForm.User.UserID, TypeUtil.EVENT_MODIFY, DateTime.Now);

                    this.DialogResult = DialogUtil.SaveSuccess();
                }
                else
                {
                    this.DialogResult = DialogUtil.SaveError();
                }
            }
            else
            {
                GeneID = (int)this.geneTableAdapter.InsertQuery(this.tbxName.Text.Trim(), this.tbxNatural.Text.Trim());
                if (GeneID > 0)
                {
                    UserLogTableAdapter logTableAdapter = new UserLogTableAdapter();
                    logTableAdapter.Insert(TypeUtil.Gene, GeneID.ToString(), MainForm.User.UserID, TypeUtil.EVENT_CREATE, DateTime.Now);

                    this.DialogResult = DialogUtil.AddSuccess();
                }
                else
                {
                    this.DialogResult = DialogUtil.AddError();
                }
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            Int32 CorralID = ((cowBindingSource.Current as DataRowView).Row as FarmManageDataSet.CowRow).CorralID;

            this.Validate();
            this.cowBindingSource.EndEdit();
            if (1 == this.cowTableAdapter.Update(this.farmManageDataSet.Cow))
            {
                if (CorralID != Convert.ToInt32(cbxCorral.SelectedValue))
                {
                    corralTableAdapter.UpdateRemoveCurrent(1, CorralID);
                    corralTableAdapter.UpdateAddCurrent(1, Convert.ToInt32(cbxCorral.SelectedValue));
                }

                var log = new UserLogTableAdapter();
                log.Insert(TypeUtil.Cow, CowNO, MainForm.User.UserID, 1, DateTime.Now);

                DialogResult = DialogUtil.SaveSuccess();
            }
            else
            {
                DialogResult = DialogUtil.SaveError();
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (OrderInID > 0)
            {
                this.Validate();
                this.bornViewBindingSource.EndEdit();
                this.fKCowOrderIn1BindingSource.EndEdit();
                Int32 result = this.bornViewTableAdapter.Update(this.farmManageDataSet.BornView);

                if (result == 1)
                {
                    this.DialogResult = DialogUtil.SaveSuccess();
                }
                else
                {
                    this.DialogResult = DialogUtil.SaveError();
                }
            }
            else
            {
                OrderInID = (int)this.bornViewTableAdapter.InsertQuery(
                    (byte?)2,
                    Convert.ToInt32(this.cbxBy.SelectedValue),
                    this.dtpDate.Value,
                    this.tbxDescription.Text);

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


                    foreach (DataGridViewRow row in this.dgvCowAdd.Rows)
                    {
                        Int32  tCorralID = Convert.ToInt32(row.Cells["colCorral"].Value);
                        Int32  tGeneID   = Convert.ToInt32(row.Cells["colGene"].Value);
                        Byte   tSexlID   = Convert.ToByte(row.Cells["colSex"].Value);
                        String tDesc     = row.Cells["colDescription"].Value.ToString();
                        String CowNO     = String.Format("{0:000}-{1:000}", OrderInID, Convert.ToInt32(row.Cells["colID"].Value));


                        CowNO = (String)this.cowTableAdapter.InsertQuery(
                            CowNO,
                            tCorralID,
                            tGeneID,
                            tSexlID,
                            tDesc,
                            OrderInID,
                            0.0,
                            new Int32?(),
                            new Double?());

                        if (CowNO != null)
                        {
                            log.Insert(TypeUtil.Cow, CowNO, MainForm.User.UserID, TypeUtil.EVENT_BORN, DateTime.Now);
                            this.corralTableAdapter.UpdateAddCurrent(1, tCorralID);
                        }
                    }

                    this.DialogResult = DialogUtil.AddSuccess();
                }
                else
                {
                    this.DialogResult = DialogUtil.AddError();
                }
            }
        }
        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();
            }
        }
        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)
        {
            btnSave.Enabled    = false;
            pnlContent.Enabled = false;
            Cursor             = System.Windows.Forms.Cursors.WaitCursor;
            if (tbxUsername.Text.Length < 5)
            {
                btnSave.Enabled    = true;
                pnlContent.Enabled = true;
                Cursor             = System.Windows.Forms.Cursors.Default;
                MessageBox.Show("ชื่อผู้ใช้ 5 อักษรขึ้นไป กรุณาลองใหม่อีกครั้ง", "แจ้งเตือน", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                tbxUsername.Focus();
                return;
            }

            int result = (int)usersTableAdapter.CheckUsername(tbxUsername.Text.Trim());

            if (result == 0)
            {
                if (!tbxPassword.Text.Trim().Equals(tbxConfirmPassword.Text.Trim()))
                {
                    btnSave.Enabled    = true;
                    pnlContent.Enabled = true;
                    Cursor             = System.Windows.Forms.Cursors.Default;
                    MessageBox.Show("รหัสผ่านไม่ตรงกัน, กรุณาลองใหม่อีกครั้ง", "แจ้งเตือน", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                else
                {
                    if (tbxPassword.Text.Trim().Length < 6)
                    {
                        btnSave.Enabled    = true;
                        pnlContent.Enabled = true;
                        Cursor             = System.Windows.Forms.Cursors.Default;
                        MessageBox.Show("รหัสผ่านสั้นเกินไป 6 ตัวอักษรขึ้นไป, กรุณาลองใหม่อีกครั้ง", "แจ้งเตือน", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }

                int UserID = (int)usersTableAdapter.InsertQuery(tbxUsername.Text.Trim(),
                                                                tbxPassword.Text.Trim(),
                                                                Convert.ToByte(cbxPrename.SelectedValue),
                                                                tbxFirstname.Text.Trim(),
                                                                tbxLastname.Text.Trim(),
                                                                chkSuperUser.Checked,
                                                                chkEnable.Checked,
                                                                tbxEmail.Text.Trim(),
                                                                tbxTel.Text.Trim());
                if (UserID > 0)
                {
                    UserLogTableAdapter logTableAdapter = new UserLogTableAdapter();
                    logTableAdapter.Insert(TypeUtil.User, UserID.ToString(), MainForm.User.UserID, TypeUtil.EVENT_CREATE, DateTime.Now);

                    DialogUtil.SaveSuccess();
                    DialogResult = System.Windows.Forms.DialogResult.Yes;
                }
                else
                {
                    DialogUtil.SaveError();
                    DialogResult = System.Windows.Forms.DialogResult.No;
                }
            }
            else
            {
                btnSave.Enabled    = true;
                pnlContent.Enabled = true;
                Cursor             = System.Windows.Forms.Cursors.Default;
                MessageBox.Show("ไม่สามารถใช้ชื่อผู้ใช้นี้ กรุณาลองใหม่อีกครั้ง", "แจ้งเตือน", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                tbxUsername.Focus();
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.dgvAddList.Rows.Count == 0)
            {
                MessageBox.Show("ไม่มีรายการที่เพิ่มอย่างสมบูรณ์ กรุณาเพิ่มหรือป้อนข้อมูลให้ครบ");
                return;
            }
            for (var i = 0; i < dgvAddList.Rows.Count; i++)
            {
                if (this.dgvAddList.Rows[i].Cells[0].Value == null || Convert.ToDouble(dgvAddList.Rows[i].Cells[1].Value) == 0)
                {
                    MessageBox.Show("ข้อมูลไม่ครบ กรุณาลองใหม่อีกครั้ง", "ข้อผิดผลาก", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }



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

            foreach (DataGridViewRow vRow in this.dgvAddList.Rows)
            {
                var StockID = Convert.ToInt32(vRow.Cells[0].Value);
                var Unit    = Convert.ToDouble(vRow.Cells[1].Value);
                if (Stocks.ContainsKey(StockID))
                {
                    Stocks[StockID] += Unit;
                }
                else
                {
                    Stocks.Add(StockID, Unit);
                }
            }

            var Msg = new StringBuilder();

            foreach (KeyValuePair <Int32, Double> Stock in Stocks)
            {
                Double?CurrentUnit = this.stockTableAdapter.QueryCurrentUnit(Stock.Key);
                if (CurrentUnit.Value < 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;
            }

            RemoveStockID = (int)this.stockRemoveTableAdapter.InsertQuery(
                Convert.ToInt32(cbxBy.SelectedValue),
                this.dtpDate.Value,
                0,
                "0");//0 เบิกตามปกติ

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



                var StockRemoveID = stockRemoveTableAdapter.QueryMaxID();
                foreach (DataGridViewRow row in this.dgvAddList.Rows)
                {
                    var result = this.stockRemoveListTableAdapter.Insert(
                        StockRemoveID.Value,
                        Convert.ToInt32(row.Cells[0].Value),
                        Convert.ToDouble(row.Cells[1].Value),
                        row.Cells[2].Value == null ? string.Empty : row.Cells[2].Value.ToString());
                    if (result == 1)
                    {
                        this.stockTableAdapter.UpdateRemoveUnit(Convert.ToDouble(row.Cells[1].Value), Convert.ToInt32(row.Cells[0].Value));
                    }
                }

                this.DialogResult = DialogUtil.SaveSuccess();
            }
            else
            {
                this.DialogResult = DialogUtil.SaveError();
            }
        }
Ejemplo n.º 22
0
        private void SaveOrder()
        {
            int result = 0;

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

                if (CustomerPID != null)
                {
                    result = 1;
                    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 OrderOutID = (int)orderOutTableAdapter.InsertQuery(
                this.OrderType,
                this.CustomerPID,
                this.OrderBy,
                this.dtpOrderDate.Value,
                this.tbxNote.Text,
                this.TransportType,
                Convert.ToDouble(this.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?() : this.dtpPaymentDate.Value);


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

                var cowTableAdapter = new CowTableAdapter();
                var OrderType       = Convert.ToByte(cbxOrderType.SelectedValue);
                foreach (DataGridViewRow row in dgvCow.Rows)
                {
                    Double Amount = Convert.ToDouble(row.Cells["colCowOrderOutAmount"].Value);
                    result = cowTableAdapter.UpdateCowOrderOut(
                        OrderOutID,
                        Amount,
                        row.Cells["colCowNO"].Value.ToString());

                    if (result == 1)
                    {
                        log.Insert(TypeUtil.Cow, row.Cells["colCowNO"].Value.ToString(), MainForm.User.UserID, TypeUtil.EVENT_SELL, DateTime.Now);


                        corralTableAdapter.UpdateRemoveCurrent(1, Convert.ToInt32(row.Cells["colCowCorral"].Value));

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


            foreach (DataGridViewRow row in this.dgvAdd.Rows)
            {
                if (row.Cells[0].Value == null || row.Cells[1].Value == null)
                {
                    MessageBox.Show("ป้อนข้อมูลไม่ครบ", "ข้อผิดผลาด", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            if (RecipesID == 0)
            {
                RecipesID = (int)this.recipesTableAdapter.InsertQuery(tbxName.Text, tbxDescription.Text);
                if (RecipesID > 0)
                {
                    var log = new UserLogTableAdapter();
                    log.Insert(TypeUtil.Recipes, RecipesID.ToString(), MainForm.User.UserID, TypeUtil.EVENT_CREATE, DateTime.Now);


                    RecipesID = recipesTableAdapter.GetMaxID().Value;
                    foreach (DataGridViewRow row in this.dgvAdd.Rows)
                    {
                        Int32  StockID = Convert.ToInt32(row.Cells[0].Value);
                        Double Unit    = Convert.ToDouble(row.Cells[1].Value);
                        this.recipesListTableAdapter.Insert(RecipesID, StockID, Unit);
                    }

                    this.DialogResult = DialogUtil.AddSuccess();
                }
                else
                {
                    this.DialogResult = DialogUtil.AddError();
                }
            }
            else
            {
                this.Validate();
                this.recipesBindingSource.EndEdit();
                this.fKrecipeslistrecipesBindingSource.EndEdit();
                this.recipesTableAdapter.Update(farmManageDataSet.Recipes);
                this.recipesListTableAdapter.Update(farmManageDataSet.RecipesList);

                foreach (DataGridViewRow row in this.dgvAdd.Rows)
                {
                    Int32  StockID = Convert.ToInt32(row.Cells[0].Value);
                    Double Unit    = Convert.ToDouble(row.Cells[1].Value);
                    this.recipesListTableAdapter.Insert(RecipesID, StockID, Unit);
                }

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


                //if (result == 1)
                //{

                //    this.recipesListTableAdapter.Update(farmManageDataSet.RecipesList);


                //    foreach (DataGridViewRow row in this.dgvAdd.Rows)
                //    {
                //        Int32 StockID = Convert.ToInt32(row.Cells[0].Value);
                //        Double Unit = Convert.ToDouble(row.Cells[1].Value);
                //        this.recipesListTableAdapter.Insert(RecipesID, StockID, Unit);
                //    }

                DialogUtil.SaveSuccess();
                this.DialogResult = DialogResult.Yes;
                //}
                //else
                //{
                //    DialogUtil.SaveError();
                //    this.DialogResult = DialogResult.No;
                //}
            }
        }
        private void btnCalculate_Click(object sender, EventArgs e)
        {
            if (Convert.ToDouble(tbxPercen.Text) == 0D)
            {
                MessageBox.Show("จำเป็นต้องเลือกอัตราคำนวน มากกว่า 0", "ข้อผิดผลาด", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }



            int ByID = Convert.ToInt32(cbxBy.SelectedValue);

            RecipesID = Convert.ToInt32(cbxRecipse.SelectedValue);
            if (RecipesID == 0)
            {
                MessageBox.Show("คุณยังไม่ได้เลือกสูตรอาหาร กรุณาเลือกสูตรอาหารก่อน", "ข้อผิดผลาด", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            var RecipesListRows = new RecipesListTableAdapter().GetDataByRecipesID(RecipesID);


            var msg = new StringBuilder();

            foreach (FarmManageDataSet.RecipesListRow row in RecipesListRows.Rows)
            {
                var unit      = (row.Unit * Double.Parse(tbxPercen.Text)) / 100;
                var StockRows = stockTableAdapter.GetDataByPK(row.StockID);
                if (StockRows.Count == 0)
                {
                    MessageBox.Show("ไม่พบพัสดุที่ต้องการจะหัก หรือพัสดุนั้นถูกลบออกจากระบบแล้วกรุนาตรวจสอบใหม่อีกครั้ง <br/>ID:" + row.StockID, "ข้อผิดผลาด", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (unit > StockRows[0].Unit)
                {
                    msg.Append(String.Format("พัสดุชื่อ {0} มีจำนวนไม่พอที่หัก จำนวนปัจจุบัน:{1} จำนวนที่ต้องการหัก:{2}<br>", StockRows[0].Name, StockRows[0].Unit, unit));
                }
            }
            if (msg.Length != 0)
            {
                MessageBox.Show(msg.ToString(), "ข้อผิดผลาด", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            var RecipesHistoryID = (int)this.recipesHistoryTableAdapter.InsertQuery(
                Convert.ToInt32(cbxBy.SelectedValue),
                RecipesID,
                dtpDate.Value,
                Convert.ToInt32(tbxPercen.Text));

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

                foreach (FarmManageDataSet.RecipesListRow row in RecipesListRows.Rows)
                {
                    var Unit = (row.Unit * Double.Parse(tbxPercen.Text)) / 100;
                    int RecipesListHistoryID = (int)recipesListHistoryTableAdapter.InsertQuery(RecipesHistoryID, row.StockID, Unit);
                    if (RecipesListHistoryID > 0)
                    {
                        log.Insert(TypeUtil.RecipesListHistory, RecipesListHistoryID.ToString(), MainForm.User.UserID, TypeUtil.EVENT_CREATE, DateTime.Now);

                        int StockRemoveID = (int)this.stockRemoveTableAdapter.InsertQuery(
                            ByID,
                            dtpDate.Value,
                            3,
                            RecipesHistoryID.ToString());

                        if (StockRemoveID > 0)
                        {
                            log.Insert(TypeUtil.StockRemove, StockRemoveID.ToString(), MainForm.User.UserID, TypeUtil.EVENT_CREATE, DateTime.Now);

                            var StockRemoveListID = (int)new StockRemoveListTableAdapter().InsertQuery(StockRemoveID, row.StockID, Unit, null);
                            if (StockRemoveListID > 0)
                            {
                                this.stockTableAdapter.UpdateRemoveUnit(Unit, row.StockID);
                                log.Insert(TypeUtil.StockRemoveList, StockRemoveListID.ToString(), MainForm.User.UserID, TypeUtil.EVENT_CREATE, DateTime.Now);
                            }
                        }
                    }
                }
                DialogResult = DialogUtil.AddSuccess();
            }
            else
            {
                DialogResult = DialogUtil.AddError();
            }
        }