<?php include("../secure.php"); ?>
<?php include("../Function/level_salf.php");?>
<?php
$id = $_POST["id"];
$company_id = $_POST["company_id"];
$lang = $_POST["lang"];
$user_id = $_POST["user_id"];
if($lang=="ar"){ $notfound="لا يوجد ..."; }else{ $notfound="not found ...";};
                    include("dbConfig.php");
                    $a=array();
                $sql_comv="SELECT * FROM waseet_company_user_tbl where system_id='2222' AND deleted='0'AND user_id='".$_SESSION['user_id']."' GROUP BY company_id ";
                $result_comv = $conn->query($sql_comv);
                if ($result_comv->num_rows > 0) {
               while($row_comv = $result_comv->fetch_assoc()) {
                $company_id_all=$row_comv['company_id']; //echo $company_id_all.'<br>';
                array_push($a,$company_id_all);
                  // echo $_SESSION['user_id'].'<br>';
                  }} 
                  // print_r($a);
                  ?>
				<?php //echo  $company_id;
				//echo $urow['user_type'];
			    $ids = join("','",$a); 
					
                    if($id=="salf_request"){
// طلبات السلف
if(($urow['user_type']=='Top_Manager' or $urow['user_type']=='System_Manager')or($view_solaf_ul==1)){
    if($company_id=='all_company'){
        
           $sql_cw="SELECT * FROM hr_salf_tbl where  company_id IN ('$ids') AND company_id!='' AND deleted='0' and stat='0' and type='salf'";
            }else{
           $sql_cw="SELECT * FROM hr_salf_tbl where company_id='$company_id'AND company_id!='' AND deleted='0' and stat='0' and type='salf'";
            }
            
           }else{
            
                $sql_cw="SELECT * FROM hr_salf_tbl where company_id!='' AND deleted='0' and stat='0' and type='salf'";  
         
           }
           $result_cw = $conn->query($sql_cw);
                        if ($result_cw->num_rows > 0) {
                          while($row_cw = $result_cw->fetch_assoc()) {
                        
                    $sql_emp="SELECT * FROM hr_employ_tbl where employ_id='".$row_cw['employ_id']."' and employ_stat='active_employ' and stop='0' and deleted='0'";
                     $result_emp = $conn->query($sql_emp);
                      if ($result_emp->num_rows > 0) {
                       while($row_emp = $result_emp->fetch_assoc()) {
                        if($lang=='ar'){$employ_name=$row_emp['employ_name_ar'];}else{$employ_name=$row_emp['employ_name_en'];}
                         $employ_id = $row_emp["employ_id"];
                       }}
                       $salf_stat_count = countsalfStat($row_cw["id"]);
                       $employy_approval = getemployacceptid($salf_stat_count,$company_id);
                       if (in_array($user_id,$employy_approval ) or ($row_cw["employ_id"]==$user_id)) {
                          ?> 
							    
                                        <tr class="D<?php echo $row_cw["id"]; ?>">
											
											<td>
											  <?php echo $row_cw["id"]; ?><br><b style="color:red;"></b>
								
											</td>
											
											<td>
											<?php  
											echo $employ_name; ?>
											</td>
                                            <?php
                                                /*$daenn = 0;
                                                $madenn = 0;
                                                $rasedd = 0;
                                                $sql_daily_restt="SELECT * FROM daily_restriction_tbl where account_number='".$row_cw["account_number"]."'";
                                                $result_daily_ress = $conn->query($sql_daily_restt);
                                                if ($result_daily_ress->num_rows > 0) {
                                                while($row_daily_restt = $result_daily_ress->fetch_assoc()) {
                                                    $daenn = $daenn + $row_daily_restt["daen"];
                                                    $madenn = $madenn + $row_daily_restt["maden"];
                                                    $rasedd = $daenn - $madenn;
                                                }}
                                                if($rasedd>=0){?>
                                                <a href="Employee_Advance_details.php?account_number=<?php echo $accont_number;?>"><?php echo round($rasedd);?></a>
                                                    
                                               <?php }else{ ?>
                                                <a href="Employee_Advance_details.php?account_number=<?php echo $accont_number;?>"><?php echo round($rasedd) * -1;?></a>
                                                   
                                               <?php  } */?>
                                           
											<td>
                                               <?php echo $row_cw["maden"];?>
                                               <input type="hidden" name="amount" id="amount<?php echo $row_cw["id"];?>" value="<?php echo $row_cw["maden"]; ?>">
                                            </td>
											<td><?php echo $row_cw["details"];?></td>
                                            <td><?php 
                                             if($lang=='ar'){$stat_detail=$row_cw['detailsar'];}else{$stat_detail=$row_cw['detailsen'];}
                                             if($lang=='ar'){$accept="موافقه";}else{$accept="accept";}
                                            echo $stat_detail;?>
                                             <input type="hidden" name="employy_id" id="employy_id<?php echo $row_cw["id"];?>" value="<?php echo $row_cw["employ_id"]; ?>">
                                             <?php if(in_array($user_id,$employy_approval )){?>
                                        <button class="btn_approve" id="<?php echo $row_cw['id']?>"><?php echo $accept."".$row_cw["level"];?></button>
                                        
                                        <?php } ?>    
                                        </td>
											</tr>
                                                <?php } ?>
											<?php }}else{ ?>
                                              <tr>
                                                <td></td>
                                                <td></td>
                                                <td><?php echo $notfound;?></td>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                              </tr>
                                           <?php } ?>
										</tr>
                                        <?php
                    }elseif($id=="Confirmed_advances"){
// السلف المؤكده
if(($urow['user_type']=='Top_Manager' or $urow['user_type']=='System_Manager')or($view_solaf_ul==1)){
    if($company_id=='all_company'){
        
           $sql_cw="SELECT * FROM hr_salf_tbl where  company_id IN ('$ids') AND company_id!='' AND deleted='0' and stat='1' and type='salf'";
            }else{
           $sql_cw="SELECT * FROM hr_salf_tbl where company_id='$company_id'AND company_id!='' AND deleted='0' and stat='1' and type='salf'";
            }
            
           }else{
            
                $sql_cw="SELECT * FROM hr_salf_tbl where employ_id='$user_id' and company_id!='' AND deleted='0' and stat='1' and type='salf'";  
         
           }
           $result_cw = $conn->query($sql_cw);
                        if ($result_cw->num_rows > 0) {
                          while($row_cw = $result_cw->fetch_assoc()) {
                        
                    $sql_emp="SELECT * FROM hr_employ_tbl where employ_id='".$row_cw['employ_id']."' and employ_stat='active_employ' and stop='0' and deleted='0'";
                     $result_emp = $conn->query($sql_emp);
                      if ($result_emp->num_rows > 0) {
                       while($row_emp = $result_emp->fetch_assoc()) {
                        if($lang=='ar'){$employ_name=$row_emp['employ_name_ar'];}else{$employ_name=$row_emp['employ_name_en'];}
                         $employ_id = $row_emp["employ_id"];
                       }}
                      
                      
                          ?> 
							    
                                        <tr class="D<?php echo $row_cw["id"]; ?>">
											
											<td>
											  <?php echo $row_cw["id"]; ?><br><b style="color:red;"></b>
								
											</td>
											
											<td>
											<?php  
											echo $employ_name; ?>
											</td>
                                            <?php
                                                /*$daenn = 0;
                                                $madenn = 0;
                                                $rasedd = 0;
                                                $sql_daily_restt="SELECT * FROM daily_restriction_tbl where account_number='".$row_cw["account_number"]."'";
                                                $result_daily_ress = $conn->query($sql_daily_restt);
                                                if ($result_daily_ress->num_rows > 0) {
                                                while($row_daily_restt = $result_daily_ress->fetch_assoc()) {
                                                    $daenn = $daenn + $row_daily_restt["daen"];
                                                    $madenn = $madenn + $row_daily_restt["maden"];
                                                    $rasedd = $daenn - $madenn;
                                                }}
                                                if($rasedd>=0){?>
                                                <a href="Employee_Advance_details.php?account_number=<?php echo $accont_number;?>"><?php echo round($rasedd);?></a>
                                                    
                                               <?php }else{ ?>
                                                <a href="Employee_Advance_details.php?account_number=<?php echo $accont_number;?>"><?php echo round($rasedd) * -1;?></a>
                                                   
                                               <?php  } */?>
                                           
											<td>
                                               <?php echo $row_cw["maden"];?>
                                               <input type="hidden" name="amount" id="amount<?php echo $row_cw["id"];?>" value="<?php echo $row_cw["maden"]; ?>">
                                            </td>
											<td><?php echo $row_cw["details"];?></td>
                                            <td><?php 
                                             if($lang=='ar'){$stat_detail=$row_cw['detailsar'];}else{$stat_detail=$row_cw['detailsen'];}
                                             if($lang=='ar'){$accept="موافقه";}else{$accept="accept";}
                                            echo $stat_detail;?>
                                              
                                        </td>
											</tr>
                                               
											<?php }}else{ ?>
                                              <tr>
                                                <td></td>
                                                <td></td>
                                                <td><?php echo $notfound;?></td>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                              </tr>
                                           <?php } ?>
										</tr>
                                        <?php 
                    }elseif($id=="Rejected_advances"){
// السلف المرفوضه
if(($urow['user_type']=='Top_Manager' or $urow['user_type']=='System_Manager')or($view_solaf_ul==1)){
    if($company_id=='all_company'){
        
           $sql_cw="SELECT * FROM hr_salf_tbl where  company_id IN ('$ids') AND company_id!='' AND deleted='0' and stat='2'";
            }else{
           $sql_cw="SELECT * FROM hr_salf_tbl where system_id='2222' and company_id='$company_id'AND company_id!='' AND deleted='0' and stat='2'";
            }
            
           }else{
            
                $sql_cw="SELECT * FROM hr_salf_tbl where company_id!='' AND deleted='0' and stat='2'";  
         
           }
                    }else{
                        if(($urow['user_type']=='Top_Manager' or $urow['user_type']=='System_Manager')or($view_solaf_ul==1)){
                            if($company_id=='all_company'){
                                
                                   $sql_cw="SELECT * FROM hr_salf_tbl where  company_id IN ('$ids') AND company_id!='' AND deleted='0' ";
                                    }else{
                                   $sql_cw="SELECT * FROM hr_salf_tbl where system_id='2222' and company_id='$company_id'AND company_id!='' AND deleted='0' ";
                                    }
                                    
                                   }else{
                                    
                                        $sql_cw="SELECT * FROM hr_salf_tbl where company_id!='' AND deleted='0' ";  
                                 
                                   }
                    }
                    


                        
								
