You are here

function invoice_search_customer in Invoice 7

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

Search if the customer already exists

1 string reference to 'invoice_search_customer'
invoice_menu in ./invoice.module
Implements hook_menu()

File

./invoice_ajax.inc, line 26
Invoice module

Code

function invoice_search_customer($value) {
  $items = array();
  $customer_value = check_plain($value);
  $template = _invoice_get_chosen_template();
  $tid = (int) db_query("SELECT tid FROM {invoice_templates} WHERE name = :name", array(
    ':name' => $template,
  ))
    ->fetchField();
  $query = db_select('invoice_customers', 'ic');
  $query
    ->fields('ic');
  $query
    ->join('invoice_invoices', 'ii', 'ic.invoice_id = ii.iid AND ii.tid = ' . $tid);
  $query
    ->condition(db_or()
    ->condition('company_name', '%' . $customer_value . '%', 'LIKE')
    ->condition('lastname', '%' . $customer_value . '%', 'LIKE')
    ->condition('firstname', '%' . $customer_value . '%', 'LIKE'));
  $query
    ->groupBy('vat_number, coc_number, company_name, country, state, city, zipcode,' . ' building_number, lastname, firstname');
  $query
    ->orderBy('company_name, lastname, firstname, invoice_id', 'DESC');
  $result = $query
    ->execute();
  foreach ($result as $row) {
    $key = $row->cid;
    $value = NULL;
    if (!empty($row->company_name)) {
      $value = $row->company_name;
      $value = !empty($row->zipcode) ? $value . " - " . $row->zipcode : $value;
      $value = !empty($row->building_number) ? $value . " - " . $row->building_number : $value;
      $value = !empty($row->city) ? $value . " - " . $row->city : $value;
      $value = !empty($row->state) ? $value . " - " . $row->state : $value;
      $value = !empty($row->country) ? $value . " - " . $row->country : $value;
      $value = !empty($row->vat_number) ? $value . " - " . $row->vat_number : $value;
      $value = !empty($row->coc_number) ? $value . " - " . $row->coc_number : $value;
      $value = !empty($row->lastname) ? $value . "\n    " . $row->lastname : $value;
      $value = !empty($row->firstname) ? $value . "\n    " . $row->firstname : $value;
    }
    else {
      $value = $row->lastname . (!empty($row->firstname) ? ', ' . $row->firstname : '');
    }
    $items[$key] = check_plain($value);
  }
  drupal_json_output($items);
  exit;
}