<?php

namespace App\Reports\Sources\CorporateContacts;

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

	public function populate() :void
	{

		global $DBTRd_corporates, $DBTRd_corporateSegments, $DBTRd_contactPersons, $DBTRd_users, $DBTRd_offices, $DBTRd_departments;

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

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


		// UPDATE CONTACT DETAILS//////////////////////////////////////////////////////////////////////////
		$this->reportGenerationStatusUpdate('processingRecords', 'updateContactDetails', 0);

		// update contact person name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $activeColumns);
		$reportCopyInfo->populateSimple($DBTRd_contactPersons, 'cont_contactPersonName', 'cont_idContactPerson', "CONCAT(joinTable.lastName, ' ', joinTable.firstName)");

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

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

		// add user details
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addJoinTable($DBTRd_users,		'addUser',		'rep.cont_idAddUser=addUser.id',	'LEFT');
		$reportCopyInfo->addStructureColumn('cont_addUserOfficeId', 'addUser.idOffice');
		$reportCopyInfo->addStructureColumn("cont_addUserName", 	"CONCAT(addUser.firstName, ' ', addUser.lastName)", 	"addUser");
		$reportCopyInfo->populate();

		$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $activeColumns);
		$reportCopyInfo->populateSimple($DBTRd_offices, 'cont_addUserOfficeName', 'cont_addUserOfficeId', 'name');

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

		///////////////////////////////////////////////////////////////////////////////////////////////////
		// UPDATE CORPORATE DETAILS////////////////////////////////////////////////////////////////////////
		///////////////////////////////////////////////////////////////////////////////////////////////////
		$this->reportGenerationStatusUpdate('processingRecords', 'updateCorpDetails', 0);
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addJoinTable($DBTRd_corporates,       'corp',        'rep.corp_id=corp.id', 	'LEFT');

		$cols = [
			'corp_alias'			=> 'corp.alias',
			'corp_companyName'		=> 'corp.companyNameStd',
			'corp_locked'			=> 'corp.locked',
			'corp_lockedComments'	=> 'corp.lockedComments',
			'corp_status'			=> 'corp.status',
			'corp_idSegment'		=> 'corp.idSegment',
			'corp_idAccountManager'	=> 'corp.idAccountManager',

		];
		$reportCopyInfo->setStructureColumns($cols);
		$reportCopyInfo->populate();

		// update segment name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $activeColumns);
		$reportCopyInfo->populateSimple($DBTRd_corporateSegments, 'corp_segmentName', 'corp_idSegment', 'name');

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


	}

}

