private void CancelGoods()
        {
            using (SSLsEntities db = new SSLsEntities())
            {
                StoreFrontStockDetails        detail;
                List <StoreFrontStockDetails> details = new List <StoreFrontStockDetails>();
                var getPGoods = db.ProcessedGoods.Where(w => w.FKProductDtl == _FKProDtl && w.Enable == true).ToList();
                foreach (var item in getPGoods)
                {
                    string docno = textBoxDocName.Text;
                    List <ProductDetails> products = Singleton.SingletonProduct.Instance().ProductDetails;
                    var getBarcode = products.FirstOrDefault(w => w.Code == item.Barcode);
                    var stockHD    = db.StoreFrontStock.SingleOrDefault(w => w.FKProduct == getBarcode.FKProduct && w.Enable == true);
                    // ไม่ยุ่แล้ว
                    item.Enable = false;
                    // คืน stock ทั้งหมด
                    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         = (item.Qty * item.PackSize) * _QTY; // ยอดคงเหลือ
                    detail.FKStoreFrontStock = stockHD.Id;
                    detail.FKTransactionType = MyConstant.PosTransaction.CancelProcessingGoods;
                    detail.Barcode           = item.Barcode;
                    detail.Name              = products.SingleOrDefault(w => w.Id == getBarcode.Id).Products.ThaiName;
                    detail.FKProductDetails  = getBarcode.Id;
                    detail.ResultQty         = 0;
                    detail.PackSize          = 1;
                    detail.CostOnlyPerUnit   = products.SingleOrDefault(w => w.Id == getBarcode.Id).CostOnly;
                    detail.SellPricePerUnit  = products.SingleOrDefault(w => w.Id == getBarcode.Id).SellPrice;
                    details.Add(detail);
                    db.Entry(item).State = EntityState.Modified;
                }
                List <ProductDetails> products1 = Singleton.SingletonProduct.Instance().ProductDetails;
                var getBarcode1 = products1.FirstOrDefault(w => w.Code == textBoxGoodsCode.Text);
                var stockHD1    = db.StoreFrontStock.SingleOrDefault(w => w.FKProduct == getBarcode1.FKProduct && w.Enable == true);
                detail                   = new StoreFrontStockDetails();
                detail.DocNo             = textBoxDocName.Text;
                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         = _QTY; // ยอดคงเหลือ
                detail.FKStoreFrontStock = stockHD1.Id;
                detail.FKTransactionType = MyConstant.PosTransaction.CancelProcessingGoods;
                detail.Barcode           = textBoxGoodsCode.Text;
                detail.Name              = products1.SingleOrDefault(w => w.Id == getBarcode1.Id).Products.ThaiName;
                detail.FKProductDetails  = getBarcode1.Id;
                detail.ResultQty         = 0;
                detail.PackSize          = 1;
                detail.CostOnlyPerUnit   = products1.SingleOrDefault(w => w.Id == getBarcode1.Id).CostOnly;
                detail.SellPricePerUnit  = products1.SingleOrDefault(w => w.Id == getBarcode1.Id).SellPrice;
                details.Add(detail);

                db.StoreFrontStockDetails.AddRange(details);
                db.SaveChanges();
            }
        }
