stack_letterbox_alerts.php 7.45 KB
Newer Older
Cyril Vazquez's avatar
Cyril Vazquez committed
1
2
<?php

3
$options = getopt("c:n:", ["config:", "notif:"]);
4

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
controlOptions($options);

$txt = '';
foreach (array_keys($options) as $key) {
    if (isset($options[$key]) && $options[$key] == false) {
        $txt .= $key . '=false,';
    } else {
        $txt .= $key . '=' . $options[$key] . ',';
    }
}
printf("{$txt}\n");

if (!is_file($options['config'])) {
    printf("Configuration file does not exist\n");
    exit();
}

$file = file_get_contents($options['config']);
$file = json_decode($file, true);

$customID   = $file['config']['customID'] ?? null;
$maarchUrl  = $file['config']['maarchUrl'];

chdir($file['config']['maarchDirectory']);

require 'vendor/autoload.php';


\SrcCore\models\DatabasePDO::reset();
new \SrcCore\models\DatabasePDO(['customId' => $customID]);

$GLOBALS['lckFile'] = "{$file['config']['maarchDirectory']}/bin/notification/{$customID}process_letterbox_alerts.lck";

if (is_file($GLOBALS['lckFile'])) {
    writeLog(['message' => "An instance of process_letterbox_alerts is already in progress", 'level' => 'INFO']);
    exit();
}
$lockFile = fopen($GLOBALS['lckFile'], 'a');
fwrite($lockFile, '1');
fclose($lockFile);

setBatchNumber();

//=========================================================================================================================================
//FIRST STEP
$alertRecordset = \Notification\models\NotificationModel::get(['select' => ['notification_sid', 'event_id'], 'where' => ['event_id in (?)'], 'data' => [['alert1', 'alert2']]]);
if (empty($alertRecordset)) {
    writeLog(['message' => "No alert set", 'level' => 'INFO']);
    unlink($GLOBALS['lckFile']);
    exit();
}
writeLog(['message' => count($alertRecordset) . " notifications set for mail alerts", 'level' => 'INFO']);

$alertNotifs = [];
foreach ($alertRecordset as $value) {
    $alertNotifs[$value['event_id']][] = $value['notification_sid'];
}


//=========================================================================================================================================
//SECOND STEP
$doctypes = \Doctype\models\DoctypeModel::get();
$doctypes = array_column($doctypes, null, 'type_id');
writeLog(['message' => count($doctypes) . " document types set", 'level' => 'INFO']);


//=========================================================================================================================================
//THIRD STEP
$resources = \Resource\models\ResModel::get([
    'select'    => ['res_id', 'type_id', 'process_limit_date', 'flag_alarm1', 'flag_alarm2'],
    'where'     => ['closing_date IS null', 'status NOT IN (?)', '(flag_alarm1 = \'N\' OR flag_alarm2 = \'N\')', 'process_limit_date IS NOT NULL'],
    'data'      => [['CLO', 'DEL', 'END']]
]);
if (empty($resources)) {
    writeLog(['message' => "No Resource to process", 'level' => 'INFO']);
    unlink($GLOBALS['lckFile']);
    exit();
}
$totalDocsToProcess = count($resources);
writeLog(['message' => "{$totalDocsToProcess} resource(s) to process", 'level' => 'INFO']);


