<?php

/**
 * See instructions for installing curl-php library locally
 * http://php.net/manual/en/book.curl.php
 * See instructions to enable curl-php in google cloud
 * https://cloud.google.com/appengine/docs/standard/php/issue-requests
 */

//code here for database connection with sales of same day 
//connect with database 
define('SITEURL','http://itscircle.dyndns.org:81/vtc/');
 try {
    $hostname = "local";
    $port = 10060;
    $dbname = "al_qalam_live";
    //$username = "ali";
    //$pw = "Ali123";
    $username="sa";
    $pw="hamabd";
    //Driver={SQL Server};Server=$hostname;Database=$dbname;
   $connection = odbc_connect("test32", $username, $pw);
  } catch (PDOException $e) {
    echo "Failed to get DB handle: " . $e->getMessage() . "\n";
    exit;
  }
// get the sales of day 
//$SalesDate='2021-03-27';
  $sql="select sum(totalAmount) as NetSales,count(*) as TransactionCount from SalesInvoiceMaster where  Convert(date,InvoiceDate)='".Date("Y-m-d")."'";
  $result=odbc_exec($connection,$sql);
  while($row=odbc_fetch_array($result))
  {
$SalesDate=Date("Y-m-d");
$TransactionCount=$row['TransactionCount'];//25;
$NetSales=$row['NetSales'];//25000;
}

$url = 'https://apidev.emaar.com/etenantsales/dailysales';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'x-apikey:xOVCYCeyR4hGskA7JJZ3b1KHTrrc3R9U',
  'Accept: application/json',
  'Content-Type: application/json',
));
$bodyArray = array(
  'SalesDataCollection' => array(
    'SalesInfo' => array(
      array(
        'UnitNo' => 'TDM-FF-072',
        'LeaseCode' => 't0005562',
        'SalesDate' => $SalesDate,
        'TransactionCount' => $TransactionCount,
        'NetSales' => $NetSales,
      ),
    ),
  ),
);
$body = json_encode($bodyArray);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$result = curl_exec($ch);
if ($result === false) {
  echo curl_error($ch);
}
else {
  echo $result;
}
