<?php
require_once '../assets/plugins/pdf/tcpdf.php'; 
$pdf = new PDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
 
include("../config.php");
if(isset($_GET['vendor_id']) && !empty($_GET['vendor_id'])) {
		$vendor_id = strtoupper($_GET['vendor_id']);
	}

	$sd = mysqli_real_escape_string($conn, date('Y-m-d', strtotime($_GET['start_date'])));
	$ed = mysqli_real_escape_string($conn, date('Y-m-d', strtotime($_GET['end_date'])));

	if(!empty($sd) && !empty($ed) && !empty($vendor_id)){
		$master_vendor       = combine(getAllRecords('vendor_master'), 'id', 'name');
	    $vendor_name 	     = @$master_vendor[$vendor_id];
	    
	    $account = mysqli_query($conn, "SELECT * FROM `act_master` WHERE `ac_name` = '".$vendor_name."'");
	   	$account_row = mysqli_fetch_assoc($account);
	   
		$trans = "SELECT *
		FROM trans 
		WHERE DATE(voucher_date) >= '".$sd."' AND DATE(voucher_date) <= '".$ed."' AND (`flag` = 'BILLINg' OR `flag` = 'GENERAL SALES') AND `branch_id` = '".$_SESSION['branch_id']."' AND `company_id` = '".$_SESSION['company_id']."' AND `balance` > 0  AND `act_code` = '".$account_row['act_code']."' GROUP BY act_code";

	}else{
		$trans = "SELECT trans.company_id, trans.branch_id, trans.act_code, act_master.act_code, act_master.ac_name, trans.voucher_date, trans.flag
		FROM act_master
		LEFT JOIN trans ON act_master.act_code=trans.act_code WHERE DATE(trans.voucher_date) >= '".$sd."' AND DATE(trans.voucher_date) <= '".$ed."' AND trans.(`flag` = 'BILLINg' OR `flag` = 'GENERAL SALES') AND trans.branch_id = '".$_SESSION['branch_id']."' AND trans.company_id = '".$_SESSION['company_id']."' AND trans.balance > 0 GROUP BY trans.act_code ORDER BY ac_name ASC";
	}

	$result 			= mysqli_query($conn,$trans);
	$total       		= mysqli_num_rows($result);
	$master_accounts    = combine(getAllRecords('vendor_master'), 'id', 'name');
	$master_company     = combine(getAllRecords('company_mst'), 'id', 'name');
	$table       	= '';
	$total_count    = 0;
	$date = $ed;
