You are here

function invoice_form in Invoice 7

Same name and namespace in other branches
  1. 6 invoice_form.inc \invoice_form()

Implements hook_form()

Parameters

object $node:

object $form_state:

Return value

array An array containing the title and any custom form elements to be displayed in the node editing form.

File

./invoice_form.inc, line 21
Invoice module

Code

function invoice_form($node, &$form_state) {
  _invoice_extend_node($node);

  // If an invoice number is available we are in editing mode
  if (!empty($node->invoice['invoice_number'])) {
    $mode = 'edit';
  }
  else {
    $mode = 'create';
  }
  $form = array();
  $form['invoice_template'] = array(
    '#type' => 'fieldset',
    '#title' => t('Invoice template'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => 1,
  );

  // Get template names
  $templates = _invoice_get_templates();

  // Build array for selecting the default template
  $template_options = array();
  foreach ($templates as $template) {
    $template_options[$template] = ucfirst($template);
  }
  if (empty($node->invoice['template'])) {
    $active_template = _invoice_get_chosen_template();
  }
  else {
    $active_template = $node->invoice['template'];
  }
  $form['invoice_template']['template'] = array(
    '#type' => 'select',
    '#title' => '',
    '#options' => $template_options,
    '#default_value' => $active_template,
    '#attributes' => empty($node->invoice['template']) ? array(
      'onchange' => 'invoice.setTemplate(this.value)',
    ) : array(),
    '#description' => t("When editing this invoice, you'll have to save first before you can see" . " template changes."),
  );
  if (empty($node->invoice['template'])) {
    $_SESSION['invoice_template'] = _invoice_get_chosen_template();
  }
  $form['customer'] = array(
    '#type' => 'fieldset',
    '#title' => t('Customer details'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#description' => t('You either have to fill in Company name or Lastname. Firstname is optional' . ' and is only saved if a Lastname is filled in.'),
    '#weight' => 2,
  );
  if (!empty($node->customer['company_name'])) {
    $customer_search_default_value = $node->customer['company_name'];
  }
  else {
    $customer_search_default_value = $node->customer['lastname'] . (!empty($node->customer['firstname']) ? ', ' . $node->customer['firstname'] : '');
  }
  $form['customer']['search'] = array(
    '#type' => 'textfield',
    '#title' => t('Search customer'),
    '#maxlength' => 60,
    '#autocomplete_path' => 'invoice/search/customer',
    '#default_value' => $customer_search_default_value,
  );
  $form['customer']['company_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Company name'),
    '#required' => FALSE,
    '#default_value' => $node->customer['company_name'],
  );
  $form['customer']['firstname'] = array(
    '#type' => 'textfield',
    '#title' => t('Firstname'),
    '#required' => FALSE,
    '#default_value' => $node->customer['firstname'],
  );
  $form['customer']['lastname'] = array(
    '#type' => 'textfield',
    '#title' => t('Lastname'),
    '#required' => FALSE,
    '#default_value' => $node->customer['lastname'],
  );
  $invoice_items_table_header = array(
    t('Description'),
    t('VAT'),
    t('Count'),
    t('Unitcost (ex. VAT)'),
    t('Unitcost (inc. VAT)'),
    t('Subtotal (ex. VAT)'),
    t('Subtotal (inc. VAT)'),
    '',
  );
  $form['invoice_items'] = array(
    '#type' => 'fieldset',
    '#title' => t('Invoice items'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => 4,
  );

  // Set locale so money has the right format for the preferred culture
  if ((int) $node->invoice['invoice_number'] === 0) {
    $locale = _invoice_get_variable(_invoice_get_chosen_template(), 'locale');
    if ($locale) {
      setlocale(LC_MONETARY, $locale);
    }
  }
  else {
    $locale = _invoice_get_variable($active_template, 'locale');
    if ($locale) {
      setlocale(LC_MONETARY, $locale);
    }
  }

  // Get invoice items
  $items = _invoice_get_invoice_items($node->invoice['invoice_number']);
  $count = $items['count'];
  $invoice_items_table_rows = $items['rows'];
  $totals = array(
    'extotal' => null,
    'inctotal' => null,
    'vattotal' => null,
  );

  // If now rows are found add an empty row
  if ($count == 0) {
    $invoice_items_table_rows = array(
      array(
        'data' => array(
          array(
            'data' => t('Empty') . '...',
            'colspan' => '8',
          ),
        ),
        'class' => array(
          'class' => 'invoice-items-empty',
        ),
      ),
    );
  }
  else {

    // Count the added items
    $totals = _invoice_get_invoice_totals((int) $node->invoice['invoice_number'], $GLOBALS['user']->uid);
  }
  $variables = array(
    'header' => $invoice_items_table_header,
    'rows' => $invoice_items_table_rows,
    'sticky' => FALSE,
    'attributes' => array(
      'id' => 'invoice-items-table',
    ),
  );
  $invoice_items_table = theme('table', $variables);
  $invoice_items_table_footer = '<tfoot><tr><td colspan="5"></td><td class="extotal">' . _invoice_round_and_format_money($totals['extotal'], 2) . '</td><td class="inctotal">' . _invoice_round_and_format_money($totals['inctotal'], 2) . '</td><td></td></tr></tfoot>';
  $invoice_items_table = str_replace('</table>', $invoice_items_table_footer . '</table>', $invoice_items_table);
  $form['invoice_items']['items'] = array(
    '#markup' => '<div class="invoice-items">' . $invoice_items_table . '</div>',
  );
  $form['invoice_items']['iid'] = array(
    '#type' => 'hidden',
    '#title' => t('Invoice item id'),
    '#required' => FALSE,
    '#attributes' => array(
      'id' => 'edit-iid',
    ),
  );
  $form['invoice_items']['token'] = array(
    '#type' => 'hidden',
    '#required' => TRUE,
    '#value' => drupal_get_token($GLOBALS['user']->uid),
    '#attributes' => array(
      'id' => 'edit-iitoken',
    ),
  );
  $form['invoice_items']['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#required' => FALSE,
    '#rows' => 1,
  );
  $form['invoice_items']['quantity'] = array(
    '#type' => 'textfield',
    '#title' => t('Quantity'),
    '#required' => FALSE,
    '#size' => 5,
  );
  $form['invoice_items']['price_without_vat'] = array(
    '#type' => 'textfield',
    '#title' => t('Price without VAT'),
    '#required' => FALSE,
    '#description' => t("If you don't fill in this field, you'll have to fill in \"Price with VAT\""),
  );
  $form['invoice_items']['price_with_vat'] = array(
    '#type' => 'textfield',
    '#title' => t('Price with VAT'),
    '#required' => FALSE,
    '#description' => t("If you don't fill in this field, you'll have to fill in \"Price without VAT\""),
  );
  $form['invoice_items']['vat'] = array(
    '#type' => 'textfield',
    '#title' => t('VAT percentage'),
    '#required' => FALSE,
    '#attributes' => array(
      'style' => 'width:40px;',
    ),
    // size attribute didn't work here for some strange reason
    '#default_value' => _invoice_get_variable($active_template, 'vat'),
  );
  $form['invoice_items']['item_actions'] = array(
    '#markup' => '<input type="button" id="button-save-item" name="button_save_item" value="' . t('Add item') . '" />' . ' <input type="button" id="button-cancel-item" value="' . t('Cancel') . '" />',
  );
  $form['customer_optional'] = array(
    '#type' => 'fieldset',
    '#title' => t('Customer details') . ' (' . t('optional') . ')',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#weight' => 3,
  );
  $form['customer_optional']['street'] = array(
    '#type' => 'textfield',
    '#title' => t('Street'),
    '#required' => FALSE,
    '#default_value' => $node->customer['street'],
  );
  $form['customer_optional']['building_number'] = array(
    '#type' => 'textfield',
    '#title' => t('Building number'),
    '#required' => FALSE,
    '#default_value' => $node->customer['building_number'],
  );
  $form['customer_optional']['zipcode'] = array(
    '#type' => 'textfield',
    '#title' => t('Zipcode'),
    '#required' => FALSE,
    '#default_value' => $node->customer['zipcode'],
  );
  $form['customer_optional']['city'] = array(
    '#type' => 'textfield',
    '#title' => t('City'),
    '#required' => FALSE,
    '#default_value' => $node->customer['city'],
  );
  $form['customer_optional']['state'] = array(
    '#type' => 'textfield',
    '#title' => t('State'),
    '#required' => FALSE,
    '#default_value' => $node->customer['state'],
  );
  $form['customer_optional']['country'] = array(
    '#type' => 'textfield',
    '#title' => t('Country'),
    '#required' => FALSE,
    '#default_value' => $node->customer['country'],
  );
  $form['customer_optional']['coc_number'] = array(
    '#type' => 'textfield',
    '#title' => t('Chamber of Commerce number'),
    '#required' => FALSE,
    '#default_value' => $node->customer['coc_number'],
  );
  $form['customer_optional']['vat_number'] = array(
    '#type' => 'textfield',
    '#title' => t('VAT number'),
    '#required' => FALSE,
    '#default_value' => $node->customer['vat_number'],
  );
  $form['customer_optional']['customer_description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#required' => FALSE,
    '#default_value' => $node->customer['description'],
  );
  $form['invoice_details'] = array(
    '#type' => 'fieldset',
    '#title' => t('Invoice details') . ' (' . t('optional') . ')',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#weight' => 5,
  );

  // Only display this form field when creating a node
  if (empty($node->invoice['invoice_number'])) {
    $form['invoice_details']['user_defined_invoice_number'] = array(
      '#type' => 'textfield',
      '#title' => t('User defined invoice number'),
      '#required' => FALSE,
      '#default_value' => '',
      // size attribute didn't work here for some strange reason
      '#attributes' => array(
        'style' => 'width:200px;',
      ),
      '#description' => t('You can define an invoice number here. The number has to be higher than' . ' the latest invoice number though. It also has to be numeric.'),
    );
  }
  $form['invoice_details']['invoice_number'] = array(
    '#type' => 'hidden',
    '#title' => t('Invoice number'),
    '#required' => FALSE,
    '#default_value' => $node->invoice['invoice_number'],
    '#attributes' => array(
      'id' => 'edit-invoice-number',
    ),
  );
  $form['invoice_details']['pay_limit'] = array(
    '#type' => 'textfield',
    '#title' => t('Pay limit'),
    '#required' => FALSE,
    '#default_value' => empty($node->invoice['pay_limit']) ? _invoice_get_variable($active_template, 'pay_limit') : $node->invoice['pay_limit'],
    '#description' => t('Pay limit in days'),
    // size attribute didn't work here for some strange reason
    '#attributes' => array(
      'style' => 'width:40px;',
    ),
  );
  $form['invoice_details']['invoice_description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#required' => FALSE,
    '#default_value' => $node->invoice['description'],
  );
  $form['invoice_details']['invoice_invoice_number_zerofill'] = array(
    '#type' => 'textfield',
    '#title' => t('Invoice number zerofill'),
    '#required' => FALSE,
    '#default_value' => empty($node->invoice['invoice_number_zerofill']) && $mode == 'create' ? variable_get('invoice_invoice_number_zerofill', 0) : $node->invoice['invoice_number_zerofill'],
    '#attributes' => array(
      'style' => 'width:40px;',
    ),
    // size attribute didn't work here for some strange reason
    '#description' => t('If you want an invoice number to be displayed as "0001" fill in 4. If you just want to display invoice number "1" leave/set empty.'),
  );
  $form['invoice_details']['invoice_invoice_number_prefix'] = array(
    '#type' => 'textfield',
    '#title' => t('Invoice number prefix'),
    '#required' => FALSE,
    '#default_value' => empty($node->invoice['invoice_number_prefix']) && $mode == 'create' ? variable_get('invoice_invoice_number_prefix', '') : $node->invoice['invoice_number_prefix'],
    '#attributes' => array(
      'style' => 'width:150px;',
    ),
    // size attribute didn't work here for some strange reason
    '#description' => t('If you want an invoice number to be displayed as "@year0001" fill in "%Y". Fillin 4 in the zerofill field above for extra zero values.', array(
      '@year' => date('Y'),
    )) . ' ' . t('If a new year is reached the numbering will still continue sequentially, so if the year ended with "!year0578", the next year will start with "!next_year0579"', array(
      '!year' => date('Y'),
      '!next_year' => date('Y') + 1,
    )) . ' ' . t('All !date values may be entered here with a "%" sign before it or every other text you like.', array(
      '!date' => l(t('date'), 'http://www.php.net/date', array(
        'absolute' => TRUE,
      )),
    )),
  );
  return $form;
}