You are here

function _invoice_api_invoice_get_list in Invoice 7

Handles GET request for a list of invoices

1 call to _invoice_api_invoice_get_list()
invoice_api_invoice in ./invoice_api.inc
GET / PUT / POST / DELETE an invoice

File

./invoice_api.inc, line 253

Code

function _invoice_api_invoice_get_list() {
  $options = array(
    'page' => isset($_GET['page']) ? $_GET['page'] : '',
    // Picked up by PagerDefault
    'max_results' => isset($_GET['max_results']) && $_GET['max_results'] > 0 ? $_GET['max_results'] : 15,
    'sort' => isset($_GET['sort']) && '' != trim($_GET['sort']) ? preg_replace('/[^a-zA-Z0-9\\.]/', '', $_GET['sort']) : 'n.nid',
    'order' => isset($_GET['order']) && in_array($_GET['order'], array(
      'asc',
      'desc',
    )) ? $_GET['order'] : 'desc',
    'template' => isset($_GET['template']) ? $_GET['template'] : '',
    'customer_number' => isset($_GET['customer_number']) ? $_GET['customer_number'] : '',
    'year' => isset($_GET['year']) ? $_GET['year'] : '',
    'month' => isset($_GET['month']) ? $_GET['month'] : '',
    'day' => isset($_GET['day']) ? $_GET['day'] : '',
  );
  $createDates = array();
  if ('' != $options['year'] || '' != $options['month'] || '' != $options['day']) {
    $year = '' != $options['year'] ? $options['year'] : date('Y');
    $month = '' != $options['month'] ? $options['month'] : date('m');
    $startDay = '' != $options['day'] ? $options['day'] : 1;
    $endDay = '' != $options['day'] ? $options['day'] : cal_days_in_month(CAL_GREGORIAN, $month, $year);
    $createStart = new DateTime();
    $createStart
      ->setDate($year, $month, $startDay)
      ->setTime(0, 0, 0);
    $createDates['start'] = $createStart;
    $createEnd = new DateTime();
    $createEnd
      ->setDate($year, $month, $endDay)
      ->setTime(23, 59, 59);
    $createDates['end'] = $createEnd;
  }
  try {
    $query = db_select('invoice_invoices', 'ii')
      ->extend('PagerDefault')
      ->limit($options['max_results']);
    $query
      ->fields('ii', array(
      'iid',
      'nid',
      'leading_zeros',
      'prefix',
      'pay_limit',
      'pay_status',
      'description',
      'uid',
    ));
    $query
      ->fields('c', array(
      'customer_number',
      'company_name',
      'lastname',
      'firstname',
    ));
    $query
      ->fields('n', array(
      'created',
      'changed',
    ));
    $query
      ->addExpression('it.name', 'template');
    $query
      ->leftJoin('node', 'n', 'ii.nid = n.nid');
    $query
      ->leftJoin('invoice_customers', 'c', 'ii.iid = c.invoice_id');
    $query
      ->leftJoin('invoice_templates', 'it', 'ii.tid = it.tid');
    $query
      ->groupBy('ii.iid')
      ->orderBy($options['sort'], strtoupper($options['order']));
    $count_query = db_select('invoice_invoices', 'ii');
    $count_query
      ->leftJoin('node', 'n', 'ii.nid = n.nid');
    $count_query
      ->leftJoin('invoice_customers', 'c', 'ii.iid = c.invoice_id');
    $count_query
      ->leftJoin('invoice_templates', 'it', 'ii.tid = it.tid');
    $count_query
      ->addExpression('COUNT(*)');
    if ('' != $options['template']) {
      $query
        ->condition('it.name', $options['template'], '=');
      $count_query
        ->condition('it.name', $options['template'], '=');
    }
    if ('' != $options['customer_number']) {
      $query
        ->condition('c.customer_number', $options['customer_number'], '=');
      $count_query
        ->condition('c.customer_number', $options['customer_number'], '=');
    }
    if (2 == count($createDates)) {
      $query
        ->condition('n.created', $createDates['start']
        ->getTimestamp(), '>=');
      $query
        ->condition('n.created', $createDates['end']
        ->getTimestamp(), '<=');
      $count_query
        ->condition('n.created', $createDates['start']
        ->getTimestamp(), '>=');
      $count_query
        ->condition('n.created', $createDates['end']
        ->getTimestamp(), '<=');
    }
    $query
      ->setCountQuery($count_query);
    $result = $query
      ->execute();
  } catch (\Exception $e) {
    _invoice_api_http_response_code(500);
    echo json_encode(array(
      'code' => 500,
      'message' => $e
        ->getMessage(),
    ));
    exit;
  }
  $rows = array();
  foreach ($result as $row) {

    // Set locale so money has the right format for the preferred culture
    $locale = _invoice_get_variable($row->template, 'locale');
    if ($locale) {
      setlocale(LC_MONETARY, $locale);
    }

    // Get invoice totals
    $totals = _invoice_get_invoice_totals($row->iid);

    // Add row
    $rows[] = array(
      'id' => (int) $row->iid,
      'number' => _invoice_get_formatted_invoice_number($row->iid, NULL, $row->created),
      'leading_zeros' => (int) $row->leading_zeros,
      'prefix' => $row->prefix,
      'template' => $row->template,
      'description' => $row->description,
      'extotal' => $totals['extotal'],
      'inctotal' => $totals['inctotal'],
      'vattotal' => $totals['vattotal'],
      'formatted_extotal' => _invoice_round_and_format_money($totals['extotal'], 2),
      'formatted_inctotal' => _invoice_round_and_format_money($totals['inctotal'], 2),
      'formatted_vattotal' => _invoice_round_and_format_money($totals['vattotal'], 2),
      'pay_status' => $row->pay_status,
      'pay_limit' => (int) $row->pay_limit,
      'uid' => (int) $row->uid,
      'created' => date('Y-m-d H:i:s', $row->created),
      'changed' => date('Y-m-d H:i:s', $row->changed),
      'customer' => array(
        'customer_number' => $row->customer_number,
        'company_name' => $row->company_name,
        'firstname' => $row->firstname,
        'lastname' => $row->lastname,
      ),
    );
  }
  _invoice_api_http_response_code(200);

  // OK
  echo json_encode($rows);
}