You are here

function invoice_form in Invoice 6

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

Implementatin of node_form()

File

./invoice_form.inc, line 15
Invoice module

Code

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

  //drupal_add_tabledrag('invoice-items-table', 'order', 'sibling', 'invoice-item', $subgroup = NULL, $source = NULL, $hidden = TRUE, $limit = 0);

  // If an invoice number is available we are in editing mode
  if (!empty($form_state->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
  $a_templates = _invoice_get_templates();

  // Build array for selecting the default template
  $a_template_options = array();
  foreach ($a_templates as $s_template) {
    $a_template_options[$s_template] = ucfirst($s_template);
  }
  $active_template = empty($form_state->invoice['template']) ? _invoice_get_chosen_template() : $form_state->invoice['template'];
  $form['invoice_template']['template'] = array(
    '#type' => 'select',
    '#title' => '',
    '#options' => $a_template_options,
    '#default_value' => $active_template,
    '#attributes' => empty($form_state->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($form_state->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,
  );
  $form['customer']['search'] = array(
    '#type' => 'textfield',
    '#title' => t('Search customer'),
    '#maxlength' => 60,
    '#autocomplete_path' => 'invoice/search/customer',
    '#default_value' => !empty($form_state->customer['company_name']) ? $form_state->customer['company_name'] : $form_state->customer['lastname'] . (!empty($form_state->customer['firstname']) ? ', ' . $form_state->customer['firstname'] : ''),
  );
  $form['customer']['company_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Company name'),
    '#required' => FALSE,
    '#default_value' => $form_state->customer['company_name'],
  );
  $form['customer']['firstname'] = array(
    '#type' => 'textfield',
    '#title' => t('Firstname'),
    '#required' => FALSE,
    '#default_value' => $form_state->customer['firstname'],
  );
  $form['customer']['lastname'] = array(
    '#type' => 'textfield',
    '#title' => t('Lastname'),
    '#required' => FALSE,
    '#default_value' => $form_state->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 (intval($form_state->invoice['invoice_number']) == 0) {
    if ($locale = _invoice_get_variable(_invoice_get_chosen_template(), 'locale')) {
      setlocale(LC_MONETARY, $locale);
    }
  }
  elseif ($locale = _invoice_get_variable($active_template, 'locale')) {
    setlocale(LC_MONETARY, $locale);
  }

  // Get invoice items
  $a_items = _invoice_get_invoice_items($form_state->invoice['invoice_number']);
  $count = $a_items['count'];
  $invoice_items_table_rows = $a_items['rows'];

  // 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' => '7',
          ),
        ),
        'class' => 'invoice-items-empty',
      ),
    );
  }
  else {

    // Count the added items
    $a_totals = _invoice_get_invoice_totals($form_state->invoice['invoice_number'], $GLOBALS['user']->uid);
    $total->extotal = $a_totals['extotal'];
    $total->inctotal = $a_totals['inctotal'];
  }
  $invoice_items_table = theme('invoice_table', $invoice_items_table_header, $invoice_items_table_rows, array(
    'id' => 'invoice-items-table',
    'disable_sticky_header' => TRUE,
  ));
  $invoice_items_table_footer = '<tfoot><tr><td colspan="5"></td><td class="extotal">' . _invoice_round_and_format_money($total->extotal, 2) . '</td><td class="inctotal">' . _invoice_round_and_format_money($total->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(
    '#value' => '<div class="invoice-items">' . $invoice_items_table . '</div>',
  );
  $form['invoice_items']['iid'] = array(
    '#type' => 'hidden',
    '#title' => t('Invoice item id'),
    '#required' => FALSE,
  );
  $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' => TRUE,
    '#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(
    '#value' => '<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' => $form_state->customer['street'],
  );
  $form['customer_optional']['building_number'] = array(
    '#type' => 'textfield',
    '#title' => t('Building number'),
    '#required' => FALSE,
    '#default_value' => $form_state->customer['building_number'],
  );
  $form['customer_optional']['zipcode'] = array(
    '#type' => 'textfield',
    '#title' => t('Zipcode'),
    '#required' => FALSE,
    '#default_value' => $form_state->customer['zipcode'],
  );
  $form['customer_optional']['city'] = array(
    '#type' => 'textfield',
    '#title' => t('City'),
    '#required' => FALSE,
    '#default_value' => $form_state->customer['city'],
  );
  $form['customer_optional']['country'] = array(
    '#type' => 'textfield',
    '#title' => t('Country'),
    '#required' => FALSE,
    '#default_value' => $form_state->customer['country'],
  );
  $form['customer_optional']['coc_number'] = array(
    '#type' => 'textfield',
    '#title' => t('Chamber of Commerce number'),
    '#required' => FALSE,
    '#default_value' => $form_state->customer['coc_number'],
  );
  $form['customer_optional']['vat_number'] = array(
    '#type' => 'textfield',
    '#title' => t('VAT number'),
    '#required' => FALSE,
    '#default_value' => $form_state->customer['vat_number'],
  );
  $form['customer_optional']['customer_description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#required' => FALSE,
    '#default_value' => $form_state->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($form_state->invoice['invoice_number'])) {
    $form['invoice_details']['user_defined_invoice_number'] = array(
      '#type' => 'textfield',
      '#title' => t('User defined invoice number'),
      '#required' => FALSE,
      '#default_value' => '',
      '#attributes' => array(
        'style' => 'width:200px;',
      ),
      // size attribute didn't work here for some strange reason
      '#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' => $form_state->invoice['invoice_number'],
  );
  $form['invoice_details']['pay_limit'] = array(
    '#type' => 'textfield',
    '#title' => t('Pay limit'),
    '#required' => FALSE,
    '#default_value' => empty($form_state->invoice['pay_limit']) ? _invoice_get_variable($active_template, 'pay_limit') : $form_state->invoice['pay_limit'],
    '#description' => t('Pay limit in days'),
    '#attributes' => array(
      'style' => 'width:40px;',
    ),
  );
  $form['invoice_details']['invoice_description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#required' => FALSE,
    '#default_value' => $form_state->invoice['description'],
  );
  $form['invoice_details']['invoice_invoice_number_zerofill'] = array(
    '#type' => 'textfield',
    '#title' => t('Invoice number zerofill'),
    '#required' => FALSE,
    '#default_value' => empty($form_state->invoice['invoice_number_zerofill']) && $mode == 'create' ? variable_get('invoice_invoice_number_zerofill', 0) : $form_state->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($form_state->invoice['invoice_number_prefix']) && $mode == 'create' ? variable_get('invoice_invoice_number_prefix', '') : $form_state->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('date', 'http://www.php.net/date', array(
        'absolute' => TRUE,
      )),
    )),
  );
  return $form;
}