/// <summary>
        ///  Returns Amount, Product(...), Changes(...), Participants(...), Discount, ReceiptId, RowId, Cost,
        /// </summary>
        /// <param name="receiptId"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        public async Task <List <DALReceiptRowDTO> > AllReceiptsRows(int receiptId, DateTime time)
        {
            var rows = await RepoDbSet
                       .Include(row => row.Product)
                       .ThenInclude(product => product.ProductName)
                       .ThenInclude(name => name.Translations)
                       .Include(row => row.Product)
                       .ThenInclude(product => product.ProductDescription)
                       .ThenInclude(desc => desc.Translations)
                       .Include(row => row.Product)
                       .ThenInclude(product => product.Prices)
                       .Include(row => row.ReceiptRowChanges)
                       .ThenInclude(receiptRowChange => receiptRowChange.Change)
                       .ThenInclude(change => change.ChangeName)
                       .ThenInclude(name => name.Translations)
                       .Include(row => row.ReceiptRowChanges)
                       .ThenInclude(receiptRowChange => receiptRowChange.Change)
                       .ThenInclude(change => change.Prices)
                       .Include(row => row.RowParticipantLoanRows)
                       .ThenInclude(row => row.Loan)
                       .ThenInclude(loan => loan.LoanTaker)
                       .Where(row => row.ReceiptId == receiptId)
                       .ToListAsync();

            return(rows
                   .Select(row => ReceiptRowMapper.FromDomain(row, time))
                   .Where(dto => dto != null)
                   .ToList());
        }
예제 #2
0
        public async Task <BLLReceiptRowDTO> RemoveRowParticipantAsync(int loanRowId, int userId)
        {
            var loanRow = await Uow.LoanRows.FindAsync(loanRowId);

            if (loanRow == null)
            {
                return(null);
            }
            if (loanRow.Loan?.ReceiptParticipant?.Receipt == null)
            {
                throw new NullReferenceException("Some data not mapped");
            }
            var receipt = loanRow.Loan.ReceiptParticipant.Receipt;

            if (receipt.IsFinalized || receipt.ReceiptManagerId != userId)
            {
                return(null);
            }

            Uow.LoanRows.Remove(loanRow.Id);
            await Uow.SaveChangesAsync();

            var receiptRow = await Uow.ReceiptRows.FindRowAndRelatedDataAsync(loanRow.ReceiptRowId);

            return(ReceiptRowMapper.FromDAL(receiptRow));
        }
        /// <summary>
        /// Returns Amount, Product(...), Changes(...), Participants(...), Discount, ReceiptId, RowId, Cost,
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task <DALReceiptRowDTO> FindRowAndRelatedDataAsync(int id)
        {
            var receiptRow = await RepoDbSet
                             .Include(row => row.Receipt)
                             .Include(row => row.Product)
                             .ThenInclude(product => product.ProductName)
                             .ThenInclude(name => name.Translations)
                             .Include(row => row.Product)
                             .ThenInclude(product => product.ProductDescription)
                             .ThenInclude(desc => desc.Translations)
                             .Include(row => row.Product)
                             .ThenInclude(product => product.Prices)
                             .Include(row => row.ReceiptRowChanges)
                             .ThenInclude(receiptRowChange => receiptRowChange.Change)
                             .ThenInclude(change => change.ChangeName)
                             .ThenInclude(name => name.Translations)
                             .Include(row => row.ReceiptRowChanges)
                             .ThenInclude(receiptRowChange => receiptRowChange.Change)
                             .ThenInclude(change => change.Prices)
                             .Include(row => row.RowParticipantLoanRows)
                             .ThenInclude(row => row.Loan)
                             .ThenInclude(loan => loan.LoanTaker)
                             .FirstOrDefaultAsync(row => row.Id == id);

            if (receiptRow == null)
            {
                return(null);
            }

            return(ReceiptRowMapper.FromDomain(receiptRow, DateTime.Now));
        }
        public async Task <DALReceiptRowDTO> FindAsync(int rowId)
        {
            var row = await RepoDbSet.FindAsync(rowId);

            if (row == null)
            {
                return(null);
            }
            return(ReceiptRowMapper.FromDomain2(row, DateTime.Now));
        }
예제 #5
0
        public async Task <BLLReceiptRowDTO> AddRowChange(int rowId, int changeId, int userId)
        {
            var receiptRow = await Uow.ReceiptRows.FindAsync(rowId);

            if (receiptRow?.ReceiptId == null)
            {
                return(null);
            }
            if (receiptRow.ProductId == null)
            {
                throw new Exception("Product id is null");
            }

            var receipt = await Uow.Receipts.FindReceiptAsync(receiptRow.ReceiptId.Value);

            if (receipt.ReceiptManagerId != userId || receipt.IsFinalized)
            {
                return(null);
            }

            var change = await Uow.Changes.FindDTOAsync(changeId);

            if (change == null)
            {
                return(null);
            }
            if (change.Categories == null)
            {
                throw new Exception("Change categories list is null(not loaded/mapped)");
            }

            var product = await Uow.Products.FindDTOAsync(receiptRow.ProductId.Value);

            if (product?.Categories == null)
            {
                throw new Exception("Product or its categories list is null (not loaded/mapped)");
            }

            var productCategories = product.Categories.Select(dto => dto.Id).ToList();
            var changeCategories  = change.Categories.Select(dto => dto.Id).ToList();

            //Check if product and change have common categories
            if (!productCategories.Intersect(changeCategories).Any())
            {
                return(null);
            }

            await Uow.ReceiptRowChanges.AddAsync(changeId, rowId);

            await Uow.SaveChangesAsync();

            receiptRow = await Uow.ReceiptRows.FindRowAndRelatedDataAsync(rowId);

            return(ReceiptRowMapper.FromDAL(receiptRow));
        }
