<? if($_SESSION['page'] == '2' and $_SESSION['section'] == '1') { ?>
<link href="../admin.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
			window.addEvent('domready', function() { 
				new vlaDatePicker('From', { style: 'adobe_cs3',separator: '-', format: 'y/m/d'});
				new vlaDatePicker('To', { style: 'adobe_cs3',separator: '-', format: 'y/m/d'});
			});	
		</script>
        
        
        
<script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
    <table width="95%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#F7D782">
    <tr>
      <td>
        <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#EFB412">
          <tr>
            <td class="Main_Text">Sales transactions</td>
            <td width="10%" class="Main_Text"><div align="right"><img src="image/more_info.gif" width="32" height="32" border="0" /></a></div></td>
          </tr>
          <tr>
            <td colspan="2">
              <? if ($_GET['stat'] == '' ) {  ?>

              <form action="index.php?stat=show" method="post" name="form1" id="form1">
                <br />
                <table width="95%" border="0" align="center">
                <tr>
                  <td width="15%" class="Main_Text1_case">Date From</td>
                  <td class="text_row1"><div align="center"><span id="sprytextfield1">
                    <input name="From" type="text" id="From" />
                    <span class="textfieldInvalidFormatMsg">*</span>
                    <span class="textfieldRequiredMsg">*</span>
                  </span></div></td>
                  <td width="15%" class="Main_Text1_case">Date To</td>
                  <td class="text_row1"><div align="center"><span id="sprytextfield2">
                    <input type="text" name="To" id="To" />
                  <span class="textfieldRequiredMsg">*</span><span class="textfieldInvalidFormatMsg">*</span></span></div></td>
                  <td width="150" rowspan="3"><div align="center">
                    <input name="Submit" type="submit" class="Button1" id="Submit" value="   Create   " />
                  </div></td>
                </tr>
                <tr>
                  <td class="Main_Text1_case">Time From</td>
                  <td class="text_row1"><div align="center"><span id="sprytextfield3">
                    <input name="time_from" type="text" id="time_from" value="00:00" />
                  <span class="textfieldRequiredMsg">*</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></div></td>
                  <td class="Main_Text1_case">Time To</td>
                  <td class="text_row1"><div align="center"><span id="sprytextfield4">
                    <input name="time_to" type="text" id="time_to" value="23:59" />
                  <span class="textfieldRequiredMsg">*</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></div></td>
                  </tr>
                <tr>
                  <td class="Main_Text1_case">Detailed</td>
                  <td class="text_row1"><div align="center">
                    <input name="detailed" type="checkbox" id="detailed" value="1" />
                  </div></td>
                  <td class="Main_Text1_case">Chart</td>
                  <td class="text_row1"><input name="pchart" type="radio" id="radio" value="0" checked="checked" />
                    No chart&nbsp;&nbsp;
  <input type="radio" name="pchart" id="radio3" value="2" />
  &nbsp;Column Chart</td>
                </tr>
                <tr>
                  <td class="Main_Text1_case">Mode</td>
                  <td class="text_row1"><input name="mode_print" type="checkbox" id="mode_print" value="1" />
                    Print</td>
                  <td>&nbsp;</td>
                  <td>&nbsp;</td>
                  <td>&nbsp;</td>
                </tr>
              </table>
            
                <br />
              </form>
           
           
    <? } if ($_GET['stat'] == 'show' ) { 
	
				
					$plate_sum = 0;
					$result = mysql_query('SELECT *  FROM p1_plate_type',$db); 
					while($row = mysql_fetch_array($result)){ 
						$plate_sum = $plate_sum + 1;
						$plate_name[$plate_sum] = $row['plate_type'];
						
					
					}
	
								$From = $_POST["From"];
						  		$To = $_POST["To"];
						  		$time_from = $_POST["time_from"];
						  		$time_to = $_POST["time_to"];
			  
				  ?>
                                      <br />
                        <table width="60%" border="0" align="center">
                          <tr>
                            <td class="Main_Text1">Date</td>
                            <td class="text_row1"><div align="center">From </div></td>
                            <td class="text_box_textarea"><div align="center"><? echo $From;?></div></td>
                            <td class="text_row1"><div align="center">to</div></td>
                            <td class="text_box_textarea"><div align="center"><? echo $To;?></div></td>
                          </tr>
                          <tr>
                            <td class="Main_Text1">Time</td>
                            <td class="text_row1"><div align="center">From </div></td>
                            <td class="text_box_textarea"><div align="center"><? echo $time_from;?></div></td>
                            <td class="text_row1"> <div align="center">to</div></td>
                            <td class="text_box_textarea"><div align="center"><? echo $time_to;?></div></td>
                          </tr>
                        </table>
    <br />
              <?
		  
				 if ($_POST['detailed'] <> '') {
				 
				 // load the price of all plate type in array
					 $number_of_plate_type = 0 ;
					 $result1 = mysql_query('select * from p1_plate_type'); 
                        while($row1 = mysql_fetch_array($result1)){ 
								$plate_price[$row1['tyid']] = $row1['plate_price'];    
								$number_of_plate_type = $number_of_plate_type + 1;
					  }
				 // Select all Date 
					  
						$start_date = $_POST["From"];
						$check_date =date ("Y-m-d", strtotime ("-1 day", strtotime($start_date))); 
						$end_date = $_POST["To"];
						
										
						$i = 0; 
				// ************* Total all row in the end total		 ********************	
							$result = mysql_query("select * from p1_powers order by fullname");
							while($row = mysql_fetch_array($result)){ 
									$user_scr_end_total[$row['uid']] = 0;
									$user_fib_end_total[$row['uid']] = 0;
									$user_fra_end_total[$row['uid']] = 0;
							}
									$user_scr_end_total_all = 0;
									$user_fib_end_total_all = 0;
									$user_fra_end_total_all = 0;
				
				
				
				// ************* End Total all row in the end total	 ********************
				
				
		$result = mysql_query("select * from p1_powers order by fullname");
		while($row = mysql_fetch_array($result)){ 
					// ****** 	 calc plate for table 1   *******
										//  make all 0									
											for ($i=1;$i<=$number_of_plate_type;$i++){
												$end_plate_count_total[$i][$row['uid']] = 0; 
												
											}
											$end_plate_count_agent_total[$row['uid']]= 0; 
											$end_plate_count_vip_total[$row['uid']]= 0; 
											
											
										$end_bd1_count_total[$row['uid']] 	= 0; 
					// ****** 	 End calc plate for table 1    *******
					
			}		
					
				
				while ($check_date != $end_date) { 
				  $check_date = date ("Y-m-d", strtotime ("+1 day", strtotime($check_date))); 
				  $first_row = 0;
								
                        
						for ($i=1;$i<=$number_of_plate_type;$i++){
							$plate_count_all[$i] = 0; 
						}
						for ($i=1;$i<=$number_of_plate_type;$i++){
							$plate_count_all_vip[$i] = 0; 
						}
						for ($i=1;$i<=$number_of_plate_type;$i++){
							$plate_count_all_agent[$i] = 0; 
						}

					// Total all row	
						$row_total_all_plate = 0;
						$row_total_all_plate_vip = 0;
						$row_total_all_plate_agent = 0;
						$bd1_all_count = 0;
						$row_total_all_price=0;
						
					// ******    calc other Screws , fiber , frame  *******
						
					 	// Load all user as 0
							$result = mysql_query("select * from p1_powers order by fullname");
							while($row = mysql_fetch_array($result)){ 
									$user_scr[$row['uid']] = 0;
									$user_fib[$row['uid']] = 0;
									$user_fra[$row['uid']] = 0;
							}
									$user_scr_total = 0;
									$user_fib_total = 0;
									$user_fra_total = 0;

						// Load the data
							$result = mysql_query("select uid , sum(screws) screws , sum(fiber) fiber , sum(bs+bl+ss+sl+ws+wl) frame from p1_sale where create_date = '".$check_date."' and create_time between '".$time_from."' and '".$time_to."' group by uid");
							while($row = mysql_fetch_array($result)){ 
									$user_scr[$row['uid']] = $row['screws'];
									$user_fib[$row['uid']] = $row['fiber'];
									$user_fra[$row['uid']] = $row['frame'];
									$user_scr_total = $user_scr_total+$row['screws'];
									$user_fib_total = $user_fib_total+$row['fiber'];
									$user_fra_total = $user_fra_total+$row['frame'];
									// calc for end table
									$user_scr_end_total[$row['uid']] = $user_scr_end_total[$row['uid']]+$row['screws'];
									$user_fib_end_total[$row['uid']] = $user_fib_end_total[$row['uid']]+$row['fiber'];
									$user_fra_end_total[$row['uid']] = $user_fra_end_total[$row['uid']]+$row['frame'];
									$user_scr_end_total_all = $user_scr_end_total_all+$row['screws'];
									$user_fib_end_total_all = $user_fib_end_total_all+$row['fiber'];
									$user_fra_end_total_all = $user_fra_end_total_all+$row['frame'];
								
									
									
							}
					// ******    End calc other Screws , fiber , frame  *******

					
					
						
								
					


					// loop for user 	
						$result = mysql_query("select * from p1_powers order by fullname");
						$tic_num = mysql_num_rows($result); 
						while($row = mysql_fetch_array($result)){ 
						
								
								
							//  make all 0
								for ($i=1;$i<=$number_of_plate_type;$i++){
									$plate_count[$i] = 0; 
								}
								for ($i=1;$i<=$number_of_plate_type;$i++){
									$plate_count_agent[$i]= 0; 
								}
								for ($i=1;$i<=$number_of_plate_type;$i++){
									$plate_count_vip[$i]= 0; 
								}

							// load 1 BD
							$result_loop = mysql_query("select  sum(frame) frame , sum(Fiber) Fiber, sum(BD1) BD1 from p1_order_info where uid = ".$row['uid']." and add_dt between '".$check_date." ".$time_from."' and '".$check_date." ".$time_to."'  and stop1 = 0 ");
$bd1_count = 0;
								while($row_loop = mysql_fetch_array($result_loop)){ 
								
				
							
									$bd1_count 			 =  $bd1_count			 + $row_loop['BD1'];
									$bd1_all_count		 =  $bd1_all_count		 + $row_loop['BD1'];
									
								//
							
									$end_bd1_count_total[$row['uid']] = $end_bd1_count_total[$row['uid']] + $row_loop['BD1'];
									
									
									
								}

							// load the data 
							
							$result_loop = mysql_query("select type1 , count(*) number_of_car ,sum(number_of_plate) number_of_plate ,  sum(frame) frame , sum(Fiber) Fiber, sum(BD1) BD1 from p1_order_info where uid = ".$row['uid']." and add_dt between '".$check_date." ".$time_from."' and '".$check_date." ".$time_to."' and vid = 0 and aid = 0 and stop1 = 0 group by type1");
							
								$row_total_plate = 0;
								
								while($row_loop = mysql_fetch_array($result_loop)){ 
								
								// For the first table 
									$plate_count[$row_loop['type1']]	 =  $row_loop['number_of_plate'];
									$plate_count_all[$row_loop['type1']] =  $plate_count_all[$row_loop['type1']] +  $row_loop['number_of_plate'];
								// For the total table
									$end_plate_count_total[$row_loop['type1']][$row['uid']]  = $end_plate_count_total[$row_loop['type1']][$row['uid']] + $row_loop['number_of_plate'];
									
								// For the first table 
									$row_total_plate 	 =  $row_total_plate 	 + $row_loop['number_of_plate'];
									$row_total_all_plate =  $row_total_all_plate + $row_loop['number_of_plate'];
								
									
									
								}
								
								
								
								
							// load the data  for Agent
								$result_loop = mysql_query("select type1 , count(*) number_of_car ,sum(number_of_plate) number_of_plate ,  sum(frame) frame , sum(Fiber) Fiber, sum(BD1) BD1 from p1_order_info where uid = ".$row['uid']." and add_dt between '".$check_date." ".$time_from."' and '".$check_date." ".$time_to."' and aid <> 0  and stop1 = 0 group by type1");

							
								$row_total_plate_agent = 0;
								while($row_loop = mysql_fetch_array($result_loop)){ 
									$plate_count_agent[$row_loop['type1']] =  $row_loop['number_of_plate'];
									$plate_count_all_agent[$row_loop['type1']] = $plate_count_all_agent[$row_loop['type1']] +  $row_loop['number_of_plate'];
									$row_total_plate_agent = $row_total_plate_agent + $row_loop['number_of_plate'];
								//	$row_total_all_plate_agent =  $row_total_all_plate_agent + $row_loop['number_of_plate'];
								
								
								}

						
								
							// load the data  for VIP
								$result_loop = mysql_query("select type1 , count(*) number_of_car ,sum(number_of_plate) number_of_plate ,  sum(frame) frame , sum(Fiber) Fiber, sum(BD1) BD1 from p1_order_info where uid = ".$row['uid']." and add_dt between '".$check_date." ".$time_from."' and '".$check_date." ".$time_to."' and vid <> 0 and stop1 = 0  group by type1");

							
								$row_total_plate_vip = 0;
								while($row_loop = mysql_fetch_array($result_loop)){ 
									$plate_count_vip[$row_loop['type1']] =  $row_loop['number_of_plate'];
									$plate_count_all_vip[$row_loop['type1']] = $plate_count_all_vip[$row_loop['type1']] +  $row_loop['number_of_plate'];
									$row_total_plate_vip = $row_total_plate_vip + $row_loop['number_of_plate'];
							
								//	$row_total_all_plate_vip =  $row_total_all_plate_vip + $row_loop['number_of_plate'];
								}

					




							$test_show = $row_total_plate+$row_total_plate_vip+$row_total_plate_agent+$user_scr[$row['uid']]+$user_fib[$row['uid']]+$user_fra[$row['uid']];
							
						if ($test_show <> 0 ) {
								
							// Total price for all row
								$row_total_price = 0;
								for ($i=1;$i<=$number_of_plate_type;$i++){
									$row_total_price = $row_total_price+$plate_count[$i]*$plate_price[$i];
								}
							// Total price for agent
								$row_total_price_agent = 0;
								for ($i=1;$i<=$number_of_plate_type;$i++){
									$row_total_price_agent = $row_total_price_agent+$plate_count_agent[$i]*$plate_price[$i];
								}
							// Total price for VIP
								$row_total_price_vip = 0;
								for ($i=1;$i<=$number_of_plate_type;$i++){
									$row_total_price_vip = $row_total_price_vip+$plate_count_vip[$i]*$plate_price[$i];
								}


							$row_total_all_price=$row_total_all_price+$row_total_price;
							$row_total_all_plate_agent=$row_total_all_plate_agent+$row_total_price_agent;
							$row_total_all_plate_vip=$row_total_all_plate_vip+$row_total_price_vip;

							
							
								if ($first_row == 0 ) { 
						?>
                      
                                <br />
                                	<div align="center">
                                			<table width="97%" border="0">
                                              <tr class="Main_Text1_case">
                                                <td rowspan="2">Date</td>
                                                <td rowspan="2">Counter sales<br />person</td>
                                                <td colspan="<? echo $plate_sum?>" bordercolor="#000000" class="Main_Text1_case"><div align="center">Plate Color code</div></td>
                                                <td rowspan="2">plates Total</td>
                                                <td rowspan="2">Screws</td>
                                                <td rowspan="2">Fiber</td>
                                                <td rowspan="2">Frames</td>
                                                <td rowspan="2">Agents</td>
                                                <td rowspan="2">VIP</td>
                                                <td rowspan="2">Total (BD)</td>
                                              </tr>
                                              
                                              <tr>
												  <? for ($loop=1;$loop<=$plate_sum;$loop++)  	{	?>

                                                    <td bordercolor="#000000" class="Main_Text1_case"><div align="center"><? echo $plate_name[$loop];?></div></td>
                                                  <? } ?>
                                              </tr>
				  		<?
                        	}	
							
							?>
                        
                        
                        
                              <tr>
                                <? if ($first_row == 0 ) { ?>  <td rowspan="<? echo $tic_num+1?>" class="Main_SText_case"><div align="center"><? echo $check_date ?></div></td> <? } ?>
                                                <td class="Main_SText_case"><div align="center"><? echo $row['fullname'];?></div></td>
                                                <? for ($loop=1;$loop<=$plate_sum;$loop++)  	{	?>
                                                <td class="Main_SText_case"><div align="center"><? echo $plate_count[$loop]*$plate_price[$loop];?></div></td>
                                                 <? } ?>

                                                <td class="Main_SText_case"><div align="center"><? echo $row_total_price?></div></td>
                                                <td class="Main_SText_case"><div align="center"><? echo $bd1_count+$user_scr[$row['uid']]?></div></td>
                                                <td class="Main_SText_case"><div align="center"><? echo $user_fib[$row['uid']];?></div></td>
                                                <td class="Main_SText_case"><div align="center"><? echo $user_fra[$row['uid']]*5;?></div></td>
                                                <td class="Main_SText_case"><div align="center"><? echo $row_total_price_agent?></div></td>
                                                <td class="Main_SText_case"><div align="center"><? echo $row_total_price_vip?></div></td>
                                                <td class="Main_SText_case"><div align="center"><? echo $row_total_price+$bd1_count+$row_total_price_agent+$row_total_price_vip+$user_scr[$row['uid']]+$user_fib[$row['uid']]+$user_fra[$row['uid']]*5?></div></td>
                                              </tr>
                       <?
					   
					  						$end_plate_count_agent_total[$row['uid']]= $end_plate_count_agent_total[$row['uid']]+$row_total_price_agent; 
											$end_plate_count_vip_total[$row['uid']]= $end_plate_count_vip_total[$row['uid']]+$row_total_price_vip; 
					   
					   			$first_row = 1;
					   		}
					   		
					    } 
						
						if ($first_row == 1 ) { 
						?>          
                        
                             
                                              <tr>
                                                <td class="Main_color2"><div align="center">Total</div></td>
                                                <? for ($loop=1;$loop<=$plate_sum;$loop++)  	{	?>
                                                <td class="Main_color2"><div align="center"><? echo $plate_count_all[$loop]*$plate_price[$loop];?></div></td>
                                                 <? } ?>
                                                <td class="Main_color2"><div align="center"><? echo $row_total_all_price?></div></td>
                                                <td class="Main_color2"><div align="center"><? echo $bd1_all_count+$user_scr_total;?></div></td>
                                                <td class="Main_color2"><div align="center"><? echo $user_fib_total;?></div></td>
                                                <td class="Main_color2"><div align="center"><? echo $user_fra_total*5?></div></td>
                                                <td class="Main_color2"><div align="center"><? echo $row_total_all_plate_agent?></div></td>
                                                <td class="Main_color2"><div align="center"><? echo $row_total_all_plate_vip?></div></td>
                                                <td class="Main_color2"><div align="center"><? echo $row_total_all_price+$bd1_all_count+$user_scr_total+$user_fib_total+$user_fra_total*5+$row_total_all_plate_agent+$row_total_all_plate_vip?></div></td>
                                              </tr>
                                            </table>                                            
                                			<br />
                                <?
								}
								
								
							}
             		?>
                      <p>&nbsp;</p>
                           			        <table width="97%" border="0">
                                              <tr class="Main_Text1_case">
                                                <td rowspan="2">Counter sales <br />
                                                person</td>
                                                <td colspan="<? echo $plate_sum?>" bordercolor="#000000" class="Main_Text1_case"><div align="center">Plate Color code</div></td>
                                                <td rowspan="2">plates Total</td>
                                                <td rowspan="2">Screws</td>
                                                <td rowspan="2">Fiber</td>
                                                <td rowspan="2">Frames</td>
                                                <td rowspan="2">Agents</td>
                                                <td rowspan="2">VIP</td>
                                                <td rowspan="2">Total (BD)</td>
                                              </tr>
                                              <tr>
									 		   <? for ($loop=1;$loop<=$plate_sum;$loop++)  	{	?>
                                                <td bordercolor="#000000" class="Main_Text1_case"><div align="center"><? echo $plate_name[$loop];?></div></td>
                                               <? } ?>
                                              </tr>
                                    <? 
									
										$result = mysql_query("select * from p1_powers order by fullname");
										while($row = mysql_fetch_array($result)){ 
											$showrow1 =0;
											$showrow =0;
											for ($loop=1;$loop<=$plate_sum;$loop++)  	{
												$showrow1 = $showrow1 + $end_plate_count_total[$loop][$row['uid']]*$plate_price[$loop];
												
											}
											
										
												
												$showrow = $showrow1 + $user_scr_end_total[$row['uid']]+$user_fib_end_total[$row['uid']]+$user_fra_end_total[$row['uid']];
												
												if ($showrow <> 0 ) {
									?>    
                                       <tr>
                                         <td class="Main_SText_case"><div align="center"><? echo $row['fullname'];?></div></td>
                                         <? for ($loop=1;$loop<=$plate_sum;$loop++)  	{	?>
                                         <td class="Main_SText_case"><div align="center"><? echo $end_plate_count_total[$loop][$row['uid']]*$plate_price[$loop];?></div></td>
                                         <? } ?>

                                         <td class="Main_SText_case"><div align="center"><? echo $showrow1;?></div></td>
                                         <td class="Main_SText_case"><div align="center"><? echo $end_bd1_count_total[$row['uid']]+$user_scr_end_total[$row['uid']];?></div></td>
                                         <td class="Main_SText_case"><div align="center"><? echo $user_fib_end_total[$row['uid']];?></div></td>
                                         <td class="Main_SText_case"><div align="center"><? echo $user_fra_end_total[$row['uid']]*5;?></div></td>
                                         <td class="Main_SText_case"><div align="center"><? echo $end_plate_count_agent_total[$row['uid']]?></div></td>
                                         <td class="Main_SText_case"><div align="center"><? echo $end_plate_count_vip_total[$row['uid']]?></div></td>
                                         <td class="Main_SText_case"><div align="center"><? echo $showrow1+$end_bd1_count_total[$row['uid']]+$user_scr_end_total[$row['uid']]+$user_fib_end_total[$row['uid']]+$user_fra_end_total[$row['uid']]*5+$end_plate_count_agent_total[$row['uid']]+$end_plate_count_vip_total[$row['uid']];?></div></td>
                                      </tr>
                                   <?
								   
								   
											for ($i=1;$i<=$number_of_plate_type;$i++){
										  	$total_of_end_plate_count_total[$i] = $total_of_end_plate_count_total[$i]+$end_plate_count_total[$i][$row['uid']]*$plate_price[$i];
											}
											$total_of_end_bd1_count_agent_total=$total_of_end_bd1_count_agent_total+$end_bd1_count_total[$row['uid']];
											$total_of_end_plate_count_agent_total= $total_of_end_plate_count_agent_total+$end_plate_count_agent_total[$row['uid']];
											$total_of_end_plate_count_vip_total= $total_of_end_plate_count_vip_total+$end_plate_count_vip_total[$row['uid']];
											
										}
										
										
								   		
								   
								    } ?>
                                   

                                              <tr>
                                                <td class="Main_color2"><div align="center">Total</div></td>
                                                 <? 
												 $total_of_end_plate_count_totals = 0;
												 for ($loop=1;$loop<=$plate_sum;$loop++)  	{	?>
                                                <td class="Main_color2"><div align="center"><? echo $total_of_end_plate_count_total[$loop];?></div></td>
                                                <? 
												$total_of_end_plate_count_totals = $total_of_end_plate_count_totals +$total_of_end_plate_count_total[$loop];
												
												} ?>
                                                <td class="Main_color2"><div align="center"><? echo $total_of_end_plate_count_totals;?></div></td>
                                                <td class="Main_color2"><div align="center"><? echo $total_of_end_bd1_count_agent_total+$user_scr_end_total_all;?></div></td>
                                                <td class="Main_color2"><div align="center"><? echo $user_fib_end_total_all;?></div></td>
                                                <td class="Main_color2"><div align="center"><? echo $user_fra_end_total_all*5;?></div></td>
                                                <td class="Main_color2"><div align="center"><? echo $total_of_end_plate_count_agent_total;?></div></td>
                                                <td class="Main_color2"><div align="center"><? echo $total_of_end_plate_count_vip_total;?></div></td>
                                                <td class="Main_color2"><div align="center"><? echo $total_of_end_plate_count_totals+$total_of_end_bd1_count_agent_total+$user_scr_end_total_all+$user_fib_end_total_all+$user_fra_end_total_all*5+$total_of_end_plate_count_agent_total+$total_of_end_plate_count_vip_total;?></div></td>
                                              </tr>
                                            </table>
                   			          <p>&nbsp;</p>
              </div>
                    <?
					/*---------------   else  -------------- */
				 }else {
				 
				 // load the price of all plate type in array
					 $number_of_plate_type = 0 ;
					 $result1 = mysql_query('select * from p1_plate_type'); 
                        while($row1 = mysql_fetch_array($result1)){ 
								$plate_price[$row1['tyid']] = $row1['plate_price'];    
								$number_of_plate_type = $number_of_plate_type + 1;
					  }
				 // Select all Date 
					  
						$start_date = $_POST["From"];
						$check_date =date ("Y-m-d", strtotime ("-1 day", strtotime($start_date))); 
						$end_date = $_POST["To"];
						
										
						$i = 0; 
				// ************* Total all row in the end total		 ********************	
							$result = mysql_query("select * from p1_powers order by fullname");
							while($row = mysql_fetch_array($result)){ 
									$user_scr_end_total[$row['uid']] = 0;
									$user_fib_end_total[$row['uid']] = 0;
									$user_fra_end_total[$row['uid']] = 0;
							}
									$user_scr_end_total_all = 0;
									$user_fib_end_total_all = 0;
									$user_fra_end_total_all = 0;
				
				
				
				// ************* End Total all row in the end total	 ********************
				
				
		$result = mysql_query("select * from p1_powers order by fullname");
		while($row = mysql_fetch_array($result)){ 
					// ****** 	 calc plate for table 1   *******
										//  make all 0									
											for ($i=1;$i<=$number_of_plate_type;$i++){
												$end_plate_count_total[$i][$row['uid']] = 0; 
												
											}
											$end_plate_count_agent_total[$row['uid']]= 0; 
											$end_plate_count_vip_total[$row['uid']]= 0; 
											
											
										$end_bd1_count_total[$row['uid']] 	= 0; 
					// ****** 	 End calc plate for table 1    *******
					
			}		
					
				
				while ($check_date != $end_date) { 
				  $check_date = date ("Y-m-d", strtotime ("+1 day", strtotime($check_date))); 
				  $first_row = 0;
								
                        
						for ($i=1;$i<=$number_of_plate_type;$i++){
							$plate_count_all[$i] = 0; 
						}
						for ($i=1;$i<=$number_of_plate_type;$i++){
							$plate_count_all_vip[$i] = 0; 
						}
						for ($i=1;$i<=$number_of_plate_type;$i++){
							$plate_count_all_agent[$i] = 0; 
						}

					// Total all row	
						$row_total_all_plate = 0;
						$row_total_all_plate_vip = 0;
						$row_total_all_plate_agent = 0;
						$bd1_all_count = 0;
						$row_total_all_price=0;
						
					// ******    calc other Screws , fiber , frame  *******
						
					 	// Load all user as 0
							$result = mysql_query("select * from p1_powers order by fullname");
							while($row = mysql_fetch_array($result)){ 
									$user_scr[$row['uid']] = 0;
									$user_fib[$row['uid']] = 0;
									$user_fra[$row['uid']] = 0;
							}
									$user_scr_total = 0;
									$user_fib_total = 0;
									$user_fra_total = 0;

						// Load the data
							$result = mysql_query("select uid , sum(screws) screws , sum(fiber) fiber , sum(bs+bl+ss+sl+ws+wl) frame from p1_sale where create_date = '".$check_date."' and create_time between '".$time_from."' and '".$time_to."' group by uid");
							while($row = mysql_fetch_array($result)){ 
									$user_scr[$row['uid']] = $row['screws'];
									$user_fib[$row['uid']] = $row['fiber'];
									$user_fra[$row['uid']] = $row['frame'];
									$user_scr_total = $user_scr_total+$row['screws'];
									$user_fib_total = $user_fib_total+$row['fiber'];
									$user_fra_total = $user_fra_total+$row['frame'];
									// calc for end table
									$user_scr_end_total[$row['uid']] = $user_scr_end_total[$row['uid']]+$row['screws'];
									$user_fib_end_total[$row['uid']] = $user_fib_end_total[$row['uid']]+$row['fiber'];
									$user_fra_end_total[$row['uid']] = $user_fra_end_total[$row['uid']]+$row['frame'];
									$user_scr_end_total_all = $user_scr_end_total_all+$row['screws'];
									$user_fib_end_total_all = $user_fib_end_total_all+$row['fiber'];
									$user_fra_end_total_all = $user_fra_end_total_all+$row['frame'];
								
									
									
							}
					// ******    End calc other Screws , fiber , frame  *******

					
					
						
								
					


					// loop for user 	
						$result = mysql_query("select * from p1_powers order by fullname");
						$tic_num = mysql_num_rows($result); 
						while($row = mysql_fetch_array($result)){ 
						
								
								
							//  make all 0
								for ($i=1;$i<=$number_of_plate_type;$i++){
									$plate_count[$i] = 0; 
								}
								for ($i=1;$i<=$number_of_plate_type;$i++){
									$plate_count_agent[$i]= 0; 
								}
								for ($i=1;$i<=$number_of_plate_type;$i++){
									$plate_count_vip[$i]= 0; 
								}

							// load 1 BD
							$result_loop = mysql_query("select  sum(frame) frame , sum(Fiber) Fiber, sum(BD1) BD1 from p1_order_info where uid = ".$row['uid']." and add_dt between '".$check_date." ".$time_from."' and '".$check_date." ".$time_to."'  and stop1 = 0 ");
$bd1_count = 0;
								while($row_loop = mysql_fetch_array($result_loop)){ 
								
				
							
									$bd1_count 			 =  $bd1_count			 + $row_loop['BD1'];
									$bd1_all_count		 =  $bd1_all_count		 + $row_loop['BD1'];
									
								//
							
									$end_bd1_count_total[$row['uid']] = $end_bd1_count_total[$row['uid']] + $row_loop['BD1'];
									
									
									
								}

							// load the data 
							
							$result_loop = mysql_query("select type1 , count(*) number_of_car ,sum(number_of_plate) number_of_plate ,  sum(frame) frame , sum(Fiber) Fiber, sum(BD1) BD1 from p1_order_info where uid = ".$row['uid']." and add_dt between '".$check_date." ".$time_from."' and '".$check_date." ".$time_to."' and vid = 0 and aid = 0 and stop1 = 0 group by type1");
							
								$row_total_plate = 0;
								
								while($row_loop = mysql_fetch_array($result_loop)){ 
								
								// For the first table 
									$plate_count[$row_loop['type1']]	 =  $row_loop['number_of_plate'];
									$plate_count_all[$row_loop['type1']] =  $plate_count_all[$row_loop['type1']] +  $row_loop['number_of_plate'];
								// For the total table
									$end_plate_count_total[$row_loop['type1']][$row['uid']]  = $end_plate_count_total[$row_loop['type1']][$row['uid']] + $row_loop['number_of_plate'];
									
								// For the first table 
									$row_total_plate 	 =  $row_total_plate 	 + $row_loop['number_of_plate'];
									$row_total_all_plate =  $row_total_all_plate + $row_loop['number_of_plate'];
								
									
									
								}
								
								
								
								
							// load the data  for Agent
								$result_loop = mysql_query("select type1 , count(*) number_of_car ,sum(number_of_plate) number_of_plate ,  sum(frame) frame , sum(Fiber) Fiber, sum(BD1) BD1 from p1_order_info where uid = ".$row['uid']." and add_dt between '".$check_date." ".$time_from."' and '".$check_date." ".$time_to."' and aid <> 0  and stop1 = 0 group by type1");

							
								$row_total_plate_agent = 0;
								while($row_loop = mysql_fetch_array($result_loop)){ 
									$plate_count_agent[$row_loop['type1']] =  $row_loop['number_of_plate'];
									$plate_count_all_agent[$row_loop['type1']] = $plate_count_all_agent[$row_loop['type1']] +  $row_loop['number_of_plate'];
									$row_total_plate_agent = $row_total_plate_agent + $row_loop['number_of_plate'];
								//	$row_total_all_plate_agent =  $row_total_all_plate_agent + $row_loop['number_of_plate'];
								
								
								}

						
								
							// load the data  for VIP
								$result_loop = mysql_query("select type1 , count(*) number_of_car ,sum(number_of_plate) number_of_plate ,  sum(frame) frame , sum(Fiber) Fiber, sum(BD1) BD1 from p1_order_info where uid = ".$row['uid']." and add_dt between '".$check_date." ".$time_from."' and '".$check_date." ".$time_to."' and vid <> 0 and stop1 = 0  group by type1");

							
								$row_total_plate_vip = 0;
								while($row_loop = mysql_fetch_array($result_loop)){ 
									$plate_count_vip[$row_loop['type1']] =  $row_loop['number_of_plate'];
									$plate_count_all_vip[$row_loop['type1']] = $plate_count_all_vip[$row_loop['type1']] +  $row_loop['number_of_plate'];
									$row_total_plate_vip = $row_total_plate_vip + $row_loop['number_of_plate'];
							
								//	$row_total_all_plate_vip =  $row_total_all_plate_vip + $row_loop['number_of_plate'];
								}

					




							$test_show = $row_total_plate+$row_total_plate_vip+$row_total_plate_agent+$user_scr[$row['uid']]+$user_fib[$row['uid']]+$user_fra[$row['uid']];
							
						if ($test_show <> 0 ) {
								
							// Total price for all row
								$row_total_price = 0;
								for ($i=1;$i<=$number_of_plate_type;$i++){
									$row_total_price = $row_total_price+$plate_count[$i]*$plate_price[$i];
								}
							// Total price for agent
								$row_total_price_agent = 0;
								for ($i=1;$i<=$number_of_plate_type;$i++){
									$row_total_price_agent = $row_total_price_agent+$plate_count_agent[$i]*$plate_price[$i];
								}
							// Total price for VIP
								$row_total_price_vip = 0;
								for ($i=1;$i<=$number_of_plate_type;$i++){
									$row_total_price_vip = $row_total_price_vip+$plate_count_vip[$i]*$plate_price[$i];
								}


								$row_total_all_price=$row_total_all_price+$row_total_price;
								$row_total_all_plate_agent=$row_total_all_plate_agent+$row_total_price_agent;
								$row_total_all_plate_vip=$row_total_all_plate_vip+$row_total_price_vip;
								$end_plate_count_agent_total[$row['uid']]= $end_plate_count_agent_total[$row['uid']]+$row_total_price_agent; 
								$end_plate_count_vip_total[$row['uid']]= $end_plate_count_vip_total[$row['uid']]+$row_total_price_vip; 
					   			$first_row = 1;
					   		}
					   		
					    } 
						
						
							}
             		?>
                      <p>&nbsp;</p>
                           			        <table width="97%" border="0">
                                              <tr class="Main_Text1_case">
                                                <td rowspan="2">Counter sales <br />
                                                person</td>
                                                <td colspan="<? echo $plate_sum?>" bordercolor="#000000" class="Main_Text1_case"><div align="center">Plate Color code</div></td>
                                                <td rowspan="2">plates Total</td>
                                                <td rowspan="2">Screws</td>
                                                <td rowspan="2">Fiber</td>
                                                <td rowspan="2">Frames</td>
                                                <td rowspan="2">Agents</td>
                                                <td rowspan="2">VIP</td>
                                                <td rowspan="2">Total (BD)</td>
                                              </tr>
                                              <tr>
									 		   <? for ($loop=1;$loop<=$plate_sum;$loop++)  	{	?>
                                                <td bordercolor="#000000" class="Main_Text1_case"><div align="center"><? echo $plate_name[$loop];?></div></td>
                                               <? } ?>
                                              </tr>
                                    <? 
									
										$result = mysql_query("select * from p1_powers order by fullname");
										while($row = mysql_fetch_array($result)){ 
											$showrow1 =0;
											$showrow =0;
											for ($loop=1;$loop<=$plate_sum;$loop++)  	{
												$showrow1 = $showrow1 + $end_plate_count_total[$loop][$row['uid']]*$plate_price[$loop];
												
											}
											
										
												
												$showrow = $showrow1 + $user_scr_end_total[$row['uid']]+$user_fib_end_total[$row['uid']]+$user_fra_end_total[$row['uid']];
												
												if ($showrow <> 0 ) {
									?>    
                                       <tr>
                                         <td class="Main_SText_case"><div align="center"><? echo $row['fullname'];?></div></td>
                                         <? for ($loop=1;$loop<=$plate_sum;$loop++)  	{	?>
                                         <td class="Main_SText_case"><div align="center"><? echo $end_plate_count_total[$loop][$row['uid']]*$plate_price[$loop];?></div></td>
                                         <? } ?>

                                         <td class="Main_SText_case"><div align="center"><? echo $showrow1;?></div></td>
                                         <td class="Main_SText_case"><div align="center"><? echo $end_bd1_count_total[$row['uid']]+$user_scr_end_total[$row['uid']];?></div></td>
                                         <td class="Main_SText_case"><div align="center"><? echo $user_fib_end_total[$row['uid']];?></div></td>
                                         <td class="Main_SText_case"><div align="center"><? echo $user_fra_end_total[$row['uid']]*5;?></div></td>
                                         <td class="Main_SText_case"><div align="center"><? echo $end_plate_count_agent_total[$row['uid']]?></div></td>
                                         <td class="Main_SText_case"><div align="center"><? echo $end_plate_count_vip_total[$row['uid']]?></div></td>
                                         <td class="Main_SText_case"><div align="center"><? echo $showrow1+$end_bd1_count_total[$row['uid']]+$user_scr_end_total[$row['uid']]+$user_fib_end_total[$row['uid']]+$user_fra_end_total[$row['uid']]*5+$end_plate_count_agent_total[$row['uid']]+$end_plate_count_vip_total[$row['uid']];?></div></td>
                                      </tr>
                                   <?
								   
								   
											for ($i=1;$i<=$number_of_plate_type;$i++){
										  	$total_of_end_plate_count_total[$i] = $total_of_end_plate_count_total[$i]+$end_plate_count_total[$i][$row['uid']]*$plate_price[$i];
											}
											$total_of_end_bd1_count_agent_total=$total_of_end_bd1_count_agent_total+$end_bd1_count_total[$row['uid']];
											$total_of_end_plate_count_agent_total= $total_of_end_plate_count_agent_total+$end_plate_count_agent_total[$row['uid']];
											$total_of_end_plate_count_vip_total= $total_of_end_plate_count_vip_total+$end_plate_count_vip_total[$row['uid']];
											
										}
										
										
								   		
								   
								    } ?>
                                   

                                              <tr>
                                                <td class="Main_color2"><div align="center">Total</div></td>
                                                 <? 
												 $total_of_end_plate_count_totals = 0;
												 for ($loop=1;$loop<=$plate_sum;$loop++)  	{	?>
                                                <td class="Main_color2"><div align="center"><? echo $total_of_end_plate_count_total[$loop];?></div></td>
                                                <? 
												$total_of_end_plate_count_totals = $total_of_end_plate_count_totals +$total_of_end_plate_count_total[$loop];
												
												} ?>
                                                <td class="Main_color2"><div align="center"><? echo $total_of_end_plate_count_totals;?></div></td>
                                                <td class="Main_color2"><div align="center"><? echo $total_of_end_bd1_count_agent_total+$user_scr_end_total_all;?></div></td>
                                                <td class="Main_color2"><div align="center"><? echo $user_fib_end_total_all;?></div></td>
                                                <td class="Main_color2"><div align="center"><? echo $user_fra_end_total_all*5;?></div></td>
                                                <td class="Main_color2"><div align="center"><? echo $total_of_end_plate_count_agent_total;?></div></td>
                                                <td class="Main_color2"><div align="center"><? echo $total_of_end_plate_count_vip_total;?></div></td>
                                                <td class="Main_color2"><div align="center"><? echo $total_of_end_plate_count_totals+$total_of_end_bd1_count_agent_total+$user_scr_end_total_all+$user_fib_end_total_all+$user_fra_end_total_all*5+$total_of_end_plate_count_agent_total+$total_of_end_plate_count_vip_total;?></div></td>
                                              </tr>
                                            </table>
                   			          <p>&nbsp;</p>
              </div>
                    <?
					/*---------------   else  -------------- */
				 }?>
                        
								

     <? if ($_POST['pchart'] == '2') {?>
				       <script type="text/javascript">
                        
                                var chart;
                        
                                var chartData = [<?
								
						for ($i=1;$i<=$number_of_user;$i++) { 
							 if ($i <> 1 ){ echo ","; } 
								
								 
						 ?>{plate:"<? echo $user_name_chart[$i];?>",income:<? echo $user_data_chart[$i]?>}<? 
						 	}
						 ?>];
                        
                       
                                window.onload = function() {
                                    chart = new AmCharts.AmSerialChart();
                                    chart.dataProvider = chartData;
                                    chart.categoryField = "plate";
                                    chart.startDuration = 1;
                                    chart.marginTop = 25;
                                    chart.marginLeft = 120;
                                 //	chart.rotate = true;
							
                                    var graph1 = new AmCharts.AmGraph();
                                    graph1.title = "Vehicles";
                                    graph1.valueField = "income";
                                    graph1.type = "column";
                                    graph1.lineAlpha = 0;
                                    graph1.fillColors = "#6633CC";
                                    graph1.fillAlphas = 1;
                                    chart.addGraph(graph1);
                        
                                 
                        
                                    var catAxis = chart.categoryAxis;
                                    catAxis.gridPosition = "start";
                                    catAxis.dashLength = 5;
                        
                                    var valAxis = new AmCharts.ValueAxis();
                                    valAxis.dashLength = 5;
                                    chart.addValueAxis(valAxis);
                        
                                    var valAxis2 = new AmCharts.ValueAxis();
                                    valAxis2.position = "top";
                                    chart.addValueAxis(valAxis2);
                                    
                                    var legend = new AmCharts.AmLegend();
                                    legend.marginLeft = 50;
                                    chart.addLegend(legend);
                        
                                    chart.write("chartdiv1");
                                }
                        
                        
                                </script>
                    
                    
		                  <div align="center"><div id="chartdiv1" style="width: 90%; height: 600px;"></div></div>
                    <? } ?>                   
		 
   <?  } ?>
            </td>
          </tr>
        </table><br />

<script type="text/javascript">
<!--
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1", "date", {format:"yyyy-mm-dd"});
var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2", "date", {format:"yyyy-mm-dd"});
var sprytextfield3 = new Spry.Widget.ValidationTextField("sprytextfield3", "time");
var sprytextfield4 = new Spry.Widget.ValidationTextField("sprytextfield4", "time");
//-->
</script>
<? } ?>
