<?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=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
<title><?php echo $PROJECT_TITLE ?></title>

<script type="text/javascript" src="js/design_analize_JS.js"></script>
<link href="css/styles.css" rel="stylesheet" type="text/css" />
<!--[if IE]> <link href="css/ie.css" rel="stylesheet" type="text/css"> <![endif]-->

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>

<script type="text/javascript" src="js/plugins/forms/ui.spinner.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.mousewheel.js"></script>
 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>

<script type="text/javascript" src="js/plugins/charts/excanvas.min.js"></script>
<script type="text/javascript" src="js/plugins/charts/jquery.flot.js"></script>
<script type="text/javascript" src="js/plugins/charts/jquery.flot.orderBars.js"></script>
<script type="text/javascript" src="js/plugins/charts/jquery.flot.pie.js"></script>
<script type="text/javascript" src="js/plugins/charts/jquery.flot.resize.js"></script>
<script type="text/javascript" src="js/plugins/charts/jquery.sparkline.min.js"></script>

<script type="text/javascript" src="js/plugins/tables/jquery.dataTables.js"></script>
<script type="text/javascript" src="js/plugins/tables/jquery.sortable.js"></script>
<script type="text/javascript" src="js/plugins/tables/jquery.resizable.js"></script>

<script type="text/javascript" src="js/plugins/forms/autogrowtextarea.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.uniform.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.inputlimiter.min.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.tagsinput.min.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.maskedinput.min.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.autotab.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.chosen.min.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.dualListBox.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.cleditor.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.ibutton.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.validationEngine-en.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.validationEngine.js"></script>

<script type="text/javascript" src="js/plugins/uploader/plupload.js"></script>
<script type="text/javascript" src="js/plugins/uploader/plupload.html4.js"></script>
<script type="text/javascript" src="js/plugins/uploader/plupload.html5.js"></script>
<script type="text/javascript" src="js/plugins/uploader/jquery.plupload.queue.js"></script>

<script type="text/javascript" src="js/plugins/wizards/jquery.form.wizard.js"></script>
<script type="text/javascript" src="js/plugins/wizards/jquery.validate.js"></script>
<script type="text/javascript" src="js/plugins/wizards/jquery.form.js"></script>

<script type="text/javascript" src="js/plugins/ui/jquery.collapsible.min.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.breadcrumbs.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.tipsy.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.progress.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.timeentry.min.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.colorpicker.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.jgrowl.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.fancybox.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.fileTree.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.sourcerer.js"></script>

<script type="text/javascript" src="js/plugins/others/jquery.fullcalendar.js"></script>
<script type="text/javascript" src="js/plugins/others/jquery.elfinder.js"></script>

<script type="text/javascript" src="js/plugins/ui/jquery.easytabs.min.js"></script>
<script type="text/javascript" src="js/files/bootstrap.js"></script>
<script type="text/javascript" src="js/files/functions.js"></script>

<script type="text/javascript" src="js/charts/chart.js"></script>
<script type="text/javascript" src="js/charts/hBar_side.js"></script>

<!-- Shared on MafiaShare.net  --><!-- Shared on MafiaShare.net  --></head>
<script type="text/javascript">
	function foms_validate()
	{
		var sel_date = $('#sel_date').val();
		if(sel_date=='')
		{
			alert('Please enter the delivery date');
			$('#sel_date').focus();
			return false;
		}
		$('#delivery').submit();
		
	}
</script> 
<body onload="tot_count();">

