<?php

ob_start();


//include my own function
include 'includes/functions.php';



//customly collect the id and session
if(isset($_GET['u']))
{
	$u = $_GET['u'];
	$s = $_GET['s'];
	$iid = $_GET['iid'];
}
else
{
	header("Location:dashboard.php?sms=view_invoice&qlk=".md5(2)."");
}


//$session = get_current_session($id = '1'); //current session is always
$session = 6;
$st = get_session_title($session);


//$semester = get_current_semester($id = '1'); //current semester is always
//$semester_t = get_semester_title($semester);



// Include the main TCPDF library (search for installation path).
require_once('tcpdf/tcpdf_include.php');


// Extend the TCPDF class to create custom Header and Footer
class MYPDF extends TCPDF {

	//Page header
	public function Header() {
		// Logo
		$image_file = K_PATH_IMAGES.'logo.jpg';
		$this->Image($image_file, 10, 10, 15, '', 'JPG', '', 'T', false, 300, '', false, false, 0, false, false, false);
		// Set font
		$this->SetFont('helvetica', 'B', 14);
		// Title
		//$this->Cell(0, 15, '<< TCPDF Example 003 >>', 0, false, 'C', 0, '', 0, false, 'M', 'M'); original commented
		$this->Cell(0, 15, 'NIGER STATE COLLEGE OF EDUCATION MINNA', 0, false, 'C', 0, '', 0, false, 'M', 'M');
	}

	// Page footer
	public function Footer() {
		// Position at 15 mm from bottom
		$this->SetY(-15);
		// Set font
		$this->SetFont('helvetica', 'I', 8);
		// Page number
		$this->Cell(0, 10, "SOURCE: http://accommodation.coeminna-edu.ng   - ACCOMMODATION Portal,  Page ".$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
	}
}

// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);


//customly collect the title
$title = str_replace ('/','_',get_user_mat_no($u)). '-INVOICE-'.$iid;
$title =strtoupper($title);
$author = get_user_fullname($u).'_ COE MX';


// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor($author);
$pdf->SetTitle($title);
$pdf->SetSubject('Payment Invoice ');
$pdf->SetKeywords('COE MX, PDF, invoice, slip, '.$session.'');


// set default header data
//$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);



// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
	require_once(dirname(__FILE__).'/lang/eng.php');
	$pdf->setLanguageArray($l);
}

// ---------------------------------------------------------

// set font
//$pdf->SetFont('times', 'BI', 12) original;
$pdf->SetFont('times', '', 12);

// add a page
$pdf->AddPage();



