public async Task <bool> AcceptBillPayment(PaymentDto paymentDto) { var payment = new BillPayment { InmateId = paymentDto.InmateId, Amount = paymentDto.Amount, BillId = paymentDto.BillId.HasValue ? paymentDto.BillId : null, PaidOn = paymentDto.PaidOn ?? DateTime.Now }; var inmate = await _unitOfWork.Repository <Inmate>().FindByIdAsync(paymentDto.InmateId); inmate.AmountDue -= paymentDto.Amount; if (inmate.AmountDue < 0) { inmate.Savings = -1 * inmate.AmountDue; inmate.AmountDue = 0; } var associatedVendorNamesList = new List <string> { VendorNames.Main, VendorNames.BillPaymentAccount }; var vendorSpec = new VendorSpecification(associatedVendorNamesList); var vendors = await _unitOfWork.Repository <Vendor>().FindAllBySpecAsync(vendorSpec); var mainAccount = vendors.FirstOrDefault(v => v.Name == VendorNames.Main); var billPaymentAccount = vendors.FirstOrDefault(v => v.Name == VendorNames.BillPaymentAccount); var transaction = new TransactionDetail { Name = $"Bill Payment-{paymentDto.PaidOn.Value.Month}- {inmate.FullName}", CategoryId = 4, PaidPartyId = billPaymentAccount.Id, PaidToId = mainAccount.Id, Amount = paymentDto.Amount, IsExpense = true, TransactionDate = DateTime.Now }; await _transactionService.PostTransaction(transaction); if (paymentDto.BillId.HasValue && paymentDto.BillId != 0) { var bill = await _unitOfWork.Repository <InmateBill>() .FindOneBySpecAsync(new BillSpecification(paymentDto.BillId.Value)); if (bill != null) { if (bill.BillItems.Any(b => b.ItemCategoryName == BillCategory.DEPOSIT.ToString())) { inmate.Savings += 100; } bill.BillPayment = payment; if (paymentDto.Amount - bill.BillAmount >= 0) { bill.PaymentStatus = PaymentStatus.Paid; } if (paymentDto.Amount < bill.BillAmount) { bill.PaymentStatus = PaymentStatus.PartiallyPaid; } } } else { _unitOfWork.Repository <BillPayment>().Add(payment); } return(await _unitOfWork.Complete() > 0); }
public async Task <bool> DeactivateInmate(DeactivatedInmate deactivatedInmate) { var inmate = await _unitOfWork.Repository <Inmate>().FindByIdAsync(deactivatedInmate.InmateId); inmate.AmountDue -= deactivatedInmate.Amount; if (inmate.AmountDue < 0) { inmate.Savings += inmate.AmountDue; inmate.AmountDue = 0; } if (deactivatedInmate.IsSettlement && inmate.AmountDue == 0) { var associatedVendorNamesList = new List <string> { VendorNames.Main, VendorNames.BillPaymentAccount }; var vendorSpec = new VendorSpecification(associatedVendorNamesList); var vendors = await _unitOfWork.Repository <Vendor>().FindAllBySpecAsync(vendorSpec); var mainAccount = vendors.FirstOrDefault(v => v.Name == VendorNames.Main); var billPaymentAccount = vendors.FirstOrDefault(v => v.Name == VendorNames.BillPaymentAccount); inmate.Status = InmateStatus.Left; _unitOfWork.Repository <DeactivatedInmate>().Add(deactivatedInmate); var settlementTxn = new TransactionDetail { Amount = inmate.Savings, Name = "Settlement - " + inmate.FullName, TransactionDate = DateTime.Now, PaidPartyId = mainAccount.Id, PaidToId = billPaymentAccount.Id, CategoryId = 6 }; inmate.Savings = 0; await _transactionService.PostTransaction(settlementTxn); } var lastDate = deactivatedInmate.LastDate; if (lastDate > DateTime.Now) { var lastDayOfMonth = DateTime.DaysInMonth(lastDate.Year, lastDate.Month); var leaveLastDate = new DateTime(lastDate.Year, lastDate.Month, lastDayOfMonth); var leave = new Leave { InmateId = inmate.Id, FromDate = lastDate, ToDate = leaveLastDate, LeaveReason = "Inmate Left", Status = LeaveStatus.Approved }; _unitOfWork.Repository <Leave>().Add(leave); } await _unitOfWork.Complete(); return(true); }
public async Task <MonthlyBillDto> GenerateMonthlyBillAsync(int month, int year, bool simulated = true) { if (!simulated) { var billSpec = new BillSpecification(month, year); var inmateBillsForMonth = await _unitOfWork.Repository <InmateBill>().FindAllBySpecAsync(billSpec); _unitOfWork.Repository <InmateBill>().RemoveMany(inmateBillsForMonth); var transactionsToDeleteSpec = new TransactionDetailSpecification(month, year, true); var transactionsToDelete = await _unitOfWork.Repository <TransactionDetail>().FindAllBySpecAsync(transactionsToDeleteSpec); _unitOfWork.Repository <TransactionDetail>().RemoveMany(transactionsToDelete); await _unitOfWork.Complete(); } var filter = new TransactionsFilter(month, year, false) { IsAutoGeneratedTxnsNeeded = false }; var spec = new TransactionWithCategoryAndVendorSpecification(filter); var transactions = await _unitOfWork.Repository <TransactionDetail>().FindAllBySpecAsync(spec); var transactionsGrouped = transactions.GroupBy(t => t.CategoryId); var categoricalSum = transactionsGrouped.ToDictionary(billCategory => billCategory.Key, billCategory => billCategory.Sum(b => b.Amount)); var monthlyBill = new MonthlyBillDto(month, year); var categories = await _unitOfWork.Repository <Category>().FindAllAsync(); var categoryWiseExpensesList = new List <CategoryWiseExpense>(); decimal monthlyTotal = 0; var associatedVendorNamesList = new List <string> { VendorNames.Main, VendorNames.Others }; var vendorSpec = new VendorSpecification(associatedVendorNamesList); var vendors = await _unitOfWork.Repository <Vendor>().FindAllBySpecAsync(vendorSpec); var mainAccount = vendors.FirstOrDefault(v => v.Name == VendorNames.Main); var othersAccount = vendors.FirstOrDefault(v => v.Name == VendorNames.Others); foreach (var category in categories) { var categoryWiseExpense = new CategoryWiseExpense { CategoryId = category.Id, CategoryName = category.Name, TotalAmount = category.ConsiderDefaultRate && category.DefaultRate != 0 ? category.DefaultRate : (categoricalSum.ContainsKey(category.Id)?categoricalSum[category.Id]:0), TransactionDetails = transactions .Where(t => t.Category.Name == category.Name) .Select(t => new TransactionDetailDto() { TransactionDetailName = t.Name, Amount = t.Amount }).ToList() }; if (categoryWiseExpense.TransactionDetails == null || categoryWiseExpense.TransactionDetails.Count == 0) { if (!simulated && categoryWiseExpense.TotalAmount > 0) { var transaction = new TransactionDetail { Name = categoryWiseExpense.CategoryName, CategoryId = category.Id, PaidPartyId = mainAccount.Id, PaidToId = othersAccount.Id, Amount = categoryWiseExpense.TotalAmount, TransactionDate = new DateTime(year, month, 1), IsExpense = true, IsAutoGenerated = true }; await _transactionService.PostTransaction(transaction); } categoryWiseExpense.TransactionDetails = new List <TransactionDetailDto> { new TransactionDetailDto { TransactionDetailName = categoryWiseExpense.CategoryName, Amount = categoryWiseExpense.TotalAmount } }; } monthlyTotal += categoryWiseExpense.TotalAmount; if (category.Name != BillCategory.DEPOSIT.ToString() && category.Name != BillCategory.BILLPAYMENT.ToString()) { categoryWiseExpensesList.Add(categoryWiseExpense); } } monthlyBill.CategoryWiseExpenses = categoryWiseExpensesList; monthlyBill.SubTotal = monthlyTotal; return(monthlyBill); }