List<Subscription> subscriptions = handlerContext.getSubscriptions();
Map<Integer, Invoice> invoiceMap = new HashMap<Integer, Invoice>();
for (Subscription subscription : subscriptions) {
Customer customer = subscription.getCustomer();
Invoice invoice = customer.getActiveInvoice();
if (invoiceMap.get(customer.getId()) == null) {
invoiceMap.put(customer.getId(), invoice);
}
// if the activeUtil date is passed, we are making them inactive
/*if (subscription.getActiveUntil().getTime() < System.currentTimeMillis()) {
// we are making the subscription inactive
subscription.setActive(false);
dataAccessObject.updateSubscription(subscription);
}*/
}
// from the invoice set we are calculating the purchase orders
for (Invoice invoice : invoiceMap.values()) {
// save the invoice first
dataAccessObject.addInvoice(invoice);
subscriptions = invoice.getSubscriptions();
for (Subscription subscription : subscriptions) {
// associate the subscription with the invoice.
int invoiceSubscriptionId =
dataAccessObject.addInvoiceSubscription(invoice, subscription);
// now iterate all the items and save it in invoice subscription item space
if (subscription.getItem() != null) {
addInvoiceSubscriptionItem(subscription.getItem(), invoiceSubscriptionId);
}
}
if (invoice.getPayments() != null) {
for (Payment payment : invoice.getPayments()) {
payment.setInvoice(invoice);
dataAccessObject.associatePaymentWithInvoice(payment, invoice);
}
}
}