}
public Invoice prepareNewInvoice() throws ServiceException {
logger.debug("prepareNewInvoice() - start");
Invoice invoice = null;
if (this.invoiceTemplate != null) {
try {
invoice = invoiceTemplate.clone();
} catch (CloneNotSupportedException e) {
logger.warn("prepareNewInvoice()", e);
}
}
if (invoice == null) {
invoice = new Invoice();
}
invoice.setInvoicePositions(new ArrayList<InvoicePosition>());
invoice.setInvoicePurchasers(new ArrayList<InvoicePurchaser>());
Calendar nowDay = Calendar.getInstance();
nowDay.set(Calendar.HOUR, 0);
nowDay.set(Calendar.MINUTE, 0);
nowDay.set(Calendar.SECOND, 0);
nowDay.set(Calendar.MILLISECOND, 0);
Long curYear = new Long(nowDay.get(Calendar.YEAR));
invoice.setYear(curYear.toString());
int maxInvoiceNumber = this.getMaxInvoiceNumber(curYear.toString());
maxInvoiceNumber++;
Long number = new Long(maxInvoiceNumber);
invoice.setNumber(number);
invoice.setDocumentDate(nowDay.getTime());
invoice.setSoldDate(nowDay.getTime());
PaymentKind pk = null;
if (invoice.getPaymentKindId() != null) {
pk = paymentKindsService.getPaymentKind(invoice.getPaymentKindId());
}
Date paymentDate = null;
if (pk != null) {
invoice.setPaymentKind(pk);
paymentDate = paymentKindsService.calculatePaymentDate(nowDay.getTime(), pk.getId());
invoice.setPaymentDate(paymentDate);
}
recalculateInvoice(invoice);
hrInvoiceNumberService.updateHRInvoiceNumber(invoice);