You are here

class CommerceInvoiceEntityController in Commerce Invoice 7

The controller class for invoices contains methods for the order CRUD operations. The load method is inherited from the default controller.

Hierarchy

Expanded class hierarchy of CommerceInvoiceEntityController

1 string reference to 'CommerceInvoiceEntityController'
commerce_invoice_entity_info in ./commerce_invoice.module
Implements hook_entity_info().

File

includes/commerce_invoice.controller.inc, line 12
The controller for the invoice entity containing the CRUD operations.

View source
class CommerceInvoiceEntityController extends DrupalCommerceEntityController {

  /**
   * Create a default invoice.
   *
   * @param array $values
   *   An array of values to set, keyed by property name
   *
   * @return
   *   An invoice object with all default fields initialized.
   */
  public function create(array $values = array()) {
    return (object) ($values + array(
      'invoice_id' => '',
      'invoice_number' => '',
      'order_id' => 0,
      'uid' => '',
      'created' => '',
      'changed' => '',
      'type' => 'commerce_invoice',
    ));
  }

  /**
   * Saves an invoice.
   *
   * When saving an invoice, the function will automatically create an invoice number
   * based
   *
   * @param $invoice
   *   The full invoice object to save.
   * @param $transaction
   *   An optional transaction object.
   *
   * @return
   *   The saved invoice object.
   */
  public function save($invoice, DatabaseTransaction $transaction = NULL) {
    $transaction = !is_null($transaction) ? $transaction : db_transaction();
    try {
      $invoice->changed = REQUEST_TIME;

      // Inserting new invoice
      if (empty($invoice->invoice_id)) {
        $invoice->created = REQUEST_TIME;
        $invoice->invoice_number = $this
          ->generate_invoice_id($invoice);
        $this
          ->invoke('presave', $invoice);
        $invoice = $this
          ->_save($invoice, $transaction);
        $this
          ->invoke('insert', $invoice);
      }
      else {
        $this
          ->invoke('presave', $invoice);
        $invoice = $this
          ->_save($invoice, $transaction);
        $this
          ->invoke('save', $invoice);
      }
      return $invoice;
    } catch (Exception $e) {
      $transaction
        ->rollback();
      watchdog_exception('commerce_invoice', $e);
      throw $e;
    }
  }

  /**
   * Saves an invoice (helper function)
   *
   * @param $invoice
   *   The full invoice object to save.
   * @param $transaction
   *   An optional transaction object.
   *
   * @return
   *   The saved invoice object.
   */
  private function _save($invoice, DatabaseTransaction $transaction = NULL) {
    if (empty($invoice->invoice_id)) {

      // Save the new invoice
      drupal_write_record('commerce_invoice', $invoice);
      field_attach_insert('commerce_invoice', $invoice);
    }
    else {
      drupal_write_record('commerce_invoice', $invoice, 'invoice_id');
      field_attach_update('commerce_invoice', $invoice);
    }

    // Ignore slave server temporarily to give time for the
    // saved invoice to be propagated to the slave.
    db_ignore_slave();
    return $invoice;
  }