Exemple #2
0
        private void SaveCommit()
        {
            using (SSLsEntities db = new SSLsEntities())
            {
                int currentYear  = DateTime.Now.Year;
                int currentMonth = DateTime.Now.Month;
                var adjust       = db.AdjustStoreFront.Where(w => w.UpdateDate.Year == currentYear && w.UpdateDate.Month == currentMonth).Count() + 1;

                //Branch branch = Singleton.SingletonP
                string code = MyConstant.PrefixForGenerateCode.AdjustStoreFront + DateTime.Now.ToString("yy") + "" + DateTime.Now.ToString("MM") + Library.GenerateCodeFormCount(adjust, 4);

                // adjust
                AdjustStoreFront obj = new AdjustStoreFront();
                obj.Code         = code;
                obj.Enable       = true;
                obj.Description  = textBoxDesc.Text;
                obj.CreateDate   = DateTime.Now;
                obj.CreateBy     = Singleton.SingletonAuthen.Instance().Id;
                obj.UpdateDate   = DateTime.Now;
                obj.UpdateBy     = Singleton.SingletonAuthen.Instance().Id;
                obj.TotalBalance = dtl.CostOnly * decimal.Parse(textBoxQtyAd.Text);
                obj.TotalQtyUnit = decimal.Parse(textBoxQtyAd.Text);

                List <AdjustStoreFrontDetail> adDtl = new List <AdjustStoreFrontDetail>();
                adDtl.Add(new AdjustStoreFrontDetail()
                {
                    Enable           = true,
                    Description      = "",
                    CreateDate       = DateTime.Now,
                    CreateBy         = obj.CreateBy,
                    UpdateDate       = DateTime.Now,
                    UpdateBy         = obj.CreateBy,
                    FKProductDetails = dtl.Id,
                    Qty              = decimal.Parse(textBoxQtyAd.Text),
                    CostPerUnit      = dtl.CostOnly,
                    SellPricePerUnit = dtl.SellPrice
                });
                obj.AdjustStoreFrontDetail = adDtl;
                db.AdjustStoreFront.Add(obj);

                // stock
                StoreFrontStockDetails addDtl = new StoreFrontStockDetails();
                addDtl.DocNo        = code;
                addDtl.DocDtlNumber = 1;
                addDtl.Description  = "Adjust หน้าร้าน";
                addDtl.CreateDate   = DateTime.Now;
                addDtl.CreateBy     = SingletonAuthen.Instance().Id;
                addDtl.UpdateDate   = DateTime.Now;
                addDtl.UpdateBy     = SingletonAuthen.Instance().Id;
                addDtl.Enable       = true;
                addDtl.ActionQty    = decimal.Parse(textBoxQtyAd.Text) * dtl.PackSize;
                var stockHD = db.StoreFrontStock.FirstOrDefault(w => w.FKProduct == dtl.FKProduct && w.Enable == true);
                addDtl.FKStoreFrontStock = stockHD.Id;
                addDtl.FKTransactionType = MyConstant.PosTransaction.ADJ;
                addDtl.Barcode           = textBoxCode.Text;
                addDtl.Name             = dtl.Products.ThaiName;
                addDtl.FKProductDetails = dtl.Id;
                addDtl.ResultQty        = addDtl.ActionQty;

                addDtl.PackSize          = dtl.PackSize;
                addDtl.DocRefer          = "-";
                addDtl.DocReferDtlNumber = 0;
                addDtl.CostOnlyPerUnit   = dtl.CostOnly;
                addDtl.SellPricePerUnit  = dtl.SellPrice;

                db.StoreFrontStockDetails.Add(addDtl);
                db.SaveChanges();

                // open paper
                frmMainReport report = new frmMainReport(this, code);
                report.Show();
                dataGridView1.Rows.Clear();
                dataGridView1.Refresh();
                var data = db.AdjustStoreFront.OrderBy(w => w.CreateDate).Where(w => w.Enable == true).ToList();
                foreach (var item in data)
                {
                    dataGridView1.Rows.Add
                    (
                        item.Code,
                        Library.ConvertDateToThaiDate(item.CreateDate),
                        Library.GetFullNameUserById(item.CreateBy),
                        item.TotalQtyUnit,
                        item.TotalBalance,
                        item.Description
                    );
                }
            }
        }
        /// <summary>
        /// เพิ่ม = เพิ่มแปรรูป ลด stock รานการย่อย
        /// ลด = เพิ่ม stock รายการย่อย ลดแปรรูป
        /// </summary>
        /// <param name="transactions"></param>
        /// <param name="newQty"></param>
        /// <param name="transactions2"></param>
        void QtyEdit(int transactions, int newQty, int transactions2)
        {
            using (SSLsEntities db = new SSLsEntities())
            {
                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);

                List <StoreFrontStockDetails> details = new List <StoreFrontStockDetails>();
                StoreFrontStockDetails        detail  = new StoreFrontStockDetails();
                detail.DocNo             = textBoxDocName.Text;
                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         = newQty;
                detail.FKStoreFrontStock = stockHD.Id;
                detail.FKTransactionType = transactions;
                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();

                    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());

                    // ตัด 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             = textBoxDocName.Text;
                    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) * newQty;
                    detail.FKStoreFrontStock = getStockHD.Id;
                    detail.FKTransactionType = transactions2;
                    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 stock
                db.StoreFrontStockDetails.AddRange(details);
                db.SaveChanges();
            }
        }
        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();
            }
        }
        private void ReturnToStock()
        {
            List <string> barcodeOnGrid = new List <string>();

            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;
                }
                barcodeOnGrid.Add(getBarcode.ToString());
            }
            using (SSLsEntities db = new SSLsEntities())
            {
                List <StoreFrontStockDetails> details = new List <StoreFrontStockDetails>();
                var getPGoods = db.ProcessedGoods.Where(w => w.FKProductDtl == _FKProDtl && w.Enable == true).ToList();
                foreach (var item in getPGoods)
                {
                    if (barcodeOnGrid.FirstOrDefault(w => w == item.Barcode) != null)
                    {
                        // ยังยุ่
                    }
                    else
                    {
                        string desc  = textBoxDesc.Text;
                        string docno = textBoxDocName.Text;
                        List <ProductDetails> products = Singleton.SingletonProduct.Instance().ProductDetails;
                        var getBarcode = products.FirstOrDefault(w => w.Code == item.Barcode);
                        var stockHD    = db.StoreFrontStock.SingleOrDefault(w => w.FKProduct == getBarcode.FKProduct && w.Enable == true);
                        // ไม่ยุ่แล้ว
                        item.Enable = false;
                        // คืน stock ทั้งหมด
                        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         = (item.Qty * item.PackSize) * _QTY; // ยอดคงเหลือ
                        detail.FKStoreFrontStock = stockHD.Id;
                        detail.FKTransactionType = MyConstant.PosTransaction.CancelProcessingGoods;
                        detail.Barcode           = item.Barcode;
                        detail.Name             = products.SingleOrDefault(w => w.Id == getBarcode.Id).Products.ThaiName;
                        detail.FKProductDetails = getBarcode.Id;
                        detail.ResultQty        = 0;
                        detail.PackSize         = 1;
                        detail.CostOnlyPerUnit  = products.SingleOrDefault(w => w.Id == getBarcode.Id).CostOnly;
                        detail.SellPricePerUnit = products.SingleOrDefault(w => w.Id == getBarcode.Id).SellPrice;
                        details.Add(detail);
                    }
                }
                db.StoreFrontStockDetails.AddRange(details);
                db.SaveChanges();
            }
        }
