<?php

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

$zroot = $_SESSION["zdir"];
$zdb = $_SESSION["zdbyear_" . $dbname];
$compcd = $_SESSION["comp_" . $dbname];
//browse($compcd);
$querycomp = mysqli_query($conn, "SELECT * FROM $zroot.`company` WHERE code ='$compcd'") or die(mysqli_error($conn));
//print "SELECT `name` FROM $zroot.`company` WHERE code ='$compcd'";
$fetcmpname = mysqli_fetch_object($querycomp)or die(mysqli_error($conn));
$mcmpname = $fetcmpname->name;
//browse($mcmpname);
mysqli_select_db($conn, $zdb);
$admin = $_SESSION["admin_" . $dbname];

$success = false;

$string = '0123456789';
//$string = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$string_shuffled = str_shuffle($string);
$pass = substr($string_shuffled, 1, 6);

//if (isset($_REQUEST["rowitem"])) {
//    $mrowitem = $_REQUEST['rowitem'];
//    $mrowrate = $_REQUEST['rowrate'];
//    $mrowqty = $_REQUEST['rowqty'];
//    
//
//    $getitname =  mysqli_query($conn, "SELECT `name`,`code` FROM `item` WHERE code ='$mrowitem'") or die(mysqli_error($conn));
//
//    $fetitname = mysqli_fetch_object($getitname)or die(mysqli_error($conn));
//    $mitname = $fetitname->name;
////    browse($mitname);
//
//    $mobnum = '9320094672';
//
//    $msgtech = 'Request From  ' . $mcmpname . " to send Below MSP for Item " . $mitname . " Qty. " . $mrowqty . " at rate of " . $mrowrate . " to sell below MSP Enter this OTP " . $pass;
//
//
//    $user = "aircon";
//    $password = "aircon2013";
//    $senderid = "AIRCON";
//    $url = "144.76.39.175/api.php?";
//
//
//
//    $mobilenumbers = $mobnum;
//
//    $messagetech = urlencode($msgtech);
//
//    $chtech = curl_init();
//    if (!$chtech) {
//        die("Couldn't initialize a cURL handle");
//    }
//    $rettech = curl_setopt($chtech, CURLOPT_URL, $url);
//    curl_setopt($chtech, CURLOPT_POST, 1);
//    curl_setopt($chtech, CURLOPT_POSTFIELDS, "username=$user&password=$password&route=1&sender=$senderid&mobile[]=$mobilenumbers&message[]=$messagetech");
//
//    $rettech = curl_setopt($chtech, CURLOPT_RETURNTRANSFER, true);
//
//    $curlresponsetech = curl_exec($chtech);
//    if (curl_errno($chtech)) {
////        echo 'curl error : ' . curl_error($chtech);
//    }
//    if (empty($rettech)) {
//
//        die(curl_error($chtech));
//        curl_close($chtech);
//    } else {
//        $infotech = curl_getinfo($chtech);
//        curl_close($chtech);
//
////        echo $curlresponsetech;
//    }
//}
if (isset($_REQUEST["party"])) {
    $party    = $_REQUEST['party'];
    $subtotal = $_REQUEST['subtotal'];
    $vouchno  = $_REQUEST['vouchno'];



    $cond = "";
    $rcond = "";
    $qcond = "";

    $query = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE `accode` ='$party'")or die(mysqli_error($conn));

    $result = mysqli_fetch_array($query)or die(mysqli_error($conn));
    $itname = $result[name];
    
        $mobnum = '9820768444,9870004442,9320094672';

//    $mobnum = '9320094672';

    $msgtech = 'Request From  ' . $mcmpname . " on Bill No. " . $vouchno . " to give Discount to " . $itname . " for the Bill of Amount Rs. " . $subtotal .  " to give Discount Enter this OTP " . $pass;


    $user = "ABDRAS";
    $password = "ABDRAS2017";
    $senderid = "ABDRAS";
    $url = "144.76.39.175/api.php?";



    $mobilenumbers = $mobnum;

    $messagetech = urlencode($msgtech);

    $chtech = curl_init();
    if (!$chtech) {
        die("Couldn't initialize a cURL handle");
    }
    $rettech = curl_setopt($chtech, CURLOPT_URL, $url);
    curl_setopt($chtech, CURLOPT_POST, 1);
    curl_setopt($chtech, CURLOPT_POSTFIELDS, "username=$user&password=$password&route=1&sender=$senderid&mobile[]=$mobilenumbers&message[]=$messagetech");

    $rettech = curl_setopt($chtech, CURLOPT_RETURNTRANSFER, true);

    $curlresponsetech = curl_exec($chtech);
    if (curl_errno($chtech)) {
//        echo 'curl error : ' . curl_error($chtech);
    }
    if (empty($rettech)) {

        die(curl_error($chtech));
        curl_close($chtech);
    } else {
        $infotech = curl_getinfo($chtech);
        curl_close($chtech);

//        echo $curlresponsetech;
    }


//    if ($curlresponsetech != '') {
    $rows = array();
    $ctr = 0;
    $r = mysqli_fetch_assoc($result);
    $rows = array('success' => true,
        'otpdssend' => "$pass");
    header("Content-type: application\json");
    echo json_encode($rows);
//    }
}