Esempio n. 1
0
        public async Task <IEnumerable <FeesReportDto> > GetFeesReport(FeesReportsFiltersDto filtersDto)
        {
            List <FeesReportDto> feesReports = new List <FeesReportDto>();

            var sales = (await _saleRepositoryRepository
                         .Find(x => x.PaymentType == ePaymentTypes.OwnFees, x => x.Include(s => s.Details))
                         ).AsQueryable().Where(filtersDto.GetExpresion()).OrderBy(x => x.Date);

            foreach (var sale in sales)
            {
                FeesReportDto feesReport = new FeesReportDto();
                feesReport.Date       = sale.Date;
                feesReport.ClientId   = sale.ClientId;
                feesReport.ClientName = sale.ClientName;

                var ownFee = (await _ownFeesRepository.Find(x => x.SaleId == sale.Id, x => x.Include(s => s.FeeList))).FirstOrDefault();
                feesReport.FeesQty     = ownFee.Quantity;
                feesReport.TotalAmount = ownFee.Amount;
                feesReport.FeeValue    = ownFee.FeeList.OrderBy(x => x.ExpirationDate).FirstOrDefault().Value;

                decimal percentage = 0;
                foreach (var detail in sale.Details)
                {
                    if (!detail.FeeRuleId.HasValue)
                    {
                        throw new InvalidOperationException("FeeRuleId is empty");
                    }

                    var rule = await _feeRuleRepository.GetById(detail.FeeRuleId.Value);

                    if (rule == null)
                    {
                        rule = (await _feeRuleRepository.FindDeleted(x => x.Id == detail.FeeRuleId.Value)).FirstOrDefault();
                    }
                    if (rule == null)
                    {
                        throw new KeyNotFoundException($"Fee Rule with id: {detail.FeeRuleId.Value} not found.");
                    }

                    percentage += rule.Percentage;
                }

                feesReport.Interest = feesReport.FeeValue * (percentage * ownFee.Quantity / 100);
                feesReport.Capital  = feesReport.FeeValue - feesReport.Interest;

                feesReports.Add(feesReport);
            }

            return(feesReports);
        }
Esempio n. 2
0
        private async Task <bool> IsDebtor(ClientDto clientDto)
        {
            var ownFeesIds = (await _saleRepository
                              .Find(
                                  x => x.ClientId == clientDto.Id &&
                                  x.PaymentType == Util.Enums.ePaymentTypes.OwnFees,
                                  x => x.Include(s => s.Payment)
                                  )
                              )
                             .Select(x => x.PaymentId);

            var result = (await _ownFeesRepository.Find(x => ownFeesIds.Contains(x.Id), x => x.Include(ow => ow.FeeList))).Select(x => x.FeeList)
                         .Where(
                x => x.Any(
                    ow => ow.PaymentDate == null &&
                    ow.ExpirationDate.ToLocalTime() <= DateTime.Now.ToLocalTime()
                    )
                )
                         .Count() > 0;

            return(result);
        }