public async Task InventoryItemType_Save_DoesSave()
        {
            // Arrange
            var itemType = new ItemType
            {
                Name = Guid.NewGuid().ToString(),
                BaseUnitOfMeasure         = Guid.NewGuid().ToString(),
                Comment                   = Guid.NewGuid().ToString(),
                CostingMethod             = CostingMethod.Lifo,
                Description               = Guid.NewGuid().ToString(),
                EformId                   = 1,
                CreatedByUserId           = 1,
                GrossWeight               = 1,
                GtinEanUpc                = Guid.NewGuid().ToString(),
                LastPhysicalInventoryDate = new DateTime(2050, 1, 1, 1, 1, 1),
                NetWeight                 = 1,
                No                 = Guid.NewGuid().ToString(),
                ProfitPercent      = 1,
                Region             = 1,
                RiskDescription    = Guid.NewGuid().ToString(),
                SalesUnitOfMeasure = UnitOfMeasure.Liter,
                StandardCost       = 1,
                Usage              = Guid.NewGuid().ToString(),
                UpdatedByUserId    = 1,
                UnitVolume         = 1,
                UnitPrice          = 1,
                UnitCost           = 1,
            };

            // Act
            await itemType.Create(DbContext);

            var itemTypes = DbContext.ItemTypes
                            .AsNoTracking()
                            .ToList();

            var itemTypeVersions = DbContext.ItemTypeVersions
                                   .AsNoTracking()
                                   .ToList();


            // Assert
            Assert.AreEqual(1, itemTypes.Count);
            Assert.AreEqual(itemType.CreatedByUserId, itemTypes[0].CreatedByUserId);
            Assert.AreEqual(itemType.UpdatedByUserId, itemTypes[0].UpdatedByUserId);
            Assert.AreEqual(itemType.Name, itemTypes[0].Name);
            Assert.AreEqual(itemType.BaseUnitOfMeasure, itemTypes[0].BaseUnitOfMeasure);
            Assert.AreEqual(itemType.Comment, itemTypes[0].Comment);
            Assert.AreEqual(itemType.CostingMethod, itemTypes[0].CostingMethod);
            Assert.AreEqual(itemType.Description, itemTypes[0].Description);
            Assert.AreEqual(itemType.EformId, itemTypes[0].EformId);
            Assert.AreEqual(itemType.GrossWeight, itemTypes[0].GrossWeight);
            Assert.AreEqual(itemType.GtinEanUpc, itemTypes[0].GtinEanUpc);
            Assert.AreEqual(itemType.LastPhysicalInventoryDate, itemTypes[0].LastPhysicalInventoryDate);
            Assert.AreEqual(itemType.NetWeight, itemTypes[0].NetWeight);
            Assert.AreEqual(itemType.No, itemTypes[0].No);
            Assert.AreEqual(itemType.ProfitPercent, itemTypes[0].ProfitPercent);
            Assert.AreEqual(itemType.Region, itemTypes[0].Region);
            Assert.AreEqual(itemType.RiskDescription, itemTypes[0].RiskDescription);
            Assert.AreEqual(itemType.SalesUnitOfMeasure, itemTypes[0].SalesUnitOfMeasure);
            Assert.AreEqual(itemType.StandardCost, itemTypes[0].StandardCost);
            Assert.AreEqual(itemType.Usage, itemTypes[0].Usage);
            Assert.AreEqual(itemType.UnitVolume, itemTypes[0].UnitVolume);
            Assert.AreEqual(itemType.UnitPrice, itemTypes[0].UnitPrice);
            Assert.AreEqual(itemType.UnitCost, itemTypes[0].UnitCost);
            Assert.AreEqual(1, itemTypes[0].Version);
            Assert.AreEqual(Constants.WorkflowStates.Created, itemTypes[0].WorkflowState);
            Assert.AreEqual(itemType.WorkflowState, itemTypes[0].WorkflowState);


            // versions
            Assert.AreEqual(1, itemTypeVersions.Count);
            Assert.AreEqual(itemType.CreatedByUserId, itemTypeVersions[0].CreatedByUserId);
            Assert.AreEqual(itemType.UpdatedByUserId, itemTypeVersions[0].UpdatedByUserId);
            Assert.AreEqual(itemType.Name, itemTypeVersions[0].Name);
            Assert.AreEqual(itemType.BaseUnitOfMeasure, itemTypeVersions[0].BaseUnitOfMeasure);
            Assert.AreEqual(itemType.Comment, itemTypeVersions[0].Comment);
            Assert.AreEqual(itemType.CostingMethod, itemTypeVersions[0].CostingMethod);
            Assert.AreEqual(itemType.Description, itemTypeVersions[0].Description);
            Assert.AreEqual(itemType.EformId, itemTypeVersions[0].EformId);
            Assert.AreEqual(itemType.GrossWeight, itemTypeVersions[0].GrossWeight);
            Assert.AreEqual(itemType.GtinEanUpc, itemTypeVersions[0].GtinEanUpc);
            Assert.AreEqual(itemType.LastPhysicalInventoryDate, itemTypeVersions[0].LastPhysicalInventoryDate);
            Assert.AreEqual(itemType.NetWeight, itemTypeVersions[0].NetWeight);
            Assert.AreEqual(itemType.No, itemTypeVersions[0].No);
            Assert.AreEqual(itemType.ProfitPercent, itemTypeVersions[0].ProfitPercent);
            Assert.AreEqual(itemType.Region, itemTypeVersions[0].Region);
            Assert.AreEqual(itemType.RiskDescription, itemTypeVersions[0].RiskDescription);
            Assert.AreEqual(itemType.SalesUnitOfMeasure, itemTypeVersions[0].SalesUnitOfMeasure);
            Assert.AreEqual(itemType.StandardCost, itemTypeVersions[0].StandardCost);
            Assert.AreEqual(itemType.Usage, itemTypeVersions[0].Usage);
            Assert.AreEqual(itemType.UnitVolume, itemTypeVersions[0].UnitVolume);
            Assert.AreEqual(itemType.UnitPrice, itemTypeVersions[0].UnitPrice);
            Assert.AreEqual(itemType.UnitCost, itemTypeVersions[0].UnitCost);
            Assert.AreEqual(itemType.Id, itemTypeVersions[0].ItemTypeId);
            Assert.AreEqual(Constants.WorkflowStates.Created, itemTypeVersions[0].WorkflowState);
            Assert.AreEqual(itemType.WorkflowState, itemTypeVersions[0].WorkflowState);
            Assert.AreEqual(1, itemTypeVersions[0].Version);
        }
        public async Task InventoryItem_Save_DoesSave()
        {
            // Arrange
            var itemType = new ItemType
            {
                Name = Guid.NewGuid().ToString(),
                BaseUnitOfMeasure         = Guid.NewGuid().ToString(),
                Comment                   = Guid.NewGuid().ToString(),
                CostingMethod             = CostingMethod.Lifo,
                Description               = Guid.NewGuid().ToString(),
                EformId                   = 1,
                CreatedByUserId           = 1,
                GrossWeight               = 1,
                GtinEanUpc                = Guid.NewGuid().ToString(),
                LastPhysicalInventoryDate = new DateTime(2050, 1, 1, 1, 1, 1),
                NetWeight                 = 1,
                No                 = Guid.NewGuid().ToString(),
                ProfitPercent      = 1,
                Region             = 1,
                RiskDescription    = Guid.NewGuid().ToString(),
                SalesUnitOfMeasure = UnitOfMeasure.Liter,
                StandardCost       = 1,
                Usage              = Guid.NewGuid().ToString(),
                UpdatedByUserId    = 1,
                UnitVolume         = 1,
                UnitPrice          = 1,
                UnitCost           = 1,
            };

            await itemType.Create(DbContext);

            var item = new Item
            {
                SN = Guid.NewGuid().ToString(),
                CreatedByUserId = 1,
                UpdatedByUserId = 1,
                Available       = true,
                ExpirationDate  = new DateTime(2050, 1, 1, 1, 1, 1),
                Location        = Guid.NewGuid().ToString(),
                CustomerId      = 1,
                ItemTypeId      = itemType.Id,
            };

            // Act
            await item.Create(DbContext);


            var items = DbContext.Items
                        .AsNoTracking()
                        .ToList();

            var itemVersions = DbContext.ItemVersions
                               .AsNoTracking()
                               .ToList();


            // Assert
            Assert.AreEqual(1, items.Count);
            Assert.AreEqual(item.CreatedByUserId, items[0].CreatedByUserId);
            Assert.AreEqual(item.UpdatedByUserId, items[0].UpdatedByUserId);
            Assert.AreEqual(item.Available, items[0].Available);
            Assert.AreEqual(item.ItemTypeId, items[0].ItemTypeId);
            Assert.AreEqual(item.CustomerId, items[0].CustomerId);
            Assert.AreEqual(item.Location, items[0].Location);
            Assert.AreEqual(item.ExpirationDate, items[0].ExpirationDate);
            Assert.AreEqual(item.SN, items[0].SN);
            Assert.AreEqual(1, items[0].Version);
            Assert.AreEqual(Constants.WorkflowStates.Created, items[0].WorkflowState);
            Assert.AreEqual(item.WorkflowState, items[0].WorkflowState);


            // versions
            Assert.AreEqual(1, itemVersions.Count);
            Assert.AreEqual(item.CreatedByUserId, itemVersions[0].CreatedByUserId);
            Assert.AreEqual(item.UpdatedByUserId, itemVersions[0].UpdatedByUserId);
            Assert.AreEqual(item.Available, itemVersions[0].Available);
            Assert.AreEqual(item.ItemTypeId, itemVersions[0].ItemTypeId);
            Assert.AreEqual(item.CustomerId, itemVersions[0].CustomerId);
            Assert.AreEqual(item.Location, itemVersions[0].Location);
            Assert.AreEqual(item.ExpirationDate, itemVersions[0].ExpirationDate);
            Assert.AreEqual(item.SN, itemVersions[0].SN);
            Assert.AreEqual(item.Id, itemVersions[0].ItemTypeId);
            Assert.AreEqual(item.WorkflowState, itemVersions[0].WorkflowState);
            Assert.AreEqual(1, itemVersions[0].Version);
        }
        public async Task <OperationDataResult <int> > CreateItemType(ItemTypeCreateModel itemTypeCreateModel)
        {
            try
            {
                var tags = new List <InventoryTag>();
                if (itemTypeCreateModel.TagIds.Count > 0)
                {
                    tags = _dbContext.InventoryTags
                           .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed)
                           .Where(x => itemTypeCreateModel.TagIds.Contains(x.Id))
                           .ToList();

                    //if (tags.Count != itemTypeCreateModel.TagIds.Count)
                    //{
                    //    return new OperationResult(false,
                    //        _inventoryLocalizationService.GetString("TagsNotFound"));
                    //}
                }

                var itemType = new ItemType
                {
                    Name            = itemTypeCreateModel.Name,
                    Description     = itemTypeCreateModel.Description,
                    CreatedByUserId = _userService.UserId,
                    UpdatedByUserId = _userService.UserId,
                    Usage           = itemTypeCreateModel.Usage,
                    RiskDescription = itemTypeCreateModel.RiskDescription,
                    ItemGroupId     = itemTypeCreateModel.ItemGroupId,
                };
                await itemType.Create(_dbContext);

                // create dependencies
                foreach (var dependency in itemTypeCreateModel.Dependencies)
                {
                    if (await _dbContext.ItemGroups.Where(y => y.WorkflowState != Constants.WorkflowStates.Removed).Where(x => x.Id == dependency.ItemGroupId).AnyAsync())
                    {
                        // create item group dependency
                        var dependencyItemGroup = new ItemGroupDependency
                        {
                            CreatedByUserId = _userService.UserId,
                            UpdatedByUserId = _userService.UserId,
                            ItemGroupId     = (int)dependency.ItemGroupId,
                            ItemTypeId      = itemType.Id,
                        };
                        await dependencyItemGroup.Create(_dbContext);

                        // create item type dependency
                        foreach (var dependencyItemTypesId in dependency.ItemTypesIds)
                        {
                            if (await _dbContext.ItemTypes.Where(y => y.WorkflowState != Constants.WorkflowStates.Removed).Where(x => x.Id == dependencyItemTypesId).AnyAsync())
                            {
                                var dependencyItemType = new ItemTypeDependency
                                {
                                    CreatedByUserId  = _userService.UserId,
                                    UpdatedByUserId  = _userService.UserId,
                                    ParentItemTypeId = itemType.Id,
                                    DependItemTypeId = dependencyItemTypesId,
                                };
                                await dependencyItemType.Create(_dbContext);
                            }
                        }
                    }
                }

                // creating a dependency between tag and item type
                foreach (var itemTypeTag in tags.Select(inventoryTag => new ItemTypeTag
                {
                    CreatedByUserId = _userService.UserId,
                    UpdatedByUserId = _userService.UserId,
                    InventoryTagId = inventoryTag.Id,
                    ItemTypeId = itemType.Id,
                }))
                {
                    await itemTypeTag.Create(_dbContext);
                }

                return(new OperationDataResult <int>(true, itemType.Id));
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                return(new OperationDataResult <int>(false,
                                                     _inventoryLocalizationService.GetString("ErrorWhileCreatingItemType")));
            }
        }