You are here

function _invoice_default_get_template_output in Invoice 6

Same name and namespace in other branches
  1. 7 templates/default.inc \_invoice_default_get_template_output()

Function to create the invoice layout

Documentation available at:

File

templates/default.inc, line 19
Invoice module

Code

function _invoice_default_get_template_output($invoice, $type = NULL) {
  ob_start();

  // Create alias
  $template =& $invoice->invoice['template'];

  // Set full url path to template
  $path_to_template = 'http://' . $_SERVER['HTTP_HOST'] . base_path() . path_to_theme('module', 'invoice') . '/templates';
  if ($type != 'pdf') {
    $s_pagenum = '1/1';
  }
  else {
    $s_pagenum = '';
  }
  $footer_content = '' . _invoice_get_variable($template, 'supplier_company_name') . ' | Acc. 12345, Foobank Foocity | CoC nr. ' . _invoice_get_variable($template, 'supplier_coc_number') . ' | VAT nr. ' . _invoice_get_variable($template, 'supplier_vat_number') . '';
  if ($type == 'pdf') {
    $footer = '<script type="text/php">
                if (isset($pdf)) {
                  // Open the object: all drawing commands will
                  // go to the object instead of the current page
                  $footer = $pdf->open_object();

                  // Get page width and height
                  $w = $pdf->get_width();
                  $h = $pdf->get_height();

                  // Get font
                  $font = Font_Metrics::get_font("arial");

                  // Set RGB line color
                  //$r=233; $g=239; $b=243;
                  $r=164; $g=168; $b=174;

                  // Leave this array alone
                  $a_line_color = array(0 => $r / 0xff, 1 => $g / 0xff, 2 => $b / 0xff,
                    "r"=>$r / 0xff, "g"=>$g / 0xff, "b"=>$b / 0xff,
                    "hex" => sprintf("#%02X%02X%02X", $r, $g, $b));

                  // Set RGB text color
                  //$r=198; $g=203; $b=207;
                  //$r=129; $g=129; $b=129;

                  // Leave this array alone
                  $a_text_color = array(0 => $r / 0xff, 1 => $g / 0xff, 2 => $b / 0xff,
                    "r"=>$r / 0xff, "g"=>$g / 0xff, "b"=>$b / 0xff,
                    "hex" => sprintf("#%02X%02X%02X", $r, $g, $b));

                  // Set line
                  $pdf->line(40, $h-40, $w-40, $h-40, $a_line_color, 1, array());

                  // Set page text
                  $pdf->text(42, $h-39, "' . $footer_content . '", $font, 10, $a_text_color);

                  $pdf->page_text($w-57, $h-39, "{PAGE_NUM}/{PAGE_COUNT}", $font, 10, $a_text_color);

                  // Close the object (stop capture)
                  $pdf->close_object();

                  // Add the object to every page. You can
                  // also specify "odd" or "even"
                  $pdf->add_object($footer, "all");
                }
                </script>';
    echo $footer;
    $footer = '';
  }
  else {
    $footer = $footer_content;
  }
  echo '
  <table class="invoice-table">
  <thead>
  <tr class="one">
    <td class="one">
     <h1>' . t('Invoice') . '</h1>
     <div class="client-wrapper">';
  if (!empty($invoice->customer['company_name'])) {
    echo '<div class="company-name break">' . check_plain($invoice->customer['company_name']) . '</div>';
  }
  if (!empty($invoice->customer['fullname'])) {
    echo '<div class="fullname ' . (empty($invoice->customer['company_name']) ? 'break' : '') . '">' . check_plain($invoice->customer['fullname']) . '</div>';
  }
  echo '
       <div class="address"><span class="street">' . check_plain($invoice->customer['street']) . '</span> <span class="building-number">' . check_plain($invoice->customer['building_number']) . '</span></div>
       <div class="zipcode-city"><span class="zipcode">' . check_plain($invoice->customer['zipcode']) . '</span> <span class="city">' . check_plain($invoice->customer['city']) . '</span></div>
       <div class="country break">' . check_plain($invoice->customer['country']) . '</div>
       <div class="coc-number">' . (!empty($invoice->customer['coc_number']) ? t('C.o.C.') . ' ' . t('nr.') . ': ' . check_plain($invoice->customer['coc_number']) : '') . '</div>
       <div class="vat-number">' . (!empty($invoice->customer['vat_number']) ? t('VAT') . ' ' . t('nr.') . ': ' . check_plain($invoice->customer['vat_number']) : '') . '</div>
       <div class="description">' . check_plain($invoice->customer['description']) . '</div>
     </div>
    </td>
    <td class="two">

    </td>
    <td class="three">
     <div class="supplier-wrapper">
       <div class="company-name">' . check_plain(_invoice_get_variable($template, 'supplier_company_name')) . '</div>
       <div class="address"><span class="street">' . check_plain(_invoice_get_variable($template, 'supplier_street')) . '</span> <span class="building-number">' . check_plain(_invoice_get_variable($template, 'supplier_building_number')) . '</span></div>
       <div class="zipcode-city"><span class="zipcode">' . check_plain(_invoice_get_variable($template, 'supplier_zipcode')) . '</span> <span class="city">' . check_plain(_invoice_get_variable($template, 'supplier_city')) . '</span></div>
       <div class="country break">' . check_plain(_invoice_get_variable($template, 'supplier_country')) . '</div>
       <div class="phone">' . check_plain(_invoice_get_variable($template, 'supplier_phone')) . '</div>
       <div class="fax">' . check_plain(_invoice_get_variable($template, 'supplier_fax')) . ' </div>
       <div class="email">' . check_plain(_invoice_get_variable($template, 'supplier_email')) . '</div>
       <div class="web">' . check_plain(_invoice_get_variable($template, 'supplier_web')) . '</div>
     </div>
    </td>
  </tr>
  <tr class="two">
    <td colspan="3" class="invoice-details">
      <table class="invoice-details">
        <tr>
          <td class="one">
            <strong>' . t('Date') . '</strong>
            <div>' . $invoice->invoice['formatted_created'] . '</div>
          </td>
          <td class="two">
            <strong>' . t('Invoice number') . '</strong>
            <div>' . $invoice->invoice['formatted_invoice_number'] . '</div>
          </td>
          <td class="three">
           <strong>' . t('Client number') . '</strong>
            <div>' . $invoice->customer['customer_number'] . '</div>
          </td>';
  if (!empty($s_pagenum)) {
    echo '<td class="four">
                  <strong>' . t('Page') . '</strong>
                  <div>
                    ' . $s_pagenum . '
                  </div>
                </td>';
  }
  echo '
       </tr>
      </table>
    </td>
  </tr>
  </thead>
  <tbody>';

  /*--------- invoice items header ---------*/

  // If an invoice has items with different vat values this column must always be shown.
  $vat = NULL;
  $multiple_vat_values = FALSE;
  foreach ($invoice->invoice_items as $item) {

    // Set vat value for the first time
    if (is_null($vat)) {
      $vat = $item['vat'];
    }
    if ($item['vat'] !== $vat) {
      $multiple_vat_values = TRUE;
    }
  }
  echo '
    <tr>
      <td colspan="3" class="items">
        <table class="items">
          <tr class="one">';
  $colspan = 0;
  echo '<th class="description">' . t('Description') . '</td>';
  if (_invoice_get_variable($template, 'display_column_vat') || $multiple_vat_values === TRUE) {
    echo '<th>' . t('VAT') . '</td>';
    $colspan++;
  }
  echo '<th>' . t('Quantity') . '</td>';
  if (_invoice_get_variable($template, 'display_column_exunitcost')) {
    echo '<th>' . t('Unitcost') . ' (' . t('ex') . ')</td>';
    $colspan++;
  }
  if (_invoice_get_variable($template, 'display_column_incunitcost')) {
    echo '<th>' . t('Unitcost') . ' (' . t('inc') . ')</td>';
    $colspan++;
  }
  if (_invoice_get_variable($template, 'display_column_extotal')) {
    echo '<th>' . t('Total') . ' (' . t('ex') . ')</td>';
    $colspan++;
  }
  if (_invoice_get_variable($template, 'display_column_inctotal')) {
    echo '<th>' . t('Total') . ' (' . t('inc') . ')</td>';
    $colspan++;
  }
  echo '</tr>';

  /*--------------- end --------------------*/

  /*------------ invoice items -------------*/
  $i = 0;
  foreach ($invoice->invoice_items as $item) {
    if ($i == 0) {
      $class = 'odd';
      $i++;
    }
    else {
      $class = 'even';
      $i = 0;
    }
    echo '<tr class="' . $class . '">';
    echo '<td class="description">' . check_plain($item['description']) . '</td>';
    if (_invoice_get_variable($template, 'display_column_vat') || $multiple_vat_values === TRUE) {
      echo '<td>' . $item['vat'] . '%</td>';
    }
    echo '<td>' . $item['quantity'] . '</td>';
    if (_invoice_get_variable($template, 'display_column_exunitcost')) {
      echo '<td>' . $item['formatted_exunitcost'] . '</td>';
    }
    if (_invoice_get_variable($template, 'display_column_incunitcost')) {
      echo '<td>' . $item['formatted_incunitcost'] . '</td>';
    }
    if (_invoice_get_variable($template, 'display_column_extotal')) {
      echo '<td>' . $item['formatted_extotal'] . '</td>';
    }
    if (_invoice_get_variable($template, 'display_column_inctotal')) {
      echo '<td>' . $item['formatted_inctotal'] . '</td>';
    }
    echo '</tr>';
  }

  /*--------------- end --------------------*/
  echo '
          <tr class="total">
            <td colspan="' . $colspan . '"></td>
            <td class="extotal">' . $invoice->invoice['formatted_extotal'] . '</td>
            <td class="inctotal">' . $invoice->invoice['formatted_inctotal'] . '</td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td colspan="3">
        <table class="total">
         <tr class="extotal">
           <td class="one"></td>
           <td class="two">' . t('Subtotal excl. VAT') . ':</td>
           <td class="three">' . $invoice->invoice['formatted_extotal'] . '</td>
         </tr>
         ';
  foreach ($invoice->invoice['vat'] as $percentage => $vat) {
    echo '<tr class="vat">
                   <td class="one"></td>
                   <td class="two">' . t('VAT') . ' ' . $percentage . '%:</td>
                   <td class="three">' . $vat['formatted_vattotal'] . '</td>
                 </tr>';
  }
  echo '

         <tr class="inctotal">
           <td class="one"></td>
           <td class="two">' . t('Total incl. VAT') . ':</td>
           <td class="three"><div class="inctotal"><div class="inctotal inner">' . $invoice->invoice['formatted_inctotal'] . '</div></div></td>
         </tr>
        </table>
      </td>
    </tr>
    <tr>
    <td colspan="3" class="additional-information">
      <div class="invoice-description">' . nl2br($invoice->invoice['description']) . '
      <div class="pay-limit">' . t('We kindly ask you to pay this invoice within @days days.', array(
    '@days' => $invoice->invoice['pay_limit'],
  )) . '
    </td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td colspan="3" class="footer">
        ' . $footer . '
      </td>
    </tr>
  </tfoot>
  </table>';
  $content = ob_get_contents();
  ob_end_clean();
  return $content;
}