<?php
/**********************************************************************
Copyright (C) Evolve Techniologies
Author : Evolve Technologies
Email : info@evolve-sl.com
Company : Evolve Technologies(pvt)Ltd
***********************************************************************/
ob_start(); 
	
require_once ('class.numbertoword.php');

$page_security = 'SA_GLTRANSVIEW';
$path_to_root = "..";
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/gl/includes/gl_db.inc");

require_once('config/lang/eng.php');
require_once('tcpdf.php');

// create new PDF document
//$pdf = new TCPDF('L', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf = new TCPDF('P', PDF_UNIT, 'A4', true, 'UTF-8', false);
//$pdf = new TCPDF('P', PDF_UNIT, 'NPV', true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);

// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);

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

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

//set some language-dependent strings
$pdf->setLanguageArray($l);

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

//set margins
$pdf->SetMargins(0, 0, 0,0);
// ---------------------------------------------------------

// set font
$pdf->SetFont('times', '', 10);

//-----------add new code for the get compny details---15.08.2012----
	$result = get_company_details();
	while($myrow = db_fetch($result)){
		if($myrow[0] == 'coy_name')
		$company_name = $myrow['value'];
		if($myrow[0] == 'postal_address')
		$company_address = $myrow['value'];
		if($myrow[0] == 'phone')
		$company_phone = $myrow['value'];
		if($myrow[0] == 'fax')
		$company_fax = $myrow['value'];
		if($myrow[0] == 'email')
		$company_email = $myrow['value'];
	}

$from = isset($_REQUEST['from'])?$_REQUEST['from']:'';
$to = isset($_REQUEST['to'])? $_REQUEST['to']:'';
$account = isset($_REQUEST['bank_account'])? $_REQUEST['bank_account']:'';
$prt_Type = 51;//----voucher type define as 50 


function get_main_memo($type, $trans_id){
	$sql = "SELECT * FROM ".TB_PREF."comments WHERE type=".db_escape($type)." AND id=".db_escape($trans_id);	

	$result = db_query($sql, "Transactions could not display");

	$row = db_fetch_row($result);
	return $row[3];
}

function get_line_memo($type, $trans_id){
	$sql = "SELECT * FROM ".TB_PREF."gl_trans WHERE type=".db_escape($type)." AND type_no=".db_escape($trans_id);	

	$result = db_query($sql, "Transactions could not display");

	$row = db_fetch($result);
        
        if($row['amount'] < 0 ){
	return $row['memo_'];
        }
}


$resultBulk = get_selected_bank_trans_for_print($account, $from, $to, $prt_Type);

$type_id_array = array();
$trans_no_array = array();

$j=0;

while($myrowsBulk = mysqli_fetch_array($resultBulk)) {
	$type_id_array[$j] = $myrowsBulk['type'];
	$trans_no_array[$j] = $myrowsBulk['trans_no'];
	$j++;
}

	if(count($trans_no_array)==0){
		$type_id_array[0] = $_GET['type_id'];
		$trans_no_array[0] = $_GET['trans_no'];
	}

