trangunghoa
10/6/2017 - 3:10 PM

Schedule for sending notification

Schedule for sending notification

<?php
/**
 * Created by trangunghoa.
 * User: Trang Lee
 * Date: 5/6/2016
 * Time: 10:19 AM
 */
namespace App\Commands;


use App\Flight;
use App\Models\QueueVerification;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;


class VerificationSchedule extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'verification:schedule';
    protected $name = 'verification:schedule';

    /**
     * The console command description.
     * https://setaintl2008.atlassian.net/browse/JETX-1037
     *
     * @var string
     */
    protected $description = 'Verification Schedule';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->info(date('d M Y H:i')." verification Schedule -- Starting!");
        $queues = QueueVerification::getQueue();
        $this->info(count($queues));

        foreach($queues as $item) {
            if ($item->verification != 1) {
                $data = json_decode($item->data);
                $data = (array)$data;
                $info = date('d M Y H:i');
                $info .=  ' flight_id:'.$data['flight_id'];
                $sendmail = false;
                switch ($item->schedule) {
                    case 'pending':
                        if ($item->status == Flight::STATUS_APPROVED) {
                            $info .= ' pending ';
                            $template = 'emails.verificationPending';
                            $title = trans('flight.verification.pending', ['tail_code' => $data['tail_code'], 'departure' => $data['departure_airport_code'], 'arrival' => $data['arrival_airport_code'], 'from_date' => formatDate($data['from_date'],'short')]);
                            $tmpFlight = Flight::find($item->flight_id);
                            if ($tmpFlight) {
                                $tmpFlight->status = Flight::STATUS_PENDING;
                                $tmpFlight->save();
                            }
                            $sendmail = true;
                        }
                        break;
                    case 'notVerified':
                        $info .= ' sheduleNotVerified ';
                        $template = 'emails.sheduleNotVerified';
                        $title = trans('flight.verification.notVerified', ['departure' => $data['departure_airport_code'], 'arrival' => $data['arrival_airport_code'], 'from_date' => formatDate($data['from_date'],'short')]);
                        $sendmail = true;
                        break;
                    case 'each3h':
                    case 'each12h':
                    case 'each48h':
                        $info .= ' '.$item->schedule;
                        $template = 'emails.verificationSchedule48h';
                        $title = trans('flight.verification.larger48h', ['departure' => $data['departure_airport_code'], 'arrival' => $data['arrival_airport_code'], 'from_date' => formatDate($data['from_date'],'short')]);
                        $sendmail = true;
                        break;
                    default:
                        $info .= ' '.$item->schedule;
                        $template = 'nothing';
                        $title = 'nothing';
                        break;
                }
                if ($sendmail) {
                    $users = DB::table('users')->where('vendorClientGUID','=',$data['vendorClientGUID'])->where('active','=',1)->get();
                    if ($users) {
                        foreach($users as $user) {
                            $mail = trim($user->email);
                            if(filter_var($mail, FILTER_VALIDATE_EMAIL)) {
                                // valid address
                                $data['operatorName'] = $user->firstname;
                                Mail::send($template, $data, function ($message) use ($title,$mail) {
                                    //$message->from(Config::get('mail.username'), 'Laravel');
                                    $message->to($mail)->subject($title);
                                });
                            }

                        }
                    }
                }

                $this->info($info);
            }
            //remove queue if mail has sent or has verified

            $scheduleSandbox = config('jetx.scheduleSandbox');
            if ($scheduleSandbox) {
                $item->sent = 1;
                $item->save();
            } else {
                $info = date('d M Y H:i');
                $info .=  ' flight_id:'.$item->flight_id.' delete queue:'.$item->id;
                $this->info($info);
                $item->delete();
            }

        }

        $queues = QueueVerification::getQueuePass();
        if ($queues) {
            foreach($queues as $item) {
                $info = date('d M Y H:i');
                $info .=  ' flight_id:'.$item->flight_id.' delete queue:'.$item->id;
                $this->info($info);
                $item->delete();

            }
        }

        //$this->info("verification Schedule -- Finished!");

    }
}