<?php

// services names
$servTranslations=servicesCfg_servicesTranslations();


///////////////////////////////////////////////////////////////////////////////////////////////////
// ADD EXTRA INFO TO EACH INVOICE / INVOICE ROW ///////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

// final list of invoices (with rows grouped for each invoice)
$invoicesList=array();

// LOOP THROUGH EACH INVOICE ROW //////////////////////////////////////////////////////////////////
if (is_array($invoiceRowsList)) {
	foreach($invoiceRowsList as $id=>$thisRow) {

		$thisTableMonth=$thisRow['tableMonth'];

		// current invoice row
		$thisIdInvoice=$thisRow['idInvoice'];
		$thisIdInvoiceRow=$thisRow['idRow'];

		// ASSOCIATE INVOICE DETAILS --------------------------------------------------------------
		if (!$invoicesList[$thisIdInvoice]) {

			// invoice id
			$invoicesList[$thisIdInvoice]['idInvoice']=$thisRow['idInvoice'];

			// invoice number
			$invoicesList[$thisIdInvoice]['invoiceNumber']=$thisRow['invoiceNumber'];

			// invoice date
			$invoicesList[$thisIdInvoice]['invoiceDate']=$thisRow['invoiceDate'];

			// invoice supplier
			$invoicesList[$thisIdInvoice]['idSupplier']=$thisRow['idSupplier'];
			$invoicesList[$thisIdInvoice]['supplierName']=\Entity\Supplier::select($thisRow['idSupplier'])->alias;

			// invoice currency
			$invoicesList[$thisIdInvoice]['currency']=$thisRow['currency'];

			// invoice statusOp
			$invoicesList[$thisIdInvoice]['statusOp']=$thisRow['statusOp'];

			// invoice blockTime
			$invoicesList[$thisIdInvoice]['blockTime']=$thisRow['blockTime'];

			// get invoice total rows count
			$invRowsCntSQL="SELECT COUNT(*) FROM `$DBTSupplierInvoiceRows$thisTableMonth` WHERE idInvoice='$thisIdInvoice'";
			$invoicesList[$thisIdInvoice]['rowsCountTotal']=$db->GetOne($invRowsCntSQL);
			if ($db->ErrorNo()) {
				writeLog("Could not get invoice rows count. SQL: ".$invRowsCntSQL, 'events', 9);
			}
		}

		// ASSOCIATE INVOICE ROW DETAILS ----------------------------------------------------------

		// invoice row id
		$invoicesList[$thisIdInvoice]['rowsList'][$thisIdInvoiceRow]['id']=$thisRow['idRow'];

		// description
		$invoicesList[$thisIdInvoice]['rowsList'][$thisIdInvoiceRow]['description']=$thisRow['description'];

		// service name
		$invoicesList[$thisIdInvoice]['rowsList'][$thisIdInvoiceRow]['serviceName']=$servTranslations[$thisRow['serviceCode']];

		// passenger
		$psgDet=getCustomerDetails($thisRow['idPassenger']);
		$invoicesList[$thisIdInvoice]['rowsList'][$thisIdInvoiceRow]['idPassenger']=$thisRow['idPassenger'];
		$invoicesList[$thisIdInvoice]['rowsList'][$thisIdInvoiceRow]['passengerName']=$psgDet['name'];

		// get balance details
		$invoicesList[$thisIdInvoice]['rowsList'][$thisIdInvoiceRow]['balanceDetails']=getBalanceBasicDetails($thisRow['idBalance']);

		// reservation details
		$invoicesList[$thisIdInvoice]['rowsList'][$thisIdInvoiceRow]['reservationId']=$thisRow['reservationId'];
		$invoicesList[$thisIdInvoice]['rowsList'][$thisIdInvoiceRow]['reservationIdConverted']=\App\Utils\Db\TablesUtils::getInstance()->switchIdReservationConversion($thisRow['reservationId'], 'converted');
		$invoicesList[$thisIdInvoice]['rowsList'][$thisIdInvoiceRow]['reservationCode']=$thisRow['reservationCode'];
		$invoicesList[$thisIdInvoice]['rowsList'][$thisIdInvoiceRow]['reservationNumber']=$thisRow['reservationNumber'];

		// ext res date/id
		$invoicesList[$thisIdInvoice]['rowsList'][$thisIdInvoiceRow]['externalReservationId']=$thisRow['externalReservationId'];
		$invoicesList[$thisIdInvoice]['rowsList'][$thisIdInvoiceRow]['externalReservationDate']=$thisRow['externalReservationDate'];

		// checkIn
		$invoicesList[$thisIdInvoice]['rowsList'][$thisIdInvoiceRow]['checkIn']=$thisRow['checkIn'];

		// contractNumber
		$invoicesList[$thisIdInvoice]['rowsList'][$thisIdInvoiceRow]['contractNumber']=$thisRow['contractNumber'];

		// ticketNo
		$invoicesList[$thisIdInvoice]['rowsList'][$thisIdInvoiceRow]['ticketNo']=$thisRow['ticketNo'];

		// supplier product name
		$invoicesList[$thisIdInvoice]['rowsList'][$thisIdInvoiceRow]['supplierServiceName']= \Entity\SupplierProduct::select($thisRow['idSupplierProduct'])->productName;

		// country & city
		$invoicesList[$thisIdInvoice]['rowsList'][$thisIdInvoiceRow]['countryCode']=$thisRow['countryCode'];
		$invoicesList[$thisIdInvoice]['rowsList'][$thisIdInvoiceRow]['countryName']=\Entity\Country::select($thisRow['countryCode'])->name;
		$invoicesList[$thisIdInvoice]['rowsList'][$thisIdInvoiceRow]['cityId']=$thisRow['cityId'];
		$invoicesList[$thisIdInvoice]['rowsList'][$thisIdInvoiceRow]['cityName']=\Entity\City::select($thisRow['cityId'])->name;

		// rowTotalValue
		$invoicesList[$thisIdInvoice]['rowsList'][$thisIdInvoiceRow]['rowTotalValue']=$thisRow['rowTotalValue'];

		// idBalance
		$invoicesList[$thisIdInvoice]['rowsList'][$thisIdInvoiceRow]['idBalance']=$thisRow['idBalance'];

		// rows count displayed
		$invoicesList[$thisIdInvoice]['rowsCountDisplayed']++;

	}

}

?>