<?php
ini_set('max_execution_time',-1); 
ini_set('memory_limit',-1);
$w=$GLOBALS['db']->query("select id,payment_date,name,parent_name,register_date,value 
		from ecmtransactions WHERE payment_date<'".date('Y-m-d')."' and type=0 and deleted=0 and (settled='' or settled is null) order by payment_date asc");
$i=0;
echo "<pre>";
$data=null;
while($r=$GLOBALS['db']->fetchByAssoc($w)){
		$tmp = array();
		
 		$tmp['payment_date']=$r['payment_date'];
 		$tmp['name']=$r['name'];
 		$tmp['parent_name']=$r['parent_name'];
 		$tmp['register_date']=$r['register_date'];
 		if($r['parent_name']=='BSL SP. Z O.O.')continue;
 		if($r['parent_name']=='BSL TRUCK PAWEŁ BARTYŚ')continue;
 		
 
 		$t = new EcmTransaction();
 		$t->retrieve($r['id']);
 		
 		$tmp['value']=$r['value'];
 		$tmp['value2']=$t->getRelations();
 		$tmp['value3']=$r['value']-$t->getRelations();
 		
 		$datetime1 = new DateTime(date('Y-m-d'));
 		$datetime2 = new DateTime($tmp['payment_date']);
 		 
		$interval = $datetime1->diff($datetime2);

 		 
 		$tmp['diff']= abs($interval->format('%R%a')).' dni';
 		
 		unset($t);
 		$data[]=$tmp;
}

//echo '<br>'.create_guid();

$es = new EcmSysInfo();

$content= "<h2>Raport nierozliczonych transakcji</h2>".$es->getName()."<br>Data wydruku: ".date("d.m.Y.")."<br><hr><table style='width:100%;font-size:7pt;'><tr>
		<td>Termin płatności</td>
		<td>Kontrahent</td>
		<td>Nr dokumentu</td>
		<td>Data dokumentu</td>
		<td>Dni po terminie</td>
		<td>Kwota</td>
		<td>Nierozliczone</td>
		<td>Rozliczone</td></tr>";

foreach ($data as $k=>$v){
	$content.='<tr>
			<td>'.date('d.m.Y',strtotime($v['payment_date'])).'</td>
		<td>'.$v['parent_name'].'</td>
	<td>'.$v['name'].'</td>
		<td>'.($v['register_date']!='01.01.1970' ? date('d.m.Y',strtotime($v['register_date'])) : '').'</td>
		<td>'.$v['diff'].'</td>
	<td style="text-align:right;">'.format_number($v['value']).'</td>
	<td style="text-align:right;">'.format_number($v['value2']).'</td>
	<td style="text-align:right;">'.format_number($v['value3']).'</td>
	</tr>';
}
		$content.="</table>";
		
		

require_once 'include/ECM/EcmSendPdfButton/EcmSendPdfButton.inc';


$t = new EcmSendPdfButton('Users', $usr_id,$itemail,$type);
#$t->setAdress('outtervision@gmail.com');
$t->setAdress('ela_t@bsltparts.pl ');
$t->setAdress('pawel_b@bsltruck.pl');
$t->setAdress('windykacja@bsltruck.pl');
#$t->setAdress('ryszard.lisiecki@saas-systems.pl');
$t->setSubject('Raport nierozliczonych transakcji');
$t->setBody(from_html('Lista w załączniku.'));

include_once ("include/MPDF57/mpdf.php");

$mPDF = new mPDF ( 'pl', 'A4', 9, 'arial',  10, 10,5, 1, 14, 20);
$mPDF->setAutoTopMargi = 'stretch';
$mPDF->setAutoBottomMargin = 'stretch';
$mPDF->mirrorMargins = 1;
$mPDF->setFooter('Strona {PAGENO}');
$mPDF->WriteHTML($content);


	$mPDF->Output('upload/raport_trans_'.date('d_m_Y').'.pdf', "F");
	
$t->setAttachment('upload/raport_trans_'.date('d_m_Y').'.pdf');

$success = false;
$success= $t->sendEmail();

?>