<?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];
$username = $_SESSION["username_" . $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;
$mpcmpname = $fetcmpname->pname;
//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'];
    $mmrp = $_REQUEST['rowmrp'];
    $mmsp = $_REQUEST['rowmsp'];
    $mrowqty = $_REQUEST['rowqty'];
    $vouchno = $_REQUEST['vouchno'];
    $salesman = $_REQUEST['salesman'];

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

    $resultsales = mysqli_fetch_array($querysales)or die(mysqli_error($conn));
    $salename = $resultsales[name];

    $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 = '9222111555,9892372200,9167531562,8108364186';//    $mobnum = '9320094672';

    $msgtech = 'MSP Req. From ' . $mcmpname . ' ' . $username . "  BN-" . $vouchno .
            " IT " . $mitname . " MRP " . $mmrp . " MSP " . $mmsp . " Qty. " .
            $mrowqty . " RATE " . $mrowrate . " Salesman " . $salename . " OTP " . $pass;


    $user = "BENHUR";
    $password = "BENHUR2017";
    $senderid = "BENHUR";
//    $url = "144.76.39.175/api.php?";
    $url = "http://vas.mobilogi.com/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["rowqty"])) {
//    $rowitem = $_REQUEST['rowitem'];
//    $rowrate = $_REQUEST['rowrate'];
//    $rowqty = $_REQUEST['rowqty'];



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

    $query = "SELECT `name` FROM `item` WHERE code ='$mrowitem'";

    $result = mysqli_query($conn, $query)or die(mysqli_error($conn));
    $itname = $result[name];


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