<?php
	include "db_connection/db_connection.php";
	include "config/config.php";
	
	if($_SESSION['admin_login_id'] == '')
	{
		echo "<script>window.location='index.php';</script>";
	}
	else // THIS IS FOR GETTING PARTY DET ID ROM LOAGIN DET AND PARTY DET TABLE
	{
		$admin_id = $_SESSION['admin_login_id'];
		
		$SLCT_log = "SELECT * FROM login_details,ac_party_det WHERE ac_party_det.party_det_id = login_details.party_det_id AND login_details.admin_id = '$admin_id'";
		//echo $SLCT_log;
		$QURY_log = mysqli_query($connect,$SLCT_log);
		$FTCH_log = mysqli_fetch_object($QURY_log);
		$party_id = '$FTCH_log->party_det_id';
	}
	
	function sum_the_time($tot_time=array()) 
	{
		if(count($tot_time)>0)
		{
			$seconds = 0;
			foreach ($tot_time as $time)
			{
				list($hour,$minute,$second) = explode(':', $time);
				$seconds += $hour*3600;
				$seconds += $minute*60;
				$seconds += $second;
			}
			$hours = floor($seconds/3600);
			$seconds -= $hours*3600;
			$minutes  = floor($seconds/60);
			$seconds -= $minutes*60;
			return "{$hours}:{$minutes}:{$seconds}";
		}
		else
		{
			return;
		}
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php echo $PROJECT_TITLE ?></title>
<script type="text/javascript" src="js/design_analize_JS.js"></script>
</head>

<body onload="tot_count();">
<?php
	$sel_date = date('d/m/Y');
	$design_date = ''; 
	if(isset($_GET['fdate']) && $_GET['fdate']!='' ){ $fdate = $_GET['fdate'];	}else{ $fdate = ''; }
	if(isset($_GET['todate']) && $_GET['todate']!='' ){ $todate = $_GET['todate'];	}else{ $todate = ''; }
	if(isset($_GET['yea_r']) && $_GET['yea_r']!='' ){ $yea_r = 'Year'; $y_r = $_GET['yea_r'];}else{ $yea_r = ''; $y_r = '';}
	if(isset($_GET['mont_h']) && $_GET['mont_h']!='' ){ $mont_h = 'Month'; $m_h = $_GET['mont_h'];}else{ $mont_h = ''; $m_h = ''; }
	if(isset($_GET['da_y']) && $_GET['da_y']!='' ){ $da_y = 'Day'; $d_y = $_GET['da_y'];}else{ $da_y = ''; $d_y = ''; }
	$vie_url = "fdate=".$fdate."&todate=".$todate."&mont_h=".$m_h."&da_y=".$d_y."&yea_r=".$y_r;
	if($fdate!='' && $todate!='')
	{
		$dat_str = " AND STR_TO_DATE(del_date,'%d/%m/%Y') BETWEEN STR_TO_DATE('$fdate','%d/%m/%Y') AND STR_TO_DATE('$todate','%d/%m/%Y')";						
	}
	else if($fdate=='' && $todate!='')
	{
		if($yea_r!='' && $mont_h!='' && $da_y!=''){ $dat_str = " AND STR_TO_DATE(del_date,'%d/%m/%Y') = STR_TO_DATE('$todate','%d/%m/%Y')"; }
		
		if($yea_r!='' && $mont_h!='' && $da_y==''){ $dat_str = " AND DATE_FORMAT(STR_TO_DATE(del_date,'%d/%m/%Y'),'%m-%Y') = DATE_FORMAT(STR_TO_DATE('$todate','%d/%m/%Y'),'%m-%Y')"; }
		
		if($yea_r!='' && $mont_h=='' && $da_y==''){ $dat_str = " AND YEAR(STR_TO_DATE(del_date,'%d/%m/%Y')) = YEAR(STR_TO_DATE('$todate','%d/%m/%Y'))"; }
		
		if($yea_r=='' && $mont_h=='' && $da_y==''){ $dat_str = " AND STR_TO_DATE(del_date,'%d/%m/%Y') <= STR_TO_DATE('$todate','%d/%m/%Y')"; }
		
		if($yea_r=='' && $mont_h!='' && $da_y==''){ $dat_str = " AND DATE_FORMAT(STR_TO_DATE(del_date,'%d/%m/%Y'),'%m-%Y') = DATE_FORMAT(STR_TO_DATE('$todate','%d/%m/%Y'),'%m-%Y')"; }
		if($yea_r=='' && $mont_h=='' && $da_y!=''){ $dat_str = " AND STR_TO_DATE(del_date,'%d/%m/%Y') = STR_TO_DATE('$todate','%d/%m/%Y')"; }
		
		if($yea_r!='' && $mont_h=='' && $da_y!=''){ $dat_str = " AND STR_TO_DATE(del_date,'%d/%m/%Y') = STR_TO_DATE('$todate','%d/%m/%Y')"; }
		
		if($yea_r=='' && $mont_h!='' && $da_y!=''){ $dat_str = " AND STR_TO_DATE(del_date,'%d/%m/%Y') = STR_TO_DATE('$todate','%d/%m/%Y')"; }
	}
	else if($fdate=='' && $todate=='')
	{
		$dat_str = " AND STR_TO_DATE(del_date,'%d/%m/%Y') BETWEEN STR_TO_DATE(del_date,'%d/%m/%Y') AND STR_TO_DATE('$sel_date','%d/%m/%Y')";						
	}
	
	$flag       = $_GET['flag'];
	$courier_id = base64_decode(base64_decode(base64_decode($_GET['courier_id'])));
	$SLCT_cor = "SELECT * FROM mas_cour_det where mas_cour_det_id='$courier_id'";
	$QURY_cor = mysqli_query($connect,$SLCT_cor);
	$FTCH_cor = mysqli_fetch_object($QURY_cor);
	if($courier_id!=0)
	{
		$SLCT_cor = "SELECT * FROM mas_cour_det where mas_cour_det_id='$courier_id'";
		$QURY_cor = mysqli_query($connect,$SLCT_cor);
		$FTCH_cor = mysqli_fetch_object($QURY_cor);
		$cour_nam = $FTCH_cor->cour_name;
	}
	else
	{
		$cour_nam = "Hand Delivery";
	}
?>
<table width="960" border="1" cellpadding="0" cellspacing="0" align="center" style="">
  <tr>
    <th colspan="7" scope="col" style="padding: 3px;">
    	<div align="center">
    		<h2><font style="font-family: PaqueteSSK; font-size:40px;">Thahaayaa</font> <font style="font-size:20px; font-family: Logan;">Crea-Dons. . .  </font></h2> 
  		</div>                
  		<div align="center" style="margin-top:-15px;font-weight: bold"><p>243A, Subramaniyapuram, Thirumoothi Layout R.S.Puram Coimbatore-641002. Ph: 0422 - 4351080</p></div>
    </th>
  </tr>
</table>
<table width="960" border="0" cellpadding="0" cellspacing="0" align="center" style="outline:#000000 solid 1px;">
  <tr>
    <th colspan="7" scope="col" style="font-size: 25px; padding: 3px"><?php echo $cour_nam ?>&nbsp;&nbsp;-&nbsp;&nbsp;Courier Report</th>
  </tr>
  <tr>
    <td width="187">&nbsp;</td>
    <td width="55">&nbsp;</td>
    <td width="233">&nbsp;</td>
    <td width="88">&nbsp;</td>
    <td width="93">&nbsp;</td>
    <td width="66">&nbsp;</td>
    <td width="203">&nbsp;&nbsp;</td>
  </tr>
  <tr>
    <td style="padding: 3px;"><b>From Date&nbsp;&nbsp;:</b>&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $fdate; ?></td>
    <td>&nbsp;</td>
    <td style="padding: 3px;"><b>To Date&nbsp;&nbsp;:</b>&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $todate; ?></td>
    <td colspan="2">&nbsp;</td>
    <td style="padding: 3px;"><b>Filter&nbsp;&nbsp;:</b></td>
    <td style="padding: 3px;"><?php echo $da_y."&nbsp;".$mont_h."&nbsp;".$yea_r; ?></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
<table width="962" border="1" align="center" cellpadding="0" cellspacing="0" >
    <thead>
        <tr>
            <th width="26" align="center" style="padding: 3px;">S.No<span class="sorting" style="display: block;"></span></th>
            <th width="80" align="center" style="padding: 3px;">Courier Date</th>
            <th width="60" align="center" style="padding: 3px;"><?php if($flag==1){ echo "Tracking ID"; }elseif($flag==2){ echo "Deliverer Name"; } ?></th>
            <th width="170" align="center" style="padding: 3px;">Company Name</th>
             <th width="60" align="center" style="padding: 3px;">Address</th>
            <th width="40" align="center" style="padding: 3px;">Weight</th>
            <th width="40" align="center" style="padding: 3px;">Amount</th>
        </tr>
    </thead>
    <tbody>
        <?php
		//$SLCT = "SELECT  * FROM po_del_cour_det WHERE mas_cour_det_id =".$courier_id.$dat_str." ORDER BY STR_TO_DATE(del_date,'%d/%m/%Y') ASC";
		$SLCT = "SELECT delivery_date,po_entry_id,po_del_cour_det.* FROM po_del_cour_det,po_delivery WHERE po_delivery.po_delivery_id=po_del_cour_det.po_delivery_id AND mas_cour_det_id =".$courier_id.$dat_str." ORDER BY STR_TO_DATE(po_del_cour_det.del_date,'%d/%m/%Y') ASC";
		$QURY = mysqli_query($connect,$SLCT);
		$loop=1;
		$tot_cur_wgt = 0;
		$tot_cur_amt = 0;
		while($FTCH = mysqli_fetch_object($QURY))
			{
				$SLCT_po_bill = "SELECT cour_wgt,courier_amt FROM billing_po_det,billing_det WHERE billing_po_det.po_delivery_id='$FTCH->po_delivery_id' AND billing_det.billing_det_id = billing_po_det.billing_det_id";
				$QURY_po_bill = mysqli_query($connect,$SLCT_po_bill);
				if(mysqli_num_rows($QURY_po_bill)>0)
				{
					$FTCH_po_bill = mysqli_fetch_object($QURY_po_bill);
					$cur_wgt = $FTCH_po_bill->cour_wgt;
					$cur_amt = $FTCH_po_bill->courier_amt;
					$tot_cur_wgt = $tot_cur_wgt+$FTCH_po_bill->cour_wgt;
					$tot_cur_amt = $tot_cur_amt+$FTCH_po_bill->courier_amt;
				}
				else
				{
					$cur_wgt = "--";
					$cur_amt = "--";	
				}
				
				
				$SLCT_party_det = "SELECT po_entry.po_entry_id,party_code,cmp_name,cour_addr FROM po_entry,party_det WHERE po_entry.po_entry_id='$FTCH->po_entry_id' AND party_det.party_det_id=po_entry.party_det_id";
				$QURY_party_det = mysqli_query($connect,$SLCT_party_det);
				$FTCH_party_det = mysqli_fetch_object($QURY_party_det);
				
				$comp_nam = $FTCH_party_det->party_code." - ".$FTCH_party_det->cmp_name;
				$expld = explode(",",$FTCH_party_det->cour_addr);
				$num = count($expld);
				$comp_addr = $expld[$num-1];
				
				$SLCT_po_qty = "SELECT count(*) as qty FROM `po_entry_det` WHERE cancelled='no' AND `po_entry_id`=".$FTCH_party_det->po_entry_id;
				$QURY_po_qty = mysqli_query($connect,$SLCT_po_qty);
				$FTCH_po_qty = mysqli_fetch_object($QURY_po_qty);
				
				$SLCT_po_qty2 = "SELECT count(*) as qty2 FROM `po_delivery_det` WHERE `po_delivery_id`=".$FTCH->po_delivery_id;
				$QURY_po_qty2 = mysqli_query($connect,$SLCT_po_qty2);
				$FTCH_po_qty2 = mysqli_fetch_object($QURY_po_qty2);
				
				$SLCT_emp_det = "SELECT name FROM `emp_det` WHERE `emp_det_id`=".$FTCH->emp_det_id;
				$QURY_emp_det = mysqli_query($connect,$SLCT_emp_det);
				$FTCH_emp_det = mysqli_fetch_object($QURY_emp_det);
			
				if($loop%2 != "0")
					$bg = "gradeX";
				else
					$bg = "class='gradeC'";
		?>
        <tr <?php echo $bg ?> align="center">
            <td align="center" style="padding: 3px;"><?php echo $loop ?></td>
            <td align="center" style="padding: 3px;"><?php echo str_replace('-','/',$FTCH->del_date )?></td>
            <td align="center" style="padding: 3px;"><?php if($flag==1){ echo $FTCH->track_id; }elseif($flag==2){ echo $FTCH_emp_det->name; } ?></td>
            <td align="left" style="padding: 3px;"><?php echo $comp_nam ?></td>
            <td align="left" style="padding: 3px;"><?php echo $comp_addr; ?></td>
            <td align="center" style="padding: 3px;"><?php echo $cur_wgt; ?></td>
            <td align="center" style="padding: 3px;"><?php echo $cur_amt; ?></td>
       </tr>
    
        <?php
			$loop ++;
			 }
		
		?>
		<tr <?php echo $bg ?> align="center">
            <td align="center" colspan="3" style="padding: 3px;">&nbsp;</td>
            <td class="center" colspan="2" style="padding: 3px;"><b>Total</b></td>
            <td class="center" style="padding: 3px;"><?php echo $tot_cur_wgt; ?></td>
             <td class="center" style="padding: 3px;"><?php echo $tot_cur_amt; ?></td>
       </tr>
    </tbody>
</table> 
</body>
</html>
