<?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';
	}
?>
<!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>Untitled Document</title>
<script type="text/javascript" src="js/party_analize_JS.js"></script>
</head>
<body onload="tot_count();">
<?php
$flag = 0;
$sta_iden = '0';
// THIS IS FOR CHECKING THE STAGE NAME
	if(isset($_GET['sta_name']) && $_GET['sta_name']!='' ) 
	{
		$mas_stage_id = $_GET['sta_name'];
		$mas_stage_id1 =	base64_decode(base64_decode(base64_decode($_GET['sta_name']))); // this is for getting the value from url
		$SLCTs = "SELECT * FROM party_det WHERE party_det_id ='$mas_stage_id1'";
		$QURYs = mysqli_query($connect,$SLCTs);
		$FTCHs = mysqli_fetch_object($QURYs);
		$party_name = $FTCHs->cmp_name;								
	}
	
	
	if(isset($_GET['fdate']) && $_GET['fdate']!='' ){ $fdate = $_GET['fdate'];	}else{ $fdate = ''; }
	//echo $fdate;
	if(isset($_GET['todate']) && $_GET['todate']!='' ){ $todate = $_GET['todate'];	}else{ $todate = ''; }
	
	if(isset($_GET['yea_r']) && $_GET['yea_r']!='' ){ $yea_r = 'Year';	}else{ $yea_r = ''; }
	if(isset($_GET['mont_h']) && $_GET['mont_h']!='' ){ $mont_h = 'Month';	}else{ $mont_h = ''; }
	if(isset($_GET['da_y']) && $_GET['da_y']!='' ){ $da_y = 'Day';	}else{ $da_y = ''; }
	
	$flg = $_GET['flg'];
?>
<table width="960" border="0" align="center" style="outline:#000000 solid 1px;">
  <tr>
    <th colspan="8" scope="col">Party Analize Report</th>
  </tr>
  <tr>
    <td width="100">&nbsp;</td>
    <td width="125">&nbsp;</td>
    <td width="100">&nbsp;</td>
    <td width="125">&nbsp;</td>
    <td width="100">&nbsp;</td>
    <td width="125">&nbsp;</td>
    <td width="100">&nbsp;</td>
    <td width="150">&nbsp;</td>
  </tr>
 
  <tr>
    <td>&nbsp;Party Name</td>
    <td colspan="7">&nbsp;:&nbsp;<?php echo $party_name; ?></td>
  </tr>
  <tr>
    <td colspan="8">&nbsp;</td>
  </tr>
  
  <tr>
    <td>&nbsp;From Date</td>
    <td>&nbsp;:&nbsp;<?php if($fdate!=''){ echo $fdate; }else{ echo "-----"; }; ?></td>
    <td>&nbsp;To Date</td>
    <td>&nbsp;:&nbsp;<?php if($todate!=''){ echo $todate; }else{ echo "-----"; }?></td>
    <td>&nbsp;Filter</td>
    <td>&nbsp;:&nbsp;
    	<?php if($yea_r!=''){ echo "Year"; } ?>&nbsp;-&nbsp;
      	<?php if($mont_h!=''){ echo "Month"; } ?>&nbsp;-&nbsp;
      	<?php if($da_y!=''){ echo "Day"; } ?>
   	</td>
    <td>&nbsp;Quantity</td>
    <td>&nbsp;:&nbsp;<input type="text" id="tot_qty" name="tot_qty" value="" readonly="readonly" disabled="disabled" style="width:50px; background:#FFFFFF; font-size:12px; text-align:center; border:none;" />(<input type="text" id="tot_die" name="tot_die" value="" title="No of Dies" readonly="readonly" disabled="disabled" style="width:50px; background:#FFFFFF; font-size:12px; text-align:center; border:none;" />)</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>


