/// <summary>
        /// แก้ไข PO ที่ยังไม่ approve เราจะ Disable  details ทอ้งทั้งหมด และ add new details ใหม่
        /// </summary>
        private void SaveCommit()
        {
            using (SSLsEntities db = new SSLsEntities())
            {
                var hd = db.POHeader.SingleOrDefault(w => w.Id == _id);
                hd.UpdateDate = DateTime.Now;
                hd.UpdateBy = SingletonAuthen.Instance().Id;
                if (discountPercent)
                {
                    hd.DiscountBath = Library.ConvertDecimalToStringForm(Library.CalPercentByValue(hd.TotalHasVat + hd.TotalUnVat, decimal.Parse(textBoxDiscountInvoice.Text))); // แปลงจากคีย์ % เป็น บาท
                    hd.DiscountPercent = textBoxDiscountInvoice.Text + " %";
                }
                else
                {
                    hd.DiscountBath = textBoxDiscountInvoice.Text.Replace("บ", "");
                    //hd.DiscountPercent = hd.TotalDiscount + ""; // แปลงเงินบาทเป็น % 
                    hd.DiscountPercent = Library.GetPercentFromDiscountBath(decimal.Parse(hd.DiscountBath), hd.TotalHasVat + hd.TotalUnVat) + " %";
                }
                hd.DiscountInput = textBoxDiscountInvoice.Text;
                hd.TotalPrice = decimal.Parse(textBoxTotal.Text);
                hd.TotalHasVat = decimal.Parse(textBoxTotalHasVat.Text);
                hd.TotalUnVat = decimal.Parse(textBoxTotalUnVat.Text);
                hd.TotalDiscount = decimal.Parse(textBoxTotalDiscountInvoice.Text);
                hd.TotalPriceDiscount = decimal.Parse(textBoxTotalAfterDis.Text);
                hd.TotalVat = decimal.Parse(textBoxTotalVat.Text);
                hd.TotalBalance = decimal.Parse(textBoxTotalBalance.Text);
                /// disable old
                foreach (var item in hd.PODetail)
                {
                    if (item.Enable == true)
                    {
                        item.Enable = false;
                        item.UpdateDate = DateTime.Now;
                        item.UpdateBy = Singleton.SingletonAuthen.Instance().Id;
                        db.Entry(item).State = EntityState.Modified;
                    }
                }
                /// add new
                //List<PODetail> details = new List<PODetail>();
                PODetail detail;
                decimal costOnly = 0;
                decimal costAndVat = 0;
                decimal totalCost = 0;
                decimal totalQty = 0;
                decimal totalGift = 0;
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    detail = new PODetail();
                    costOnly = decimal.Parse(dataGridView1.Rows[i].Cells[colCostPerUnit].Value.ToString());
                    costAndVat = decimal.Parse(dataGridView1.Rows[i].Cells[colCostVatPerUnit].Value.ToString());
                    if (_poCostType == MyConstant.POCostType.CostOnly)
                    {
                        totalCost = costOnly * decimal.Parse(dataGridView1.Rows[i].Cells[colQty].Value.ToString());
                    }
                    else
                    {
                        totalCost = costAndVat * decimal.Parse(dataGridView1.Rows[i].Cells[colQty].Value.ToString());
                    }

                    detail.Qty = decimal.Parse(dataGridView1.Rows[i].Cells[colQty].Value.ToString());
                    totalQty += detail.Qty;

                    detail.GiftQty = decimal.Parse(dataGridView1.Rows[i].Cells[colGift].Value.ToString());
                    totalGift += detail.GiftQty;

                    detail.CostOnly = costOnly;
                    detail.CostAndVat = costAndVat;
                    detail.DiscountInput = dataGridView1.Rows[i].Cells[colDiscount].Value.ToString();
                    detail.DiscountBath = decimal.Parse(dataGridView1.Rows[i].Cells[colDiscountBath].Value.ToString());
                    detail.TotalCost = totalCost - decimal.Parse(dataGridView1.Rows[i].Cells[colDiscountBath].Value.ToString());
                    detail.CreateDate = DateTime.Now;
                    detail.CreateBy = SingletonAuthen.Instance().Id;
                    detail.UpdateDate = DateTime.Now;
                    detail.UpdateBy = SingletonAuthen.Instance().Id;
                    detail.Enable = true;
                    //detail.RcvQty = 0;
                    detail.FKProductDetail = int.Parse(dataGridView1.Rows[i].Cells[colFKProDtlId].Value.ToString());
                    if (dataGridView1.Rows[i].Cells[colInterface].Value != null)
                    {
                        string interfaceDate = dataGridView1.Rows[i].Cells[colInterface].Value.ToString();
                        detail.InterfaceDate = DateTime.Parse(interfaceDate);
                    }
                    //int id = int.Parse(dataGridView1.Rows[i].Cells[colId].Value.ToString());
                    if (dataGridView1.Rows[i].Cells[colId].Value != null)
                    {
                        int id = int.Parse(dataGridView1.Rows[i].Cells[colId].Value.ToString());
                        detail.RcvQty = hd.PODetail.SingleOrDefault(w => w.Id == id).RcvQty;
                        detail.RcvGiftQty = hd.PODetail.SingleOrDefault(w => w.Id == id).RcvGiftQty;
                    }
                    detail.Sequence = hd.SequenceEdit;
                    hd.PODetail.Add(detail);
                    if (i >= dataGridView1.Rows.Count - 2) break;
                }
                hd.TotalQty = totalQty;
                hd.TotalGift = totalGift;
                db.SaveChanges();
                MainReportViewer mr = new MainReportViewer(this, _id);
                mr.ShowDialog();

            }
        }
        private void UnApproveSave()
        {
            List <int> idsPO = new List <int>();

            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                var    po    = dataGridView1.Rows[i].Cells[3].Value.ToString();
                string check = "";
                if (dataGridView1.Rows[i].Cells[9].Value == null)
                {
                    check = "FALSE";
                }
                else
                {
                    check = dataGridView1.Rows[i].Cells[9].Value.ToString();
                }

                var id = dataGridView1.Rows[i].Cells[0].Value.ToString();
                Console.WriteLine(id + " " + po + " --------- " + check);
                if (check.ToUpper() == "TRUE")
                {
                    idsPO.Add(int.Parse(id));
                }
            }
            Console.WriteLine(idsPO.Count());
            if (idsPO.Count() < 1)
            {
                MessageBox.Show("ไม่มีพบเลือกรายการ");
                return;
            }
            else if (idsPO.Count() > 0 && textBoxNotApproveRemark.Text == "")
            {
                MessageBox.Show("กรุณากรอกเหตุผล");
                return;
            }
            using (SSLsEntities db = new SSLsEntities())
            {
                var             pos      = db.POHeader.Include("PODetail").Where(w => idsPO.Contains(w.Id)).ToList();
                List <POHeader> poAddNew = new List <POHeader>();
                foreach (var item in pos)
                {
                    if (item.ApproveDate != null)
                    {
                        // add new ก่อน
                        POHeader po = new POHeader();
                        po.FKBranch           = item.FKBranch;
                        po.FKVender           = item.FKVender;
                        po.PONo               = item.PONo;
                        po.PODate             = item.PODate;
                        po.FKBudgetYear       = item.FKBudgetYear;
                        po.ReferenceNo        = item.ReferenceNo;
                        po.DueDate            = item.DueDate;
                        po.POExpire           = item.POExpire;
                        po.FKPaymentType      = item.FKPaymentType;
                        po.DiscountInput      = item.DiscountInput;
                        po.DiscountBath       = item.DiscountBath;
                        po.DiscountPercent    = item.DiscountPercent;
                        po.Description        = item.Description;
                        po.NotApproveDate     = item.NotApproveDate;
                        po.NotApproveBy       = item.NotApproveBy;
                        po.NotApproveRemark   = item.NotApproveRemark;
                        po.ApproveBy          = null;
                        po.ApproveDate        = null;
                        po.TotalPrice         = item.TotalPrice;
                        po.TotalHasVat        = item.TotalHasVat;
                        po.TotalUnVat         = item.TotalUnVat;
                        po.TotalDiscount      = item.TotalDiscount;
                        po.TotalPriceDiscount = item.TotalPriceDiscount;
                        po.TotalVat           = item.TotalVat;
                        po.TotalBalance       = item.TotalBalance;
                        po.TotalQty           = item.TotalQty;
                        po.TotalGift          = item.TotalGift;
                        po.CreateDate         = item.CreateDate;
                        po.CreateBy           = item.CreateBy;
                        po.UpdateDate         = item.UpdateDate;
                        po.UpdateBy           = item.UpdateBy;
                        po.Enable             = true;
                        po.FKPOStatus         = item.FKPOStatus;
                        po.UnApproveDate      = null;
                        po.UpApproveBy        = null;
                        po.UpApproveRemark    = null;
                        po.SequenceEdit       = item.SequenceEdit + 1;

                        List <PODetail> details = new List <PODetail>();
                        PODetail        dtl;
                        foreach (var d in item.PODetail.Where(w => w.Enable == true).ToList())
                        {
                            dtl = new PODetail();
                            dtl.FKProductDetail = d.FKProductDetail;
                            dtl.Qty             = d.Qty;
                            dtl.GiftQty         = d.GiftQty;
                            dtl.CostOnly        = d.CostOnly;
                            dtl.CostAndVat      = d.CostAndVat;
                            dtl.DiscountInput   = d.DiscountInput;
                            dtl.DiscountBath    = d.DiscountBath;
                            dtl.TotalCost       = d.TotalCost;
                            dtl.CreateDate      = d.CreateDate;
                            dtl.CreateBy        = d.CreateBy;
                            dtl.UpdateDate      = d.UpdateDate;
                            dtl.UpdateBy        = d.UpdateBy;
                            dtl.Enable          = d.Enable;
                            dtl.RcvQty          = d.RcvQty;
                            dtl.RcvGiftQty      = d.RcvGiftQty;
                            dtl.InterfaceDate   = d.InterfaceDate;
                            dtl.Sequence        = po.SequenceEdit;
                            po.PODetail.Add(dtl);
                        }
                        db.POHeader.Add(po);
                        ///////////////////////////////////// disableตัวเดิมทิ้ง
                        item.UnApproveDate   = DateTime.Now;
                        item.UpApproveBy     = Singleton.SingletonAuthen.Instance().Id;
                        item.UpApproveRemark = textBoxNotApproveRemark.Text;
                        item.Enable          = false;
                        db.Entry(item).State = EntityState.Modified;
                        foreach (var dtlForDisable in item.PODetail.Where(w => w.Enable == true).ToList())
                        {
                            // disable รายละเอียด
                            dtlForDisable.Enable          = false;
                            db.Entry(dtlForDisable).State = EntityState.Modified;
                        }
                        //SendAutoMapper(item);
                        //POHeader po = (POHeader)Mapper.Map<POHeader>(item);
                        //poAddNew.Add(po);
                        //db.POHeader.Attach(po);
                        //db.POHeader.Add(po);
                        //POHeader po = new POHeader();
                        //po = item;
                        //po.PODetail = null;
                        //po.ApproveBy = null;
                        //po.ApproveDate = null;
                        //po.UnApproveDate = null;
                        //po.UpApproveBy = null;
                        //po.UpApproveRemark = null;
                        //po.Enable = true;
                        //List<PODetail> details = new List<PODetail>();
                        //PODetail dtls;
                        //foreach (var dtl in item.PODetail.Where(w => w.Enable == true).ToList())
                        //{
                        //    dtls = new PODetail();
                        //    dtls = dtl;
                        //    details.Add(dtls);
                        //    //po.PODetail.Add(dtls);
                        //}
                        //po.PODetail = details;
                        //db.POHeader.Add(po);
                        //db.SaveChanges();
                    }
                    else
                    {
                        MessageBox.Show("ต้องเป็น PO ที่อนุมัติแล้วเท่านั้น " + item.PONo);
                        return;
                    }
                }
                db.SaveChanges();
            }
            Search();
        }