<?php

// all the filters (supplier) from request
$reqFilters=$R['supplier'];

if (isset($reqFilters['idReservation'])) {
	$S['balanceInvoices']['supplier']['idReservation']=$reqFilters['idReservation'];

	$idsList = explode(',', $reqFilters['idReservation']);
	$arr = array();
	foreach ($idsList as $id) {
		$idOrder = \App\Utils\Db\TablesUtils::getInstance()->switchIdReservationConversion(trim($id), 'normal');
		$arr[] = \App\Utils\Db\TablesUtilsCached::getInstance()->getYmFromId($idOrder);
	}

	$arr = array_unique($arr);

	$reqFilters['invoiceIdOfficeUser'] 	= '0|0';
	$reqFilters['invoiceStartDate'] 	= \Engine\DateTime::createFromIsoDate(sprintf('%s-%s-01', substr(min($arr), 0, 4), substr(min($arr), -2)))->sub(new \DateInterval("P3M"))->formatDateStd();



	$invoiceEndIso = \Engine\DateTime::createFromIsoDate(sprintf('%s-%s-01', substr(max($arr), 0, 4), substr(max($arr), -2)))->add(new \DateInterval("P9M"))->formatDateIso();
	if ($invoiceEndIso>TODAY_ISO) {
		$invoiceEndIso = TODAY_ISO;
	}
	$invoiceEndDate = \Engine\DateTime::isoToStd($invoiceEndIso);

	$reqFilters['invoiceEndDate'] = $invoiceEndDate;

}
if (isset($reqFilters['idReservationType']))
	$S['balanceInvoices']['supplier']['idReservationType']=$reqFilters['idReservationType'];


// show advanced filters
if (isset($reqFilters['advancedFilters']))
	$S['balanceInvoices']['supplier']['advancedFilters']=$reqFilters['advancedFilters'];

// invoice date
if (isset($reqFilters['invoiceStartDate']))
	$S['balanceInvoices']['supplier']['invoiceStartDate']=$reqFilters['invoiceStartDate'];
if (isset($reqFilters['invoiceEndDate']))
	$S['balanceInvoices']['supplier']['invoiceEndDate']=$reqFilters['invoiceEndDate'];

// invoice number
if (isset($reqFilters['invoiceNumber']))
	$S['balanceInvoices']['supplier']['invoiceNumber']=$reqFilters['invoiceNumber'];

// invoice responsible
if (isset($reqFilters['invoiceIdOfficeUser']))
	$S['balanceInvoices']['supplier']['invoiceIdOfficeUser']=$reqFilters['invoiceIdOfficeUser'];
if (isset($reqFilters['typeInvoiceOfficeUserName']))
	$S['balanceInvoices']['supplier']['typeInvoiceOfficeUserName']=$reqFilters['typeInvoiceOfficeUserName'];

// supplier and service id
if (isset($reqFilters['idSupplierAndService']))
	$S['balanceInvoices']['supplier']['idSupplierAndService']=$reqFilters['idSupplierAndService'];
if (isset($reqFilters['typeSupplierAndServiceName']))
	$S['balanceInvoices']['supplier']['typeSupplierAndServiceName']=$reqFilters['typeSupplierAndServiceName'];

// service code
if (isset($reqFilters['serviceCode']))
	$S['balanceInvoices']['supplier']['serviceCode']=$reqFilters['serviceCode'];

// balance status
if (isset($reqFilters['balanceStatus']))
	$S['balanceInvoices']['supplier']['balanceStatus']=$reqFilters['balanceStatus'];

// text filter
if (isset($reqFilters['text']))
	$S['balanceInvoices']['supplier']['text']=$reqFilters['text'];

// invoice due date
if (isset($reqFilters['dueStartDate']))
	$S['balanceInvoices']['supplier']['dueStartDate']=$reqFilters['dueStartDate'];
if (isset($reqFilters['dueEndDate']))
	$S['balanceInvoices']['supplier']['dueEndDate']=$reqFilters['dueEndDate'];

// invoice status
if (isset($reqFilters['status']))
	$S['balanceInvoices']['supplier']['status']=$reqFilters['status'];

// external res date/id
if (isset($reqFilters['extResDate']))
	$S['balanceInvoices']['supplier']['extResDate']=$reqFilters['extResDate'];
if (isset($reqFilters['extResId']))
	$S['balanceInvoices']['supplier']['extResId']=$reqFilters['extResId'];

// passenger
if (isset($reqFilters['idPassenger']))
	$S['balanceInvoices']['supplier']['idPassenger']=$reqFilters['idPassenger'];
if (isset($reqFilters['typePassengerName']))
	$S['balanceInvoices']['supplier']['typePassengerName']=$reqFilters['typePassengerName'];

// destination
if (isset($reqFilters['idDestination']))
	$S['balanceInvoices']['supplier']['idDestination']=$reqFilters['idDestination'];
if (isset($reqFilters['typeDestinationName']))
	$S['balanceInvoices']['supplier']['typeDestinationName']=$reqFilters['typeDestinationName'];

// check in
if (isset($reqFilters['serviceStartDate']))
	$S['balanceInvoices']['supplier']['serviceStartDate']=$reqFilters['serviceStartDate'];
if (isset($reqFilters['serviceEndDate']))
	$S['balanceInvoices']['supplier']['serviceEndDate']=$reqFilters['serviceEndDate'];

// ticket no
if (isset($reqFilters['ticketNo']))
	$S['balanceInvoices']['supplier']['ticketNo']=$reqFilters['ticketNo'];

// mice project
if (isset($reqFilters['idMiceProject']))
	$S['balanceInvoices']['supplier']['idMiceProject']=$reqFilters['idMiceProject'];
if (isset($reqFilters['typeMiceProjectName']))
	$S['balanceInvoices']['supplier']['typeMiceProjectName']=$reqFilters['typeMiceProjectName'];


// UPDATE ID RESERVATION FILTER -------------------------------------------------------------------
if ($reqFilters['changeIdRes']=="number") {

	// update the id reservation type
	$S['balanceInvoices']['supplier']['idReservationType']="number";

	// GET RESERVATION NUMBERS FROM THEIR IDS
	// list of reservation ids
	$resIds=explode(",", $S['balanceInvoices']['supplier']['idReservation']);
	$resIdFinal=array();

	// scan ids and get res number
	foreach($resIds as $thisId) {
		$thisResNo=\Entity\Order::select( \App\Utils\Db\TablesUtils::getInstance()->switchIdReservationConversion($thisId, 'normal') )->reservationNumber;
		if ($thisResNo) $resIdFinal[]=$thisResNo;
	}
	// update the filter in session
	$S['balanceInvoices']['supplier']['idReservation']=implode(", ", $resIdFinal);

}

?>