<?php

      include "../include/mysql.php";

        include "../include/common.php";

		 extract($_REQUEST);

        //echo $val;

        $exp=explode("***",$val);

        $itemid=$exp[0];

        $fdate=date("Y-m-d",strtotime($exp[1]));

        $tdate=date("Y-m-d",strtotime($exp[2]));

		$fdateDisplay=date("d/m/Y",strtotime($exp[1]));

		$tdateDisplay=date("d/m/Y",strtotime($exp[2]));

       if($itemid=="All")
        {

             
					
				$icondn=" where booking_type='2'";
        
        
		
		}
        else
        {
               
              $icondn=" where booking_type='2' and ticketcategory='$itemid'";

        }   

			$table="<table style='width:100%;'><tr><td align='center'><b><span>அருள்மிகு மீனாட்சி சுந்தரேசுவரர் திருக்கோவில் <br>மதுரை</span> </b></td></tr><tr><td align='center'><b><span>ADVANCED BOOKING CONSOLIDATED DATEWISE REPORT</span></b></td></tr><tr><td align='center'><b><span>$fdateDisplay To  $tdateDisplay</span></b></td></tr></table><br />";	

           	 $table=$table."<table id='$field_display_table' width='850px;'  border='0' class='price1' cellpadding='8' cellspacing='6' style='border-collapse:collapse;'>
											<col style='width: 20%' class='col1'>
											<col style='width: 20%'>
											<col style='width: 20%'>
											<col style='width: 20%'>
											<col style='width: 20%'>	
											<col style='width: 20%'>	
										  <thead>			
												<tr>
																									
													<th align='left' class='pdftbl' style='text-align:left;'>TicketName</th>

													<th align='right' class='pdftbl' style='text-align:right;'>Price</th>

													<th align='right' class='pdftbl' style='text-align:right;' width='11%'>Start No.</th>

													<th align='right' class='pdftbl' style='text-align:right;' width='10%'>End No.</th>
													
													<th align='right' class='pdftbl' style='text-align:right;'>Sold</th>

													<th align='right' class='pdftbl' style='text-align:right;'>ChequeAmt.</th>

													<th align='right' class='pdftbl' style='text-align:right;'>Total</th>
																								
													</thead>";

				if($itemid=="All")
				{
			
					$cashdateCondn=" where created_date between '$fdate' and '$tdate' and cashtype='1'";

					$cashdateCondnCancel=" where created_date between '$fdate' and '$tdate' and cashtype='1' and cancel='1'";

				}
				else
				{
						$cashdateCondn=" where created_date between '$fdate' and '$tdate' and cashtype='1' and advbooktype='$itemid'";

						$cashdateCondnCancel=" where created_date between '$fdate' and '$tdate' and cashtype='1' and advbooktype='$itemid' and cancel='1'";

				}

			$getOverAllTotalDetails=$db->select(AB,"sum(rate)",$cashdateCondn);
		
			$getOverAllTotalDetailsFetch=$db->fetch($getOverAllTotalDetails);

			$overAllTotalCash=$getOverAllTotalDetailsFetch[0];

		$getOverAllTotalDetailsCancel=$db->select(AB,"sum(rate)",$cashdateCondnCancel);
		
		$getOverAllTotalDetailsCancelFetch=$db->fetch($getOverAllTotalDetailsCancel);

		$overAllTotalCashCancel=$getOverAllTotalDetailsCancelFetch[0];

			if($itemid=="All")
				{

			$carddateCondn=" where created_date between '$fdate' and '$tdate' and cashtype='2'";

				$carddateCondnCanel=" where created_date between '$fdate' and '$tdate' and cashtype='2' and cancel='1'";

				}
				else
				{
					$carddateCondn=" where created_date between '$fdate' and '$tdate' and cashtype='2' and advbooktype='$itemid'";

					$carddateCondnCanel=" where created_date between '$fdate' and '$tdate' and cashtype='2' and advbooktype='$itemid' and cancel='1'";

				}

			$getOverAllTotalDetailsCard=$db->select(AB,"sum(rate)",$carddateCondn);
		
			$getOverAllTotalDetailsCardfetch=$db->fetch($getOverAllTotalDetailsCard);

			$overAllTotalCard=$getOverAllTotalDetailsCardfetch[0];

