private void toolStripButton4_Click_1(object sender, EventArgs e)
 {
     if (strProductionReturnId == "") { return; }
     ProductionNew.ShowReport rpt = new ProductionNew.ShowReport(Data(strProductionReturnId), 1);
     rpt.ShowDialog();
 }
        private void button1_Click(object sender, EventArgs e)
        {
            int cutType = getCutType();
            double TotalCost = 0;

            if (textBox11.Text != "") { TotalCost = (textBox11.Text.ToDouble()); }
            else { TotalCost = 0; }

            if (dataGridView2.Rows.Count < 1 && dataGridView4.Rows.Count < 1 && dgvMetLost.Rows.Count < 1)
            {
                MetroFramework.MetroMessageBox.Show(this, "กรุณาเลือกรายการรับคืน", "ผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (textBox6.Text == "") { MessageBox.Show("กรุณาป้อนขนส่ง"); textBox6.Focus(); return; }
            List<string> sqlAdd = new List<string>();

            #region Edit
            if (Pid != "")
            {
                if (Class.Function.GetDate(dateTimePicker1.Value).Substring(5, 2) != textBox1.Text.Substring(3, 2))
                {
                    MetroFramework.MetroMessageBox.Show(this, "วันที่และรหัสใบรับคืนไม่ตรงกัน", "ผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                String sqlz = "";
                string sqledit = "select r.NewProductionId,f.FmName,r.Quantity,f.FmId,r.cutType " +
                        "FROM ProductionDrawReturn r " +
                        "inner JOIN ProductionDraw_Head h ON h.NewProductionId  = r.NewProductionId " +
                        "inner JOIN ProductionFormular f ON f.FmId = h.FmId " +
                        "where r.ProductionReturnId = '" + textBox1.Text + "' ";
                sqledit += " UNION ALL ";
                sqledit += "select r.NewProductionId,f.FmName,r.Quantity,f.FmId,r.cutType " +
                       "FROM ProductionDrawReturn r " +
                       "inner JOIN ProductionOutstandingDetail h ON h.OldProductionId  = r.NewProductionId " +
                       "inner JOIN ProductionFormular f ON f.FmId = h.FmId " +
                       "where r.ProductionReturnId = '" + textBox1.Text + "' ";

                DataTable tableReturn = Class.DBConnString.clsDB.QueryDataTable(sqledit);
                foreach (DataRow row in tableReturn.Rows)
                {
                    if (row["cutType"].ToInt() == 1)
                    {
                        sqlz = "update productiondraw_head set ReceiveQuantity = (ReceiveQuantity - " + row["Quantity"].IsNullAsEmpty() + ")," +
                                        " ProductionStatusRecId = case when (ReceiveQuantity - " + row["Quantity"].IsNullAsEmpty() + ") = 0 then 0 " +
                                        " when (ReceiveQuantity - " + row["Quantity"].IsNullAsEmpty() + ") > 0 and (ReceiveQuantity + " + row["Quantity"].IsNullAsEmpty() + ") < Amount then 1 " +
                                        " when (ReceiveQuantity - " + row["Quantity"].IsNullAsEmpty() + ") > 0 and (ReceiveQuantity + " + row["Quantity"].IsNullAsEmpty() + ") >= Amount then 2 end " +
                                        " where NewProductionId = '" + row["NewProductionId"].IsNullAsEmpty() + "'";
                        sqlAdd.Add(sqlz);//update ข้อมูลเบิกผลิต
                    }
                    else
                    {
                        sqlz = "update ProductionOutstandingDetail set OutstandingReceive=OutstandingReceive-" + row["Quantity"].IsNullAsEmpty() + " " +
                        "where OldProductionId = '" + row["NewProductionId"].IsNullAsEmpty() + "' " +
                        "and FmId = '" + row["FmId"].IsNullAsEmpty() + "' ";
                        sqlAdd.Add(sqlz);//update ข้อมูลเบิกผลิต
                    }
                }

                String sqlx = "DELETE FROM productionbasereturndetailfail WHERE ProductionReturnId = '" + Pid + "' ";
                sqlAdd.Add(sqlx);
                sqlx = "DELETE FROM productionbasereturndetailLost WHERE ProductionReturnId = '" + Pid + "' ";
                sqlAdd.Add(sqlx);
                sqlx = "DELETE FROM productionbasereturnfail WHERE ProductionReturnId = '" + Pid + "' ";
                sqlAdd.Add(sqlx);
                sqlx = "DELETE FROM productionbasereturnLost WHERE ProductionReturnId = '" + Pid + "' ";
                sqlAdd.Add(sqlx);
                sqlx = "delete from ProductionReturnDetailLost where ProductionReturnId = '" + Pid + "' ";
                sqlAdd.Add(sqlx);
                sqlx = "delete from ProductionReturnWeightfail where ProductionReturnId = '" + Pid + "' ";
                sqlAdd.Add(sqlx);
                sqlx = "delete from ProductionReturnWeightLost where ProductionReturnId = '" + Pid + "' ";
                sqlAdd.Add(sqlx);
                sqlx = "delete from ProductionReturnDetailfail where ProductionReturnId = '" + Pid + "' ";
                sqlAdd.Add(sqlx);
                sqlx = "delete from ProductionReturnWeight where ProductionReturnId = '" + Pid + "' ";
                sqlAdd.Add(sqlx);
                sqlx = "delete from ProductionReturnDetail where ProductionReturnId = '" + Pid + "' ";
                sqlAdd.Add(sqlx);
                sqlx = "DELETE FROM productionbasereturndetail WHERE ProductionReturnId = '" + Pid + "' ";
                sqlAdd.Add(sqlx);
                sqlx = "delete from productionbasereturn where ProductionReturnId = '" + Pid + "' ";
                sqlAdd.Add(sqlx);
                sqlx = "delete from ProductionReturn where ProductionReturnId = '" + Pid + "' ";
                sqlAdd.Add(sqlx);
                sqlx = "delete from ProductionDrawReturn where ProductionReturnId = '" + Pid + "' ";
                sqlAdd.Add(sqlx);
            }

            #endregion

            String sql = "insert into ProductionReturn values('" + textBox1.Text + "','" + Class.Function.GetDate(dateTimePicker1.Value) + "', " +
            "GETDATE()," + Class.Function.GetTextId(textBox3.Text) + ",'" + textBox4.Text + "'," + Class.Function.GetTextId(textBox6.Text) + "," +
            "'" + textBox5.Text + "','" + FmId + "'," + TotalCost + ",'" + Class.clsCash.sUserIdLogin + "',1,0," +
            "'',''," +
            "'" + textBox2.Text + "'," + cutType + " " + ",1" +
            ")";//บันทึกข้อมูลรับคืนวัตถุดิบ
            sqlAdd.Add(sql);

            foreach (DataGridViewRow row in dgvDraw.Rows)
            {
                if (row.Cells["cutType"].Value.ToInt() == 1)
                {
                    sql = "update productiondraw_head set ReceiveQuantity = (ReceiveQuantity + " + row.Cells["MQty"].Value.IsNullAsEmpty() + ")," +
                   " ProductionStatusRecId = case when (ReceiveQuantity + " + row.Cells["MQty"].Value.IsNullAsEmpty() + ") = 0 then 0 " +
                   " when (ReceiveQuantity + " + row.Cells["MQty"].Value.IsNullAsEmpty() + ") > 0 and (ReceiveQuantity + " + row.Cells["MQty"].Value.IsNullAsEmpty() + ") < Amount then 1 " +
                   " when (ReceiveQuantity + " + row.Cells["MQty"].Value.IsNullAsEmpty() + ") > 0 and (ReceiveQuantity + " + row.Cells["MQty"].Value.IsNullAsEmpty() + ") >= Amount then 2 end " +
                   " where NewProductionId = '" + row.Cells["Mid"].Value.IsNullAsEmpty() + "'";
                    sqlAdd.Add(sql);//update ข้อมูลเบิกผลิต
                }
                else
                {
                    sql = "update ProductionOutstandingDetail set OutstandingReceive=OutstandingReceive+" + row.Cells["MQty"].Value.IsNullAsEmpty() + " " +
                        "where OldProductionId = '" + row.Cells["Mid"].Value.IsNullAsEmpty() + "' " +
                        "and FmId = '" + row.Cells["FmId"].Value.IsNullAsEmpty() + "' ";
                    sqlAdd.Add(sql);//update ข้อมูลเบิกผลิต
                }

                String sqld = "insert into ProductionDrawReturn values('" + textBox1.Text + "','" + row.Cells["Mid"].Value.IsNullAsEmpty() + "'," +
                    "" + row.Cells["MQty"].Value.IsNullAsEmpty() + ",'" + row.Cells["OutstandingId"].Value.IsNullAsEmpty() + "' " + ",'" +row.Cells["cutType"].Value.IsNullAsEmpty() + "'" +
                    ")";//บันทึกข้อมูลรายการวัตถุดิบของใบรับคืนวัตถุดิบ
                sqlAdd.Add(sqld);
            }

            foreach (DataGridViewRow row in dataGridView2.Rows)
            {
                String sqld = "insert into ProductionReturnDetail values('" + textBox1.Text + "','" + row.Cells["ProductId"].Value.IsNullAsEmpty() + "'," +
                    "" + row.Cells["QuantityPack"].Value.IsNullAsEmpty() + "," + row.Cells["Quantity"].Value.IsNullAsEmpty() + "," +
                    "" + row.Cells["Weight"].Value.IsNullAsEmpty() + "," + row.Cells["Mid"].Value.IsNullAsEmpty() + "," +
                    "" + row.Cells["CostPrice"].Value.IsNullAsEmpty() + "," + row.Cells["Total"].Value.IsNullAsEmpty() + "," +
                    "" + row.Cells["Type"].Value.IsNullAsEmpty() + ",'" + row.Cells["Note"].Value.IsNullAsEmpty() + "'," +
                    "'" + row.Cells["NewProductionId"].Value.IsNullAsEmpty() + "')";//บันทึกข้อมูลรายการวัตถุดิบของใบรับคืนวัตถุดิบ
                sqlAdd.Add(sqld);

                if (row.Cells["Type"].Value.IsNullAsEmpty() != "3")
                {
                    String[] weight = row.Cells["Detail"].Value.IsNullAsEmpty().Split('#');
                    for (int i = 0; i < weight.Length - 1; i++)//1 2 3,4 5 6,7 8 9
                    {
                        if (i % ((row.Cells["Type"].Value.IsNullAsEmpty() == "1") ? 3 : 2) == 0)
                        {
                            String Pack = weight[i];
                            String Quantity = weight[i + 1];
                            String Weight = "0";
                            if (((row.Cells["Type"].Value.IsNullAsEmpty() == "1") ? 3 : 2) == 3)
                            {
                                Weight = weight[i + 2];
                            }
                            String sqlw = "insert into ProductionReturnWeight " +
                              "VALUES ('" + textBox1.Text + "','" + row.Cells["ProductId"].Value.IsNullAsEmpty() + "'," + Weight + "," + Quantity + "," +
                              "" + row.Cells["Mid"].Value.IsNullAsEmpty() + "," + Pack + ")";//บันทึกข้อมูลน้ำหนักสินค้าของใบรับคืนวัตถุดิบ
                            sqlAdd.Add(sqlw);
                        }
                    }
                }
            }

            foreach (DataGridViewRow row in dataGridView4.Rows)
            {
                String sqld = "insert into ProductionReturnDetailfail values('" + textBox1.Text + "','" + row.Cells["ProductId"].Value.IsNullAsEmpty() + "'," +
                    "" + row.Cells["QuantityPack"].Value.IsNullAsEmpty() + "," + row.Cells["Quantity"].Value.IsNullAsEmpty() + "," +
                    "" + row.Cells["Weight"].Value.IsNullAsEmpty() + "," + row.Cells["Mid"].Value.IsNullAsEmpty() + "," +
                    "" + row.Cells["CostPrice"].Value.IsNullAsEmpty() + "," + row.Cells["Total"].Value.IsNullAsEmpty() + "," +
                    "" + row.Cells["Type"].Value.IsNullAsEmpty() + ",'" + row.Cells["Note"].Value.IsNullAsEmpty() + "'," +
                    "'" + row.Cells["NetFId"].Value.IsNullAsEmpty() + "'," +
                    "'" + row.Cells["NewProductionId"].Value.IsNullAsEmpty() + "')";//บันทึกข้อมูลรายการวัตถุดิบที่เสียของใบรับคืนวัตถุดิบ
                sqlAdd.Add(sqld);

                if (row.Cells["Type"].Value.IsNullAsEmpty() != "3")
                {
                    String[] weight = row.Cells["Detail"].Value.IsNullAsEmpty().Split('#');
                    for (int i = 0; i < weight.Length - 1; i++)//1 2 3,4 5 6,7 8 9
                    {
                        if (i % ((row.Cells["Type"].Value.IsNullAsEmpty() == "1") ? 3 : 2) == 0)
                        {
                            String Pack = weight[i];
                            String Quantity = weight[i + 1];
                            String Weight = "0";
                            if (((row.Cells["Type"].Value.IsNullAsEmpty() == "1") ? 3 : 2) == 3)
                            {
                                Weight = weight[i + 2];
                            }
                            String sqlw = "insert into ProductionReturnWeightfail " +
                              "VALUES ('" + textBox1.Text + "','" + row.Cells["ProductId"].Value.IsNullAsEmpty() + "'," + Weight + "," + Quantity + "," +
                              "" + row.Cells["Mid"].Value.IsNullAsEmpty() + "," + Pack + ")";
                            sqlAdd.Add(sqlw);//บันทึกข้อมูลน้ำหนักของวัตถุดิบที่เสียของใบรับคืนวัตถุดิบ
                        }
                    }
                }
            }

            foreach (DataGridViewRow row in dgvMetLost.Rows)
            {
                String sqld = "insert into ProductionReturnDetailLost values('" + textBox1.Text + "','" + row.Cells["ProductId"].Value.IsNullAsEmpty() + "'," +
                    "" + row.Cells["QuantityPack"].Value.IsNullAsEmpty() + "," + row.Cells["Quantity"].Value.IsNullAsEmpty() + "," +
                    "" + row.Cells["Weight"].Value.IsNullAsEmpty() + "," + row.Cells["Mid"].Value.IsNullAsEmpty() + "," +
                    "" + row.Cells["CostPrice"].Value.IsNullAsEmpty() + "," + row.Cells["Total"].Value.IsNullAsEmpty() + "," +
                    "" + row.Cells["Type"].Value.IsNullAsEmpty() + ",'" + row.Cells["Note"].Value.IsNullAsEmpty() + "'," +
                    "'" + row.Cells["NetFId"].Value.IsNullAsEmpty() + "'," +
                    "'" + row.Cells["NewProductionId"].Value.IsNullAsEmpty() + "')";//บันทึกข้อมูลรายการวัตถุดิบที่เสียของใบรับคืนวัตถุดิบ
                sqlAdd.Add(sqld);

                if (row.Cells["Type"].Value.IsNullAsEmpty() != "3")
                {
                    String[] weight = row.Cells["Detail"].Value.IsNullAsEmpty().Split('#');
                    for (int i = 0; i < weight.Length - 1; i++)//1 2 3,4 5 6,7 8 9
                    {
                        if (i % ((row.Cells["Type"].Value.IsNullAsEmpty() == "1") ? 3 : 2) == 0)
                        {
                            String Pack = weight[i];
                            String Quantity = weight[i + 1];
                            String Weight = "0";
                            if (((row.Cells["Type"].Value.IsNullAsEmpty() == "1") ? 3 : 2) == 3)
                            {
                                Weight = weight[i + 2];
                            }
                            String sqlw = "insert into ProductionReturnWeightLost " +
                              "VALUES ('" + textBox1.Text + "','" + row.Cells["ProductId"].Value.IsNullAsEmpty() + "'," + Weight + "," + Quantity + "," +
                              "" + row.Cells["Mid"].Value.IsNullAsEmpty() + "," + Pack + ")";
                            sqlAdd.Add(sqlw);//บันทึกข้อมูลน้ำหนักของวัตถุดิบที่เสียของใบรับคืนวัตถุดิบ
                        }
                    }
                }
            }

            DataTable table = ConvertListToDataTable(listFist);
            Double totalsum = 0;
            Double msum = 0;
            Double totalsumf = 0;
            Double msumf = 0;

            foreach (DataRow h in table.Rows)
            {
                Double sum = 0;
                Double sumf = 0;
                foreach (DataGridViewRow d in dataGridView2.Rows)
                {
                    if (h["mid"].IsNullAsEmpty() == d.Cells["Mid"].Value.IsNullAsEmpty())
                    {
                        sum += d.Cells["Quantity"].Value.ToDouble();
                    }
                    String sqlz = "insert into productionbasereturndetail values('" + textBox1.Text + "'," + h["mid"].IsNullAsEmpty() + "," +
                    "" + d.Cells["Quantity"].Value.IsNullAsEmpty() + ",'" + LotId + "','" + h["id"].IsNullAsEmpty() + "'" +
                    ")";//บันทึกข้อมูลรายการวัตถุดิบ
                    sqlAdd.Add(sqlz);
                }
                ////////////////////////////
                foreach (DataGridViewRow d in dataGridView4.Rows)
                {
                    if (h["mid"].IsNullAsEmpty() == d.Cells["Mid"].Value.IsNullAsEmpty())
                    {
                        sumf += d.Cells["Quantity"].Value.ToDouble();
                    }
                    String sqlz = "insert into productionbasereturndetailfail values('" + textBox1.Text + "'," + h["mid"].IsNullAsEmpty() + "," +
                    "" + d.Cells["Quantity"].Value.IsNullAsEmpty() + ",'" + LotId + "','" + h["id"].IsNullAsEmpty() + "'" +
                    ")";//บันทึกข้อมูลรายการวัตถุดิบที่เสีย
                    sqlAdd.Add(sqlz);
                }
                ////////////////////////////
                foreach (DataGridViewRow d in dgvMetLost.Rows)
                {
                    if (h["mid"].IsNullAsEmpty() == d.Cells["Mid"].Value.IsNullAsEmpty())
                    {
                        sumf += d.Cells["Quantity"].Value.ToDouble();
                    }
                    String sqlz = "insert into productionbasereturndetailLost values('" + textBox1.Text + "'," + h["mid"].IsNullAsEmpty() + "," +
                    "" + d.Cells["Quantity"].Value.IsNullAsEmpty() + ",'" + LotId + "','" + h["id"].IsNullAsEmpty() + "'" +
                    ")";//บันทึกข้อมูลรายการวัตถุดิบที่เสีย
                    sqlAdd.Add(sqlz);
                }
                ///////////////////////////
                String sqls = "insert into productionbasereturn values('" + textBox1.Text + "'," + h["mid"].IsNullAsEmpty() + "," +
                    "" + h["mquantity"].IsNullAsEmpty() + "," + sum + "," +
                    "" + (((h["mquantity"].ToDouble()) <= (sum+sumf)) ? 1 : 0) + "," +
                    "'" + LotId + "','" + h["id"].IsNullAsEmpty() + "'," + h["mquantity"].IsNullAsEmpty() + "" +
                    ")";//บันทึกข้อมูลวัตถุดิบที่ต้องใช้
                String sqlf = "insert into productionbasereturnfail values('" + textBox1.Text + "'," + h["mid"].IsNullAsEmpty() + "," +
                    "" + h["mquantity"].IsNullAsEmpty() + "," + sumf + "," +
                    "" + (((h["mquantity"].ToDouble()) <= (sum + sumf)) ? 1 : 0) + "," +
                    "'" + LotId + "','" + h["id"].IsNullAsEmpty() + "'," + h["mquantity"].IsNullAsEmpty() + "" +
                    ")";//บันทึกข้อมูลรับคืนวัตถุดิบที่เสีย
                String sqll = "insert into productionbasereturnLost values('" + textBox1.Text + "'," + h["mid"].IsNullAsEmpty() + "," +
                    "" + h["mquantity"].IsNullAsEmpty() + "," + sumf + "," +
                    "" + (((h["mquantity"].ToDouble()) <= (sum + sumf)) ? 1 : 0) + "," +
                    "'" + LotId + "','" + h["id"].IsNullAsEmpty() + "'," + h["mquantity"].IsNullAsEmpty() + "" +
                    ")";//บันทึกข้อมูลรับคืนวัตถุดิบขาด
                ////////////////////////////
                totalsum += sum;
                msum += (h["mquantity"].ToDouble());
                sqlAdd.Add(sqls);
                ///////////////////////////
                totalsumf += sumf;
                msumf += (h["mquantity"].ToDouble());
                sqlAdd.Add(sqlf);
            }

            if ((msum+msumf) <= (totalsum+totalsumf))
            {
                //String up = "update productionreturn set ProductionReturnStatusId = 1 where ProductionReturnId = '" + textBox1.Text + "'";
                //sqlAdd.Add(up);//ปรับสถานะของใบรับคืนวัตถุดิบ
            }

            if (MetroFramework.MetroMessageBox.Show(this, "คุณต้องการบันทึกข้อมูลใบรับคืนเลขที่ " + textBox1.Text + "\nใช่หรือไม่ ?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (Class.DBConnString.clsDB.ExecuteTransaction(sqlAdd.ToArray()))
                {
                    if (MetroFramework.MetroMessageBox.Show(this, "บันทึกสำเร็จ คุณต้องการพิมพ์รายงาน " + textBox1.Text + "\nใช่หรือไม่ ?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        ProductionNew.ShowReport rpt = new ProductionNew.ShowReport(Data(textBox1.Text),1);
                        rpt.ShowDialog();
                    }
                }
                this.DialogResult = DialogResult.OK;
            }
        }