public async Task AddProductItemToCatalog(CatalogProductModel productCatalogModel)
        {
            var company = await _dbContext.Companies.Include(c => c.Parent).Include(c => c.MyCompanies.Select(mc => mc.CompanyCatalogs))
                          .Include(c => c.CompanyCatalogs.Select(cc => cc.Catalog.CatalogProductItems)).FirstOrDefaultAsync(c => c.Id == productCatalogModel.CompanyId);

            if (company == null)
            {
                throw new CompanyNotFoundException($"CompanyId: {productCatalogModel.CompanyId}");
            }

            var assignedCatalog = company.CompanyCatalogs.FirstOrDefault(c => c.CatalogType == CatalogType.Assigned);

            if (assignedCatalog == null)
            {
                throw new NoAssignedCatalogException($"CompanyId: {productCatalogModel.CompanyId}");
            }

            var assignedCatalogProductItem = assignedCatalog.Catalog.CatalogProductItems.FirstOrDefault(p => p.ProductItemId == productCatalogModel.ProductItemId);

            if (assignedCatalogProductItem == null)
            {
                throw new NoProductInProductCatalogException($"CompanyId: {productCatalogModel.CompanyId}, CatalogId: {assignedCatalog.Catalog.Id}, ProductId: {productCatalogModel.ProductItemId}");
            }

            var companyCatalog = company.CompanyCatalogs.FirstOrDefault(c => c.CatalogId == productCatalogModel.CatalogId && c.CatalogType == CatalogType.MyCatalog);

            if (companyCatalog == null)
            {
                throw new NonExistingCompanyCatalogException($"CompanyId: {company.Id}, CatalogI: {productCatalogModel.CatalogId}");
            }

            if (companyCatalog.Catalog.CatalogProductItems.All(p => p.ProductItemId != productCatalogModel.ProductItemId))
            {
                companyCatalog.Catalog.CatalogProductItems.Add(new CatalogProductItem
                {
                    CatalogId        = productCatalogModel.CatalogId,
                    ProductItemId    = productCatalogModel.ProductItemId,
                    ResellerPrice    = productCatalogModel.ResellerPrice,
                    RetailPrice      = company.Parent == null ? productCatalogModel.RetailPrice : assignedCatalogProductItem.FixedRetailPrice ? assignedCatalogProductItem.RetailPrice : productCatalogModel.RetailPrice,
                    FixedRetailPrice = company.Parent == null ? productCatalogModel.FixedRetailPrice : assignedCatalogProductItem.FixedRetailPrice,
                    Available        = true
                });
            }

            foreach (var myCompany in company.MyCompanies.Where(c => c.CompanyCatalogs.Any(cc => cc.CatalogType == CatalogType.Assigned && cc.CatalogId == productCatalogModel.CatalogId)))
            {
                foreach (var myCompanyCatalog in myCompany.CompanyCatalogs.Where(c => c.CatalogType == CatalogType.MyCatalog))
                {
                    await AddProductItemToCatalog(new CatalogProductModel
                    {
                        CompanyId        = myCompany.Id,
                        ProductItemId    = productCatalogModel.ProductItemId,
                        CatalogId        = myCompanyCatalog.CatalogId,
                        FixedRetailPrice = company.Parent == null ? productCatalogModel.FixedRetailPrice : assignedCatalogProductItem.FixedRetailPrice,
                        RetailPrice      = company.Parent == null ? productCatalogModel.RetailPrice : assignedCatalogProductItem.FixedRetailPrice ? assignedCatalogProductItem.RetailPrice : productCatalogModel.RetailPrice,
                        ResellerPrice    = _catalogUtilities.CalculateResellerPrice(productCatalogModel.RetailPrice, productCatalogModel.ResellerPrice)
                    });
                }
            }

            await _dbContext.SaveChangesAsync();
        }
예제 #2
0
        public async Task AssignCatalogToCompany(int parentCompanyId, int companyId, int catalogId)
        {
            var parentCompany = await _dbContext.Companies
                                .Include(c => c.MyCompanies.Select(mc => mc.CompanyCatalogs.Select(cc => cc.Catalog.CatalogProductItems)))
                                .Include(c => c.MyCompanies.Select(mc => mc.MyCompanies.Select(cc => cc.CompanyCatalogs.Select(mcc => mcc.Catalog.CatalogProductItems))))
                                .Include(c => c.CompanyCatalogs.Select(cc => cc.Catalog.CatalogProductItems)).FirstOrDefaultAsync(c => c.Id == parentCompanyId);

            if (parentCompany == null)
            {
                throw new CompanyNotFoundException($"CompanyId: {companyId}");
            }

            var company = parentCompany.MyCompanies.FirstOrDefault(c => c.Id == companyId);

            if (company == null)
            {
                throw new CompanyNotFoundException($"CompanyId: {companyId}");
            }

            var assignedCatalog = company.CompanyCatalogs.FirstOrDefault(c => c.CatalogType == CatalogType.Assigned);

            if (assignedCatalog != null)
            {
                if (assignedCatalog.CatalogId == catalogId)
                {
                    return;
                }

                _dbContext.CompanyCatalogs.Remove(assignedCatalog);
            }
            var catalogToAssign = parentCompany.CompanyCatalogs.FirstOrDefault(cc => cc.CatalogType == CatalogType.MyCatalog && cc.CatalogId == catalogId);

            if (catalogToAssign == null)
            {
                throw new NonExistingCompanyCatalogException($"CompanyId {parentCompanyId}, CatalogId {catalogId}");
            }

            _dbContext.CompanyCatalogs.Add(new CompanyCatalog
            {
                CatalogType = CatalogType.Assigned,
                Catalog     = catalogToAssign.Catalog,
                Company     = company
            });

            foreach (var myCompanyCatalog in company.CompanyCatalogs.Where(cc => cc.CatalogType == CatalogType.MyCatalog))
            {
                foreach (var assignedCatalogProductItem in catalogToAssign.Catalog.CatalogProductItems)
                {
                    var myCompanyCatalogProductItem = myCompanyCatalog.Catalog.CatalogProductItems.FirstOrDefault(cc => cc.ProductItemId == assignedCatalogProductItem.ProductItemId);

                    if (myCompanyCatalogProductItem == null && assignedCatalogProductItem.Available)
                    {
                        await _catalogProductItemService.AddProductItemToCatalog(new CatalogProductModel
                        {
                            CatalogId        = myCompanyCatalog.CatalogId,
                            CompanyId        = myCompanyCatalog.CompanyId,
                            ProductItemId    = assignedCatalogProductItem.ProductItemId,
                            RetailPrice      = assignedCatalogProductItem.RetailPrice,
                            FixedRetailPrice = assignedCatalogProductItem.FixedRetailPrice,
                            ResellerPrice    = _catalogUtilities.CalculateResellerPrice(assignedCatalogProductItem.RetailPrice, assignedCatalogProductItem.ResellerPrice)
                        });
                    }
                    else if (myCompanyCatalogProductItem != null && !assignedCatalogProductItem.Available)
                    {
                        await _catalogProductItemService.RemoveProductItemFromCatalog(company.Id, assignedCatalogProductItem.ProductItemId, myCompanyCatalog.CatalogId);
                    }

                    await _catalogProductItemService.UpdateFixedRetailPrice(company.Id, assignedCatalogProductItem.ProductItemId, myCompanyCatalog.CatalogId, assignedCatalogProductItem.FixedRetailPrice);
                }
            }

            await _dbContext.SaveChangesAsync();
        }