public async Task Handle(ReceiptAddedNotification notification)
        {
            Logger.Trace("Handle");

            if (!featureService.IsEnabled(Feature.Restitution))
            {
                return;
            }

            if (notification.Receipt.ReceiptType.Name != ReceiptType.Payroll)
            {
                return;
            }

            if (!(notification.Receipt.Account is ClientAccount))
            {
                return;
            }

            var receipt   = notification.Receipt;
            var account   = receipt.Account as ClientAccount;
            var residency = account.Residency;

            var order = await context.Orders
                        .Where(o => !o.IsSatified)
                        .Where(o => o.OrganizationId == receipt.OrganizationId)
                        .Where(o => o.Residency.ClientId == residency.ClientId)
                        .OrderByDescending(o => o.IsPropertyDamage)
                        .ThenBy(o => o.Created)
                        .FirstOrDefaultAsync();

            if (order != null)
            {
                var restitutionAccount = residency.Accounts.First(a => a.AccountType.Name == AccountType.Restitution);
                var form = new TrasferAmountEditorForm
                {
                    ParentBatchId               = receipt.TransactionBatchId,
                    TriggerTransactionId        = receipt.Id,
                    TriggerTransactionAccountId = receipt.AccountId,
                    FromAccountId               = account.Id,
                    ToAccountId = restitutionAccount.Id,
                    PayeeId     = order.PayeeId,
                    Amount      = GetAmount(receipt.Amount, order.WithholdingPercent)
                };

                var result = await mediator.SendAsync(new TransferAmountCommand(form, new ModelStateDictionary()));

                if (result is FailureResult)
                {
                    throw new RestitutionOrderTransferException(result.Result.ToString());
                }
            }


            await context.SaveChangesAsync();
        }
Exemple #2
0
        public async Task <ActionResult> Create(TrasferAmountEditorForm form)
        {
            Logger.Trace("Create::Post");

            if (ModelState.IsValid)
            {
                var result = await mediator.SendAsync(new TransferAmountCommand(form, ModelState));

                if (result.IsSuccess)
                {
                    return(RedirectToAction("BatchShow", new { Id = result.Result }));
                }

                ModelState.AddModelError("", result.Result.ToString());
            }

            return(View(form));
        }