<?php

require ("includes/check_auth_user.php");
require ("includes/dbconnect.php");
include ('includes/function.php');

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$fseries = $_SESSION["society_" . $dbname];
$zroot = $_SESSION["zdbroot"];

mysqli_select_db($conn, $zdb);

if (isset($_POST["submit"])) {


//    $message = substr($_POST["sms"], 0, 320);


    $getsoc = mysqli_query($conn, "Select compname from $zroot.company where `code`='$fseries'") or die(mysqli_error($conn));
    $fetsoc = mysqli_fetch_array($getsoc);
    
    $compname = mysqli_real_escape_string($conn, $fetsoc['compname']);
    
    
    $getcomp = mysqli_query($conn, "Select user, password from $zroot.company ") or die(mysqli_error($conn));
    $fetcomp = mysqli_fetch_array($getcomp);
    $muser = mysqli_real_escape_string($conn, $fetcomp['user']);
    $mpassword = mysqli_real_escape_string($conn, $fetcomp['password']);
    $user = $muser;
    $senderid = $muser;
    $password = $mpassword;


    $alert = $_POST["alert"];
//    $url = "144.76.39.175/api.php?";
    $url = "http://vas.mobilogi.com/api.php?";

    $date = date("Y-m-d");

    $mobilenumbers = array();
    foreach ($_POST['check_list'] as $to) {
//            $mobilenumbers .= $_POST['check_list'][$i]."|";
//    $ctr = 0;
//    for ($i = 0; $i<=$_POST['selchk']; $i++) {
//    
////browse($_POST['check_list']);
//        $to = $_POST['selchk_'.$i];
        $mob2 = $alert;

        $result = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `active`='1' and `mobile` = '$to' and `active`='1'") or die(mysqli_error($conn));

        $row = mysqli_fetch_array($result);

        $mopbal = $row["openbal"];
        $flatno = $row["flatno"];
        $mprincipal = $row["prinamt"];
        $mopdc = $row["dc"];
        $maccode = $row["accode"];


        if ($mopdc == "D") {
            $mopbal = $mopbal * - 1;
        }
        $getbal = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `actran` WHERE `series`='$fseries' and `accode`='$maccode' AND  date < '$mtodate'") or die(mysqli_error($conn));
        $getamt = mysqli_fetch_array($getbal);
        $amt = $getamt["amt"];
        $mbal = $mopbal + $amt;

        if ($mbal < 0) {
            $mdc = "DR";
        } else {
            $mdc = "CR";
        }

        $balance = abs($mbal);

        $mobilenumbers = "$to,$alert";
        

        $message = 'Flat No. ' . $flatno . ' Your Outstanding Amount is Rs.' . $mbal . ' as on ' . $date . ' Please Do the Payment asap.' . $compname ;
        
        $message = urlencode($message);

        $ch = curl_init();
        if (!$ch) {
            die("Couldn't initialize a cURL handle");
        }


        $ret = curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "username=$user&password=$password&route=1&sender=$senderid&mobile[]=$mobilenumbers&message[]=$message");

        $ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $curlresponse = curl_exec($ch);
        if (curl_errno($ch)) {
            echo 'curl error : ' . curl_error($ch);
        }
        if (empty($ret)) {

            die(curl_error($ch));
            curl_close($ch);
        } else {
            $info = curl_getinfo($ch);
            curl_close($ch);

            echo $curlresponse;
        }
    }
//    header("refresh: ;menu.php?action=sms");
    echo "<script>
            parent.window.location.reload();
            </script>";
}
?>
    
