public static stock_item_running GetStockNo(string documentNo) { try { using (var db = new SlaughterhouseEntities()) { var documentGenerate = db.document_generate.Find(Constants.STK); var stockItemRunning = db.stock_item_running.Where(p => p.doc_no.Equals(documentNo)).SingleOrDefault(); if (stockItemRunning == null) { //get new stock doc no var stockNo = Constants.STK + documentGenerate.running.ToString().PadLeft(10 - Constants.STK.Length, '0'); //insert stock_item_running var newStockItem = new stock_item_running { doc_no = documentNo, stock_no = stockNo, stock_item = 1, create_by = Helper.GetLocalIPAddress() }; db.stock_item_running.Add(newStockItem); documentGenerate.running += 1; db.Entry(documentGenerate).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); return(newStockItem); } else { //documentGenerate.running += 1; //db.Entry(documentGenerate).State = System.Data.Entity.EntityState.Modified; //db.SaveChanges(); stockItemRunning.stock_item += 1; db.Entry(stockItemRunning).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); return(stockItemRunning); } } } catch (Exception) { throw; } }
private void SaveData() { try { var receiveWgh = lblWeight.Text.ToDecimal(); string createBy = Helper.GetLocalIPAddress(); var productionDate = plant.production_date; using (var db = new SlaughterhouseEntities()) { var receive = db.receives.Where(p => p.receive_no.Equals(lblReceiveNo.Text)).SingleOrDefault(); //var carcassQty = receive.carcass_qty == 0 ? receive.carcass_qty : (receive.carcass_qty / RECEIVE_QTY); if (receive.farm_qty - receive.carcass_qty == 0) { throw new Exception("จำนวนรับครบแล้ว\rไม่สามารถรับเพิ่มได้!"); } int seq = receive.receive_item.Where(p => p.product_code.Equals(product.product_code)).Count(); ////หารับหมูเป็นมาตัดออกจาก stock //var swine = receive.receive_item.Where(p => p.product_code == "P001" // && p.seq == seq).Select(p => new { p.receive_qty, p.receive_wgh }).SingleOrDefault(); seq += 1; //string stock_no = db.stock_item_running.Where(p => p.doc_no.Equals(receive.receive_no)).Select(p => p.stock_no).SingleOrDefault(); string stock_no = ""; var stockDocument = (from p in db.document_generate where p.document_type == Constants.STK select p).SingleOrDefault(); //check stock_item_running var stockItemRunning = db.stock_item_running.Where(p => p.doc_no.Equals(receive.receive_no)).SingleOrDefault(); if (stockItemRunning == null) { //get new stock doc no stock_no = Constants.STK + stockDocument.running.ToString().PadLeft(10 - Constants.STK.Length, '0'); } else { stock_no = stockItemRunning.stock_no; } using (DbContextTransaction transaction = db.Database.BeginTransaction()) { try { var item = new receive_item { receive_no = receive.receive_no, product_code = product.product_code, seq = seq, lot_no = receive.lot_no, sex_flag = sexFlag, receive_qty = 1, receive_wgh = receiveWgh, chill_qty = 0, chill_wgh = 0, create_by = createBy }; db.receive_item.Add(item); //var stocks = new List<stock>(); //stocks.Add(new stock //{ // stock_date = productionDate, // stock_no = stock_no, // stock_item = item.seq, // product_code = item.product_code, // stock_qty = item.receive_qty, // stock_wgh = item.receive_wgh, // ref_document_no = receive.receive_no, // ref_document_type = Constants.REV, // lot_no = receive.lot_no, // location_code = 1, // barcode_no = 0, // transaction_type = 2, // create_by = item.create_by //}); //insert stock var stock = new stock { stock_date = productionDate, stock_no = stock_no, stock_item = item.seq, product_code = item.product_code, stock_qty = 1, stock_wgh = receiveWgh, ref_document_no = receive.receive_no, ref_document_type = Constants.REV, lot_no = receive.lot_no, location_code = 2, //ห้องเย็นเก็บหมุซีก barcode_no = 0, transaction_type = 1, create_by = createBy }; db.stocks.Add(stock); receive.carcass_qty += 1; receive.carcass_wgh += receiveWgh; if (stockItemRunning == null) { //insert stock_item_running var newStockItem = new stock_item_running { doc_no = receive.receive_no, stock_no = stock_no, stock_item = 1, create_by = createBy }; db.stock_item_running.Add(newStockItem); //update document_generate stockDocument.running += 1; db.Entry(stockDocument).State = System.Data.Entity.EntityState.Modified; } else { //update stock_item_running stockItemRunning.stock_item += 1; db.Entry(stockItemRunning).State = System.Data.Entity.EntityState.Modified; } db.SaveChanges(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } } } catch (Exception) { throw; } }
//private void BtnControl(bool start, bool ok, bool close) //{ // btnStart.Enabled = start; // BtnOK.Enabled = ok; // btnStop.Enabled = close; //} private bool SaveData() { try { var weight = lblWeight.Text.ToDecimal(); using (var db = new SlaughterhouseEntities()) { //update receive var receive = db.receives.Where(p => p.receive_no.Equals(lblReceiveNo.Text)).SingleOrDefault(); if (receive.farm_qty - receive.factory_qty == 0) { throw new Exception("จำนวนรับครบแล้ว ไม่สามารถรับเพิ่มได้!"); } int seq = receive.receive_item.Count(); //int seq = db.receive_item.Where(p => p.receive_no == receive.receive_no).Count(); seq += 1; var item = new receive_item { receive_no = receive.receive_no, product_code = productCode, seq = seq, lot_no = receive.lot_no, sex_flag = sexFlag, receive_qty = 1, receive_wgh = weight, chill_qty = 0, chill_wgh = 0, create_by = Constants.CREATE_BY }; string stock_no = ""; var documentGenerate = (from p in db.document_generate where p.document_type == Constants.STK select p).SingleOrDefault(); //check stock_item_running var stockItemRunning = db.stock_item_running.Where(p => p.doc_no.Equals(receive.receive_no)).SingleOrDefault(); if (stockItemRunning == null) { //get new stock doc no stock_no = Constants.STK + documentGenerate.running.ToString().PadLeft(10 - Constants.STK.Length, '0'); } else { stock_no = stockItemRunning.stock_no; } using (DbContextTransaction transaction = db.Database.BeginTransaction()) { try { receive.factory_qty += item.receive_qty; receive.factory_wgh += item.receive_wgh; db.Entry(receive).State = System.Data.Entity.EntityState.Modified; db.receive_item.Add(item); //insert stock var stock = new stock { stock_date = receive.receive_date, stock_no = stock_no, stock_item = item.seq, product_code = item.product_code, stock_qty = item.receive_qty, stock_wgh = item.receive_wgh, ref_document_no = receive.receive_no, ref_document_type = Constants.REV, lot_no = receive.lot_no, location_code = 1, barcode_no = 0, transaction_type = 1, create_by = item.create_by }; if (stockItemRunning == null) { //insert stock_item_running var newStockItem = new stock_item_running { doc_no = receive.receive_no, stock_no = stock_no, stock_item = 1, create_by = item.create_by }; db.stock_item_running.Add(newStockItem); //update document_generate documentGenerate.running += 1; db.Entry(documentGenerate).State = System.Data.Entity.EntityState.Modified; } else { //update stock_item_running stockItemRunning.stock_item += 1; db.Entry(stockItemRunning).State = System.Data.Entity.EntityState.Modified; } db.stocks.Add(stock); db.SaveChanges(); transaction.Commit(); lblMessage.Text = Constants.SAVE_SUCCESS; } catch (Exception) { transaction.Rollback(); throw; } } } //ReceiveItem receiveItem = new ReceiveItem //{ // ReceiveNo = receive.ReceiveNo, // ProductCode = this.productCode, // SexFlag = sexFlag, // ReceiveQty = 1, // ReceiveWgh = lblWeight.Text.ToDecimal(), // CreateBy = "Auto" //}; //return ReceiveController.InsertItem(receiveItem); return true; } catch (Exception) { throw; } }
private bool SaveData() { try { var orderNo = lblOrderNo.Text; var scaleWeight = lblWeight.Text.ToDecimal(); var refDocumentNo = ""; var refDocumentType = ""; var transportNo = ""; var truckId = cboTruckNo.SelectedValue.ToString().ToInt16(); string createBy = Helper.GetLocalIPAddress(); using (var db = new SlaughterhouseEntities()) { var receiveItem = db.receive_item.Where(p => p.product_code == product.product_code && p.lot_no == lotNo && p.chill_qty == 0) .OrderBy(p => p.seq).Take(1).SingleOrDefault(); if (receiveItem == null) { LoadLotNo(); throw new Exception("ไม่พบ Lot No\rกรุณาเลือก Lot No ใหม่!"); } using (DbContextTransaction transaction = db.Database.BeginTransaction()) { try { int seq = 0; string stockNo = ""; int bomCode = 0; if (!string.IsNullOrEmpty(orderNo)) { refDocumentNo = orderNo; refDocumentType = Constants.SO; //update order var orderItem = db.orders_item.Where(p => p.order_no == orderNo && p.product_code == product.product_code).SingleOrDefault(); if (orderItem.order_qty - orderItem.unload_qty == 0) { throw new Exception("ไม่สามารถจ่ายได้!\rจำนวนจ่ายครบแล้ว"); } bomCode = orderItem.bom_code; //update production order orderItem.unload_qty += 1; orderItem.unload_wgh += scaleWeight; orderItem.modified_at = DateTime.Now; orderItem.modified_by = createBy; db.Entry(orderItem).State = EntityState.Modified; var transport = db.transports.Where(p => p.ref_document_no == orderNo).SingleOrDefault(); var transportGenrate = db.document_generate.Find(Constants.TP); if (transport == null) { transportNo = Constants.TP + transportGenrate.running.ToString().PadLeft(10 - Constants.TP.Length, '0'); //update transport running transportGenrate.running += 1; db.Entry(transportGenrate).State = EntityState.Modified; } else { transportNo = transport.transport_no; } } else { var issDoc = db.document_generate.Find(Constants.ISS);// (from p in db.document_generate where p.document_type == Constants.ISS select p).SingleOrDefault(); refDocumentNo = Constants.ISS + issDoc.running.ToString().PadLeft(10 - Constants.ISS.Length, '0'); refDocumentType = Constants.ISS; } var stockGenerate = db.document_generate.Find(Constants.STK); //check stock_item_running var stockItemRunning = db.stock_item_running.Where(p => p.doc_no.Equals(refDocumentNo)).SingleOrDefault(); if (stockItemRunning == null) { //get new stock doc no stockNo = Constants.STK + stockGenerate.running.ToString().PadLeft(10 - Constants.STK.Length, '0'); seq = 1; } else { stockNo = stockItemRunning.stock_no; seq = stockItemRunning.stock_item + 1; } if (!string.IsNullOrEmpty(orderNo)) { //update receive item receiveItem.transfer_qty = 1; receiveItem.transfer_wgh = scaleWeight; var transport = db.transports.Find(transportNo); if (transport == null) { var trans = new transport { transport_no = transportNo, transport_date = DateTime.Today, ref_document_no = orderNo, truck_id = truckId, transport_flag = 0, create_at = DateTime.Now, create_by = createBy }; db.transports.Add(trans); } else { transport.truck_id = truckId; db.Entry(transport).State = EntityState.Modified; } //insert transport item var transItem = new transport_item { transport_no = transportNo, product_code = product.product_code, seq = seq, transport_qty = 1, transport_wgh = scaleWeight, stock_no = stockNo, lot_no = lotNo, truck_id = truckId, barcode_no = 0, bom_code = bomCode, create_at = DateTime.Now, create_by = createBy }; db.transport_item.Add(transItem); } else { //var issued = db.issued_info.Where(p => p.issued_date == productionDate // && p.product_code == product.product_code // && p.lot_no == lotNo).SingleOrDefault(); var issuedInfo = db.issued_info.Find(productionDate, product.product_code, lotNo); if (issuedInfo == null) { var newIssued = new issued_info { issued_date = productionDate, product_code = product.product_code, lot_no = lotNo, issued_qty = 1, issued_wgh = scaleWeight, usage_qty = 0, usage_wgh = 0, active = true, create_at = DateTime.Now, create_by = createBy }; db.issued_info.Add(newIssued); } else { issuedInfo.issued_qty += 1; issuedInfo.issued_wgh += scaleWeight; issuedInfo.modified_at = DateTime.Now; issuedInfo.modified_by = createBy; db.Entry(issuedInfo).State = EntityState.Modified; } } //update receive item receiveItem.chill_qty = 1; receiveItem.chill_wgh = scaleWeight; receiveItem.modified_at = DateTime.Now; receiveItem.modified_by = createBy; db.Entry(receiveItem).State = EntityState.Modified; //insert stock var stock = new stock { stock_date = productionDate, stock_no = stockNo, stock_item = seq, product_code = product.product_code, stock_qty = 1, stock_wgh = scaleWeight, ref_document_no = refDocumentNo, ref_document_type = refDocumentType, lot_no = lotNo, location_code = 2, //ห้องเย็นเก็บหมุซีก barcode_no = 0, transaction_type = 2, create_by = createBy }; db.stocks.Add(stock); //รับหมูซีก เครื่องใน ไม่ต้อง update stock_item_running เพราะเริ่มตาม receive_item.seq if (stockItemRunning == null) { //insert stock_item_running var newStockItem = new stock_item_running { doc_no = refDocumentNo, stock_no = stockNo, stock_item = 1, create_by = createBy }; db.stock_item_running.Add(newStockItem); //update document_generate stockGenerate.running += 1; db.Entry(stockGenerate).State = EntityState.Modified; } else { //update stock_item_running stockItemRunning.stock_item += 1; db.Entry(stockItemRunning).State = EntityState.Modified; } db.SaveChanges(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } } return(true); } catch (Exception) { throw; } }