XeroAPI / Xero-Java

Official Java client for use with Xero API
MIT License
75 stars 87 forks source link

Uploading a pdf at the same time when creating an invoice #334

Closed arifainchtein closed 1 year ago

arifainchtein commented 1 year ago

SDK you're using (please complete the following information):

Describe the bug I am creating and invoice and want to upload the pdf of the sale invoice

To Reproduce Here is the code i am using. It runs clean without any exceptions, but when i go see the invoice, there is no attachment I have verified that the file exist at the location and is a valid pdf file

`Invoice invoice = new Invoice(); invoice.setType(com.xero.models.accounting.Invoice.TypeEnum.ACCREC); invoice.setContact(contact); SimpleDateFormat invoiceDateFormat = new SimpleDateFormat("yyyy-MM-dd"); String dateValue = invoiceDateFormat.format(new Date()); // System.out.println("line 400, dateValue=" + dateValue); // invoice.setDate(dateValue); YearMonth ym = YearMonth.now( ZoneId.of( "Australia/Melbourne" ) ) ; YearMonth ymNext = ym.plusMonths( 1 ) ; java.time.LocalDate ld = ymNext.atEndOfMonth() ; String dueDateValue = ld.toString() ; // invoice.setDueDate(dueDateValue); // System.out.println("line 407, dueDateValue=" + dueDateValue); invoice.setLineItems(lineItems); invoice.setReference("Customer Order " + customerorder.getInt("id")); invoice.setStatus(com.xero.models.accounting.Invoice.StatusEnum.DRAFT);

    SaleInvoicePDFGenerator aSaleInvoicePDFGenerator = new SaleInvoicePDFGenerator(contact, quote);
    String filename = aSaleInvoicePDFGenerator.process(customerorder);
    System.out.println("line 478, created pdf file " + filename );
    Attachment attachement = new Attachment();
    attachement.setAttachmentID(UUID.randomUUID());
    attachement.setFileName(filename);
    attachement.setIncludeOnline(true);

    invoice.addAttachmentsItem(attachement);
    Invoices invoices = new Invoices();
    invoices.addInvoicesItem(invoice);
    boolean summarizeErrors=false;
    int unitdp=2;
    try {
        Invoices result = accountingApi.createInvoices(access_token, xero_tenant_id, invoices, summarizeErrors, unitdp);
        System.out.println("line 467, invoice created for customerorder " + customerorderid );

    } catch (Exception e) {
        System.err.println("Exception when calling AccountingApi#createInvoices");

        e.printStackTrace();
    }`

Expected behavior The PDF file should be part of the invoice when i go see it in xero

Screenshots

Additional context I am using the demo company

github-actions[bot] commented 1 year ago

PETOSS-283

github-actions[bot] commented 1 year ago

Thanks for raising an issue, a ticket has been created to track your request

arifainchtein commented 1 year ago

sorry, it looks like part of the code (the top) in my bug description was not stylized properly

rdemarco-xero commented 1 year ago

Hi @arifainchtein , thanks for bringing this to our attention. The addAttachmentsItem method should be made private in our SDK since it is only used for unmarshalling attachment details in the GetInvoices call. I'll look into updating this in the future since it can be misleading.

You can only upload an attachment once an invoice is created. You will want to use accountingApi.createInvoiceAttachmentByFileName to add attachments for each invoice created after calling accountingApi.createInvoices.