<?php 
//include("../languege_filee_hr.php");
 include("dbConfig.php");
 
  $lang = $_POST["lang"];
  if($lang=="ar"){
    $message = "تمت عملية الربط بنجاح";
  }else{
    $message = "connect successfully";
  }
  if($lang=="ar"){
    $details = "رصيد افتتاحي من برنامج سماك";
  }else{
    $details = "opening balance from smak";
  }
  $add_user_id = $_POST["add_user_id"];
 $company_id = $_POST["company_id"];
 $company_id_sel = $_POST["company_id_sel"];
 $employ_id = $_POST["employ_id"];
 $account_number = $_POST["account_number"];
$salf_id = $_POST["salf_id"];
 date_default_timezone_set("Asia/Riyadh");
 $createdDate_Time = date("Y-m-d h:i:sa");
 $createdDate = date("Y-m-d");
 //check if already exists
 $sql_check="SELECT * FROM coonect_employ_with_solfacount_tn where company_id='$company_id_sel' and employ_id='$employ_id' and account_number='".$account_number."'";
                $result_check = $conn->query($sql_check);
                     if ($result_check->num_rows > 0) {

                     }else{

                     
 //
// رصيد السلف
     $daenn = 0;
     $madenn = 0;
     $rasedd = 0;
     $sql_daily_restt="SELECT * FROM daily_restriction_tbl where account_number='".$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 = $madenn - $daenn;
                        }}
                                            
                                             
//

 $sql_con = "INSERT INTO coonect_employ_with_solfacount_tn(company_id,employ_id,account_number,createdDate,add_user_id)values('$company_id_sel','$employ_id','$account_number','$createdDate','$add_user_id')";
 $sql_salf = "INSERT INTO hr_salf_tbl(id,company_id,employ_id,details,maden,daen,stat,createdDate,createdDate_time,add_user_id)values('$salf_id','$company_id_sel','$employ_id','$details','$rasedd','0','1','$createdDate','$createdDate_Time','$add_user_id')";
 $add_salf_account = "INSERT INTO hr_salf_accounts(salf_id,employ_id,maden,daen,createdDate,createdDate_time,add_user_id)VALUES('$salf_id','$employ_id','$rasedd','0','$createdDate','$createdDate_Time','$add_user_id')" ;
 //
 if ($conn->query($sql_con) === TRUE && $conn->query($sql_salf) === TRUE && $conn->query($add_salf_account) === TRUE) {
    $_SESSION['message'] =$message;
    header("Refresh:0; url=hr_prepare_smakadvance.php?lang=$lang&company_id=$company_id");
} else {
  echo "Error updating record: " . $conn->error;
}
}
 ?>
