die(“Connection failed: ” . $conn->connect_error);
}

// Set character set for proper Persian handling
mysqli_set_charset($conn, “utf8”);

// Function to convert Gregorian date to Jalali
function convertToJalali($gregorianDate)
{
$jalaliDateObj = new jDateTime(true, true, ‘Asia/Tehran’);
return $jalaliDateObj->date(“Y/m/d”, strtotime($gregorianDate));
}

// Function to insert data into calllog table and return the inserted ID
function insertCallLog($serial, $receptor, $operator)
{
global $conn;
$datetime = date(“Y-m-d H:i:s”);

$insertQuery = “INSERT INTO calllog (serial, receptor, operator, date_time) VALUES (‘$serial’, ‘$receptor’, ‘$operator’, ‘$datetime’)”;

if (mysqli_query($conn, $insertQuery)) {
return mysqli_insert_id($conn);
} else {
echo ”

“;
return false;
}
}

// Function to update calllog table with task number and call data
function updateCallLog($id, $taskNumber, $callData)
{
global $conn;
$updateQuery = “UPDATE calllog SET tasknumber=’$taskNumber’, calldata=’$callData’ WHERE id=’$id'”;
if (mysqli_query($conn, $updateQuery)) {
echo ”

“;
} else {
echo ”

“;
}
}

// Kavenegar API integration
function sendSMS($receptor, $token1, $token2)
{
// Check if the receptor number is empty
if (empty($receptor)) {
return; // Don’t proceed if receptor number is empty
}

$apiKey = “415270574F5349545265306244503252575A44584C52614C69736C6C56437841”; // Replace with your Kavenegar API key
$template = “sipaad”;

// Replace spaces in token2 with empty string
$token2 = str_replace(‘ ‘, ”, $token2);

$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => “https://api.kavenegar.com/v1/$apiKey/verify/lookup.json”,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => “”,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => “POST”,
CURLOPT_POSTFIELDS => “receptor=$receptor&token=$token1&token2=$token2&template=$template”,
CURLOPT_HTTPHEADER => array(
“Content-Type: application/x-www-form-urlencoded”
),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
echo “cURL Error #:” . $err;
} else {
$response = json_decode($response, true);
if ($response[‘return’][‘status’] == 200) {
echo ”

“;
} else {
echo ”

“;
}
}
}
?>

 

 

 

 

اطلاعات دستگاه




