<?php
include("../../config.php");
require_once('../../assets/plugins/tcpdf/tcpdf.php');
$master_company   = combine(getAllRecords('companies'), 'id', 'name');
$company_name     = @$master_company[$_SESSION['company_id']];

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

$pdf->SetAutoPageBreak(TRUE, 5);

$p1 = $_GET['p1'];
$p2 = $_GET['p2'];

$query = mysqli_query($conn, "SELECT * FROM `ledger_print` WHERE `act_code` = '".$p1."' AND `user_id` = '".$p2."' ORDER BY id");
$product = array();
while ($rows = mysqli_fetch_assoc($query)) {
	$product[] 		= $rows;
	$tr_bal_dr 		= $rows['tr_bal_dr'];
	$tr_bal_cr 		= $rows['tr_bal_cr'];
	$cl_bal_dr 		= $rows['cl_bal_dr'];
	$cl_bal_cr 		= $rows['cl_bal_cr'];	
	$cl_bal_flag 	= $rows['cl_bal_flag'];
	$opening_debit 	= $rows['opening_debit'];
	$opening_credit = $rows['opening_credit'];
	$opening_balance= $rows['opening_balance'];
	$opening_flag 	= $rows['opening_flag'];
	$act_code 		= $rows['act_code'];
	$act_name 		= $rows['act_name'];
}
$max_product_on_page = 60;
$array_parts = array_chunk($product, $max_product_on_page);

