public async Task <RetainerResult> SetupRetainer(IOrganizationCustomer customer, Guid projectId, CreateRetainerOptions options) { _logger.LogInformation(GetLogMessage("Customer:{0}; Project:{1}"), customer.CustomerId, projectId); var retVal = new RetainerResult(); var project = await _projects.Queryable() .ForOrganizationCustomer(customer) .Include(x => x.CustomerAccount) .Where(x => x.Id == projectId) .FirstAsync(); var retainer = new ProjectRetainerIntent() { AccountManagerId = project.CustomerAccount.AccountManagerId, ProviderOrganizationId = project.CustomerAccount.AccountManagerOrganizationId, CustomerId = project.CustomerAccount.CustomerId, CustomerOrganizationId = project.CustomerAccount.CustomerOrganizationId, ProjectId = projectId, TopOffAmount = options.TopOffAmount, CurrentBalance = 0 }; var records = await Repository.InsertAsync(retainer, true); if (records > 0) { retVal.Succeeded = true; retVal.RetainerId = projectId; } return(retVal); }
public async Task <RetainerResult> TopOffRetainer(IOrganizationCustomer customer, Guid retainerId, TopoffRetainerOptions options) { _logger.LogInformation(GetLogMessage("Customer:{0}; Organization:{1}; Retainer: {2}"), customer.CustomerId, customer.OrganizationId, retainerId); var retainerResult = new RetainerResult(); var retainer = await Repository.Queryable().Where(x => x.CustomerId == customer.CustomerId && x.CustomerOrganizationId == customer.OrganizationId && x.ProjectId == retainerId) .Include(x => x.Project) .ThenInclude(x => x.Proposal) .ThenInclude(x => x.ProposalAcceptance) .Include(x => x.Credits) .FirstOrDefaultAsync(); var retainerAmount = retainer.TopOffAmount; var currentBalance = retainer.CurrentBalance; var chargeAmount = Math.Min(Math.Max(retainerAmount - currentBalance, 0), retainerAmount); _logger.LogDebug(GetLogMessage("Charge Amount: {0}; Current Balance: {1}; Retainer Amount: {2}"), chargeAmount, currentBalance, chargeAmount); // make the charge for top off amount var records = await Repository.UpdateAsync(retainer, true); _logger.LogDebug(GetLogMessage("{0} Records Updated"), records); if (records > 0) { retainerResult.Succeeded = true; retainerResult.RetainerId = retainerId; retainerResult.CurrentBalance = retainer.CurrentBalance; retainerResult.TopOffAmount = retainer.TopOffAmount; } return(retainerResult); }