|  | @@ -0,0 +1,123 @@
 | 
		
	
		
			
			|  | 1 | +<?php
 | 
		
	
		
			
			|  | 2 | +/**
 | 
		
	
		
			
			|  | 3 | + * Created by PhpStorm.
 | 
		
	
		
			
			|  | 4 | + * User: robin
 | 
		
	
		
			
			|  | 5 | + * Date: 10/1/16
 | 
		
	
		
			
			|  | 6 | + * Time: 5:32 PM
 | 
		
	
		
			
			|  | 7 | + */
 | 
		
	
		
			
			|  | 8 | +
 | 
		
	
		
			
			|  | 9 | +namespace App\Business;
 | 
		
	
		
			
			|  | 10 | +
 | 
		
	
		
			
			|  | 11 | +
 | 
		
	
		
			
			|  | 12 | +use App\DataAccess\SchedulesDataAccess;
 | 
		
	
		
			
			|  | 13 | +use App\Dbo\StopsLiteRoutesLiteDbo;
 | 
		
	
		
			
			|  | 14 | +use App\Dbo\StopsLiteRoutesLiteDboArray;
 | 
		
	
		
			
			|  | 15 | +use App\Dbo\StopsSchedulesDbo;
 | 
		
	
		
			
			|  | 16 | +use Carbon\Carbon;
 | 
		
	
		
			
			|  | 17 | +use Luticate\Utils\Dbo\LuPaginatedDbo;
 | 
		
	
		
			
			|  | 18 | +
 | 
		
	
		
			
			|  | 19 | +class SchedulesBusiness
 | 
		
	
		
			
			|  | 20 | +{
 | 
		
	
		
			
			|  | 21 | +    public static $days = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];
 | 
		
	
		
			
			|  | 22 | +    public static $dateFormat = "Y-m-d";
 | 
		
	
		
			
			|  | 23 | +    public static $timeFormat = "H:i:s";
 | 
		
	
		
			
			|  | 24 | +
 | 
		
	
		
			
			|  | 25 | +    protected static function getDataAccess()
 | 
		
	
		
			
			|  | 26 | +    {
 | 
		
	
		
			
			|  | 27 | +        return new SchedulesDataAccess();
 | 
		
	
		
			
			|  | 28 | +    }
 | 
		
	
		
			
			|  | 29 | +
 | 
		
	
		
			
			|  | 30 | +    public static function getScheduleTypeForDate(array $config, Carbon $date)
 | 
		
	
		
			
			|  | 31 | +    {
 | 
		
	
		
			
			|  | 32 | +
 | 
		
	
		
			
			|  | 33 | +        foreach ($config["scheduleTypes"] as $scheduleType) {
 | 
		
	
		
			
			|  | 34 | +            if ($scheduleType["type"] == static::$days[$date->dayOfWeek]) {
 | 
		
	
		
			
			|  | 35 | +                $from = Carbon::createFromFormat(static::$dateFormat, $scheduleType["dateFrom"]);
 | 
		
	
		
			
			|  | 36 | +                $to = Carbon::createFromFormat(static::$dateFormat, $scheduleType["dateTo"]);
 | 
		
	
		
			
			|  | 37 | +                if ($date->between($from, $to)) {
 | 
		
	
		
			
			|  | 38 | +                    return $scheduleType;
 | 
		
	
		
			
			|  | 39 | +                }
 | 
		
	
		
			
			|  | 40 | +            }
 | 
		
	
		
			
			|  | 41 | +        }
 | 
		
	
		
			
			|  | 42 | +
 | 
		
	
		
			
			|  | 43 | +        foreach ($config["scheduleTypes"] as $scheduleType) {
 | 
		
	
		
			
			|  | 44 | +            if ($scheduleType["type"] == static::$days[$date->dayOfWeek]) {
 | 
		
	
		
			
			|  | 45 | +                return $scheduleType;
 | 
		
	
		
			
			|  | 46 | +            }
 | 
		
	
		
			
			|  | 47 | +        }
 | 
		
	
		
			
			|  | 48 | +
 | 
		
	
		
			
			|  | 49 | +        foreach ($config["scheduleTypes"] as $scheduleType) {
 | 
		
	
		
			
			|  | 50 | +            if ($scheduleType["type"] == "week") {
 | 
		
	
		
			
			|  | 51 | +                $from = Carbon::createFromFormat(static::$dateFormat, $scheduleType["dateFrom"]);
 | 
		
	
		
			
			|  | 52 | +                $to = Carbon::createFromFormat(static::$dateFormat, $scheduleType["dateTo"]);
 | 
		
	
		
			
			|  | 53 | +                if ($date->between($from, $to)) {
 | 
		
	
		
			
			|  | 54 | +                    return $scheduleType;
 | 
		
	
		
			
			|  | 55 | +                }
 | 
		
	
		
			
			|  | 56 | +            }
 | 
		
	
		
			
			|  | 57 | +        }
 | 
		
	
		
			
			|  | 58 | +
 | 
		
	
		
			
			|  | 59 | +        foreach ($config["scheduleTypes"] as $scheduleType) {
 | 
		
	
		
			
			|  | 60 | +            if ($scheduleType["type"] == "week") {
 | 
		
	
		
			
			|  | 61 | +                return $scheduleType;
 | 
		
	
		
			
			|  | 62 | +            }
 | 
		
	
		
			
			|  | 63 | +        }
 | 
		
	
		
			
			|  | 64 | +
 | 
		
	
		
			
			|  | 65 | +        return $config["scheduleTypes"][0];
 | 
		
	
		
			
			|  | 66 | +    }
 | 
		
	
		
			
			|  | 67 | +
 | 
		
	
		
			
			|  | 68 | +    public static function getSchedulesForDate(array $schedules, Carbon $date)
 | 
		
	
		
			
			|  | 69 | +    {
 | 
		
	
		
			
			|  | 70 | +        foreach ($schedules as $schedule) {
 | 
		
	
		
			
			|  | 71 | +            if (in_array($date->dayOfWeek, $schedule["week_days"])) {
 | 
		
	
		
			
			|  | 72 | +                return $schedule["schedule"];
 | 
		
	
		
			
			|  | 73 | +            }
 | 
		
	
		
			
			|  | 74 | +        }
 | 
		
	
		
			
			|  | 75 | +        return null;
 | 
		
	
		
			
			|  | 76 | +    }
 | 
		
	
		
			
			|  | 77 | +
 | 
		
	
		
			
			|  | 78 | +    public static function getSingle(array $scheduleType, StopsLiteRoutesLiteDbo $stop, Carbon $date, int $count)
 | 
		
	
		
			
			|  | 79 | +    {
 | 
		
	
		
			
			|  | 80 | +        $data = static::getDataAccess()->get($scheduleType["resourceId"], "allweek", $stop->getStopId(), $stop->getRouteId());
 | 
		
	
		
			
			|  | 81 | +        $schedules = static::getSchedulesForDate($data["data"]["schedules"], $date);
 | 
		
	
		
			
			|  | 82 | +
 | 
		
	
		
			
			|  | 83 | +        $schedulesCarbon = [];
 | 
		
	
		
			
			|  | 84 | +
 | 
		
	
		
			
			|  | 85 | +        for ($i = 0; $i < min($count, count($schedules)); ++$i) {
 | 
		
	
		
			
			|  | 86 | +            $scheduleDate = Carbon::createFromFormat(static::$timeFormat . " " . static::$dateFormat, $schedules[$i] . " " . $date->toDateString());
 | 
		
	
		
			
			|  | 87 | +            if ($scheduleDate->gte($date)) {
 | 
		
	
		
			
			|  | 88 | +                $schedulesCarbon[] = $scheduleDate;
 | 
		
	
		
			
			|  | 89 | +            }
 | 
		
	
		
			
			|  | 90 | +        }
 | 
		
	
		
			
			|  | 91 | +
 | 
		
	
		
			
			|  | 92 | +        $dbo = new StopsSchedulesDbo();
 | 
		
	
		
			
			|  | 93 | +        $dbo->setRouteId($stop->getRouteId());
 | 
		
	
		
			
			|  | 94 | +        $dbo->setStopId($stop->getStopId());
 | 
		
	
		
			
			|  | 95 | +        $dbo->setSchedules($schedulesCarbon);
 | 
		
	
		
			
			|  | 96 | +        return $dbo;
 | 
		
	
		
			
			|  | 97 | +    }
 | 
		
	
		
			
			|  | 98 | +
 | 
		
	
		
			
			|  | 99 | +    public static function getMultiple(StopsLiteRoutesLiteDboArray $stops, Carbon $date, int $count)
 | 
		
	
		
			
			|  | 100 | +    {
 | 
		
	
		
			
			|  | 101 | +        $config = MiscBusiness::getConfig();
 | 
		
	
		
			
			|  | 102 | +
 | 
		
	
		
			
			|  | 103 | +        $scheduleType = static::getScheduleTypeForDate($config, $date);
 | 
		
	
		
			
			|  | 104 | +
 | 
		
	
		
			
			|  | 105 | +        $dbos = [];
 | 
		
	
		
			
			|  | 106 | +
 | 
		
	
		
			
			|  | 107 | +        foreach ($stops->getArray() as $stop) {
 | 
		
	
		
			
			|  | 108 | +            $dbo = static::getSingle($scheduleType, $stop, $date, $count);
 | 
		
	
		
			
			|  | 109 | +            if (count($dbo->getSchedules()) < $count) {
 | 
		
	
		
			
			|  | 110 | +                $count2 = $count - count($dbo->getSchedules());
 | 
		
	
		
			
			|  | 111 | +                $date2 = $date->copy();
 | 
		
	
		
			
			|  | 112 | +                $date2->addDay(1);
 | 
		
	
		
			
			|  | 113 | +                $date2->setTime(0, 0, 0);
 | 
		
	
		
			
			|  | 114 | +                $scheduleType2 = static::getScheduleTypeForDate($config, $date2);
 | 
		
	
		
			
			|  | 115 | +                $dbo2 = static::getSingle($scheduleType2, $stop, $date2, $count2);
 | 
		
	
		
			
			|  | 116 | +                $dbo->setSchedules(array_merge($dbo->getSchedules(), $dbo2->getSchedules()));
 | 
		
	
		
			
			|  | 117 | +            }
 | 
		
	
		
			
			|  | 118 | +            $dbos[] = $dbo;
 | 
		
	
		
			
			|  | 119 | +        }
 | 
		
	
		
			
			|  | 120 | +
 | 
		
	
		
			
			|  | 121 | +        return $dbos;
 | 
		
	
		
			
			|  | 122 | +    }
 | 
		
	
		
			
			|  | 123 | +}
 |