Exemple #7
0
        private void SaveCommit()
        {
            using (SSLsEntities db = new SSLsEntities())
            {
                StoreFrontTransferWaste sft = new StoreFrontTransferWaste();
                sft.Enable      = true;
                sft.Code        = textBoxCode.Text;
                sft.CreateDate  = DateTime.Now;
                sft.CreateBy    = SingletonAuthen.Instance().Id;
                sft.Description = textBoxRemark.Text;
                sft.UpdateDate  = DateTime.Now;
                sft.UpdateBy    = SingletonAuthen.Instance().Id;
                decimal qtyPiece = 0;
                List <StoreFrontTransferWasteDtl> details = new List <StoreFrontTransferWasteDtl>();
                StoreFrontTransferWasteDtl        detail;
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    var code = dataGridView1.Rows[i].Cells[colCode].Value;
                    if (code == null)
                    {
                        continue;
                    }
                    detail                  = new StoreFrontTransferWasteDtl();
                    qtyPiece               += decimal.Parse(dataGridView1.Rows[i].Cells[colQty].Value.ToString()) * decimal.Parse(dataGridView1.Rows[i].Cells[colPZ].Value.ToString());
                    detail.Enable           = true;
                    detail.Description      = dataGridView1.Rows[i].Cells[colDescription].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());
                    detail.Qty              = decimal.Parse(dataGridView1.Rows[i].Cells[colQty].Value.ToString());
                    detail.CostPerUnit      = decimal.Parse(dataGridView1.Rows[i].Cells[colCostPerUnit].Value.ToString());
                    details.Add(detail);
                    //if (i >= dataGridView1.Rows.Count - 2) break;
                }
                sft.TotalQty     = qtyPiece;
                sft.TotalQtyUnit = details.Sum(w => w.Qty);

                sft.FKWarehouse   = MyConstant.WareHouse.StoreFront;
                sft.FKWasteReason = _WasteReason;
                sft.StoreFrontTransferWasteDtl = details;
                db.StoreFrontTransferWaste.Add(sft);
                db.SaveChanges();

                // + ห้องของเสีย
                // Initisl waste warehouse ก่อน
                List <int>            fkProDtl = details.Select(w => w.FKProductDetails).Distinct().ToList <int>();
                List <int>            fkPro    = Singleton.SingletonProduct.Instance().ProductDetails.Where(w => fkProDtl.Contains(w.Id)).Select(w => w.FKProduct).Distinct().ToList <int>();
                List <WasteWarehouse> wss      = new List <WasteWarehouse>();
                foreach (var item in fkPro)
                {
                    var data = db.WasteWarehouse.FirstOrDefault(w => w.FKProduct == item && w.Enable == true);
                    if (data == null)
                    {
                        WasteWarehouse ws = new WasteWarehouse();
                        ws.FKProduct   = item;
                        ws.FKWarehouse = MyConstant.WareHouse.WasteWarehouse;
                        ws.QtyPiece    = 0;
                        ws.QtyUnit     = 0;
                        ws.Description = "Auto Detect";
                        ws.Enable      = true;
                        ws.CreateDate  = DateTime.Now;
                        ws.CreateBy    = Singleton.SingletonAuthen.Instance().Id;
                        ws.UpdateDate  = DateTime.Now;
                        ws.UpdateBy    = Singleton.SingletonAuthen.Instance().Id;
                        wss.Add(ws);
                    }
                }
                db.WasteWarehouse.AddRange(wss);
                db.SaveChanges();
                /// จัดการ stock
                /// -storefront
                //Library.MakeValueForUpdateStockPos(details); // อันเดิม ยกเลิก
                /// + warehouse
                ///Library.MakeValueForUpdateStockWms(details);
                /// + ห้องของเสีย
                Library.AddWasteWarehouse(details, sft.Code);
                // print ใบ ของเสียหน้าร้าน สู่ ห้องของเสีย
                // Initisl หน้าร้าน ก่อน เผื่อยังไม่มี
                List <StoreFrontStock> stocks = new List <StoreFrontStock>();
                foreach (var item in fkPro)
                {
                    var data = db.StoreFrontStock.FirstOrDefault(w => w.Enable == true && w.FKProduct == item);
                    if (data == null)
                    {
                        stocks.Add(new StoreFrontStock()
                        {
                            CreateDate  = DateTime.Now,
                            CreateBy    = SingletonAuthen.Instance().Id,
                            UpdateDate  = DateTime.Now,
                            UpdateBy    = SingletonAuthen.Instance().Id,
                            Enable      = true,
                            CurrentQty  = 0,
                            FKProduct   = item,
                            Description = "พบของเสียห้านร้าน"
                        });
                    }
                }
                db.StoreFrontStock.AddRange(stocks);
                db.SaveChanges();
                // - หน้าร้าน
                int number = 1;
                foreach (var item in details)
                {
                    var proDtl  = Singleton.SingletonProduct.Instance().ProductDetails.SingleOrDefault(w => w.Id == item.FKProductDetails && w.Enable == true);
                    var stockHD = db.StoreFrontStock.FirstOrDefault(w => w.FKProduct == proDtl.FKProduct && w.Enable == true);
                    StoreFrontStockDetails addDtl = new StoreFrontStockDetails();
                    addDtl.DocNo             = sft.Code;
                    addDtl.DocDtlNumber      = number;
                    addDtl.Description       = "ของเสียหน้าร้าน";
                    addDtl.CreateDate        = DateTime.Now;
                    addDtl.CreateBy          = SingletonAuthen.Instance().Id;
                    addDtl.UpdateDate        = DateTime.Now;
                    addDtl.UpdateBy          = SingletonAuthen.Instance().Id;
                    addDtl.Enable            = true;
                    addDtl.ActionQty         = item.Qty * proDtl.PackSize;
                    addDtl.FKStoreFrontStock = stockHD.Id;
                    addDtl.FKTransactionType = MyConstant.PosTransaction.CNToWarehouse;
                    addDtl.Barcode           = proDtl.Code;
                    addDtl.Name             = proDtl.Products.ThaiName;
                    addDtl.FKProductDetails = item.FKProductDetails;
                    addDtl.ResultQty        = addDtl.ActionQty;
                    var lastAction = db.StoreFrontStockDetails.OrderByDescending(w => w.CreateDate).FirstOrDefault(w => w.FKProductDetails == item.FKProductDetails && w.Enable == true);
                    if (lastAction != null)
                    {
                        addDtl.ResultQty = lastAction.ResultQty - addDtl.ActionQty;
                    }
                    addDtl.PackSize          = proDtl.PackSize;
                    addDtl.DocRefer          = "-";
                    addDtl.DocReferDtlNumber = 0;
                    addDtl.CostOnlyPerUnit   = proDtl.CostOnly;
                    addDtl.SellPricePerUnit  = proDtl.SellPrice;

                    stockHD.CurrentQty      = stockHD.CurrentQty - addDtl.ActionQty;
                    db.Entry(stockHD).State = EntityState.Modified;

                    db.StoreFrontStockDetails.Add(addDtl);
                    db.SaveChanges();
                    number++;
                }

                try
                {
                    /// ใบของเสียหน้าร้าน
                    frmMainReport mr = new frmMainReport(this, sft.Code);
                    mr.Show();
                }
                catch (Exception)
                {
                    MessageBox.Show("จำนวนเอกสารผิดพลาด");
                }

                this.Dispose();
            }
        }
        private void SaveCommit()
        {
            using (SSLsEntities db = new SSLsEntities())
            {
                int    count   = db.GetGoodsStoreFront.Where(w => w.CreateDate.Year == DateTime.Now.Year && w.CreateDate.Month == DateTime.Now.Month).Count() + 1;
                string docCode = MyConstant.PrefixForGenerateCode.GetGoodsForUse + Singleton.SingletonThisBudgetYear.Instance().ThisYear.CodeYear + DateTime.Now.ToString("MM") + Library.GenerateCodeFormCount(count, 4);

                GetGoodsStoreFront gg = new GetGoodsStoreFront();
                List <GetGoodsStoreFrontDetails> details = new List <GetGoodsStoreFrontDetails>();
                GetGoodsStoreFrontDetails        detail;
                gg.Enable       = true;
                gg.Code         = docCode;
                gg.CreateDate   = DateTime.Now;
                gg.CreateBy     = Singleton.SingletonAuthen.Instance().Id;
                gg.UpdateDate   = DateTime.Now;
                gg.UpdateBy     = Singleton.SingletonAuthen.Instance().Id;
                gg.TotalQtyUnit = decimal.Parse(textBoxQtyUnit.Text);
                gg.TotalBalance = decimal.Parse(textBoxTotalBalance.Text);
                gg.Description  = textBoxDesc.Text;
                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 GetGoodsStoreFrontDetails();
                    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.SellPricePerUnit = prodDtl.SellPrice;
                    details.Add(detail);
                    gg.GetGoodsStoreFrontDetails.Add(detail);

                    // check product นี้ว่ามีในหน้าร้านหรือไม่ ถ้าไม่มีแสดงว่า ไม่เคยเบิกเติมหน้าร้าน เดี่ยวจะมีปันหา
                    var getTransactionPos = db.StoreFrontStockDetails.FirstOrDefault(w => w.Enable == true && w.FKProductDetails == detail.FKProductDetails);
                    if (getTransactionPos == null) // ถ้าไม่มีตัวตนในหน้าร้าน แปลว่า ของไม่มีการเบิกเติม แต่มีสินค้ายุ่จริง
                    {
                        MessageBox.Show("" + code + " " + prodDtl.Products.ThaiName + "(" + prodDtl.ProductUnit.Name + ") " + "ไม่มีในระบบ ไม่สามารถเบิกใช้ได้ กรุณาติดต่อ admin");
                        return;
                    }
                }
                db.GetGoodsStoreFront.Add(gg);
                /// add To Transaction
                int j = 1;
                foreach (var item in details)
                {
                    var prodDtl = SingletonProduct.Instance().ProductDetails.SingleOrDefault(w => w.Id == item.FKProductDetails);
                    StoreFrontStockDetails addDtl = new StoreFrontStockDetails();
                    addDtl.DocNo        = gg.Code;
                    addDtl.DocDtlNumber = j;
                    addDtl.Description  = "เบิกหน้าร้าน ใช้เอง";
                    addDtl.CreateDate   = DateTime.Now;
                    addDtl.CreateBy     = SingletonAuthen.Instance().Id;
                    addDtl.UpdateDate   = DateTime.Now;
                    addDtl.UpdateBy     = SingletonAuthen.Instance().Id;
                    addDtl.Enable       = true;
                    addDtl.ActionQty    = prodDtl.PackSize * item.Qty; // จำนวนหน่วย * pz
                    var stockHD = db.StoreFrontStock.FirstOrDefault(w => w.FKProduct == prodDtl.FKProduct && w.Enable == true);
                    addDtl.FKStoreFrontStock = stockHD.Id;
                    addDtl.FKTransactionType = MyConstant.PosTransaction.GGF;
                    addDtl.Barcode           = prodDtl.Code;
                    addDtl.Name             = prodDtl.Products.ThaiName;
                    addDtl.FKProductDetails = prodDtl.Id;
                    addDtl.ResultQty        = 0;

                    addDtl.PackSize          = prodDtl.PackSize;
                    addDtl.DocRefer          = "-";
                    addDtl.DocReferDtlNumber = 0;
                    addDtl.CostOnlyPerUnit   = prodDtl.CostOnly;
                    addDtl.SellPricePerUnit  = prodDtl.SellPrice;
                    db.StoreFrontStockDetails.Add(addDtl);
                    j++;
                }

                db.SaveChanges();
                // open paper
                frmMainReport report = new frmMainReport(this, gg.Code);
                report.Show();
                dataGridView1.Rows.Clear();
                dataGridView1.Refresh();

                dataGridView1.Rows.Add(1);
                count              = db.GetGoodsStoreFront.Where(w => w.CreateDate.Year == DateTime.Now.Year && w.CreateDate.Month == DateTime.Now.Month).Count() + 1;
                docCode            = MyConstant.PrefixForGenerateCode.GetGoodsForUse + Singleton.SingletonThisBudgetYear.Instance().ThisYear.CodeYear + DateTime.Now.ToString("MM") + Library.GenerateCodeFormCount(count, 4);
                textBoxTOCode.Text = docCode;
                textBoxTODate.Text = Library.ConvertDateToThaiDate(DateTime.Now);
            }
        }
        /// <summary>
        /// ยืนยัน จะตัด stock หน้าร้าน
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            var stackCode = GetAllCodeForAction();

            using (SSLsEntities db = new SSLsEntities())
            {
                foreach (var item in stackCode)
                {
                    var data = db.StoreFrontTransferOut.SingleOrDefault(w => w.Code == item);
                    if (data.Enable == false)
                    {
                        MessageBox.Show(item + " ถูกยกเลิกไปแล้ว");
                        return;
                    }
                    else if (data.ConfirmDate != null)
                    {
                        MessageBox.Show(item + " ยืนยันไปแล้ว");
                        return;
                    }
                }
                DialogResult dr = MessageBox.Show("ยืนยันทำรายการ ?",
                                                  "คำเตือนจากระบบ", MessageBoxButtons.YesNo);
                switch (dr)
                {
                case DialogResult.Yes:
                    foreach (var item in stackCode)
                    {
                        var data = db.StoreFrontTransferOut.SingleOrDefault(w => w.Code == item);
                        data.ConfirmDate     = DateTime.Now;
                        data.ConfirmBy       = Singleton.SingletonAuthen.Instance().Id;
                        db.Entry(data).State = EntityState.Modified;
                    }
                    db.SaveChanges();
                    // ตัดสต๊อก
                    // Initisl หน้าร้าน ก่อน เผื่อยังไม่มี
                    List <StoreFrontStock> stocks = new List <StoreFrontStock>();
                    foreach (var item in stackCode)
                    {
                        var        data  = db.StoreFrontTransferOut.SingleOrDefault(w => w.Code == item);
                        List <int> fkPro = data.StoreFrontTransferOutDtl.Select(w => w.ProductDetails.FKProduct).ToList().Distinct().ToList <int>();
                        foreach (var fk in fkPro)
                        {
                            var stock = db.StoreFrontStock.FirstOrDefault(w => w.Enable == true && w.FKProduct == fk);
                            if (stock == null)
                            {
                                stocks.Add(new StoreFrontStock()
                                {
                                    CreateDate  = DateTime.Now,
                                    CreateBy    = SingletonAuthen.Instance().Id,
                                    UpdateDate  = DateTime.Now,
                                    UpdateBy    = SingletonAuthen.Instance().Id,
                                    Enable      = true,
                                    CurrentQty  = 0,
                                    FKProduct   = fk,
                                    Description = "พบการโอนสาขา"
                                });
                            }
                        }
                        db.StoreFrontStock.AddRange(stocks);
                        db.SaveChanges();
                    }

                    // - หน้าร้าน
                    //List<StoreFrontStockDetails> addDtl = new List<StoreFrontStockDetails>();

                    foreach (var item in stackCode)
                    {
                        var data   = db.StoreFrontTransferOut.SingleOrDefault(w => w.Code == item);
                        int number = 1;
                        foreach (var getDtl in data.StoreFrontTransferOutDtl.Where(w => w.Enable == true).ToList())
                        {
                            var proDtl  = Singleton.SingletonProduct.Instance().ProductDetails.SingleOrDefault(w => w.Id == getDtl.FKProductDetails && w.Enable == true);
                            var stockHD = db.StoreFrontStock.FirstOrDefault(w => w.FKProduct == proDtl.FKProduct && w.Enable == true);
                            StoreFrontStockDetails addDtl = new StoreFrontStockDetails();
                            addDtl.DocNo             = data.Code;
                            addDtl.DocDtlNumber      = number;
                            addDtl.Description       = "โอนไปสาขาอื่น";
                            addDtl.CreateDate        = DateTime.Now;
                            addDtl.CreateBy          = SingletonAuthen.Instance().Id;
                            addDtl.UpdateDate        = DateTime.Now;
                            addDtl.UpdateBy          = SingletonAuthen.Instance().Id;
                            addDtl.Enable            = true;
                            addDtl.ActionQty         = getDtl.Qty * proDtl.PackSize;
                            addDtl.FKStoreFrontStock = stockHD.Id;
                            addDtl.FKTransactionType = MyConstant.PosTransaction.TransferStoreFrontToBranch;
                            addDtl.Barcode           = proDtl.Code;
                            addDtl.Name             = proDtl.Products.ThaiName;
                            addDtl.FKProductDetails = getDtl.FKProductDetails;
                            addDtl.ResultQty        = addDtl.ActionQty;
                            var lastAction = db.StoreFrontStockDetails.OrderByDescending(w => w.CreateDate).FirstOrDefault(w => w.FKProductDetails == getDtl.FKProductDetails && w.Enable == true);
                            if (lastAction != null)
                            {
                                addDtl.ResultQty = lastAction.ResultQty - addDtl.ActionQty;
                            }
                            addDtl.PackSize          = proDtl.PackSize;
                            addDtl.DocRefer          = "-";
                            addDtl.DocReferDtlNumber = 0;
                            addDtl.CostOnlyPerUnit   = proDtl.CostOnly;
                            addDtl.SellPricePerUnit  = proDtl.SellPrice;

                            stockHD.CurrentQty      = stockHD.CurrentQty - addDtl.ActionQty;
                            db.Entry(stockHD).State = EntityState.Modified;

                            db.StoreFrontStockDetails.Add(addDtl);
                            db.SaveChanges();
                            number++;
                        }
                    }

                    ReloadGrid();
                    break;

                case DialogResult.No:
                    break;
                }
            }

            //string code = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[2].Value.ToString();
            //using (SSLsEntities db = new SSLsEntities())
            //{
            //    var data = db.StoreFrontTransferOut.SingleOrDefault(w => w.Code == code);
            //    if (data.Enable == true && data.ConfirmDate == null)
            //    {
            //        DialogResult dr = MessageBox.Show("คุณต้องการบันทึกข้อมูล ใช่หรือไม่ ?", "คำเตือนจากระบบ", MessageBoxButtons.YesNo);
            //        switch (dr)
            //        {
            //            case DialogResult.Yes:
            //                /// ยืนยัน
            //                data.ConfirmDate = DateTime.Now;
            //                data.ConfirmBy = Singleton.SingletonAuthen.Instance().Id;
            //                data.UpdateDate = DateTime.Now;
            //                data.UpdateBy = Singleton.SingletonAuthen.Instance().Id;
            //                db.Entry(data).State = EntityState.Modified;
            //                db.SaveChanges();
            //                /// Update Stock Version เก่า
            //                //Library.MakeValueForUpdateStockPos(data.StoreFrontTransferOutDtl.Where(w => w.Enable == true).ToList());

            //                ///// StoreFrontStock
            //                foreach (var item in data.StoreFrontTransferOutDtl.Where(w => w.Enable == true).ToList())
            //                {
            //                    // add to StoreFrontStockDetails
            //                    var prodtl = Singleton.SingletonProduct.Instance().ProductDetails
            //                        .SingleOrDefault(w => w.Enable == true && w.Id == item.FKProductDetails);
            //                    // check in header
            //                    var storeFrontStock = db.StoreFrontStock.FirstOrDefault(w => w.Enable == true && w.FKProduct == prodtl.FKProduct);

            //                }
            //                ReloadGrid();
            //                break;
            //            case DialogResult.No:
            //                break;
            //        }
            //    }
            //    else
            //    {
            //        MessageBox.Show("ไม่สามารถยกเลิกได้");
            //    }
            //}
        }