//set the content //this is my own function
$output_ = '';
$output = '';

 //$output_ .= '<br/><h5  style="color:#00000; font-weight:normal" ><i>(Office of the Vice Chancellor)</i></h5>';
 //$output_ .= '<h3>NON-DEGREE PROGRAMME UNIT</h3>';
 $output_ .= '<h5  style="color:#00000; font-weight:normal" >NIGER STATE COLLEGE OF EDUCATION MINNA</h5>';
 $output_  .= '<h6   style="color:#00000; font-weight:normal"> '. $st .' ACADEMIC SESSION</h6>';

   $output_ .= '<h5  style="color:#F00; font-weight:normal" >INVOICE SLIP (Please take this to bank to make school fee payment)</h5>';






 //invoice to
	 $invoice_to = get_invoice_invoice_to($iid);
	 $pay_to = get_invoice_pay_to($iid);
	 $invoice_title = get_invoice_title($iid);
	 $invoice_description = get_invoice_desc($iid);
	 $invoice_amount  = get_invoice_amount($iid);
	 $invoice_total_amount  = get_invoice_total_amount($iid);
	 $invoiced_credit  = get_invoiced_credit($iid);

	 $invoice_total_amount = 'N'.number_format($invoice_total_amount,2).'K';
	 $invoices_credit = 'N'.number_format($invoiced_credit,2);
	 $invoice_amount = 'N'.number_format($invoice_amount,2);


	$invoice_date =  get_invoice_date($iid) ;
	$invoice_due_date =  get_invoice_due_date($iid) ;
	$invoice_exp_date = get_invoice_exp_date($iid) ;
	$type_id = get_type_id($iid);

	if($type_id==1)
  {

 	$invoice_to_ufn= get_user_fullname_prence($invoice_to);
 	$invoice_to_add = get_user_address_prence($invoice_to);
 	$invoice_to_st = show_state(get_user_state_id_prence($invoice_to));
 	$email = get_user_email_prence($invoice_to);
 	$phone_no = get_user_phone_no_prence($invoice_to);
 	//pass invoice to user id
 	$user_id = $invoice_to;
 	//collect matric number
 	$mat_no = strtoupper (get_user_mat_no_prence($user_id));
 	$pt = strtoupper(show_programme_type($type_id));
 	$department = get_user_department_id_prence($user_id); //department
 	$programme = strtoupper(get_programme_title(get_user_programme_id_prence($user_id))); //programme
  }else if($type_id==2)
  {
 	$invoice_to_ufn= get_user_fullname_nce($invoice_to);
 	$invoice_to_add = get_user_address_nce($invoice_to);
 	$invoice_to_st = show_state(get_user_state_id_nce($invoice_to));
 	$email = get_user_email_nce($invoice_to);
 	$phone_no = get_user_phone_no_nce($invoice_to);
 	//pass invoice to user id
 	$user_id = $invoice_to;
 	//collect matric number
 	$mat_no = strtoupper (get_user_mat_no_nce($user_id));
 	$pt = strtoupper(show_programme_type($type_id));
 	$department = get_user_department_id_nce($user_id); //department
 	$programme = strtoupper(get_programme_title(get_user_programme_id_nce($user_id))); //programme
  }else if($type_id==3)
  {

 	$invoice_to_ufn= get_user_fullname_duap($conn_2, $invoice_to);
 	$invoice_to_add = get_user_address_duap($conn_2, $invoice_to);
 	$invoice_to_st = show_state(get_user_state_id_duap($conn_2, $invoice_to));

 	$payment_status = get_invoice_payment_status($iid); //get the invoice payment status
 	$invoice_expire_date = get_invoice_exp_date($iid);
 	$email = get_user_email_duap($conn_2, $invoice_to);
 	$phone_no = get_user_phone_no_duap($conn_2, $invoice_to);
 	//pass invoice to user id
 	$user_id = $invoice_to;
 	//collect matric number
 	$mat_no = strtoupper (get_user_mat_no_duap($conn_2, $user_id));
 	$pt = strtoupper(show_programme_type($type_id));
 	$department = get_user_department_id_duap($conn_2, $user_id); //department
 	$programme = strtoupper(get_programme_title2($conn_2, get_user_programme_id_duap($conn_2, $user_id))); //programme
  }else if($type_id==4)
  {

 	$invoice_to_ufn= get_user_fullname_duap($conn_2, $invoice_to);
 	$invoice_to_add = get_user_address_duap($conn_2, $invoice_to);
 	$invoice_to_st = show_state(get_user_state_id_duap($conn_2, $invoice_to));
 	$email = get_user_email_duap($conn_2, $invoice_to);
 	$phone_no = get_user_phone_no_duap($conn_2, $invoice_to);
 	//pass invoice to user id
 	$user_id = $invoice_to;
 	//collect matric number
 	$mat_no = strtoupper (get_user_mat_no_duap($conn_2, $user_id));
 	$pt = strtoupper(show_programme_type($type_id));
 	$department = get_user_department_id_duap($conn_2, $user_id); //department
 	$programme = strtoupper(get_programme_title2($conn_2, get_user_programme_id_duap($conn_2, $user_id))); //programme
  }else if($type_id==5)
  {

 	$invoice_to_ufn= get_user_fullname_summer($invoice_to);
 	$invoice_to_add = get_user_address_summer($invoice_to);
 	$invoice_to_st = show_state(get_user_state_id_summer($invoice_to));

 	$email = get_user_email_summer($invoice_to);
 	$phone_no = get_user_phone_no_summer($invoice_to);
 	//pass invoice to user id
 	$user_id = $invoice_to;
 	//collect matric number
 	$mat_no = strtoupper (get_user_mat_no_summer($user_id));
 	$pt = strtoupper(show_programme_type($type_id));
 	$department = get_user_department_id_summer($user_id); //department
 	$programme = strtoupper(get_programme_title(get_user_programme_id_summer($user_id))); //programme
  }else if($type_id==6)
  {

 	$invoice_to_ufn= get_user_fullname_lvsp($invoice_to);
 	$invoice_to_add = get_user_address_lvsp($invoice_to);
 	$invoice_to_st = show_state(get_user_state_id_lvsp($invoice_to));

 	$email = get_user_email_lvsp($invoice_to);
 	$phone_no = get_user_phone_no_lvsp($invoice_to);
 	//pass invoice to user id
 	$user_id = $invoice_to;
 	//collect matric number
 	$mat_no = strtoupper (get_user_mat_no_lvsp($user_id));
 	$pt = strtoupper(show_programme_type($type_id));
 	$department = get_user_department_id_lvsp($user_id); //department
 	$programme = strtoupper(get_programme_title(get_user_programme_id_lvsp($user_id))); //programme
  }



	//pdf generated
	$pdf_generated = date('d-m-Y / H:i:s');



