<?php
require_once('../Db/dbconfig.php');


$sqlCat = "select sum(quantity) as totalqty from purchase p where DATE(created_at) = curdate();";
$resultCat = $con->query($sqlCat);
if ($resultCat->num_rows > 0) 
{
    // output data of each row
    while($rowCat = $resultCat->fetch_assoc()) 
    {
        if($rowCat['totalqty'] !=null){
            sendSMS('Purchased Qty',$rowCat['totalqty']);
        }
    }
}

$sqlstock ="select sum(send_to_quantity) as totalqty from package_to_stock pts where DATE(created_at) = curdate();";
$resultStock = $con->query($sqlstock);
if ($resultStock->num_rows > 0) 
{
    // output data of each row
    while($rowStock = $resultStock->fetch_assoc()) 
    {
        if($rowStock['totalqty'] !=null){
            sendSMS('Stock Qty',$rowStock['totalqty']);
        }
    }
}

$sqlexport ="select sum(quantity) as totalqty from stock_to_export ste where DATE(created_at) = curdate();";
$resultExport = $con->query($sqlexport);
if ($resultExport->num_rows > 0) 
{
    // output data of each row
    while($rowExport = $resultExport->fetch_assoc()) 
    {
        if($rowExport['totalqty'] !=null){
            sendSMS('Export Qty',$rowExport['totalqty']);
        }
    }
}

function sendSMS($reportnm, $summary){
    $ch = curl_init();
  
    $url = "https://ssdhostserver.com/selva/call_sms_controller.php";

    $todaydate = new DateTime("now",new DateTimeZone('Asia/Kolkata'));
    $todaydate = $todaydate->format('Y-m-d');
    
    $message=" date:".$todaydate."\n stock report: ".$reportnm."\n summary: ".$summary."\n -petrix";
    
    $dataArray = ['reportnm' =>$reportnm,'summary'=>$summary,'destination'=>917977233600,'templateId'=>1207162798231915334,'username' =>'vnt-petrix','password'=>'india123','type'=>0,'dlr'=>1,'source'=>'petrix','message'=>$message,'entityid'=>1201162643842575508];
        
    $data = http_build_query($dataArray);
    
    $getUrl = $url."?".$data;
    
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_URL, $getUrl);
    curl_setopt($ch, CURLOPT_TIMEOUT, 80);
       
    $response = curl_exec($ch);
        
    if(curl_error($ch)){
        error_log("Error in sms_controller for ".$reportnm." report: ". curl_error($ch));
    }
       
    curl_close($ch);
}

?>