<!-- Top line begins -->
<?php include "header_footer/header.php"; ?>
<!-- Top line ends -->
<!-- Sidebar begins -->
<?php include "menus/left_menu.php"; ?>
<!-- Sidebar ends -->
<!-- Content begins -->
<div id="content">
<?php include "menus/top_menu.php"; ?>    
    <!-- Breadcrumbs line -->
    <div class="breadLine">
        <div class="bc">
            <ul id="breadcrumbs" class="breadcrumbs">
                <li><a href="#">Dashboard</a></li>
                <li><a href="javascript:">Courier View</a></li>
                    
            </ul>
        </div>
	<?php include "menus/top_left_menu.php"; ?>            
    </div>
    <!-- Main content -->
    <div class="wrapper">
    	<?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 = 'checked="checked"'; $y_r = $_GET['yea_r'];}else{ $yea_r = ''; $y_r = '';}
			if(isset($_GET['mont_h']) && $_GET['mont_h']!='' ){ $mont_h = 'checked="checked"'; $m_h = $_GET['mont_h'];}else{ $mont_h = ''; $m_h = ''; }
			if(isset($_GET['da_y']) && $_GET['da_y']!='' ){ $da_y = 'checked="checked"'; $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'])));
    	   	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";
			}
    	?>
    	<ul class="middleNavR">
            <li><a href="courier_list.php?<?php echo $vie_url; ?>" title="Total List of Courier" id="addpage" class="tipN"><img src="images/icons/middlenav/stats.png" alt="" /></a></li>        	
        </ul>            
    	<div class="widget"> <!--style="width:600px; margin-left:150px;"-->
        	<div class="whead">
        	<h6 style="float:right; margin-right:35px;text-decoration:underline;">
				<a href="courier_print.php?courier_id=<?php echo $_GET['courier_id']?>&flag=<?php echo $_GET['flag']?>&<?php echo $vie_url; ?>" target="_blank">Print</a>
			</h6>
        	<h6><?php echo $cour_nam ?>&nbsp;&nbsp;-&nbsp;&nbsp;Courier View</h6><div class="clear"></div></div>
        	<div id="dyn" class="hiddenpars">
	        	<a href="javascript:0" id="searchlink" onfocus="foc(this.id)" onblur="stnon(this.id)" class="tOptions" title="Options"><img src="images/icons/options.png" alt="" /></a>
	            <table cellpadding="0" cellspacing="0" border="0" class="dTable" id="dynamic">
		            <thead>
			            <tr>
				            <th width="26">S.No<span class="sorting" style="display: block;"></span></th>
				            <th width="192">Party Code & Name</th>
				            <th width="83">Po No</th>
				            <th width="50">Po Date</th>
				            <th width="98">Po Qty</th>
				            <th width="81">Delivery Qty</th>
				            <th width="98">PO Delivery Date</th>
				            <th width="98">Courier Date</th>
				            <th width="98">Weight</th>
				            <th width="98">Cost</th>
				            <th width="100"><?php if($flag==1){ echo "Tracking ID"; }elseif($flag==2){ echo "Deliverer Name"; } ?></th>
			            </tr>
		            </thead>
		            <tbody>
			            <?php
						$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_delivery.delivery_date,'%d-%m-%Y') DESC";
						//echo $SLCT;
						$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,billing_det.billing_det_id 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;
									$bill_id = $FTCH_po_bill->billing_det_id;
								}
								else
								{
									$cur_wgt = "--";
									$cur_amt = "--";
									$bill_id = '--';	
								}
							
							
								$SLCT_party_det = "SELECT po_entry.po_entry_id,po_no,po_date,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);
								
								$SLCT_po_qty = "SELECT count(*) as qty,IFNULL(SUM(master_pieces),'0') AS tot_pic FROM `po_entry_det`,design_det WHERE design_det.design_det_id=po_entry_det.design_det_id AND 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,IFNULL(SUM(master_pieces),'0') AS tot_pic FROM `po_delivery_det`,design_det WHERE design_det.design_det_id=po_delivery_det.design_det_id AND `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 ?>>
				            <td align="center"><?php echo $loop ?></td>
				            <td align="center"><?php echo $FTCH_party_det->party_code." - ".$FTCH_party_det->cmp_name ?></td>
				            <td align="center"><?php echo $FTCH_party_det->po_no ?></td>
				            <td align="center"><?php echo str_replace('-','/',$FTCH_party_det->po_date ) ?></td>
				            <td class="center"><?php echo $FTCH_po_qty->qty ?><br><span title="No of Dies for Order Designs">(<?php echo $FTCH_po_qty->tot_pic; ?>)</span></td>
				            <td class="center"><?php echo $FTCH_po_qty2->qty2 ?><br><span title="No of Dies for Delivery Designs">(<?php echo $FTCH_po_qty2->tot_pic; ?>)</span></td>
				            <td class="center"><?php echo str_replace('-','/',$FTCH->delivery_date )?></td>
				            <td class="center"><?php echo str_replace('-','/',$FTCH->del_date )?></td>
				            <td align="center"><?php echo $cur_wgt; ?></td>
            				<td align="center"><?php echo $cur_amt; ?></td>
			            	<td class="center"><?php if($flag==1){ echo $FTCH->track_id; }elseif($flag==2){ echo $FTCH_emp_det->name; } ?></td>
			            </tr>
		            
			            <?php
							$loop ++;
							 }
						
						?>
		            </tbody>
	            </table> 
        	</div>
        </div>    
        <div class="fluid"><!-- Media table --></div> 			
     </div>
    <!-- Main content ends -->
</div>
<!-- Content ends -->
<?php /*?><?php include "header_footer/footer.php"; ?><?php */?>
</body>
</html>
