Ejemplo n.º 1
0
        public async Task <ProductEntryLineDto> Create(Guid userId, ProductEntryLineForCreationDto productEntryLineForCreationDto)
        {
            var product = await _productRepository.GetById(productEntryLineForCreationDto.ProductId);

            if (product == null)
            {
                throw new KeyNotFoundException($"Product with id: {productEntryLineForCreationDto.ProductId} not found.");
            }

            var productEntry = await _productEntryRepository.GetById(productEntryLineForCreationDto.ProductEntryId.Value);

            if (productEntry == null)
            {
                throw new KeyNotFoundException($"Product Entry with id: {productEntryLineForCreationDto.ProductEntryId.Value} not found.");
            }

            var productEntryLine = new ProductEntryLine()
            {
                Quantity       = productEntryLineForCreationDto.Quantity,
                ProductId      = productEntryLineForCreationDto.ProductId,
                ProductEntryId = productEntryLineForCreationDto.ProductEntryId.Value,
                CreatedBy      = userId
            };

            productEntryLine = await _productEntryLineRepository.Add(productEntryLine);

            product.Stock += productEntryLineForCreationDto.Quantity;
            await _productRepository.Update(product);

            await _productEntryLineRepository.CommitAsync();

            await _productRepository.CommitAsync();

            return(_mapper.Map <ProductEntryLine, ProductEntryLineDto>(productEntryLine));
        }
Ejemplo n.º 2
0
        public async Task <ProductEntryDto> Create(Guid userId, ProductEntryForCreationDto productEntryForCreationDto)
        {
            try
            {
                if (productEntryForCreationDto.IsEntry)
                {
                    var vendor = await _vendorRepository.GetById(productEntryForCreationDto.VendorId.Value);

                    if (vendor == null)
                    {
                        throw new KeyNotFoundException($"Vendor with id: {productEntryForCreationDto.VendorId.Value} not found.");
                    }
                }

                ProductEntry productEntry = new ProductEntry()
                {
                    Date         = productEntryForCreationDto.Date.ToLocalTime(),
                    IsEntry      = productEntryForCreationDto.IsEntry,
                    VendorId     = productEntryForCreationDto.VendorId,
                    Cost         = productEntryForCreationDto.Cost,
                    PaymentType  = productEntryForCreationDto.PaymentType,
                    Observations = productEntryForCreationDto.Observations,
                    CreatedBy    = userId
                };

                productEntry = await _productEntryRepository.Add(productEntry);

                //ProductEntryLines
                foreach (var pelDto in productEntryForCreationDto.ProductEntryLines)
                {
                    var product = await _productRepository.GetById(pelDto.ProductId);

                    if (product == null)
                    {
                        throw new KeyNotFoundException($"Product with id: {pelDto.ProductId} not found.");
                    }

                    var productEntryLine = new ProductEntryLine()
                    {
                        Quantity       = pelDto.Quantity,
                        ProductId      = pelDto.ProductId,
                        ProductEntryId = productEntry.Id,
                        CreatedBy      = userId
                    };

                    productEntryLine = await _productEntryLineRepository.Add(productEntryLine);

                    productEntry.ProductEntryLines.Add(productEntryLine);

                    product.Stock += pelDto.Quantity * (productEntry.IsEntry ? 1 : -1);
                    await _productRepository.Update(product);
                }

                await _productEntryRepository.CommitAsync();

                await _productEntryLineRepository.CommitAsync();

                await _productRepository.CommitAsync();

                var productEntryDto = _mapper.Map <ProductEntry, ProductEntryDto>(productEntry);

                return(productEntryDto);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Ejemplo n.º 3
0
        public async Task Update(Guid id, ProductEntryDto productEntryDto)
        {
            var productEntry = await _productEntryRepository.GetById(productEntryDto.Id, x => x.Include(pe => pe.ProductEntryLines));

            if (productEntry == null)
            {
                throw new KeyNotFoundException($"Product entry with id: {id} not found.");
            }

            //Delete PEL Deleted
            var deleted = productEntry.ProductEntryLines.Where(
                pel => !pel.IsDeleted && !productEntryDto.ProductEntryLines.Any(pelDto => pelDto.Id == pel.Id)
                );

            foreach (var pel in deleted)
            {
                var product = await _productRepository.GetById(pel.ProductId);

                if (product == null)
                {
                    product = (await _productRepository.FindDeleted(x => x.Id == pel.ProductId)).FirstOrDefault();
                }
                if (product == null)
                {
                    throw new KeyNotFoundException($"Product with id: {pel.ProductId} not found.");
                }

                product.Stock -= pel.Quantity * (productEntry.IsEntry ? 1 : -1);
                await _productRepository.Update(product);

                await _productEntryLineRepository.Delete(productEntryDto.LastModificationBy.Value, pel.Id);
            }

            //Add PEL Added
            var added = productEntryDto.ProductEntryLines.Where(x => x.Id == Guid.Empty);

            foreach (var pel in added)
            {
                var product = await _productRepository.GetById(pel.ProductId);

                if (product == null)
                {
                    throw new KeyNotFoundException($"Product with id: {pel.ProductId} not found.");
                }

                var productEntryLine = new ProductEntryLine()
                {
                    Quantity       = pel.Quantity,
                    ProductId      = pel.ProductId,
                    ProductEntryId = productEntryDto.Id,
                    CreatedBy      = productEntryDto.LastModificationBy.Value
                };

                productEntryLine = await _productEntryLineRepository.Add(productEntryLine);

                product.Stock += pel.Quantity * (productEntryDto.IsEntry ? 1 : -1);
                await _productRepository.Update(product);
            }

            //Updated

            //Change Input/Output
            if (productEntry.IsEntry != productEntryDto.IsEntry)
            {
                var changed = productEntry.ProductEntryLines.Where(pel => !pel.IsDeleted && !deleted.Contains(pel));
                foreach (var pel in changed)
                {
                    var product = await _productRepository.GetById(pel.ProductId);

                    if (product == null)
                    {
                        product = (await _productRepository.FindDeleted(x => x.Id == pel.ProductId)).FirstOrDefault();
                    }
                    if (product == null)
                    {
                        throw new KeyNotFoundException($"Product with id: {pel.ProductId} not found.");
                    }

                    product.Stock += pel.Quantity * 2 * (productEntryDto.IsEntry ? 1 : -1);
                    await _productRepository.Update(product);
                }
            }

            Vendor vendor = null;

            if (productEntryDto.IsEntry)
            {
                vendor = await _vendorRepository.GetById(productEntryDto.VendorId.Value);

                if (vendor == null)
                {
                    throw new KeyNotFoundException($"Vendor with id: {productEntryDto.VendorId.Value} not found.");
                }
            }

            productEntry.Date               = productEntryDto.Date.ToLocalTime();
            productEntry.IsEntry            = productEntryDto.IsEntry;
            productEntry.VendorId           = productEntryDto.IsEntry ? productEntryDto.VendorId : null;
            productEntry.Cost               = productEntryDto.IsEntry ? productEntryDto.Cost : null;
            productEntry.PaymentType        = productEntryDto.IsEntry ? productEntryDto.PaymentType : null;
            productEntry.Observations       = productEntryDto.Observations;
            productEntry.LastModificationBy = productEntryDto.LastModificationBy;

            await _productEntryRepository.Update(productEntry);

            await _productEntryRepository.CommitAsync();

            await _productEntryLineRepository.CommitAsync();

            await _productRepository.CommitAsync();
        }