private void CreateProducts(ListViewItem listItem) { using (var ctx = new EF6.RT2020Entities()) { // Check BatchId(listItem.SubItems[1].Text) and Stock Code(listItem.SubItems[2].Text) Guid batchId = Guid.Empty; if (IsValid() && Guid.TryParse(listItem.SubItems[1].Text, out batchId) && listItem.SubItems[2].Text.Length > 0) { var oBatch = ctx.ProductBatch.Find(batchId); if (oBatch != null) { string a1 = listItem.SubItems[3].Text.Trim(); string a2 = listItem.SubItems[4].Text.Trim(); string a3 = listItem.SubItems[5].Text.Trim(); Guid a1Id = Guid.Empty, a2Id = Guid.Empty, a3Id = Guid.Empty; Guid.TryParse(listItem.SubItems[6].Text, out a1Id); Guid.TryParse(listItem.SubItems[7].Text, out a2Id); Guid.TryParse(listItem.SubItems[8].Text, out a3Id); string prodCode = listItem.SubItems[2].Text + a1 + a2 + a3; if (prodCode.Length <= 22) { var stkcode = listItem.SubItems[2].Text.Trim(); StringBuilder sql = new StringBuilder(); sql.Append(" STKCODE = '").Append(listItem.SubItems[2].Text.Trim()).Append("' "); sql.Append(" AND APPENDIX1 = '").Append(a1.Trim()).Append("' "); sql.Append(" AND APPENDIX2 = '").Append(a2.Trim()).Append("' "); sql.Append(" AND APPENDIX3 = '").Append(a3.Trim()).Append("' "); var oItem = ctx.Product.Where(x => x.STKCODE == stkcode && x.APPENDIX1 == a1 && x.APPENDIX2 == a2 && x.APPENDIX3 == a3).FirstOrDefault(); if (oItem == null) { #region add new Product oItem = new EF6.Product(); oItem.ProductId = Guid.NewGuid(); oItem.STKCODE = listItem.SubItems[2].Text; oItem.APPENDIX1 = a1; oItem.APPENDIX2 = a2; oItem.APPENDIX3 = a3; oItem.Status = Convert.ToInt32(EnumHelper.Status.Active.ToString("d")); oItem.CLASS1 = oBatch.CLASS1; oItem.CLASS2 = oBatch.CLASS2; oItem.CLASS3 = oBatch.CLASS3; oItem.CLASS4 = oBatch.CLASS4; oItem.CLASS5 = oBatch.CLASS5; oItem.CLASS6 = oBatch.CLASS6; oItem.ProductName = oBatch.Description; oItem.ProductName_Chs = oBatch.Description; oItem.ProductName_Cht = oBatch.Description; oItem.Remarks = oBatch.REMARKS; oItem.NormalDiscount = oBatch.NRDISC.Value; oItem.UOM = oBatch.MAINUNIT; oItem.NatureId = ProductNatureEx.GetIdByCode(oBatch.NATURE); oItem.FixedPriceItem = false; oItem.CreatedBy = ConfigHelper.CurrentUserId; oItem.CreatedOn = DateTime.Now; oItem.ModifiedBy = ConfigHelper.CurrentUserId; oItem.ModifiedOn = DateTime.Now; ctx.Product.Add(oItem); var productId = oItem.ProductId; #endregion #region SaveProductBarcode(oBatch, productId, prodCode); var oBarcode = ctx.ProductBarcode.Where(x => x.ProductId == productId).FirstOrDefault(); if (oBarcode == null) { oBarcode = new EF6.ProductBarcode(); oBarcode.ProductBarcodeId = Guid.NewGuid(); oBarcode.ProductId = productId; oBarcode.Barcode = prodCode; oBarcode.BarcodeType = "INTER"; oBarcode.PrimaryBarcode = true; oBarcode.DownloadToPOS = (oBatch.RETAILITEM == "F") ? false : true; oBarcode.DownloadToCounter = (oBatch.COUNTER_ITEM == "F") ? false : true; ctx.ProductBarcode.Add(oBarcode); ctx.SaveChanges(); } #endregion #region Appendix / Class System.Guid c1Id = ProductClass1Ex.GetClassIdByCode(oBatch.CLASS1); System.Guid c2Id = ProductClass2Ex.GetClassIdByCode(oBatch.CLASS2); System.Guid c3Id = ProductClass3Ex.GetClassIdByCode(oBatch.CLASS3); System.Guid c4Id = ProductClass4Ex.GetClassIdByCode(oBatch.CLASS4); System.Guid c5Id = ProductClass5Ex.GetClassIdByCode(oBatch.CLASS5); System.Guid c6Id = ProductClass6Ex.GetClassIdByCode(oBatch.CLASS6); // SaveProductCode(productId, a1Id, a2Id, a3Id, c1Id, c2Id, c3Id, c4Id, c5Id, c6Id); var oCode = ctx.ProductCode.Where(x => x.ProductId == productId).FirstOrDefault(); if (oCode == null) { oCode = new EF6.ProductCode(); oCode.CodeId = Guid.NewGuid(); oCode.ProductId = productId; oCode.Appendix1Id = a1Id; oCode.Appendix2Id = a2Id; oCode.Appendix3Id = a3Id; ctx.ProductCode.Add(oCode); } oCode.Class1Id = c1Id; oCode.Class2Id = c2Id; oCode.Class3Id = c3Id; oCode.Class4Id = c4Id; oCode.Class5Id = c5Id; oCode.Class6Id = c6Id; ctx.SaveChanges(); #endregion // Product Price #region SaveProductSupplement(oBatch, oItem.ProductId); //string sql = "ProductId = '" + productId.ToString() + "'"; var oProdSupp = ctx.ProductSupplement.Where(x => x.ProductId == productId).FirstOrDefault(); if (oProdSupp == null) { oProdSupp = new EF6.ProductSupplement(); oProdSupp.SupplementId = Guid.NewGuid(); oProdSupp.ProductId = productId; ctx.ProductSupplement.Add(oProdSupp); } oProdSupp.VendorCurrencyCode = oBatch.VCURR; oProdSupp.VendorPrice = oBatch.VPRC; oProdSupp.ProductName_Memo = oBatch.DESC_MEMO; oProdSupp.ProductName_Pole = oBatch.DESC_POLE; ctx.SaveChanges(); #endregion #region SaveProductPrice(oBatch, oItem.ProductId); SaveProductPrice(productId, ProductHelper.Prices.BASPRC.ToString(), "HKD", oBatch.BASPRC.ToString()); SaveProductPrice(productId, ProductHelper.Prices.ORIPRC.ToString(), "HKD", oBatch.ORIPRC.ToString()); SaveProductPrice(productId, ProductHelper.Prices.VPRC.ToString(), oBatch.VCURR, oBatch.VPRC.ToString()); SaveProductPrice(productId, ProductHelper.Prices.WHLPRC.ToString(), "HKD", oBatch.WHLPRC.ToString()); #endregion // Remarks #region SaveProductRemarks(oBatch, oItem.ProductId); var oRemarks = ctx.ProductRemarks.Where(x => x.ProductId == productId).FirstOrDefault(); if (oRemarks == null) { oRemarks = new EF6.ProductRemarks(); oRemarks.ProductRemarksId = Guid.NewGuid(); oRemarks.ProductId = productId; ctx.ProductRemarks.Add(oRemarks); } oRemarks.Photo = oBatch.PHOTO; oRemarks.BinX = oBatch.BINX; oRemarks.BinY = oBatch.BINY; oRemarks.BinZ = oBatch.BINZ; oRemarks.DownloadToShop = (oBatch.RETAILITEM == "F") ? false : true; oRemarks.OffDisplayItem = (oBatch.OFF_DISPLAY_ITEM == "F") ? false : true; oRemarks.DownloadToCounter = (oBatch.COUNTER_ITEM == "F") ? false : true; oRemarks.REMARK1 = oBatch.REMARK1; oRemarks.REMARK2 = oBatch.REMARK2; oRemarks.REMARK3 = oBatch.REMARK3; oRemarks.REMARK4 = oBatch.REMARK4; oRemarks.REMARK5 = oBatch.REMARK5; oRemarks.REMARK6 = oBatch.REMARK6; ctx.SaveChanges(); #endregion oBatch.STATUS = "OK"; ctx.SaveChanges(); } } } } } }