private void CreateStockTakeHHTDetails(Guid hhtHeaderId, string txNumber, string hhtId, DateTime uploadedOn, List <ImportDetailsInfo> detailList, string hhtTxNumber) { int iCount = 0; using (var ctx = new EF6.RT2020Entities()) { using (var scope = ctx.Database.BeginTransaction()) { try { foreach (ImportDetailsInfo detail in detailList) { Guid productId = ProductBarcodeEx.GetProductIdByBarcode(detail.Barcode); if (!string.IsNullOrEmpty(detail.Barcode.Trim()) && productId != System.Guid.Empty) { #region update dbo.StockTakeDetails_HHT //string sql = "HeaderId = '" + hhtHeaderId.ToString() + "' AND TxNumber = '" + txNumber + "' AND ProductId = '" + productId.ToString() + "'"; var hhtDetail = ctx.StockTakeDetails_HHT .Where(x => x.HeaderId == hhtHeaderId && x.TxNumber == txNumber && x.ProductId == productId) .FirstOrDefault(); if (hhtDetail == null) { hhtDetail = new EF6.StockTakeDetails_HHT(); hhtDetail.DetailsId = Guid.NewGuid(); hhtDetail.HeaderId = hhtHeaderId; hhtDetail.TxNumber = txNumber; hhtDetail.LineNumber = iCount++; hhtDetail.UploadedOn = uploadedOn; hhtDetail.ProductId = productId; hhtDetail.Barcode = detail.Barcode; ctx.StockTakeDetails_HHT.Add(hhtDetail); } hhtDetail.Qty = detail.Qty; hhtDetail.Remarks = hhtTxNumber; ctx.SaveChanges(); #endregion } } scope.Commit(); } catch (Exception ex) { scope.Rollback(); } } } }
private void CreateStockTakeHHTDetails(Guid headerId, DateTime uploadOn, bool overwrite) { int totalLine = PacketDataList.Length, missingLine = 0; decimal totalQty = 0, missingQty = 0; using (var ctx = new EF6.RT2020Entities()) { using (var scope = ctx.Database.BeginTransaction()) { try { for (int i = 0; i < PacketDataList.Length; i++) { EF6.StockTakeDetails_HHT hhtDetail; ImportPOSData posData = PacketDataList[i]; totalQty += posData.Qty; // Calc missing line & qty Guid productId = GetProductId(posData.STKCODE, posData.APPENDIX1, posData.APPENDIX2, posData.APPENDIX3); if (productId == System.Guid.Empty) { missingLine++; missingQty += posData.Qty; } else { var pb = ctx.ProductBarcode.Where(x => x.ProductId == productId).FirstOrDefault(); if (pb == null) { missingLine++; missingQty += posData.Qty; } string hhtId = posData.HHT.Trim().Length == 0 ? "POS_ADV1" : posData.HHT.Trim(); if (overwrite) { //string sql = "TxNumber = '" + posData.ExportNum + "' AND HHTId ='" + hhtId + "'"; hhtDetail = ctx.StockTakeDetails_HHT .Where(x => x.TxNumber == posData.ExportNum && x.HHTId == hhtId) .FirstOrDefault(); } else { hhtDetail = new EF6.StockTakeDetails_HHT(); hhtDetail.DetailsId = Guid.NewGuid(); ctx.StockTakeDetails_HHT.Add(hhtDetail); } hhtDetail.TxNumber = posData.ExportNum; hhtDetail.HHTId = hhtId; hhtDetail.UploadedOn = uploadOn; hhtDetail.Barcode = posData.Barcode; hhtDetail.Qty = posData.Qty; hhtDetail.LineNumber = posData.SeqNum; hhtDetail.ProductId = productId; hhtDetail.Remarks = posData.Shelf; ctx.SaveChanges(); } } scope.Commit(); } catch (Exception ex) { scope.Rollback(); } } } UpdateHHtHeader(headerId, totalLine, missingLine, totalQty, missingQty); }