<?php

namespace App\ClientCollection\Corporate\Document;

/**
 * This class populates the data for the extended template
 * @author Laura
 *
 */
class ExtendedXlsx extends DefaultXlsx
{

	/**
	 * The attached file name.
	 * @var string
	 */
	protected $attachmentFileName = 'Invoices list reminder.xlsx';

	/**
	 * The sheet name.
	 * @var string
	 */
	protected $sheetName = 'Invoices';


	/**
	 * The columns list and their style
	 * @var array
	 */
	protected $columns = [
	    'status'                => 'status',
	    'extSystReservationId'  => 'default',
	    'confirmationNumber'    => 'default',
	    'orderNumber'           => 'default',
	    'invoiceNumber'         => 'numeric',
	    'invoiceDate'           => 'date',
	    'invoiceDueDate'        => 'date',
	    'noOfDelayedDays'       => 'numeric',
	    'valueInvCurr'          => 'price',
	    'invoiceCurr'           => 'default',
	    'alreadyPaidInvCurr'    => 'price',
	    'amountToBePaidInvCurr' => 'price',
	    'paymentsList'          => 'default',
	];

	/**
	 * The general row style
	 * @var string
	 */
	protected $rowStyle = 'default';

	/**
	 *
	 * {@inheritDoc}
	 * @see \App\ClientCollection\Corporate\Document\DefaultXlsx::loadLang()
	 */
	protected function loadLang(): void
	{
	    \App\Loader_Lang::getInstance()->loadAppFile('_customLoad/corporateCollection/document/extendedXlsx.lang.php');
	    // we load this lang as we use it for status type name
	    \App\Loader_Lang::getInstance()->loadAppFile('_customLoad/corporateCollection/document/defaultXlsx.lang.php');
	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \App\ClientCollection\Corporate\Document\DefaultXlsx::addInvoiceDetailsRow()
	 */
	protected function addInvoiceDetailsRow(int $idInvoice, array $invoiceDetails) : array
	{
	    $row = parent::addInvoiceDetailsRow($idInvoice, $invoiceDetails);

	    $clientInvoiceRowsObj = new \App\ReportsData\EntityList\ClientInvoiceRows();
	    $clientInvoiceRowsObj->setColumns(['row.idService']);
	    $clientInvoiceRowsObj->setFilter('row.idInvoice', '=', $idInvoice);
	    $rowServicesList = $clientInvoiceRowsObj->exportRecords();
	    $serviceIdsList = array_unique(array_column($rowServicesList, 'idService'));

	    $confirmationNoList = [];
	    $extSystReservationIdList = [];
	    $orderNoList = [];

	    if ($serviceIdsList) {
	        // entity list services
	        $servicesListObj = new \App\ReportsData\EntityList\Services();

	        $servicesListObj->setColumns(['off.confirmationNumber', 'off.externalSystemReservationId', 'ord.orderNumber']);
	        $servicesListObj->setFilter('serv.id', 'IN', $serviceIdsList);
	        $servicesList = $servicesListObj->exportRecords();

	        foreach ($servicesList as $serviceDetails) {
	            $extSystReservationIdList[] = $serviceDetails['externalSystemReservationId'];
	            $confirmationNoList[] = $serviceDetails['confirmationNumber'];
	            $orderNoList[] = $serviceDetails['orderNumber'];
	        }
	    }

	    $row['extSystReservationId'] = implode(', ', array_filter($extSystReservationIdList));
	    $row['confirmationNumber'] = implode(', ', array_filter($confirmationNoList));
	    $row['orderNumber'] = implode(', ', array_unique($orderNoList));

	    return $row;
	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \App\ClientCollection\Corporate\Document\DefaultXlsx::addTotalRow()
	 */
	protected function addTotalRow(float $total, float $cashedTotal, string $currency) : array
	{
	    $row = parent::addTotalRow($total, $cashedTotal, $currency);

	    $row['extSystReservationId'] = "";
	    $row['confirmationNumber'] = "";
	    $row['orderNumber'] = "";

	    return $row;
	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \App\ClientCollection\Corporate\Document\XlsxAbstract::getColumnName()
	 */
	protected function getColumnName($columnCode): string
	{
	    $_LANG = \App\Loader_Lang::getInstance()->getAll();

	    return $_LANG['CorporateCollection']['Document']['ExtendedXlsx']['header'][$columnCode];
	}

}