$opt location_id, staff_id, staff_rules, except_hold, except_booking. * @return array> */ public function available( Bookable $bookable, int $from, int $to, array $opt = array() ): array { $tz = Services::availability()->zone( $bookable, $opt ); [ $earliest, $latest ] = Services::availability()->limits( $bookable, $opt, $tz ); $sessions = Services::sessions_repo()->between( array( $bookable->id ), max( $from, $earliest ), min( $to, $latest ) ); $location = (int) ( $opt['location_id'] ?? 0 ); $staff = (int) ( $opt['staff_id'] ?? 0 ); $sessions = array_values( array_filter( $sessions, static fn( array $s ): bool => ( 0 === $location || 0 === $s['location_id'] || $s['location_id'] === $location ) && ( 0 === $staff || $s['staff_id'] === $staff ) ) ); return $this->with_places( $bookable, $sessions, (int) ( $opt['except_booking'] ?? 0 ), (string) ( $opt['except_hold'] ?? '' ) ); } /** * Add places left to sessions. * * @param Bookable $bookable Bookable. * @param array> $sessions Sessions. * @param int $except_booking Booking to leave out. * @param string $except_hold Hold to leave out. * @return array> */ public function with_places( Bookable $bookable, array $sessions, int $except_booking = 0, string $except_hold = '' ): array { $ids = array_column( $sessions, 'id' ); $sold = Services::bookings()->sold( $ids, $except_booking ); $held = Services::holds()->held_in_sessions( $ids, $except_hold ); foreach ( $sessions as $i => $session ) { $capacity = $session['capacity'] > 0 ? $session['capacity'] : $bookable->capacity; $taken = ( $sold[ $session['id'] ]['total'] ?? 0 ) + ( $held[ $session['id'] ] ?? 0 ); $tickets = array(); foreach ( $this->tickets( $bookable, $session ) as $ticket ) { $cap = (int) $ticket['capacity']; $tickets[] = $ticket + array( 'remaining' => $cap > 0 ? max( 0, $cap - ( $sold[ $session['id'] ]['tickets'][ $ticket['id'] ] ?? 0 ) ) : null, ); } $sessions[ $i ]['capacity'] = $capacity; $sessions[ $i ]['booked'] = $taken; $sessions[ $i ]['remaining'] = max( 0, $capacity - $taken ); $sessions[ $i ]['tickets'] = $tickets; } return $sessions; } /** * Ticket types for a session: its own, else the bookable's. * * @param Bookable $bookable Bookable. * @param array $session Session. * @return array> */ public function tickets( Bookable $bookable, array $session ): array { $own = (array) ( $session['tickets'] ?? array() ); return array() !== $own ? $own : (array) $bookable->settings['tickets']; } /** * Make class sessions from the weekly timetable for the next weeks, * keeping booked ones and replacing unbooked future ones. * * @param Bookable $bookable Bookable. * @return int Sessions created. */ public function generate( Bookable $bookable ): int { if ( Bookable::CLASS_TYPE !== $bookable->type ) { return 0; } $repo = Services::sessions_repo(); $now = Clock::now(); $existing = $repo->between( array( $bookable->id ), $now, $now + ( self::WEEKS_AHEAD + 1 ) * WEEK_IN_SECONDS ); $booked = Services::bookings()->sold( array_column( $existing, 'id' ) ); $keep = array(); $taken = array(); foreach ( $existing as $session ) { if ( ( $booked[ $session['id'] ]['total'] ?? 0 ) > 0 || 'timetable' !== $session['series'] ) { $keep[] = $session['id']; $taken[ $session['start'] . '-' . $session['staff_id'] ] = true; } } $repo->delete_unbooked( $bookable->id, 'timetable', $now, $keep ); $created = 0; foreach ( (array) $bookable->settings['timetable'] as $entry ) { $location = (int) $entry['location_id'] > 0 ? Services::locations()->find( (int) $entry['location_id'] ) : null; $tz = Settings::zone( (string) ( $location['timezone'] ?? '' ) ); $minutes = WeeklyHours::minutes( (string) $entry['time'], 'time' ); $date = ( new DateTimeImmutable( '@' . $now ) )->setTimezone( $tz )->setTime( 0, 0 ); for ( $day = 0; $day < self::WEEKS_AHEAD * 7; $day++ ) { $local = $date->modify( "+{$day} days" ); if ( (int) $local->format( 'N' ) !== (int) $entry['weekday'] ) { continue; } $start = Hours::at( $local->format( 'Y-m-d' ), $minutes, $tz ); if ( $start <= $now || isset( $taken[ $start . '-' . (int) $entry['staff_id'] ] ) ) { continue; } $repo->create( array( 'bookable_id' => $bookable->id, 'start' => $start, 'end' => $start + $bookable->duration * 60, 'capacity' => 0, 'staff_id' => (int) $entry['staff_id'], 'location_id' => (int) $entry['location_id'], 'series' => 'timetable', 'status' => 'scheduled', ) ); ++$created; } } \ScheduleReed\Cache::bump(); return $created; } /** * Top up every class's sessions; runs daily. */ public function generate_all(): void { foreach ( Services::bookables()->all( true ) as $row ) { $bookable = Bookable::from_row( $row ); if ( Bookable::CLASS_TYPE === $bookable->type && array() !== $bookable->settings['timetable'] ) { $this->generate( $bookable ); } } } /** * Cancel sessions that did not reach their minimum number of people by * the cut-off, with their bookings, so customers hear in good time. */ public function cancel_under_minimum(): void { $now = Clock::now(); foreach ( Services::bookables()->all( true ) as $row ) { $bookable = Bookable::from_row( $row ); $minimum = (int) $bookable->settings['min_attendees']; if ( ! $bookable->uses_sessions() || $minimum < 2 ) { continue; } $cutoff = (int) $bookable->settings['min_hours'] * HOUR_IN_SECONDS; $sessions = $this->with_places( $bookable, Services::sessions_repo()->between( array( $bookable->id ), $now, $now + $cutoff + HOUR_IN_SECONDS ) ); foreach ( $sessions as $session ) { if ( $session['start'] - $now <= $cutoff && $session['booked'] < $minimum ) { $this->cancel( $session['id'], __( 'Not enough people booked this session, so it will not go ahead.', 'schedulereed' ) ); } } } } /** * Cancel a session and every booking in it, refunding what the policy * would not keep (all of it, since the business cancelled). * * @param int $session_id Session. * @param string $reason Told to customers. */ public function cancel( int $session_id, string $reason = '' ): int { $session = Services::sessions_repo()->find( $session_id ); if ( null === $session || 'cancelled' === $session['status'] ) { return 0; } Services::sessions_repo()->update( $session_id, array( 'status' => 'cancelled' ) ); $count = 0; foreach ( Services::bookings()->search( array( 'session_id' => $session_id, 'status' => array( 'awaiting_payment', 'pending', 'confirmed' ), 'per_page' => 500, ) )['items'] as $booking ) { try { Services::checkout()->cancel( $booking['id'], 'business', $booking['paid'], true, $reason ); ++$count; } catch ( BookingException ) { continue; } } /** * Fires after a session is cancelled. * * @param array $session Session. */ do_action( 'schedulereed_session_cancelled', $session ); \ScheduleReed\Cache::bump(); return $count; } }