Ejemplo n.º 1
0
        public async Task <IActionResult> Payment()
        {
            var model = new ChargesPaymentModel();
            var units = await _dbContext.Units.AsNoTracking()
                        .Include(u => u.Owner)
                        .OrderBy(u => u.Name)
                        .ToArrayAsync();

            model.Units = units.Select(u => new SelectedUnit {
                Unit = u, UnitId = u.Id, IsSelected = true
            }).ToList();
            model.Charges = await _dbContext.Charges.AsNoTracking()
                            .OrderBy(c => c.Name)
                            .ToListAsync();

            model.Date = DateTime.Now;
            return(View(model));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Payment([FromForm] ChargesPaymentModel model)
        {
            var charge = await _dbContext.Charges.AsNoTracking()
                         .FirstOrDefaultAsync(c => c.Id == model.SelectedChargesId);

            if (charge == null)
            {
                return(NotFound("Charge not found"));
            }
            var selectedUnitIds = model.Units.Where(u => u.IsSelected).Select(u => u.UnitId).ToArray();
            var transactions    = new Transaction[selectedUnitIds.Length];

            for (int i = 0; i < selectedUnitIds.Length; i++)
            {
                var unitId = selectedUnitIds[i];
                var unit   = await _dbContext.Units.Include(u => u.Owner).ThenInclude(u => u.Account).FirstOrDefaultAsync(u => u.Id == unitId);

                if (unit == null)
                {
                    return(NotFound($"{unitId} does not exits."));
                }
                transactions[i] = new Transaction
                {
                    Id        = Guid.NewGuid().ToString(),
                    Date      = model.Date,
                    AccountId = unit.Owner.Account.Id,
                    UnitId    = unit.Id,
                    LedgerId  = charge.LedgerId,
                    IsDue     = true,
                    // PassthroughId
                    Description     = $"{model.Particulars} {charge.Name} Charge {unit.Name} on {unit.Owner.Name}",
                    CreatedByUserOn = DateTime.Now,
                    Amount          = CalculateChargeAmount(charge, unit)
                };
            }

            return(Json(transactions));
        }