Exemplo n.º 1
0
        public async Task <ProductSegmentDto> SaveAsync(Guid orgId, ProductSegmentDto dto)
        {
            if (orgId == null || orgId == Guid.Empty)
            {
                throw new ArgumentNullException("orgId", "Org id is missing");
            }

            ProductSegment segment = _mapper.Map <ProductSegment>(dto);

            if (segment.Id == Guid.Empty)
            {
                segment.OrgId        = orgId;
                segment.DateCreated  = DateTime.UtcNow;
                segment.DateModified = null;
                await _context.ProductSegments.AddAsync(segment);
            }
            else
            {
                segment.DateModified = DateTime.UtcNow;
                _context.ProductSegments.Update(segment);
            }
            await _context.SaveChangesAsync();

            dto = _mapper.Map <ProductSegmentDto>(segment);
            return(dto);
        }
Exemplo n.º 2
0
        public async Task <InvoiceCategoryDto> SaveAsync(Guid orgId, InvoiceCategoryDto dto)
        {
            if (orgId == null || orgId == Guid.Empty)
            {
                throw new ArgumentNullException("orgId", "Org id is missing");
            }

            InvoiceCategory category = _mapper.Map <InvoiceCategory>(dto);

            if (category.Id == Guid.Empty)
            {
                category.OrgId        = orgId;
                category.DateCreated  = DateTime.UtcNow;
                category.DateModified = null;
                await _context.InvoiceCategories.AddAsync(category);
            }
            else
            {
                category.OrgId        = orgId;
                category.DateModified = DateTime.UtcNow;
                _context.InvoiceCategories.Update(category);
            }
            await _context.SaveChangesAsync();

            dto = _mapper.Map <InvoiceCategoryDto>(category);
            return(dto);
        }
Exemplo n.º 3
0
        public async Task <ProductVarietyDto> SaveAsync(Guid orgId, ProductVarietyDto dto)
        {
            if (orgId == null || orgId == Guid.Empty)
            {
                throw new ArgumentNullException("orgId", "Org id is missing");
            }

            ProductVariety variety = _mapper.Map <ProductVariety>(dto);

            if (variety.Id == Guid.Empty)
            {
                variety.OrgId        = orgId;
                variety.DateCreated  = DateTime.UtcNow;
                variety.DateModified = null;
                await _context.ProductVarieties.AddAsync(variety);
            }
            else
            {
                variety.DateModified = DateTime.UtcNow;
                _context.ProductVarieties.Update(variety);
            }
            await _context.SaveChangesAsync();

            dto = _mapper.Map <ProductVarietyDto>(variety);
            return(dto);
        }
Exemplo n.º 4
0
        public async Task <OrganizationDto> CreateAsync(OrganizationDto model)
        {
            Organization org = _mapper.Map <Organization>(model);

            org.DateCreated  = DateTime.UtcNow;
            org.DateModified = null;
            await _context.Organizations.AddAsync(org);

            await _context.SaveChangesAsync();

            model = _mapper.Map <OrganizationDto>(org);
            return(model);
        }
Exemplo n.º 5
0
        public async Task <UnitDto> CreateAsync(Guid orgId, UnitDto model)
        {
            if (orgId == null || orgId == Guid.Empty)
            {
                throw new ArgumentNullException("orgId", "Org id is missing");
            }

            var unit = _mapper.Map <Unit>(model);

            unit.OrgId        = orgId;
            unit.DateCreated  = DateTime.UtcNow;
            unit.DateModified = null;
            await _context.Units.AddAsync(unit);

            await _context.SaveChangesAsync();

            return(_mapper.Map <UnitDto>(unit));
        }
Exemplo n.º 6
0
        public async Task <ProductCategoryDto> CreateAsync(Guid orgId, ProductCategoryDto model)
        {
            if (orgId == null || orgId == Guid.Empty)
            {
                throw new ArgumentNullException("orgId", "Org id is missing");
            }

            var item = _mapper.Map <ProductCategory>(model);

            item.OrgId        = orgId;
            item.DateCreated  = DateTime.UtcNow;
            item.DateModified = null;
            await _context.ProductCategories.AddAsync(item);

            await _context.SaveChangesAsync();

            return(_mapper.Map <ProductCategoryDto>(item));
        }
Exemplo n.º 7
0
        public async Task <PartyCategoryDto> CreateAsync(Guid orgId, PartyCategoryDto model)
        {
            if (orgId == null || orgId == Guid.Empty)
            {
                throw new ArgumentNullException("orgId", "Org id is missing");
            }

            PartyCategory category = _mapper.Map <PartyCategory>(model);

            category.OrgId        = orgId;
            category.DateCreated  = DateTime.UtcNow;
            category.DateModified = null;
            await _context.PartyCategories.AddAsync(category);

            await _context.SaveChangesAsync();

            return(_mapper.Map <PartyCategoryDto>(category));
        }
Exemplo n.º 8
0
        public async Task <AccountYearDto> CreateAsync(Guid orgId, AccountYearDto model)
        {
            if (orgId == null || orgId == Guid.Empty)
            {
                throw new ArgumentNullException("orgId", "Org id is missing");
            }

            AccountYear year = _mapper.Map <AccountYear>(model);

            year.OrgId        = orgId;
            year.DateCreated  = DateTime.UtcNow;
            year.DateModified = null;
            await _context.AccountYears.AddAsync(year);

            await _context.SaveChangesAsync();

            return(_mapper.Map <AccountYearDto>(year));
        }
