/// <summary> /// การบันทึก disable อันเดิมทิ้ง /// </summary> private void SaveCommit() { using (SSLsEntities db = new SSLsEntities()) { var data = db.StoreFrontTransferOut.SingleOrDefault(w => w.Code == code); foreach (var item in data.StoreFrontTransferOutDtl.Where(w => w.Enable == true).ToList()) { item.Enable = false; item.UpdateDate = DateTime.Now; item.UpdateBy = Singleton.SingletonAuthen.Instance().Id; db.Entry(item).State = EntityState.Modified; } /// Update data.UpdateDate = DateTime.Now; data.UpdateBy = Singleton.SingletonAuthen.Instance().Id; List <StoreFrontTransferOutDtl> details = new List <StoreFrontTransferOutDtl>(); StoreFrontTransferOutDtl detail; decimal totalPiece = 0; decimal totalUnit = 0; for (int i = 0; i < dataGridView1.Rows.Count; i++) { detail = new StoreFrontTransferOutDtl(); detail.FKTransferOut = data.Code; 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()); 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[colAllPrice].Value.ToString()); details.Add(detail); totalPiece += (detail.Qty * prodDtl.PackSize); totalUnit += detail.Qty; if (i >= dataGridView1.Rows.Count - 2) { break; } } db.StoreFrontTransferOutDtl.AddRange(details); data.TotalQty = totalPiece; data.TotalQtyUnit = totalUnit; data.TotalUnVat = decimal.Parse(textBoxTotalUnVat.Text); data.TotalBeforeVat = decimal.Parse(textBoxTotalBeforeVat.Text); data.TotalVat = decimal.Parse(textBoxTotalVat.Text); data.FKEmployee = _EmployeeId; data.TotalBalance = decimal.Parse(textBoxTotalBalance.Text); db.Entry(data).State = EntityState.Modified; db.SaveChanges(); this.transferOutListForm.ReloadGrid(); this.Dispose(); } }
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); } } }