<?php

namespace App\Reports\Sources\OrdersList;

class PopulateLoopRecords extends \App\Reports\Sources\PopulateLoopRecordsAbstract
{

	/**
	 * These are the standard columns that we expect are filled in during the loop records. The list
	 * may not contain anyting
	 * @var array
	 */
	protected array $processStandardColumns = [
		'ord_corporateIdsList', 'ord_corporateAliasList', 'ord_customerIdsList', 'ord_customerNamesList',
	];

	/**
	 * {@inheritDoc}
	 * @see \App\Reports\Sources\PopulateLoopRecordsAbstract::updateReportStdColumns()
	 */
	protected function updateReportStdColumns(array $currentRow) :array
	{

		// loads all the standard columns from
		$tableColumns = $this->columnsManager->getStdColumns();

		$updateColumns = [];

		if ($tableColumns['ord_corporateAliasList'] or $tableColumns['ord_corporateIdsList']) {
			$order = new \App\ReportsData\EntityFields\Order($currentRow['ord_id']);
			$idCorporates = $order->getValue('idCorporates');

			if ($tableColumns['ord_corporateIdsList']) {
				$updateColumns['ord_corporateIdsList'] = implode(', ', $idCorporates);
			}

			if ($tableColumns['ord_corporateAliasList']) {
				if ($idCorporates) {
					$corporates = [];
					foreach ($idCorporates as $idCorp) {
						$corpObj = new \App\ReportsData\EntityFields\Corporate($idCorp);
						$corporates[] = $corpObj->getValue('alias');
					}

					$updateColumns['ord_corporateAliasList'] = implode(', ', $corporates);
				}
				else {
					$updateColumns['ord_corporateAliasList'] = '';
				}
			}
		}

		if ($tableColumns['ord_customerIdsList'] or $tableColumns['ord_customerNamesList']) {
			$order = new \App\ReportsData\EntityFields\Order($currentRow['ord_id']);
			$idCustomers = $order->getValue('idCustomers');

			if ($tableColumns['ord_customerIdsList']) {
				$updateColumns['ord_customerIdsList'] = implode(', ', $idCustomers);
			}

			if ($tableColumns['ord_customerNamesList']) {
				if ($idCustomers) {
					$customers = [];
					foreach ($idCustomers as $idCust) {
						$custObj = new \App\ReportsData\EntityFields\Customer($idCust);
						$customers[] = $custObj->getValue('fullNameStd');
					}

					$updateColumns['ord_customerNamesList'] = implode(', ', $customers);
				}
				else {
					$updateColumns['ord_customerNamesList'] = '';
				}
			}
		}

		return $updateColumns;

	}

}