<?php

namespace App\Reports\Sources\Tasks;

class PopulateQuickColumns extends \App\Reports\Sources\PopulateAbstract
{

	public function populate() :void
	{

		// INITS ----------------------------------------------------------------------------------
		global $DBTRd_users, $DBTRd_offices, $DBTRd_departments, $DBTRd_customers, $DBTRd_corporates;
		global $DBTRd_taskCategories, $DBTRd_taskOperationTypes, $DBTRd_clientInvoices;

		$reportTable = $this->tableManager->getTableName();

		$activeColumns = array_keys( $this->columnsManager->getStdColumns() );

		$_LANG = \App\Loader_Lang::getInstance()->getGlobal();

		// UPDATE TASK DETAILS ////////////////////////////////////////////////////////////////////
		$this->reportGenerationStatusUpdate('extractMainDataFromDb', 'updateTaskDetails', 0);

		// update status name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addCaseStructureColumn("task_statusName",   "task_statusCode",   (Array)$_LANG['tasks']['statusName']);
		$reportCopyInfo->populate();

		// update status reason name ( from $_LANG['tasks']['reasonForStatus'] )
		if (in_array('task_statusReasonName', $activeColumns)) {

			\Entity\Task::loadCFG();
			$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();

			foreach ($CFG['globalEntity']['::Task']['_special']['statusReasonsList'] as $status=>$reason) {
				$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
				$reportCopyInfo->addCaseStructureColumn("task_statusReasonName",   "task_statusReasonCode",   (Array)$_LANG['tasks']['reasonForStatus'][$status]);
				$reportCopyInfo->addFilter("rep.task_statusCode", "=", $status);
				$reportCopyInfo->populate();
			}
		}

		// update operation type name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $activeColumns);
		$reportCopyInfo->populateSimple($DBTRd_taskOperationTypes, 'task_operationTypeName', 'task_idOperationType', 'description');

		// update category name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $activeColumns);
		$reportCopyInfo->populateSimple($DBTRd_taskCategories, 'task_categoryName', 'task_idCategory', 'category');

		// update priority name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addCaseStructureColumn("task_priorityName",   "task_priorityCode",   (Array)$_LANG['tasks']['priorities']);
		$reportCopyInfo->populate();

		// update office name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $activeColumns);
		$reportCopyInfo->populateSimple($DBTRd_offices, 'task_officeName', 'task_idOffice', "name");

		// update department name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $activeColumns);
		$reportCopyInfo->populateSimple($DBTRd_departments, 'task_departmentName', 'task_idDepartment', "name");

		// update reponsible user name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $activeColumns);
		$reportCopyInfo->populateSimple($DBTRd_users, 'task_userName', 'task_idUser', "CONCAT(joinTable.lastName, ' ', joinTable.firstName)");

		// update assignor user name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $activeColumns);
		$reportCopyInfo->populateSimple($DBTRd_users, 'task_assignorUserName', 'task_assignorIdUser', "CONCAT(joinTable.lastName, ' ', joinTable.firstName)");

		// update add user name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $activeColumns);
		$reportCopyInfo->populateSimple($DBTRd_users, 'task_addUserName', 'task_idAddUser', "CONCAT(joinTable.lastName, ' ', joinTable.firstName)");

		// update update user name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $activeColumns);
		$reportCopyInfo->populateSimple($DBTRd_users, 'task_updateUserName', 'task_idUpdateUser', "CONCAT(joinTable.lastName, ' ', joinTable.firstName)");

		// UPDATE PIVOT DETAILS ///////////////////////////////////////////////////////////////////
		$this->reportGenerationStatusUpdate('extractMainDataFromDb', 'updatePivotDetails', 0);

		// update pivot name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addCaseStructureColumn("pivot_name",   "pivot_code",   (Array)$_LANG['tasks']['pivotsArr']);
		$reportCopyInfo->populate();

		// UPDATE CLIENT NAME ---------------------------------------------------------------------
		if (in_array('pivot_clientName', $activeColumns)) {
			// pivotCode = corporate
			$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
			$reportCopyInfo->addJoinTable($DBTRd_corporates,		'corp',			'rep.pivot_id=corp.id',	'LEFT');
			$reportCopyInfo->addStructureColumn("pivot_clientName", 	"corp.alias", 	"corp");
			$reportCopyInfo->addFilter("rep.pivot_code", "=", "corporate");
			$reportCopyInfo->populate();

			// pivotCode = invoice && idCorporate
			$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
			$reportCopyInfo->addJoinTable($DBTRd_clientInvoices,	'inv',		'rep.pivot_id=inv.id',	'LEFT');
			$reportCopyInfo->addJoinTable($DBTRd_corporates,		'corp',		'inv.idCorporate=corp.id',	'LEFT');
			$reportCopyInfo->addStructureColumn("pivot_clientName", 	"corp.alias", 	"corp");
			$reportCopyInfo->addFilter("rep.pivot_code", "=", "invoice");
			$reportCopyInfo->addFilter("inv.idCorporate", "!=", "0");
			$reportCopyInfo->populate();

			// pivotCode = invoice && idCustomer
			$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
			$reportCopyInfo->addJoinTable($DBTRd_clientInvoices,	'inv',		'rep.pivot_id=inv.id',	'LEFT');
			$reportCopyInfo->addJoinTable($DBTRd_customers,			'cust',		'inv.idCustomer=cust.id',	'LEFT');
			$reportCopyInfo->addStructureColumn("pivot_clientName", 	"CONCAT(cust.lastNameStd, ' ', cust.firstNameStd)", 	"cust");
			$reportCopyInfo->addFilter("rep.pivot_code", "=", "invoice");
			$reportCopyInfo->addFilter("inv.idCustomer", "!=", "0");
			$reportCopyInfo->populate();
		}

	}

}

