You are here

function invoice_load in Invoice 6

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

Implementation of hook_load()

Parameters

object $node:

Return value

mixed

File

./invoice.module, line 219
Invoice module

Code

function invoice_load($node) {
  (object) $o_additions;

  // Get all invoices information
  $o_invoice = db_fetch_object(db_query("SELECT *,ii.iid as invoice_number,ii.leading_zeros AS leading_zeros, ii.prefix AS prefix,\n    ii.description AS invoice_description, ii.pay_limit AS pay_limit, 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=%d\n    GROUP BY ii.iid", $node->nid));
  $a_totals = _invoice_get_invoice_totals($o_invoice->invoice_number);
  $o_invoice->extotal = $a_totals['extotal'];
  $o_invoice->inctotal = $a_totals['inctotal'];
  $o_invoice->vattotal = $a_totals['vattotal'];

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

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

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

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

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

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

  // Add general invoice information to the node object
  $o_additions->invoice = array(
    'invoice_number' => $o_invoice->invoice_number,
    'formatted_invoice_number' => _invoice_get_formatted_invoice_number($o_invoice->invoice_number, NULL, $node->created),
    'invoice_number_zerofill' => $o_invoice->leading_zeros,
    'invoice_number_prefix' => $o_invoice->prefix,
    'description' => $o_invoice->invoice_description,
    'vat' => $a_vattotals,
    'pay_status' => $o_invoice->pay_status,
    'pay_limit' => $o_invoice->pay_limit,
    'template' => $template,
    '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
  $o_additions->customer = array(
    'cid' => $o_invoice->cid,
    'customer_number' => $o_invoice->customer_number,
    'name' => $name,
    'company_name' => $o_invoice->company_name,
    'firstname' => $o_invoice->firstname,
    'lastname' => $o_invoice->lastname,
    'fullname' => $o_invoice->lastname . (!empty($o_invoice->firstname) ? ', ' . $o_invoice->firstname : ''),
    'street' => $o_invoice->street,
    'building_number' => $o_invoice->building_number,
    'zipcode' => $o_invoice->zipcode,
    'city' => $o_invoice->city,
    'country' => $o_invoice->country,
    'coc_number' => $o_invoice->coc_number,
    'vat_number' => $o_invoice->vat_number,
    'description' => $o_invoice->customer_description,
  );

  // Add invoices items to the node object
  $o_additions->invoice_items = array();
  $result = db_query("SELECT * FROM {invoice_items} WHERE invoice_id=%d ORDER BY created ASC, weight ASC", $o_invoice->invoice_number);
  while ($row = db_fetch_object($result)) {

    // Add invoice item row to the array
    $o_additions->invoice_items[] = array(
      'iid' => $row->iid,
      '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 $o_additions;
}