private void SaveCommit()
        {
            try
            {
                WarehouseToWaste wt = new WarehouseToWaste();
                wt.Enable      = true;
                wt.Description = textBoxRemark.Text;
                wt.CreateDate  = DateTime.Now;
                wt.CreateBy    = Singleton.SingletonAuthen.Instance().Id;
                wt.UpdateDate  = DateTime.Now;
                wt.UpdateBy    = Singleton.SingletonAuthen.Instance().Id;
                wt.DocNo       = textBoxCode.Text;
                wt.FKWarehouse = _WarehouseId;
                if (_WasteReason == 0)
                {
                    MessageBox.Show("กรุณาเลือกเหตุผล");
                    return;
                }
                wt.FKWasteReason = _WasteReason;
                List <WarehouseToWasteDetails> details = new List <WarehouseToWasteDetails>();
                WarehouseToWasteDetails        d;
                using (WH_TRATEntities whEN = new WH_TRATEntities())
                {
                    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                    {
                        var code = dataGridView1.Rows[i].Cells[colCode].Value;
                        if (code == null)
                        {
                            continue;
                        }
                        d                  = new WarehouseToWasteDetails();
                        d.Enable           = true;
                        d.Description      = textBoxRemark.Text;
                        d.CreateDate       = DateTime.Now;
                        d.CreateBy         = Singleton.SingletonAuthen.Instance().Id;
                        d.UpdateDate       = DateTime.Now;
                        d.UpdateBy         = Singleton.SingletonAuthen.Instance().Id;
                        d.FKProductDetails = int.Parse(dataGridView1.Rows[i].Cells[colId].Value.ToString());
                        d.QtyUnit          = decimal.Parse(dataGridView1.Rows[i].Cells[colQty].Value.ToString());
                        d.Packsize         = decimal.Parse(dataGridView1.Rows[i].Cells[colPZ].Value.ToString());
                        d.PricePerUnit     = decimal.Parse(dataGridView1.Rows[i].Cells[colCostPerUnit].Value.ToString());

                        decimal qty  = decimal.Parse(dataGridView1.Rows[i].Cells[colQty].Value.ToString());
                        decimal cost = decimal.Parse(dataGridView1.Rows[i].Cells[colCostPerUnit].Value.ToString());
                        decimal sum  = qty * cost;
                        Console.WriteLine(sum);

                        int       locId  = int.Parse(dataGridView1.Rows[i].Cells[colLocation + 1].Value.ToString());
                        WH_LOCSTK locStk = whEN.WH_LOCSTK.FirstOrDefault(w => w.ROWID == locId);

                        d.Location = locStk.LOCATION_NO;
                        details.Add(d);
                        locStk.UPDATE_DATE = DateTime.Now;
                        locStk.QTY         = locStk.QTY - ((float)qty * (float)locStk.PACK_SIZE);
                        if (locStk.QTY <= 0)
                        {
                            // ลบ row ทิ้ง
                            whEN.WH_LOCSTK.Remove(locStk);
                            whEN.SaveChanges();

                            // check WH_LOCSTK เพื่อ set book flag master
                            var locationList = whEN.WH_LOCSTK.Where(w => w.LOCATION_NO == locStk.LOCATION_NO && (w.QTY > 0 || w.BOOK_QTY > 0)).ToList();
                            if (locationList.Count() > 0) // ถ้าบ้านนี้ ยัง
                            {
                                // not set
                            }
                            else
                            {
                                // set book flag
                                // ต้องไปอัพเดท location mast ให้ book flag = null

                                //var locationMast = whEN.WH_LOCATION_MAST.SingleOrDefault(w => w.LOCATION_NO == locStk.LOCATION_NO);
                                //locationMast.BOOK_FLAG = null;
                                //whEN.Entry(locationMast).State = EntityState.Modified;
                                //whEN.SaveChanges();
                                whEN.Database.ExecuteSqlCommand("UPDATE dbo.WH_LOCATION_MAST SET BOOK_FLAG = NULL WHERE LOCATION_NO = '" + locStk.LOCATION_NO + "'");
                            }
                        }
                        else
                        {
                            whEN.Entry(locStk).State = EntityState.Modified;
                            whEN.SaveChanges();
                        }
                    }
                }
                wt.WarehouseToWasteDetails = details;
                using (SSLsEntities db = new SSLsEntities())
                {
                    db.WarehouseToWaste.Add(wt);
                    db.SaveChanges();
                    //reset form
                    dataGridView1.Rows.Clear();
                    dataGridView1.Refresh();

                    int    count = db.WarehouseToWaste.Where(w => w.CreateDate.Year == DateTime.Now.Year && w.CreateDate.Month == DateTime.Now.Month).Count() + 1;
                    string code  = MyConstant.PrefixForGenerateCode.WHToWaste + Singleton.SingletonThisBudgetYear.Instance().ThisYear.CodeYear + DateTime.Now.ToString("MM") + Library.GenerateCodeFormCount(count, 4);
                    textBoxCode.Text = code;
                    textBoxDate.Text = Library.ConvertDateToThaiDate(DateTime.Now);

                    textBoxTotalBalance.Text = "0.00";
                    textBoxTotalQty.Text     = "0.00";
                    // Update Stock Wms
                    //Library.MakeValueForUpdateStockWms(details);
                    // + ห้องของเสีย
                    // 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();
                    Library.AddWasteWarehouse(details, wt.DocNo);
                    /// ต้องไปตัด stock ระบบจารกอต โดยใช้ บาร์โค้ด

                    dataGridView1.Rows.Add(1);
                    textBoxRemark.Text = "";
                    textBoxRemark.Select();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("พบข้อผิดพลาด " + ex.ToString());
            }
        }
Beispiel #2
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();
            }
        }