// set some text to print
$txt = <<<EOD


<div align="center"> $output_ </div>

<div align="right"> $output </div>

 <table cellspacing="1"  border="0" cellpadding="1" style="font-size:11px; background-color:#CCC">

			  <tr><td width="100%" style="font-size:24px; background-color:#CCC" >INVOICE #$iid</td></tr>
			  <tr><td width="100%"> Invoice Date:  $invoice_date </td></tr>
			  <tr><td width="100%"> Due Date: $invoice_due_date</td></tr>
			  <tr><td width="100%"> Expire Date / Time: $invoice_exp_date </td></tr>

 </table>
 <br><br>


 <h6 style="border-bottom:solid thin #CCC; margin-bottom:3px; font-weight:bold;font-size:14px">INVOICE TO</h6>

 <table cellspacing="1"  border="0" cellpadding="3" style="font-size:11px;">

			 <tr><td width="100%"> $invoice_to_ufn , Mat no.: $mat_no ,</td></tr>
			 <tr><td width="100%"> Programme Type. : $pt ,</td></tr>
			 <tr><td width="100%"> Programme. : $programme  </td></tr>
			 <tr><td width="100%"> of  $invoice_to_add ,</td></tr>
			 <tr><td width="100%"> $invoice_to_st</td></tr>

 </table>

 <br><br>
 <table cellspacing="1"   cellpadding="2" border="0.5"  style="border:#000 solid 1px; font-size:12px;">

			  <tr style="background-color:#CCC">
			  <th width="75%" ><b> Description </b></th>
			  <th width="25%"> <b> Total </b></th>
			  </tr>


			  <tr>
			  <td width="75%"> $invoice_title <br/> $invoice_description </td>
			  <td width="25%">  $invoice_amount  </td>
			  </tr>



			  <tr>
			  <td width="75%" align="right"> <b> Sub Total </b> </td>
			  <td width="25%"> <b> $invoice_amount </b> </td>
			  </tr>


			  <tr>
			  <td width="75%" align="right"> <b> Credit </b></td>
			  <td width="25%"> <b> $invoiced_credit </b> </td>
			  </tr>


			  <tr>
			  <td width="75%" align="right"> <b> Total </b> </td>
			  <td width="25%"> <b> $invoice_total_amount </b> </td>
			  </tr>




 </table>



 <br><br>





		  <br><br>


		  PDF generated - $pdf_generated.


EOD;

// print a block of text using Write()
//$pdf->Write(0, $txt, '', 0, 'C', true, 0, false, false, 0);

//$pdf->writeHTML($txt, true, false, false, false, '');

// ---------------------------------------------------------






// -- set new background ---

//// get the current page break margin
//$bMargin = $pdf->getBreakMargin();
//// get current auto-page-break mode
//$auto_page_break = $pdf->getAutoPageBreak();
//// disable auto-page-break
//$pdf->SetAutoPageBreak(false, 0);
//// set bacground image
//$img_file = K_PATH_IMAGES.'bg.jpg';
//$pdf->Image($img_file, 0, 0, 210, 297, '', '', '', false, 300, '', false, false, 0);
//// restore auto-page-break status
//$pdf->SetAutoPageBreak($auto_page_break, $bMargin);
//// set the starting point for the page content
//$pdf->setPageMark();


$pdf->writeHTML($txt, true, false, false, false, '');

// ---------------------------------------------------------




//Close and output PDF document
//$pdf->Output('example_003.pdf', 'I');
$pdf->Output($title.'.pdf', 'D');

//============================================================+
// END OF FILE
//============================================================+
