private bool SaveData() { try { decimal receiveWgh = lblWeight.Text.ToDecimal(); string createBy = Helper.GetLocalIPAddress(); barcode_no = Helper.GetMaxBarcode(); using (var db = new SlaughterhouseEntities()) { using (DbContextTransaction transaction = db.Database.BeginTransaction()) { try { //insert barcode var barcode = new barcode { barcode_no = barcode_no, product_code = productIssued.product_code, production_date = productionDate, lot_no = productIssued.lot_no, qty = 1, wgh = receiveWgh, active = true, create_by = createBy, qrcode_image = Helper.GenerateQRCode(barcode_no.ToString()), //location_code = locationCode }; db.barcodes.Add(barcode); //insert basic_yield_info var basicYield = new basic_yield_info { production_date = productionDate, product_code = barcode.product_code, lot_no = barcode.lot_no, quantity = barcode.qty, weight = barcode.wgh, ref_product_code = PRODUCT_CODE, ref_lot_no = barcode.lot_no.Substring(0, barcode.lot_no.Length - 4), barcode_no = barcode_no, create_by = createBy }; db.basic_yield_info.Add(basicYield); db.SaveChanges(); transaction.Commit(); return(true); } catch (Exception) { transaction.Rollback(); throw; } } } } catch (Exception) { throw; } }
private bool SaveData() { try { decimal receiveWgh = lblWeight.Text.ToDecimal(); string createBy = Helper.GetLocalIPAddress(); //string stockNo = ""; //int stockItem = 0; barcode_no = Helper.GetMaxBarcode(); using (var db = new SlaughterhouseEntities()) { //var barcodeGenerate = db.document_generate.Find(Constants.BAR); //barcodeGenerate.running += 1; //barcodeNo = barcodeGenerate.running; //db.Entry(barcodeGenerate).State = System.Data.Entity.EntityState.Modified; //db.SaveChanges(); //var stocks = db.stocks.Where(p => p.stock_date == productionDate // && p.product_code == product_code // && p.lot_no == lot_no // && p.transaction_type == 1).ToList(); //if (stocks.Count == 0) //{ // var stockGenerate = db.document_generate.Find(Constants.STK); // stockNo = Constants.STK + stockGenerate.running.ToString().PadLeft(10 - Constants.STK.Length, '0'); // stockItem = 1; // stockGenerate.running += 1; // db.Entry(stockGenerate).State = System.Data.Entity.EntityState.Modified; // db.SaveChanges(); //} //else //{ // stockNo = stocks[0].stock_no; // stockItem = stocks.Count + 1; //} using (DbContextTransaction transaction = db.Database.BeginTransaction()) { try { //Get Barcode //var maxValue = db.barcodes.Max(x => (long?)x.barcode_no) ?? 0; //if (maxValue == 0) //{ // barcode_no = string.Format("{0}0000000001", plantID).ToLong(); //} //else //{ // barcode_no = maxValue + 1; //} //var barString = barcodeNo.ToString().PadLeft(12, '0'); //barcode_no = string.Format("{0}{1}", plantID, barString).ToLong(); //insert barcode var barcode = new barcode { barcode_no = barcode_no, product_code = product_code, production_date = productionDate, lot_no = lot_no, qty = 1, wgh = receiveWgh, active = true, create_by = createBy, qrcode_image = Helper.GenerateQRCode(barcode_no.ToString()), //location_code = locationCode }; db.barcodes.Add(barcode); //insert basic_yield_info var basicYield = new basic_yield_info { production_date = productionDate, product_code = barcode.product_code, lot_no = barcode.lot_no, quantity = barcode.qty, weight = barcode.wgh, ref_product_code = PRODUCT_CODE, ref_lot_no = barcode.lot_no.Substring(0, barcode.lot_no.Length - 4), barcode_no = barcode_no, create_by = createBy }; db.basic_yield_info.Add(basicYield); //insert receive_item //db.receive_item.Add(item); //insert stock //var stock = new stock //{ // stock_date = productionDate, // stock_no = stockNo, // stock_item = stockItem, // product_code = barcode.product_code, // stock_qty = barcode.qty, // stock_wgh = barcode.wgh, // //ref_document_no = receive.receive_no, // //ref_document_type = Constants.REV, // lot_no = barcode.lot_no, // location_code = locationCode, // barcode_no = barcode_no, // transaction_type = 1, // create_by = createBy //}; //db.stocks.Add(stock); db.SaveChanges(); transaction.Commit(); return(true); } catch (Exception) { transaction.Rollback(); throw; } } } } catch (Exception) { throw; } }