private void search()
        {
            this.txtCheckdate.Clear();
            this.txtCav.Clear();
            this.txtWeight.Clear();
            this.txtEquipment.Clear();
            this.txtShot.Clear();
            this.txtLength.Clear();
            this.txtWidth.Clear();
            this.txtHeight.Clear();
            this.txtInstockRemarks.Clear();

            this.dgvResult.Rows.Clear();
            this.dgvPreview.Rows.Clear();

            string po = txtPo.Text;

            if (!DataChecking.isValidPo(po))
            {
                if (po.StartsWith("12") && po.EndsWith("10"))
                {
                    string tmpo = po.Substring(0, po.Length - 2);
                    tmpo = tmpo + "20";

                    if (DataChecking.isValidPo(tmpo))
                    {
                        switch (MessageBox.Show("Temporary receive PO: " + po + " ?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                        {
                        case DialogResult.Yes:

                            string poText = string.Format("insert into tb_temppo (tp_po) values ('{0}')", po);
                            DataService.GetInstance().ExecuteNonQuery(poText);

                            MessageBox.Show("Record has been saved.");
                            break;

                        case DialogResult.No:
                            break;
                        }

                        return;
                    }
                    else
                    {
                        MessageBox.Show("Invalid PO No.");
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("Invalid PO No.");
                    return;
                }
            }

            if (DataChecking.IsPoRmb(po))
            {
                foreach (CheckBox checkbox in tbCheckbox.Controls)
                {
                    checkbox.Enabled = false;
                    checkbox.Checked = false;
                }

                this.getDataByPo(po);

                tmpPo = po;

                if (checkPoIsMpa(po))
                {
                    pictureBox1.BackgroundImage = Properties.Resources.tick;

                    ckbNormal.Enabled   = false;
                    ckbTransfer.Enabled = false;

                    string is50 = DataChecking.getInstock50ByPo(po);

                    if (is50 == "Yes")
                    {
                        ckbFirst50.Enabled  = true;
                        ckbSecond50.Enabled = false;
                        ckbPo1.Enabled      = true;
                        ckbPo2.Enabled      = true;
                    }
                    else
                    {
                        ckbFirst50.Enabled  = false;
                        ckbSecond50.Enabled = true;
                        ckbPo1.Enabled      = false;
                        ckbPo2.Enabled      = false;
                    }
                }
                else
                {
                    pictureBox1.BackgroundImage = Properties.Resources.cross;

                    string type = DataChecking.getTypeByPo(po);

                    if (type == "New" || type == "Modify")
                    {
                        ckbNormal.Enabled   = true;
                        ckbTransfer.Enabled = false;
                    }
                    else
                    {
                        ckbNormal.Enabled   = false;
                        ckbTransfer.Enabled = true;
                    }

                    ckbFirst50.Enabled  = false;
                    ckbSecond50.Enabled = false;
                    ckbPo1.Enabled      = false;
                    ckbPo2.Enabled      = false;
                }

                btnPreview.Enabled = true;
            }
            else
            {
                string currentInStockdate = DataChecking.getInStockDateByPo(po);

                if (currentInStockdate == "Received")
                {
                    switch (MessageBox.Show("PO: " + po + " data already received. Do you want to modify it?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                    {
                    case DialogResult.Yes:
                        break;

                    case DialogResult.No:
                        foreach (CheckBox checkbox in tbCheckbox.Controls)
                        {
                            checkbox.Enabled = true;
                            checkbox.Checked = false;
                        }
                        pictureBox1.BackgroundImage = null;
                        return;
                    }
                }

                this.getDataByPo(po);

                tmpPo = po;

                if (checkPoIsMpa(po))
                {
                    pictureBox1.BackgroundImage = Properties.Resources.tick;

                    ckbNormal.Enabled   = false;
                    ckbTransfer.Enabled = false;

                    string is50 = DataChecking.getInstock50ByPo(po);

                    if (is50 == "Yes")
                    {
                        ckbFirst50.Enabled  = true;
                        ckbSecond50.Enabled = false;
                        ckbPo1.Enabled      = true;
                        ckbPo2.Enabled      = true;
                    }
                    else
                    {
                        ckbFirst50.Enabled  = false;
                        ckbSecond50.Enabled = true;
                        ckbPo1.Enabled      = false;
                        ckbPo2.Enabled      = false;
                    }
                }
                else
                {
                    pictureBox1.BackgroundImage = Properties.Resources.cross;

                    string type = DataChecking.getTypeByPo(po);

                    if (type == "New" || type == "Modify")
                    {
                        ckbNormal.Enabled   = true;
                        ckbTransfer.Enabled = false;
                    }
                    else
                    {
                        ckbNormal.Enabled   = false;
                        ckbTransfer.Enabled = true;
                    }

                    ckbFirst50.Enabled  = false;
                    ckbSecond50.Enabled = false;
                    ckbPo1.Enabled      = false;
                    ckbPo2.Enabled      = false;
                }

                btnPreview.Enabled = true;
            }
        }
        private void btnReceive_Click(object sender, EventArgs e)
        {
            List <int> count = new List <int>();

            foreach (Control control in tbCheckbox.Controls)
            {
                CheckBox ckb = (CheckBox)control;
                if (ckb.Checked == true)
                {
                    count.Add(1);
                }
            }

            bool isRmb = DataChecking.IsPoRmb(txtPo.Text.Trim());

            if (count.Count == 0 && !isRmb)
            {
                MessageBox.Show("Nothing selected. Please click Modify Spec if you want to update Mould Spec only");
                return;
            }

            string checkdate = "", cav = "", weight = "", accessory = "", camera = "", length = "", width = "", height = "", instockremarks = "";

            checkdate = txtCheckdate.Text;

            if (!string.IsNullOrEmpty(txtCav.Text))
            {
                cav = txtCav.Text;
            }

            if (!string.IsNullOrEmpty(txtWeight.Text))
            {
                weight = txtWeight.Text;
            }

            if (!string.IsNullOrEmpty(txtEquipment.Text))
            {
                accessory = txtEquipment.Text;
            }

            if (!string.IsNullOrEmpty(txtShot.Text))
            {
                camera = txtShot.Text;
            }

            if (!string.IsNullOrEmpty(txtLength.Text))
            {
                length = txtLength.Text;
            }

            if (!string.IsNullOrEmpty(txtWidth.Text))
            {
                width = txtWidth.Text;
            }

            if (!string.IsNullOrEmpty(txtHeight.Text))
            {
                height = txtHeight.Text;
            }

            if (!string.IsNullOrEmpty(txtInstockRemarks.Text))
            {
                instockremarks = txtInstockRemarks.Text;
            }

            string calpo = txtPo.Text;

            int thismonth     = DateTime.Today.Month;
            int selectedmonth = dpCheckDate.Value.Month;

            /*checkdate = dpCheckDate.Value.ToString("yyyy/MM/dd");
             *
             * int result = thismonth - selectedmonth;
             *
             * if (result > 3 || result < -3)
             * {
             *  switch (MessageBox.Show("Checkdate selection out of range (+ / - 3 months). Keep modifying?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
             *  {
             *      case DialogResult.Yes:
             *
             *          break;
             *
             *      case DialogResult.No:
             *          return;
             *  }
             * }*/

            if (isRmb)
            {
                string query = string.Format("update tb_betamould set tm_instockdate50 = '{0}', tm_instockdate = '{1}', tm_checkdate = '{2}', tm_cav = '{3}'" +
                                             ", tm_weight = '{4}', tm_accessory = '{5}', tm_camera = '{6}', tm_vertical = '{7}', tm_horizontal = '{8}', tm_height = '{9}', tm_moulditemviewer = 'Yes', tm_st_code = 'PR', tm_instockremarks = '{10}'" +
                                             " where tm_po = '{11}'", instockdate50, instockdate, checkdate, cav, weight, accessory, camera, length, width, height, instockremarks, tmpPo);

                //Debug.WriteLine(query);
                DataService.GetInstance().ExecuteNonQuery(query);
            }
            else
            {
                if (inputRequired)
                {
                    if (string.IsNullOrEmpty(checkdate))
                    {
                        MessageBox.Show("檢查合格日 cannot be null");
                        return;
                    }

                    string query = string.Format("update tb_betamould set tm_instockdate50 = '{0}', tm_instockdate = '{1}', tm_checkdate = '{2}', tm_cav = '{3}'" +
                                                 ", tm_weight = '{4}', tm_accessory = '{5}', tm_camera = '{6}', tm_vertical = '{7}', tm_horizontal = '{8}', tm_height = '{9}', tm_moulditemviewer = 'Yes', tm_st_code = 'PR', tm_instockremarks = '{10}'" +
                                                 " where tm_po = '{11}'", instockdate50, instockdate, checkdate, cav, weight, accessory, camera, length, width, height, instockremarks, tmpPo);

                    DataService.GetInstance().ExecuteNonQuery(query);

                    //MessageBox.Show("Record has been saved");
                }
                else
                {
                    string type = DataChecking.getTypeByPo(calpo);

                    if (type == "New" || type == "Modify")
                    {
                        string query = string.Format("update tb_betamould set tm_instockdate50 = '{0}', tm_instockdate = '{1}', tm_checkdate = '{2}', tm_cav = '{3}'" +
                                                     ", tm_weight = '{4}', tm_accessory = '{5}', tm_camera = '{6}', tm_vertical = '{7}', tm_horizontal = '{8}', tm_height = '{9}', tm_moulditemviewer = 'Yes', tm_st_code = 'PR', tm_instockremarks = '{10}'" +
                                                     " where tm_po = '{11}'", instockdate50, instockdate, checkdate, cav, weight, accessory, camera, length, width, height, instockremarks, tmpPo);

                        DataService.GetInstance().ExecuteNonQuery(query);
                    }
                    else
                    {
                        List <string> polist = new List <string>();

                        string text = string.Format("select tm_po from tb_betamould where tm_po like '{0}%'", calpo);

                        GlobalService.reader = DataService.GetInstance().ExecuteReader(text);

                        while (GlobalService.reader.Read())
                        {
                            string po = GlobalService.reader.GetString(0);
                            polist.Add(po);
                        }
                        GlobalService.reader.Close();
                        GlobalService.reader.Dispose();

                        for (int i = 0; i < polist.Count; i++)
                        {
                            string targetpo = polist[i];

                            string query = string.Format("update tb_betamould set tm_instockdate50 = '{0}', tm_instockdate = '{1}', tm_checkdate = '{2}', tm_cav = '{3}'" +
                                                         ", tm_weight = '{4}', tm_accessory = '{5}', tm_camera = '{6}', tm_vertical = '{7}', tm_horizontal = '{8}', tm_height = '{9}', tm_instockremarks = '{10}', tm_moulditemviewer = 'Yes', tm_st_code = 'S'" +
                                                         " where tm_po = '{11}'", instockdate50, instockdate, checkdate, cav, weight, accessory, camera, length, width, height, instockremarks, targetpo);

                            DataService.GetInstance().ExecuteNonQuery(query);
                        }
                    }
                }
            }
            MessageBox.Show("Record has been saved");
            //dpCheckDate.Value = DateTime.Today;
            this.cancel();
        }