<?php
	include "db_connection/db_connection.php";
	include "config/config.php";

	if($_SESSION['admin_login_id'] == '')
	{
		echo "<script>window.location='index.php';</script>";
	}
	$SLCT_e = "SELECT  * FROM admin,login_details WHERE login_details.admin_id=admin.admin_id AND admin.admin_id= '$_SESSION[admin_login_id]'";	
	//echo $SLCT_e;
	$QURY_e = mysqli_query($connect,$SLCT_e);
	$FTCH_e = mysqli_fetch_object($QURY_e);
	$admin_id	=	$_SESSION['admin_login_id'];
	
	$check_acc  = check_access('24');
			$ot=1;
			if(isset($_GET['ot'])!=''){
				$ot = $_GET['ot'];
			}
?>
<!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>
		<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="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
		<script type="text/javascript" src="js/plugins/charts/jquery.flot.js"></script>
		<script type="text/javascript" src="js/plugins/forms/jquery.chosen.min.js"></script>
		<script type="text/javascript" src="js/files/bootstrap.js"></script>
		<script src="js/Html2Canvas/html2canvas.js"></script>		
		<style>
			.axisLabel {
			    position: absolute;
			    text-align: center;
			    font-size: 12px;
			    font-weight: bold;
			}

			.xaxisLabel {
			    bottom: -15px;
			    left: 0;
			    right: 0;
			}

			.yaxisLabel {
			    top: 155px;
			    left: -18px;
			    transform: rotate(-90deg);
			    -o-transform: rotate(-90deg);
			    -ms-transform: rotate(-90deg);
			    -moz-transform: rotate(-90deg);
			    -webkit-transform:  rotate(-90deg);
			    transform-origin: 0 0;
			    -o-transform-origin: 0 0;
			    -ms-transform-origin: 0 0;
			    -moz-transform-origin: 0 0;
			    -webkit-transform-origin: 0 0;
			    font-weight: bold;
			}
		</style>
		<script>
			function reload_page(ids)
			{
				window.location='home.php?chrt_yr='+ids;
			}
		</script>
		<!-- Shared on MafiaShare.net  --><!-- Shared on MafiaShare.net  -->
	</head>
	<?php 
	
	$today = date('Y-m-d');
	$chk_bk = mysqli_query($connect,"SELECT * FROM `backup_details` where (start_date LIKE '%".$today."%' or end_date LIKE '%".$today."%') and status='1'");
	$dup_bk = mysqli_num_rows($chk_bk);
	
	
	?>
	<body>
		<!-- 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">
				<?php include "menus/top_left_menu.php"; ?>
			</div>
    		<!-- Main content -->
			<div class="wrapper" style="margin:100px; text-align:center;"><h3>Welcome to Thamaniyam</h3></div>
    		<div class="wrapper" style="display:none;">
   				<!--THIS IS FOR ALERT WHEN THE EMPLOYEE OR PARTY'S BIRHT DAY-->
				<?php 
				$SLCT= "SELECT * FROM party_det ORDER BY party_det_id ASC";
				$QURY= mysqli_query($connect,$SLCT);
				while($FTCH = mysqli_fetch_object($QURY))
				{
					$this_date =  date("d-m-Y", strtotime($cur_date));
					$dat_arr = explode('-',$this_date);
					$da_y = $dat_arr[0];
					$mon_th = $dat_arr[1];
					if($FTCH->dob_1!='')
					{
						$dateee1 = str_replace("/", "-", $FTCH->dob_1);
						$dob_1_arr = explode('-',$dateee1); //this is for party 1 date of birth
						$d_1_mon = $dob_1_arr[1];
						if($d_1_mon == $mon_th)
						{
							$d_1_day = $dob_1_arr[0];
							$dif = $d_1_day - $da_y; 
							if($dif ==0 || $dif ==1)
							{
				?>
								<script>
									var p_cod = '<?php echo $FTCH->party_code;  ?>';
									var c_name = '<?php echo $FTCH->cmp_name ?>';
									var pr_name = '<?php echo $FTCH->con_pers_1 ?>';
									var dob = '<?php echo $FTCH->dob_1 ?>';
									alert('\t\t Party \n \t\t ***** \n Party Code : '+p_cod+' \n Company Name : '+c_name+' \n Person Name : '+pr_name+' \n D.O.B : '+dob );
								</script>
				<?php
							}
						}
					}
					if($FTCH->dob_2!='')
					{
						$dateee2 = str_replace("/", "-", $FTCH->dob_2);
						$dob_2_arr = explode('-',$dateee2); //this is for party 2 date of birth
						$d_2_mon = $dob_2_arr[1];
						if($d_2_mon == $mon_th)
						{
							$d_2_day = $dob_2_arr[0];
							$dif = $d_2_day - $da_y; 
							if($dif ==0 || $dif ==1)
							{
				?>
								<script>
									var p_cod = '<?php echo $FTCH->party_code;  ?>';
									var c_name = '<?php echo $FTCH->cmp_name ?>';
									var pr_name = '<?php echo $FTCH->con_pers_2 ?>';
									var dob = '<?php echo $FTCH->dob_2 ?>';
									alert('\t\t Party \n \t\t ***** \n Party Code : '+p_cod+' \n Company Name : '+c_name+' \n Person Name : '+pr_name+' \n D.O.B : '+dob );
								</script>
				<?php
							}
						}
					}
					if($FTCH->dob_3!='')
					{
						$dateee3 = str_replace("/", "-", $FTCH->dob_3);
						$dob_3_arr = explode('-',$dateee3); //this is for party 3 date of birth
						$d_3_mon = $dob_3_arr[1];
						if($d_3_mon == $mon_th)
						{
							$d_3_day = $dob_3_arr[0];
							$dif = $d_3_day - $da_y; 
							if($dif ==0 || $dif ==1)
							{
							?>
								<script>
									var p_cod = '<?php echo $FTCH->party_code;  ?>';
									var c_name = '<?php echo $FTCH->cmp_name ?>';
									var pr_name = '<?php echo $FTCH->con_pers_3 ?>';
									var dob = '<?php echo $FTCH->dob_3 ?>';
									alert('\t\t Party \n \t\t ***** \n Party Code : '+p_cod+' \n Company Name : '+c_name+' \n Person Name : '+pr_name+' \n D.O.B : '+dob );
								</script>
							<?php
							}
						}
					}
				}

				$SLCTe= "SELECT * FROM emp_det where statu_s='active' ORDER BY emp_det_id ASC ";
				$QURYe= mysqli_query($connect,$SLCTe);
				while($FTCHe = mysqli_fetch_object($QURYe))
				{
					$this_date =  date("d-m-Y", strtotime($cur_date));
					$dat_arr = explode('-',$this_date);
					$da_y = $dat_arr[0];
					$mon_th = $dat_arr[1];
					if($FTCHe->dob!='')
					{
						$dateee = str_replace("/", "-", $FTCHe->dob);
						$dob_1_arr = explode('-',$dateee); //this is for employee
						$d_1_mon = $dob_1_arr[1];
						if($d_1_mon == $mon_th)
						{
							$d_1_day = $dob_1_arr[0];
							$dif = $d_1_day - $da_y; 
							if($dif ==0 || $dif ==1)
							{
							?>
								<script>
									var e_name = '<?php echo $FTCHe->name ?>';
									var edob = '<?php echo $FTCHe->dob ?>';
									alert('\t\t Employee \n \t\t ********** \n Name : '+e_name+' \n D.O.B : '+edob );
								</script>
							<?php
							}
						}
					}
				}
				if(isset($_GET['chrt_yr']) && $_GET['chrt_yr']!='')
				{
					$encde = $_GET['chrt_yr'];
					$chrt_yr = base64_decode(base64_decode(base64_decode(base64_decode($encde))));
					$dat_frm = date("Ym",strtotime($chrt_yr));
					$day_frm = date("m",strtotime($chrt_yr));
					$yer_frm = date("Y",strtotime($chrt_yr));

					$chrt_lbl = date("M Y",strtotime($chrt_yr));	
				}
				else
				{
					$chrt_lbl = date("M Y");
					$chrt_yr = date("F - Y");
					$encde = base64_encode(base64_encode(base64_encode(base64_encode($chrt_yr))));	
					$dat_frm = date("Ym",strtotime($chrt_yr));
					$day_frm = date("m",strtotime($chrt_yr));
					$yer_frm = date("Y",strtotime($chrt_yr));
				}
				if($admin_flagg==1)
				{
				?> 
					<div class="widget">
	        			<div class="formRow" >
			                <table width="100%">
			                    <tr>
			                        <td width="25%" style="display:table-cell; vertical-align:top;">Select Stage:</td>
			                        <td width="75%">
			                            <div class="searchDrop">
			                             <select  data-placeholder="" class="select" name="chrt_yr" id="chrt_yr" style="width:350px;" onchange="reload_page(this.value)">
			                                 <option selected="selected" value="">Choose a Year...</option>
			                                 <?php
			                                 	$SLCT_MONTH = "select DATE_FORMAT(m1, '%M - %Y') AS RSLT from( select ('2013-01-23' - INTERVAL DAYOFMONTH('2013-01-23')-1 DAY) +INTERVAL m MONTH as m1 from ( select @rownum:=@rownum+1 as m from (select 1 union select 2 union select 3 union select 4) t1, (select 1 union select 2 union select 3 union select 4) t2, (select 1 union select 2 union select 3 union select 4) t3, (select 1 union select 2 union select 3 union select 4) t4, (select @rownum:=-1) t0 ) d1 ) d2 where m1<=NOW() order by m1 DESC";
			                                 	echo $SLCT_MONTH;
			                                 	$QURY_MONTH = mysqli_query($connect,$SLCT_MONTH);
			                                 	while($FTCH_MONTH = mysqli_fetch_object($QURY_MONTH))
			                                 	{
			                                 		$MONTH_ENC = base64_encode(base64_encode(base64_encode(base64_encode($FTCH_MONTH->RSLT))));
			                                 ?>
			                                 		<option <?php if($encde == $MONTH_ENC){ ?> selected="selected" <?php }?> value="<?php echo $MONTH_ENC; ?>"><?php echo $FTCH_MONTH->RSLT; ?></option>
			                                 <?php
			                                 	}
			                                 ?>
			                            </select>
			                            </div> 
			                        </td>
			                    </tr>
			                </table>
			          	</div>
			       	</div>
					<div class="widget chartWrapper" style="overflow-x:scroll; overflow-y: hidden;">
			            <div class="whead">
			            	<h6>Orders Chart</h6>
			            	<span style="float: right; margin-right: 5px;">
			            		<a id="download1" class="buttonS bDefault opt tipS" original-title="Download" style="margin-top:5px;"><span class="icos-download" style="padding: 0px;"></span></a>
			                    <!--<span id="preview1" class="buttonS bDefault opt tipS" original-title="Refresh" style="margin-top:5px;"><span class="icos-refresh" style="padding: 0px;"></span></span>-->
			            	</span>
			            	<div class="clear"></div>
			           	</div>
			            <div id="orders_print" class="body" style="">
			            	<span style="font-weight:bold; margin-left:45%;">Orders Chart</span>
				            <div class="chart"></div>
			            </div>
			        </div>
			        <div class="widget chartWrapper" style="overflow-x:scroll; overflow-y: hidden;">
			            <div class="whead">
			            	<h6>Accounts Chart</h6>
			            	<span style="float: right; margin-right: 5px;">
			            		<a id="download2" class="buttonS bDefault opt tipS" original-title="Download" style="margin-top:5px; "><span class="icos-download" style="padding: 0px;"></span></a>
			                    <!--<span id="preview2" class="buttonS bDefault opt tipS" original-title="Refresh" style="margin-top:5px;"><span class="icos-refresh" style="padding: 0px;"></span></span>-->
			            	</span>
			            	<div class="clear"></div>			          		
			          	</div>
			            <div id="accounts_print" class="body">
			            	<span style="font-weight:bold; margin-left:45%;">Accounts Chart</span>
			            	<div class="acc_chart"></div>
			           	</div>
			        </div>
			        <div id="previewImage1" class="widget chartWrapper" style="display: none;"></div>
				    <div id="previewImage2" class="widget chartWrapper" style="display: none;"></div>
		        <?php
		        }
		        ?>
	    	</div>
	    	<?php
	    		if($admin_flagg==1)
				{
		    		$tot_day = cal_days_in_month(CAL_GREGORIAN,$day_frm,$yer_frm); // THIS IS FOR GET THE NO OF DAYS IN A MONTH
			    	$cur_day = date('d');
			    	if($day_frm==date('m'))
			    	{
						$rst_day = $cur_day;
					}
					else
					{
						$rst_day = $tot_day;
					}

					// THE PO ENTRY OPERATION STARTS HERE 
		    		$ENTR_DET 		= 	'';
		    		$MSTR_PIEC_EN	= 	'0';
					$RSLT_ENTR_DI 	=	'0';
					$RSLT_ENTR_DS 	=	'0';
					$MNTH_ENTR_DIE  =	array();

			    	$SLCT_ENTR = "SELECT GROUP_CONCAT(DISTINCT PE.party_det_id) AS par_id, GROUP_CONCAT(DISTINCT PE.po_entry_id) AS poe_id, GROUP_CONCAT(DISTINCT PE.po_no) AS poe_no, COUNT(PED.design_det_id) AS DES_CNT, EXTRACT(DAY FROM STR_TO_DATE(PE.po_date,'%d-%m-%Y')) AS DES_DAY, SUM(master_pieces) AS MSTR_PIEC FROM `po_entry` PE, po_entry_det PED, design_det DD WHERE DD.design_det_id = PED.design_det_id AND PE.po_entry_id=PED.po_entry_id AND PED.cancelled='no' AND EXTRACT(YEAR_MONTH FROM STR_TO_DATE(PE.po_date,'%d-%m-%Y'))='$dat_frm' GROUP BY STR_TO_DATE(PE.po_date,'%d-%m-%Y') ORDER BY STR_TO_DATE(PE.po_date,'%d-%m-%Y') ASC";
					$QURY_ENTR = mysqli_query($connect,$SLCT_ENTR);
					if(mysqli_num_rows($QURY_ENTR)>0)
					{
						while($FTCH_ENTR = mysqli_fetch_object($QURY_ENTR))
						{
							$party_expld = explode(",",$FTCH_ENTR->par_id);
							$poent_expld = explode(",",$FTCH_ENTR->poe_id);
							$poeno_expld = explode(",",$FTCH_ENTR->poe_no);
							foreach($party_expld AS $key=>$value)
							{
								$SLCT_ENTR_DD 	= "SELECT CONCAT(PD.`party_code`,' - ',PD.`cmp_name`,' - ','$poeno_expld[$key]',' - ',COUNT(PED.design_det_id),' Designs - ',SUM(DD.master_pieces),' Dies<br>') AS entr_det, SUM(DD.master_pieces) AS MSTR_PIEC FROM `po_entry` PE, po_entry_det PED, party_det PD, design_det DD WHERE PE.po_entry_id=PED.po_entry_id AND PE.po_entry_id='$poent_expld[$key]' AND PED.cancelled='no' AND PE.party_det_id = PD.party_det_id AND DD.design_det_id=PED.design_det_id";
								$QURY_ENTR_DD 	= mysqli_query($connect,$SLCT_ENTR_DD);
								$FTCH_ENTR_DD 	= mysqli_fetch_object($QURY_ENTR_DD);
								$ENTR_DET  		= $ENTR_DET.$FTCH_ENTR_DD->entr_det;
								//$MSTR_PIEC_EN	+= $FTCH_ENTR_DD->MSTR_PIEC;
							}
							$MSTR_PIEC_EN	+= $FTCH_ENTR->MSTR_PIEC;
							$MNTH_ENTR_DES[$FTCH_ENTR->DES_DAY] = $FTCH_ENTR->DES_CNT;
							$MNTH_ENTR_DIE[$FTCH_ENTR->DES_DAY] = $MSTR_PIEC_EN;
							$MNTH_ENTR_KEY[] = $FTCH_ENTR->DES_DAY;
							$MNTH_ENTR_DET[$FTCH_ENTR->DES_DAY] = trim($ENTR_DET,'<br>');
							$ENTR_DET ='';
							$MSTR_PIEC_EN = '0';
						}
					}
					if(count($MNTH_ENTR_DIE)>=1 && $MNTH_ENTR_DIE!='')
					{
						for($lop=0;$lop<=$rst_day;$lop++)
						{
							if(in_array($lop, $MNTH_ENTR_KEY))
							{
								$ENTR_DES_CNT[] 	= $MNTH_ENTR_DES[$lop];
								$ENTR_DIE_CNT[] 	= $MNTH_ENTR_DIE[$lop];
								$RSLT_ENTR_DI 		= $RSLT_ENTR_DI+$MNTH_ENTR_DIE[$lop];
								$RSLT_ENTR_DS 		= $RSLT_ENTR_DS+$MNTH_ENTR_DES[$lop];
								$RSLT_ENTR_DES[]	= $RSLT_ENTR_DS;
								$RSLT_ENTR_DIE[]	= $RSLT_ENTR_DI;
								$RSLT_ENTR_DET[]	= $MNTH_ENTR_DET[$lop];
							}
							else
							{
								$ENTR_DES_CNT[] 	= '0';
								$ENTR_DIE_CNT[]  	= '0';
								$RSLT_ENTR_DI		= $RSLT_ENTR_DI+0;
								$RSLT_ENTR_DS 	 	= $RSLT_ENTR_DS+0;
								$RSLT_ENTR_DES[] 	= $RSLT_ENTR_DS;
								$RSLT_ENTR_DIE[]	= $RSLT_ENTR_DI;
								$RSLT_ENTR_DET[] 	= '';
							}
						}
						$MAX_ENTR = max($MNTH_ENTR_DIE);
					}
					else
					{
						for($lop=0;$lop<=$rst_day;$lop++)
						{
							$ENTR_DES_CNT[] 	= '0';
							$ENTR_DIE_CNT[]  	= '0';
							$RSLT_ENTR_DI		= $RSLT_ENTR_DI+0;
							$RSLT_ENTR_DS 	 	= $RSLT_ENTR_DS+0;
							$RSLT_ENTR_DES[] 	= $RSLT_ENTR_DS;
							$RSLT_ENTR_DIE[]	= $RSLT_ENTR_DI;
							$RSLT_ENTR_DET[] 	= '';
						}
						$MAX_ENTR = 0;
					}
					$entryArray = array();
					 
					if ($rst_day>0) {
						$i = 0;
						do{
							$entryArray[$i]['designCount'] 	= $ENTR_DES_CNT[$i];
							$entryArray[$i]['dieCount'] 	= $ENTR_DIE_CNT[$i];
							$entryArray[$i]['entryDesign'] 	= $RSLT_ENTR_DES[$i];
							$entryArray[$i]['entryDie']	 	= $RSLT_ENTR_DIE[$i];
							$entryArray[$i]['entryDetails'] = $RSLT_ENTR_DET[$i];
							$i++;
						} while ($rst_day>=$i);
					} 
					else
					{
						$entryArray[0]['designCount'] 	= '';
						$entryArray[0]['dieCount'] 		= '';
						$entryArray[0]['entryDesign'] 	= '';
						$entryArray[0]['entryDie']	 	= '';
						$entryArray[0]['entryDetails'] 	= '';
					}
					$entryArrayJSON = json_encode($entryArray);
					// THE PO ENTRY OPERATION ENDS HERE 

					// THE PO FINISH OPERATION STARTS HERE 

					$FNSH_DET 		= 	'';
		    		$MSTR_PIEC_FN	= 	'0';
					$RSLT_FNSH_DI 	=	'0';
					$RSLT_FNSH_DS 	=	'0';
					$MNTH_FNSH_DIE	=	array();
					
					//$SLCT_FNSH = "SELECT GROUP_CONCAT(DISTINCT PE.party_det_id) AS par_id, GROUP_CONCAT(DISTINCT PE.po_entry_id) AS poe_id, GROUP_CONCAT(DISTINCT PE.po_no) AS poe_no, COUNT(design_det_id) AS DES_CNT, EXTRACT(DAY FROM STR_TO_DATE(die_fin_date,'%d-%m-%Y')) AS DES_DAY FROM `po_finish` WHERE EXTRACT(YEAR_MONTH FROM STR_TO_DATE(die_fin_date,'%d-%m-%Y'))='$dat_frm' GROUP BY STR_TO_DATE(`die_fin_date`,'%d-%m-%Y') ORDER BY STR_TO_DATE(`die_fin_date`,'%d-%m-%Y') ASC";
					//$SLCT_FNSH = "SELECT GROUP_CONCAT(DISTINCT PE.party_det_id) AS par_id, GROUP_CONCAT(DISTINCT PE.po_entry_id) AS poe_id, GROUP_CONCAT(DISTINCT PE.po_no) AS poe_no, COUNT(PED.design_det_id) AS DES_CNT, EXTRACT(DAY FROM STR_TO_DATE(PF.die_fin_date,'%d-%m-%Y')) AS DES_DAY FROM `po_finish` PF,`po_entry` PE, po_entry_det PED WHERE PE.po_entry_id=PED.po_entry_id AND PF.po_entry_det_id=PED.po_entry_det_id AND PED.cancelled='no' AND EXTRACT(YEAR_MONTH FROM STR_TO_DATE(PF.die_fin_date,'%d-%m-%Y'))='$dat_frm' GROUP BY STR_TO_DATE(PF.die_fin_date,'%d-%m-%Y') ORDER BY STR_TO_DATE(PF.die_fin_date,'%d-%m-%Y') ASC";
					$SLCT_FNSH = "SELECT GROUP_CONCAT(DISTINCT PED.po_entry_id) AS poe_id, COUNT(PED.design_det_id) AS DES_CNT, EXTRACT(DAY FROM STR_TO_DATE(PF.post_dt,'%Y-%m-%d')) AS DES_DAY, SUM(master_pieces) AS MSTR_PIEC FROM `po_finish` PF, po_entry_det PED, design_det DD, po_entry PE WHERE PF.po_entry_det_id=PED.po_entry_det_id AND DD.design_det_id = PED.design_det_id AND PE.po_entry_id=PED.po_entry_id AND PED.cancelled='no' AND PED.fin_status='yes' AND EXTRACT(YEAR_MONTH FROM STR_TO_DATE(PF.post_dt,'%Y-%m-%d'))='$dat_frm' GROUP BY STR_TO_DATE(PF.post_dt,'%Y-%m-%d') ORDER BY STR_TO_DATE(PF.post_dt,'%Y-%m-%d') ASC";
					//echo $SLCT_FNSH;
					$QURY_FNSH = mysqli_query($connect,$SLCT_FNSH);
					$loop=1;
			if(mysqli_num_rows($QURY_FNSH)>0){
				while($FTCH_FNSH = mysqli_fetch_object($QURY_FNSH)){
					$poent_expld = explode(",",$FTCH_FNSH->poe_id);
						foreach($poent_expld AS $key=>$value){
								//$SLCT_FNSH_DD 	= "SELECT CONCAT(PD.`party_code`,' - ',PD.`cmp_name`,' - ','$poeno_expld[$key]',' - ',COUNT(PED.design_det_id),' Designs - ',SUM(DD.master_pieces),' Dies<br>') AS fnsh_det, SUM(DD.master_pieces) AS MSTR_PIEC FROM `po_finish` PF,`po_entry` PE, po_entry_det PED, party_det PD, design_det DD WHERE PE.po_entry_id=PED.po_entry_id AND PF.po_entry_det_id=PED.po_entry_det_id AND PE.po_entry_id='$poent_expld[$key]' AND PED.cancelled='no' AND PED.fin_status='yes' AND PE.party_det_id = PD.party_det_id AND DD.design_det_id=PED.design_det_id";
								$SLCT_FNSH_DD 	= "SELECT PD.`party_code`,PD.`cmp_name`,PE.po_no FROM `po_entry` PE, party_det PD WHERE PE.po_entry_id='$poent_expld[$key]' AND PE.party_det_id = PD.party_det_id ";
								$QURY_FNSH_DD 	= mysqli_query($connect,$SLCT_FNSH_DD);
								$FTCH_FNSH_DD 	= mysqli_fetch_object($QURY_FNSH_DD);

								$SLCT_FNSH_DD2 	= "SELECT CONCAT('$FTCH_FNSH_DD->party_code',' - ','$FTCH_FNSH_DD->cmp_name',' - ','$FTCH_FNSH_DD->po_no',' - ',COUNT( PF.design_det_id ),' Designs - ',SUM( master_pieces ),' Dies<br>') AS fnsh_det, SUM( master_pieces ) AS MSTR_PIEC FROM  `po_finish` PF, po_entry_det PED, design_det DD, po_entry PE WHERE PE.po_entry_id = PED.po_entry_id AND PED.fin_status='yes' AND PED.cancelled='no' AND PF.`po_entry_det_id` = PED.`po_entry_det_id` AND PED.po_entry_id='$poent_expld[$key]' AND DD.design_det_id = PED.design_det_id AND EXTRACT(YEAR_MONTH FROM STR_TO_DATE(PF.post_dt,'%Y-%m-%d'))='$dat_frm' AND EXTRACT(DAY FROM STR_TO_DATE(PF.post_dt,'%Y-%m-%d'))='$FTCH_FNSH->DES_DAY' GROUP BY STR_TO_DATE(PF.post_dt,'%Y-%m-%d') ORDER BY STR_TO_DATE(PF.post_dt,'%Y-%m-%d') ASC";
								$QURY_FNSH_DD2 	= mysqli_query($connect,$SLCT_FNSH_DD2);
								$FTCH_FNSH_DD2 	= mysqli_fetch_object($QURY_FNSH_DD2);
								$FNSH_DET  		= $FNSH_DET.$FTCH_FNSH_DD2->fnsh_det;
								$MSTR_PIEC_FN	+= $FTCH_FNSH_DD2->MSTR_PIEC;
							}
							$MNTH_FNSH_DES[$FTCH_FNSH->DES_DAY] = $FTCH_FNSH->DES_CNT;
							$MNTH_FNSH_DIE[$FTCH_FNSH->DES_DAY] = $MSTR_PIEC_FN;
							$MNTH_FNSH_KEY[] = $FTCH_FNSH->DES_DAY;
							$MNTH_FNSH_DET[$FTCH_FNSH->DES_DAY] = trim($FNSH_DET,'<br>');
							$FNSH_DET ='';
							$MSTR_PIEC_FN = '0';
						}
					}
			if(count($MNTH_FNSH_DIE)>=1 && $MNTH_FNSH_DIE!=''){
				for($lop=0;$lop<=$rst_day;$lop++){
					if(in_array($lop, $MNTH_FNSH_KEY)){
						$FNSH_DES_CNT[] 	= $MNTH_FNSH_DES[$lop];
						$FNSH_DIE_CNT[] 	= $MNTH_FNSH_DIE[$lop];
						$RSLT_FNSH_DI 	= $RSLT_FNSH_DI+$MNTH_FNSH_DIE[$lop];
						$RSLT_FNSH_DS 		= $RSLT_FNSH_DS+$MNTH_FNSH_DES[$lop];
						$RSLT_FNSH_DES[]	= $RSLT_FNSH_DS;
						$RSLT_FNSH_DIE[]	= $RSLT_FNSH_DI;
						$RSLT_FNSH_DET[]	= $MNTH_FNSH_DET[$lop];
					} else {
						$FNSH_DES_CNT[] 	= '0';
						$FNSH_DIE_CNT[]  	= '0';
						$RSLT_FNSH_DI		= $RSLT_FNSH_DI+0;
						$RSLT_FNSH_DS 	 	= $RSLT_FNSH_DS+0;
						$RSLT_FNSH_DES[] 	= $RSLT_FNSH_DS;
						$RSLT_FNSH_DIE[]	= $RSLT_FNSH_DI;
						$RSLT_FNSH_DET[] 	= '';
					}
				}
						$MAX_FNSH = max($MNTH_FNSH_DIE);
			} else {
			for($lop=0;$lop<=$rst_day;$lop++)
			{
				$FNSH_DES_CNT[] 	= '0';
				$FNSH_DIE_CNT[]  	= '0';
				$RSLT_FNSH_DI		= $RSLT_FNSH_DI+0;
				$RSLT_FNSH_DS 	 	= $RSLT_FNSH_DS+0;
				$RSLT_FNSH_DES[] 	= $RSLT_FNSH_DS;
				$RSLT_FNSH_DIE[]	= $RSLT_FNSH_DI;
				$RSLT_FNSH_DET[] 	= '';
			}
			$MAX_FNSH = 0;
					}
					$finishArray = array();

					if ($rst_day>0) {
						$j = 0;
						do{

							$finishArray[$j]['designCount'] 	= $FNSH_DES_CNT[$j];
							$finishArray[$j]['dieCount'] 		= $FNSH_DIE_CNT[$j];
							$finishArray[$j]['entryDesign'] 	= $RSLT_FNSH_DES[$j];
							$finishArray[$j]['entryDie']	 	= $RSLT_FNSH_DIE[$j];
							$finishArray[$j]['entryDetails'] 	= $RSLT_FNSH_DET[$j];
							$j++;
						} while ($rst_day>=$j);
					} 
					else
					{
						$finishArray[0]['designCount'] 		= '';
						$finishArray[0]['dieCount'] 		= '';
						$finishArray[0]['entryDesign'] 		= '';
						$finishArray[0]['entryDie']	 		= '';
						$finishArray[0]['entryDetails'] 	= '';
					}
					$finshArrayJSON = json_encode($finishArray);
					// THE PO FINISH OPERATION ENDS HERE 

					// THE PO DELIVERY OPERATION STARTS HERE 

		    		$DELI_DET 		= 	'';
		    		$MSTR_PIEC_DL	= 	'0';
					$RSLT_DELI_DI 	=	'0';
					$RSLT_DELI_DS 	=	'0';
					$MNTH_DELI_DIE	=	array();

					//$SLCT_DELI = "SELECT COUNT(PDD.design_det_id) AS DES_CNT, EXTRACT(DAY FROM STR_TO_DATE(PD.delivery_date,'%d-%m-%Y')) AS DES_DAY FROM `po_delivery` PD, po_delivery_det PDD WHERE PD.po_delivery_id=PDD.po_delivery_id AND PDD.cancelled='no' AND EXTRACT(YEAR_MONTH FROM STR_TO_DATE(PD.delivery_date,'%d-%m-%Y'))='$dat_frm' GROUP BY STR_TO_DATE(PD.delivery_date,'%d-%m-%Y') ORDER BY STR_TO_DATE(PD.delivery_date,'%d-%m-%Y') ASC";
					$SLCT_DELI = "SELECT GROUP_CONCAT(DISTINCT PE.party_det_id) AS par_id, GROUP_CONCAT(DISTINCT PE.po_entry_id) AS poe_id, GROUP_CONCAT(DISTINCT PE.po_no) AS poe_no, COUNT(PDD.design_det_id) AS DES_CNT, EXTRACT(DAY FROM STR_TO_DATE(PD.delivery_date,'%d-%m-%Y')) AS DES_DAY, SUM(master_pieces) AS MSTR_PIEC FROM `po_entry` PE, `po_delivery` PD, po_delivery_det PDD, design_det DD WHERE DD.design_det_id = PDD.design_det_id AND PE.po_entry_id=PD.po_entry_id AND PD.po_delivery_id=PDD.po_delivery_id AND PDD.cancelled='no' AND EXTRACT(YEAR_MONTH FROM STR_TO_DATE(PD.delivery_date,'%d-%m-%Y'))='$dat_frm' GROUP BY STR_TO_DATE(PD.delivery_date,'%d-%m-%Y') ORDER BY STR_TO_DATE(PD.delivery_date,'%d-%m-%Y') ASC";
					$QURY_DELI = mysqli_query($connect,$SLCT_DELI);
					if(mysqli_num_rows($QURY_DELI)>0)
					{
						while($FTCH_DELI = mysqli_fetch_object($QURY_DELI))
						{
							$party_expld = explode(",",$FTCH_DELI->par_id);
							$poent_expld = explode(",",$FTCH_DELI->poe_id);
							$poeno_expld = explode(",",$FTCH_DELI->poe_no);
							foreach($party_expld AS $key=>$value)
							{
								$SLCT_DELI_DD1 	= "SELECT PD.`party_code`,PD.`cmp_name`,PE.po_no FROM `po_entry` PE, party_det PD WHERE PE.po_entry_id='$poent_expld[$key]' AND PE.party_det_id = PD.party_det_id ";
								$QURY_DELI_DD1 	= mysqli_query($connect,$SLCT_DELI_DD1);
								$FTCH_DELI_DD1 	= mysqli_fetch_object($QURY_DELI_DD1);

								$SLCT_DELI_DD 	= "SELECT CONCAT('$FTCH_DELI_DD1->party_code',' - ','$FTCH_DELI_DD1->cmp_name',' - ','$FTCH_DELI_DD1->po_no',' - ',COUNT( PDD.design_det_id ),' Designs - ',SUM( master_pieces ),' Dies<br>') AS deli_det, SUM(DD.master_pieces) AS MSTR_PIEC FROM `po_entry` PE, `po_delivery` PD, po_delivery_det PDD, design_det DD WHERE DD.design_det_id = PDD.design_det_id AND PE.po_entry_id=PD.po_entry_id AND PD.po_delivery_id=PDD.po_delivery_id AND PDD.cancelled='no' AND PE.po_entry_id='$poent_expld[$key]' AND EXTRACT(YEAR_MONTH FROM STR_TO_DATE(PD.delivery_date,'%d-%m-%Y'))='$dat_frm'";
								$QURY_DELI_DD 	= mysqli_query($connect,$SLCT_DELI_DD);
								$FTCH_DELI_DD 	= mysqli_fetch_object($QURY_DELI_DD);
								$DELI_DET  		= $DELI_DET.$FTCH_DELI_DD->deli_det;
								//$MSTR_PIEC_DL	+= $FTCH_DELI_DD->MSTR_PIEC;
							}
							$MSTR_PIEC_DL	+= $FTCH_DELI->MSTR_PIEC;
							$MNTH_DELI_DES[$FTCH_DELI->DES_DAY] = $FTCH_DELI->DES_CNT;
							$MNTH_DELI_DIE[$FTCH_DELI->DES_DAY] = $MSTR_PIEC_DL;
							$MNTH_DELI_KEY[] = $FTCH_DELI->DES_DAY;
							$MNTH_DELI_DET[$FTCH_DELI->DES_DAY] = trim($DELI_DET,'<br>');
							$DELI_DET ='';
							$MSTR_PIEC_DL = '0';
						}
					}
					if(count($MNTH_DELI_DIE)>=1 && $MNTH_DELI_DIE!='')
					{
						for($lop=0;$lop<=$rst_day;$lop++)
						{
							if(in_array($lop, $MNTH_DELI_KEY))
							{
								$DELI_DES_CNT[] 	= $MNTH_DELI_DES[$lop];
								$DELI_DIE_CNT[] 	= $MNTH_DELI_DIE[$lop];
								$RSLT_DELI_DI 		= $RSLT_DELI_DI+$MNTH_DELI_DIE[$lop];
								$RSLT_DELI_DS 		= $RSLT_DELI_DS+$MNTH_DELI_DES[$lop];
								$RSLT_DELI_DES[]	= $RSLT_DELI_DS;
								$RSLT_DELI_DIE[]	= $RSLT_DELI_DI;
								$RSLT_DELI_DET[]	= $MNTH_DELI_DET[$lop];
							}
							else
							{
								$DELI_DES_CNT[] 	= '0';
								$DELI_DIE_CNT[]  	= '0';
								$RSLT_DELI_DI		= $RSLT_DELI_DI+0;
								$RSLT_DELI_DS 	 	= $RSLT_DELI_DS+0;
								$RSLT_DELI_DES[] 	= $RSLT_DELI_DS;
								$RSLT_DELI_DIE[]	= $RSLT_DELI_DI;
								$RSLT_DELI_DET[] 	= '';
							}
						}
						$MAX_DELI = max($MNTH_DELI_DIE);
					}
					else
					{
						for($lop=0;$lop<=$rst_day;$lop++)
						{
							$DELI_DES_CNT[] 	= '0';
							$DELI_DIE_CNT[]  	= '0';
							$RSLT_DELI_DI		= $RSLT_DELI_DI+0;
							$RSLT_DELI_DS 	 	= $RSLT_DELI_DS+0;
							$RSLT_DELI_DES[] 	= $RSLT_DELI_DS;
							$RSLT_DELI_DIE[]	= $RSLT_DELI_DI;
							$RSLT_DELI_DET[] 	= '';
						}
						$MAX_DELI = 0;
					}
		    		$deliveryArray = array();

					if ($rst_day>0) {
						$k = 0;
						do{
							$deliveryArray[$k]['designCount'] 	= $DELI_DES_CNT[$k];
							$deliveryArray[$k]['dieCount'] 		= $DELI_DIE_CNT[$k];
							$deliveryArray[$k]['entryDesign'] 	= $RSLT_DELI_DES[$k];
							$deliveryArray[$k]['entryDie']	 	= $RSLT_DELI_DIE[$k];
							$deliveryArray[$k]['entryDetails'] 	= $RSLT_DELI_DET[$k];
							$k++;
						} while ($rst_day>=$k);
					} 
					else
					{
						$deliveryArray[0]['designCount'] 		= '';
						$deliveryArray[0]['dieCount'] 			= '';
						$deliveryArray[0]['entryDesign'] 		= '';
						$deliveryArray[0]['entryDie']	 		= '';
						$deliveryArray[0]['entryDetails'] 		= '';
					}
					$deliveryArrayJSON = json_encode($deliveryArray);
		    		// THE PO DELIVERY OPERATION ENDS HERE
		    		
		    		$RSLT_MAX_EFD  = max($RSLT_ENTR_DI,$RSLT_FNSH_DI,$RSLT_DELI_DI);
				   	if($RSLT_MAX_EFD==0)
				  	{
				   		$RSLT_MAX_EFD = 10;	
				   	}

		    		// THE BILLING OPERATION STARTS HERE

			    	$MNTH_BILL = array();
			    	$MNTH_BILL_KEY = array();
			    	$MNTH_BILL_DET = array();
			    	$RSLT_BILL = array();
			    	$RSLT_BILL_DI = 0;
			    	$RSLT_BILL_DET = array();
			    	$RSLT_BILL_AMT = array();
			    	$tot_day = cal_days_in_month(CAL_GREGORIAN,$day_frm,$yer_frm);
			    	$SLCT_BILL = "SELECT GROUP_CONCAT(CONCAT(`party_code`,' / ',`cmp_name`,' - ',`bill_no`,' - Rs. ',`bill_amt`) SEPARATOR '<br>') AS bill_det,SUM(bill_amt) AS bill_amt, EXTRACT(DAY FROM STR_TO_DATE(bill_date,'%d-%m-%Y')) AS bill_day FROM `billing_det`,`party_det` WHERE party_det.party_det_id = `billing_det`.party_det_id AND EXTRACT(YEAR_MONTH FROM STR_TO_DATE(bill_date,'%d-%m-%Y'))='$dat_frm' GROUP BY STR_TO_DATE(`bill_date`,'%d-%m-%Y') ORDER BY STR_TO_DATE(`bill_date`,'%d-%m-%Y') ASC";
					$QURY_BILL = mysqli_query($connect,$SLCT_BILL);
					if(mysqli_num_rows($QURY_BILL)>0)
					{
						while($FTCH_BILL = mysqli_fetch_object($QURY_BILL))
						{
							$MNTH_BILL[$FTCH_BILL->bill_day] = $FTCH_BILL->bill_amt;
							$MNTH_BILL_KEY[] = $FTCH_BILL->bill_day;
							$MNTH_BILL_DET[$FTCH_BILL->bill_day] = trim($FTCH_BILL->bill_det,'<br>');
						}
					}
					if(count($MNTH_BILL)>=1 && $MNTH_BILL!='')
					{
						for($lop=0;$lop<=$rst_day;$lop++)
						{
							if(in_array($lop, $MNTH_BILL_KEY))
							{
								$RSLT_BILL_AMT[] = $MNTH_BILL[$lop];
								$RSLT_BILL_DI = $RSLT_BILL_DI+$MNTH_BILL[$lop];
								$RSLT_BILL[] = $RSLT_BILL_DI;
								$RSLT_BILL_DET[] = $MNTH_BILL_DET[$lop];
							}
							else
							{
								$RSLT_BILL_AMT[] = '0';
								$RSLT_BILL_DI = $RSLT_BILL_DI+0;
								$RSLT_BILL[] = $RSLT_BILL_DI;
								$RSLT_BILL_DET[] = '';
							}
						}
						$MAX_BILL = max($MNTH_BILL);
					}
					else
					{
						for($lop=0;$lop<=$rst_day;$lop++)
						{
							$RSLT_BILL_AMT[] = '0';
							$RSLT_BILL_DI = $RSLT_BILL_DI+0;
							$RSLT_BILL[] = $RSLT_BILL_DI;
							$RSLT_BILL_DET[] = '';
						}
						$MAX_BILL = 0;
					}
					$billArray = array();
					 
					if ($rst_day>0) {
						$k = 0;
						do{
							$billArray[$k]['accountsCount'] 	= $RSLT_BILL[$k];
							$billArray[$k]['accountsAmount'] 	= $RSLT_BILL_AMT[$k];
							$billArray[$k]['accountsDetails'] 	= $RSLT_BILL_DET[$k];
							$k++;
						} while ($rst_day>=$k);
					} 
					else
					{
						$billArray[0]['accountsCount'] 		= '';
						$billArray[0]['accountsAmount']		= '';
						$billArray[0]['accountsDetails'] 	= '';
					}
					$billArrayJSON = json_encode($billArray);
					// THE BILLING OPERATION ENDS HERE 

					// THE COLLECTION OPERATION STARTS HERE 

					$MNTH_COLL = array();
			    	$MNTH_COLL_KEY = '';
			    	$MNTH_COLL_DET = '';
			    	$RSLT_COLL = array();
			    	$RSLT_COLL_DI = 0;
			    	$RSLT_COLL_DET = array();
			    	$RSLT_COLL_AMT = array();
					$SLCT_COLL = "SELECT GROUP_CONCAT(CONCAT(PD.`party_code`,' / ',PD.`cmp_name` ,' - Rs. ', ARD.`received_amt`) SEPARATOR '<br>') AS coll_det, SUM(ARD.`received_amt`) AS received_amt, EXTRACT(DAY FROM STR_TO_DATE(ARD.`received_date`,'%d-%m-%Y')) AS received_day FROM `ac_received_det` ARD, `ac_party_det` APD, `party_det` PD WHERE APD.`ac_party_det_id`=ARD.`ac_party_det_id` AND APD.`party_det_id`=PD.`party_det_id` AND EXTRACT(YEAR_MONTH FROM STR_TO_DATE(ARD.`received_date`,'%d-%m-%Y'))='$dat_frm' GROUP BY STR_TO_DATE(ARD.`received_date`,'%d-%m-%Y') ORDER BY STR_TO_DATE(ARD.`received_date`,'%d-%m-%Y') ASC";
					$QURY_COLL = mysqli_query($connect,$SLCT_COLL);
					if(mysqli_num_rows($QURY_COLL)>0)
					{
				while($FTCH_COLL = mysqli_fetch_object($QURY_COLL)){
					$MNTH_COLL[$FTCH_COLL->received_day] = $FTCH_COLL->received_amt;
					$MNTH_COLL_KEY[] = $FTCH_COLL->received_day;
					$MNTH_COLL_DET[$FTCH_COLL->received_day] = trim($FTCH_COLL->coll_det,'<br>');
				}
			}
					if(count($MNTH_COLL)>=1 && $MNTH_COLL!='')
					{
						for($lop=0;$lop<=$rst_day;$lop++)
						{
							if(in_array($lop, $MNTH_COLL_KEY))
							{
								$RSLT_COLL_AMT[] = $MNTH_COLL[$lop];
								$RSLT_COLL_DI = $RSLT_COLL_DI+$MNTH_COLL[$lop];
								$RSLT_COLL[] = $RSLT_COLL_DI;
								$RSLT_COLL_DET[] = $MNTH_COLL_DET[$lop];
							}
							else
							{
								$RSLT_COLL_AMT[] = '0';
								$RSLT_COLL_DI = $RSLT_COLL_DI+0;
								$RSLT_COLL[] = $RSLT_COLL_DI;
								$RSLT_COLL_DET[] = '';
							}
						}
						$MAX_COLL = max($MNTH_COLL);
					}
					else
					{
						for($lop=0;$lop<=$rst_day;$lop++)
						{
							$RSLT_COLL_AMT[] = '0';
							$RSLT_COLL_DI = $RSLT_COLL_DI+0;
							$RSLT_COLL[] = $RSLT_COLL_DI;
							$RSLT_COLL_DET[] = '';
						}
						$MAX_COLL = 0;
					}

					$collArray = array();

					if ($rst_day>0) {
						$k = 0;
						do{
							$collArray[$k]['accountsCount'] 	= $RSLT_COLL[$k];
							$collArray[$k]['accountsAmount'] 	= $RSLT_COLL_AMT[$k];
							$collArray[$k]['accountsDetails'] 	= $RSLT_COLL_DET[$k];
							$k++;
						} while ($rst_day>=$k);
					} 
					else
					{
						$collArray[0]['accountsCount'] 		= '';
						$collArray[0]['accountsAmount']		= '';
						$collArray[0]['accountsDetails'] 	= '';
					}
					$collArrayJSON = json_encode($collArray);

					// THE COLLECTION OPERATION ENDS HERE 

					$RSLT_MAX  = max($RSLT_BILL_DI,$RSLT_COLL_DI);
				    if($RSLT_MAX==0)
				  	{
				   		$RSLT_MAX = 10;	
				   	}
				}
		    ?>
	    </div>
	    <?php
	    if($admin_flagg==1)
		{
		?>
		    <script>
				//===== Chosen plugin =====//
				$(".select").chosen();

				// THIS IS FOR ORDERS CHART PRINT CODE
				var element_ord = $("#orders_print"); // global variable
				var getCanvas_ord; // global variable
				var element_acc = $("#accounts_print"); // global variable
				var getCanvas_acc; // global variable

				$( document ).ready(function() {
				   html2canvas(element_ord, {
						onrendered: function (canvas_ord) {
						$("#previewImage1").append(canvas_ord);
						getCanvas_ord = canvas_ord;
						}
					});

					html2canvas(element_acc, {
						onrendered: function (canvas_acc) {
						$("#previewImage2").append(canvas_acc);
						getCanvas_acc = canvas_acc;
						}
					});
				});

				/*$("#preview1").on('click', function () {
					html2canvas(element_ord, {
						onrendered: function (canvas_ord) {
						$("#previewImage1").append(canvas_ord);
						getCanvas_ord = canvas_ord;
						}
					});
					$("#download1").show();
				});*/
				$("#download1").on('click', function () {
					var imgageData_ord = getCanvas_ord.toDataURL("image/png");
					// Now browser starts downloading it instead of just showing it
					var newData_ord = imgageData_ord.replace(/^data:image\/png/, "data:application/octet-stream");
					$("#download1").attr("download", "Orders Chart on <?php echo $chrt_yr; ?>.png").attr("href", newData_ord);
				});

				// THIS IS FOR ACCOUNTS CHART PRINT CODE
				/*$("#preview2").on('click', function () {
					html2canvas(element_acc, {
						onrendered: function (canvas_acc) {
						$("#previewImage2").append(canvas_acc);
						getCanvas_acc = canvas_acc;
						}
					});
					$("#download2").show();
				});*/
				$("#download2").on('click', function () {
					var imgageData_acc = getCanvas_acc.toDataURL("image/png");
					// Now browser starts downloading it instead of just showing it
					var newData_acc = imgageData_acc.replace(/^data:image\/png/, "data:application/octet-stream");
					$("#download2").attr("download", "Accounts Chart on <?php echo $chrt_yr; ?>.png").attr("href", newData_acc);
				});
			</script>
		    <script>
		    	var day_cnt = '<?php echo ltrim($rst_day,"0"); ?>';	    		
		    	$(function () {
		    		var array_en = new Array();
		    		var array_fn = new Array();
		    		var array_dl = new Array();

		    		var arrayEntry = '<?php echo $entryArrayJSON ?>';
		    		var arrayFinish = '<?php echo $finshArrayJSON ?>';
		    		var arrayDelivery = '<?php echo $deliveryArrayJSON ?>';

				   array_en = $.parseJSON(arrayEntry);
				   array_fn = $.parseJSON(arrayFinish);
				   array_dl = $.parseJSON(arrayDelivery);
				   var entryString = [], finishString = [], deliveryString = [];

				    for (var i = 0; i <= day_cnt; i++) {
				        entryString.push([i,array_en[i]['entryDie']]);
				    }

				    for (var i = 0; i <= day_cnt; i++) {
				        finishString.push([i,array_fn[i]['entryDie']]);
				    }

				    for (var i = 0; i <= day_cnt; i++) {
				        deliveryString.push([i,array_dl[i]['entryDie']]);
				    }

				    var entry_rslt = array_en[day_cnt]['entryDesign']+"( "+array_en[day_cnt]['entryDie']+" )";
		    		var finis_rslt = array_fn[day_cnt]['entryDesign']+"( "+array_fn[day_cnt]['entryDie']+" )";
		    		var deliv_rslt = array_dl[day_cnt]['entryDesign']+"( "+array_dl[day_cnt]['entryDie']+" )";

					var plot = $.plot($(".chart"),
			           [ { data: entryString, textval: "PO Entry", label: "PO Entry - "+entry_rslt}, { data: finishString, textval: "PO Finish", label: "PO Finish - "+finis_rslt}, { data: deliveryString, textval: "PO Delivery", label: "PO Delivery - "+deliv_rslt}], {
			               series: {
			                   lines: { show: true },
			                   points: { show: true }
			               },
			               grid: { hoverable: true, clickable: true },
			               yaxis: { min: 0, max: '<?php echo $RSLT_MAX_EFD; ?>' },
						   xaxis: { min: 0, max: '<?php echo $tot_day; ?>' }
			             });

			    	function showTooltip(pageX, pageY, textval, label, x, y, day_suffix) {
						if(x<26){
							var ps = pageX + 5;
						}else {
							var ps =1200;
						}

				    	if(textval=='PO Entry')
				    	{
							var totDes	= "Total "+array_en[x]['entryDesign']+" Designs, "+y+" Dies  till "+ x + day_suffix + " <?php echo $chrt_lbl; ?><hr>";
							var det		= textval + " on " + x + day_suffix + " <?php echo $chrt_lbl; ?> => "+ array_en[x]['designCount'] +" Designs, "+ array_en[x]['dieCount'] +" Dies<br>"+array_en[x]['entryDetails'];
						}
						else if(textval=='PO Finish')
						{
							var totDes	= "Total "+array_fn[x]['entryDesign']+" Designs, "+y+" Dies till "+ x + day_suffix + " <?php echo $chrt_lbl; ?><hr>";
							var det		= textval + " on " + x + day_suffix + " <?php echo $chrt_lbl; ?> => "+ array_fn[x]['designCount'] +" Designs, "+ array_fn[x]['dieCount'] +" Dies<br>"+array_fn[x]['entryDetails'];
						}
						else if(textval=='PO Delivery')
						{
							var totDes	= "Total "+array_dl[x]['entryDesign']+" Designs, "+y+" Dies till "+ x + day_suffix + " <?php echo $chrt_lbl; ?><hr>";
							var det		= textval + " on " + x + day_suffix + " <?php echo $chrt_lbl; ?> => "+ array_dl[x]['designCount'] +" Designs, "+ array_dl[x]['dieCount'] +" Dies<br>"+array_dl[x]['entryDetails'];
						}

						$('<div id="tooltip" class="tooltip" style="margin-left:auto;">' + totDes + det + '</div>').css( {
				            position: 'absolute',
				            display: 'none',
				            top: pageY + 5,
				            left:ps,
							'z-index': '9999',
							'color': '#fff',
							'font-size': '11px',
							'margin':'auto',
							'display':'inline-block',
				            opacity: 0.8
				        }).appendTo("body").fadeIn(200);
						
			    	}

				    var previousPoint = null;
				    $(".chart").bind("plothover", function (event, pos, item) {
				        $("#x").text(pos.x);
				        $("#y").text(pos.y.toFixed(2));

				        if ($(".chart").length > 0) {
				            if (item) {
				                if (previousPoint != item.dataIndex) {
				                    previousPoint = item.dataIndex;

				                    $("#tooltip").remove();
				                    var x = item.datapoint[0],
				                        y = item.datapoint[1];

			// Set a suffix for the day number:
			var day_suffix = 'th';
			if (x < 10 | x > 20)
			{
				switch (x%10)
				{
					case 1:
						day_suffix = 'st';
						break;
					case 2:
						day_suffix = 'nd';
						break;
					case 3:
						day_suffix = 'rd';
						break;
					default:
						break;
				}
			}
		showTooltip(item.pageX, item.pageY, item.series.textval, item.series.label, x, y, day_suffix);
				                }
				            }
				            else {
				                $("#tooltip").remove();
				                previousPoint = null;
				            }
				        }
				   	});

				    $(".chart").bind("plotclick", function (event, pos, item) {
				        if (item) {
				            $("#clickdata").text("You clicked point " + item.dataIndex + " in " + item.series.label + ".");
				            plot.highlight(item.series, item.datapoint);
				        }
				    });
				    /*var xaxisLabel = $("<div class='axisLabel xaxisLabel'></div>")
					  .text("Date of <?php echo $chrt_yr; ?>")
					  .appendTo($('.chart'));

					var yaxisLabel = $("<div class='axisLabel yaxisLabel'></div>")
					  .text("Die Quantity")
					  .appendTo($('.chart'));*/

					var xaxisLabel = $("<div class='axisLabel yaxisLabel'></div>")
					  .text("Die Quantity")
					  .appendTo($('.chart'));

					var yaxisLabel = $("<div class='axisLabel xaxisLabel'></div>")
					  .text("Date of <?php echo $chrt_yr; ?>")
					  .appendTo($('.chart'));


				});

				$(function () {
				   	var array_bill = new Array();
		    		var array_coll = new Array();

		    		var arrayBill = '<?php echo $billArrayJSON ?>';
		    		var arrayColl = '<?php echo $collArrayJSON ?>';
		    		
		    		array_bill = $.parseJSON(arrayBill);
		    		array_coll = $.parseJSON(arrayColl);

		    		var acc_bill = [], acc_coll = [];
				    for (var i = 0; i <= day_cnt; i++) {
				        acc_bill.push([i,array_bill[i]['accountsCount']]);
				    }
				    for (var i = 0; i <= day_cnt; i++) {
				        acc_coll.push([i,array_coll[i]['accountsCount']]);
				    }

				    var bill_rslt = "Rs. "+array_bill[day_cnt]['accountsCount'].toFixed(2);
		    		var coll_rslt = "Rs. "+array_coll[day_cnt]['accountsCount'].toFixed(2);

				    var plot = $.plot($(".acc_chart"),
				           [ { data: acc_bill, textval: "Billing", label: "Billing - "+bill_rslt}, { data: acc_coll, textval: "Collection", label: "Collection - "+coll_rslt } ], {
				               series: {
				                   lines: { show: true },
				                   points: { show: true }
				               },
				               grid: { hoverable: true, clickable: true },
				               yaxis: { min: 0, max: '<?php echo $RSLT_MAX; ?>' },
							   xaxis: { min: 0, max: '<?php echo $tot_day; ?>' }
				             });

				   // function showTooltip(x, y, contents, ids, lbl) {
				   function showTooltip(pageX, pageY, textval, label, x, y, day_suffix) {
				    	if(textval=='Billing')
				    	{
				    		var totDes	= "Total Rs. "+y+" till "+ x + day_suffix + " <?php echo $chrt_lbl; ?><hr>";
							var det		= textval + " on " + x + day_suffix + " <?php echo $chrt_lbl; ?> = Rs. "+ array_bill[x]['accountsAmount'] +" <br>"+array_bill[x]['accountsDetails'];
							//var det = splt3[ids];
						}
						else if(textval=='Collection')
						{
							var totDes	= "Total Rs. "+y+" till "+ x + day_suffix + " <?php echo $chrt_lbl; ?><hr>";
							var det		= textval + " on " + x + day_suffix + " <?php echo $chrt_lbl; ?> = Rs. "+ array_coll[x]['accountsAmount'] +" <br>"+array_coll[x]['accountsDetails'];
							//var det = splt4[ids];
						}
				        $('<div id="tooltip" class="tooltip">' + totDes + det + '</div>').css( {
				            position: 'absolute',
				            display: 'none',
				            top: pageY + 5,
				            left: pageX + 5,
							'z-index': '9999',
							'color': '#fff',
							'font-size': '11px',
				            opacity: 0.8
				        }).appendTo("body").fadeIn(200);
				    }

				    var previousPoint = null;
				    $(".acc_chart").bind("plothover", function (event, pos, item) {
				        $("#x").text(pos.x);
				        $("#y").text(pos.y.toFixed(2));

				        if ($(".acc_chart").length > 0) {
				            if (item) {
				                if (previousPoint != item.dataIndex) {
				                    previousPoint = item.dataIndex;
				                    
				                    $("#tooltip").remove();
				                    var x = item.datapoint[0],
				                        y = item.datapoint[1].toFixed(2);
				                        
										// Set a suffix for the day number:
										var day_suffix = 'th';
										if (x < 10 | x > 20)
										{
											switch (x%10)
											{
												case 1:
													day_suffix = 'st';
													break;
												case 2:
													day_suffix = 'nd';
													break;
												case 3:
													day_suffix = 'rd';
													break;
												default:
													break;
											}
										}
				                    //showTooltip(item.pageX, item.pageY, item.series.label + " on " + x + day_suffix + " <?php echo $chrt_lbl; ?> = Rs. " + y, x, item.series.label);
				                  	showTooltip(item.pageX, item.pageY, item.series.textval,  item.series.label, x, y, day_suffix);
				                }
				            }
				            else {
				                $("#tooltip").remove();
				                previousPoint = null;
				            }
				        }
				    });

				    $(".acc_chart").bind("plotclick", function (event, pos, item) {
				        if (item) {
				            $("#clickdata").text("You clicked point " + item.dataIndex + " in " + item.series.label + ".");
				            plot.highlight(item.series, item.datapoint);
				        }
				    });

				    var xaxisLabel = $("<div class='axisLabel xaxisLabel'></div>")
					  .text("Date of <?php echo $chrt_yr; ?>")
					  .appendTo($('.acc_chart'));

					var yaxisLabel = $("<div class='axisLabel yaxisLabel'></div>")
					  .text("Amount in INR")
					  .appendTo($('.acc_chart'));
				});
				function backup(){
				      var txt;
                      if (confirm("Will you download the backup now?")) {
                            window.location.href='export.php';
                      } else {

                      }


				   // window.location.href='test1.php';
				}

		    </script>
	   	<?php
	   	}
	   	?> 
	    <!-- Main content ends -->
		<!-- Content ends -->
	</body>
</html>