for($i=0;$i<count($trans_no_array);){
	$result = get_gl_trans($type_id_array[$i], $trans_no_array[$i]);	

//$result = get_gl_trans($_GET['type_id'], $_GET['trans_no']);
         $count = 1;
while ($myrow = db_fetch($result)) 
{
	if ($myrow['amount'] == 0) continue;


	$system_number = $myrow["reference"];//------@widanage chnage the code to get new payment 22.04.2013----
	
	$trans_no = get_payment_voucher_number($myrow["type"], $myrow["type_no"]);
	
	//$trans_no = $myrow["reference"];	
	
	$memo = get_main_memo($myrow["type"], $myrow["type_no"]);
	

	$date = $myrow["tran_date"];
	$myrow['account'];
	$myrow['account_name'];
	
	if($myrow['amount']>0){
		$peson_typeid = $myrow['person_type_id'];
		$personid = $myrow['person_id'];
	}
	/*$peson_typrid = $myrow['person_type_id'];
	$personid = $myrow['person_id'];*/
	//$description = $myrow['memo_'];
	//$description = $myrow['account'].'  '.$myrow['account_name'];
	 $gl_code = $myrow['account'].'  '.$myrow['account_name'];
	
//	if($description == ''){
//		$description .= payment_person_name_cheque($peson_typeid,$personid);
//	}

	if( $myrow['memo_'] == ''){
		//$description = $myrow["type"].'-'. $myrow["type_no"];
             $description = get_line_memo($myrow["type"], $myrow["type_no"]);
//                if ($myrow['amount'] < 0){
//                    $description = $myrow['memo_'];
//                }
	}else{
              $description = $myrow['memo_'];
        }
	if ($dim >= 1)
		 get_dimension_string($myrow['dimension_id'], true);
	if ($dim > 1)
		 get_dimension_string($myrow['dimension2_id'], true);
	

	if($myrow['type'] == ST_SUPPAYMENT){
		
		$result_items = get_voucher_details($myrow['type_no']);	
		$display_total =0;
		$items ='';	
		while($row_items = db_fetch($result_items)){
			
			$mysql_invoice = get_supp_details($row_items['trans_no_to']);
                      
			$invoice_number = db_fetch($mysql_invoice,0,'supp_reference');
			//$supp_address = mysql_result($mysql_invoice,0,'address');
			//$payee_name = html_entity_decode(mysql_result($mysql_invoice,0,'supp_name'));
			$result_tax = get_trans_tax_details(20, $row_items['trans_no_to']);
			
			$without_tax = $row_items['amt'] - db_fetch($result_tax,0,'amount');

//-------------------items of invoice--------------			
			 $items .=	'<tr height=50>
							<td width="290">Settlement of Supplier Invoice - '.$invoice_number.'</td>
							<td width="100" align="right">'.number_format($without_tax,2,'.',',').'</td>
							<td width="65" align="right">'.number_format(db_fetch($result_tax,0,'amount'),2,'.',',').'</td>
							<td width="85" align="right">'.number_format($row_items['amt'],2,'.',',').'</td>
						</tr>';
//-------------------items of invoice--------------	
			$display_total = $display_total + $row_items['amt'];		
	
		}

		if($items == ''){
			 $items .=	'<tr height=50>
						<td width="290">Supplier Advance Payment</td>
						<td width="100" align="right"></td>
						<td width="65" align="right"></td>
						<td width="85" align="right">'.number_format(abs($myrow['amount']),2,'.',',').'</td>
					</tr>';
			$display_total = $display_total +abs($myrow['amount']);
		}

	}else{
		if($myrow['amount']>0){
			$display_total = $display_total + $myrow['amount'];
			$items .=	'<tr>
							<td width="240">'.$description.'</td>
							<td width="75" align="right">'.$gl_code.'</td>
							<td width="75" align="right"></td>
							<td width="65" align="right"></td>
							<td width="85" align="right">'.number_format($myrow['amount'],2,'.',',').'</td>
						</tr>';
		}
	}
	
    if ($myrow['amount'] > 0 ) 
    	$debit += $myrow['amount'];
    else 
    	$credit += $myrow['amount'];
		
		//$cheque_no =  $myrow['cheque_no'];
		
		
	//$payee_name =  payment_person_name_cheque($peson_typrid,$personid);
	//$payee_name = payment_person_name_cheque($type_id_array[$i], $trans_no_array[$i], $peson_typeid, $personid);

	$bank_trans = db_fetch(get_bank_trans($type_id_array[$i], $trans_no_array[$i]));
	
	$cheque_no =  $bank_trans['cheque_reference'];

//------get payee name form the banktrans table----
	$payee_name = payment_person_name_cheque($type_id_array[$i], $trans_no_array[$i], $bank_trans['person_type_id'], $bank_trans['person_id']);
        $count = $count+1;
}



	
//-------------------changed code 15.08.2012---------------	
	$result_paymode = get_payment_mode($cheque_no);
	while ($paymode_row = db_fetch($result_paymode)) {
		$bank_act = $paymode_row['bank_act'];//----danu add pay mode 30.11.2011	
	}
//-------------------changed code 15.08.2012---------------	
	
	if($bank_act != ''){
		$sql_bank_accounts = get_bank_account_type($bank_act);///------changed code 15.08.2012-----------
		$result_bank_account = db_query($sql_bank_accounts);
		$account_types = db_fetch_assoc($result_bank_account);
		$account_type = $account_types['account_type'];
		switch ($account_type) {
			case 0:
				$voucher_eading =  ' CHEQUE PAYMENT VOUCHER';
				break;
			case 1:
				$voucher_eading =  ' CHEQUE PAYMENT VOUCHER';
				break;
			case 2:
				$voucher_eading =  'CASH PAYMENT VOUCHER';
				break;
			case 3:
				$voucher_eading =  'CASH PAYMENT VOUCHER';
				break;
		}
	}

		$amount = abs($display_total);
		$iNumber = number_format($display_total,2,'.','');
		$number_amount = number_format($iNumber,2,'.',',');	
		
		$values = explode(".", $iNumber);
		$rupees  = intval($values[0]);
		$cents  = intval($values[1]);
		
		 str_pad($iNumber,'0',STR_PAD_LEFT);
		
		switch ($cents) {
			case 0:
				$amount_w_db =  CLASS_Numbertoword::convert($rupees,'en').' Only';
				break;
			case 1:
				$amount_w_db =  CLASS_Numbertoword::convert($rupees,'en').' and '.CLASS_Numbertoword::convert($cents,'en').' Cent Only';
				break;
			default:
			   $amount_w_db = CLASS_Numbertoword::convert($rupees,'en').' and '.CLASS_Numbertoword::convert($cents,'en').' Cents Only';
		}
// -------------total row of invoice-------------		
		$items .=	'<tr>
						<td width="455" colspan="3" align = "center">Total</td>
						<td width="85" align="right">'.number_format($display_total,2,'.',',').'</td>
					</tr>';
// -------------total row of invoice-------------	
		 
$word = '<table cellspacing="0" cellpadding="1" border="0">
    <tr>
        <td>Amount of '.$amount_w_db.'</td>
    </tr>
</table>';

$word_last = '<table cellspacing="0" cellpadding="1" border="0">
    <tr>
        <td>Received with thanks from  <b>'.$company_name.'</b> The sum of Rupees  <b>'.$amount_w_db.'</b></td>
    </tr>
</table>';


//SetFont($family, $style='', $size=0, $fontfile='', $subset='default')
// add a page
$pdf->AddPage();

$record = explode(",",$company_address);
$name = $record[0];
$name2 = $record[1];
$vat = $record[2];

$totname = $name.','.$name2;

$pdf->SetFont('helvetica', '', 10);
$pdf->Text(137, 10, 'VOUCHER NO : ');//---voucher number
$pdf->SetFont('times', '', 10);
$pdf->Text(165, 10, $trans_no);//---voucher number

$pdf->SetFont('helvetica', '', 10);
$pdf->Text(137, 17, 'SYSTEM GEN NO : ');//---voucher number
$pdf->SetFont('times', '', 12);
$pdf->Text(170, 16, $system_number);//---voucher number


//$pdf->SetFont('helvetica', '', 8);
//$pdf->Text(140, 14, 'System Number : '.$system_number);//---voucher number

$pdf->SetFont('times', 'BI', 16);
$pdf->Text(14, 5, $company_name);//---company name

$pdf->SetFont('times', '', 10);
$pdf->Text(14, 11, $totname);//---company address
$pdf->Text(14, 15, $vat);//---company contacts
$pdf->Text(14, 19, 'Tel : '.$company_phone.' Fax : '.$company_fax);//---company contacts
$pdf->Text(14, 23, $company_email);//---company emails

$pdf->SetFont('helvetica', 'UB', 16);
$pdf->Text(14, 28, $voucher_eading);//---voucher number------------

//==============amount in number================
$pdf->SetFont('times', 'B', 10);
$pdf->Text(137, 40, 'Date : ');
$pdf->SetFont('times', '', 10);
$pdf->Text(150, 40, $date);
$pdf->SetFont('times', 'B', 10);
$pdf->Text(137, 46, 'Cheque No : ');//---cheque number
$pdf->SetFont('times', '', 10);
$pdf->Text(165, 46, $cheque_no);//---cheque number

//==========payee name-----

$pdf->SetFont('times', 'B', 10);
$pdf->Text(14, 36, 'Description : ');
$pdf->SetFont('times', '', 10);
$pdf->Text(35, 36, $memo);

$pdf->SetFont('times', 'B', 10);
$pdf->Text(14, 42, 'Name : ');
$pdf->SetFont('times', '', 10);
$pdf->Text(30, 42, html_entity_decode($payee_name));
$pdf->SetFont('times', 'B', 10);
$pdf->Text(14, 47, 'Address : ');
$pdf->SetFont('times', '', 10);
$pdf->Text(30, 47, $supp_address);
$pdf->SetFont('times', 'B', 10);
$pdf->Text(14, 53, 'Payment : ');

//------------------amount in word-----------------
$amout_words = <<<EOD
	<table width="500" height="42" border="0" cellpadding="1" cellspacing="0">
		<tr align="left">
			<td width="500">
			$word
			</td>
    </tr>
</table>
EOD;

$amout_words_last = <<<EOD
	<table width="500" height="42" border="0" cellpadding="1" cellspacing="0">
		<tr align="left">
			<td width="500">
			$word_last
			</td>
    </tr>
</table>
EOD;

$item_table = <<<EOD
				<table width="500" height="500" border="1" cellpadding="1" cellspacing="0">
						<tr style="background-color:#D8ECF2; font-weight:bold">
							<td width="240" align="center">Description</td>
							<td width="75" align="center">GL Code</td>
							<td width="75" align="center"><b>Before VAT Value</b></td>
							<td width="65" align="center"><b>VAT %</b></td>
							<td width="85" align="center"><b>Invoice Value</b></td>
						</tr>
					$items
				</table>
EOD;

$amount_table = <<<EOD
					<table width="180" border="1" height="20">
						<tr>
							<td height="20">
								&nbsp;&nbsp;Rs. : **$number_amount
							</td>
						</tr>
					</table>
EOD;

if($count==1){

$val = 65+(30*$count);
$val_potioion = 75+(5*$count);
	
}else{
	
$val = 70+(10*$count);
$val_potioion = 80+(9*$count);
$line_posi = 75+(9*$count);
$word_amt = 92+(9*$count);
	
}
// Print text using writeHTMLCell()
$pdf->writeHTMLCell($w=0, $h=0, $x=14, $y=220, $memo, $border=0, $ln=1, $fill=0, $reseth=true, $align='L', $autopadding=true);


//$pdf->writeHTMLCell($w=0, $h=0, $x=14, $y=230, $amout_words, $border=0, $ln=1, $fill=0, $reseth=true, $align='L', $autopadding=true);

$pdf->writeHTMLCell($w=0, $h=500, $x=14, $y=65, $item_table, $border=0, $ln=1, $fill=0, $reseth=true, $align='L', $autopadding=true);//item details

$pdf->writeHTMLCell($w=0, $h=0, $x=137, $y=28, $amount_table, $border=0, $ln=1, $fill=0, $reseth=true, $align='L', $autopadding=true);//number amount


//$memo


$pdf->SetFont('times', 'B', 10);
$pdf->Text(24, $val_potioion, 'Prepared');
$pdf->Text(100, $val_potioion, 'Checked by');
$pdf->Text(180, $val_potioion, 'Authorized');
$pdf->SetFont('times', 'B', 10);
$pdf->Text(14, $line_posi, '------------------------------------------------------------------------------------------------------------------------------------------------------------------');

$pdf->SetFont('times', '', 10);
//$pdf->writeHTMLCell($w=0, $h=0, $x=14, $y=250, $amout_words_last, $border=0, $ln=1, $fill=0, $reseth=true, $align='L', $autopadding=true);//number amount

$pdf->SetFont('times', 'BI', 10);
$pdf->Text(14, $word_amt, 'Amount of');
$pdf->SetFont('times', '', 10);
$pdf->Text(31, $word_amt, ''.$amount_w_db);

$pdf->SetFont('times', 'BI', 10);
$pdf->Text(170, $word_amt, 'Signature Of Receiver');

//-----value clearing here----
$items ='';
$display_total = 0;

$i++;
}
$pdf->lastPage();
//Close and output PDF document
$pdf->Output('voucher.pdf', 'I');

//============================================================+
// END OF FILE                                                
//============================================================+
?>