예제 #6
0
        public async Task <ActionResult <ReceiptRowAllDTO> > RemoveParticipantFromRow(RowParticipantDTO participant)
        {
            if (participant?.LoanRowId == null)
            {
                return(BadRequest());
            }
            var receiptRow = await _bll.ReceiptsService.RemoveRowParticipantAsync(participant.LoanRowId.Value, User.GetUserId());

            if (receiptRow == null)
            {
                return(BadRequest("Participant was not removed (RowParticipantMinDTO or user might not be valid)"));
            }
            return(ReceiptRowMapper.FromBLL(receiptRow));
        }
예제 #7
0
        public async Task <ActionResult <ReceiptRowAllDTO> > AddParticipantToRow(RowParticipantMinDTO dto)
        {
            if (dto == null)
            {
                return(BadRequest("RowParticipantMinDTO is null"));
            }
            var receiptRow = await _bll.ReceiptsService.AddRowParticipantAsync(RowParticipantMapper.FromAPI(dto), User.GetUserId());

            if (receiptRow == null)
            {
                return(BadRequest("Participant was not added (RowParticipantMinDTO or user might not be valid)"));
            }
            return(ReceiptRowMapper.FromBLL(receiptRow));
        }
예제 #8
0
        public async Task <ActionResult <ReceiptRowAllDTO> > UpdateRowAmount(ReceiptRowAmountChangeDTO dto)
        {
            if (dto == null)
            {
                return(BadRequest("DTO missing"));
            }
            var receiptRow = await _bll.ReceiptsService.UpdateRowAmount(ReceiptRowMapper.FromAPI(dto), User.GetUserId());

            if (receiptRow == null)
            {
                return(BadRequest());
            }
            return(ReceiptRowMapper.FromBLL(receiptRow));
        }
        /// <summary>
        /// Adds row
        /// </summary>
        /// <param name="row"></param>
        /// <param name="userId"></param>
        /// <returns>receiptRowId</returns>
        public async Task <int?> AddAsync(DALReceiptRowDTO row)
        {
            var receiptRow = ReceiptRowMapper.FromDAL(row);

            if (receiptRow == null)
            {
                return(null);
            }

            var receiptEntity = (await RepoDbSet.AddAsync(receiptRow)).Entity;

            EntityCreationCache.Add(receiptEntity.Id, receiptEntity);

            return(receiptEntity.Id);
        }
예제 #10
0
        public async Task <BLLReceiptRowDTO> UpdateRowAmount(BLLReceiptRowDTO dto, int userId)
        {
            if (dto?.ReceiptRowId == null || dto.Amount == null)
            {
                return(null);
            }
            var rowId = await Uow.ReceiptRows.UpdateRowAmount(dto.ReceiptRowId.Value, dto.Amount.Value, userId);

            if (rowId == null)
            {
                return(null);
            }
            await Uow.SaveChangesAsync();

            var row = await Uow.ReceiptRows.FindRowAndRelatedDataAsync(rowId.Value);

            return(ReceiptRowMapper.FromDAL(row));
        }
예제 #11
0
        public async Task <ActionResult <ReceiptRowAllDTO> > RemoveComponentFromRow(RowAndChangeDTO dto)
        {
            if (dto.RowId == null)
            {
                return(BadRequest("rowId is missing"));
            }
            if (dto.ComponentId == null)
            {
                return(BadRequest("componentId is missing"));
            }

            var receiptRow = await _bll.ReceiptsService.RemoveRowChangeAsync(dto.RowId.Value, dto.ComponentId.Value, User.GetUserId());

            if (receiptRow == null)
            {
                return(BadRequest("Component was not removed (changeId, rowId or user might not be valid)"));
            }
            return(ReceiptRowMapper.FromBLL(receiptRow));
        }
예제 #12
0
        public async Task <ActionResult <ReceiptRowAllDTO> > AddRow(ReceiptRowMinDTO receiptRowDTO)
        {
            if (receiptRowDTO == null)
            {
                return(BadRequest("DTO missing"));
            }
            var bllDto = ReceiptRowMapper.FromAPI2(receiptRowDTO);

            if (bllDto == null)
            {
                return(BadRequest("Data missing from dto"));
            }

            var receiptRowDto = await _bll.ReceiptsService.AddRow(bllDto, User.GetUserId());

            if (receiptRowDto == null)
            {
                return(BadRequest("Something went wrong while adding"));
            }
            return(ReceiptRowMapper.FromBLL(receiptRowDto));
        }
