<?php

namespace App\EntityList;

class SupplierInvoicePayments extends SupplierInvoices {

	/**
	 * The class constructor
	 */
	public function __construct()
	{
		// store the table name (without the YYYYMM terminator)
		global $DBTSupplierInvoicePayment, $DBTSupplierInvoices, $DBTImportedDocuments_documents;
		$this->tableName = $DBTSupplierInvoicePayment;
		$this->tableAlias = 'doc';
		$this->allowedTables['doc']		= ['table'=>$DBTSupplierInvoicePayment, 'type'=>'month'];
		$this->allowedTables['inv']		= ['table'=>$DBTSupplierInvoices, 'type'=>'month', 'foreignKeyRef'=>"doc.idInvoice=inv.id", 'joinType'=>'INNER'];
		$this->allowedTables['impDoc']	= ['table'=>$DBTImportedDocuments_documents, 'type'=>'single', 'foreignKeyRef'=>"doc.idImportedDoc=impDoc.id", 'joinType'=>'LEFT'];

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

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

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

		// get the table months if the documents were filtered by invoice id
		$tableMonths_idInv1=$this->getMonthTablesList_id(['inv.id']);
		$tableMonths_idInv2=$this->getMonthTablesList_id(['doc.idInvoice']);

		// get the table months if the documents were filtered by document id
		$tableMonths_idDoc=$this->getMonthTablesList_id(['doc.id']);

		// get the table months if the documents were filtered by invoice date
		$tableMonths_invDate=$this->getMonthTablesList_invoiceDate();

		// get the table months if the documents were filtered by document date
		$tableMonths_docDate=$this->getMonthTablesList_documentDate();

		// intersect the two lists of months
		$tableMonths=array_intersect($tableMonths_idInv1, $tableMonths_idInv2, $tableMonths_idDoc, $tableMonths_invDate, $tableMonths_docDate);

		return $tableMonths;

	}

	/**
	 * Returns the months list needed for document date filtering
	 * @return Array
	 */
	protected function getMonthTablesList_documentDate()
	{

		// get the table months if the invoice rows were filtered by invoice id
		$period=$this->getMonthTablesList_period( ['doc.docDate', 'docDate'] );

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

		// GET THE LIST OF MONTH TABLES ///////////////////////////////////////////////////////////
		$paymentTablesList = \App\Utils\Db\TablesUtilsCached::getInstance()->getRealYmFromVirtualDate('supplierInvoicePayment', $period['startDate'], $period['endDate']);

		// return the list of yearMonth tables
		return $paymentTablesList;


	}

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

	}

}

