<?php

namespace App\ReportsData\Populate\Entity;

class Supplier extends \App\ReportsData\Populate\GeneralAbstract
{
	/**
	 * The supplier groups table.
	 * @var string
	 */
	private $dbTableName_supplierGroups;

	/**
	 * Returns the table name that will store the current entity queue.
	 * @return string
	 */
	protected function getDbTableName()
	{
		global $DBTRd_suppliers, $DBTRd_supplierGroups;
		$this->dbTableName_supplierGroups = $DBTRd_supplierGroups;
		return $DBTRd_suppliers;
	}


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

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

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

		// populate the entity details
		$return = [
			'id'					=> $this->id,
			'deleted'				=> $entity->deleted,
			'locked'				=> $entity->locked,
			'alias'					=> $entity->alias,
			'companyPrefix'			=> $entity->companyPrefix,
			'companyName'			=> $entity->company,
			'companySuffix'			=> $entity->companySuffix,
			'idTrust'				=> $entity->idTrust,
			'category'				=> $entity->category,
			'address'				=> $entity->address,
			'deliveryAddress'		=> $entity->deliveryAddress,
			'zipCode'				=> $entity->zipCode,
			'countryCode'			=> $entity->countryCode,
			'cityId'				=> $entity->cityId,
			'regNo'					=> $entity->regNo,
			'fiscalCode'			=> $entity->fiscalCode,
			'vatRegistered'			=> $entity->vatRegistered,
			'vatCertificateNumber'	=> $entity->vatCertificateNumber,
			'incomeTaxPayer'		=> $entity->incomeTaxPayer,
			'accountingNumber'		=> $entity->accountingNumber,
			'bankAccounts'			=> ($entity->bankAccounts ? json_encode($entity->bankAccounts) : '' ),
			'telephone'				=> $entity->telephone,
			'fax'					=> $entity->fax,
			'email'					=> $entity->email,
			'website'				=> $entity->website,
			'comments'				=> $entity->comments,
			'remarks'				=> $entity->remarks,
			'processedRemarks'		=> ($entity->processedRemarks ? json_encode($entity->processedRemarks) : ''),
			'lockedComments'		=> $entity->lockedComments,
			'codes'					=> ( $entity->codes ? json_encode($entity->codes) : '' ),
			'iataCode'				=> $entity->codeIata,
			'tktCode'				=> $entity->codeTktCode,
			'idRealSupplier'		=> $entity->idRealSupplier,
			'addTime'				=> \Engine\DateTime::createFromTimestamp($entity->addTime)->formatDatetimeIso(),
			'idAddUser'				=> $entity->idAddUser,
			'lastUpdateTime'		=> ( $entity->lastUpdateTime ? \Engine\DateTime::createFromTimestamp($entity->lastUpdateTime)->formatDatetimeIso() : null ),
			'idUpdateUser'			=> $entity->idUpdateUser,
			'updatesCount'			=> $entity->updatesCount,
		];

		// mark duplicated suppliers as changed
		if ($entity->duplicatedSuppliersList) {
			foreach($entity->duplicatedSuppliersList as $idDuplSupp) {
				$this->addPopulatingReference('Supplier', $idDuplSupp);
			}
		}

		return $return;
	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \App\ReportsData\Populate\GeneralAbstract::populate()
	 */
	public function populate() :void
	{

		parent::populate();

		// GET GROUPS DETAILS ///////////////////////////////////////////////////////////////////
		$groups = \Entity\Supplier::select($this->id)->idGroupElements;

		// DELETE OLD GROUPS ///////////////////////////////////////////////////////////////////
		$this->dbExecuteDelete($this->dbTableName_supplierGroups, 'idSupplier', $this->id);


		// ADD GROUPS //////////////////////////////////////////////////////////////////////////
		if ($groups) {
			foreach($groups as $idGroupElement) {
				$fields = [
					'idGroupElement'	=> $idGroupElement,
					'idSupplier'		=> $this->id,
				];

				$this->dbExecuteInsert($this->dbTableName_supplierGroups, $fields);

			}
		}

	}
}
