private void SaveCommit()
        {
            using (SSLsEntities db = new SSLsEntities())
            {
                //var ss = textBoxTotalCost.Text;
                // 1. เพิ่มใหม่
                List <ProcessedGoods> lists = new List <ProcessedGoods>();
                ProcessedGoods        obj;
                string desc  = textBoxDesc.Text;
                string docno = textBoxDocName.Text;
                List <ProductDetails> products = Singleton.SingletonProduct.Instance().ProductDetails;
                int idHD    = products.SingleOrDefault(w => w.Id == _FKProDtl).Products.Id;
                var stockHD = db.StoreFrontStock.SingleOrDefault(w => w.FKProduct == idHD && w.Enable == true);
                if (stockHD == null)
                {
                    StoreFrontStock hd = new StoreFrontStock();
                    hd.Description = "-";
                    hd.CreateDate  = DateTime.Now;
                    hd.CreateBy    = Singleton.SingletonAuthen.Instance().Id;
                    hd.UpdateDate  = DateTime.Now;
                    hd.UpdateBy    = Singleton.SingletonAuthen.Instance().Id;
                    hd.Enable      = true;
                    hd.CurrentQty  = 0;
                    hd.FKProduct   = idHD;
                    db.StoreFrontStock.Add(hd);
                    db.SaveChanges();
                    stockHD = db.StoreFrontStock.SingleOrDefault(w => w.FKProduct == idHD && w.Enable == true);
                }
                List <StoreFrontStockDetails> details = new List <StoreFrontStockDetails>();
                StoreFrontStockDetails        detail  = new StoreFrontStockDetails();
                detail.DocNo             = docno;
                detail.DocDtlNumber      = 0;
                detail.Description       = "";
                detail.CreateDate        = DateTime.Now;
                detail.CreateBy          = Singleton.SingletonAuthen.Instance().Id;
                detail.UpdateDate        = DateTime.Now;
                detail.UpdateBy          = Singleton.SingletonAuthen.Instance().Id;
                detail.Enable            = true;
                detail.ActionQty         = decimal.Parse(textBoxQty.Text);
                detail.FKStoreFrontStock = stockHD.Id;
                detail.FKTransactionType = MyConstant.PosTransaction.SetStock;
                detail.Barcode           = textBoxGoodsCode.Text;
                detail.Name             = products.SingleOrDefault(w => w.Id == _FKProDtl).Products.ThaiName;
                detail.FKProductDetails = _FKProDtl;
                detail.ResultQty        = 0;
                detail.PackSize         = 1;
                detail.CostOnlyPerUnit  = products.SingleOrDefault(w => w.Id == _FKProDtl).CostOnly;
                detail.SellPricePerUnit = products.SingleOrDefault(w => w.Id == _FKProDtl).SellPrice;
                details.Add(detail);

                //ProcessedGoods procGoods;
                //List<ProcessedGoods> procList = new List<ProcessedGoods>();
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    //procGoods = new ProcessedGoods();
                    obj    = new ProcessedGoods();
                    detail = new StoreFrontStockDetails();
                    var getBarcode = dataGridView1.Rows[i].Cells[colCode].Value;
                    if (getBarcode == null)
                    {
                        continue;
                    }
                    else if (getBarcode.ToString() == "")
                    {
                        continue;
                    }
                    decimal qty        = decimal.Parse(dataGridView1.Rows[i].Cells[colQty].Value.ToString());
                    decimal costOnly   = decimal.Parse(dataGridView1.Rows[i].Cells[colCostOnly].Value.ToString());
                    decimal costAndVat = decimal.Parse(dataGridView1.Rows[i].Cells[colCostAndVat].Value.ToString());
                    decimal packsize   = decimal.Parse(dataGridView1.Rows[i].Cells[colpz].Value.ToString());
                    decimal sellprice  = decimal.Parse(dataGridView1.Rows[i].Cells[colPrice].Value.ToString());
                    int     id         = int.Parse(dataGridView1.Rows[i].Cells[colFKId].Value.ToString());
                    obj.FKProductDtl = _FKProDtl;
                    obj.Barcode      = getBarcode.ToString();
                    obj.CreateDate   = DateTime.Now;
                    obj.CreateBy     = Singleton.SingletonAuthen.Instance().Id;
                    obj.UpdateDate   = DateTime.Now;
                    obj.UpdateBy     = Singleton.SingletonAuthen.Instance().Id;
                    obj.CostOnly     = costOnly;
                    obj.CostAndVat   = costAndVat;
                    obj.CostVat      = costAndVat - costOnly;
                    obj.PackSize     = packsize;
                    obj.SellPrice    = sellprice;
                    obj.Qty          = qty;
                    obj.Description  = desc;
                    obj.Enable       = true;
                    obj.DocumentNo   = docno;
                    lists.Add(obj);
                    // ตัด Stock
                    int idHDForProd = products.SingleOrDefault(w => w.Id == id).Products.Id; // fkproduct
                    var getStockHD  = db.StoreFrontStock.SingleOrDefault(w => w.FKProduct == idHDForProd && w.Enable == true);
                    if (getStockHD == null)
                    {
                        AddStockHD(idHDForProd);
                        getStockHD = db.StoreFrontStock.SingleOrDefault(w => w.FKProduct == idHDForProd && w.Enable == true);
                    }
                    detail.DocNo             = docno;
                    detail.DocDtlNumber      = (i + 1);
                    detail.Description       = "";
                    detail.CreateDate        = DateTime.Now;
                    detail.CreateBy          = Singleton.SingletonAuthen.Instance().Id;
                    detail.UpdateDate        = DateTime.Now;
                    detail.UpdateBy          = Singleton.SingletonAuthen.Instance().Id;
                    detail.Enable            = true;
                    detail.ActionQty         = (qty * packsize) * decimal.Parse(textBoxQty.Text);
                    detail.FKStoreFrontStock = getStockHD.Id;
                    detail.FKTransactionType = MyConstant.PosTransaction.ProcessingGoods;
                    detail.Barcode           = getBarcode.ToString();
                    detail.Name             = products.SingleOrDefault(w => w.Id == id).Products.ThaiName;
                    detail.FKProductDetails = id;
                    detail.ResultQty        = 0;
                    detail.PackSize         = packsize;
                    detail.CostOnlyPerUnit  = costOnly;
                    detail.SellPricePerUnit = sellprice;
                    details.Add(detail);
                }
                // add to process goods
                db.ProcessedGoods.AddRange(lists);
                // add to stock
                db.StoreFrontStockDetails.AddRange(details);
                db.SaveChanges();
            }
        }
        /// <summary>
        /// คืน stock กรณีลด รายการ
        /// </summary>
        private void CheckAddNew()
        {
            List <ProductDetails> products = Singleton.SingletonProduct.Instance().ProductDetails;

            using (SSLsEntities db = new SSLsEntities())
            {
                List <StoreFrontStockDetails> details = new List <StoreFrontStockDetails>();
                var getPGoods = db.ProcessedGoods.Where(w => w.FKProductDtl == _FKProDtl && w.Enable == true).ToList();
                List <ProcessedGoods> lists = new List <ProcessedGoods>();
                ProcessedGoods        obj;
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    var getBarcode = dataGridView1.Rows[i].Cells[colCode].Value;
                    if (getBarcode == null)
                    {
                        continue;
                    }
                    else if (getBarcode.ToString() == "")
                    {
                        continue;
                    }
                    var detect = getPGoods.FirstOrDefault(w => w.Barcode == getBarcode.ToString());
                    if (detect != null)
                    {
                        // แสดงว่าเป็นบาร์โค้ดเดิม
                    }
                    else
                    {
                        // แสดงว่า เพิ่มมาใหม่
                        obj = new ProcessedGoods();
                        obj.FKProductDtl = _FKProDtl;
                        obj.Barcode      = getBarcode.ToString();
                        obj.CreateDate   = DateTime.Now;
                        obj.CreateBy     = Singleton.SingletonAuthen.Instance().Id;
                        obj.UpdateDate   = DateTime.Now;
                        obj.UpdateBy     = Singleton.SingletonAuthen.Instance().Id;

                        decimal qty        = decimal.Parse(dataGridView1.Rows[i].Cells[colQty].Value.ToString());
                        decimal costOnly   = decimal.Parse(dataGridView1.Rows[i].Cells[colCostOnly].Value.ToString());
                        decimal costAndVat = decimal.Parse(dataGridView1.Rows[i].Cells[colCostAndVat].Value.ToString());
                        decimal packsize   = decimal.Parse(dataGridView1.Rows[i].Cells[colpz].Value.ToString());
                        decimal sellprice  = decimal.Parse(dataGridView1.Rows[i].Cells[colPrice].Value.ToString());
                        obj.CostOnly    = costOnly;
                        obj.CostAndVat  = costAndVat;
                        obj.CostVat     = costAndVat - costOnly;
                        obj.PackSize    = packsize;
                        obj.SellPrice   = sellprice;
                        obj.Qty         = qty;
                        obj.Description = textBoxDesc.Text;
                        obj.Enable      = true;
                        obj.DocumentNo  = textBoxDocName.Text;
                        lists.Add(obj);

                        string desc  = textBoxDesc.Text;
                        string docno = textBoxDocName.Text;

                        var getGoodfromSing = products.FirstOrDefault(w => w.Code == obj.Barcode);
                        var stockHD         = db.StoreFrontStock.SingleOrDefault(w => w.FKProduct == getGoodfromSing.FKProduct && w.Enable == true);

                        if (stockHD == null)
                        {
                            StoreFrontStock hd = new StoreFrontStock();
                            hd.Description = "-";
                            hd.CreateDate  = DateTime.Now;
                            hd.CreateBy    = Singleton.SingletonAuthen.Instance().Id;
                            hd.UpdateDate  = DateTime.Now;
                            hd.UpdateBy    = Singleton.SingletonAuthen.Instance().Id;
                            hd.Enable      = true;
                            hd.CurrentQty  = 0;
                            hd.FKProduct   = getGoodfromSing.FKProduct;
                            db.StoreFrontStock.Add(hd);
                            db.SaveChanges();
                            stockHD = db.StoreFrontStock.SingleOrDefault(w => w.FKProduct == getGoodfromSing.FKProduct && w.Enable == true);
                        }
                        // ตัด stock
                        StoreFrontStockDetails detail = new StoreFrontStockDetails();
                        detail.DocNo             = docno;
                        detail.DocDtlNumber      = i + 1;
                        detail.Description       = "";
                        detail.CreateDate        = DateTime.Now;
                        detail.CreateBy          = Singleton.SingletonAuthen.Instance().Id;
                        detail.UpdateDate        = DateTime.Now;
                        detail.UpdateBy          = Singleton.SingletonAuthen.Instance().Id;
                        detail.Enable            = true;
                        detail.ActionQty         = (qty * packsize) * _QTY; // ยอดคงเหลือ
                        detail.FKStoreFrontStock = stockHD.Id;
                        detail.FKTransactionType = MyConstant.PosTransaction.ProcessingGoods;
                        detail.Barcode           = obj.Barcode;
                        detail.Name             = products.SingleOrDefault(w => w.Id == getGoodfromSing.Id).Products.ThaiName;
                        detail.FKProductDetails = getGoodfromSing.Id;
                        detail.ResultQty        = 0;
                        detail.PackSize         = 1;
                        detail.CostOnlyPerUnit  = products.SingleOrDefault(w => w.Id == getGoodfromSing.Id).CostOnly;
                        detail.SellPricePerUnit = products.SingleOrDefault(w => w.Id == getGoodfromSing.Id).SellPrice;
                        details.Add(detail);
                    }
                }
                // add to process goods
                db.StoreFrontStockDetails.AddRange(details);
                db.ProcessedGoods.AddRange(lists);
                db.SaveChanges();
            }
        }