You are here

function invoice_invoices in Invoice 7

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

Overview of all invoices

5 string references to 'invoice_invoices'
invoice_get_customer_info in ./invoice_ajax.inc
Get customer info
invoice_menu in ./invoice.module
Implements hook_menu()
invoice_search_customer in ./invoice_ajax.inc
Search if the customer already exists
invoice_uninstall in ./invoice.install
Implementation of hook_uninstall()
_invoice_api_invoice_get_list in ./invoice_api.inc
Handles GET request for a list of invoices

File

./invoice.module, line 613
Invoice module

Code

function invoice_invoices() {
  $content = '';

  //Our header defenition
  $header = array(
    array(
      'data' => t('Invoice #'),
      'field' => 'ii.iid',
    ),
    array(
      'data' => t('Customer'),
      'field' => 'c.customer',
    ),
    array(
      'data' => t('Total (ex)'),
      'field' => 'extotal',
    ),
    array(
      'data' => t('Total (inc)'),
      'field' => 'inctotal',
    ),
    array(
      'data' => t('Created'),
      'field' => 'invoices.created',
    ),
    array(
      'data' => _invoice_get_icon('bullet_black', NULL, array(
        'title' => t('sort by @s', array(
          '@s' => t('Pay status'),
        )),
      )),
      'field' => 'ii.pay_status',
    ),
    t('Actions'),
  );
  if (!isset($_GET['order']) || empty($_GET['order'])) {
    $_GET['order'] = t("Invoice #");
    $_GET['sort'] = "desc";
  }
  $query = db_select('invoice_invoices', 'ii')
    ->extend('PagerDefault')
    ->limit(15);
  $query
    ->fields('ii', array(
    'iid',
    'nid',
    'pay_limit',
    'pay_status',
  ));
  $query
    ->fields('c', array(
    'company_name',
    'lastname',
    'firstname',
  ));
  $query
    ->fields('n', array(
    'created',
  ));
  $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('n.nid', 'DESC');
  $count_query = db_select('invoice_invoices', 'ii');
  $count_query
    ->addExpression('COUNT(*)');
  $query
    ->setCountQuery($count_query);
  $result = $query
    ->execute();
  $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 locale settings
    $a_locales = localeconv();

    // Set formatted create date
    $created = format_date($row->created, 'custom', variable_get('invoice_date_format', ''));

    // If no default invoice date format is set, use the small drupal date format
    if (empty($created)) {
      $created = format_date($row->created, 'small');
    }

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

    // Set pay status
    if ($row->pay_status == 'paid') {
      $pay_status = _invoice_get_icon('bullet_green', NULL, array(
        'title' => t('Paid'),
      ));
    }
    else {
      $pay_status = _invoice_get_icon('bullet_yellow', NULL, array(
        'title' => t('Unpaid'),
      ));
    }

    // Check if invoice has pay limit, if yes see if the date exceeded it
    if ($row->pay_status == 'unpaid' && $row->pay_limit > 0) {
      if (mktime(0, 0, 0, date('m'), date('d'), date('Y')) > $row->created + 86400 * $row->pay_limit) {
        $pay_status = _invoice_get_icon('bullet_red', NULL, array(
          'title' => t('Overtime'),
        ));
      }
    }

    // Set user actions
    $actions = '';
    if (_invoice_user_has_admin_access_to_invoice($row->iid)) {
      $deleteIcon = '';
      if ($row->pay_status != 'paid') {
        $deleteIcon = _invoice_get_icon('delete', 'node/' . $row->nid . '/delete', array(
          'title' => t('Delete'),
        ));
      }
      $editIcon = _invoice_get_icon('edit', 'node/' . $row->nid . '/edit', array(
        'title' => t('Edit'),
      ));
      $actions = $editIcon . $deleteIcon;
    }

    // Set admin actions
    if (user_access('administer invoices')) {
      $setPaidIcon = '';
      if ($row->pay_status != 'paid') {
        $setPaidIcon = _invoice_get_icon('accept', 'invoice/set/pay_status/' . $row->iid . '/paid/' . _invoice_getvars_array_to_string($_GET), array(
          'title' => t('Set paid'),
        ));
      }
      $setUnpaidIcon = '';
      if ($row->pay_status == 'paid') {
        $setUnpaidIcon = _invoice_get_icon('coins_delete', 'invoice/set/pay_status/' . $row->iid . '/unpaid/' . _invoice_getvars_array_to_string($_GET), array(
          'title' => t('Set unpaid'),
        ));
      }
      $actions .= $setPaidIcon . $setUnpaidIcon;
    }

    // Set customer
    if (!empty($row->company_name)) {
      $customer = _invoice_get_icon('building') . ' ' . check_plain($row->company_name);
    }
    else {
      $customer = _invoice_get_icon('user') . ' ' . check_plain($row->lastname) . (!empty($row->firstname) ? ', ' . check_plain($row->firstname) : '');
    }

    // Add row
    $rows[] = array(
      'invoices.iid' => l(_invoice_get_formatted_invoice_number($row->iid, NULL, $row->created), 'node/' . $row->nid),
      'customer' => $customer,
      'extotal' => _invoice_round_and_format_money($a_totals['extotal'], 2),
      'inctotal' => _invoice_round_and_format_money($a_totals['inctotal'], 2),
      'invoices.created' => $created,
      'ii.status' => $pay_status,
      'actions' => $actions,
    );
  }
  $variables = array(
    'header' => $header,
    'rows' => $rows,
  );
  $content .= theme('table', $variables);
  $content .= theme('pager');
  return $content;
}