//=========================================================================================================================================
//FOURTH STEP
foreach ($resources as $myDoc) {
    $myDoctype = $doctypes[$myDoc['type_id']];

    writeLog(['message' => "Processing resource {$myDoc['res_id']} with doctype {$myDoc['type_id']}", 'level' => 'INFO']);

    $users = \User\models\UserModel::get(['select' => ['id'], 'orderBy' => ["user_id='superadmin' desc"], 'limit' => 1]);
    $user = $users[0];

    if ($myDoc['flag_alarm1'] != 'Y' && $myDoc['flag_alarm2'] != 'Y' && $myDoctype['delay1'] > 0) {
        $processDate = \Resource\controllers\IndexingController::calculateProcessDate(['date' => $myDoc['process_limit_date'], 'delay' => $myDoctype['delay1'], 'sub' => true]);
        if (strtotime($processDate) <= time()) {
            writeLog(['message' => "Alarm 1 is going to be sent", 'level' => 'INFO']);

            $info = 'Relance 1 pour traitement du document No' . $myDoc['res_id'] . ' avant date limite.';
            if (count($alertNotifs['alert1']) > 0) {
                foreach ($alertNotifs['alert1'] as $notification_sid) {
                    \Notification\models\NotificationsEventsModel::create([
                        'notification_sid' => $notification_sid,
                        'table_name'       => 'res_view_letterbox',
                        'record_id'        => $myDoc['res_id'],
                        'user_id'          => $user['id'],
                        'event_info'       => $info
                    ]);
Cyril Vazquez's avatar
Cyril Vazquez committed
112
                }
113
114
115
116
117
118
119
120
121
            }
            \Resource\models\ResModel::update(['set' => ['flag_alarm1' => 'Y', 'alarm1_date' => 'CURRENT_TIMESTAMP'], 'where' => ['res_id = ?'], 'data' => [$myDoc['res_id']]]);
        }
    }

    if ($myDoc['flag_alarm2'] != 'Y' && $myDoctype['delay2'] > 0) {
        $processDate = \Resource\controllers\IndexingController::calculateProcessDate(['date' => $myDoc['process_limit_date'], 'delay' => $myDoctype['delay2']]);
        if (strtotime($processDate) <= time()) {
            writeLog(['message' => "Alarm 2 is going to be sent", 'level' => 'INFO']);
122

123
124
125
126
127
128
129
130
131
132
            $info = 'Relance 2 pour traitement du document No' . $myDoc['res_id'] . ' apres date limite.';
            if (count($alertNotifs['alert2']) > 0) {
                foreach ($alertNotifs['alert2'] as $notification_sid) {
                    \Notification\models\NotificationsEventsModel::create([
                        'notification_sid' => $notification_sid,
                        'table_name'       => 'res_view_letterbox',
                        'record_id'        => $myDoc['res_id'],
                        'user_id'          => $user['id'],
                        'event_info'       => $info
                    ]);
Cyril Vazquez's avatar
Cyril Vazquez committed
133
134
                }
            }
135
136
            \Resource\models\ResModel::update(['set' => ['flag_alarm1' => 'Y', 'flag_alarm2' => 'Y', 'alarm2_date' => 'CURRENT_TIMESTAMP'], 'where' => ['res_id = ?'], 'data' => [$myDoc['res_id']]]);
        }
Giovannoni Laurent's avatar
Giovannoni Laurent committed
137
    }
Cyril Vazquez's avatar
Cyril Vazquez committed
138
139
}

140

141
writeLog(['message' => "End of process : {$totalDocsToProcess} process without error", 'level' => 'INFO', 'history' => true]);
Cyril Vazquez's avatar
Cyril Vazquez committed
142
unlink($GLOBALS['lckFile']);
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
updateBatchNumber();


function controlOptions(array &$options)
{
    if (empty($options['c']) && empty($options['config'])) {
        printf("Configuration file missing\n");
        exit();
    } elseif (!empty($options['c']) && empty($options['config'])) {
        $options['config'] = $options['c'];
        unset($options['c']);
    }
}

function setBatchNumber()
{
    $parameter = \Parameter\models\ParameterModel::getById(['select' => ['param_value_int'], 'id' => "process_letterbox_alerts_id"]);
    if (!empty($parameter)) {
        $GLOBALS['wb'] = $parameter['param_value_int'] + 1;
    } else {
        \Parameter\models\ParameterModel::create(['id' => 'process_letterbox_alerts_id', 'param_value_int' => 1]);
        $GLOBALS['wb'] = 1;
    }
}

function updateBatchNumber()
{
    \Parameter\models\ParameterModel::update(['id' => 'process_letterbox_alerts_id', 'param_value_int' => $GLOBALS['wb']]);
}

function writeLog(array $args)
{
    \SrcCore\controllers\LogsController::add([
        'isTech'    => true,
        'moduleId'  => 'Notification',
        'level'     => $args['level'] ?? 'INFO',
        'tableName' => '',
        'recordId'  => 'processLetterboxAlerts',
        'eventType' => 'Notification',
        'eventId'   => $args['message']
    ]);

    if (!empty($args['history'])) {
        \History\models\BatchHistoryModel::create(['info' => $args['message'], 'module_name' => 'Notification']);
    }
}