You are here

function invoice_load in Invoice 7

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

Implements hook_load()

Parameters

object|array $node One node or an array with multiple nodes:

Return value

mixed

File

./invoice.module, line 300
Invoice module

Code

function invoice_load($nodes) {
  $multiple = is_array($nodes) ? true : false;
  if (!is_array($nodes)) {
    $nodes = array(
      $nodes,
    );
  }
  foreach ($nodes as $node) {

    // Get all information for this invoice
    $invoice = db_query("SELECT *,ii.iid as invoice_number,ii.leading_zeros AS leading_zeros,\n      ii.prefix AS prefix, ii.description AS invoice_description, ii.pay_limit AS pay_limit,\n      ic.description AS customer_description, t.name as template\n      FROM {invoice_invoices} ii\n      LEFT JOIN {invoice_customers} ic ON ic.invoice_id = ii.iid\n      LEFT JOIN {invoice_templates} t ON ii.tid = t.tid\n      WHERE nid = :nid\n      GROUP BY ii.iid", array(
      ':nid' => $node->nid,
    ))
      ->fetchObject();
    $totals = _invoice_get_invoice_totals($invoice->invoice_number);
    $invoice->extotal = $totals['extotal'];
    $invoice->inctotal = $totals['inctotal'];
    $invoice->vattotal = $totals['vattotal'];

    // Determine template to use
    $template = !empty($invoice->template) ? $invoice->template : variable_get('invoice_default_template', 'default');

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

    // Calculate vat totals per different VAT percentage
    $vattotals = array();
    $result = db_query("SELECT vat, (quantity*unitcost)*((vat / 100) +1) * (1 - (1 / ((vat / 100) +1))) as vattotal\n      FROM {invoice_items} WHERE invoice_id = :invoice_id", array(
      ':invoice_id' => $invoice->invoice_number,
    ))
      ->fetchAll();

    // SUM all vat totals per different VAT percentage
    foreach ($result as $row) {
      if (!isset($vattotals[$row->vat])) {
        $vattotals[$row->vat] = array(
          'vattotal' => $row->vattotal,
        );
      }
      else {
        $vattotals[$row->vat]['vattotal'] += $row->vattotal;
      }
    }

    // Round every total per different VAT percentage
    // and add a formatted version
    foreach ($vattotals as $key => $total) {
      $vattotals[$key]['vattotal'] = _invoice_round($total['vattotal'], 2);
      $vattotals[$key]['formatted_vattotal'] = _invoice_format_money($total['vattotal'], 2);
    }

    // Set totals
    $extotal = _invoice_round($invoice->extotal, 2);
    $inctotal = _invoice_round($invoice->inctotal, 2);
    $vattotal = _invoice_round($invoice->vattotal, 2);

    // Add general invoice information to the node object
    $node->invoice = array(
      'invoice_number' => $invoice->invoice_number,
      'formatted_invoice_number' => _invoice_get_formatted_invoice_number($invoice->invoice_number, NULL, $node->created),
      'invoice_number_zerofill' => $invoice->leading_zeros,
      'invoice_number_prefix' => $invoice->prefix,
      'description' => $invoice->invoice_description,
      'vat' => $vattotals,
      'pay_status' => $invoice->pay_status,
      'pay_limit' => $invoice->pay_limit,
      'template' => $template,
      'vattotal' => $vattotal,
      'extotal' => $extotal,
      'inctotal' => $inctotal,
      'formatted_vattotal' => _invoice_format_money($vattotal, 2),
      'formatted_extotal' => _invoice_format_money($extotal, 2),
      'formatted_inctotal' => _invoice_format_money($inctotal, 2),
      'formatted_created' => format_date($node->created, 'custom', _invoice_get_variable($template, 'date_format')),
    );

    // Add customer information to the node object
    $node->customer = array(
      'cid' => $invoice->cid,
      'customer_number' => $invoice->customer_number,
      'name' => null,
      'company_name' => $invoice->company_name,
      'firstname' => $invoice->firstname,
      'lastname' => $invoice->lastname,
      'fullname' => $invoice->lastname . (!empty($invoice->firstname) ? ', ' . $invoice->firstname : ''),
      'street' => $invoice->street,
      'building_number' => $invoice->building_number,
      'zipcode' => $invoice->zipcode,
      'city' => $invoice->city,
      'state' => $invoice->state,
      'country' => $invoice->country,
      'coc_number' => $invoice->coc_number,
      'vat_number' => $invoice->vat_number,
      'description' => $invoice->customer_description,
    );

    // Add invoices items to the node object
    $node->invoice_items = array();
    $result = db_query("SELECT * FROM {invoice_items}\n      WHERE invoice_id = :invoice_id ORDER BY weight ASC, created ASC, iid ASC", array(
      ':invoice_id' => $invoice->invoice_number,
    ))
      ->fetchAll();
    foreach ($result as $row) {

      // Add invoice item row to the array
      $node->invoice_items[] = array(
        'iid' => $row->iid,
        'weight' => $row->weight,
        'description' => $row->description,
        'quantity' => $row->quantity,
        'unitcost' => $row->unitcost,
        'vat' => $row->vat,
        'formatted_exunitcost' => _invoice_round_and_format_money($row->unitcost, 3),
        'formatted_incunitcost' => _invoice_round_and_format_money($row->unitcost * _invoice_vat_percent_to_decimal($row->vat), 2),
        'formatted_extotal' => _invoice_round_and_format_money($row->quantity * $row->unitcost, 2),
        'formatted_inctotal' => _invoice_round_and_format_money($row->quantity * $row->unitcost * _invoice_vat_percent_to_decimal($row->vat), 2),
      );
    }
  }
  return $multiple ? $nodes : $nodes[0];
}