if (isset($_POST[“submit”])) {
$serial = $_POST[“serial”];
$receptor = $_POST[“receptor”];
$operator = $_POST[“operator”];
if (!empty($receptor)) {
// Insert into calllog table and get the inserted ID
$callLogId = insertCallLog($serial, $receptor, $operator);
};
if (empty($receptor)) {
$callLogId = 1;
};
if ($callLogId) {
// Fetch device details and send SMS
if ($_SERVER[“REQUEST_METHOD”] == “POST”) {
$serial = $_POST[“serial”];
$receptor = $_POST[“receptor”];

// Fetch device code, simcard, indate, and serial from inventory table
$inventoryQuery = “SELECT devicecode, simcard, indate, serial FROM inventory WHERE serial = ‘$serial'”;
$inventoryResult = mysqli_query($conn, $inventoryQuery);

if (!$inventoryResult) {
die(”

Error fetching inventory data: ” . mysqli_error($conn) . “

“);
}

$inventoryRow = mysqli_fetch_assoc($inventoryResult);

if ($inventoryRow) {
$deviceCode = $inventoryRow[“devicecode”];
$simcard = $inventoryRow[“simcard”];
$indate = $inventoryRow[“indate”];
$serial = $inventoryRow[“serial”];

// Fetch data from names table
$namesQuery = “SELECT name, melli, malek, pelak, branchid FROM names WHERE devicecode = ‘$deviceCode'”;
$namesResult = mysqli_query($conn, $namesQuery);

if (!$namesResult) {
die(”

Error fetching names data: ” . mysqli_error($conn) . “

“);
}

$namesRow = mysqli_fetch_assoc($namesResult);
$branchId = $namesRow[“branchid”];

// Fetch branch details based on branchId
$branchQuery = “SELECT `name`, `parent`, `number`, `address` FROM branches WHERE id = ‘$branchId'”;
$branchResult = mysqli_query($conn, $branchQuery);
$branchRow = mysqli_fetch_assoc($branchResult);

// Display branch details if found
if ($branchRow) {
echo ”

جزئیات شعبه

“;
echo ”

نام شعبه: ” . $branchRow[‘name’] . ”

“;
echo ”

آدرس: ” . $branchRow[‘address’] . ”

“;
echo ”

شماره والد: ” . $branchRow[‘parent’] . ”

“;
echo ”

شماره تماس: ” . $branchRow[‘number’] . ”

“;
} else {
echo ”

جزئیات شعبه یافت نشد.

“;
}
$token = $inventoryRow[“serial”];
$token2 = str_replace(‘ ‘, ”, $namesRow[“name”]);
// Display device and user details
echo ”

“;
echo ”

اطلاعات دستگاه و کاربر

“;
echo ”

نام: ” . $namesRow[“name”] . ”

“;
echo ”

کد دستگاه: ” . $deviceCode . ”

“;
echo ”

شماره سیم‌کارت: ” . $simcard . ”

“;
echo ”

پلاک: ” . $namesRow[“pelak”] . ”

“;
echo ”

تاریخ ورود به انبار: ” . $indate . ”

“;
echo ”

شماره سریال: ” . $serial . ”

“;
echo ”

“;
echo ”

لاگ تماس‌ها:

“;

// Fetch calllog entries by serial
$callLogQuery = “SELECT * FROM calllog WHERE serial = ‘$serial'”;
$callLogResult = mysqli_query($conn, $callLogQuery);

if (!$callLogResult) {
die(”

Error fetching call log data: ” . mysqli_error($conn) . “

“);
}

if (mysqli_num_rows($callLogResult) > 0) {
echo ”

“;$rowNum = 1;
while ($callLogRow = mysqli_fetch_assoc($callLogResult)) {
// Convert Gregorian date to Jalali
$jalaliDate = convertToJalali($callLogRow[‘date_time’]);
// Convert time to Iranian time zone
$iranianTime = date(“H:i”, strtotime($callLogRow[‘date_time’]) + 12600); // 12600 seconds = 3.5 hours (Iran’s timezone offset)
echo “”;
}echo ”

ردیف تاریخ و زمان شماره وظیفه داده تماس شماره گیرنده
” . $rowNum++ . “ ” . $jalaliDate . ” ” . $iranianTime . “ ” . $callLogRow[‘tasknumber’] . “ ” . $callLogRow[‘calldata’] . “ ” . $callLogRow[‘receptor’] . “

“;
} else {
echo ”

هیچ اطلاعاتی برای این شماره سریال یافت نشد.

“;
}

echo ”

“;

// Send SMS notification
sendSMS($receptor, $serial, $token2);

// Fetch last 5 days’ data from datadevice table
// Your existing code here…

// Fetch last 5 days’ data from datadevice table
$lastFiveDaysQuery = “SELECT SUMDISTANCEFACTOR, date FROM datadevice WHERE devicecode = ‘$deviceCode’ AND date >= DATE_SUB(CURDATE(), INTERVAL 5 DAY) ORDER BY date”;
$lastFiveDaysResult = mysqli_query($conn, $lastFiveDaysQuery);

if (!$lastFiveDaysResult) {
die(”

Error fetching last five days’ data: ” . mysqli_error($conn) . “

“);
}

echo ”

“;
echo ”

داده‌های آخرین ۵ روز:

“;
echo ”

    • “;

while ($lastFiveDaysRow = mysqli_fetch_assoc($lastFiveDaysResult)) {
$jalaliDate = convertToJalali($lastFiveDaysRow[“date”]);
$distance = $lastFiveDaysRow[“SUMDISTANCEFACTOR”];
echo ”

    • $jalaliDate: $distance

“;
}

echo ”

“;
echo ”

“;

// Fetch all SUMDISTANCEFACTOR and date from datadevice table
$allDataQuery = “SELECT SUMDISTANCEFACTOR, date FROM datadevice WHERE devicecode = ‘$deviceCode’ ORDER BY date”;
$allDataResult = mysqli_query($conn, $allDataQuery);

if (!$allDataResult) {
die(”

Error fetching all data: ” . mysqli_error($conn) . “

“);
}

// Chart data arrays for all data
$allChartDataLabels = [];
$allChartData = [];

while ($allDataRow = mysqli_fetch_assoc($allDataResult)) {
// Convert Gregorian date to Jalali using the helper function
$jalaliDate = convertToJalali($allDataRow[“date”]);

// Populate chart data
$allChartDataLabels[] = $jalaliDate;
$allChartData[] = $allDataRow[“SUMDISTANCEFACTOR”];
}

echo ”

“;
echo ”

نمودار فاصله کل (همه‌ی داده‌ها):

“;
echo ““;
echo ”

“;
} else {
echo ”

دستگاه در انبار یافت نشد.

“;
}
echo ”

تاریخ جدیدترین داده: ” . convertToJalali($newestDate) . “

“;
} // Your existing code here…

// Display the chart
// Your existing code here…

// Display the form for the second stage
echo ”

“;
echo “

“;
echo ““;
echo ”

“;
echo ““;
echo ““;
echo “

“;
echo ”

“;
echo ““;
echo ““;
echo “

“;
echo ““;
echo ”

“;
echo ”

“;
}
}

if (isset($_POST[“update”])) {
$callLogId = $_POST[“callLogId”];
$taskNumber = $_POST[“taskNumber”];
$callData = $_POST[“callData”];

// Update calllog table with task number and call data
updateCallLog($callLogId, $taskNumber, $callData);
}
?>