<?
function getInfoEdit($date_field="date")
{
	//template info
	$template_id = $_SESSION['PREVIEW_PDF']['template_id'];
	$rr=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select * from ecmdocumenttemplates where id='".$template_id."'"));
	$arr['template_logo_path']=$rr['logo_path'];
	$arr['header_text']=$rr['header_text'];
	$arr['footer_text']=$rr['footer_text'];
	$account_id=$rr['account_id'];
	//from info
	$rr=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select * from accounts where id='".$account_id."'"));
	$arr['from_name'] = $rr['name'];
	$arr['from_address_street']=$rr['billing_address_street'];
	$arr['from_address_city']=$rr['billing_address_city'];
	$arr['from_address_postalcode']=$rr['billing_address_postalcode'];
	//$assigned_user_id=$rr['assigned_user_id'];
	//to info
	$arr['to_name'] = $_SESSION['PREVIEW_PDF']['to_name'];
	$arr['to_address_street'] = $_SESSION['PREVIEW_PDF']['to_address_street'];
	$arr['to_address_postalcode'] = $_SESSION['PREVIEW_PDF']['to_address_postalcode'];
	$arr['to_address_city'] = $_SESSION['PREVIEW_PDF']['to_address_city'];
	//date
	$arr['date'] = $_SESSION['PREVIEW_PDF'][$date_field];
	//no
	$arr['no'] = $_SESSION['PREVIEW_PDF']['name'];
	//user
	$assigned_user_id = $_SESSION['PREVIEW_PDF']['assigned_user_id'];
	$r=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select first_name,last_name from users where id='".$assigned_user_id."'"));
	$arr['user_name']=$r['first_name']." ".$r['last_name'];
	//invoice
	$arr['invoice_name'] = $_SESSION['PREVIEW_PDF']['invoice_name'];
	
	return $arr;
}
function getInfo($table,$document_id,$date_field="date")
{
	global $timedate;
	$r=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select * from ".$table." where id='".$document_id."'"));
	
	//template info
	$template_id = $r['template_id'];
	$rr=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select * from ecmdocumenttemplates where id='".$template_id."'"));
	$arr['template_logo_path']=$rr['logo_path'];
	$arr['header_text']=$rr['header_text'];
	$arr['footer_text']=$rr['footer_text'];
	$account_id=$rr['account_id'];
	//from info
	$rr=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select * from accounts where id='".$account_id."'"));
	$arr['from_name'] = $rr['name'];
	$arr['from_address_street']=$rr['billing_address_street'];
	$arr['from_address_city']=$rr['billing_address_city'];
	$arr['from_address_postalcode']=$rr['billing_address_postalcode'];
	$arr['assigned_user_id']=$rr['assigned_user_id'];
	//to info
	$arr['to_name'] = $r['to_name'];
	$arr['to_address_street'] = $r['to_address_street'];
	$arr['to_address_postalcode'] = $r['to_address_postalcode'];
	$arr['to_address_city'] = $r['to_address_city'];
	//date
	$arr['date'] = $timedate->to_display($r[$date_field],"Y-m-d",$timedate->get_date_format());
	//no
	$arr['no'] = $r['name'];
	//user
	$assigned_user_id = $r['assigned_user_id'];
	$rr=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select first_name,last_name from users where id='".$assigned_user_id."'"));
	$arr['user_name']=$rr['first_name']." ".$rr['last_name'];
	//invoice
	$arr['invoice_name'] = $r['invoice_name'];
	
	//$footer_text = $r['footer_text'];
	//$header_text = $r['header_text'];
	
	return $arr;
}
function getProductsArrayEdit($products)
{
//print_r($products);
	foreach($products as $key=>$r)
	{
		$rr=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select tax_class_id from ecmproducts where id='".$r['id']."'"));
		$rr=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select value,name from ecmproducttaxs where id='".$rr['tax_class_id']."'"));
		$tax=$rr['value'];
		$tax_name=$rr['name'];
		
		$netto_value=round($r['quantity']*$r['price'],2);
		$brutto_value=round($netto_value*(1+$tax/100),2);
		$tax_value=round($netto_value*$tax/100,2);
		
		$total_netto_value+=round($netto_value,2);
		$total_brutto_value+=round($brutto_value,2);
		$total_tax_value+=round($tax_value,2);

		$table[]=array(
			'index'=>array('width'=>5,
				'value' =>$i+1,
				'background'=>array(255,255,255),
				'border'=>0,
				'overflow'=>1,
				'align'=>'L',
				'font-size'=>9,
				'font-style'=>'',
			),
			'product_index'=>array('width'=>10,
				'value' =>$r['product_index'],
				'background'=>array(255,255,255),
				'border'=>0,
				'overflow'=>1,
				'align'=>'L',
				'font-size'=>9,
				'font-style'=>'',
			),
			'name'=>array('width'=>30,
				'value' =>$r['name'],
				'background'=>array(255,255,255),
				'border'=>0,
				'overflow'=>1,
				'align'=>'L',
				'font-size'=>9,
				'font-style'=>'',
			),
			'quantity'=>array('width'=>10,
				'value' =>$r['quantity'],
				'background'=>array(255,255,255),
				'border'=>0,
				'overflow'=>1,
				'align'=>'L',
				'font-size'=>9,
				'font-style'=>'',
			),
			'price'=>array('width'=>10,
				'value' =>$r['price'],
				'background'=>array(255,255,255),
				'border'=>0,
				'overflow'=>1,
				'align'=>'L',
				'font-size'=>9,
				'font-style'=>'',
			),
			'netto'=>array('width'=>10,
				'value' =>number_format($netto_value,2,",","."),
				'background'=>array(255,255,255),
				'border'=>0,
				'overflow'=>1,
				'align'=>'L',
				'font-size'=>9,
				'font-style'=>'',
			),
			'brutto'=>array('width'=>10,
				'value' =>number_format($brutto_value,2,",","."),
				'background'=>array(255,255,255),
				'border'=>0,
				'overflow'=>1,
				'align'=>'L',
				'font-size'=>9,
				'font-style'=>'',
			),
			'tax'=>array('width'=>10,
				'value' =>number_format($tax_value,2,",","."),
				'background'=>array(255,255,255),
				'border'=>0,
				'overflow'=>1,
				'align'=>'L',
				'font-size'=>9,
				'font-style'=>'',
			),
		);
		$i++;
	}
	$table[$i]['total_netto_blank']=array('width'=>60,
				'value' =>'',
				'background'=>array(255,255,255),
				'border'=>0,
				'overflow'=>1,
				'align'=>'L',
				'font-size'=>9,
				'font-style'=>'b',
			);
	$table[$i]['total_netto_label']=array('width'=>20,
				'value' =>'Suma',
				'background'=>array(233,233,233),
				'border'=>0,
				'overflow'=>1,
				'align'=>'L',
				'font-size'=>9,
				'font-style'=>'b',
			);
	$table[$i]['total_netto_value']=array('width'=>15,
				'value' =>number_format($total_netto_value,2,",","."),
				'background'=>array(255,255,255),
				'border'=>0,
				'overflow'=>1,
				'align'=>'R',
				'font-size'=>9,
				'font-style'=>'b',
			);
	$table[$i+1]['total_tax_blank']=array('width'=>60,
				'value' =>'',
				'background'=>array(255,255,255),
				'border'=>0,
				'overflow'=>1,
				'align'=>'L',
				'font-size'=>9,
				'font-style'=>'b',
			);
	$table[$i+1]['total_tax_label']=array('width'=>20,
				'value' =>'VAT',
				'background'=>array(233,233,233),
				'border'=>0,
				'overflow'=>1,
				'align'=>'L',
				'font-size'=>9,
				'font-style'=>'b',
			);
	$table[$i+1]['total_tax_value']=array('width'=>15,
				'value' =>number_format($total_tax_value,2,",","."),
				'background'=>array(255,255,255),
				'border'=>0,
				'overflow'=>1,
				'align'=>'R',
				'font-size'=>9,
				'font-style'=>'b',
			);
	$table[$i+2]['total_brutto_blank']=array('width'=>60,
				'value' =>'',
				'background'=>array(255,255,255),
				'border'=>0,
				'overflow'=>1,
				'align'=>'L',
				'font-size'=>9,
				'font-style'=>'b',
			);
	$table[$i+2]['total_brutto_label']=array('width'=>20,
				'value' =>'Suma końcowa',
				'background'=>array(233,233,233),
				'border'=>0,
				'overflow'=>1,
				'align'=>'L',
				'font-size'=>9,
				'font-style'=>'b',
			);
	$table[$i+2]['total_brutto_value']=array('width'=>15,
				'value' =>number_format($total_brutto_value,2,",","."),
				'background'=>array(255,255,255),
				'border'=>0,
				'overflow'=>1,
				'align'=>'R',
				'font-size'=>9,
				'font-style'=>'b',
			);
	return $table;
}
function getProductsArray($document_id,$document_type)
{
	$w=$GLOBALS['db']->query("select * from ecmproductsdocumentsrelations where document_id='".$document_id."' and document_type='".$document_type."' and deleted='0' order by product_position asc");
	$i=1;
	while($r=$GLOBALS['db']->fetchByAssoc($w))
	{
		
		$rr=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select tax_class_id from ecmproducts where id='".$r['product_id']."'"));
		$rr=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select value,name from ecmproducttaxs where id='".$rr['tax_class_id']."'"));
		$tax=$rr['value'];
		$tax_name=$rr['name'];
		
		$netto_value=round($r['product_quantity']*$r['product_price'],2);
		$brutto_value=round($netto_value*(1+$tax/100),2);
		$tax_value=round($netto_value*$tax/100,2);
		
		$total_netto_value+=$netto_value;
		$total_brutto_value+=$brutto_value;
		$total_tax_value+=$tax_value;

		$table[]=array(
			'index'=>array('width'=>5,
				'value' =>$i,
				'background'=>array(255,255,255),
				'border'=>0,
				'overflow'=>1,
				'align'=>'L',
				'font-size'=>9,
				'font-style'=>'',
			),
			'product_index'=>array('width'=>10,
				'value' =>$r['product_code'],
				'background'=>array(255,255,255),
				'border'=>0,
				'overflow'=>1,
				'align'=>'L',
				'font-size'=>9,
				'font-style'=>'',
			),
			'name'=>array('width'=>30,
				'value' =>$r['product_name'],
				'background'=>array(255,255,255),
				'border'=>0,
				'overflow'=>1,
				'align'=>'L',
				'font-size'=>9,
				'font-style'=>'',
			),
			'quantity'=>array('width'=>10,
				'value' =>$r['product_quantity'],
				'background'=>array(255,255,255),
				'border'=>0,
				'overflow'=>1,
				'align'=>'L',
				'font-size'=>9,
				'font-style'=>'',
			),
			'price'=>array('width'=>10,
				'value' =>$r['product_price'],
				'background'=>array(255,255,255),
				'border'=>0,
				'overflow'=>1,
				'align'=>'L',
				'font-size'=>9,
				'font-style'=>'',
			),
			'netto'=>array('width'=>10,
				'value' =>number_format($netto_value,2,",","."),
				'background'=>array(255,255,255),
				'border'=>0,
				'overflow'=>1,
				'align'=>'L',
				'font-size'=>9,
				'font-style'=>'',
			),
			'brutto'=>array('width'=>10,
				'value' =>number_format($brutto_value,2,",","."),
				'background'=>array(255,255,255),
				'border'=>0,
				'overflow'=>1,
				'align'=>'L',
				'font-size'=>9,
				'font-style'=>'',
			),
			'tax'=>array('width'=>10,
				'value' =>number_format($tax_value,2,",","."),
				'background'=>array(255,255,255),
				'border'=>0,
				'overflow'=>1,
				'align'=>'L',
				'font-size'=>9,
				'font-style'=>'',
			),
		);
		$i++;
	}
	$table[$i]['total_netto_blank']=array('width'=>60,
				'value' =>'',
				'background'=>array(255,255,255),
				'border'=>0,
				'overflow'=>1,
				'align'=>'L',
				'font-size'=>9,
				'font-style'=>'b',
			);
	$table[$i]['total_netto_label']=array('width'=>20,
				'value' =>'Suma',
				'background'=>array(233,233,233),
				'border'=>0,
				'overflow'=>1,
				'align'=>'L',
				'font-size'=>9,
				'font-style'=>'b',
			);
	$table[$i]['total_netto_value']=array('width'=>15,
				'value' =>number_format($total_netto_value,2,",","."),
				'background'=>array(255,255,255),
				'border'=>0,
				'overflow'=>1,
				'align'=>'R',
				'font-size'=>9,
				'font-style'=>'b',
			);
	$table[$i+1]['total_tax_blank']=array('width'=>60,
				'value' =>'',
				'background'=>array(255,255,255),
				'border'=>0,
				'overflow'=>1,
				'align'=>'L',
				'font-size'=>9,
				'font-style'=>'b',
			);
	$table[$i+1]['total_tax_label']=array('width'=>20,
				'value' =>'VAT',
				'background'=>array(233,233,233),
				'border'=>0,
				'overflow'=>1,
				'align'=>'L',
				'font-size'=>9,
				'font-style'=>'b',
			);
	$table[$i+1]['total_tax_value']=array('width'=>15,
				'value' =>number_format($total_tax_value,2,",","."),
				'background'=>array(255,255,255),
				'border'=>0,
				'overflow'=>1,
				'align'=>'R',
				'font-size'=>9,
				'font-style'=>'b',
			);
	$table[$i+2]['total_brutto_blank']=array('width'=>60,
				'value' =>'',
				'background'=>array(255,255,255),
				'border'=>0,
				'overflow'=>1,
				'align'=>'L',
				'font-size'=>9,
				'font-style'=>'b',
			);
	$table[$i+2]['total_brutto_label']=array('width'=>20,
				'value' =>'Suma końcowa',
				'background'=>array(233,233,233),
				'border'=>0,
				'overflow'=>1,
				'align'=>'L',
				'font-size'=>9,
				'font-style'=>'b',
			);
	$table[$i+2]['total_brutto_value']=array('width'=>15,
				'value' =>number_format($total_brutto_value,2,",","."),
				'background'=>array(255,255,255),
				'border'=>0,
				'overflow'=>1,
				'align'=>'R',
				'font-size'=>9,
				'font-style'=>'b',
			);
	return $table;
}
function getHeaderArray()
{
	$table[]=array(
		'index'=>array('width'=>5,
			'value' =>"Lp",
			'background'=>array(233,233,233),
			'border'=>0,
			'overflow'=>1,
			'align'=>'L',
			'font-size'=>9,
			'font-style'=>'b',
		),
		'product_index'=>array('width'=>10,
			'value' =>'Index',
			'background'=>array(233,233,233),
			'border'=>0,
			'overflow'=>1,
			'align'=>'L',
			'font-size'=>9,
			'font-style'=>'b',
		),
		'name'=>array('width'=>30,
			'value' =>'Opis',
			'background'=>array(233,233,233),
			'border'=>0,
			'overflow'=>1,
			'align'=>'L',
			'font-size'=>9,
			'font-style'=>'b',
		),
		'quantity'=>array('width'=>10,
			'value' =>'Ilość',
			'background'=>array(233,233,233),
			'border'=>0,
			'overflow'=>1,
			'align'=>'L',
			'font-size'=>9,
			'font-style'=>'b',
		),
		'price'=>array('width'=>10,
			'value' =>'Cena',
			'background'=>array(233,233,233),
			'border'=>0,
			'overflow'=>1,
			'align'=>'L',
			'font-size'=>9,
			'font-style'=>'b',
		),
		'netto'=>array('width'=>10,
			'value' =>'Netto',
			'background'=>array(233,233,233),
			'border'=>0,
			'overflow'=>1,
			'align'=>'L',
			'font-size'=>9,
			'font-style'=>'b',
		),
		'brutto'=>array('width'=>10,
			'value' =>'Brutto',
			'background'=>array(233,233,233),
			'border'=>0,
			'overflow'=>1,
			'align'=>'L',
			'font-size'=>9,
			'font-style'=>'b',
		),
		'tax'=>array('width'=>10,
			'value' =>'VAT',
			'background'=>array(233,233,233),
			'border'=>0,
			'overflow'=>1,
			'align'=>'L',
			'font-size'=>9,
			'font-style'=>'b',
		),
	);
	return $table;
}
function getCompanyTable($from_name,$from_address_street,$from_address_postalcode,$from_address_city,$to_name,$to_address_street,$to_address_postalcode,$to_address_city)
{
	$table[]=array(
		'company_line'=>array('width'=>100,
			'value' =>$from_name." - ".$from_address_street." ".$from_address_postalcode." ".$from_address_city,
			'border'=>0,
			'overflow'=>1,
			'align'=>'L',
			'font-size'=>9,
			'font-style'=>'u',
		),
	);
	$table[]=array(
		'to_name'=>array('width'=>100,
			'value' =>$to_name,
			'border'=>0,
			'overflow'=>1,
			'align'=>'L',
			'font-size'=>9,
			'font-style'=>'',
		),
	);
	$table[]=array(
		'to_address_street'=>array('width'=>100,
			'value' =>$to_address_street,
			'border'=>0,
			'overflow'=>1,
			'align'=>'L',
			'font-size'=>9,
			'font-style'=>'',
		),
	);
	$table[]=array(
		'to_address_postalcode'=>array('width'=>100,
			'value' =>$to_address_postalcode." - ".$to_address_city,
			'border'=>0,
			'overflow'=>1,
			'align'=>'L',
			'font-size'=>9,
			'font-style'=>'',
		),
	);
	return $table;
}
function getRightBlockArray($no,$date,$user_name)
{

	$table[]=array(
		'numer'=>array('width'=>100,
			'value' =>'Numer',
			'border'=>0,
			'overflow'=>1,
			'align'=>'L',
			'font-size'=>9,
			'font-style'=>'B',
		),
	);
	$table[]=array(
		'numer_value'=>array('width'=>100,
			'value' =>$no,
			'border'=>0,
			'overflow'=>1,
			'align'=>'L',
			'font-size'=>9,
			'font-style'=>'',
		),
	);
	$table[]=array(
		'data'=>array('width'=>100,
			'value' =>'Data wystawienia',
			'border'=>0,
			'overflow'=>1,
			'align'=>'L',
			'font-size'=>9,
			'font-style'=>'B',
		),
	);
	$table[]=array(
		'data_value'=>array('width'=>100,
			'value' =>$date,
			'border'=>0,
			'overflow'=>1,
			'align'=>'L',
			'font-size'=>9,
			'font-style'=>'',
		),
	);
	$table[]=array(
		'wystawil'=>array('width'=>100,
			'value' =>'Wystawił',
			'border'=>0,
			'overflow'=>1,
			'align'=>'L',
			'font-size'=>9,
			'font-style'=>'B',
		),
	);
	$table[]=array(
		'wystawil_value'=>array('width'=>100,
			'value' =>$user_name,
			'border'=>0,
			'overflow'=>1,
			'align'=>'L',
			'font-size'=>9,
			'font-style'=>'',
		),
	);
	return $table;
}
function getLeftBlockArray($to_name,$type_name)
{
	$table[]=array(
		'type'=>array('width'=>100,
			'value' =>$type_name,
			'border'=>0,
			'overflow'=>1,
			'align'=>'L',
			'font-size'=>20,
			'font-style'=>'',
		),
	);
	$table[]=array(
		'type'=>array('width'=>100,
			'value' =>$type_name.' dla '.$to_name,
			'border'=>0,
			'overflow'=>1,
			'align'=>'L',
			'font-size'=>9,
			'font-style'=>'',
		),
	);
	return $table;
}
?>