You are here

function invoice_invoices in Invoice 6

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

Overview of all invoices

3 string references to 'invoice_invoices'
invoice_menu in ./invoice.module
Implementation of hook_menu()
invoice_update_1 in ./invoice.install
Implementation of hook_update_N()
invoice_update_3 in ./invoice.install
Implementation of hook_update_N()

File

./invoice.module, line 498
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 = "SELECT COUNT(*),ii.iid,ii.nid,ii.pay_limit,ii.pay_status,c.company_name,c.lastname,c.firstname,n.created,it.name as template\n    FROM {invoice_invoices} ii\n    LEFT JOIN {node} n ON ii.nid=n.nid\n    LEFT JOIN {invoice_customers} c ON ii.iid=c.invoice_id\n    LEFT JOIN {invoice_templates} it ON ii.tid=it.tid\n    GROUP BY ii.iid\n    ";

  //$query .= tablesort_sql($header);
  $query .= "ORDER BY nid DESC";
  $count_query = "SELECT COUNT(*) FROM {invoice_invoices}";
  $rows = array();
  $result = pager_query($query, 15, 0, $count_query);
  while ($row = db_fetch_object($result)) {

    // Set locale so money has the right format for the preferred culture
    if ($locale = _invoice_get_variable($row->template, '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
    $pay_status = $row->pay_status == 'paid' ? _invoice_get_icon('bullet_green', NULL, array(
      'title' => t('Paid'),
    )) : _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'),
        ));
      }
    }
    $actions = '';
    if (_invoice_user_has_admin_access_to_invoice($row->iid)) {
      $actions = _invoice_get_icon('edit', 'node/' . $row->nid . '/edit', array(
        'title' => t('Edit'),
      )) . ($row->pay_status != 'paid' ? _invoice_get_icon('delete', 'node/' . $row->nid . '/delete', array(
        'title' => t('Delete'),
      )) : '');
    }
    if (user_access('administer invoices')) {
      $actions .= ($row->pay_status != 'paid' ? _invoice_get_icon('accept', 'invoice/set/pay_status/' . $row->iid . '/paid/' . _invoice_getvars_array_to_string($_GET), array(
        'title' => t('Set paid'),
      )) : '') . ($row->pay_status == 'paid' ? _invoice_get_icon('coins_delete', 'invoice/set/pay_status/' . $row->iid . '/unpaid/' . _invoice_getvars_array_to_string($_GET), array(
        'title' => t('Set unpaid'),
      )) : '');
    }

    // 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) : '');
    }
    $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,
    );
  }
  $content .= theme('invoice_table', $header, $rows);
  $content .= theme('pager');
  return $content;
}