<?php

namespace App\ReportsData\Populate\Entity;

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

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


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

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

		// for preview docs - skip this record
		if ($entity->preview) {
			return null;
		}

		// CURRENTLY GENERATED DOCUMENT ///////////////////////////////////////////////////////////

		// get generation history
		$history = $entity->history;
		$genHis = $history[0];
		$emailDetails = [];
		foreach (array_reverse($history) as $data) {
			if ($data['op'] == 'e') {
				$emailDetails = $data['details'];
				$emailDetails['time'] = $data['t'];
			}
		}

		// get custom text from fieldsDetails
		$fieldsDetails = $entity->fieldsDetails;
		$customTextArr = [];
		if ($fieldsDetails) {
			foreach ($fieldsDetails as $name => $field) {
				if (strpos($name, 'customText') !== FALSE) {
					$customTextArr[$name] = [
						'name' 	=> $field['n'],
						'value' => $field['val'],
					];
				}
			}
		}



		// populate the entity details
		$return = [
			'id'				=> $this->id,
			'pivot'				=> $entity->pivotCode,
			'idPivot'			=> $entity->idPivot,
			'docType'			=> $entity->docTypeCode,
			'idTemplate'		=> $entity->idTemplate,
			'idDocSeries'		=> $entity->idDocumentSeries,
			'format'			=> $entity->format,
			'docNumber'			=> $entity->docNumber,
			'docDate'			=> $entity->docDate,
			'generation'		=> $entity->generationCode,
			'preview'			=> $entity->preview,
			'comments'			=> $entity->comments,
			'idAutoProcessRule'	=> $entity->idAutoProcessRule,
			'idAddUser'			=> (Int)$genHis['us'], // addDateTime - some problems appear because of invalid serialization of history field
			'addTime'			=> ($entity->addDateTime?:($entity->docDate." 00:00:00")), // addDateTime - some problems appear because of invalid serialization of history field
			'emailDetails'		=> $emailDetails ? json_encode($emailDetails) : '',
			'fieldsDetails'		=> $customTextArr ? json_encode($customTextArr) : '',
			'extraInfo'			=> ($entity->extraInfo ? json_encode($entity->extraInfo) : ''),
		];

		// ADD REFERENCES THAT ARE UPDATED DURING DOC ISSUE ///////////////////////////////////////
		switch ($entity->pivotCode) {
			case 'invoice':
				$this->addPopulatingReference('ClientInvoice', $entity->idPivot);
			break;
			case 'order':
				$this->addPopulatingReference('Order', $entity->idPivot);
			break;
			case 'service':
				$this->addPopulatingReference('Service', $entity->idPivot);
			break;
			case 'cashing':
				$this->addPopulatingReference('ClientInvoiceCashing', $entity->idPivot);
			break;
			case 'payment':
				$this->addPopulatingReference('SupplierInvoicePayment', $entity->idPivot);
			break;
			case 'importedDocument':
				$this->addPopulatingReference('ImportedDocument', $entity->idPivot);
			break;
		}

		return $return;
	}

}
