private void button6_Click(object sender, EventArgs e)
        {
            int row = dataGridView1.CurrentRow.Index;

            int id = int.Parse(dataGridView1.Rows[row].Cells[0].Value.ToString());

            MainReportViewer mr = new MainReportViewer(this, id);

            mr.ShowDialog();
        }
        private void PrintConfirm()
        {
            int seq = int.Parse(textBoxKeyValue.Text.Trim());

            if (seq > v)
            {
                MessageBox.Show("ไม่ถูกต้อง");
            }
            else
            {
                using (SSLsEntities db = new SSLsEntities())
                {
                    var data            = db.POHeader.SingleOrDefault(w => w.SequenceEdit == seq && w.PONo == _code);
                    MainReportViewer mr = new MainReportViewer(data.Id);
                    mr.ShowDialog();
                }
            }
        }
        private void button6_Click(object sender, EventArgs e)
        {
            int    row  = dataGridView1.CurrentRow.Index;
            int    id   = int.Parse(dataGridView1.Rows[row].Cells[0].Value.ToString());
            string code = dataGridView1.Rows[row].Cells[3].Value.ToString();

            using (SSLsEntities db = new SSLsEntities())
            {
                //var polist = db.POHeader.Where(w => w.PONo == code).OrderBy(w => w.SequenceEdit).ToList();
                //if (polist.Count() > 1)
                //{
                //    //MessageBox.Show("พบ PO หลายฉบับ");
                //    POApproveManagePrintSequenceDialog obj = new POApproveManagePrintSequenceDialog(this, polist.Count(), code);
                //    obj.ShowDialog();
                //}
                //else
                //{
                //    MainReportViewer mr = new MainReportViewer(this, id);
                //    mr.ShowDialog();
                //}
                MainReportViewer mr = new MainReportViewer(this, id);
                mr.ShowDialog();
            }
        }
        /// <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();

            }
        }