<?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'];
//    $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'];
    $salesman = $_REQUEST['salesman'];
    $subtotal = $_REQUEST['subtotal'];
    $vouchno = $_REQUEST['vouchno'];
    $totmrp = $_REQUEST['totmrp'];
    $totmsp = $_REQUEST['msptotal'];
    $totdisc = $_REQUEST['totdisc'];



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

    $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];

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

    $msgtech = 'Disc. Req. ' . $mcmpname . " " . $username . " BN-" . $vouchno . " MSP Total " . $totmsp . " Customer " . $itname . " Bill Amount " . $subtotal . " Discount Total " . $totdisc . " 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 ($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);
//    }
}