<?php

namespace App\EntityList;

class OrderServiceOffers extends GeneralMtAbstract
{

	/**
	 * The class constructor
	 */
	public function __construct()
	{

		// store the table name (without the YYYYMM terminator)
		global $DBTReservationServices, $DBTReservations, $DBTReservationServiceOffers;
		$this->tableName = $DBTReservationServiceOffers;
		$this->tableAlias = 'off';
		$this->allowedTables['off']= ['table'=>$DBTReservationServiceOffers, 'type'=>'month'];
		$this->allowedTables['serv']= ['table'=>$DBTReservationServices, 'type'=>'month', 'foreignKeyRef'=>"serv.id=off.idReservationService", 'joinType'=>'INNER'];
		$this->allowedTables['res']= ['table'=>$DBTReservations, 'type'=>'month', 'foreignKeyRef'=>"res.id=serv.idReservation", 'joinType'=>'INNER'];

		// run the parent
		parent::__construct();

		// set some default column
		$this->setColumn('off.id', 'id');
	}

	/**
	 * Returns an array of yearMonth depending on current filters
	 * @return Array
	 */
	public function getMonthTablesList(): array
	{

		// get the table months if the services were filtered by reservation id
		$tableMonths_id1=$this->getMonthTablesList_id(['off.id']);
		$tableMonths_id2=$this->getMonthTablesList_id(['off.idReservationService']);
		$tableMonths_id3=$this->getMonthTablesList_id(['serv.id']);
		$tableMonths_id4=$this->getMonthTablesList_id(['serv.idReservation']);
		$tableMonths_id5=$this->getMonthTablesList_id(['res.id']);

		// get the table months if the services were filtered by reservation date
		$tableMonths_resDate=$this->getMonthTablesList_reservationDate();

		// intersect the two lists of months
		$tableMonths=array_intersect($tableMonths_id1, $tableMonths_id2, $tableMonths_id3, $tableMonths_id4, $tableMonths_id5, $tableMonths_resDate);

		return $tableMonths;

	}

	/**
	 * Returns the months needed for reservation date filtering.
	 * @return Array of YYYYMM=>YYYYMM
	 */
	private function getMonthTablesList_reservationDate()
	{

		// get start/end date
		$period=$this->getMonthTablesList_period(['res.date']);

		// if no months needed - skip this step
		if ($period['startDate']>$period['endDate']) return [];

		// get the list of month tables
		$resMonthsList = array_keys( \App\Utils\Db\TablesUtilsCached::getInstance()->getExistingMonthTablesByInterval($this->tableName, $period['startDate'], $period['endDate']) );

		return $resMonthsList;

	}


	/**
	 * Applys the user access restrictions.
	 * @see \App\EntityList\GeneralMtAbstract::addUserRestrictionFilter()
	 */
	protected function addUserRestrictionFilter()
	{

	}

}