  /**
   * Generates invoice id
   *
   * @param object $invoice The invoice to generate an invoice number for.
   *
   * @return
   *   The generated invoice id
   */
  protected function generate_invoice_id($invoice) {

    // TODO: there is probably a better way to do this
    // Invoice generation method
    $method = variable_get('commerce_invoice_number_method', COMMERCE_INVOICE_METHOD_YEAR);
    if ($method == COMMERCE_INVOICE_METHOD_CALLBACK) {
      $function_name = variable_get('commerce_invoice_number_callback', '');
      if (empty($function_name) || !function_exists($function_name)) {
        drupal_set_message(t('No valid callback defined for invoice number generation.'), 'error');
        return t('no-invoice-number');
      }
      else {
        return $function_name($invoice);
      }
    }

    // Get last invoice created
    $result = db_select('commerce_invoice', 'i')
      ->fields('i')
      ->orderBy('created', 'DESC')
      ->range(0, 1)
      ->execute();
    if ($record = $result
      ->fetchAssoc()) {
      $last_number = $record['invoice_number'];
      switch ($method) {
        case COMMERCE_INVOICE_METHOD_INFINITE:
          $last_id = $record['invoice_id'];
          $id = $last_id + 1;
          $return = str_replace('[invoice_id]', $id, $method);
          break;
        case COMMERCE_INVOICE_METHOD_YEAR:

          // Are we in the same year as the last invoice
          $dash_pos = strpos($last_number, '-');
          $last_year = substr($last_number, 0, $dash_pos);
          if ($last_year == date('Y')) {

            // Get last invoice id
            $last_id = strstr($last_number, '-');
            $last_id = str_replace('-', '', $last_id);

            // Increment invoice id
            $id = $last_id + 1;
            $return = $last_year . '-' . $id;
          }
          else {

            // Reset invoice id to 1
            $return = date('Y') . '-1';
          }
          break;
        case COMMERCE_INVOICE_METHOD_MONTH:
          $parts = explode('-', $last_number);
          $last_year = $parts[0];
          $last_month = $parts[1];
          $last_id = $parts[2];
          $year = date('Y');
          $month = date('m');
          if ($last_year == $year) {
            if ($last_month == $month) {
              $id = $last_id + 1;
            }
            else {
              $id = 1;
            }
          }
          else {
            $id = 1;
          }
          $return = date('Y') . '-' . date('m') . '-' . $id;
          break;
      }
      return $return;
    }
    else {

      // First invoice being generated
      $id = 1;
      $result = str_replace('[invoice_id]', $id, $method);
      return date($result);
    }
  }

  /**
   * Deletes multiple invoices by ID.
   *
   * @param $invoice_ids
   *   An array of invoice IDs to delete.
   * @param $transaction
   *  An optional transaction object.
   *
   * @return
   *   TRUE on success, FALSE otherwise.
   */
  public function delete($invoice_ids, DatabaseTransaction $transaction = NULL) {
    if (!empty($invoice_ids)) {
      $invoices = $this
        ->load($invoice_ids, array());
      $transaction = isset($transaction) ? $transaction : db_transaction();
      try {
        db_delete('commerce_invoice')
          ->condition('invoice_id', $invoice_ids, 'IN')
          ->execute();

        // Reset the cache as soon as the changes have been applied.
        $this
          ->resetCache($invoice_ids);
        foreach ($invoices as $id => $invoice) {
          $this
            ->invoke('delete', $invoice);
        }

        // Ignore slave server temporarily to give time for the
        // saved invoice to be propagated to the slave.
        db_ignore_slave();
      } catch (Exception $e) {
        $transaction
          ->rollback();
        watchdog_exception('commerce_invoice', $e);
        throw $e;
      }

      // Clear the page and block and line_item_load_multiple caches.
      cache_clear_all();
      $this
        ->resetCache();
    }
    return TRUE;
  }

