Current File : /home/inlingua/www/crm.vprotectindia.com/cron/payment_due_test.php
<?php
// DB connection
$conn  = new mysqli("localhost", "inlingua_vprotect_user", "zDqxZh3sn65?vyjN", "inlingua_vprotect");

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Today’s date
$today = date('d-m-Y');

// Fetch pending payments
//echo $sql = "SELECT DUE_DATE,party_name,invoice_number,customer_number    FROM outstanding_reports WHERE DUE_DATE>='$today'";

 $sql= "SELECT DUE_DATE,party_name,invoice_number,customer_number,bal_func_amt FROM outstanding_reports WHERE STR_TO_DATE(due_date, '%d-%m-%Y')>=STR_TO_DATE('$today', '%d-%m-%Y') order by due_date";
//echo $sql = "SELECT DUE_DATE,party_name,invoice_number,customer_number,bal_func_amt FROM outstanding_reports WHERE STR_TO_DATE(due_date, '%d-%m-%Y')=STR_TO_DATE('23-08-2025', '%d-%m-%Y') order by due_date";

$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
    $due_date = $row['DUE_DATE'];
    $customer = $row['party_name'];
    $mobile = $row['invoice_number'];
 // Calculate days left
 echo '<br>';
 echo $row['customer_number'].'<>'.$days_left = (strtotime($due_date) - strtotime($today)) / (60 * 60 * 24);

    // Check if reminder needed
    if (in_array($days_left, [7, 15, 30])) {
		
		$customer = "SELECT * FROM customers WHERE ACCOUNT_NUMBER='".$row['customer_number']."' ";
    $res_cus = $conn->query($customer);
    if ($res_cus->num_rows > 0) {
		
		$row1 = mysqli_fetch_assoc($res_cus);
		
		if($row1['CUSTOMER_PHONE_NUMBER']!='')
					{
			
			$phone='91'.$row1['CUSTOMER_PHONE_NUMBER'];
			$billno=$row['invoice_number']; 
			$amount=$row['bal_func_amt'];
			$DUE_DATE= $row['DUE_DATE'];
			$date = new DateTime($DUE_DATE);
			$newdate=$date->format("d F Y"); 
			$template="bill_generate_notifications";
			//echo $phone, $billno, $amount,$newdate;
			
			//$response = sendTextMessage($phone, $billno, $amount,$newdate,$template);
			
					}
		
    			}
    }if($days_left==1 || $days_left==0) {
		$customer = "SELECT * FROM customers WHERE ACCOUNT_NUMBER='".$row['customer_number']."' ";
    $res_cus = $conn->query($customer);
    if ($res_cus->num_rows > 0) {
		
		$row1 = mysqli_fetch_assoc($res_cus);
		
		if($row1['CUSTOMER_PHONE_NUMBER']!='')
					{
			
			$phone='91'.$row1['CUSTOMER_PHONE_NUMBER'];
			$billno=$row['invoice_number']; 
			$amount=$row['bal_func_amt'];
			$DUE_DATE= $row['DUE_DATE'];
			$date = new DateTime($DUE_DATE);
			$newdate='Immediate'; 
			$template='bill_generate_notifications';
			
			//$newdate=$DUE_DATE;  
			
			echo $phone.'<>' .$billno.'<>'. $amount.'--'.$newdate;
			
			//$response = sendTextMessage($phone, $billno, $amount,$newdate,$template);
			//print_r($response); //exit;
					}
		
    			}
	}
}
}
$conn->close();


function sendTextMessage($phone, $billno, $amount,$date,$template) {
    $token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwaG9uZU51bWJlciI6Iis5MTg0NDcxMjQ5NjYiLCJwaG9uZU51bWJlcklkIjoiMTM3NjUzNDQ5NDI4NTg4IiwiaWF0IjoxNjk1NzI5ODkxfQ.zouNW93K9Je56uNrDoyHqLWwrApcZbyUtsl88rpd7wo';
$apiBase = 'https://wb.omni.tatatelebusiness.com/whatsapp-cloud/messages';

    $body =[
  "to"=> $phone,
  "type"=> "template",
  "source"=> "external",
  "template"=> [
    "name"=> $template,
    "language"=> [
      "code"=> "en"
    ],
    "components"=> [
      [
        "type"=> "header",
        "parameters"=> [
          [
            "type"=> "image",
            "image"=> [
              "link"=> "https://crm.vprotectindia.com/public/user/img/brand/white.png"
            ]
          ]
        ]
      ],
      
      [
        "type"=> "body",
        "parameters"=> [
          [
            "type"=> "text",
            "text"=> $billno
          ],
          [
            "type"=> "text",
            "text"=> $amount
          ]
,
          [
            "type"=> "text",
            "text"=> $date
          ]
        ]
      ],[
        "type"=> "button",
        "sub_type"=> "URL",
        "index"=> 1,
        "parameters"=> [
          [
            "type"=> "text",
            "text"=> $billno
          ]
        ]
      ]
    ]
  ],
  "metaData"=> [
    "custom_callback_data"=> "<optional_value>"
  ]
];
echo '<pre>'; echo json_encode($body);
//exit;
     $ch = curl_init("$apiBase");
    curl_setopt_array($ch, [
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => [
            "Authorization:$token",
            "accept: application/json",
            "content-type: application/json; charset=utf-8 "
        ],
        CURLOPT_POSTFIELDS => json_encode($body)
    ]);
    $resp = curl_exec($ch);
    curl_close($ch);
    return json_decode($resp, true);
}



?>