<?php

namespace App\Reports\Sources\CorporateContracts;


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

	/**
	 * {@inheritDoc}
	 * @see \App\Reports\Sources\PopulateAbstract::populate()
	 */
	public function populate() :void
	{

		// INITS //////////////////////////////////////////////////////////////////////////////////

		global $DBTRd_corporateContracts, $DBTRd_corporates, $DBTRd_users;

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

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

		$stdFilters = $this->filtersManager->getStdFilters();


		// CREATE SQL FOR COPYING CORPORATE CONTACT PERSONS LIST //////////////////////////////////

		// refresh report status
		$this->reportGenerationStatusUpdate('extractMainDataFromDb', 'runningMainSQL', 0);

		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Insert($reportTable);
		$reportCopyInfo->setMainTable($DBTRd_corporateContracts,	'cont');
		$reportCopyInfo->addJoinTable($DBTRd_corporates,       	'corp',        		'cont.idCorporate=corp.id', 		'LEFT');
		$reportCopyInfo->addJoinTable($DBTRd_users,             'user',     		'cont.idResponsibleUser=user.id', 	'LEFT');

		$cols = [
			'cont_id'				=> 'cont.id',
			'cont_number'			=> 'cont.contractNumber',
			'cont_date'				=> 'cont.contractDate',
			'cont_typeCode'			=> 'cont.type',
			'cont_expireDate'		=> 'cont.expireDate',
			'cont_active'			=> 'cont.active',
			'cont_serviceCode'		=> 'cont.serviceCode',
			'cont_content'			=> 'cont.content',
			'cont_fileName'			=> 'cont.fileName',

			'user_respIdUser'		=> 'cont.idResponsibleUser',
			'user_respIdOffice' 	=> 'user.idOffice',
			'user_respIdDepartment' => 'user.idDepartment',

			'corp_idOriginal'		=> 'cont.idCorporate',

			'user_idAddUser'		=> 'cont.idAddUser',
			'user_idUpdateUser'		=> 'cont.idUpdateUser',

			'cont_addTime'			=> 'cont.addTime',
			'cont_lastUpdateTime'	=> 'cont.lastUpdateTime',
			'cont_updatesCount'		=> 'cont.updatesCount',

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

		$reportCopyInfo->addStructureColumn("corp_id", 					"IF(corp.idRealCorporate, corp.idRealCorporate, corp.id)", 	"corp");
		$reportCopyInfo->addStructureColumn("corp_duplicated", 			"IF(corp.idRealCorporate, 1, 0)", 							"corp");
		$reportCopyInfo->addStructureColumn("user_respUserName", 		"CONCAT(user.firstName, ' ', user.lastName)", 				"user");

		$reportCopyInfo->setActiveColumns($activeColumns);


		// ADD FILTERS ////////////////////////////////////////////////////////////////////////////

		$reportCopyInfo->addFilter('cont.deleted', '=', 0);

		// contract date
		if ($stdFilters['contractDate']['start']) {
			$reportCopyInfo->addFilter('cont.contractDate', '>=', $stdFilters['contractDate']['start']);
		}
		if ($stdFilters['contractDate']['end']) {
			$reportCopyInfo->addFilter('cont.contractDate', '<=', $stdFilters['contractDate']['end']);
		}

		// expire date
		if ($stdFilters['expireDate']['start']) {
			$reportCopyInfo->addFilter('cont.expireDate', '>=', $stdFilters['expireDate']['start']);
		}
		if ($stdFilters['expireDate']['end']) {
			$reportCopyInfo->addFilter('cont.expireDate', '<=', $stdFilters['expireDate']['end']);
		}

		// add date
		if ($stdFilters['addDate']['start']) {
			$reportCopyInfo->addFilter('cont.addTime', '>=', $stdFilters['addDate']['start']." 00:00:00");
		}
		if ($stdFilters['addDate']['end']) {
			$reportCopyInfo->addFilter('cont.addTime', '<=', $stdFilters['addDate']['end']." 23:59:59");
		}

		// contract responsible user
		if ($stdFilters['idUsers']) {
			$reportCopyInfo->addFilter('cont.idResponsibleUser' , 'IN', $stdFilters['idUsers']);
		}
		if ($stdFilters['idOffices']) {
			$reportCopyInfo->addFilter('user.idOffice' , 'IN', $stdFilters['idOffices']);
		}

		// corporate
		if ($stdFilters['idCorporates']) {
			$reportCopyInfo->addFilter('cont.idCorporate', 'IN', $this->filtersManager->getDuplicateEntitiesGroup('idCorporates'));
		}

		$reportCopyInfo->populate();

	}

}
