<?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);
$pdf->SetFont('helvetica', '', '9', '', true);

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

$new_company           = "SELECT * FROM `company_mst` WHERE `id` = '".$_SESSION['company_id']."'";
$new_company_res       = mysqli_query($conn,$new_company);
$new_company_row       = mysqli_fetch_array($new_company_res);

$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'] + $rows['opening_debit'];
	$tr_bal_cr 		= $rows['tr_bal_cr'] + $rows['opening_credit'];
	$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;text-align:center">'.$new_company_row['name'].'</td>
		</tr>
		<tr>
			<td style="width:100%;font-size:8px;text-align:center">'.$new_company_row['address'].'</td>
		</tr>

		<tr>
			<td style="width:100%;font-size:11px;text-align:center;font-weight:bolder">'.$act_name.'</td>
		</tr>

		<tr>
			<td style="width:100%;font-size:10px;text-align:center;">'.risFormatDate($_GET['p3'],'d-M-y').' To Date '.risFormatDate($_GET['p4'],'d-M-y').'</td>
		</tr>
		
		<tr>
			<td style="width:8%;font-size:8px;border-top:1px solid black;font-weight:bolder;border-bottom:1px solid black;text-align:center;">Date</td>

			<td style="width:10%;font-size:8px;border-top:1px solid black;font-weight:bolder;text-align:right;border-bottom:1px solid black;;text-align:center;">Voucher No</td>

			<td style="width:14%;font-size:8px;border-top:1px solid black;font-weight:bolder;border-bottom:1px solid black;;text-align:left;">V. Type</td>

			<td style="width:34%;font-size:8px;border-top:1px solid black;font-weight:bolder;border-bottom:1px solid black;;text-align:left;">Particulars</td>

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

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

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

			<td style="width:4%;font-size:8px;border-top:1px solid black;font-weight:bolder;text-align:right;border-bottom:1px solid black;;text-align:right;"></td>
		</tr>

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

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

			<td style="width:14%;font-size:8px;border-top:1px solid black;"></td>

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

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

			<td style="width:10%;font-size:8px;border-top:1px solid black;text-align:right;font-weight:bolder">'.customFormatMoney($opening_credit).'</td>
			<td style="width:10%;font-size:8px;border-top:1px solid black;text-align:right;font-weight:bolder"></td>
			<td style="width:4%;font-size:8px;border-top:1px solid black;text-align:right;font-weight:bolder"></td>
		</tr>
		';
	   	if(count($product) > 0 ) {
	   		$cl_bal_1 = $opening_debit-$opening_credit;
	   		foreach ($product as $prod) {
     	   		$cl_bal_1 = ($cl_bal_1 + $prod['debit']) - $prod['credit'];

	   			$vno = $prod['voucher'];
	   			$tbl .='
	   			
	   			<tr>
					<td style="width:8%;font-size:8px;;text-align:center;">'.risFormatDate($prod['date'],'d-m-y').'</td>

					<td style="width:10%;font-size:8px;text-align:center">'.$vno.'</td>
					<td style="width:14%;font-size:8px;">'.$prod['type'].'</td>

					<td style="width:34%;font-size:8px;">'.$prod['account'].'</td>

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

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

					<td style="width:10%;font-size:8px;text-align:right;">'.customFormatMoney(abs($cl_bal_1)).'</td>';

					if($cl_bal_1 < 0) {
            			$tbl .='
    					<td style="width:4%;font-size:8px;text-align:right;">CR</td>';
    				}else{
        	   			$tbl .='
    					<td style="width:4%;font-size:8px;text-align:right;">DR</td>';
    				}	
    	   			$tbl .='</tr>';
    	   			if($prod['remarks']!=''){
                        $tbl .='<tr>
     					<td style="width:8%;font-size:8px;text-align:center;"></td>
    					<td style="width:10%;font-size:8px;text-align:center;"></td>
	    				<td style="width:14%;font-size:8px;"></td>
		    			<td style="width:34%;font-size:6px;">'.$prod['remarks'].'</td>
			    		<td style="width:10%;font-size:8px;text-align:right;"></td>
				    	<td style="width:10%;font-size:8px;text-align:right;"></td>
    					<td style="width:10%;font-size:8px;text-align:right;"></td>
  	     				<td style="width:4%;font-size:8px;text-align:right;"></td>
		    		</tr>';
		    	}
	   		}
	   	}

	   	$tbl .='
		<tr>
			<td style="width:8%;font-size:8px;"></td>
			<td style="width:10%;font-size:8px;font-weight:bolder"></td>
			<td style="width:14%;font-size:8px;"></td>
			<td style="width:34%;font-size:8px;"></td>
			<td style="width:10%;font-size:8px;text-align:right;border-top:1px solid black;">'.customFormatMoney($tr_bal_dr).'</td>
			<td style="width:10%;font-size:8px;text-align:right;border-top:1px solid black;">'.customFormatMoney($tr_bal_cr).'</td>
			<td style="width:10%;font-size:8px;text-align:right;border-top:1px solid black;"></td>
			<td style="width:4%;font-size:8px;text-align:right;border-top:1px solid black;"></td>
		</tr>
		';

		$tbl .='
		<tr>
			<td style="width:8%;font-size:8px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
			<td style="width:10%;font-size:8px;"></td>
			<td style="width:14%;font-size:8px;"></td>
			<td style="width:34%;font-size:8px;font-weight:bolder">Closing Balance</td>';

			if(($cl_bal_dr - $cl_bal_cr) < 0){
            	$tbl .='
    			<td style="width:10%;font-size:8px;text-align:right;">'.customFormatMoney(abs($cl_bal_dr - $cl_bal_cr)).'</td>';
    		}else{
            	$tbl .='
    			<td style="width:10%;font-size:8px;text-align:right;"></td>';
    		}	

			if(($cl_bal_dr - $cl_bal_cr) < 0){
            	$tbl .='
    			<td style="width:10%;font-size:8px;text-align:right;"></td>';
    		}else{
            	$tbl .='
    			<td style="width:10%;font-size:8px;text-align:right;">'.customFormatMoney(abs($cl_bal_dr - $cl_bal_cr)).'</td>';
    		}	

           	$tbl .='
			<td style="width:10%;font-size:8px;text-align:right;font-weight:bolder;"></td>
			<td style="width:4%;font-size:8px;text-align:right;font-weight:bolder;"></td>
		</tr>
		';

		$tbl .='
		<tr>
			<td style="width:8%;font-size:8px;"></td>
			<td style="width:10%;font-size:8px;font-weight:bolder"></td>
			<td style="width:14%;font-size:8px;"></td>
			<td style="width:34%;font-size:8px;"></td>';

			if(($cl_bal_dr - $cl_bal_cr) < 0){
				$tcr = (abs($cl_bal_dr - $cl_bal_cr)) +$cl_bal_dr;
            	$tbl .='
    			<td style="width:10%;font-size:8px;text-align:right;font-weight:bolder;border-top:1px solid black;border-bottom:1px solid black;">'.customFormatMoney($tcr).'</td>';
    		}else{
            	$tbl .='
    			<td style="width:10%;font-size:8px;text-align:right;font-weight:bolder;border-top:1px solid black;border-bottom:1px solid black;">'.customFormatMoney($cl_bal_dr).'</td>';
    		}

			if(($cl_bal_dr - $cl_bal_cr) < 0){
            	$tbl .='
    			<td style="width:10%;font-size:8px;text-align:right;font-weight:bolder;border-top:1px solid black;border-bottom:1px solid black;">'.customFormatMoney($cl_bal_cr).'</td>';
    		}else{
				$tcr = (abs($cl_bal_dr - $cl_bal_cr)) +$cl_bal_cr;
            	$tbl .='
    			<td style="width:10%;font-size:8px;text-align:right;font-weight:bolder;border-top:1px solid black;border-bottom:1px solid black;">'.customFormatMoney($tcr).'</td>';
    		}	
           	$tbl .='
			<td style="width:10%;font-size:8px;text-align:right;font-weight:bolder;border-top:1px solid black;border-bottom:1px solid black;"></td>
			<td style="width:4%;font-size:8px;text-align:right;font-weight:bolder;border-top:1px solid black;border-bottom:1px solid black;"></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');