Exemplo n.º 9
0
        public async Task <InvoiceDto> SaveAsync(Guid yearId, InvoiceDto dto)
        {
            if (yearId == null || yearId == Guid.Empty)
            {
                throw new ArgumentNullException("yearId", "Year id is missing");
            }

            Invoice invoice = _mapper.Map <Invoice>(dto);

            if (invoice.Id == Guid.Empty)
            {
                invoice.YearId       = yearId;
                invoice.DateCreated  = DateTime.UtcNow;
                invoice.DateModified = null;
                await _context.Invoices.AddAsync(invoice);
            }
            else
            {
                Invoice dbInvoice = await _context.Invoices
                                    .Where(i => i.Id == invoice.Id)
                                    .Include(i => i.InvoiceItems).ThenInclude(ii => ii.Stock)
                                    .SingleOrDefaultAsync();

                if (dbInvoice == null)
                {
                    invoice.DateCreated  = DateTime.UtcNow;
                    invoice.DateModified = null;
                    await _context.Invoices.AddAsync(invoice);
                }
                else
                {
                    invoice.DateModified = DateTime.UtcNow;
                    // delete all linet items that no longer exists
                    foreach (InvoiceItem dbLineItem in dbInvoice.InvoiceItems)
                    {
                        if (invoice.InvoiceItems.All(ii => ii.InvoiceItemId != dbLineItem.InvoiceItemId))
                        {
                            _context.Set <InvoiceItem>().Remove(dbLineItem);
                        }
                    }
                    // copy current (incoming) values to db
                    _context.Entry(dbInvoice).CurrentValues.SetValues(invoice);
                    var itemPairs = from curr in invoice.InvoiceItems //.Select(pi => pi.IngredientProduct)
                                    join db in dbInvoice.InvoiceItems //.Select(pi => pi.IngredientProduct)
                                    on curr.InvoiceItemId equals db.InvoiceItemId into grp
                                    from db in grp.DefaultIfEmpty()
                                    select new { curr, db };
                    foreach (var pair in itemPairs)
                    {
                        if (pair.db != null)
                        {
                            _context.Entry(pair.db).CurrentValues.SetValues(pair.curr);
                        }
                        else
                        {
                            await _context.Set <InvoiceItem>().AddAsync(pair.curr);
                        }
                    }
                }
            }
            await _context.SaveChangesAsync();

            dto = _mapper.Map <InvoiceDto>(invoice);
            return(dto);
        }
Exemplo n.º 10
0
        public async Task <ProductDto> SaveAsync(Guid orgId, ProductDto dto)
        {
            if (orgId == null || orgId == Guid.Empty)
            {
                throw new ArgumentNullException("orgId", "Org id is missing");
            }

            Product product = _mapper.Map <Product>(dto);

            if (product.Id == Guid.Empty)
            {
                product.OrgId        = orgId;
                product.DateCreated  = DateTime.UtcNow;
                product.DateModified = null;
                await _context.Products.AddAsync(product);
            }
            else
            {
                Product dbProd = await _context.Products
                                 .Where(p => p.Id == product.Id)
                                 .Include(p => p.ParentIngredients)
                                 .Include(p => p.ProductPricings).ThenInclude(pr => pr.PricingRanges)
                                 .SingleOrDefaultAsync();

                if (dbProd == null)
                {
                    product.DateCreated  = DateTime.UtcNow;
                    product.DateModified = null;
                    await _context.Products.AddAsync(product);
                }
                else
                {
                    product.DateModified = DateTime.UtcNow;
                    // delete all ingredients that no longer exists
                    foreach (ProductIngredient dbIngredient in dbProd.ParentIngredients)
                    {
                        if (product.ParentIngredients.All(pi => pi.ParentProductId != dbIngredient.ParentProductId && pi.ChildProductId != dbIngredient.ChildProductId))
                        {
                            _context.Set <ProductIngredient>().Remove(dbIngredient);
                        }
                    }
                    foreach (ProductPricing dbPricing in dbProd.ProductPricings)
                    {
                        if (product.ProductPricings.All(pp => pp.Id != dbPricing.Id))
                        {
                            _context.Set <ProductPricing>().Remove(dbPricing);
                        }
                    }
                    // copy current (incoming) values to db
                    _context.Entry(dbProd).CurrentValues.SetValues(product);
                    var ingredientPairs = from curr in product.ParentIngredients //.Select(pi => pi.IngredientProduct)
                                          join db in dbProd.ParentIngredients    //.Select(pi => pi.IngredientProduct)
                                          on new { curr.ParentProductId, curr.ChildProductId } equals
                    new { db.ParentProductId, db.ChildProductId } into grp
                    from db in grp.DefaultIfEmpty()
                    select new { curr, db };
                    foreach (var pair in ingredientPairs)
                    {
                        if (pair.db != null)
                        {
                            _context.Entry(pair.db).CurrentValues.SetValues(pair.curr);
                            _context.Set <ProductIngredient>().Update(pair.db);
                        }
                        else
                        {
                            await _context.Set <ProductIngredient>().AddAsync(pair.curr);
                        }
                    }
                    var pricingPairs = from curr in product.ProductPricings //.Select(pi => pi.IngredientProduct)
                                       join db in dbProd.ProductPricings    //.Select(pi => pi.IngredientProduct)
                                       on curr.Id equals db.Id into grp
                                       from db in grp.DefaultIfEmpty()
                                       select new { curr, db };
                    foreach (var pair in pricingPairs)
                    {
                        if (pair.db != null)
                        {
                            _context.Entry(pair.db).CurrentValues.SetValues(pair.curr);
                            _context.Set <ProductPricing>().Update(pair.db);
                        }
                        else
                        {
                            await _context.Set <ProductPricing>().AddAsync(pair.curr);
                        }
                    }
                }
            }
            await _context.SaveChangesAsync();

            dto = _mapper.Map <ProductDto>(product);
            return(dto);
        }