You are here

function invoice_settings_form_submit in Invoice 7

Same name and namespace in other branches
  1. 6 invoice.module \invoice_settings_form_submit()

Submit function for the settings form

File

./invoice.module, line 522
Invoice module

Code

function invoice_settings_form_submit(&$form_state, $form) {
  $fv =& $form['values'];

  // Invoice specific settings
  variable_set('invoice_locale', $fv['locale']);
  variable_set('invoice_date_format', $fv['date_format']);
  variable_set('invoice_vat', $fv['vat']);
  variable_set('invoice_pay_limit', $fv['pay_limit']);
  variable_set('invoice_invoice_number_zerofill', $fv['invoice_number_zerofill']);
  variable_set('invoice_invoice_number_prefix', $fv['invoice_number_prefix']);
  variable_set('invoice_default_template', $fv['default_template']);

  // Display columns specific settings
  variable_set('invoice_display_column_vat', $fv['display_column_vat']);
  variable_set('invoice_display_column_exunitcost', $fv['display_column_exunitcost']);
  variable_set('invoice_display_column_incunitcost', $fv['display_column_incunitcost']);
  variable_set('invoice_display_column_extotal', $fv['display_column_extotal']);
  variable_set('invoice_display_column_inctotal', $fv['display_column_inctotal']);

  // Supplier specific settings
  variable_set('invoice_supplier_company_name', $fv['supplier_company_name']);
  variable_set('invoice_supplier_street', $fv['supplier_street']);
  variable_set('invoice_supplier_building_number', $fv['supplier_building_number']);
  variable_set('invoice_supplier_zipcode', $fv['supplier_zipcode']);
  variable_set('invoice_supplier_city', $fv['supplier_city']);
  variable_set('invoice_supplier_state', $fv['supplier_state']);
  variable_set('invoice_supplier_country', $fv['supplier_country']);
  variable_set('invoice_supplier_phone', $fv['supplier_phone']);
  variable_set('invoice_supplier_fax', $fv['supplier_fax']);
  variable_set('invoice_supplier_email', $fv['supplier_email']);
  variable_set('invoice_supplier_web', $fv['supplier_web']);
  variable_set('invoice_supplier_coc_number', $fv['supplier_coc_number']);
  variable_set('invoice_supplier_vat_number', $fv['supplier_vat_number']);

  // API specific settings
  variable_set('invoice_api_allowed_ips', $fv['api_allowed_ips']);
  variable_set('invoice_api_root_username', $fv['api_root_username']);
  $templates = _invoice_get_templates();
  foreach ($templates as $template) {
    $count = db_query("SELECT COUNT(*) FROM {invoice_templates} WHERE name = :name", array(
      ':name' => $template,
    ))
      ->fetchField();
    if ($count == 0) {
      db_insert('invoice_templates')
        ->fields(array(
        'name' => $template,
      ))
        ->execute();
    }

    // Numeric fields in this query will become a zero if nothing is filled in, instead of the
    // overloading system. So numeric fields that are empty will take over the general setting
    // and update that in the database for this template.
    db_update('invoice_templates')
      ->fields(array(
      'locale' => $fv[$template . '_locale'],
      'date_format' => $fv[$template . '_date_format'],
      'vat' => $fv[$template . '_vat'] != '' ? $fv[$template . '_vat'] : $fv['vat'],
      'pay_limit' => $fv[$template . '_pay_limit'] != '' ? $fv[$template . '_pay_limit'] : $fv['pay_limit'],
      'supplier_company_name' => $fv[$template . '_supplier_company_name'],
      'supplier_street' => $fv[$template . '_supplier_street'],
      'supplier_building_number' => $fv[$template . '_supplier_building_number'],
      'supplier_zipcode' => $fv[$template . '_supplier_zipcode'],
      'supplier_city' => $fv[$template . '_supplier_city'],
      'supplier_state' => $fv[$template . '_supplier_state'],
      'supplier_country' => $fv[$template . '_supplier_country'],
      'supplier_phone' => $fv[$template . '_supplier_phone'],
      'supplier_fax' => $fv[$template . '_supplier_fax'],
      'supplier_email' => $fv[$template . '_supplier_email'],
      'supplier_web' => $fv[$template . '_supplier_web'],
      'supplier_coc_number' => $fv[$template . '_supplier_coc_number'],
      'supplier_vat_number' => $fv[$template . '_supplier_vat_number'],
      'api_username' => $fv[$template . '_api_username'],
      // set default value if we deal with a new template
      'display_column_vat' => $count > 0 ? $fv[$template . '_display_column_vat'] : 0,
      // set default value if we deal with a new template
      'display_column_exunitcost' => $count > 0 ? $fv[$template . '_display_column_exunitcost'] : 1,
      // set default value if we deal with a new template
      'display_column_incunitcost' => $count > 0 ? $fv[$template . '_display_column_incunitcost'] : 1,
      // set default value if we deal with a new template
      'display_column_extotal' => $count > 0 ? $fv[$template . '_display_column_extotal'] : 1,
      // set default value if we deal with a new template
      'display_column_inctotal' => $count > 0 ? $fv[$template . '_display_column_inctotal'] : 1,
    ))
      ->condition('name', $template)
      ->execute();
  }
  drupal_set_message(t('Succesfully saved'));
}