<?php
			$design_date = ''; // THIS IS FOR ASIGNING THE COLUMN NAME DYNAMICALLY
			
			$design_date = "po_entry.po_date"; 
			
			$dat_str = ''; //THIS IS FOR QUERY STRING
						
			if($fdate!='' && $todate!='')
			{
				if($yea_r!='' && $mont_h!='' && $da_y!=''){ $dat_str = "STR_TO_DATE(".$design_date.",'%d-%m-%Y') BETWEEN STR_TO_DATE('$fdate','%d/%m/%Y') AND STR_TO_DATE('$todate','%d/%m/%Y')"; }
				
				if($yea_r!='' && $mont_h!='' && $da_y==''){ $dat_str = "MONTH(STR_TO_DATE(".$design_date.",'%d-%m-%Y')) BETWEEN MONTH(STR_TO_DATE('$fdate','%d/%m/%Y')) AND MONTH(STR_TO_DATE('$todate','%d/%m/%Y')) AND YEAR(STR_TO_DATE(".$design_date.",'%d-%m-%Y')) BETWEEN YEAR(STR_TO_DATE('$fdate','%d/%m/%Y')) AND YEAR(STR_TO_DATE('$todate','%d/%m/%Y'))"; }
				
				if($yea_r!='' && $mont_h=='' && $da_y==''){ $dat_str = "YEAR(STR_TO_DATE(".$design_date.",'%d-%m-%Y')) BETWEEN YEAR(STR_TO_DATE('$fdate','%d/%m/%Y')) AND YEAR(STR_TO_DATE('$todate','%d/%m/%Y'))"; }
				
				if($yea_r=='' && $mont_h=='' && $da_y==''){ $dat_str = "STR_TO_DATE(".$design_date.",'%d-%m-%Y') BETWEEN STR_TO_DATE('$fdate','%d/%m/%Y') AND STR_TO_DATE('$todate','%d/%m/%Y')"; }
				
				if($yea_r=='' && $mont_h!='' && $da_y==''){ $dat_str = "MONTH(STR_TO_DATE(".$design_date.",'%d-%m-%Y')) BETWEEN MONTH(STR_TO_DATE('$fdate','%d/%m/%Y')) AND MONTH(STR_TO_DATE('$todate','%d/%m/%Y')) AND YEAR(STR_TO_DATE(".$design_date.",'%d-%m-%Y')) BETWEEN YEAR(STR_TO_DATE('$fdate','%d/%m/%Y')) AND YEAR(STR_TO_DATE('$todate','%d/%m/%Y'))"; }
				
				if($yea_r=='' && $mont_h=='' && $da_y!=''){ $dat_str = "STR_TO_DATE(".$design_date.",'%d-%m-%Y') BETWEEN STR_TO_DATE('$fdate','%d/%m/%Y') AND STR_TO_DATE('$todate','%d/%m/%Y')"; }
				
				if($yea_r!='' && $mont_h=='' && $da_y!=''){ $dat_str = "STR_TO_DATE(".$design_date.",'%d-%m-%Y') BETWEEN STR_TO_DATE('$fdate','%d/%m/%Y') AND STR_TO_DATE('$todate','%d/%m/%Y')"; }
				
				if($yea_r=='' && $mont_h!='' && $da_y!=''){ $dat_str = "STR_TO_DATE(".$design_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 = "STR_TO_DATE(".$design_date.",'%d-%m-%Y') = STR_TO_DATE('$todate','%d/%m/%Y')"; }
				
				if($yea_r!='' && $mont_h!='' && $da_y==''){ $dat_str = "DATE_FORMAT(STR_TO_DATE(".$design_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 = "YEAR(STR_TO_DATE(".$design_date.",'%d-%m-%Y')) = YEAR(STR_TO_DATE('$todate','%d/%m/%Y'))"; }
				
				if($yea_r=='' && $mont_h=='' && $da_y==''){ $dat_str = "STR_TO_DATE(".$design_date.",'%d-%m-%Y') <= STR_TO_DATE('$todate','%d/%m/%Y')"; }
				
				if($yea_r=='' && $mont_h!='' && $da_y==''){ $dat_str = "DATE_FORMAT(STR_TO_DATE(".$design_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 = "STR_TO_DATE(".$design_date.",'%d-%m-%Y') = STR_TO_DATE('$todate','%d/%m/%Y')"; }
				
				if($yea_r!='' && $mont_h=='' && $da_y!=''){ $dat_str = "STR_TO_DATE(".$design_date.",'%d-%m-%Y') = STR_TO_DATE('$todate','%d/%m/%Y')"; }
				
				if($yea_r=='' && $mont_h!='' && $da_y!=''){ $dat_str = "STR_TO_DATE(".$design_date.",'%d-%m-%Y') = STR_TO_DATE('$todate','%d/%m/%Y')"; }
			}
			
					
			?>
		 	
        	
            <table width="960" border="1" cellpadding="0" cellspacing="0" align="center">
            <thead>
            <tr>
            <th width="47">S.No<span class="sorting" style="display: block;"></span></th>
            <th width="280">Po No</th>
			<th width="280">Po Date</th>
            <th width="280" >Order Qty</th>
            <th width="280">Finished Qty</th>
            <th width="280">Delivered Qty</th>
			
			           
            </tr>
            </thead>
            <tbody>

			<?php 
			$SLCT = "SELECT * FROM party_det,po_entry WHERE ".$dat_str." AND po_entry.party_det_id = '$mas_stage_id1' AND party_det.party_det_id = po_entry.party_det_id GROUP BY po_entry.po_entry_id ASC";
			//echo $SLCT;
			$sum_qty = 0;
			$tot_pic = 0;
			$QURY = mysqli_query($connect,$SLCT);
			$loop=1;
			if(mysqli_num_rows($QURY)>0)
			{			
			while($FTCH = mysqli_fetch_object($QURY))
			{
				$SLCT_d = "SELECT IFNULL(SUM(master_pieces),'0') AS tot_pic, COUNT(po_entry_det.design_det_id) as cnt_des FROM po_entry_det,design_det WHERE design_det.design_det_id=po_entry_det.design_det_id AND po_entry_id = '$FTCH->po_entry_id' AND cancelled='no'";
				$QURY_d = mysqli_query($connect,$SLCT_d);
				$FTCH_d = mysqli_fetch_object($QURY_d);
				
				$SLCT_dc = "SELECT IFNULL(SUM(master_pieces),'0') AS tot_pic, COUNT(po_entry_det.design_det_id) as cnt_des_c FROM po_entry_det,design_det WHERE design_det.design_det_id=po_entry_det.design_det_id AND po_entry_id = '$FTCH->po_entry_id' AND cancelled='yes'";
				$QURY_dc = mysqli_query($connect,$SLCT_dc);
				$FTCH_dc = mysqli_fetch_object($QURY_dc);
				
				$SLCT_df = "SELECT IFNULL(SUM(master_pieces),'0') AS tot_pic, COUNT(po_entry_det.design_det_id) as cnt_des_f FROM po_entry_det,design_det WHERE design_det.design_det_id=po_entry_det.design_det_id AND po_entry_id = '$FTCH->po_entry_id' AND fin_status='yes' AND cancelled='no'";
				$QURY_df = mysqli_query($connect,$SLCT_df);
				$FTCH_df = mysqli_fetch_object($QURY_df);
				
				$SLCT_dd = "SELECT IFNULL(SUM(master_pieces),'0') AS tot_pic, COUNT(po_entry_det.design_det_id) as cnt_des_d FROM po_entry_det,design_det WHERE design_det.design_det_id=po_entry_det.design_det_id AND po_entry_id = '$FTCH->po_entry_id' AND fin_status='yes' AND del_status='yes' AND cancelled='no'";
				$QURY_dd = mysqli_query($connect,$SLCT_dd);
				$FTCH_dd = mysqli_fetch_object($QURY_dd);
							
				if($loop%2 != "0")
					$bg = "gradeX";
				else
					$bg = "class='gradeC'";
					
				
			?>
            <tr <?php echo $bg ?>>
            <td align="center" style="display:table-cell;vertical-align:middle"><?php echo $loop ?></td>
             <td align="center" style="display:table-cell;vertical-align:middle"><?php echo $FTCH->po_no; ?></td>
			 <td align="center" style="display:table-cell;vertical-align:middle"><?php echo $FTCH->po_date; ?></td>
              <td align="center" style="display:table-cell;vertical-align:middle"><?php echo $FTCH_d->cnt_des; ?>( <?php echo $FTCH_dc->cnt_des_c; ?> )<br><span title="No of Dies for Order Designs">(<?php echo $FTCH_d->tot_pic; ?>)</span>&nbsp;<span title="No of Dies for Order Cancel Designs">(<?php echo $FTCH_dc->tot_pic; ?>)</span></td>
               <td align="center" style="display:table-cell;vertical-align:middle"><?php echo $FTCH_df->cnt_des_f; ?><br><span title="No of Dies for Finish Designs">(<?php echo $FTCH_df->tot_pic; ?>)</span></td>
            <td align="center" style="display:table-cell;vertical-align:middle"><?php echo $FTCH_dd->cnt_des_d; ?><br><span title="No of Dies for Delivery Designs">(<?php echo $FTCH_dd->tot_pic; ?>)</span></td>
			
			               
            </tr>
            
            <?php
			$loop ++;
			$tot_pic = $tot_pic+$FTCH_d->tot_pic;
			$sum_qty = $sum_qty+$FTCH_d->cnt_des; 
			 }
			 }
			 ?>
            </tbody>
            </table> 
       <input name="tot_cnt" id="tot_cnt" type="hidden" value="<?php echo $sum_qty; ?>" />	 
		<input name="mas_die" id="mas_die" type="hidden" value="<?php echo $tot_pic;  ?>" />	
	
</body>
</html>
