<?php  ob_start(); 

	define("TB_PREF","0_");
		
	include_once ('config.php');
	/*$conn = mysql_connect("localhost","root","");
	mysql_select_db("danu_acc",$conn);*/

	require_once ('class.numbertoword.php');

	$page_security = 'SA_SUPPTRANSVIEW';
	$path_to_root = "..";
	
	include_once($path_to_root . "/purchasing/includes/purchasing_db.inc");
	include_once($path_to_root . "/includes/session.inc");
	
	include_once($path_to_root . "/purchasing/includes/purchasing_ui.inc");



	$sql_cname = "SELECT * FROM 0_sys_prefs WHERE name = 'coy_name'";
	$result_cname = mysql_query($sql_cname);
	$company_name = db_fetch($result_cname,0,'value');//-----------------campnay name-----------
	
	$sql_caddress = "SELECT * FROM 0_sys_prefs WHERE name = 'postal_address'";
	$result_caddress = mysql_query($sql_caddress);
	$company_address = db_fetch($result_caddress,0,'value');//-----------------campnay address-----------
	
	$sql_cphone = "SELECT * FROM 0_sys_prefs WHERE name = 'phone'";
	$result_cphone = mysql_query($sql_cphone);
	$company_phone = db_fetch($result_cphone,0,'value');//-----------------campnay phone----------
	
	$sql_cfax = "SELECT * FROM 0_sys_prefs WHERE name = 'fax'";
	$result_cfax = mysql_query($sql_cfax);
	$company_fax = db_fetch($result_cfax,0,'value');//-----------------campnay fax-----------
	
	$sql_cemail = "SELECT * FROM 0_sys_prefs WHERE name = 'email'";
	$result_cemail = mysql_query($sql_cemail);
	$company_email = db_fetch($result_cemail,0,'value');//-----------------campnay email-----------
	



	if (isset($_GET["trans_no"])){
		$trans_no = $_GET["trans_no"];
	}elseif (isset($_POST["trans_no"]))	{
		$trans_no = $_POST["trans_no"];
	}
	
	$supp_trans = new supp_trans(ST_SUPPINVOICE);
	read_supp_invoice($trans_no, ST_SUPPINVOICE, $supp_trans);
	$supplier_curr_code = get_supplier_currency($supp_trans->supplier_id);
	
	$sql_item_chk = "SELECT * FROM ".TB_PREF."supp_invoice_items WHERE supp_trans_no = ".$trans_no."";
	$result_item_chk = mysql_query($sql_item_chk);
	
	$gl_code = db_fetch($result_item_chk,0,'gl_code');
	
	if($gl_code == 0){
		$sql_items = "SELECT *, unit_price AS FullUnitPrice, ".TB_PREF."supp_invoice_items.quantity AS qty,
					".TB_PREF."item_codes.description
					FROM
					".TB_PREF."supp_invoice_items
					INNER JOIN ".TB_PREF."item_codes ON ".TB_PREF."item_codes.stock_id = ".TB_PREF."supp_invoice_items.stock_id
					WHERE supp_trans_type = 20
					AND supp_trans_no = ".$trans_no." ORDER BY ".TB_PREF."supp_invoice_items.id";
				
		$result_items = mysql_query($sql_items);
		while($row_items = mysql_fetch_array($result_items)){
			 $items .=	'<tr>
							<td width="290">'.$row_items['description'].'</td>
							<td width="100" align="right">'.number_format(($row_items['FullUnitPrice']*$row_items['qty']),2,'.','').'</td>
							<td width="65" align="right">'.number_format(($row_items['unit_tax']*$row_items['qty']),2,'.','').'</td>
							<td width="85" align="right">'.number_format(($row_items['unit_tax']*$row_items['qty']+$row_items['FullUnitPrice']*$row_items['qty']),2,'.','').'</td>
						</tr>';
		}
	
	}else{
	
		//$sql_items = "SELECT * FROM ".TB_PREF."chart_master WHERE account_code = ".$gl_code."";
		
	 	$sql_items = "SELECT
						".TB_PREF."chart_master.account_code,
						".TB_PREF."chart_master.account_code2,
						".TB_PREF."chart_master.account_name,
						".TB_PREF."chart_master.account_type,
						".TB_PREF."chart_master.inactive,
						".TB_PREF."supp_invoice_items.gl_code,
						".TB_PREF."supp_invoice_items.supp_trans_no,
						".TB_PREF."supp_invoice_items.unit_price,
						".TB_PREF."supp_invoice_items.description
						FROM
						".TB_PREF."chart_master
						INNER JOIN ".TB_PREF."supp_invoice_items ON ".TB_PREF."chart_master.account_code = ".TB_PREF."supp_invoice_items.gl_code WHERE supp_trans_no = ".$trans_no."";
		
		
		
		
		$result_items = mysql_query($sql_items);
		while($row_items = mysql_fetch_array($result_items)){
			 $items .=	'<tr>
							<td width="290">'.$row_items['account_name'].'</td>
							<td width="100" align="right">'.number_format($row_items['unit_price'],2,'.','').'</td>
							<td width="65" align="right"></td>
							<td width="85" align="right">'.number_format($row_items['unit_price'],2,'.','').'</td>
						</tr>';
		}
	}

	$display_sub_tot = $supp_trans->ov_amount;
	
	$tax_items = get_trans_tax_details(ST_SUPPINVOICE, $trans_no);
	
	$display_total = $supp_trans->ov_amount + $supp_trans->ov_gst;
	
	$amount = abs($display_total);
	$payee_name = html_entity_decode($supp_trans->supplier_name);
	
	$sql_supp = "SELECT * FROM ".TB_PREF."suppliers WHERE ".TB_PREF."suppliers.supplier_id = ".$supp_trans->supplier_id."";
	$supp_result = mysql_query($sql_supp);
	
	$supp_address = db_fetch($supp_result,0,'address');

	//$iNumber = 223233.25;number_format($row_strsql['roomprice'],2,'.','')
	$iNumber = number_format($amount,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';
		}
		
		 
		 
		$date = $supp_trans->tran_date;

