|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Console\Commands; |
| 4 | + |
| 5 | +use App\Models\Course; |
| 6 | +use App\Models\Level; |
| 7 | +use App\Models\Period; |
| 8 | +use App\Models\Rhythm; |
| 9 | +use App\Models\Student; |
| 10 | +use App\Models\User; |
| 11 | +use App\Models\Year; |
| 12 | +use Carbon\Carbon; |
| 13 | +use Illuminate\Console\Command; |
| 14 | +use Illuminate\Support\Facades\Hash; |
| 15 | +use Illuminate\Support\Str; |
| 16 | +use League\Csv\Reader; |
| 17 | + |
| 18 | +class importData extends Command |
| 19 | +{ |
| 20 | + protected $signature = 'academico:import'; |
| 21 | + |
| 22 | + protected $description = 'Command description'; |
| 23 | + |
| 24 | + private function stripAccents($str) { |
| 25 | + return str_replace(' ', '', strtr(utf8_decode($str), utf8_decode('àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ'), 'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY')); |
| 26 | + } |
| 27 | + |
| 28 | + public function handle() |
| 29 | + { |
| 30 | + |
| 31 | + $reader = Reader::createFromPath(\Storage::path('enrollments.csv'), 'r'); |
| 32 | + $reader->setHeaderOffset(0); |
| 33 | + $records = $reader->getRecords(); |
| 34 | + |
| 35 | + foreach ($records as $offset => $record) { |
| 36 | + // if the course already exists, retrieve it |
| 37 | + $period = Period::firstWhere('name', $record['year']); |
| 38 | + |
| 39 | + // Retrieve or create the student |
| 40 | + $email = $record['email'] !== "" ? $record['email'] : $this->stripAccents($record['firstname']) . "." . $this->stripAccents($record['name']) . "@academico.afsantiago.es"; |
| 41 | + |
| 42 | + if (User::where('email', $email)->count() > 0) { |
| 43 | + $user = User::where('email', $email)->first(); |
| 44 | + if (($user->firstname !== trim($record['firstname']) || $user->lastname !== trim($record['name']))) |
| 45 | + { |
| 46 | + $email = $this->stripAccents($record['firstname']) . "." . $this->stripAccents($record['name']) . "@academico.afsantiago.es"; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + $user = User::firstOrCreate([ |
| 51 | + 'firstname' => trim($record['firstname']), |
| 52 | + 'lastname' => trim($record['name']), |
| 53 | + 'locale' => 'es', |
| 54 | + 'email' => $email, |
| 55 | + ], [ |
| 56 | + 'password' => Hash::make(Str::random(12)), |
| 57 | + ]); |
| 58 | + |
| 59 | + $student = Student::firstOrCreate( |
| 60 | + ['id' => $user->id], |
| 61 | + [ |
| 62 | + 'idnumber' => $record['idnumber'] !== "" ? $record['idnumber'] : null, |
| 63 | + 'address' => $record['address'] !== "" ? $record['address'] : null, |
| 64 | + 'city' => $record['city'] !== "" ? $record['city'] : null, |
| 65 | + 'birthdate' => $record['birthdate'] !== "" ? Carbon::createFromFormat("d/m/Y", $record['birthdate'])->toDateString() : null, |
| 66 | + 'zip_code' => $record['zip_code'] !== "" ? $record['zip_code'] : null, |
| 67 | + 'iban' => $record['IBAN'] !== "" ? $record['IBAN'] : null, |
| 68 | + 'bic' => $record['bic_code'] !== "" ? $record['bic_code'] : null, |
| 69 | + ] |
| 70 | + ); |
| 71 | + |
| 72 | + // add phone number |
| 73 | + if ($record['phone'] !== "" && $student->phone()->where('phone_number', $record['phone'])->count() == 0) { |
| 74 | + $student->phone()->create(['phone_number' => $record['phone']]); |
| 75 | + } |
| 76 | + |
| 77 | + if ($period) { |
| 78 | + $course = Course::where('period_id', $period->id)->where('name', $record['course_name']); |
| 79 | + if ($course->exists()) { |
| 80 | + $course = $course->first(); |
| 81 | + } // otherwise try and build a course matching the info we have |
| 82 | + else { |
| 83 | + // if the level name is contained in the course name, assign it. |
| 84 | + foreach (Level::all() as $level) { |
| 85 | + if (Str::contains($record['course_name'], $level->name)) { |
| 86 | + $levelID = $level->id; |
| 87 | + break; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // if the rhythm name is contained in the course name, assign it. |
| 92 | + foreach (Rhythm::all() as $rhythm) { |
| 93 | + if (Str::contains($record['course_name'], $rhythm->name)) { |
| 94 | + $rhythmID = $rhythm->id; |
| 95 | + break; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + $course = Course::firstOrCreate([ |
| 100 | + 'campus_id' => 1, |
| 101 | + 'rhythm_id' => $rhythmID ?? null, |
| 102 | + 'level_id' => $levelID ?? null, |
| 103 | + 'name' => $record['course_name'], |
| 104 | + 'period_id' => $period->id, |
| 105 | + 'start_date' => $period->start, |
| 106 | + 'end_date' => $period->end, |
| 107 | + ]); |
| 108 | + |
| 109 | + unset($rhythmID); |
| 110 | + unset($levelID); |
| 111 | + } |
| 112 | + $student->enroll($course); |
| 113 | + } |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + /*$reader = Reader::createFromPath(\Storage::path('years.csv'), 'r'); |
| 119 | + $records = $reader->getRecords(); |
| 120 | +
|
| 121 | + foreach ($records as $offset => $record) { |
| 122 | + Year::firstOrCreate([ |
| 123 | + 'name' => $record[0], |
| 124 | + ]); |
| 125 | + } |
| 126 | +
|
| 127 | + foreach (Year::all() as $year) { |
| 128 | + Period::create([ |
| 129 | + 'name' => $year->name, |
| 130 | + 'year_id' => $year->id, |
| 131 | + 'start' => \Carbon\Carbon::parse('01/01/' . $year->name)->format('Y-m-d'), |
| 132 | + 'end' => \Carbon\Carbon::parse('12/31/' . $year->name)->format('Y-m-d'), |
| 133 | + ]); |
| 134 | + }*/ |
| 135 | + } |
| 136 | +} |
0 commit comments