/// <summary>
 /// จากการแก้ไข
 /// </summary>
 /// <param name="transferOutListForm"></param>
 /// <param name="code"></param>
 public TransferOutForm(TransferOutListForm transferOutListForm, string code)
 {
     InitializeComponent();
     using (SSLsEntities db = new SSLsEntities())
     {
         hd = db.StoreFrontTransferOut.FirstOrDefault(w => w.Code == code);
         if (hd.Enable == false)
         {
             button2.Enabled = false;
         }
         else if (hd.ConfirmDate != null)
         {
             button2.Enabled = false;
         }
         // bindding
         BinddingBranchSelected(hd.Branch);
         textBoxTOCode.Text       = hd.Code;
         textBoxTODate.Text       = Library.ConvertDateToThaiDate(hd.CreateDate);
         textBoxDesc.Text         = hd.Remark;
         textBoxQtyUnit.Text      = Library.ConvertDecimalToStringForm(hd.TotalQtyUnit);
         textBoxTotalBalance.Text = Library.ConvertDecimalToStringForm(hd.TotalBalance);
         this.transferOutListForm = transferOutListForm;
         this.code = code;
         foreach (var item in hd.StoreFrontTransferOutDtl.Where(w => w.Enable == true).ToList())
         {
             dataGridView1.Rows.Add
             (
                 item.FKProductDetails,
                 item.ProductDetails.Code,
                 "",
                 item.ProductDetails.Products.ThaiName,
                 item.ProductDetails.PackSize,
                 item.ProductDetails.ProductUnit.Name,
                 Library.ConvertDecimalToStringForm(item.Qty),
                 Library.ConvertDecimalToStringForm(item.CostPerUnit),
                 Library.ConvertDecimalToStringForm(item.TotalPrice),
                 item.ProductDetails.Products.ProductVatType.Name,
                 item.Description
             );
         }
         //dataGridView1.Rows.Add("");
     }
 }
        private void SaveCommit()
        {
            using (SSLsEntities db = new SSLsEntities())
            {
                if (hd != null)
                {
                    // พบการแก้ไข
                    StoreFrontTransferOut sf = db.StoreFrontTransferOut.SingleOrDefault(w => w.Code == hd.Code);
                    sf.Remark    = textBoxDesc.Text;
                    sf.FK2Branch = _BranchId;
                    foreach (var item in sf.StoreFrontTransferOutDtl.Where(w => w.Enable == true).ToList())
                    {
                        item.Enable          = false;
                        db.Entry(item).State = EntityState.Modified;
                    }

                    List <StoreFrontTransferOutDtl> details = new List <StoreFrontTransferOutDtl>();
                    StoreFrontTransferOutDtl        detail;
                    decimal totalPiece = 0;
                    decimal totalUnit  = 0;
                    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                    {
                        var code = dataGridView1.Rows[i].Cells[colCode].Value;
                        if (code == null)
                        {
                            continue;
                        }
                        code = code.ToString();

                        detail                  = new StoreFrontTransferOutDtl();
                        detail.Enable           = true;
                        detail.Description      = dataGridView1.Rows[i].Cells[colLocation].Value.ToString();
                        detail.CreateDate       = DateTime.Now;
                        detail.CreateBy         = Singleton.SingletonAuthen.Instance().Id;
                        detail.UpdateDate       = DateTime.Now;
                        detail.UpdateBy         = Singleton.SingletonAuthen.Instance().Id;
                        detail.FKProductDetails = int.Parse(dataGridView1.Rows[i].Cells[colId].Value.ToString());
                        var prodDtl = Singleton.SingletonProduct.Instance().ProductDetails.SingleOrDefault(w => w.Id == detail.FKProductDetails);

                        detail.Qty              = decimal.Parse(dataGridView1.Rows[i].Cells[colQty].Value.ToString());
                        detail.CostPerUnit      = decimal.Parse(dataGridView1.Rows[i].Cells[colCostPerUnit].Value.ToString());
                        detail.BeforeVat        = 0;
                        detail.Vat              = 0;
                        detail.TotalPrice       = decimal.Parse(dataGridView1.Rows[i].Cells[colTotalCost].Value.ToString());
                        detail.SellPricePerUnit = prodDtl.SellPrice;
                        details.Add(detail);
                        totalPiece += (detail.Qty * prodDtl.PackSize);
                        totalUnit  += detail.Qty;
                        sf.StoreFrontTransferOutDtl.Add(detail);
                    }
                    sf.TotalQty       = totalPiece;
                    sf.TotalQtyUnit   = totalUnit;
                    sf.TotalUnVat     = 0;
                    sf.TotalBeforeVat = 0;
                    sf.TotalVat       = 0;
                    sf.FKEmployee     = _EmployeeId;

                    sf.TotalBalance    = decimal.Parse(textBoxTotalBalance.Text);
                    db.Entry(sf).State = EntityState.Modified;

                    db.SaveChanges();
                    transferOutListForm.ReloadGrid();
                    this.Dispose();
                }
                else
                {
                    StoreFrontTransferOut sf = new StoreFrontTransferOut();
                    sf.Code       = textBoxTOCode.Text;
                    sf.Enable     = true;
                    sf.Remark     = textBoxDesc.Text;
                    sf.CreateDate = DateTime.Now;
                    sf.CreateBy   = Singleton.SingletonAuthen.Instance().Id;
                    sf.UpdateDate = DateTime.Now;
                    sf.UpdateBy   = Singleton.SingletonAuthen.Instance().Id;
                    sf.FK2Branch  = _BranchId;
                    List <StoreFrontTransferOutDtl> details = new List <StoreFrontTransferOutDtl>();
                    StoreFrontTransferOutDtl        detail;
                    decimal totalPiece = 0;
                    decimal totalUnit  = 0;
                    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                    {
                        var code = dataGridView1.Rows[i].Cells[colCode].Value;
                        if (code == null)
                        {
                            continue;
                        }
                        code = code.ToString();

                        detail                  = new StoreFrontTransferOutDtl();
                        detail.Enable           = true;
                        detail.Description      = dataGridView1.Rows[i].Cells[colLocation].Value.ToString();
                        detail.CreateDate       = DateTime.Now;
                        detail.CreateBy         = Singleton.SingletonAuthen.Instance().Id;
                        detail.UpdateDate       = DateTime.Now;
                        detail.UpdateBy         = Singleton.SingletonAuthen.Instance().Id;
                        detail.FKProductDetails = int.Parse(dataGridView1.Rows[i].Cells[colId].Value.ToString());
                        var prodDtl = Singleton.SingletonProduct.Instance().ProductDetails.SingleOrDefault(w => w.Id == detail.FKProductDetails);

                        detail.Qty              = decimal.Parse(dataGridView1.Rows[i].Cells[colQty].Value.ToString());
                        detail.CostPerUnit      = decimal.Parse(dataGridView1.Rows[i].Cells[colCostPerUnit].Value.ToString());
                        detail.BeforeVat        = 0;
                        detail.Vat              = 0;
                        detail.TotalPrice       = decimal.Parse(dataGridView1.Rows[i].Cells[colTotalCost].Value.ToString());
                        detail.SellPricePerUnit = prodDtl.SellPrice;
                        details.Add(detail);
                        totalPiece += (detail.Qty * prodDtl.PackSize);
                        totalUnit  += detail.Qty;
                    }

                    sf.TotalQty                 = totalPiece;
                    sf.TotalQtyUnit             = totalUnit;
                    sf.TotalUnVat               = 0;
                    sf.TotalBeforeVat           = 0;
                    sf.TotalVat                 = 0;
                    sf.FKEmployee               = _EmployeeId;
                    sf.StoreFrontTransferOutDtl = details;
                    sf.TotalBalance             = decimal.Parse(textBoxTotalBalance.Text);
                    db.StoreFrontTransferOut.Add(sf);
                    db.SaveChanges();
                    textBoxTotalBalance.Text = "0.00";
                    textBoxDesc.Text         = "";
                    textBoxQtyUnit.Text      = "0.00";
                    int    countTO = db.StoreFrontTransferOut.Where(w => w.CreateDate.Year == DateTime.Now.Year && w.CreateDate.Month == DateTime.Now.Month).Count() + 1;
                    string toCode  = MyConstant.PrefixForGenerateCode.TransferOut + Singleton.SingletonThisBudgetYear.Instance().ThisYear.CodeYear + DateTime.Now.ToString("MM") + Library.GenerateCodeFormCount(countTO, 4);
                    textBoxTOCode.Text = toCode;
                    textBoxTODate.Text = Library.ConvertDateToThaiDate(DateTime.Now);

                    dataGridView1.Rows.Clear();
                    dataGridView1.Refresh();
                    dataGridView1.Rows.Add(1);
                }
            }
        }