$getOverTotalDetailsCardCancel=$db->select(AB,"sum(rate)",$carddateCondnCanel);
$getOverAllTotalDetailsCardCancelFetch=$db->fetch($getOverTotalDetailsCardCancel);
$overAllTotalCardCancel=$getOverAllTotalDetailsCardCancelFetch[0];

			$getdetails_code=$db->select(TM,"*",$icondn);
			
            while($getdetails_fetch=$db->fetch($getdetails_code))
            {
   
						$ticketName=$getdetails_fetch["tname_tam"];

						$ticketId=$getdetails_fetch["tid"];

						$getSoldInfoCondn=" where tid='$ticketId' and created_date between '$fdate' and '$tdate'";

					$byCashCondn=$getSoldInfoCondn." and cashtype='1'";
					
					$byCardCondn=$getSoldInfoCondn." and cashtype='2'"; 

					$getSoldCashInfo=$db->select(AB,"count(*)",$byCashCondn);

					$getSoldCashInfoFetch=$db->fetch($getSoldCashInfo);

					$soldCash=$getSoldCashInfoFetch[0];

					$getSoldInfo=$db->select(AB,"count(*)",$byCardCondn);

					$getSoldInfoFetch=$db->fetch($getSoldInfo);

					$soldCard=$getSoldInfoFetch[0];

					$sold=$soldCash+$soldCard;

						$getStartEndInfo=$db->select(AB,"min(ticketno),max(ticketno)",$getSoldInfoCondn);

						$getStartEndInfoFetch=$db->fetch($getStartEndInfo);

						if($sold==0)
						{

							$nextStartNoCondn=" where tid='$ticketId'";
							
							$getnextStartNo=$db->select(AB,"max(ticketno)",$nextStartNoCondn);

							$getnextStartNoFetch=$db->fetch($getnextStartNo);

							$getnextStartNo=$getnextStartNoFetch[0]+1;

							$startNo=$getnextStartNo;

							$endNo="";

						}
						else
						{

								$startNo=$getStartEndInfoFetch[0];

								$endNo=$getStartEndInfoFetch[1];

						}
								$priceCondn=" where tid='$ticketId' order by lastupdated_date desc";

								$getrateFromMaster=$db->select(TR,"*",$priceCondn);

								$getrateFromMasterFetch=$db->fetch($getrateFromMaster);

								$displayPrice=$getrateFromMasterFetch["rate"];


				if($displayPrice=="0.00" ||$displayPrice=="0")
				{
	
				

					$getKanikaiAmountDetails=$db->select(AB,"sum(rate)",$byCashCondn);

					$getKanikaiAmountDetailsFetch=$db->fetch($getKanikaiAmountDetails);

					$totalAmtCash=$getKanikaiAmountDetailsFetch[0];

				$getKanikaiAmountDetailsCard=$db->select(AB,"sum(rate)",$byCardCondn);

				$getKanikaiAmountDetailsCardFetch=$db->fetch($getKanikaiAmountDetailsCard);

				$totalAmtCard=$getKanikaiAmountDetailsCardFetch[0];

					
	
				}
				else
				{

								$getRateDetails=$db->select(AB,"distinct(rate)",$getSoldInfoCondn);

								$getRateDetailsFetch=$db->fetch($getRateDetails);

								$price=$getRateDetailsFetch[0];

						if($price==$displayPrice)
						{
			
								$actualPrice=$displayPrice;

						}
						else
						{

								$actualPrice=$price;

						}

						$totalAmtCash=$soldCash*$actualPrice;
					
						$totalAmtCard=$soldCard*$actualPrice;

				}

						$totalAmtCard=number_format($totalAmtCard,2);

						$totalAmtCash=number_format($totalAmtCash,2);

						$table=$table."<tr><td>$ticketName</td><td align='right'>$displayPrice</td><td align='right'>$startNo</td><td align='right'>$endNo</td><td align='right'>$sold</td><td align='right'>$totalAmtCard</td align='right'><td align='right'>$totalAmtCash</td></tr>";
			
   
            }			
							$totalCard=$overAllTotalCard-$overAllTotalCardCancel;

						$totalCash=$overAllTotalCash-$overAllTotalCashCancel;

						$overAllTotalCard=number_format($overAllTotalCard,2);

						$overAllTotalCash=number_format($overAllTotalCash,2);

						$overAllTotalCashCancel=number_format($overAllTotalCashCancel,2);

						$overAllTotalCardCancel=number_format($overAllTotalCardCancel,2);

					

						$totalCard=number_format($totalCard,2);

						$totalCash=number_format($totalCash,2);
			
				$table=$table."<tr><td colspan='5' align='right'><b>Sold Total</b></td><td align='right'><b>$overAllTotalCard</b></td><td align='right'><b>$overAllTotalCash</b></td></tr><tr><td colspan='5' align='right'><b>Cancelled Total</b></td><td align='right'><b>$overAllTotalCardCancel</b></td><td align='right'><b>$overAllTotalCashCancel</b></td></tr><tr><td colspan='5' align='right'><b>Total</b></td><td align='right'><b>$totalCard</b></td><td align='right'><b>$totalCash</b></td></tr></table><br><br><br><table border='0' width='100%' style='font-weight:bold'><tr><td align='center'>உதவியாளர்</td><td align='center'>பேஷ்கார்</td><td align='center'>கண்காணிப்பாளர்</td></tr></table><br><br><table border='0' width='100%' style='font-weight:bold'><tr><td align='right'>கணக்காளர்</td><td align='right'>முதுநிலை கணக்கு அலுவலர்</td><td align='right'>இணை ஆணையர் / நிர்வாக அதிகாரி</td></tr></table>";  	
				

                //$table=$table."<table width='100%' cellpadding='4' cellspacing='4'><tr><td colspan='2' align='right'><b>Total : </b></td></tr><tr><td colspan='2' align='center'><p align='center'><!--<img onclick='itemwise_pdf();' style='cursor:pointer;' src='images/print3.png'>--></p></td></tr></table>";

         


//==============================================================
//==============================================================
//==============================================================


include("mpdf.php");

$mpdf=new mPDF(); 
$mpdf->SetDefaultFontSize("12");
$mpdf->useAdobeCJK = true;		// Default setting in config.php
						// You can set this to false if you have defined other CJK fonts

$mpdf->SetAutoFont(AUTOFONT_ALL);	//	AUTOFONT_CJK | AUTOFONT_THAIVIET | AUTOFONT_RTL | AUTOFONT_INDIC	// AUTOFONT_ALL
						// () = default ALL, 0 turns OFF (default initially)

//$mpdf->mirrorMargins = 1;


/*$mpdf->pagenumPrefix = 'Page number ';
$mpdf->pagenumSuffix = ' - ';
$mpdf->nbpgPrefix = ' out of ';
$mpdf->nbpgSuffix = ' pages';

$mpdf->SetFooter('{PAGENO}{nbpg}');*/

$mpdf->WriteHTML($table);

$mpdf->Output(); 

exit;
//==============================================================
//==============================================================
//==============================================================


?>