/**
  * Retrieves the invoice that matches the specified id.
  *
  * @param invoiceId The id to match.
  * @return An Invoice instance or null if the id supplied was invalid or could not be matched.
  */
 public FreeAgentInvoice getInvoice(String invoiceId) {
   if (invoiceId != null && !invoiceId.isEmpty()) {
     FreeAgentInvoiceWrapper invoiceWrapper = freeAgentServiceInstance.getInvoice(invoiceId);
     if (invoiceWrapper != null) {
       return invoiceWrapper.getInvoice();
     }
   }
   return null;
 }
 /**
  * Attempts to create a new invoice entry in the associated FreeAgent account.
  *
  * <p>Will return null if the invoice instance provided is null or cannot be saved into the
  * account.
  *
  * @param invoice The populated invoice instance.
  * @return The updated invoice instance or null.
  */
 public FreeAgentInvoice createInvoice(FreeAgentInvoice invoice) {
   if (invoice != null) {
     FreeAgentInvoiceWrapper invoiceWrapper =
         freeAgentServiceInstance.createInvoice(new FreeAgentInvoiceWrapper(invoice));
     if (invoiceWrapper != null) {
       return invoiceWrapper.getInvoice();
     }
   }
   return null;
 }
  /**
   * Returns a list of the invoices contained within FreeAgent for the authorised account.
   *
   * @param nestInvoiceItems Should the invoice items also be included with the invoice
   * @return A list of FreeAgentInvoice instances.
   */
  public List<FreeAgentInvoice> getInvoices(boolean nestInvoiceItems) {
    FreeAgentInvoiceWrapper invoicesWrapper =
        freeAgentServiceInstance.getInvoices(nestInvoiceItems);

    if (invoicesWrapper != null && invoicesWrapper.hasInvoices()) {
      return invoicesWrapper.getInvoices();
    } else {
      return null;
    }
  }
  /**
   * Attempts to import a new invoice into the associated FreeAgent account by deserialising the
   * specified JSON invoice information and requesting a new invoice be created.
   *
   * <p>NOTE: The import (creation within FreeAgent) will only be actioned if no URL property is
   * present or if the URL property is not populated. Otherwise null will be returned.
   *
   * @param invoiceJSON A string containing invoice information in FreeAgent friendly format.
   * @return The newly populated invoice instance that has been imported into FreeAgent or null.
   * @throws JsonSyntaxException If the format does not match the FreeAgent V2 Invoice format.
   */
  public FreeAgentInvoice importInvoice(String invoiceJSON) throws JsonSyntaxException {
    if (invoiceJSON == null || invoiceJSON.isEmpty()) {
      return null;
    }

    FreeAgentInvoice invoice = buildInvoice(invoiceJSON);

    if (invoice != null && (invoice.getUrl() == null || invoice.getUrl().isEmpty())) {
      FreeAgentInvoiceWrapper invoiceWrapper =
          freeAgentServiceInstance.createInvoice(new FreeAgentInvoiceWrapper(invoice));

      if (invoiceWrapper != null) {
        return invoiceWrapper.getInvoice();
      }
    }

    return null;
  }