public void InsertItems(List <ItemEntity> itemEntityList, string itemTradingTypeName, string objectTypeName) { using (UnitOfWork uow = XpoHelper.GetNewUnitOfWork()) { foreach (var itemEntity in itemEntityList) { try { if (itemEntity.Code.Equals("EVE006")) { string debug = "Y"; } //Check required if (itemEntity.Code == null || itemEntity.Code.Trim().Length == 0) { continue; } //Check dupplicate code bool isExist = NAS.DAL.Util.isExistXpoObject <NAS.DAL.Nomenclature.Item.Item> ("Code", itemEntity.Code, Constant.ROWSTATUS_ACTIVE, Constant.ROWSTATUS_DEFAULT, Constant.ROWSTATUS_INACTIVE); if (isExist) { continue; } //Get item trading type ItemTradingType itemTradingType = Util.getXPCollection <ItemTradingType>(uow, "Name", itemTradingTypeName).FirstOrDefault(); //Get Manufacturer ManufacturerOrg manufacturerOrg = Util.getXPCollection <ManufacturerOrg>(uow, "Code", itemEntity.ManufacturerCode, Constant.ROWSTATUS_ACTIVE).FirstOrDefault(); if (manufacturerOrg == null) { manufacturerOrg = Util.getDefaultXpoObject <ManufacturerOrg>(uow); } //Insert into Item table NAS.DAL.Nomenclature.Item.Item item = new NAS.DAL.Nomenclature.Item.Item(uow) { Code = itemEntity.Code, Description = itemEntity.Description, ManufacturerOrgId = manufacturerOrg, ItemTradingTypeId = itemTradingType, Name = itemEntity.Name, RowStatus = Constant.ROWSTATUS_ACTIVE, RowCreationTimeStamp = DateTime.Now }; item.Save(); //Get Supplier SupplierOrg supplierOrg = Util.getXPCollection <SupplierOrg>(uow, "Code", itemEntity.SupplierCode, Constant.ROWSTATUS_ACTIVE).FirstOrDefault(); if (supplierOrg != null) { //Insert into ItemSupplier table ItemSupplier itemSupplier = new ItemSupplier(uow) { ItemId = item, SupplierOrgId = supplierOrg }; itemSupplier.Save(); } ObjectType objectType = Util.getXPCollection <ObjectType>(uow, "Name", objectTypeName, Constant.ROWSTATUS_ACTIVE).FirstOrDefault(); if (objectType != null) { //Insert into ItemSupplier table ItemCustomType itemCustomType = new ItemCustomType(uow) { ItemId = item, ObjectTypeId = objectType }; itemCustomType.Save(); } uow.CommitChanges(); //using (Session itemUnitSession = XpoHelper.GetNewSession()) //{ // //Get UNIT relation type // ItemUnitRelationType unitRelationType = // NAS.DAL.Util.getXPCollection<ItemUnitRelationType>(itemUnitSession, "Name", "UNIT").FirstOrDefault(); // itemEntity.ItemUnits = itemEntity.ItemUnits.OrderBy(r => r.Level).ToList(); // ////Insert into ItemUnit // foreach (ItemUnitEntity itemUnitEntity in itemEntity.ItemUnits) // { // NAS.DAL.Nomenclature.Item.Item itemTemp = // NAS.DAL.Util.getXPCollection<NAS.DAL.Nomenclature.Item.Item> // (itemUnitSession, "Code", itemUnitEntity.ItemCode).FirstOrDefault(); // Unit unit = // NAS.DAL.Util.getXPCollection<Unit>(itemUnitSession, "Code", itemUnitEntity.UnitCode).FirstOrDefault(); // if (unit == null) // break; // Unit parentUnit = // NAS.DAL.Util.getXPCollection<Unit>(itemUnitSession, "Code", itemUnitEntity.ParentUnitCode).FirstOrDefault(); // ItemUnit parentItemUnit = itemTemp.ItemUnits.Where(r => r.UnitId == parentUnit).FirstOrDefault(); // ItemUnit itemUnit = new ItemUnit(itemUnitSession) // { // ItemId = itemTemp, // ItemUnitRelationTypeId = unitRelationType, // NumRequired = itemUnitEntity.NumRequired, // RowStatus = Constant.ROWSTATUS_ACTIVE, // UnitId = unit, // ParentItemUnitId = parentItemUnit, // RowCreationTimeStamp = DateTime.Now // }; // itemUnit.Save(); // } //} } catch (Exception ex) { string msg = ex.Message; } } } }
/// <summary> /// Populate default data to database /// </summary> public static void Populate() { try { ////NAS.DAL.Accounting.AccountChart NAS.DAL.BI.Accounting.Account.CorrespondFinancialAccountDim.Populate(); NAS.DAL.BI.Accounting.Account.FinancialAccountDim.Populate(); Account.Populate(); AccountType.Populate(); AccountCategory.Populate(); Currency.Populate(); ////NAS.DAL.Accounting.Configure AllocationType.Populate(); ////NAS.DAL.CMS.ObjectDocument ObjectType.Populate(); CustomFieldType.Populate(); ObjectTypeCustomField.Populate(); ////NAS.DAL.Inventory.Item RecordedType.Populate(); ////NAS.DAL.Inventory.Operation CommanderStockCartStatus.Populate(); CommanderStockCartType.Populate(); ////NAS.DAL.Inventory.StockCart StockCartActorType.Populate(); ////NAS.DAL.Invoice TaxType.Populate(); PromotionType.Populate(); ////NAS.DAL.Nomenclature.Inventory Nomenclature.Inventory.Inventory.Populate(); InventoryUnit.Populate(); ////NAS.DAL.Nomenclature.Item ItemUnitRelationType.Populate(); UnitType.Populate(); Unit.Populate(); ItemUnit.Populate(); ItemTradingType.Populate(); ItemCustomType.Populate(); ////NAS.DAL.Nomenclature.Organization TradingCategory.Populate(); AuthenticationProvider.Populate(); DepartmentType.Populate(); OrganizationType.Populate(); Person.Populate(); Organization.Populate(); OwnerOrg.Populate(); CustomerOrg.Populate(); SupplierOrg.Populate(); ManufacturerOrg.Populate(); Department.Populate(); DepartmentPerson.Populate(); //NAS.DAL.Staging.Accounting.Journal AccountActorType.Populate(); ////NAS.DAL.Vouches VouchesType.Populate(); VouchesActorType.Populate(); ReceiptVouchesType.Populate(); PaymentVouchesType.Populate(); ForeignCurrency.Populate(); ////NAS.DAL.Accounting.Journal AccountingPeriod.Populate(); //SalesInvoicePickingStockCart.Populate(); ////NAS.DAL.System.ArtifactCode ArtifactType.Populate(); CodeRuleDataType.Populate(); CodeRuleDataFormat.Populate(); RuleRepeaterType.Populate(); //NAS.DAL.Sales.Price PricePolicyType.Populate(); //NAS.DAL.Inventory.Lot.Lot NAS.DAL.Inventory.Lot.Lot.Populate(); //NAS.DAL.Inventory.Command.InventoryCommandActorType NAS.DAL.Inventory.Command.InventoryCommandActorType.Populate(); NAS.DAL.BI.Inventory.InventoryCommandDim.Populate(); #region Other populate using (Session session = XpoHelper.GetNewSession()) { //Insert undefined supplier if (!Util.isExistXpoObject <SupplierOrg>("OrganizationId", Guid.Parse("3DEF2B62-2162-46CD-8418-DEE6F8E59E21"))) { SupplierOrg undefinedSupplierOrg = new SupplierOrg(session) { OrganizationId = Guid.Parse("3DEF2B62-2162-46CD-8418-DEE6F8E59E21"), Name = "Mặc định", Description = "Mặc định", Code = "MACDINH", RowCreationTimeStamp = DateTime.Now, RowStatus = Constant.ROWSTATUS_ACTIVE, OrganizationTypeId = NAS.DAL.Util.getDefaultXpoObject <OrganizationType>(session) }; undefinedSupplierOrg.Save(); } } #endregion } catch (Exception) { throw new Exception("Populate failed"); } finally { } }