$word = '<table cellspacing="0" cellpadding="1" border="0">
    <tr>
        <td>'.$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>';


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('L', PDF_UNIT, 'PV', true, 'UTF-8', false);
$pdf = new TCPDF('L', PDF_UNIT, 'A5', true, 'UTF-8', false);

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

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


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

// 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 font
$pdf->SetFont('helvetica', '', 10);
// add a page
$pdf->AddPage();

$pdf->SetFont('helvetica', '', 16);
$pdf->Text(140, 10, 'VOUCHER NO : ');//---voucher number
$pdf->SetFont('Courier', '', 16);
$pdf->Text(185, 10, $trans_no);//---voucher number

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

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

$pdf->SetFont('helvetica', 'UB', 16);
$pdf->Text(14, 26, 'PAYMENT VOUCHER CHEQUE');//---voucher number<strong></strong>

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

//==========payee name-----
$pdf->SetFont('helvetica', 'B', 10);
$pdf->Text(14, 34, 'Name : ');
$pdf->SetFont('Courier', '', 10);
$pdf->Text(30, 34, $payee_name);
$pdf->SetFont('helvetica', 'B', 10);
$pdf->Text(14, 40, 'Address : ');
$pdf->SetFont('Courier', '', 10);
$pdf->Text(30, 40, $supp_address);
$pdf->SetFont('helvetica', 'B', 10);
$pdf->Text(14, 45, 'Payment : ');
$pdf->SetFont('Courier', '', 10);

//------------------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="42" border="1" cellpadding="1" cellspacing="0">
						<tr>
							<td width="290"></td>
							<td width="100" 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. : **$iNumber
							</td>
						</tr>
					</table>
EOD;

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

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

$pdf->writeHTMLCell($w=0, $h=0, $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=140, $y=20, $amount_table, $border=0, $ln=1, $fill=0, $reseth=true, $align='L', $autopadding=true);//number amount


$pdf->SetFont('helvetica', 'B', 10);
$pdf->Text(24, 112, 'Prepared');
$pdf->Text(100, 112, 'Checked by');
$pdf->Text(180, 112, 'Authorized');
$pdf->SetFont('Courier', 'B', 10);
$pdf->Text(14, 115, '----------------------------------------------------------------------------------------');

$pdf->SetFont('helvetica', '', 10);
/*$pdf->Text(24, 130, 'Received with thanks from ');
$pdf->SetFont('helvetica', 'B', 10);
$pdf->Text(67, 130, $company_name);
$pdf->SetFont('helvetica', '', 10);
$pdf->Text(112, 130, 'The sum of Rupees ');
*/
$pdf->SetFont('Courier', '', 10);
$pdf->writeHTMLCell($w=0, $h=0, $x=14, $y=120, $amout_words_last, $border=0, $ln=1, $fill=0, $reseth=true, $align='L', $autopadding=true);//number amount


$pdf->SetFont('helvetica', 'BI', 10);
$pdf->Text(14, 135, 'Rs : ');
$pdf->SetFont('Courier', '', 10);
$pdf->Text(22, 135, '**'.$iNumber);

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



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

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