You are here

function commerce_invoice_form in Commerce Invoice 7.2

Entity API form callback for an invoice.

Parameters

array $form:

array &$form_state:

\Drupal\commerce_invoice\Entity\Invoice $invoice:

string $op:

Return value

array

File

./commerce_invoice.admin.inc, line 111
Administrative form and page callbacks for the Commerce Invoice module.

Code

function commerce_invoice_form($form, &$form_state, Invoice $invoice, $op = 'edit') {
  $wrapper = $invoice
    ->wrapper();
  if (!empty($invoice->order_id)) {
    $form['order_id'] = array(
      '#type' => 'item',
      '#title' => t('Order'),
      '#markup' => l($wrapper->order->order_number
        ->value(), 'admin/commerce/orders/' . $wrapper->order
        ->getIdentifier()),
      '#weight' => -100,
    );
    if (isset($invoice->order_revision_id)) {
      $form['order_id']['#markup'] .= ' ' . t('(revision ID: @id)', [
        '@id' => $invoice->order_revision_id,
      ]);
    }
  }
  $form['invoice_status'] = array(
    '#type' => 'select',
    '#options' => commerce_invoice_statuses(),
    '#default_value' => $invoice->invoice_status,
    '#required' => TRUE,
    '#title' => t('Invoice status'),
    '#weight' => -80,
  );
  $form['invoice_date'] = array(
    '#type' => 'date_select',
    '#weight' => -61,
    '#title' => t('Invoice date'),
    '#date_format' => 'Y-m-d',
    '#default_value' => date('Y-m-d H:i:s', !empty($invoice->invoice_date) ? $invoice->invoice_date : REQUEST_TIME),
  );
  $net_s = variable_get('commerce_invoice_net_d', 30) * 86400;
  $form['invoice_due'] = array(
    '#type' => 'date_select',
    '#weight' => -60,
    '#title' => t('Invoice due'),
    '#date_format' => 'Y-m-d',
    '#default_value' => date('Y-m-d H:i:s', !empty($invoice->invoice_due) ? $invoice->invoice_due : REQUEST_TIME + $net_s),
  );
  field_attach_form('commerce_invoice', $invoice, $form, $form_state);

  // Hide the invoice total field from direct editing.
  $form['commerce_invoice_total']['#access'] = FALSE;
  $form['additional_settings'] = [
    '#type' => 'vertical_tabs',
    '#weight' => 99,
  ];
  $form['invoice_number'] = [
    '#type' => 'fieldset',
    '#title' => t('Invoice number'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#group' => 'additional_settings',
    '#weight' => -70,
  ];
  $form['invoice_number']['number_pattern'] = [
    '#title' => t('Invoice number pattern'),
    '#type' => 'select',
    '#options' => commerce_invoice_number_pattern_options_list(),
    '#default_value' => $invoice->number_pattern ?: \Drupal\commerce_invoice\Entity\InvoiceNumberPattern::getDefaultName(),
    '#description' => t('The pattern used to generate the invoice number (<a href="@url">administer patterns</a>).', [
      '@url' => url('admin/commerce/config/invoice/numbers'),
    ]),
    '#disabled' => empty($invoice->is_new),
    '#required' => TRUE,
  ];
  $form['user'] = [
    '#type' => 'fieldset',
    '#title' => t('User information'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#group' => 'additional_settings',
    '#weight' => -60,
    '#attached' => array(
      'js' => array(
        drupal_get_path('module', 'commerce_invoice') . '/commerce_invoice.js',
        array(
          'type' => 'setting',
          'data' => array(
            'anonymous' => variable_get('anonymous', t('Anonymous')),
          ),
        ),
      ),
    ),
  ];
  $form['user']['owner'] = array(
    '#type' => 'textfield',
    '#weight' => 90,
    '#autocomplete_path' => 'user/autocomplete',
    '#default_value' => !empty($invoice->uid) ? $wrapper->owner->name
      ->raw() : '',
    '#title' => t('Owner'),
    '#description' => t('If left empty, this will default to the order owner.'),
  );
  $form['actions'] = array(
    '#type' => 'actions',
    '#weight' => 100,
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  if (!empty($invoice->invoice_id)) {
    $form['actions']['delete'] = array(
      '#type' => 'link',
      '#title' => t('Delete'),
      '#href' => 'admin/commerce/invoices/' . $invoice->invoice_id . '/delete',
    );
  }
  return $form;
}