<?
session_start();
$brcode = $_SESSION['brcode'];
$user = $_SESSION['user'];
date_default_timezone_set("Asia/Kolkata"); 
if(strlen($brcode)<=0)
{
 	print "<script> alert('Invalid Login'); parent.document.location='ind.php' </script>";
 	return;
}
include("../opendb.php");
include("../test.php");
$d = $_REQUEST['d'];
$m = $_REQUEST['m'];
$y = $_REQUEST['y'];
$d1 = $_REQUEST['d1'];
$m1 = $_REQUEST['m1'];
$y1 = $_REQUEST['y1'];
$asondt = $_REQUEST['asondt'];

$df=$y.'-'.$m.'-'.$d;
$dt=date('Y-m-d');
$curdt=implode('-', array_reverse(explode('-', $dt)));
$asondt1=implode('-', array_reverse(explode('-', $asondt)));
$chkled = $_REQUEST['chkled'];
$i=0;
foreach($chkled as $a)
{
    $chkled[$i]=$a; 
    $i++;
}
//print_r($chkled);
$branchqry=mysqli_query($db,"select incharge_no from branch where brcode='$brcode'");
$branchrow=mysqli_fetch_array($branchqry);
$branchinchargeno=$branchrow[0];

for($j=0;$j<$i;$j++)
{
    $smsqry=mysqli_query($db,"select phone1,acname,father from customer where acname='$chkled[$j]'");
    $smsrow=mysqli_fetch_array($smsqry);
    $custphone=$smsrow[0];
    $customername=$smsrow[1];
    $salescontact=$smsrow[2];
    $customername=trim($customername);
    $customer30=substr($customername, 0, 30);
    //$custphone='9895570415';
    //$custphone='9961301466';
    $phone ="";
    for ($ctr1=0;$ctr1<=strlen($custphone);$ctr1++)
    {
        $mnum = substr($custphone,$ctr1,1);
        if($mnum>='0' && $mnum<='9')
            $phone .= $mnum;
    }
    $custphone=$phone;
    //$custphone='9961301466';
    $phonestring=$custphone;
    if(strlen($branchinchargeno)>0)
        $phonestring=$custphone.",".$branchinchargeno;
    if(strlen($salescontact)>0)
        $phonestring=$phonestring.",".$salescontact;
        
    $msms_code ="";
    for ($ctr=0;$ctr<=3;$ctr++)
    {
	    $msms_code .=chr(rand(48,57)).chr(rand(65,90)).chr(rand(97,122));
    }
    //$smsstring=$msms_code.$chkled[$j];
    $smsstring=$msms_code;
    $curdate=date("Y-m-d H:i:s");
    
    //print "insert into accledgersms (acname,datetime,code,custid,brcode,mobile) values('$smsrow[1]','$curdate','$smsstring','$chkled[$j]','$brcode','$custphone')";
    $brname=' PSNAUTO';
    $url1="http://68.178.160.62/ampleicher/paypdf/custledger.php?custid=$smsstring";
     //echo $url;
    //echo "<br>";
    //$custphone='9746013982';
     //$message="GATEENTRY - Vehicle No $regno entered on $curdate for $purps to meet $contactperson. http://psnauto.in/gateentry_3793/admin/index.php";
    $message="Dear Customer, Statement of accounts generated as on $asondt1. To open $url1 - $customer30 - PSNAUT";
        $message=urlencode($message);
        $url="www.bulksmsalert.in/SendSms.aspx?username=psnauto&password=THCM$456&from=PSNAUT&to=".$phonestring."&message=".$message."";
        $ch = curl_init($url); 
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: 0'));
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);  
        if ($output === FALSE) 
        {
            die(curl_error($ch));
        }
        curl_close($ch);
          if ($output === FALSE) 
        {
            die(curl_error($ch));
        }
        else
        {
            mysqli_query($db,"insert into accledgersms (acname,datetime,code,custid,brcode,mobile,asondate,username) values('$smsrow[1]','$curdate','$smsstring','0','$brcode','$custphone','$asondt','$user')");

            //$sql="insert into accledgersms (acname,datetime,code,custid,brcode,mobile) values('$smsrow[1]','$curdate','$smsstring','0','$brcode','$custphone')";
            //echo $sql;
           // mysqli_query($db,"update vehentry set smsflg='1' where id='$smsrwid' and brcode='$brcode'");
        //echo "message sent successfully";
            
        }
    /*
    $query2="select corp,acname,id from customer where id='$chkled[$j]' and brcode='$brcode'";
    echo $query2;
    echo "<br>";
	$rs=mysqli_query($db,$query2);
	$jv_parts_row=mysqli_fetch_array($rs);
	*/
     //$msgtxt="'$itmtyp[$j]'";
     
}


    print "<script>alert('Message sent successfully'); </script>";
	print "<script>document.location='custled.php'</script>";
?>