  /**
   * Builds a structured array representing the entity's content.
   *
   * The content built for the entity will vary depending on the $view_mode
   * parameter.
   *
   * @param $entity
   *   An entity object.
   * @param $view_mode
   *   View mode, e.g. 'administrator'
   * @param $langcode
   *   (optional) A language code to use for rendering. Defaults to the global
   *   content language of the current request.
   * @return
   *   The renderable array.
   */
  public function buildContent($invoice, $view_mode = 'administrator', $langcode = NULL, $content = array()) {

    // Load the order this invoice is attached to.
    $order = commerce_order_load($invoice->order_id);
    $order_build_content = entity_build_content('commerce_order', $order, $view_mode, $langcode);
    foreach ($order_build_content as $field => $instance) {
      if (substr($field, 0, 1) != '#' || $field == '#attached') {
        $content[$field] = $instance;
      }
    }
    $content['invoice_number'] = array(
      '#markup' => theme('commerce_invoice_number', array(
        'invoice_number' => $invoice->invoice_number,
        'label' => t('Invoice number:'),
        'invoice' => $invoice,
      )),
    );
    $content['created'] = array(
      '#markup' => theme('commerce_invoice_created', array(
        'created' => format_date($invoice->created, 'short'),
        'label' => t('Date:'),
        'invoice' => $invoice,
      )),
    );
    return parent::buildContent($invoice, $view_mode, $langcode, $content);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommerceInvoiceEntityController::buildContent public function Builds a structured array representing the entity's content. Overrides DrupalCommerceEntityController::buildContent
CommerceInvoiceEntityController::create public function Create a default invoice. Overrides DrupalCommerceEntityController::create
CommerceInvoiceEntityController::delete public function Deletes multiple invoices by ID. Overrides DrupalCommerceEntityController::delete
CommerceInvoiceEntityController::generate_invoice_id protected function Generates invoice id
CommerceInvoiceEntityController::save public function Saves an invoice. Overrides DrupalCommerceEntityController::save
CommerceInvoiceEntityController::_save private function Saves an invoice (helper function)
DrupalCommerceEntityController::$controllerTransaction protected property Stores our transaction object, necessary for pessimistic locking to work.
DrupalCommerceEntityController::$lockedEntities protected property Stores the ids of locked entities, necessary for knowing when to release a lock by committing the transaction.
DrupalCommerceEntityController::$unchangedEntities protected property Stores the ids of unchanged entities, necessary for knowing if we're dealing with unchanged entities before acting on them.
DrupalCommerceEntityController::buildQuery protected function Override of DrupalDefaultEntityController::buildQuery(). Overrides DrupalDefaultEntityController::buildQuery
DrupalCommerceEntityController::export public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::export
DrupalCommerceEntityController::import public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::import
DrupalCommerceEntityController::invoke public function (Internal use) Invokes a hook on behalf of the entity. Overrides EntityAPIControllerInterface::invoke
DrupalCommerceEntityController::isUnchanged public function Implements DrupalCommerceEntityControllerInterface::isUnchanged(). Overrides DrupalCommerceEntityControllerInterface::isUnchanged
DrupalCommerceEntityController::releaseLock protected function Checks the list of tracked locked entities, and if it's empty, commits the transaction in order to remove the acquired locks.
DrupalCommerceEntityController::resetCache public function Implements DrupalEntityControllerInterface::resetCache(). Overrides DrupalDefaultEntityController::resetCache
DrupalCommerceEntityController::view public function Generate an array for rendering the given entities. Overrides EntityAPIControllerInterface::view
DrupalDefaultEntityController::$cache protected property Whether this entity type should use the static cache.
DrupalDefaultEntityController::$entityCache protected property Static cache of entities, keyed by entity ID.
DrupalDefaultEntityController::$entityInfo protected property Array of information about the entity.
DrupalDefaultEntityController::$entityType protected property Entity type for this controller instance.
DrupalDefaultEntityController::$hookLoadArguments protected property Additional arguments to pass to hook_TYPE_load().
DrupalDefaultEntityController::$idKey protected property Name of the entity's ID field in the entity database table.
DrupalDefaultEntityController::$revisionKey protected property Name of entity's revision database table field, if it supports revisions.
DrupalDefaultEntityController::$revisionTable protected property The table that stores revisions, if the entity supports revisions.
DrupalDefaultEntityController::attachLoad protected function Attaches data to entities upon loading. 4
DrupalDefaultEntityController::cacheGet protected function Gets entities from the static cache. 1
DrupalDefaultEntityController::cacheSet protected function Stores entities in the static entity cache.
DrupalDefaultEntityController::cleanIds protected function Ensures integer entity IDs are valid.
DrupalDefaultEntityController::filterId protected function Callback for array_filter that removes non-integer IDs.
DrupalDefaultEntityController::load public function Implements DrupalEntityControllerInterface::load(). Overrides DrupalEntityControllerInterface::load
DrupalDefaultEntityController::__construct public function Constructor: sets basic variables.