@POST @Produces(APPLICATION_JSON) @Consumes(APPLICATION_JSON) @Path("/{accountId:" + UUID_PATTERN + "}/" + PAYMENTS) public Response payAllInvoices( @PathParam("accountId") final String accountId, @QueryParam(QUERY_PAYMENT_EXTERNAL) @DefaultValue("false") final Boolean externalPayment, @HeaderParam(HDR_CREATED_BY) final String createdBy, @HeaderParam(HDR_REASON) final String reason, @HeaderParam(HDR_COMMENT) final String comment, @javax.ws.rs.core.Context final HttpServletRequest request) throws AccountApiException, PaymentApiException { final CallContext callContext = context.createContext(createdBy, reason, comment, request); final Account account = accountUserApi.getAccountById(UUID.fromString(accountId), callContext); final Collection<Invoice> unpaidInvoices = invoiceApi.getUnpaidInvoicesByAccountId(account.getId(), clock.getUTCToday(), callContext); for (final Invoice invoice : unpaidInvoices) { if (externalPayment) { paymentApi.createExternalPayment( account, invoice.getId(), invoice.getBalance(), callContext); } else { paymentApi.createPayment(account, invoice.getId(), invoice.getBalance(), callContext); } } return Response.status(Status.OK).build(); }
@GET @Path("/{accountId:" + UUID_PATTERN + "}/" + INVOICES) @Produces(APPLICATION_JSON) public Response getInvoices( @PathParam("accountId") final String accountId, @QueryParam(QUERY_INVOICE_WITH_ITEMS) @DefaultValue("false") final boolean withItems, @QueryParam(QUERY_UNPAID_INVOICES_ONLY) @DefaultValue("false") final boolean unpaidInvoicesOnly, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @javax.ws.rs.core.Context final HttpServletRequest request) throws AccountApiException { final TenantContext tenantContext = context.createContext(request); // Verify the account exists accountUserApi.getAccountById(UUID.fromString(accountId), tenantContext); final List<Invoice> invoices = unpaidInvoicesOnly ? new ArrayList<Invoice>( invoiceApi.getUnpaidInvoicesByAccountId( UUID.fromString(accountId), null, tenantContext)) : invoiceApi.getInvoicesByAccount(UUID.fromString(accountId), tenantContext); final AuditLogsForInvoices invoicesAuditLogs = auditUserApi.getAuditLogsForInvoices(invoices, auditMode.getLevel(), tenantContext); if (withItems) { final List<InvoiceJson> result = new LinkedList<InvoiceJson>(); for (final Invoice invoice : invoices) { result.add( new InvoiceJson( invoice, invoicesAuditLogs.getInvoiceAuditLogs().get(invoice.getId()), invoicesAuditLogs.getInvoiceItemsAuditLogs())); } return Response.status(Status.OK).entity(result).build(); } else { final List<InvoiceJson> result = new LinkedList<InvoiceJson>(); for (final Invoice invoice : invoices) { result.add( new InvoiceJson(invoice, invoicesAuditLogs.getInvoiceAuditLogs().get(invoice.getId()))); } return Response.status(Status.OK).entity(result).build(); } }
@Override public AuditLogsForInvoices getAuditLogsForInvoices( final List<Invoice> invoices, final AuditLevel auditLevel, final TenantContext context) { final Map<UUID, List<AuditLog>> invoiceAuditLogs = new HashMap<UUID, List<AuditLog>>(); final Map<UUID, List<AuditLog>> invoiceItemsAuditLogs = new HashMap<UUID, List<AuditLog>>(); for (final Invoice invoice : invoices) { invoiceAuditLogs.put( invoice.getId(), getAuditLogs(invoice.getId(), ObjectType.INVOICE, auditLevel, context)); for (final InvoiceItem invoiceItem : invoice.getInvoiceItems()) { invoiceItemsAuditLogs.put( invoiceItem.getId(), getAuditLogs(invoiceItem.getId(), ObjectType.INVOICE_ITEM, auditLevel, context)); } } return new DefaultAuditLogsForInvoices(invoiceAuditLogs, invoiceItemsAuditLogs); }
@POST @Path("/{accountId:" + UUID_PATTERN + "}/" + PAYMENT_METHODS) @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) public Response createPaymentMethod( final PaymentMethodJson json, @PathParam("accountId") final String accountId, @QueryParam(QUERY_PAYMENT_METHOD_IS_DEFAULT) @DefaultValue("false") final Boolean isDefault, @QueryParam(QUERY_PAY_ALL_UNPAID_INVOICES) @DefaultValue("false") final Boolean payAllUnpaidInvoices, @HeaderParam(HDR_CREATED_BY) final String createdBy, @HeaderParam(HDR_REASON) final String reason, @HeaderParam(HDR_COMMENT) final String comment, @javax.ws.rs.core.Context final UriInfo uriInfo, @javax.ws.rs.core.Context final HttpServletRequest request) throws AccountApiException, PaymentApiException { final CallContext callContext = context.createContext(createdBy, reason, comment, request); final PaymentMethod data = json.toPaymentMethod(accountId); final Account account = accountUserApi.getAccountById(data.getAccountId(), callContext); final boolean hasDefaultPaymentMethod = account.getPaymentMethodId() != null || isDefault; final Collection<Invoice> unpaidInvoices = payAllUnpaidInvoices ? invoiceApi.getUnpaidInvoicesByAccountId( account.getId(), clock.getUTCToday(), callContext) : Collections.<Invoice>emptyList(); if (payAllUnpaidInvoices && unpaidInvoices.size() > 0 && !hasDefaultPaymentMethod) { return Response.status(Status.BAD_REQUEST).build(); } final UUID paymentMethodId = paymentApi.addPaymentMethod( data.getPluginName(), account, isDefault, data.getPluginDetail(), callContext); if (payAllUnpaidInvoices && unpaidInvoices.size() > 0) { for (final Invoice invoice : unpaidInvoices) { paymentApi.createPayment(account, invoice.getId(), invoice.getBalance(), callContext); } } return uriBuilder.buildResponse( PaymentMethodResource.class, "getPaymentMethod", paymentMethodId, uriInfo.getBaseUri().toString()); }
@PUT @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) @Path( "/{accountId:" + UUID_PATTERN + "}/" + PAYMENT_METHODS + "/{paymentMethodId:" + UUID_PATTERN + "}/" + PAYMENT_METHODS_DEFAULT_PATH_POSTFIX) public Response setDefaultPaymentMethod( @PathParam("accountId") final String accountId, @PathParam("paymentMethodId") final String paymentMethodId, @QueryParam(QUERY_PAY_ALL_UNPAID_INVOICES) @DefaultValue("false") final Boolean payAllUnpaidInvoices, @HeaderParam(HDR_CREATED_BY) final String createdBy, @HeaderParam(HDR_REASON) final String reason, @HeaderParam(HDR_COMMENT) final String comment, @javax.ws.rs.core.Context final HttpServletRequest request) throws AccountApiException, PaymentApiException { final CallContext callContext = context.createContext(createdBy, reason, comment, request); final Account account = accountUserApi.getAccountById(UUID.fromString(accountId), callContext); paymentApi.setDefaultPaymentMethod(account, UUID.fromString(paymentMethodId), callContext); if (payAllUnpaidInvoices) { final Collection<Invoice> unpaidInvoices = invoiceApi.getUnpaidInvoicesByAccountId( account.getId(), clock.getUTCToday(), callContext); for (final Invoice invoice : unpaidInvoices) { paymentApi.createPayment(account, invoice.getId(), invoice.getBalance(), callContext); } } return Response.status(Status.OK).build(); }