vn-asterisk/incoming.php

82 lines
1.9 KiB
PHP
Executable File

#!/usr/bin/php -q
<?php
if (file_exists(__DIR__.'/env.php'))
include_once __DIR__.'/env.php';
require_once 'agi.php';
require_once 'vn-autoload.php';
$app = new Vn\Lib\App('vn-asterisk');
$app->init();
$db = $app->getSysConn();
Agi::init();
// Formats the caller phone number
$callerId = str_replace('+', '00', Agi::get('agi_callerid'));
$countryPrefix = $db->getValue('SELECT countryPrefix FROM config');
$prefixLen = strlen($countryPrefix);
if (substr($callerId, 0, $prefixLen) === $countryPrefix)
$callerId = substr($callerId, $prefixLen);
// Checks if phone number is on the blacklist
if ($db->getValue('SELECT COUNT(*) > 0 FROM blacklist WHERE phone = #', [$callerId])) {
Agi::exec('HANGUP');
exit;
}
// Checks whether its a festive day
$sundayFestive = $db->getValue('SELECT sundayFestive FROM config');
if (date('N') == 7 && $sundayFestive) {
Agi::exec('SET VARIABLE MACRO playback');
Agi::exec('SET VARIABLE ARG1 out-of-ours');
exit;
}
// Checks schedules
$scheduledQueue = $db->getValue(
'SELECT `queue` FROM pbx.schedule
WHERE `weekDay` = WEEKDAY(CURDATE())
AND util.VN_CURTIME() BETWEEN timeStart AND timeEnd
LIMIT 1'
);
if ($scheduledQueue) {
Agi::exec('SET VARIABLE MACRO queue');
Agi::exec("SET VARIABLE ARG1 $scheduledQueue");
exit;
}
// Gets the client from the phone number
$client = $db->getValue('SELECT clientFromPhone(#)', [$callerId]);
if ($client) {
// Gets the client salesperson extension
$extension = $db->getValue(
'SELECT d.pbxQueue
FROM vn.client c
JOIN vn.worker w ON w.id = c.salesPersonFk
JOIN vn.business b ON b.id = w.businessFk
JOIN vn.department d ON d.id = b.departmentFk
WHERE c.id= #',[$client]
);
if ($extension) {
Agi::exec('SET VARIABLE MACRO exten');
Agi::exec("SET VARIABLE ARG1 $extension");
} else {
Agi::exec('SET VARIABLE MACRO playback');
Agi::exec('SET VARIABLE ARG1 busy');
}
}