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

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

// Data array

		$id=date('Y-m-d');
		$url = "https://api.sisindia.com/Magicxpi4.13/MgWebRequester.dll?prgname=HTTP&appname=IFSPROD_GET_APIS&arguments=-AReports%23SISOutstandingV2&ORG=SIS%20Alarm%20Monitoring%20and%20Response%20Services%20Private%20Limited&DATE=".$id."&USERID=SIS&PWD=U0lTTWFnaWM=";
$xml = simplexml_load_file($url);



$i=0;
foreach ($xml->G_1 as $row) {
	
	$data=[
		'organization_id' => $row->ORGANIZATION_ID,
		'party_name' => str_replace("'"," ",$row->PARTY_NAME), 
		'bal_func_amt' => $row->BAL_FUNC_AMT, 
		'invoice_number' => $row->INVOICE_NUMBER,
		'invoice_amount' => $row->INVOICE_AMOUNT,
		'customer_number' => $row->CUSTOMER_NUMBER,
		'rec_account' => $row->REC_ACCOUNT, 
		'customer_trx_id' => $row->CUSTOMER_TRX_ID,
		'payment_schedule_id' => $row->PAYMENT_SCHEDULE_ID,
		'INVOICE_DATE' => $row->INVOICE_DATE,
		'DUE_DATE' => $row->DUE_DATE
		
				];
	//echo '<pre>'; print_r($data);			
				
$sql= "INSERT INTO `outstanding_reports`(`id`, `organization_id`, `party_name`, `bal_func_amt`, `invoice_number`, `invoice_amount`, `customer_number`, `rec_account`, `customer_trx_id`, `payment_schedule_id`,`INVOICE_DATE`,`DUE_DATE`) 
VALUES (NULL, '".$data['organization_id']."', '".$data['party_name']."', '".$data['bal_func_amt']."', '".$data['invoice_number']."', '".$data['invoice_amount']."', '".$data['customer_number']."', '".$data['rec_account']."', '".$data['customer_trx_id']."', '".$data['payment_schedule_id']."', '".$data['INVOICE_DATE']."', '".$data['DUE_DATE']."')";


$check = "SELECT * FROM outstanding_reports WHERE customer_trx_id='".$row->CUSTOMER_TRX_ID."' ";
$result = $conn->query($check);

if ($result->num_rows > 0) {
    //echo "recor already exists.".'<br>';
} else {

// Execute query
if ($conn->query($sql) === TRUE) {
	
	$customer = "SELECT * FROM customers WHERE ACCOUNT_NUMBER='".$data['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=$data['invoice_number']; 
			$amount=$data['bal_func_amt'];
			$DUE_DATE= $data['DUE_DATE'];
			$date = new DateTime($DUE_DATE);
			$newdate=$date->format("d F Y"); 
			
			$response = sendTextMessage($phone, $billno, $amount,$newdate);
			
		}
		
    //echo "recor already exists.".'<br>';
		}
	
	
	
	
	
	
	$i++;
    //echo "New record inserted successfully.";
	
	
} else {
    //echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}



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

    $body =[
  "to"=> $phone,
  "type"=> "template",
  "source"=> "external",
  "template"=> [
    "name"=> "bill_generate_notifications",
    "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);
}


echo $i."- records inserted successfully.";

$conn->close();

?>