<?php
  session_start();
  if(empty($_SESSION['branch']) || $_SESSION['branch'] == ''){
    header("Location: index.php");
    die();
  }
    require_once 'system/conn.php';

$date=date('Y-m', strtotime(date('Y-m')." -1 month"));
$date=explode('-',$date);
$month=$date[1];
$monthNum  = $month;
$dateObj   = DateTime::createFromFormat('!m', $monthNum);
$monthName = $dateObj->format('F');
$year=$date[0];
$dt=$monthName." ".$year;
$select_gross=$conn->query("SELECT * FROM `emp_salary` WHERE `month`='".$month."' AND `year`='".$year."' ");
$count_gross=$select_gross->num_rows;
if($count_gross>0)
{
while ($result = $select_gross->fetch_assoc()) {

$emp=$conn->query("SELECT `mobile_num` FROM `emp_details` WHERE `e_id`='".$result['emp_id']."' LIMIT 1");
while ($row = $emp->fetch_assoc()) {

$name = $result['name'];
$Attendance= $result['attendance'];
$Basic_Pay= $result['basic_pay'];
$HRA= $result['hra'];
$Convyance= $result['convyance'];
$Mediacal= $result['medical'];
$Gross_Pay= $result['gross_pay'];
$Pf= $result['pf'];
$ESIC = $result['esic'];
$Advance = $result['advance'];
$tax = $result['tax'];
$deduction = $result['deduction'];
$Netpay = $result['net_pay'];


$user = "0HPgglPto0C5YmPnuDSPsg";
$senderid = "UTCPLB";
$channel = "Trans";
$DCS = 0;
$flashsms = 0;
$number = "91".$row['mobile_num'];
$route = 15;
$m=rawurlencode("Dear ".$name.", your salary for the month of ".$dt.", is : Attendanace ".$Attendance.", Basic pay ".$Basic_Pay.", HRA ".$HRA.", Conveyance ".$Convyance.", Mediacal ".$Mediacal.", Gross_Pay".$Gross_Pay.", PF ".$Pf.", ESIC ".$ESIC.", Advance ".$Advance.", TAX ".$tax.", Total Deduction".$deduction." and Net payment is ".$Netpay.", Regards. UTKAL TEAM CARE PVT LTD");
if($row['mobile_num']!=''||$row['mobile_num']!='NA')
{
$ch=curl_init('http://182.18.143.11/api/mt/SendSMS?APIKEY='.$user.'&senderid='.$senderid.'&channel='.$channel.'&DCS='.$DCS.'&flashsms='.$flashsms.'&number='.$number.'&text='.$m.'&route='.$route.'&DLTTemplateId=1707161777709791730&PEID=1701161537206189435');
$data = curl_exec($ch);
}
}
}
echo '<script type="text/javascript"> window.location="home.php";</script>';
}
else
{
    echo "Sorry, Salary hasnot been generated yet.";
}
?>