package dummyApp.persistence;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import com.google.inject.Injector;
import com.premiumminds.billy.core.services.UID;
import com.premiumminds.billy.core.services.exceptions.DocumentIssuingException;
import com.premiumminds.billy.gin.services.exceptions.ExportServiceException;
import com.premiumminds.billy.portugal.BillyPortugal;
import com.premiumminds.billy.portugal.services.documents.util.PTIssuingParams;
import com.premiumminds.billy.portugal.services.entities.PTBusiness;
import com.premiumminds.billy.portugal.services.entities.PTCreditNote;
import com.premiumminds.billy.portugal.services.entities.PTCustomer;
import com.premiumminds.billy.portugal.services.entities.PTInvoice;
import com.premiumminds.billy.portugal.services.entities.PTProduct;
import com.premiumminds.billy.portugal.services.entities.PTSimpleInvoice;
import com.premiumminds.billy.portugal.services.export.exceptions.SAFTPTExportException;
import com.premiumminds.billy.portugal.services.export.pdf.creditnote.PTCreditNotePDFExportRequest;
import com.premiumminds.billy.portugal.services.export.pdf.creditnote.PTCreditNoteTemplateBundle;
import com.premiumminds.billy.portugal.services.export.pdf.invoice.PTInvoicePDFExportRequest;
import com.premiumminds.billy.portugal.services.export.pdf.invoice.PTInvoiceTemplateBundle;
import com.premiumminds.billy.portugal.services.export.pdf.simpleinvoice.PTSimpleInvoicePDFExportRequest;
import com.premiumminds.billy.portugal.services.export.pdf.simpleinvoice.PTSimpleInvoiceTemplateBundle;

public class Billy {

	public static final String INVOICE_XSL_PATH = "src/main/resources/templates/pt_invoice.xsl";
	public static final String CREDIT_NOTE_XSL_PATH = "src/main/resources/templates/pt_creditnote.xsl";
	public static final String SIMPLE_INVOICE_XSL_PATH = "src/main/resources/templates/pt_simpleinvoice.xsl";
	public static final String LOGO_PATH = "src/main/resources/logoBig.png";
	public Injector injector;
	public BillyPortugal billyPortugal;
	public PTIssuingParams parameters;

	public Billy(Injector injector, BillyPortugal billyPortugal) {
		this.injector = injector;
		this.billyPortugal = billyPortugal;
	}

	public void persistCustomer(PTCustomer.Builder customer) {
		billyPortugal.customers().persistence().create(customer);
	}

	public void persistBusiness(PTBusiness.Builder business) {
		billyPortugal.businesses().persistence().create(business);
	}

	public void persistProduct(PTProduct.Builder product) {
		billyPortugal.products().persistence().create(product);
	}

	public void issueInvoice(PTInvoice.Builder invoice, PTIssuingParams params)
			throws DocumentIssuingException {
		billyPortugal.invoices().issue(invoice, params);
	}

	public void issueSimpleInvoice(PTSimpleInvoice.Builder simpleInvoice,
			PTIssuingParams params) throws DocumentIssuingException {
		billyPortugal.simpleInvoices().issue(simpleInvoice, params);
	}

	public void issueCreditNote(PTCreditNote.Builder creditNote,
			PTIssuingParams params) throws DocumentIssuingException {
		billyPortugal.creditNotes().issue(creditNote, params);
	}

	public void exportSaft(UID appUID, UID businessUID, Date from, Date to)
			throws IOException, SAFTPTExportException {
		billyPortugal.saft().export(appUID, businessUID, "12-NrCertificado",
				from, to);
	}

	public void exportInvoicePDF(UID invoiceUID) throws IOException,
			SAFTPTExportException, ExportServiceException {
		InputStream xsl = new FileInputStream(Billy.INVOICE_XSL_PATH);
		PTInvoiceTemplateBundle bundle = new PTInvoiceTemplateBundle(
				Billy.LOGO_PATH, xsl, "12-NrCertificado");
		billyPortugal.invoices().pdfExport(
				new PTInvoicePDFExportRequest(invoiceUID, bundle));
	}

	public void exportSimpleInvoicePDF(UID invoiceUID) throws IOException,
			SAFTPTExportException, ExportServiceException {
		InputStream xsl = new FileInputStream(Billy.SIMPLE_INVOICE_XSL_PATH);
		PTSimpleInvoiceTemplateBundle bundle = new PTSimpleInvoiceTemplateBundle(
				Billy.LOGO_PATH, xsl, "12-NrCertificado");
		billyPortugal.simpleInvoices().pdfExport(
				new PTSimpleInvoicePDFExportRequest(invoiceUID, bundle));
	}

	public void exportCreditNotePDF(UID invoiceUID) throws IOException,
			SAFTPTExportException, ExportServiceException {
		InputStream xsl = new FileInputStream(Billy.CREDIT_NOTE_XSL_PATH);
		PTCreditNoteTemplateBundle bundle = new PTCreditNoteTemplateBundle(
				Billy.LOGO_PATH, xsl, "12-NrCertificado");
		billyPortugal.creditNotes().pdfExport(
				new PTCreditNotePDFExportRequest(invoiceUID, bundle));
	}
}

@@ -2014,16 +2014,81 @@
 g.png%22;%0A
+%09public static final String RESULT_PATH = %22src/main/resources/%22;%0A
 %09public 
@@ -3437,19 +3437,45 @@
 from, to
+, RESULT_PATH + %22SAFT.xml%22
 );%0A
-
 %09%7D%0A%0A%09pub
@@ -3742,33 +3742,48 @@
 H, xsl, 
-%2212-NrCertificado
+RESULT_PATH + %22Invoice.pdf%22,%22123
 %22);%0A%09%09bi
@@ -4173,33 +4173,54 @@
 H, xsl, 
-%2212-NrCertificado
+RESULT_PATH + %22SimpleInvoice.pdf%22,%22123
 %22);%0A%09%09bi
@@ -4610,33 +4610,53 @@
 H, xsl, 
-%2212-NrCertificado
+ RESULT_PATH + %22CreditNote.pdf%22, %22123
 %22);%0A%09%09bi

update Billy.java