/*foreach ($array_parts as $product) {*/

	$tbl = '<table cellpadding="3">
		<tr>
			<td style="width:100%;font-size:13px;font-weight:bolder">'.$company_name.'</td>
		</tr>
		<tr>
			<td style="width:100%;font-size:10px;border-top:1px solid black;border-left:1px solid black;border-right:1px solid black">Ledger Account From Date : '.risFormatDate($_GET['p3'],'d/m/Y').' To Date '.risFormatDate($_GET['p4'],'d/m/Y').'</td>
		</tr>
		<tr>
			<td style="width:100%;font-size:9px;border-left:1px solid black;border-right:1px solid black;font-weight:bolder">'.$act_code.' - '.$act_name.'</td>
		</tr>
		<tr>
			<td style="width:10%;font-size:7px;border-left:1px solid black;border-right:1px solid black;border-top:1px solid black;font-weight:bolder">Date</td>

			<td style="width:10%;font-size:7px;border-left:1px solid black;border-right:1px solid black;border-top:1px solid black;font-weight:bolder">Voucher</td>

			<td style="width:38%;font-size:7px;border-left:1px solid black;border-right:1px solid black;border-top:1px solid black;font-weight:bolder">Account</td>

			<td style="width:9%;font-size:7px;border-left:1px solid black;border-right:1px solid black;border-top:1px solid black;font-weight:bolder">Type</td>

			<td style="width:10%;font-size:7px;border-left:1px solid black;border-right:1px solid black;border-top:1px solid black;font-weight:bolder">Debit</td>

			<td style="width:10%;font-size:7px;border-left:1px solid black;border-right:1px solid black;border-top:1px solid black;font-weight:bolder">Credit</td>

			<td style="width:13%;font-size:7px;border-left:1px solid black;border-right:1px solid black;border-top:1px solid black;font-weight:bolder">Balance</td>
		</tr>

		<tr>
			<td style="width:10%;font-size:7px;border-left:1px solid black;border-right:1px solid black;border-top:1px solid black;"></td>

			<td style="width:10%;font-size:7px;border-left:1px solid black;border-right:1px solid black;border-top:1px solid black;"></td>

			<td style="width:38%;font-size:7px;border-left:1px solid black;border-right:1px solid black;border-top:1px solid black;font-weight:bolder">Opening Balance</td>

			<td style="width:9%;font-size:7px;border-left:1px solid black;border-right:1px solid black;border-top:1px solid black;"></td>

			<td style="width:10%;font-size:7px;border-left:1px solid black;border-right:1px solid black;border-top:1px solid black;text-align:right;font-weight:bolder">'.customFormatMoney($opening_debit).'</td>

			<td style="width:10%;font-size:7px;border-left:1px solid black;border-right:1px solid black;border-top:1px solid black;text-align:right;font-weight:bolder">'.customFormatMoney($opening_credit).'</td>

			<td style="width:13%;font-size:7px;border-left:1px solid black;border-right:1px solid black;border-top:1px solid black;text-align:right;font-weight:bolder">'.customFormatMoney($opening_balance).'&nbsp;'.$opening_flag.'</td>
		</tr>';
	   	if(count($product) > 0 ) {
	   		foreach ($product as $prod) {
	         	$tbl .='<tr>
	               <td style="width:10%;font-size:7px;border-left:1px solid black;border-right:1px solid black;text-align:center">'.risformatDate($prod['date'],'d-m-Y').'</td>

	               <td style="width:10%;font-size:7px;border-left:1px solid black;border-right:1px solid black;text-align:center">'.$prod['voucher'].'</td>

	               <td style="width:38%;font-size:7px;border-left:1px solid black;border-right:1px solid black;">'.$prod['account'].'</td>

	               <td style="width:9%;font-size:7px;border-left:1px solid black;border-right:1px solid black;text-align:center">'.$prod['type'].'</td>

	               <td style="width:10%;font-size:7px;border-left:1px solid black;border-right:1px solid black;text-align:right">'.customFormatMoney($prod['debit']).'</td>

	               <td style="width:10%;font-size:7px;border-left:1px solid black;border-right:1px solid black;text-align:right">'.customFormatMoney($prod['credit']).'</td>

	               <td style="width:13%;font-size:7px;border-left:1px solid black;border-right:1px solid black;text-align:right">'.customFormatMoney(abs($prod['balance'])).' &nbsp; '.$prod['flag'].'</td>
				</tr>';
				if($prod['remarks'] !=''){
					$tbl .='<tr>
		               <td style="width:10%;font-size:7px;border-left:1px solid black;border-right:1px solid black;text-align:center"></td>

		               <td style="width:10%;font-size:7px;border-left:1px solid black;border-right:1px solid black;text-align:center"></td>

		               <td style="width:38%;font-size:6px;border-left:1px solid black;border-right:1px solid black;">'.$prod['remarks'].'</td>

		               <td style="width:9%;font-size:7px;border-left:1px solid black;border-right:1px solid black;text-align:center"></td>

		               <td style="width:10%;font-size:7px;border-left:1px solid black;border-right:1px solid black;text-align:right"></td>

		               <td style="width:10%;font-size:7px;border-left:1px solid black;border-right:1px solid black;text-align:right"></td>

		               <td style="width:13%;font-size:7px;border-left:1px solid black;border-right:1px solid black;text-align:right"></td>
					</tr>';
				}
	      	}
	   	}

	   	$tbl .='<tr>
           <td style="width:10%;font-size:7px;border-left:1px solid black;border-right:1px solid black;text-align:center;border-top:1px solid black"></td>

           <td style="width:10%;font-size:7px;border-left:1px solid black;border-right:1px solid black;text-align:center;border-top:1px solid black"></td>

           <td style="width:38%;font-size:7px;border-left:1px solid black;border-right:1px solid black;border-top:1px solid black;text-align:right;font-weight:bolder">Transaction Total </td>

           <td style="width:9%;font-size:7px;border-left:1px solid black;border-right:1px solid black;text-align:center;border-top:1px solid black"></td>

           <td style="width:10%;font-size:7px;border-left:1px solid black;border-right:1px solid black;text-align:right;border-top:1px solid black;font-weight:bolder">'.customFormatMoney($tr_bal_dr).'</td>

           <td style="width:10%;font-size:7px;border-left:1px solid black;border-right:1px solid black;text-align:right;border-top:1px solid black;font-weight:bolder">'.customFormatMoney($tr_bal_cr).'</td>

           <td style="width:13%;font-size:7px;border-left:1px solid black;border-right:1px solid black;text-align:right;border-top:1px solid black"></td>
		</tr>';

		$tbl .='<tr>
           <td style="width:10%;font-size:7px;border-left:1px solid black;border-right:1px solid black;text-align:center;border-top:1px solid black;border-bottom:1px solid black"></td>

           <td style="width:10%;font-size:7px;border-left:1px solid black;border-right:1px solid black;text-align:center;border-top:1px solid black;border-bottom:1px solid black"></td>

           <td style="width:38%;font-size:7px;border-left:1px solid black;border-right:1px solid black;border-top:1px solid black;text-align:right;font-weight:bolder;border-bottom:1px solid black">Closing Balance </td>

           <td style="width:9%;font-size:7px;border-left:1px solid black;border-right:1px solid black;text-align:center;border-top:1px solid black;border-bottom:1px solid black"></td>

           <td style="width:10%;font-size:7px;border-left:1px solid black;border-right:1px solid black;text-align:right;border-top:1px solid black;font-weight:bolder;border-bottom:1px solid black">'.customFormatMoney($cl_bal_dr).'</td>

           <td style="width:10%;font-size:7px;border-left:1px solid black;border-right:1px solid black;text-align:right;border-top:1px solid black;font-weight:bolder;border-bottom:1px solid black">'.customFormatMoney($cl_bal_cr).'</td>

           <td style="width:13%;font-size:7px;border-left:1px solid black;border-right:1px solid black;text-align:right;border-top:1px solid black;border-bottom:1px solid black;font-weight:bolder">'.customFormatMoney(abs($cl_bal_dr - $cl_bal_cr)).' &nbsp; '.$cl_bal_flag.'</td>
		</tr>';

$tbl .='</table>';
$pdf->AddPage();
$pdf->writeHTML($tbl, true, false, false, false, '');
/*}*/
$pdf->SetPrintFooter(true);
$pdf->SetTitle('ACCOUNT LEDGER'.' - '.$act_code.'('.$act_name.')');
$pdf->Output('account_ledger'.'_'.$act_code.'('.$act_name.').pdf', 'I');
