<?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'";
		$QURY_log = mysqli_query($connect,$SLCT_log);
		$FTCH_log = mysqli_fetch_object($QURY_log);
		$party_id = '$FTCH_log->party_det_id';
	}
	$check_acc  = check_access('51');
?>
<!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/prod_analize_JS.js"></script>
		<link href="css/styles.css" rel="stylesheet" type="text/css" />
		<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>
	<body onload="tot_count(1)">
		<!-- 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:">Production/Selling Analyze</a></li>
		            </ul>
		        </div>
				<?php include "menus/top_left_menu.php"; ?>    
    		</div>
    		<!-- Main content -->
    		<div class="wrapper">
			<?php if($check_acc==1){?>
			<?php
			   	$flag = 0;
			   	$sta_iden = '0';
			   	// THIS IS FOR CHECKING THE STAGE NAME
				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 = ''; }
			?>
		<ul class="middleNavR"></ul>
		<form id="acc_dat" name="acc_dat" class="main" method="post" action="acc_report.php" target="_blank">
		<fieldset>
		<div class="widget">
		<div class="formRow" >
		<table width="100%">
			<tr>
			<td width="9%" style="display:table-cell; vertical-align:middle;">From Date:</td>
			<td width="22%"><div class="grid6"><input type="text" name="fdate" id="fdate" value="<?php echo $fdate ?>" class="datepicker" /></div></td>
			<td width="9%" style="display:table-cell; vertical-align:middle;">To Date:</td>
			<td width="23%"><div class="grid6"><input type="text" name="todate" id="todate" value="<?php echo $todate ?>" class="datepicker" /></div></td>
			<td width="10%" style="display:table-cell; vertical-align:middle;">Filter:</td>
			<td width="8%"><input id="yea_r"  name="filter" type="checkbox" value="y" <?php echo $yea_r ?> /> <label for="yea_r">Year</label></td>
			<td width="8%"><input id="mont_h" name="filter2" type="checkbox" value="m" <?php echo $mont_h ?> /> <label for="mont_h">Month</label></td>
			<td width="11%"><input id="da_y" name="filter3" type="checkbox" value="d" <?php echo $da_y ?> /> <label for="da_y">Day</label></td>
			</tr>
		</table>
	</div>
			<div class="formRow">
				<input type="button" value="Submit" class="buttonM bBlack formSubmit" onclick="fom_validate();" style="float: right; margin-left:20px;"/>
				<input type="button" value="Reset" class="buttonM bRed" style="float: right; margin-left:20px;" onclick="form_rest();"/>
				<div class="clear"></div>
            				</div>
                        	<div class="clear"></div>
                    	</div> 
                    	<?php if(isset($_GET['fdate']) && $_GET['fdate']!='' || isset($_GET['todate']) && $_GET['todate']!=''){ ?>	
					<input type="hidden" value="<?php echo $_GET['fdate']; ?>" name="std" id="std" />
					<input type="hidden" value="<?php echo $_GET['todate']; ?>" name="edd" id="edd" />
					<input type="hidden" value="<?php echo $_GET['yea_r']; ?>" name="yr" id="yr" />
					<input type="hidden" value="<?php echo $_GET['mont_h']; ?>" name="mnt" id="mnt" />
					<input type="hidden" value="<?php echo $_GET['da_y']; ?>" name="dty" id="dty" />
					<input type="hidden" value="<?php echo $_GET['todate']; ?>" name="edd" id="edd" />
					<input type="hidden" value="" name="par_id" id="par_id" />
		<?php
			$billing_date = "REPLACE(BD.bill_date,'/','-')"; 
			$dat_str_bill = ''; //THIS IS FOR QUERY STRING
			if($fdate!='' && $todate!='')
			{
				$dat_str_bill = "AND STR_TO_DATE(".$billing_date.",'%d-%m-%Y') BETWEEN STR_TO_DATE('$fdate','%d/%m/%Y') AND STR_TO_DATE('$todate','%d/%m/%Y')"; 
				$flgg=1;
			}
	else if($fdate=='' && $todate!='')
	{
		if($yea_r!='' && $mont_h!='' && $da_y!='')
		{ 
			$dat_str_bill = "AND STR_TO_DATE(".$billing_date.",'%d-%m-%Y') = STR_TO_DATE('$todate','%d/%m/%Y')"; 
			$flgg = 1;
		}
		else if($yea_r!='' && $mont_h!='' && $da_y=='')
		{
			$vl = explode("/",$todate);
			$todat_vl = $vl[2]."-".$vl[1]."-".$vl[0];
		 $dat_str_bill = "AND DATE_FORMAT(STR_TO_DATE(".$billing_date.",'%d-%m-%Y'),'%m-%Y') = DATE_FORMAT(STR_TO_DATE('$todate','%d/%m/%Y'),'%m-%Y')";  
			$flgg = 1;
		}
		else if($yea_r!='' && $mont_h=='' && $da_y=='')
		{ 
			$vl = explode("/",$todate);
			$todat_vl = ($vl[2]-1)."-12-31";
			$dat_str_bill = "AND YEAR(STR_TO_DATE(".$billing_date.",'%d-%m-%Y')) = YEAR(STR_TO_DATE('$todate','%d/%m/%Y'))";
			$flgg = 1;
		}
		else if($yea_r=='' && $mont_h=='' && $da_y=='')
		{ 
			$dat_str_bill = "AND STR_TO_DATE(".$billing_date.",'%d-%m-%Y') <= STR_TO_DATE('$todate','%d/%m/%Y')";
			$flgg = 0;
		}
		else if($yea_r=='' && $mont_h!='' && $da_y=='')
		{ 
			$vl = explode("/",$todate);
			$todat_vl = $vl[2]."-".$vl[1]."-".$vl[0];
		   $dat_str_bill = "AND DATE_FORMAT(STR_TO_DATE(".$billing_date.",'%d-%m-%Y'),'%m-%Y') = DATE_FORMAT(STR_TO_DATE('$todate','%d/%m/%Y'),'%m-%Y')";  
			$flgg = 1;
		}
		else if($yea_r=='' && $mont_h=='' && $da_y!='')
		{ 
			$dat_str_bill = "AND STR_TO_DATE(".$billing_date.",'%d-%m-%Y') = STR_TO_DATE('$todate','%d/%m/%Y')"; 
			$flgg = 1;
		}
		else if($yea_r!='' && $mont_h=='' && $da_y!='')
		{ 
			$dat_str_bill = "AND STR_TO_DATE(".$billing_date.",'%d-%m-%Y') = STR_TO_DATE('$todate','%d/%m/%Y')"; 
			$flgg = 1;
		}
		else if($yea_r=='' && $mont_h!='' && $da_y!='')
		{ 
			$dat_str_bill = "AND STR_TO_DATE(".$billing_date.",'%d-%m-%Y') = STR_TO_DATE('$todate','%d/%m/%Y')";
			$flgg = 1;
		}
	}
	?>
	<div class="widget" style="display: block">
		<div class="whead">
			<h6>Production / Selling Report</h6>
			<ul class="titleToolbar">
		<li><h6 style=" margin-right:0px; text-align:center;">Total Design Qty&nbsp;&nbsp;&nbsp;<input class="dataNumGrey" type="text" id="tot_des" name="tot_des" value="" readonly="readonly" disabled="disabled" style="width:50px; font-size:12px; text-align:right; padding-right: 5px; margin: 0;" /></h6></li>
		<li><h6 style=" margin-right:0px; text-align:center;">Total Billing Qty&nbsp;&nbsp;&nbsp;<input class="dataNumGrey" type="text" id="tot_sel" name="tot_sel" value="" readonly="readonly" disabled="disabled" style="width:50px; font-size:12px; text-align:right; padding-right: 5px; margin: 0;" /></h6></li>
		<li><h6 style=" margin-right:42px; text-align:center;"><a href="prod_sell_analize_print.php?fdate=<?php echo $_GET['fdate'] ?>&todate=<?php echo $_GET['todate'] ?>&yea_r=<?php echo $_GET['yea_r'] ?>&mont_h=<?php echo $_GET['mont_h'] ?>&da_y=<?php echo $_GET['da_y'] ?>&flg=<?php echo base64_encode(base64_encode(base64_encode(base64_encode(1)))); ?>" target="_blank">Print</a></h6></li>
		</ul>
		<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 width="677" border="0" cellpadding="0" cellspacing="0" class="dTable" id="dynamic">
		<thead>
		<tr>
			<th width="8%">S.No<span class="sorting" style="display: block;"></span></th>
			<th width="8%" style="text-align:left;">Category</th>
			<th width="10%">Design Qty</th>
			<th width="12%">Design Qty %</th>
			<th width="14%">Cumulative Design Qty</th>
			<th width="10%">Billing Qty</th>
			<th width="12%">Billing Qty %</th>
			<th width="14%">Cumulative Billing Qty</th>
			<th width="12%">View</th>
		</tr>
	</thead>
	<tbody>
	<?php
	$loop=1;
	$desCumulative = 0;
	$selCumulative = 0;
	$totDes = '0';
	$totCnt = '0';
	$totWgt = '0';

/*	$SLCT_BILL = "SELECT BPDD.`master_cat_id`, MC.`master_cat_name`, COUNT(DISTINCT BPDD.`design_det_id`) AS DES_CNT, COUNT(BPDD.`design_det_id`) AS TOT_CNT, SUM(MF.`mas_weight`) AS TOT_WHT FROM `billing_det` BD 
	INNER JOIN `billing_po_det` BPD ON BPD.`billing_det_id`=BD.`billing_det_id` 
    INNER JOIN `billing_po_design_det` BPDD ON BPDD.`billing_po_det_id`=BPD.`billing_po_det_id`
    INNER JOIN `master_cat` MC ON MC.`master_cat_id` = BPDD.`master_cat_id`
    INNER JOIN `mas_finishing` MF ON MF.`design_det_id`=BPDD.`design_det_id`
    WHERE BD.`cancelled`='no' AND BPD.`cancelled`='no' AND BPDD.`cancelled`='no' AND MF.`up_status`='no' 
	".$dat_str_bill." 
    GROUP BY BPDD.`master_cat_id`
	ORDER BY COUNT(BPDD.`design_det_id`) DESC limit 0,5";
	$QURY_BILL = mysqli_query($connect,$SLCT_BILL); */
 
	$SLCT_BILL = "SELECT BPDD.`master_cat_id`, MC.`master_cat_name`, COUNT(DISTINCT BPDD.`design_det_id`) AS DES_CNT, COUNT(BPDD.`design_det_id`) AS TOT_CNT  FROM `billing_det` BD 
	INNER JOIN `billing_po_det` BPD ON BPD.`billing_det_id`=BD.`billing_det_id` 
    INNER JOIN `billing_po_design_det` BPDD ON BPDD.`billing_po_det_id`=BPD.`billing_po_det_id`
    INNER JOIN `master_cat` MC ON MC.`master_cat_id` = BPDD.`master_cat_id`
    WHERE BD.`cancelled`='no' AND BPD.`cancelled`='no' AND BPDD.`cancelled`='no'
	".$dat_str_bill." 
    GROUP BY BPDD.`master_cat_id`
	ORDER BY COUNT(BPDD.`design_det_id`) DESC";
	$QURY_BILL = mysqli_query($connect,$SLCT_BILL);
	if(mysqli_num_rows($QURY_BILL)>0)
	{
	while($FTCH_BILL = mysqli_fetch_object($QURY_BILL))
	{
		$catNmArray[] = $FTCH_BILL->master_cat_name;
		$catIdArray[] = $FTCH_BILL->master_cat_id;
		$catDsArray[] = $FTCH_BILL->DES_CNT;
		$catTtArray[] = $FTCH_BILL->TOT_CNT;
		//$catWtArray[] = $FTCH_BILL->TOT_WHT;
	}
	$totDes = array_sum($catDsArray);
	$totCnt = array_sum($catTtArray);
	$totWgt = '0';//array_sum($catWtArray);
	foreach($catIdArray AS $key=>$value)
	{
	$desPercentage = ($catDsArray[$key]/$totDes)*100;
	$desCumulative = $desCumulative+$desPercentage;
	$selPercentage = ($catTtArray[$key]/$totCnt)*100;
	$selCumulative = $selCumulative+$selPercentage;
    ?>
	<tr>
	<td align="center" style="display:table-cell;vertical-align:middle"><?php echo $loop; ?></td>
	<td align="left" style="display:table-cell;vertical-align:middle"><?php echo $catNmArray[$key]; ?></td>
	<td align="center" style="display:table-cell;vertical-align:middle"><a href="prod_sell_analize_view.php?cad_id=<?php echo base64_encode(base64_encode(base64_encode(base64_encode($catIdArray[$key])))); ?>&pg_flag=<?php echo base64_encode(base64_encode(base64_encode(base64_encode('1')))); ?>&fdate=<?php echo $fdate; ?>&todate=<?php echo $todate; ?>&yea_r=<?php echo $y_r; ?>&mont_h=<?php echo $m_h; ?>&da_y=<?php echo $d_y; ?>"><?php echo $catDsArray[$key]; ?></a></td>
<td align="center" style="display:table-cell;vertical-align:middle"><?php echo number_format($desPercentage,2); ?></td>
<td align="center" style="display:table-cell;vertical-align:middle"><?php echo number_format($desCumulative,2); ?></td>
<td align="center" style="display:table-cell;vertical-align:middle"><?php echo $catTtArray[$key]; ?></td>
<td align="center" style="display:table-cell;vertical-align:middle"><?php echo number_format($selPercentage,2); ?></td>
<td align="center" style="display:table-cell;vertical-align:middle"><?php echo number_format($selCumulative,2); ?></td>
<td align="center" style="display:table-cell;vertical-align:middle"><a href="prod_sell_analize_view.php?cad_id=<?php echo base64_encode(base64_encode(base64_encode(base64_encode($catIdArray[$key])))); ?>&pg_flag=<?php echo base64_encode(base64_encode(base64_encode(base64_encode('2')))); ?>&fdate=<?php echo $fdate; ?>&todate=<?php echo $todate; ?>&yea_r=<?php echo $y_r; ?>&mont_h=<?php echo $m_h; ?>&da_y=<?php echo $d_y; ?>">View</a></td>
	</tr>
		<?php
					$loop++;
				}
			}
		?> 
		</tbody>
					</table> 
				</div>
			</div>
	        			<?php } ?>
	        			<input name="tot_design" id="tot_design" type="hidden" value="<?php echo $totDes; ?>" />
	        			<input name="tot_selling" id="tot_selling" type="hidden" value="<?php echo $totCnt; ?>" />
	        			<input name="tot_weight" id="tot_weight" type="hidden" value="<?php echo number_format($totWgt,2); ?>" /> 
        			</fieldset>
     			</form>
        		<div class="fluid"><!-- Media table --></div>
				<?php } else {  echo ACCESS_MSG;} ?>
    		</div>
    		<!-- Main content ends -->
		</div>
		<!-- Content ends -->
	</body>
</html>