예제 #13
0
        public async Task <BLLReceiptRowDTO> RemoveRowChangeAsync(int rowId, int changeId, int userId)
        {
            var receiptRow = await Uow.ReceiptRows.FindAsync(rowId);

            if (receiptRow?.ReceiptId == null)
            {
                return(null);
            }

            var receipt = await Uow.Receipts.FindReceiptAsync(receiptRow.ReceiptId.Value);

            if (receipt.ReceiptManagerId != userId || receipt.IsFinalized)
            {
                return(null);
            }

            var change = await Uow.Changes.FindDTOAsync(changeId);

            if (change == null)
            {
                return(null);
            }



            var removedSuccessfully = await Uow.ReceiptRowChanges.RemoveWhereAsync(changeId, rowId);

            if (!removedSuccessfully)
            {
                return(null);
            }
            await Uow.SaveChangesAsync();

            receiptRow = await Uow.ReceiptRows.FindRowAndRelatedDataAsync(rowId);

            return(ReceiptRowMapper.FromDAL(receiptRow));
        }
예제 #14
0
        public async Task <BLLReceiptRowDTO> AddRow(BLLReceiptRowDTO receiptRowDTO, int currentUserId)
        {
            if (receiptRowDTO == null)
            {
                return(null);
            }

            var receiptRow = ReceiptRowMapper.FromBLL(receiptRowDTO);

            if (receiptRow == null)
            {
                return(null);
            }
            if (receiptRow.Amount < 0)
            {
                return(null);
            }
            if (receiptRow.Discount.HasValue && (receiptRow.Discount.Value < 0.0m || receiptRow.Discount.Value > 1.0m))
            {
                return(null);
            }
            if (!receiptRow.ReceiptId.HasValue)
            {
                return(null);
            }
            if (!receiptRow.ProductId.HasValue)
            {
                return(null);
            }

            var receipt = await Uow.Receipts.FindReceiptAsync(receiptRow.ReceiptId.Value);

            var product = await Uow.Products.FindDTOAsync(receiptRow.ProductId.Value);

            if (receipt == null || receipt.IsFinalized || receipt.ReceiptManagerId != currentUserId)
            {
                return(null);
            }
            if (product == null)
            {
                return(null);
            }



            var addedRowId = await Uow.ReceiptRows.AddAsync(receiptRow);

            if (addedRowId == null)
            {
                return(null);
            }

            await Uow.SaveChangesAsync();

            var newId = Uow.ReceiptRows.GetEntityIdAfterSaveChanges(addedRowId.Value);

            if (newId == null)
            {
                return(null);
            }
            var row = await Uow.ReceiptRows.FindRowAndRelatedDataAsync(newId.Value);

            return(ReceiptRowMapper.FromDAL(row));
        }
예제 #15
0
        public async Task <BLLReceiptRowDTO> AddRowParticipantAsync(BLLRowParticipantDTO newParticipant, int currentUserId)
        {
            //Involvement is between 0 and 1
            if (newParticipant.Involvement == null ||
                newParticipant.Involvement > 1 ||
                newParticipant.Involvement <= 0)
            {
                return(null);
            }
            if (newParticipant.AppUserId == null)
            {
                return(null);
            }

            //Participant user exists
            if (!await Uow.AppUsers.Exists(newParticipant.AppUserId.Value))
            {
                return(null);
            }

            var receiptRow = await Uow.ReceiptRows.FindRowAndRelatedDataAsync(newParticipant.ReceiptRowId);

            if (receiptRow?.ReceiptId == null)
            {
                return(null);
            }

            var receipt = await Uow.Receipts.FindReceiptAsync(receiptRow.ReceiptId.Value);


            if (receipt?.ReceiptParticipants == null)
            {
                return(null);
            }
            if (receipt.ReceiptManagerId != currentUserId || receipt.IsFinalized)
            {
                return(null);
            }


            if (receiptRow.Participants != null && receiptRow.Participants.Any())
            {
                //Participant can't already be a participant in this row
                if (receiptRow.Participants.Any(dto => dto.AppUserId == newParticipant.AppUserId))
                {
                    return(null);
                }

                var currentInvolvementSum = receiptRow.Participants.Select(dto => dto.Involvement).Sum();
                //Total involvement can't be greater than 1
                if (currentInvolvementSum == null || currentInvolvementSum + newParticipant.Involvement > 1)
                {
                    return(null);
                }
            }

            var receiptParticipant = await Uow.ReceiptParticipants.FindOrAddAsync(receipt.ReceiptId, newParticipant.AppUserId.Value);

            var loanId = await Uow.Loans.FindOrAddAsync(receiptParticipant, receipt.ReceiptManagerId);

            await Uow.LoanRows.AddAsync(loanId, receiptRow.ReceiptRowId.Value, newParticipant.Involvement.Value);

            await Uow.SaveChangesAsync();

            receiptRow = await Uow.ReceiptRows.FindRowAndRelatedDataAsync(newParticipant.ReceiptRowId);

            return(ReceiptRowMapper.FromDAL(receiptRow));
        }