|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use App\Http\Controllers\StudentImportSuccessMailController; |
|
6
|
|
|
use App\Imports\UsersImport; |
|
7
|
|
|
use App\Imports\StudentUpdate; |
|
8
|
|
|
use App\Mail\StudentImportFailure; |
|
9
|
|
|
use App\Mail\StudentImportSuccess; |
|
10
|
|
|
use App\Mail\EmptyFile; |
|
11
|
|
|
use App\Mail\IncorrectTemplate; |
|
12
|
|
|
use App\Models\Upload; |
|
13
|
|
|
use App\Models\User; |
|
14
|
|
|
use Illuminate\Support\Facades\Log; |
|
15
|
|
|
use Illuminate\Console\Command; |
|
16
|
|
|
use Illuminate\Support\Facades\DB; |
|
17
|
|
|
use Illuminate\Support\Facades\File; |
|
18
|
|
|
use Illuminate\Support\Facades\Mail; |
|
19
|
|
|
use Illuminate\Support\Facades\Storage; |
|
20
|
|
|
use Maatwebsite\Excel\Concerns\Importable; |
|
21
|
|
|
use Maatwebsite\Excel\Facades\Excel; |
|
22
|
|
|
use Illuminate\Support\Carbon; |
|
23
|
|
|
use Illuminate\Support\Facades\App; |
|
24
|
|
|
use Webpatser\Uuid\Uuid; |
|
25
|
|
|
|
|
26
|
|
|
class ImportStudents extends Command |
|
27
|
|
|
{ |
|
28
|
|
|
use Importable; |
|
29
|
|
|
/** |
|
30
|
|
|
* The name and signature of the console command. |
|
31
|
|
|
* |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $signature = 'import:students {node}'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The console command description. |
|
38
|
|
|
* |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $description = 'Bulk students data upload'; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Create a new command instance. |
|
45
|
|
|
* |
|
46
|
|
|
* @return void |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct() |
|
49
|
|
|
{ |
|
50
|
|
|
$this->output = new \Symfony\Component\Console\Output\ConsoleOutput(); |
|
51
|
|
|
parent::__construct(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Execute the console command. |
|
56
|
|
|
* |
|
57
|
|
|
* @return mixed |
|
58
|
|
|
*/ |
|
59
|
|
|
public function handle() |
|
60
|
|
|
{ |
|
61
|
|
|
$files = $this->getFiles(); |
|
62
|
|
|
// if(empty($files)){ |
|
63
|
|
|
// $files = $this->getTerminatedFiles(); |
|
64
|
|
|
// } |
|
65
|
|
|
while ($this->checkTime()) { |
|
66
|
|
|
if ($this->checkTime()) { |
|
67
|
|
|
try { |
|
68
|
|
|
if (!empty($files)) { |
|
69
|
|
|
$this->process($files); |
|
70
|
|
|
unset($files); |
|
71
|
|
|
exit(); |
|
|
|
|
|
|
72
|
|
|
} else { |
|
73
|
|
|
$output = new \Symfony\Component\Console\Output\ConsoleOutput(); |
|
74
|
|
|
$this->output->writeln('No files found,Waiting for files'); |
|
75
|
|
|
exit(); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
} catch (Exception $e) { |
|
78
|
|
|
$output = new \Symfony\Component\Console\Output\ConsoleOutput(); |
|
79
|
|
|
$this->output->writeln($e); |
|
80
|
|
|
sleep(300); |
|
81
|
|
|
$this->handle(); |
|
82
|
|
|
} |
|
83
|
|
|
} else { |
|
84
|
|
|
exit(); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
protected function process($files) |
|
91
|
|
|
{ |
|
92
|
|
|
$time = Carbon::now()->tz('Asia/Colombo'); |
|
93
|
|
|
$node = $this->argument('node'); |
|
94
|
|
|
$files[0]['node'] = $node; |
|
95
|
|
|
$this->processSheet($files[0]); |
|
96
|
|
|
$now = Carbon::now()->tz('Asia/Colombo'); |
|
97
|
|
|
$this->output->writeln('=============== Time taken to batch ' . $now->diffInMinutes($time)); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
protected function getTerminatedFiles() |
|
101
|
|
|
{ |
|
102
|
|
|
$files = Upload::where('is_processed', '=', 3) |
|
103
|
|
|
->where('updated_at', '<=', Carbon::now()->tz('Asia/Colombo')->subHours(3)) |
|
104
|
|
|
->limit(1) |
|
105
|
|
|
->orderBy('updated_at', 'desc') |
|
106
|
|
|
->get()->toArray(); |
|
107
|
|
|
if (!empty($files)) { |
|
108
|
|
|
$this->output->writeln('******************* Processing a terminated file **********************'); |
|
109
|
|
|
DB::beginTransaction(); |
|
110
|
|
|
DB::table('uploads') |
|
111
|
|
|
->where('id', $files[0]['id']) |
|
112
|
|
|
->update(['is_processed' => 4, 'updated_at' => now()]); |
|
113
|
|
|
DB::commit(); |
|
114
|
|
|
} |
|
115
|
|
|
return $files; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
protected function getFiles() |
|
119
|
|
|
{ |
|
120
|
|
|
$query = Upload::where('is_processed', '=', 0) |
|
121
|
|
|
->select( |
|
122
|
|
|
'uploads.id', |
|
123
|
|
|
'uploads.security_user_id', |
|
124
|
|
|
'uploads.institution_class_id', |
|
125
|
|
|
'uploads.model', |
|
126
|
|
|
'uploads.filename', |
|
127
|
|
|
'uploads.is_processed', |
|
128
|
|
|
'uploads.deleted_at', |
|
129
|
|
|
'uploads.created_at', |
|
130
|
|
|
'uploads.updated_at', |
|
131
|
|
|
'uploads.is_email_sent', |
|
132
|
|
|
'uploads.update', |
|
133
|
|
|
'uploads.insert', |
|
134
|
|
|
'uploads.node' |
|
135
|
|
|
) |
|
136
|
|
|
->join('user_contacts', 'uploads.security_user_id', '=', 'user_contacts.security_user_id') |
|
137
|
|
|
->join('contact_types', 'user_contacts.contact_type_id', '=', 'contact_types.id') |
|
138
|
|
|
|
|
139
|
|
|
; |
|
140
|
|
|
if(env('APP_ENV') == 'stage'){ |
|
|
|
|
|
|
141
|
|
|
$query->where('contact_types.contact_option_id', '=', 5) |
|
142
|
|
|
->where('contact_types.name', '=', 'TestEmail'); |
|
143
|
|
|
}else{ |
|
144
|
|
|
$query->where('contact_types.contact_option_id', '!=', 5); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$files = $query->limit(1)->get()->toArray(); |
|
148
|
|
|
$node = $this->argument('node'); |
|
149
|
|
|
if (!empty($files)) { |
|
150
|
|
|
DB::beginTransaction(); |
|
151
|
|
|
DB::table('uploads') |
|
152
|
|
|
->where('id', $files[0]['id']) |
|
153
|
|
|
->update(['is_processed' => 3, 'updated_at' => now(), 'node' => $node]); |
|
154
|
|
|
DB::commit(); |
|
155
|
|
|
} |
|
156
|
|
|
return $files; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
protected function checkTime() |
|
160
|
|
|
{ |
|
161
|
|
|
$time = Carbon::now()->tz('Asia/Colombo'); |
|
162
|
|
|
$morning = Carbon::create($time->year, $time->month, $time->day, env('CRON_START_TIME', 0), 29, 0)->tz('Asia/Colombo')->setHour(0); //set time to 05:59 |
|
163
|
|
|
|
|
164
|
|
|
$evening = Carbon::create($time->year, $time->month, $time->day, env('CRON_END_TIME', 0), 30, 0)->tz('Asia/Colombo')->setHour(23); //set time to 18:00 |
|
165
|
|
|
|
|
166
|
|
|
$check = $time->between($morning, $evening, true); |
|
167
|
|
|
return true; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function processSuccessEmail($file, $user, $subject) |
|
171
|
|
|
{ |
|
172
|
|
|
$file['subject'] = $subject; |
|
173
|
|
|
$this->output->writeln('Processing the file: ' . $file['filename']); |
|
174
|
|
|
try { |
|
175
|
|
|
Mail::to($user->email)->send(new StudentImportSuccess($file)); |
|
176
|
|
|
DB::table('uploads') |
|
177
|
|
|
->where('id', $file['id']) |
|
178
|
|
|
->update(['is_processed' => 1, 'is_email_sent' => 1, 'updated_at' => now()]); |
|
179
|
|
|
} catch (\Exception $ex) { |
|
180
|
|
|
$this->output->writeln($ex->getMessage()); |
|
181
|
|
|
DB::table('uploads') |
|
182
|
|
|
->where('id', $file['id']) |
|
183
|
|
|
->update(['is_processed' => 1, 'is_email_sent' => 2, 'updated_at' => now()]); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public function processFailedEmail($file, $user, $subject) |
|
188
|
|
|
{ |
|
189
|
|
|
$file['subject'] = $subject; |
|
190
|
|
|
try { |
|
191
|
|
|
Mail::to($user->email)->send(new StudentImportFailure($file)); |
|
192
|
|
|
DB::table('uploads') |
|
193
|
|
|
->where('id', $file['id']) |
|
194
|
|
|
->update(['is_processed' => 2, 'is_email_sent' => 1, 'updated_at' => now()]); |
|
195
|
|
|
} catch (\Exception $ex) { |
|
196
|
|
|
$this->output->writeln($ex->getMessage()); |
|
197
|
|
|
DB::table('uploads') |
|
198
|
|
|
->where('id', $file['id']) |
|
199
|
|
|
->update(['is_processed' => 2, 'is_email_sent' => 2, 'updated_at' => now()]); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
public function processEmptyEmail($file, $user, $subject) |
|
204
|
|
|
{ |
|
205
|
|
|
$file['subject'] = $subject; |
|
206
|
|
|
try { |
|
207
|
|
|
Mail::to($user->email)->send(new EmptyFile($file)); |
|
208
|
|
|
DB::table('uploads') |
|
209
|
|
|
->where('id', $file['id']) |
|
210
|
|
|
->update(['is_processed' => 2, 'is_email_sent' => 1, 'updated_at' => now()]); |
|
211
|
|
|
} catch (\Exception $ex) { |
|
212
|
|
|
$this->output->writeln($ex->getMessage()); |
|
213
|
|
|
DB::table('uploads') |
|
214
|
|
|
->where('id', $file['id']) |
|
215
|
|
|
->update(['is_processed' => 2, 'is_email_sent' => 2, 'updated_at' => now()]); |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
protected function checkNode($file) |
|
220
|
|
|
{ |
|
221
|
|
|
$node = $this->argument('node'); |
|
222
|
|
|
if ($node == $file['node']) { |
|
223
|
|
|
$output = new \Symfony\Component\Console\Output\ConsoleOutput(); |
|
224
|
|
|
$this->output->writeln('Processing from:' . $node); |
|
225
|
|
|
return true; |
|
226
|
|
|
} else { |
|
227
|
|
|
exit; |
|
|
|
|
|
|
228
|
|
|
return false; |
|
|
|
|
|
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
protected function processSheet($file) |
|
233
|
|
|
{ |
|
234
|
|
|
$this->startTime = Carbon::now()->tz('Asia/Colombo'); |
|
235
|
|
|
$user = User::find($file['security_user_id']); |
|
236
|
|
|
$this->checkNode($file); |
|
237
|
|
|
$this->output->writeln('##########################################################################################################################'); |
|
238
|
|
|
$this->output->writeln('Processing the file: ' . $file['filename']); |
|
239
|
|
|
if ($this->checkTime()) { |
|
240
|
|
|
try { |
|
241
|
|
|
$this->import($file, 1, 'C'); |
|
242
|
|
|
sleep(10); |
|
243
|
|
|
$this->import($file, 2, 'B'); |
|
244
|
|
|
} catch (\Maatwebsite\Excel\Validators\ValidationException $e) { |
|
245
|
|
|
$this->output->writeln($e->getMessage()); |
|
246
|
|
|
try { |
|
247
|
|
|
Mail::to($user->email)->send(new IncorrectTemplate($file)); |
|
248
|
|
|
DB::table('uploads') |
|
249
|
|
|
->where('id', $file['id']) |
|
250
|
|
|
->update(['is_processed' => 2, 'is_email_sent' => 1, 'updated_at' => now()]); |
|
251
|
|
|
} catch (\Exception $ex) { |
|
252
|
|
|
$this->output->writeln($e->getMessage()); |
|
253
|
|
|
$this->handle(); |
|
254
|
|
|
DB::table('uploads') |
|
255
|
|
|
->where('id', $file['id']) |
|
256
|
|
|
->update(['is_processed' => 2, 'is_email_sent' => 2, 'updated_at' => now()]); |
|
257
|
|
|
} |
|
258
|
|
|
} |
|
259
|
|
|
} else { |
|
260
|
|
|
exit(); |
|
|
|
|
|
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
protected function getType($file) |
|
265
|
|
|
{ |
|
266
|
|
|
$file = storage_path() . '/app/sis-bulk-data-files/' . $file; |
|
267
|
|
|
$inputFileType = \PhpOffice\PhpSpreadsheet\IOFactory::identify($file); |
|
268
|
|
|
return $inputFileType; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
|
|
272
|
|
|
protected function getSheetWriter($file, $reader) |
|
273
|
|
|
{ |
|
274
|
|
|
switch ($this->getType($file['filename'])) { |
|
275
|
|
|
case 'Xlsx': |
|
276
|
|
|
return new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($reader); |
|
277
|
|
|
break; |
|
|
|
|
|
|
278
|
|
|
case 'Ods': |
|
279
|
|
|
return new \PhpOffice\PhpSpreadsheet\Writer\Ods($reader); |
|
280
|
|
|
break; |
|
281
|
|
|
default: |
|
282
|
|
|
return new \PhpOffice\PhpSpreadsheet\Writer\Xls($reader); |
|
283
|
|
|
break; |
|
284
|
|
|
} |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
protected function getSheetType($file) |
|
288
|
|
|
{ |
|
289
|
|
|
switch ($this->getType($file)) { |
|
290
|
|
|
case 'Xlsx': |
|
291
|
|
|
return \Maatwebsite\Excel\Excel::XLSX; |
|
292
|
|
|
break; |
|
|
|
|
|
|
293
|
|
|
case 'Ods': |
|
294
|
|
|
return \Maatwebsite\Excel\Excel::ODS; |
|
295
|
|
|
break; |
|
296
|
|
|
case 'Xml': |
|
297
|
|
|
return \Maatwebsite\Excel\Excel::XML; |
|
298
|
|
|
break; |
|
299
|
|
|
default: |
|
300
|
|
|
return \Maatwebsite\Excel\Excel::XLS; |
|
301
|
|
|
break; |
|
302
|
|
|
} |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
|
|
306
|
|
|
protected function getSheetCount($file) |
|
307
|
|
|
{ |
|
308
|
|
|
$objPHPExcel = $this->setReader($file); |
|
309
|
|
|
return $objPHPExcel->getSheetCount(); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
|
|
313
|
|
|
|
|
314
|
|
|
/** |
|
315
|
|
|
* @param $file |
|
316
|
|
|
* @param $sheet |
|
317
|
|
|
* @param $column |
|
318
|
|
|
*/ |
|
319
|
|
|
protected function import($file, $sheet, $column) |
|
320
|
|
|
{ |
|
321
|
|
|
try { |
|
322
|
|
|
ini_set('memory_limit', '2048M'); |
|
323
|
|
|
$this->getFileSize($file); |
|
324
|
|
|
$user = User::find($file['security_user_id']); |
|
325
|
|
|
$excelFile = '/sis-bulk-data-files/' . $file['filename']; |
|
326
|
|
|
$this->highestRow = $this->getHighestRow($file, $sheet, $column); |
|
327
|
|
|
switch ($sheet) { |
|
328
|
|
|
case 1; |
|
329
|
|
|
$this->output->writeln('Trying to insert Students'); |
|
330
|
|
|
if (($this->getSheetName($file, 'Insert Students')) && $this->highestRow > 0) { // |
|
331
|
|
|
$import = new UsersImport($file); |
|
332
|
|
|
$import->import($excelFile, 'local', $this->getSheetType($file['filename'])); |
|
333
|
|
|
// Excel::import($import, $excelFile, 'local'); |
|
334
|
|
|
DB::table('uploads') |
|
335
|
|
|
->where('id', $file['id']) |
|
336
|
|
|
->update(['insert' => 1, 'is_processed' => 1, 'updated_at' => now()]); |
|
337
|
|
|
if ($import->failures()->count() > 0) { |
|
338
|
|
|
self::writeErrors($import, $file, 'Insert Students'); |
|
339
|
|
|
DB::table('uploads') |
|
340
|
|
|
->where('id', $file['id']) |
|
341
|
|
|
->update(['insert' => 3, 'updated_at' => now()]); |
|
342
|
|
|
$this->processFailedEmail($file, $user, 'Fresh Student Data Upload:Partial Success '); |
|
343
|
|
|
$this->stdOut('Insert Students', $this->highestRow); |
|
344
|
|
|
} else { |
|
345
|
|
|
DB::table('uploads') |
|
346
|
|
|
->where('id', $file['id']) |
|
347
|
|
|
->update(['insert' => 1, 'updated_at' => now()]); |
|
348
|
|
|
$this->processSuccessEmail($file, $user, 'Fresh Student Data Upload:Success '); |
|
349
|
|
|
$this->stdOut('Insert Students', $this->highestRow); |
|
350
|
|
|
} |
|
351
|
|
|
} else if (($this->getSheetName($file, 'Insert Students')) && $this->highestRow > 0) { |
|
352
|
|
|
DB::table('uploads') |
|
353
|
|
|
->where('id', $file['id']) |
|
354
|
|
|
->update(['is_processed' => 2]); |
|
355
|
|
|
$this->processEmptyEmail($file, $user, 'Fresh Student Data Upload '); |
|
356
|
|
|
} |
|
357
|
|
|
break; |
|
358
|
|
|
case 2; |
|
359
|
|
|
$this->output->writeln('Trying to update Students'); |
|
360
|
|
|
if (($this->getSheetName($file, 'Update Students')) && $this->highestRow > 0) { |
|
361
|
|
|
$import = new StudentUpdate($file); |
|
362
|
|
|
$import->import($excelFile, 'local', $this->getSheetType($file['filename'])); |
|
363
|
|
|
if ($import->failures()->count() > 0) { |
|
364
|
|
|
self::writeErrors($import, $file, 'Update Students'); |
|
365
|
|
|
DB::table('uploads') |
|
366
|
|
|
->where('id', $file['id']) |
|
367
|
|
|
->update(['update' => 3, 'is_processed' => 1, 'updated_at' => now()]); |
|
368
|
|
|
$this->processFailedEmail($file, $user, 'Existing Student Data Update:Partial Success '); |
|
369
|
|
|
$this->stdOut('Update Students', $this->highestRow); |
|
370
|
|
|
} else { |
|
371
|
|
|
DB::table('uploads') |
|
372
|
|
|
->where('id', $file['id']) |
|
373
|
|
|
->update(['update' => 1, 'is_processed' => 1, 'updated_at' => now()]); |
|
374
|
|
|
$this->processSuccessEmail($file, $user, 'Existing Student Data Update:Success '); |
|
375
|
|
|
$this->stdOut('Update Students', $this->highestRow); |
|
376
|
|
|
} |
|
377
|
|
|
} else if (($this->getSheetName($file, 'Update Students')) && $this->highestRow == 0) { |
|
378
|
|
|
DB::table('uploads') |
|
379
|
|
|
->where('id', $file['id']) |
|
380
|
|
|
->update(['is_processed' => 2, 'updated_at' => now()]); |
|
381
|
|
|
$this->processEmptyEmail($file, $user, 'Existing Student Data Update'); |
|
382
|
|
|
} |
|
383
|
|
|
break; |
|
384
|
|
|
} |
|
385
|
|
|
} catch (\Maatwebsite\Excel\Validators\ValidationException $e) { |
|
386
|
|
|
$this->output->writeln($e->getMessage()); |
|
387
|
|
|
if ($sheet == 1) { |
|
388
|
|
|
self::writeErrors($e, $file, 'Insert Students'); |
|
389
|
|
|
DB::table('uploads') |
|
390
|
|
|
->where('id', $file['id']) |
|
391
|
|
|
->update(['insert' => 2, 'updated_at' => now()]); |
|
392
|
|
|
$this->processFailedEmail($file, $user, 'Fresh Student Data Upload:Failed'); |
|
393
|
|
|
} else if ($sheet == 2) { |
|
394
|
|
|
self::writeErrors($e, $file, 'Update Students'); |
|
395
|
|
|
DB::table('uploads') |
|
396
|
|
|
->where('id', $file['id']) |
|
397
|
|
|
->update(['update' => 2, 'updated_at' => now()]); |
|
398
|
|
|
$this->processFailedEmail($file, $user, 'Existing Student Data Update:Failed'); |
|
399
|
|
|
} |
|
400
|
|
|
DB::table('uploads') |
|
401
|
|
|
->where('id', $file['id']) |
|
402
|
|
|
->update(['is_processed' => 2, 'updated_at' => now()]); |
|
403
|
|
|
} |
|
404
|
|
|
} |
|
405
|
|
|
|
|
406
|
|
|
protected function processErrors($failure) |
|
407
|
|
|
{ |
|
408
|
|
|
$error_mesg = implode(',', $failure->errors()); |
|
409
|
|
|
$failure = [ |
|
410
|
|
|
'row' => $failure->row(), |
|
411
|
|
|
'errors' => [$error_mesg], |
|
412
|
|
|
'attribute' => $failure->attribute() |
|
413
|
|
|
]; |
|
414
|
|
|
return $failure; |
|
415
|
|
|
} |
|
416
|
|
|
|
|
417
|
|
|
protected function getFileSize($file) |
|
418
|
|
|
{ |
|
419
|
|
|
$excelFile = '/sis-bulk-data-files/' . $file['filename']; |
|
420
|
|
|
$size = Storage::disk('local')->size($excelFile); |
|
421
|
|
|
$user = User::find($file['security_user_id']); |
|
422
|
|
|
if ($size > 0) { |
|
423
|
|
|
return true; |
|
424
|
|
|
} else { |
|
425
|
|
|
DB::table('uploads') |
|
426
|
|
|
->where('id', $file['id']) |
|
427
|
|
|
->update(['is_processed' => 2, 'updated_at' => now()]); |
|
428
|
|
|
$this->stdOut('No valid data found :Please re-upload the file', 0); |
|
429
|
|
|
$this->processEmptyEmail($file, $user, 'No valid data found :Please re-upload the file'); |
|
430
|
|
|
} |
|
431
|
|
|
} |
|
432
|
|
|
|
|
433
|
|
|
protected function setReader($file) |
|
434
|
|
|
{ |
|
435
|
|
|
try { |
|
436
|
|
|
$excelFile = '/sis-bulk-data-files/processed/' . $file['filename']; |
|
437
|
|
|
$exists = Storage::disk('local')->exists($excelFile); |
|
438
|
|
|
if (!$exists) { |
|
439
|
|
|
|
|
440
|
|
|
$excelFile = "/sis-bulk-data-files/" . $file['filename']; |
|
441
|
|
|
} |
|
442
|
|
|
$excelFile = storage_path() . "/app" . $excelFile; |
|
443
|
|
|
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($this->getType($file['filename'])); |
|
444
|
|
|
$objPHPExcel = $reader->load($excelFile); |
|
445
|
|
|
return $objPHPExcel; |
|
446
|
|
|
} catch (Exception $e) { |
|
447
|
|
|
$this->output->writeln($e->getMessage()); |
|
448
|
|
|
$user = User::find($file['security_user_id']); |
|
449
|
|
|
DB::table('uploads') |
|
450
|
|
|
->where('id', $file['id']) |
|
451
|
|
|
->update(['is_processed' => 2, 'updated_at' => now()]); |
|
452
|
|
|
$this->stdOut('No valid data found :Please re-upload the file', 0); |
|
453
|
|
|
$this->processEmptyEmail($file, $user, 'No valid data found :Please re-upload the file'); |
|
454
|
|
|
} |
|
455
|
|
|
} |
|
456
|
|
|
|
|
457
|
|
|
protected function getSheetName($file, $sheet) |
|
458
|
|
|
{ |
|
459
|
|
|
try {; |
|
460
|
|
|
$objPHPExcel = $this->setReader($file); |
|
461
|
|
|
return $objPHPExcel->getSheetByName($sheet) !== null; |
|
462
|
|
|
} catch (Exception $e) { |
|
463
|
|
|
$this->output->writeln($e->getMessage()); |
|
464
|
|
|
$user = User::find($file['security_user_id']); |
|
465
|
|
|
DB::table('uploads') |
|
466
|
|
|
->where('id', $file['id']) |
|
467
|
|
|
->update(['is_processed' => 2, 'updated_at' => now()]); |
|
468
|
|
|
$this->stdOut('No valid data found :Please re-upload the file', 0); |
|
469
|
|
|
$this->processEmptyEmail($file, $user, 'No valid data found :Please re-upload the file'); |
|
470
|
|
|
} |
|
471
|
|
|
} |
|
472
|
|
|
|
|
473
|
|
|
protected function getHighestRow($file, $sheet, $column) |
|
474
|
|
|
{ |
|
475
|
|
|
try { |
|
476
|
|
|
$reader = $this->setReader($file); |
|
477
|
|
|
$reader->setActiveSheetIndex($sheet); |
|
478
|
|
|
$highestRow = 0; |
|
479
|
|
|
$highestRow = $reader->getActiveSheet()->getHighestRow($column); |
|
480
|
|
|
return $highestRow-1; |
|
481
|
|
|
} catch (\Exception $e) { |
|
482
|
|
|
$this->output->writeln($e->getMessage()); |
|
483
|
|
|
$user = User::find($file['security_user_id']); |
|
484
|
|
|
DB::beginTransaction(); |
|
485
|
|
|
DB::table('uploads') |
|
486
|
|
|
->where('id', $file['id']) |
|
487
|
|
|
->update(['is_processed' => 2, 'updated_at' => now()]); |
|
488
|
|
|
DB::commit(); |
|
489
|
|
|
$this->processEmptyEmail($file, $user, 'No valid data found'); |
|
490
|
|
|
exit(); |
|
|
|
|
|
|
491
|
|
|
} |
|
492
|
|
|
} |
|
493
|
|
|
|
|
494
|
|
|
protected function stdOut($title, $rows) |
|
495
|
|
|
{ |
|
496
|
|
|
$this->output->writeln($title . ' Process completed at . ' . ' ' . now()); |
|
497
|
|
|
$now = Carbon::now()->tz('Asia/Colombo'); |
|
498
|
|
|
$this->output->writeln('Total Processed lines: ' . $rows); |
|
499
|
|
|
$this->output->writeln('Time taken to process : ' . $now->diffInSeconds($this->startTime) . ' Seconds'); |
|
500
|
|
|
$this->output->writeln('--------------------------------------------------------------------------------------------------------------------------'); |
|
501
|
|
|
} |
|
502
|
|
|
|
|
503
|
|
|
|
|
504
|
|
|
|
|
505
|
|
|
protected function removeRows($row, $count, $params) |
|
506
|
|
|
{ |
|
507
|
|
|
$reader = $params['reader']; |
|
508
|
|
|
$sheet = $reader->getActiveSheet(); |
|
509
|
|
|
if (!in_array($row, $params['rows'])) { |
|
510
|
|
|
$output = new \Symfony\Component\Console\Output\ConsoleOutput(); |
|
511
|
|
|
$this->output->writeln(' removing row . ' . ' ' . $row); |
|
512
|
|
|
$reader->getActiveSheet()->getCellCollection()->removeRow($row); |
|
513
|
|
|
} |
|
514
|
|
|
} |
|
515
|
|
|
|
|
516
|
|
|
|
|
517
|
|
|
protected function writeErrors($e, $file, $sheet) |
|
518
|
|
|
{ |
|
519
|
|
|
try { |
|
520
|
|
|
ini_set('memory_limit', '2048M'); |
|
521
|
|
|
$baseMemory = memory_get_usage(); |
|
522
|
|
|
gc_enable(); |
|
523
|
|
|
gc_collect_cycles(); |
|
524
|
|
|
$output = new \Symfony\Component\Console\Output\ConsoleOutput(); |
|
525
|
|
|
ini_set('memory_limit', -1); |
|
526
|
|
|
$failures = $e->failures(); |
|
527
|
|
|
$reader = $this->setReader($file); |
|
528
|
|
|
$reader->setActiveSheetIndexByName($sheet); |
|
529
|
|
|
|
|
530
|
|
|
$failures = gettype($failures) == 'object' ? array_map(array($this, 'processErrors'), iterator_to_array($failures)) : array_map(array($this, 'processErrors'), ($failures)); |
|
531
|
|
|
if (count($failures) > 0) { |
|
532
|
|
|
$rows = array_map('rows', $failures); |
|
533
|
|
|
$rows = array_unique($rows); |
|
534
|
|
|
$rowIndex = range(3, $this->highestRow + 2); |
|
535
|
|
|
$params = [ |
|
536
|
|
|
'rows' => $rows, |
|
537
|
|
|
'reader' => $reader |
|
538
|
|
|
]; |
|
539
|
|
|
array_walk($failures, 'append_errors_to_excel', $reader); |
|
540
|
|
|
array_walk($rowIndex, array($this, 'removeRows'), $params); |
|
541
|
|
|
$objWriter = $this->getSheetWriter($file, $reader); |
|
542
|
|
|
Storage::disk('local')->makeDirectory('sis-bulk-data-files/processed'); |
|
543
|
|
|
$objWriter->save(storage_path() . '/app/sis-bulk-data-files/processed/' . $file['filename']); |
|
544
|
|
|
$now = Carbon::now()->tz('Asia/Colombo'); |
|
545
|
|
|
$this->output->writeln($reader->getActiveSheet()->getTitle() . ' Process completed at . ' . ' ' . now()); |
|
546
|
|
|
$this->output->writeln('memory usage for the processes : ' . (memory_get_usage() - $baseMemory)); |
|
547
|
|
|
$this->output->writeln('Time taken to process : ' . $now->diffInSeconds($this->startTime) . ' Seconds'); |
|
548
|
|
|
$this->output->writeln(' errors reported : ' . count($failures)); |
|
549
|
|
|
$this->output->writeln('--------------------------------------------------------------------------------------------------------------------------'); |
|
550
|
|
|
unset($objWriter); |
|
551
|
|
|
unset($failures); |
|
552
|
|
|
} |
|
553
|
|
|
} catch (Eception $e) { |
|
554
|
|
|
$this->output->writeln($e->getMessage()); |
|
555
|
|
|
$user = User::find($file['security_user_id']); |
|
556
|
|
|
DB::beginTransaction(); |
|
557
|
|
|
DB::table('uploads') |
|
558
|
|
|
->where('id', $file['id']) |
|
559
|
|
|
->update(['is_processed' => 2, 'updated_at' => now()]); |
|
560
|
|
|
DB::commit(); |
|
561
|
|
|
$this->processEmptyEmail($file, $user, 'No valid data found'); |
|
562
|
|
|
exit(); |
|
|
|
|
|
|
563
|
|
|
} |
|
564
|
|
|
} |
|
565
|
|
|
} |
|
566
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.