<?php

namespace App\ReportsData\Populate\Entity;

class ClientInvoiceCashing extends \App\ReportsData\Populate\GeneralAbstract
{

	/**
	 * Returns the table name that will store the current entity queue.
	 * @return string
	 */
	protected function getDbTableName()
	{
		global $DBTRd_clientInvoiceCashings;
		return $DBTRd_clientInvoiceCashings;
	}


	/**
	 * {@inheritDoc}
	 * @see \App\ReportsData\Populate\GeneralAbstract::getEntityFields()
	 */
	protected function getEntityFields()
	{

		// get current entity details
		$entity = \Entity\InvoiceCashing::select($this->id);

		// for non existing rows - return null
		if (!$entity->recordExists()) {
			return null;
		}

		// get default currency rate
		$defaultCurrency = \App\CurrencyExchange\Date::getInstance($entity->docDate)->getMarketCurrency();
		$workCurrency = \App\Config\Currencies::getInstance()->getWorkCurrency();
		$currencyRate = \App\CurrencyExchange\Rates::getInstance($entity->docDate)->convert(1, $entity->currency, $defaultCurrency, null);
		$workCurrencyRate = \App\CurrencyExchange\Rates::getInstance($entity->docDate)->convert(1, $workCurrency, $defaultCurrency, null);

		// populate the entity details
		$return = [
			'id'					=> $this->id,
			'idInvoice'				=> $entity->idInvoice,
			'idCashingSeries'		=> $entity->idCashingSeries,
			'docType'				=> $entity->docType,
			'docDate'				=> $entity->docDate,
			'docNumber'				=> $entity->docNumber,
			'value'					=> $entity->value,
			'currency'				=> $entity->currency,
			'currencyRate'			=> $currencyRate,
			'workCurrencyRate'		=> $workCurrencyRate,
			'invoiceCashedValue'	=> $entity->invoiceCashedValue,
			'idBankAccount'			=> $entity->idBankAccount,
			'explanations'			=> $entity->explanations,
			'comments'				=> $entity->comments,
			'idImportedDoc'			=> $entity->idImportedDoc,
			'remarks'				=> $entity->remarks,
			'processedRemarks'		=> ($entity->processedRemarks ? json_encode($entity->processedRemarks) : ''),
			'genDocsAssoc'			=> ($entity->generatedDocumentsAssoc ? json_encode($entity->generatedDocumentsAssoc) : ''),
			'extraInfo'				=> ($entity->extraInfo ? json_encode($entity->extraInfo) : ''),
			'idUser'				=> $entity->idUser,
			'idOffice'				=> $entity->idOffice,
			'idIssueUser'			=> $entity->idIssueUser,
			'issueTime'				=> ( $entity->issueTime ? \Engine\DateTime::createFromTimestamp($entity->issueTime)->formatDatetimeIso() : null ),
			'voidReason'			=> $entity->voidReason,
			'idVoidUser'			=> $entity->idVoidUser,
			'voidTime'				=> ( $entity->voidTime ? \Engine\DateTime::createFromTimestamp($entity->voidTime)->formatDatetimeIso() : null ),
			'idBlockUser'			=> $entity->idBlockUser,
			'blockTime'				=> ( $entity->blockTime ? \Engine\DateTime::createFromTimestamp($entity->blockTime)->formatDatetimeIso() : null ),
			'idAddUser'				=> $entity->idAddUser,
			'addTime'				=> \Engine\DateTime::createFromTimestamp($entity->addTime)->formatDatetimeIso(),
			'idUpdateUser'			=> $entity->idUpdateUser,
			'lastUpdateTime'		=> ( $entity->lastUpdateTime ? \Engine\DateTime::createFromTimestamp($entity->lastUpdateTime)->formatDatetimeIso() : null ),
			'updatesCount'			=> $entity->updatesCount,

		];

		// ADD POPULATING REFERENCES --------------------------------------------------------------
		$this->addPopulatingReference('ClientInvoice', $entity->idInvoice);
		if ($entity->idImportedDoc) {
			$this->addPopulatingReference('ImportedDocument', $entity->idImportedDoc);
		}

		return $return;
	}

}
