You are here

function commerce_invoice_pdf_create in Commerce Invoice 7.2

Create a PDF for an invoice.

Parameters

Invoice $invoice:

bool $create: Whether to create a new file if it doesn't already exist (default: TRUE). You can check whether the file exists already by setting this to FALSE.

bool $recreate: Whether to recreate (replace) the file if it already exists (default: FALSE).

Return value

object|FALSE An object describing the managed file for the PDF (including the 'uri' property), or FALSE on failure.

6 calls to commerce_invoice_pdf_create()
commerce_invoice_mail_pdf_prepare in modules/mail/commerce_invoice_mail.module
Helper function to prepare the invoice mail.
commerce_invoice_pdf_action_create in modules/pdf/commerce_invoice_pdf.rules.inc
Rules action: Create a PDF file for a given invoice entity.
commerce_invoice_pdf_create_worker in modules/pdf/commerce_invoice_pdf.module
commerce_invoice_pdf_deliver in modules/pdf/commerce_invoice_pdf.module
Page callback to generate and deliver a PDF file for an invoice.
commerce_invoice_pdf_download_invoices in modules/pdf/commerce_invoice_pdf.module
Action callback to generate a PDF.

... See full list

File

modules/pdf/commerce_invoice_pdf.module, line 168
The Commerce Invoice PDF module.

Code

function commerce_invoice_pdf_create(Invoice $invoice, $create = TRUE, $recreate = FALSE) {

  // Find existing pdf if it exists.
  $fid = db_select('file_usage', 'f')
    ->fields('f', array(
    'fid',
  ))
    ->condition('module', 'commerce_invoice_pdf')
    ->condition('type', 'commerce_invoice')
    ->condition('id', $invoice->invoice_id)
    ->execute()
    ->fetchField();

  // If file exists and does not need to be recreated, just return it!
  if (!$recreate && !empty($fid)) {
    $file = file_load($fid);
    if ($file && file_exists($file->uri)) {
      return $file;
    }
  }

  // Return false if file should not be created.
  if (!$create) {
    return FALSE;
  }

  // Invoices must not be public! Make sure private file system is configured.
  if (!drupal_realpath('private://')) {
    watchdog('commerce_invoice', 'The private filesystem directory is not configured.', [], WATCHDOG_ERROR);
    return FALSE;
  }
  $dir = 'private://commerce_invoice/' . date('Y-m', $invoice->created);
  $filename = preg_replace('/[^a-z0-9-]+/i', '_', 'invoice_' . $invoice
    ->getInvoiceNumber()) . '.pdf';
  if (!file_prepare_directory($dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
    watchdog('commerce_invoice', 'The directory does not exist or is not writable: @dir', [
      '@dir' => $dir,
    ], WATCHDOG_ERROR);
    return FALSE;
  }
  $data = commerce_invoice_pdf_generate($invoice);

  // file_save_data() cannot be used as it insists on global $user to be owner
  // of the file. In this use case, owner of the invoice must be owner of the
  // pdf.
  if ($uri = file_unmanaged_save_data($data, $dir . '/' . $filename, FILE_EXISTS_REPLACE)) {

    // Create a file object.
    $file = new stdClass();
    $file->fid = empty($fid) ? NULL : $fid;
    $file->uri = $uri;
    $file->filename = drupal_basename($uri);
    $file->filemime = file_get_mimetype($file->uri);
    $file->uid = $invoice->uid;
    $file->status = FILE_STATUS_PERMANENT;
    $file = file_save($file);

    // Increment usage only if the file was not recreated.
    if (empty($fid)) {
      file_usage_add($file, 'commerce_invoice_pdf', 'commerce_invoice', $invoice->invoice_id);
    }
    rules_invoke_all('commerce_invoice_pdf_created', $invoice, $file);
    return $file;
  }
  watchdog('commerce_invoice', 'Failed to create PDF file for invoice @id, path @path', [
    '@id' => $invoice->invoice_id,
    '@path' => $dir . '/' . $filename,
  ], WATCHDOG_ERROR);
  return FALSE;
}