<?php

namespace App\ReportsData\Populate\Entity;

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

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


	/**
	 * {@inheritDoc}
	 * @see \App\ReportsData\Populate\GeneralAbstract::getEntityFields()
	 */
	protected function getEntityFields()
	{
		// get current entity details
		$entity = \Entity\CashBookDocument::select($this->id);

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

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

		// populate the entity details
		$return = [
			'id'					=> $this->id,
			'deleted'				=> $entity->deleted,
			'category'				=> $entity->category,
			'type'					=> $entity->type,
			'number'				=> $entity->number,
			'date'					=> $entity->date,
			'value'					=> $entity->value,
			'currency'				=> $entity->currency,
			'currencyRate'			=> $currencyRate,
			'workCurrencyRate'		=> $workCurrencyRate,
			'explanations'			=> $entity->explanations,
			'comments'				=> $entity->comments,
			'idBankAccount'			=> $entity->idAgencyBankAccount,
			'partner'				=> $entity->partner,
			'idUser'				=> $entity->idUser,
			'idOffice'				=> $entity->idOffice,
			'idIssueUser'			=> $entity->idIssueUser,
			'issueTime'				=> ( $entity->issueTime ? \Engine\DateTime::createFromTimestamp($entity->issueTime)->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,
			'idBlockUser'			=> $entity->idBlockUser,
			'blockTime'				=> ( $entity->blockTime ? \Engine\DateTime::createFromTimestamp($entity->blockTime)->formatDatetimeIso() : null ),
		];

		return $return;
	}

}
