You are here

class Generator in Commerce Invoice 7.2

Invoice number generator.

Hierarchy

  • class \Drupal\commerce_invoice\InvoiceNumber\Generator

Expanded class hierarchy of Generator

1 file declares its use of Generator
InvoiceController.php in src/Entity/InvoiceController.php
The entity controller for invoices.

File

src/InvoiceNumber/Generator.php, line 15
Invoice Number Generator

Namespace

Drupal\commerce_invoice\InvoiceNumber
View source
class Generator {

  /** @var string */
  protected $name;

  /** @var string */
  protected $pattern;

  /**
   * Constructor.
   *
   * @param \Drupal\commerce_invoice\Entity\InvoiceNumberPattern $pattern
   */
  public function __construct(InvoiceNumberPattern $pattern) {
    $this->name = $pattern->name;
    $this->pattern = $pattern->pattern;
  }

  /**
   * Returns the key which differentiates sequential numbers.
   *
   * @return string
   */
  protected function getKey(Invoice $invoice = NULL) {
    return token_replace($this->pattern, [
      'commerce_invoice' => $invoice,
    ]);
  }

  /**
   * Returns the next invoice number for the pattern.
   */
  public function getNext(Invoice $invoice = NULL) {
    $key = $this
      ->getKey($invoice);
    return new InvoiceNumber($this
      ->getNextSequence($key), $key, $this->name);
  }

  /**
   * Calculates the next sequential number for this pattern.
   *
   * @param string $key
   *
   * @return int
   */
  protected function getNextSequence($key) {
    $last = $this
      ->getLastSequence($key) ?: 0;
    return $last + 1;
  }

  /**
   * Finds the last invoice number generated for this pattern.
   *
   * @param string $key
   *
   * @return int|FALSE
   *   The last number or FALSE if no previous number is found.
   */
  protected function getLastSequence($key) {
    $query = db_select('commerce_invoice', 'ci')
      ->fields('ci', array(
      'number_sequence',
    ))
      ->condition('number_pattern', $this->name)
      ->condition('number_key', $key)
      ->orderBy('number_sequence', 'DESC')
      ->range(0, 1);
    $last = $query
      ->execute()
      ->fetchField();
    return $last !== FALSE ? (int) $last : FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Generator::$name protected property @var string
Generator::$pattern protected property @var string
Generator::getKey protected function Returns the key which differentiates sequential numbers.
Generator::getLastSequence protected function Finds the last invoice number generated for this pattern.
Generator::getNext public function Returns the next invoice number for the pattern.
Generator::getNextSequence protected function Calculates the next sequential number for this pattern.
Generator::__construct public function Constructor.