<?php
session_start();
?>
<script src="ajax1.js"></script>
<?php
include ('dbf.php');
$cod='selkala'.$_GET['q6'];
if($_GET['q2']=='nerkh')
  $focus="onchange='ajaxval(\"mab\",\"$cod\",\"nerkh_morning\",\"nerkh\",\"code\",\"$_SESSION[pe]\",\"$_SESSION[typ]\")'";
$_SESSION[inp]=$_GET['qc1'];
$_SESSION[tblna]=$_GET['q2'];
$_SESSION[val]=$_GET['q3'];
$_SESSION[txt]=$_GET['q4'];
$_SESSION[label]=$_GET['q5'];
$_SESSION[idinp]=$_GET['q6'];
$val1=$_SESSION[val];
$txt1=$_SESSION[txt];
//echo $val1.'f '.$txt1;
?>
<table>
<tr>
 <td><input type="text" id="<?php echo 'tx'.$_SESSION[idinp]; ?>"  size="10"  onblur="ajaxcode1('a',this.value,'<?php echo $_SESSION[idinp]; ?>')"/></td>
 <td>
 <span id=<?php echo 'selkalad'.$_SESSION[idinp]; ?> >
			<select name="<?php echo 'selkala'.$_SESSION[idinp]; ?>" id="<?php echo 'selkala'.$_SESSION[idinp]; ?>" lang="<?php echo $_SESSION[idinp]; ?>"  onblur="trans1(this.lang,this.value,this.id)" <?php echo $focus; ?> class='drop'>
				<option value=''>انتخاب فرمایید</option>
 </span>
           <?php
  if(($_GET['q2']=='driver') && ($_SESSION[mainpage]=='amaliat.php'))
    {
        $qv2=mysql_query("select * from property   ");
        $rv2=mysql_fetch_array($qv2);
       if($_SESSION[typ]=='ins')
       {
       $qu=mysql_query("select * from amaliat ");
       $cu=mysql_num_rows($qu);
       //echo "select  amaliat.id,driver.city,driver.code,driver.name as name ,outime,sum(pay.mab)  from  driver
     //left join amaliat on driver.code=amaliat.drivid inner join pay on driver.code=pay.drivid  where  driver.kind='1' and sum(pay.mab)>='{$rv2[mab]}' group by code order by outime ";
       if($cu=='0')
        $f1=mysql_query("select * from  driver");
       else
        //$f1=mysql_query("select  amaliat.id,driver.city,driver.code,driver.name as name ,outime  from  driver
     //left join amaliat on driver.code=amaliat.drivid   where  driver.kind='1' group by code order by outime ");

	 //$f1=mysql_query("select tb.* from (SELECT amaliat.id,driver.city,driver.code,driver.kind,driver.name AS name, outime,sum( pay.mab ) AS sm,(sum(amaliat.total)* ($rv2[darsad]))/100 AS ttl,case when ((sum(amaliat.total)* ($rv2[darsad]))/100)-(sum( pay.mab ))!=0 then ((sum(amaliat.total)* ($rv2[darsad]))/100)-(sum( pay.mab ))ELSE '0'
//END AS totl   FROM driver LEFT JOIN amaliat ON driver.code = amaliat.drivid left JOIN pay ON driver.code = pay.drivid GROUP BY code )as tb where tb.kind='1' and (((totl)<'{$rv2[mab]}') or ((totl)='0') ) order by outime ");
	$f1=mysql_query("select tb3.sm, tb3.sm1,tb3.totl,tb3.code,tb3.name from (SELECT tb.sm, tb2.sm1,tb.drivid,tb.outime,tb2.kind, tb2.code, tb2.name,case when ((sm* ($rv2[darsad]))/100)-(sm1)!=0 then ((sm* ($rv2[darsad]))/100)-(sm1)ELSE '0'
END AS totl   FROM (SELECT amaliat.drivid, sum( total ) AS sm,outime FROM amaliat GROUP BY drivid) AS tb left JOIN (SELECT sum( mab ) AS sm1, drivid, kind,code,name FROM pay  left JOIN driver ON pay.drivid = driver.code GROUP BY drivid) AS tb2 ON tb.drivid = tb2.drivid) as tb3 WHERE kind =1 and (((totl)>='{$rv2[mab]}') or ((totl)='0') ) UNION SELECT id AS sm, id AS sm1, id AS totl, code, name FROM driver WHERE code NOT IN (SELECT drivid FROM amaliat) ");

	  // echo "select tb3.sm, tb3.sm1,tb3.totl,tb3.code,tb3.name from (SELECT tb.sm, tb2.sm1,tb.drivid,tb.outime,tb2.kind, tb2.code, tb2.name,case when ((sm* ($rv2[darsad]))/100)-(sm1)!=0 then ((sm* ($rv2[darsad]))/100)-(sm1)ELSE '0'
//END AS totl   FROM (SELECT amaliat.drivid, sum( total ) AS sm,outime FROM amaliat GROUP BY drivid) AS tb left JOIN (SELECT sum( mab ) AS sm1, drivid, kind,code,name FROM pay  left JOIN driver ON pay.drivid = driver.code GROUP BY drivid) AS tb2 ON tb.drivid = tb2.drivid) as tb3 WHERE kind =1 and (((totl)<'{$rv2[mab]}') or ((totl)='0') ) order by outime ";
	   }
	   else
	   {

	    $qv1=mysql_query("select * from amaliat  where id ='{$_SESSION[pe]}' ");
        $rv1=mysql_fetch_array($qv1);
	    $f1=mysql_query("select * from driver where kind='1' union select * from driver where code=$rv1[drivid] group by $name");
	   }
     }
    else
	 $f1=mysql_query("select * from $_SESSION[tblna] ");
		    while($n=mysql_fetch_array($f1))
		     {
		       echo "<option value=$n[$val1]> $n[$txt1]</option>";
		     }
	      ?>
		</select>
</td>
</tr>
</table>