$company       = "SELECT * FROM company_mst WHERE id = '".$_SESSION['company_id']."'";
	$company_row   = mysqli_query($conn,$company);
	$company_res   = mysqli_fetch_assoc($company_row);
	
	$output = '';
	$output .= '<html>
	<table style="width: 100%" cellpadding="1">
	<thead>
		<tr style="border: 0.1px solid black;">
			<td style="text-align:center;font-size:15px;"><strong>'.@$master_company[$_SESSION['company_id']].'</strong></td>
		</tr>
		<tr style="border: 0.1px solid black;">
			<td style="text-align:center;font-size:9px;">'.$company_res['address'].'</td>
		</tr>

		<tr style="border: 0.1px solid black;">
			<td style="text-align:center;font-size:9px;">Mobile : '.$company_res['phone'].'</td>
		</tr>

		<tr style="border: 0.1px solid black;">
			<td style="text-align:center;font-size:9px;">E-mail : '.$company_res['email'].'</td>
		</tr>
		<tr>
			<td style="width:100%"></td>
		</tr>
		<tr style="border: 0.1px solid black;text-align:center;">
			<td style="text-align:center;border-bottom:0.1pt solid black">OUTSTANDING REPORT <strong>START FROM</strong> : '.$_GET['start_date'].' <strong>TO</strong> :  '.$_GET['end_date'].'</td>
		</tr>
	</thead>
	<tbody>
	    <tr style="border: 0.1px solid black;">
			<td width="5%" style="text-align: center;"><strong>SR.NO</strong></td>
			<td width="15%" style="text-align: center;"><strong>BILL NO</strong></td>
			<td width="10%" style="text-align: center;"><strong>BILL DATE</strong></td>
			<td width="10%" style="text-align: center;"><strong>BILL AMOUNT</strong></td>
			<td width="10%" style="text-align: center;"><strong>RECEIVED</strong></td>
			<td width="10%" style="text-align: center;"><strong>BALANCE</strong></td>
			<td width="15%" style="text-align: center;"><strong>BILL SUB.BY</strong></td>
			<td width="15%" style="text-align: center;"><strong>BILL SUB.DATE</strong></td>
			<td width="10%" style="text-align: center;"><strong>DAYS</strong></td>
	    </tr>';
	$t_bill_amnt = 0;
	$t_received  = 0;
	$t_balance   = 0;
	$bill_amnt = 0;

	while ($row = mysqli_fetch_array($result)) {
		//GET BILL RECORD
		$get_bill = mysqli_query($conn, "SELECT * FROM `trans` WHERE act_code = '".$row['act_code']."' AND (`flag` = 'BILLINg' OR `flag` = 'GENERAL SALES') AND balance > 0 AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'");

		$master_account = combine(getAllRecords('act_master'), 'act_code', 'ac_name');

		$output .= '<tr>';
		$output .= '<td colspan="9" class="report-bg" style="border-top:1px solid black;border-bottom:1px solid black;text-align:center"><b>'.@$master_account[$row['act_code']].'</b></td>';
		$output .= '</tr>';

		$bill_amnt = 0;
		$received  = 0;
		$balance   = 0;
		$cou = 1;
		while ($get_bill_row 	= mysqli_fetch_assoc($get_bill)) {
			
			//TOTAL COUNT 
			$bill_amnt   += $get_bill_row['amount'];
			$received    += $get_bill_row['adjust'];
			$balance     += $get_bill_row['balance'];

			// GRAND TOTAL
			$t_bill_amnt += $get_bill_row['amount'];
			$t_received  += $get_bill_row['adjust'];
			$t_balance   += $get_bill_row['balance'];

			//GET BILL SUBMIT DATE & DAYS & SYBMITTED BY
			$bill_data = mysqli_query($conn, "SELECT * FROM `billing` WHERE `invoice_no` = '".$get_bill_row['voucher_no']."'");
			$bill_data_row = mysqli_fetch_assoc($bill_data);

			if($bill_data_row['bill_submited_date'] != ''){
				$bsd  = risFormatDate($bill_data_row['bill_submited_date']);
				$days = date_diffrence($bill_data_row['bill_submited_date'],$date);
			}else{
				$bsd = '';
				$days = '';
			}


			$output .= '<tr class="text-center">';
			$output .= '<td width="5%" style="text-align: center;">'.$cou++.'</td>';
			$output .= '<td width="15%" style="text-align: left;">'.$get_bill_row['voucher_no'].'</td>';
			$output .= '<td width="10%" style="text-align: center;">'.risFormatDate($get_bill_row['voucher_date']).'</td>';
			$output .= '<td width="10%" style="text-align: center;">'.customFormatMoney($get_bill_row['amount']).'</td>';
			$output .= '<td width="10%" style="text-align: center;">'.customFormatMoney($get_bill_row['adjust']).'</td>';
			$output .= '<td width="10%" style="text-align: center;">'.customFormatMoney($get_bill_row['balance']).'</td>';
			$output .= '<td width="15%" style="text-align: center;">'.$bill_data_row['bill_submited_by'].'</td>';
			$output .= '<td width="15%" style="text-align: center;">'.$bsd.'</td>';
			$output .= '<td width="10%" style="text-align: center;">'.$days.'</td>';
			$output .= '</tr>';
		}
		$output .= '<tr>';
		$output .= '<td width="30%" colspan="3" style="border-top:1px dashed balck;text-align:left"><b>TOTAL</b></td>';

		$output .= '<td width="10%" style="border-top:1px dashed balck;text-align:center"><b>'.customFormatMoney($bill_amnt).'</b></td>';

		$output .= '<td width="10%" style="border-top:1px dashed balck;text-align:center"><b>'.customFormatMoney($received).'</b></td>';

		$output .= '<td width="10%" style="border-top:1px dashed balck;text-align:center"><b>'.customFormatMoney($balance).'</b></td>';

		$output .= '</tr>';
	}
	$output .= '<tr>';
	$output .= '<td width="30%" colspan="3" style="border-top:1px dashed balck"><b>GRAND TOTAL</b></td>';
	$output .= '<td width="10%" style="border-top:1px dashed balck;text-align:center"><b>'.customFormatMoney($t_bill_amnt).'</b></td>';
	$output .= '<td width="10%" style="border-top:1px dashed balck;text-align:center"><b>'.customFormatMoney($t_received).'</b></td>';
	$output .= '<td width="10%" style="border-top:1px dashed balck;text-align:center"><b>'.customFormatMoney($t_balance).'</b></td>';
	$output .= '</tr>';

	$output .='
		</tbody>
	</table>
	</html>';
	$title = 'OUTSTANDING REPORT';
	$fname = 'outstanding_report.pdf';

	$pdf->SetPrintHeader(true);
	$pdf->SetPrintFooter(true);


	$pdf->SetAutoPageBreak(TRUE, 40);
	$pdf->SetFont('helvetica', '', 8, '', true);

	$pdf->AddPage('P', 'A4');
	$pdf->writeHTML($output, true, false, false, false, '');

	$pdf->SetTitle($title);
	$pdf->IncludeJS("print();");
	$pdf->Output($fname);

      
$pdf->SetPrintHeader(true);
$pdf->SetPrintFooter(true);
$pdf->writeHTML($output, true, false, false, false, '');
$pdf->SetTitle($title);
$pdf->IncludeJS("print();");
$pdf->Output($fname);


?>