<?php

/*
Copyright (c) 2010, Till Brehm, projektfarm Gmbh
All rights reserved.
*/

class plugin_invoiceitemlist extends plugin_base {

        var $module;
        var $form;
        var $tab;
        var $record_id;
        var $formdef;
        var $options;

        function onShow() {
            global $app,$conf;

            $pluginTpl = new tpl;
            $pluginTpl->newTemplate('templates/invoice_edit_item_list.htm');
			
			include('lib/lang/'.$_SESSION['s']['language'].'_invoice.lng');
			$pluginTpl->setVar($wb);
			
			$invoice_id = $this->form->id;
			$sql = "SELECT * FROM invoice_item WHERE invoice_id = $invoice_id";
			$tmp_records = $app->db->queryAllRecords($sql);
			$records = array();
			
			$app->uses('billing_functions');
			$invoice_settings = $app->billing_functions->get_invoice_settings();
			
			foreach($tmp_records as $tmp) {
				if($invoice_settings['invoice_item_list_with_vat'] == 'y') {
					$tmp['sum'] = $app->functions->currency_format($tmp['quantity']*$tmp['price']+($tmp['quantity']*$tmp['price']*($tmp['vat']/100)));
					//$tmp['price'] = $app->functions->currency_format($tmp['price']+($tmp['price']*($tmp['vat']/100)));
				} else {
					$tmp['sum'] = $app->functions->currency_format($tmp['quantity']*$tmp['price']);
					//$tmp['price'] = $app->functions->currency_format($tmp['price']);
				}
				$tmp['price'] = $app->functions->currency_format($tmp['price']);
				$tmp['description'] = nl2br(htmlspecialchars($app->db->unquote($tmp['description'])));
				$tmp['vat'] = $app->functions->currency_format($tmp['vat']);
				$records[] = $tmp;
			}
			
			
			$pluginTpl->setLoop('invoice_items',$records);
            
			$has_invoice_items = (count($records) > 0)?1:0;
			$pluginTpl->setVar('has_invoice_items',$has_invoice_items);
			$pluginTpl->setVar('invoice_item_list_with_vat',$invoice_settings['invoice_item_list_with_vat']);
			
			return $pluginTpl->grab();

        }
}

?>