%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/camillo/camillo-api-master/app/Jobs/
Upload File :
Create Path :
Current File : /var/www/html/camillo/camillo-api-master/app/Jobs/CallGenerator.php

<?php

namespace App\Jobs;

use App\Enums\NoticeStatusEnum;
use App\Models\Notice;
use App\Services\XcallyService;
use App\Traits\EventEmitterTrait;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class CallGenerator implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, EventEmitterTrait;


    protected $days = [
        1 => "cr.mon",
        2 => "cr.tue",
        3 => "cr.wed",
        4 => "cr.thu",
        5 => "cr.fri",
        6 => "cr.sat",
        7 => "cr.sun"
    ];
    /**
     * Create a new job instance.
     *
     */
    public function __construct()
    {

    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $presence_event_id = DB::select('select id FROM event_types WHERE event_type = "presence_confirmed"')[0]->id;
        $answered_call__event_id = DB::select('select id FROM event_types WHERE event_type = "emergency_call_answered"')[0]->id;
        $qr_code_arrival_event_id= DB::select('select id FROM event_types WHERE event_type = "qr_minor_arrived"')[0]->id;
        $qr_code_left_event_id = DB::select('select id FROM event_types WHERE event_type = "qr_minor_left"')[0]->id;
        $minor_left_event_id = DB::select('select id FROM event_types WHERE event_type = "minor_left_class"')[0]->id;
        $no_tutor_answered= DB::select('select id FROM event_types WHERE event_type = "no_tutor_answered"')[0]->id;
        $weekday = $this->days[date('N')];
        $now = date("Y-m-d");
        $now_as_int = (int)date("Ymd");
        $now_time = date("H:i:s");

        $query = DB::select("
        select 
        m.id as minor_id,
        i.id as institute_id,
        cr.id as classroom_id,
        ind.id as individual_id,
        sy.id as schoolyear_id,
        ind.user_id as user_id,
        im.is_admin
        from institutes i
        inner join schoolyears sy on i.id = sy.institute_id and sy.start_date <= '$now' and sy.end_date >= '$now'
        left join festivities f on f.schoolyear_id = sy.id and f.start_date <= '$now' and f.end_date >= '$now'
        inner join classrooms cr on cr.institute_id = i.id and cr.schoolyear_id = sy.id and cr.arrival_time < '$now_time' and cr.leaving_time > '$now_time'
        inner join classroom_minor crm on crm.classroom_id = cr.id and crm.active = 1
        inner join minors m on crm.minor_id = m.id and m.active = 1
        left join events e on m.id = e.minor_id and e.classroom_id = crm.classroom_id and e.institute_id = cr.institute_id and e.schoolyear_id = cr.schoolyear_id and date_as_int = '$now_as_int' and e.event_type_id in(:presence_confirmed, :call_answered, :qr_minor_arrived, :qr_minor_left, :minor_left_class, :no_tutor_answered)
        left join absences a on a.minor_id = m.id and a.classroom_id = cr.id and a.date = '$now'
        left join arrival_exceptions ae on ae.minor_id = m.id and ae.classroom_id = cr.id and ae.date = '$now'
        left join notices n on n.institute_id = i.id and n.classroom_id = cr.id and n.minor_id = m.id and n.start_date <= '$now' and n.end_date >= '$now'
        inner join individual_minor im on im.minor_id = m.id
        inner join individuals ind on ind.id = im.individual_id
        where
        i.active = 1 
        and $weekday = 1
        and f.id is null
        and a.id is null
        and n.id is null
        and e.id is null
        and AddTime(coalesce(ae.arrival_time, crm.arrival_time),'00:15:00') < '$now_time'
        ", [
            'presence_confirmed' => $presence_event_id,
            'call_answered' => $answered_call__event_id,
            'qr_minor_arrived' => $qr_code_arrival_event_id,
            'qr_minor_left' => $qr_code_left_event_id,
            'minor_left_class' =>  $minor_left_event_id,
            'no_tutor_answered' =>  $no_tutor_answered
        ]);
        
        $groupedNotices = collect($query)->groupBy(function($item){
           return $item->minor_id.'-'.$item->institute_id.'-'.$item->classroom_id;
        });
        try {
            //per ogni minore
            foreach ($groupedNotices as $noticeCollection){
                DB::beginTransaction();
                try {
                    $minorsToCall = collect();
                    $nowWithTime = date("Y-m-d H:i:s");
                    //Per 3 ripetizioni
                    for ($i = 0; $i < 3; $i++) {
                        $delay = env('XCALLY_TIMEOUT', 150) * $i * $noticeCollection->count();
                        $start_date = date("Y-m-d H:i:s", (strtotime($nowWithTime) + $delay));

                        //per ogni individual nella lista delle persone da avvertire
                        $sortedCollection = collect($noticeCollection)->sortByDesc('is_admin');
                        foreach ($sortedCollection as $key => $individualNotice) {
                            // se per questo minore e questo individual questa classroom e questa data esistono giĆ  3 eventi di tipo emergency_call_forwarded non generare altre notice
                            /*$notice = Notice::create((array)$individualNotice);
                            $notice->content = "Chiamata d'emergenza";
                            Log::channel('database')->info("Chiamata d'emergenza ".$notice->individual->mobile);
                            $delay = env('XCALLY_TIMEOUT', 150) * $key;
                            $scheduled_at = date("Y-m-d H:i:s", (strtotime($start_date) + $delay));
                            $notice->scheduled_at = $scheduled_at;
                            $notice->saveOrFail();*/
                            $individualNotice->content = "Chiamata d'emergenza";
                            $individualNotice->urgency = "high";
                            $individualNotice->start_date = $now;
                            $individualNotice->end_date = $now;
                            $individualNotice->active = 1;

                            $delay = env('XCALLY_TIMEOUT', 150) * $key;
                            $scheduled_at = date("Y-m-d H:i:s", (strtotime($start_date) + $delay));
                            $individualNotice->scheduled_at = $scheduled_at;
                            $individualNotice->created_at = Carbon::now();
                            $individualNotice->updated_at = Carbon::now();
                            $minorsToCall->push((collect($individualNotice)->only(['institute_id', 'minor_id', 'classroom_id', 'content', 'scheduled_at', 'schoolyear_id', 'user_id', 'urgency', 'start_date', 'end_date', 'active', 'individual_id', 'created_at', 'updated_at']))->toArray());
                            /*if($key==0 && $i == 0){
                                sleep(2);
                                $service = new XcallyService;
                                $service->forwardCall($notice);
                           }*/
                        }
                    }
                    Notice::insert($minorsToCall->toArray());
                    DB::commit();
                }
                catch (\Exception $exception) {
                    DB::rollBack();
                    Log::channel('database')->error($noticeCollection->first()->minor_id." ".$exception->getMessage());
                }
                $notice = Notice::where('minor_id', $noticeCollection->first()->minor_id)->first();
                Log::channel('database')->info("{$notice->minor_id}: Chiamata d'emergenza a ".$notice->individual->mobile);
                $service = new XcallyService;
                $service->forwardCall($notice);
            }
        }
        catch (\Exception $exception) {
            Log::channel('database')->error($now_time. " " . $exception->getMessage());
        }
    }
}

Zerion Mini Shell 1.0