2016-12-12 08:30:01 +00:00
|
|
|
#!/usr/bin/php -q
|
|
|
|
<?php
|
|
|
|
|
2018-08-02 11:43:57 +00:00
|
|
|
if (file_exists(__DIR__.'/env.php'))
|
2024-04-10 16:12:38 +00:00
|
|
|
include_once __DIR__.'/env.php';
|
2018-08-02 11:43:57 +00:00
|
|
|
require_once 'agi.php';
|
2016-12-12 08:30:01 +00:00
|
|
|
require_once 'vn-autoload.php';
|
|
|
|
|
2018-08-02 11:43:57 +00:00
|
|
|
$app = new Vn\Lib\App('vn-asterisk');
|
|
|
|
$app->init();
|
|
|
|
$db = $app->getSysConn();
|
2016-12-12 08:30:01 +00:00
|
|
|
|
2018-08-02 11:43:57 +00:00
|
|
|
Agi::init();
|
2016-12-12 08:30:01 +00:00
|
|
|
|
2024-04-10 16:12:38 +00:00
|
|
|
// http://www.asteriskdocs.org/en/3rd_Edition/asterisk-book-html-chunk/AGI-communication.html
|
|
|
|
$callerId = Agi::get('agi_callerid');
|
|
|
|
$countryCode = Agi::get('agi_arg_1');
|
|
|
|
|
2016-12-12 08:30:01 +00:00
|
|
|
// Formats the caller phone number
|
|
|
|
|
2024-04-10 16:12:38 +00:00
|
|
|
if (preg_match('/^\\+/', $callerId)) {
|
|
|
|
$callerId = '00'. substr($callerId, 1);
|
|
|
|
}
|
|
|
|
elseif (!preg_match('/^00/', $callerId)) {
|
|
|
|
$prefix = $db->getValue(
|
|
|
|
'SELECT prefix FROM prefix WHERE country = #',
|
|
|
|
[$countryCode]
|
|
|
|
);
|
|
|
|
if ($prefix)
|
|
|
|
$callerId = $prefix . $callerId;
|
|
|
|
}
|
2016-12-12 08:30:01 +00:00
|
|
|
|
2024-04-10 16:12:38 +00:00
|
|
|
$countryPrefix = $db->getValue('SELECT defaultPrefix FROM config');
|
2018-08-02 11:43:57 +00:00
|
|
|
$prefixLen = strlen($countryPrefix);
|
2016-12-12 08:30:01 +00:00
|
|
|
|
2018-08-02 11:43:57 +00:00
|
|
|
if (substr($callerId, 0, $prefixLen) === $countryPrefix)
|
|
|
|
$callerId = substr($callerId, $prefixLen);
|
2016-12-12 08:30:01 +00:00
|
|
|
|
2024-04-10 16:12:38 +00:00
|
|
|
// Checks schedules
|
2016-12-12 08:30:01 +00:00
|
|
|
|
2024-04-10 16:12:38 +00:00
|
|
|
if ($countryCode) {
|
|
|
|
$isInSchedule = $db->getValue(
|
|
|
|
'SELECT COUNT(*) > 0 FROM pbx.schedule
|
|
|
|
WHERE weekDays & (1 << WEEKDAY(CURDATE()))
|
|
|
|
AND country = #
|
|
|
|
AND TIME(NOW()) BETWEEN startTime AND endTime',
|
|
|
|
[$countryCode]
|
|
|
|
);
|
2016-12-12 08:30:01 +00:00
|
|
|
|
2024-04-10 16:12:38 +00:00
|
|
|
if ($isInSchedule) {
|
|
|
|
$isOutOfHours = $db->getValue(
|
|
|
|
'SELECT COUNT(*) > 0 FROM pbx.holiday
|
|
|
|
WHERE country = #
|
|
|
|
AND `day` = CURDATE()',
|
|
|
|
[$countryCode]
|
|
|
|
);
|
|
|
|
} else
|
|
|
|
$isOutOfHours = true;
|
2016-12-12 08:30:01 +00:00
|
|
|
}
|
|
|
|
|
2024-04-10 16:12:38 +00:00
|
|
|
if ($isOutOfHours) {
|
|
|
|
Agi::exec("SET VARIABLE MACRO $countryCode-out-of-ours");
|
2018-08-02 11:43:57 +00:00
|
|
|
exit;
|
2016-12-12 09:21:44 +00:00
|
|
|
}
|
|
|
|
|
2024-03-06 16:50:38 +00:00
|
|
|
// Gets the client from the phone number
|
2016-12-12 08:30:01 +00:00
|
|
|
|
2024-04-10 16:12:38 +00:00
|
|
|
$clientId = $db->getValue('SELECT clientFromPhone(#)', [$callerId]);
|
2016-12-12 08:30:01 +00:00
|
|
|
|
2024-04-10 16:12:38 +00:00
|
|
|
if ($clientId) {
|
|
|
|
$client = $db->getObject(
|
|
|
|
'SELECT d.pbxQueue, c.id, d.notificationEmail
|
2024-03-07 15:57:14 +00:00
|
|
|
FROM vn.client c
|
|
|
|
JOIN vn.worker w ON w.id = c.salesPersonFk
|
2024-04-10 16:12:38 +00:00
|
|
|
JOIN vn.business b ON b.id = w.businessFk
|
|
|
|
JOIN vn.department d ON d.id = b.departmentFk
|
|
|
|
WHERE c.id= #',[$clientId]
|
2016-12-12 08:30:01 +00:00
|
|
|
);
|
|
|
|
|
2024-04-10 16:12:38 +00:00
|
|
|
if ($client) {
|
|
|
|
Agi::exec('SET VARIABLE MACRO queue');
|
|
|
|
Agi::exec("SET VARIABLE ARG1 \"{$client->pbxQueue}\"");
|
|
|
|
Agi::exec("SET VARIABLE ARG2 \"Client {$client->id}\"");
|
|
|
|
Agi::exec("SET VARIABLE ARG3 \"{$client->id}\"");
|
|
|
|
Agi::exec("SET VARIABLE ARG4 \"{$client->notificationEmail}\"");
|
2016-12-12 08:30:01 +00:00
|
|
|
}
|
|
|
|
}
|