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 CreatedStockTakeDetail(Guid stktkHeaderId, string txNumber, List <ImportDetailsInfo> detailList, Guid workplaceId, DateTime uploadedOn) { 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 != Guid.Empty) { //string sql = "HeaderId = '" + stktkHeaderId.ToString() + "' AND TxNumber = '" + txNumber + "' AND ProductId = '" + productId.ToString() + "' AND WorkplaceId = '" + workplaceId.ToString() + "'"; var item = ctx.StockTakeDetails.Where(x => x.HeaderId == stktkHeaderId && x.TxNumber == txNumber && x.ProductId == productId && x.WorkplaceId == workplaceId).FirstOrDefault(); if (item == null) { item = new EF6.StockTakeDetails(); item.DetailsId = Guid.NewGuid(); item.HeaderId = stktkHeaderId; item.TxNumber = txNumber; item.ProductId = productId; item.WorkplaceId = workplaceId; item.ModifiedOn = uploadedOn; item.ModifiedBy = ConfigHelper.CurrentUserId; ctx.StockTakeDetails.Add(item); } ctx.SaveChanges(); } } scope.Commit(); } catch (Exception ex) { scope.Rollback(); } } } }
private void Save() { int iCount = 1; bool isValid = true; foreach (ListViewItem lvItem in lvPPCPacketList.Items) { if (lvItem.Checked) { string stktkNumber = string.Empty; Guid workplaceId = Guid.Empty; DateTime uploadedOn = DateTime.Now; List <ImportDetailsInfo> detailAllList = new List <ImportDetailsInfo>(); decimal totalLine = 0, totalQty = 0, missingLine = 0, missingQty = 0; this.CheckHHTLog(lvItem.SubItems[4].Text); #region Load Header's detail info FileHelperEngine <ImportHeaderInfo> headerInfoEngine = new FileHelperEngine <ImportHeaderInfo>(); headerInfoEngine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue; ImportHeaderInfo[] headerInfoList = headerInfoEngine.ReadFile(lvItem.SubItems[8].Text); if (headerInfoEngine.ErrorManager.ErrorCount > 0) { headerInfoEngine.ErrorManager.SaveErrors(logFile); } #endregion Utility.WriteLog("Date Create : "+ uploadedOn.ToString("dd/MM/yyyy HH:mm:ss"), logFile); Utility.WriteLog("Session ID : "+ uploadedOn.ToString("yyyyMMdd-HHmmss") + "-" + iCount.ToString().PadLeft(3, '0'), logFile); Utility.WriteLog("Upload Time : "+ uploadedOn.ToString("dd/MM/yyyy HH:mm:ss"), logFile); Utility.WriteLog("HHT TRN# : "+ lvItem.SubItems[2].Text, logFile); Utility.WriteLog("Location# : " + lvItem.SubItems[9].Text + " [Original]; " + lvItem.Text + " [Current]", logFile); Utility.WriteLog("Stock Take# : "+ lvItem.SubItems[10].Text + " [Suggested]; " + lvItem.SubItems[1].Text + " [Current]", logFile); Utility.WriteLog("Process Detail : Import Data", logFile); Utility.WriteLog("Message :- ", logFile); Utility.WriteLog("=> Checking Loc# ", logFile); #region Check Workplace (Loc#) using (var ctx = new EF6.RT2020Entities()) { var wp = ctx.Workplace.Where(x => x.WorkplaceCode == lvItem.Text).AsNoTracking().FirstOrDefault(); if (wp != null) { if (wp.Retired) { Utility.WriteLog(" [ERROR] Loc# was retired ", logFile); isValid = isValid & false; } else { Utility.WriteLog(" [OK] ", logFile); workplaceId = wp.WorkplaceId; } } else { Utility.WriteLog(" [ERROR] Loc# Not Found", logFile); isValid = isValid & false; } } #endregion Utility.WriteLog(" RESULT : COMPLETED", logFile); Utility.WriteLog("=> Import Packet File ", logFile); #region Load details files string[] packetFiles = Directory.GetFiles(tempDirectory, "DT_" + lvItem.SubItems[2].Text + "*", SearchOption.TopDirectoryOnly); for (int i = 0; i < packetFiles.Length; i++) { Utility.WriteLog(@" " + (i + 1).ToString() + @") Packet => " + Path.GetFileNameWithoutExtension(packetFiles[i]) + " [" + packetFiles[i] + "] ", logFile); } #endregion Utility.WriteLog(" RESULT : COMPLETED", logFile); Utility.WriteLog("=> Checking (Header) ", logFile); stktkNumber = lvItem.SubItems[1].Text.Trim(); #region checking Header info using (var ctx = new EF6.RT2020Entities()) { var stktkHeader = ctx.StockTakeHeader.Where(x => x.TxNumber == lvItem.SubItems[1].Text.Trim()).FirstOrDefault(); if (stktkHeader != null) { if (!string.IsNullOrEmpty(stktkHeader.ADJNUM)) { Utility.WriteLog(" [ERROR] The Stock Take Number was posted, cannot be used anymore. ", logFile); isValid = isValid & false; } else if (!WorkplaceEx.GetWorkplaceCodeById(stktkHeader.WorkplaceId.Value).Equals(lvItem.Text.Trim())) { Utility.WriteLog(" [ERROR] The loc# in Stock Take Header must be as same as the selected one. ", logFile); isValid = isValid & false; } else { //? Why compare the UploadedOn down to "seconds" //string sql = "TxNumber = '" + lvItem.SubItems[1].Text.Trim() + "' AND HHTId = '" + lvItem.SubItems[4].Text + "' AND CONVERT(NVARCHAR(20), UploadedOn, 120) = '" + uploadedOn.ToString("yyyy-MM-dd HH:mm:ss") + "'"; var txNumber = lvItem.SubItems[1].Text.Trim(); var hhtId = lvItem.SubItems[4].Text; var hhtHeader = ctx.StocktakeHeader_HHT.Where(x => x.TxNumber == txNumber && x.HHTId == hhtId && x.UploadedOn.Value.ToString("yyyy-MM-dd HH:mm:ss") == uploadedOn.ToString("yyyy-MM-dd HH:mm:ss")).FirstOrDefault(); if (hhtHeader != null) { if (hhtHeader.PostedOn.Value.Year > 1900) { Utility.WriteLog(" [ERROR] The Stock Take (HHT) Number was posted, cannot be used anymore. ", logFile); isValid = isValid & false; } else { Utility.WriteLog(" [ERROR] The Stock Take (HHT) Number existed. ", logFile); isValid = isValid & false; } } else { Utility.WriteLog(" [OK] ", logFile); } } } else { Utility.WriteLog(" [OK] ", logFile); } } #endregion Utility.WriteLog("=> Checking (Detail) ", logFile); int iCountBarcode = 0; #region checking details info for (int iHeader = 0; iHeader < headerInfoList.Length; iHeader++) { ImportHeaderInfo headerInfo = headerInfoList[iHeader]; FileHelperEngine <ImportDetailsInfo> detailInfoEngine = new FileHelperEngine <ImportDetailsInfo>(); detailInfoEngine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue; string detailPacket = Path.Combine(tempDirectory, "DT_" + lvItem.SubItems[2].Text + "_" + headerInfo.ShelfId + ".TXT"); ImportDetailsInfo[] detailInfoList = detailInfoEngine.ReadFile(detailPacket); if (headerInfoEngine.ErrorManager.ErrorCount > 0) { headerInfoEngine.ErrorManager.SaveErrors(logFile); } Utility.WriteLog(" => Checking Shelf ("+ headerInfo.ShelfId + " - " + headerInfo.ShelfName + ")", logFile); for (int iDetail = 0; iDetail < detailInfoList.Length; iDetail++) { ImportDetailsInfo detailInfo = detailInfoList[iDetail]; if (string.IsNullOrEmpty(detailInfo.Barcode)) { iCountBarcode++; missingQty += detailInfo.Qty; Utility.WriteLog(" [ERROR] Barcode does not exist. ", logFile); } else { Guid productId = ProductBarcodeEx.GetProductIdByBarcode(detailInfo.Barcode); if (productId == System.Guid.Empty) { iCountBarcode++; missingQty += detailInfo.Qty; Utility.WriteLog(" [ERROR] Barcode ("+ detailInfo.Barcode + ") does not exist. ", logFile); } else { if (detailInfo.Qty <= 0) { Utility.WriteLog(" [ERROR] Barcode ("+ detailInfo.Barcode + ") QTY <= 0 ", logFile); isValid = isValid & false; } else { Utility.WriteLog(" [OK] Barcode ("+ detailInfo.Barcode + ") QTY > 0 ", logFile); } } } totalLine++; totalQty += detailInfo.Qty; detailAllList.Add(detailInfo); } missingLine += iCountBarcode; if (iCountBarcode > 0) { Utility.WriteLog(" [ERROR] Details of Shelf ("+ headerInfo.ShelfId + " - " + headerInfo.ShelfName + ") has " + iCountBarcode.ToString() + " empty barcode.", logFile); } else { Utility.WriteLog(" [OK] Details of Shelf ("+ headerInfo.ShelfId + " - " + headerInfo.ShelfName + ") has 0 empty barcode.", logFile); } } #endregion Utility.WriteLog(" RESULT : COMPLETED", logFile); Utility.WriteLog("=> Save Packet", logFile); #region isValid: wrapping up if (isValid) { if (stktkNumber.Trim().Length == 0) { stktkNumber = SystemInfoHelper.Settings.QueuingTxNumber(EnumHelper.TxType.STK); } Utility.WriteLog(" [OK] System Queue ", logFile); if (stktkNumber.Length > 0) { // Stock take header System.Guid stktkheaderId = CreateStockTakeHeader(stktkNumber.Trim(), workplaceId); Utility.WriteLog(" [OK] Create Worksheet (Stock Take - Header)", logFile); // Stock take details if (stktkheaderId != System.Guid.Empty) { CreatedStockTakeDetail(stktkheaderId, stktkNumber.Trim(), detailAllList, workplaceId, uploadedOn); } Utility.WriteLog(" [OK] Create Worksheet (Stock Take - Detail)", logFile); // Stock take header (HHT) System.Guid hhtHeaderId = CreateStockTakeHHTHeader(stktkNumber.Trim(), lvItem.SubItems[4].Text, uploadedOn, workplaceId, lvItem.SubItems[2].Text, totalLine, totalQty, missingLine, missingQty); Utility.WriteLog(" [OK] Create Worksheet (HHT Data Review - Header)", logFile); // Stock take details (HHT) if (hhtHeaderId != System.Guid.Empty) { CreateStockTakeHHTDetails(hhtHeaderId, stktkNumber.Trim(), lvItem.SubItems[4].Text, uploadedOn, detailAllList, lvItem.SubItems[2].Text); } Utility.WriteLog(" [OK] Create Worksheet (HHT Data Review - Detail)", logFile); Utility.WriteLog(" [OK] Barcode Matching", logFile); Utility.WriteLog(" [OK] Counting Missing Data", logFile); Utility.WriteLog(" RESULT : COMPLETED", logFile); // Backup text files if (!Directory.Exists(backupDirectory)) { Directory.CreateDirectory(backupDirectory); } // Header file File.Move(lvItem.SubItems[8].Text, Path.Combine(backupDirectory, Path.GetFileName(lvItem.SubItems[8].Text))); for (int i = 0; i < packetFiles.Length; i++) { File.Move(packetFiles[i], Path.Combine(backupDirectory, Path.GetFileName(packetFiles[i]))); } Utility.WriteLog("=> Backup Data RESULT : COMPLETED", logFile); } } #endregion iCount++; } } }