<?php

namespace App\Reports\Sources\CorporateOffices;


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

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

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

		global $DBTRd_corporateOffices, $DBTRd_corporates, $DBTRd_users;

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

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

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


		// CREATE SQL FOR COPYING CORPORATE OFFICES LIST //////////////////////////////////////////

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

		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Insert($reportTable);
		$reportCopyInfo->setMainTable($DBTRd_corporateOffices,	'office');
		$reportCopyInfo->addJoinTable($DBTRd_corporates,		'corp',		'corp.id=office.idCorporate');
		$reportCopyInfo->addJoinTable($DBTRd_users,				'addUser',	'office.idAddUser=addUser.id', 'LEFT');

		$cols = [
			'office_id'					=> 'office.id',
			'office_name'				=> 'office.name',
			'office_main'				=> 'office.mainOffice',
			'office_countryCode'		=> 'office.countryCode',
			'office_cityId'				=> 'office.cityId',
			'office_address'			=> 'office.address',
			'office_deliveryAddress'	=> 'office.deliveryAddress',
			'office_zipCode'			=> 'office.zipCode',
			'office_regNo'				=> 'office.regNo',
			'office_telephone'			=> 'office.telephone',
			'office_fax'				=> 'office.fax',
			'office_email'				=> 'office.email',
			'office_comments'			=> 'office.comments',
			'office_remarks'			=> 'office.remarks',
			'office_processedRemarks'	=> 'office.processedRemarks',

			'office_idAddUser'			=> 'office.idAddUser',
			'office_addOfficeId'		=> 'addUser.idOffice',
			'office_addTime'			=> 'office.addTime',
			'office_lastUpdateTime'		=> 'office.lastUpdateTime',
			'office_idUpdateUser'		=> 'office.idUpdateUser',
			'office_updatesCount'		=> 'office.updatesCount',

			'corp_id'					=> 'corp.id',
			'corp_alias'				=> 'corp.alias',
			'corp_companyNameStd'		=> 'corp.companyNameStd',
			'corp_companyPrefixStd'		=> 'corp.companyPrefixStd',
			'corp_companySuffixStd'		=> 'corp.companySuffixStd',
			'corp_companyNameLoc'		=> 'corp.companyNameLoc',
			'corp_companyPrefixLoc'		=> 'corp.companyPrefixLoc',
			'corp_companySuffixLoc'		=> 'corp.companySuffixLoc',
			'corp_typeCode'				=> 'corp.type',
			'corp_status'				=> 'corp.status',
			'corp_comments'				=> 'corp.comments',
			'corp_remarks'				=> 'corp.remarks',
			'corp_processedRemarks'		=> 'corp.processedRemarks',
			'corp_locked'				=> 'corp.locked',
			'corp_lockedComments'		=> 'corp.lockedComments',
			'corp_idSegment'			=> 'corp.idSegment',
			'corp_idAccountManager'		=> 'corp.idAccountManager',
			'corp_idCollectionResp'		=> 'corp.idCollectionResponsible',
			'corp_idInvoiceResp'		=> 'corp.idInvoiceResponsible',

			'trust_id'					=> 'corp.idTrust',
			'domain_id'					=> 'corp.idDomain',
			'domainCategory_id'			=> 'corp.idDomainCategory',

			'corp_accountingNumber'		=> 'corp.accountingNumber',
			'corp_registryAddress'		=> 'corp.registryAddress',
			'corp_regNo'				=> 'corp.regNo',
			'corp_fiscalCode'			=> 'corp.fiscalCode',
			'corp_vatRegistered'		=> 'corp.vatRegistered',
			'corp_vatCertificateNumber'	=> 'corp.vatCertificateNumber',
			'corp_incomeTaxPayer'		=> 'corp.incomeTaxPayer',
			'corp_liablePerson'			=> 'corp.liablePerson',

			'corp_telephone'			=> 'corp.telephone',
			'corp_fax'					=> 'corp.fax',
			'corp_email'				=> 'corp.email',
			'corp_website'				=> 'corp.website',

			'realCorp_id'				=> 'corp.idRealCorporate',

			'corp_addTime'				=> 'corp.addTime',
			'corp_idAddUser'			=> 'corp.idAddUser',
			'corp_lastUpdateTime'		=> 'corp.lastUpdateTime',
			'corp_idUpdateUser'			=> 'corp.idUpdateUSer',
			'corp_updatesCount'			=> 'corp.updatesCount',
		];

		$reportCopyInfo->setStructureColumns($cols);
		$reportCopyInfo->addStructureColumn("corp_companyFullStd", 		"TRIM(CONCAT(corp.companyPrefixStd, ' ',corp.companyNameStd, ' ', corp.companySuffixStd))", "corp");
		$reportCopyInfo->addStructureColumn("corp_companyFullLoc", 		"TRIM(CONCAT(corp.companyPrefixLoc, ' ',corp.companyNameLoc, ' ', corp.companySuffixLoc))", "corp");
		$reportCopyInfo->addStructureColumn("corp_duplicated", 			"IF(corp.idRealCorporate, 1, 0)", "corp");
		$reportCopyInfo->addStructureColumn("office_addUserName", 		"CONCAT(addUser.lastName, ' ', addUser.firstName)", 'addUser');

		$reportCopyInfo->setActiveColumns($activeColumns);

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

		// filter deleted corporated as the deleted ones can also be found in rd tables
		$reportCopyInfo->addFilter('office.deleted', '=', 0);

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

		// corp type
		if ($stdFilters['corporateType'] and count($stdFilters['corporateType']) == 1) {
			$reportCopyInfo->addFilter('corp.type', 'IN', $stdFilters['corporateType']);
		}

		// addOfficeUser
		if ($stdFilters['idAddUsers']) {
			$reportCopyInfo->addFilter('office.idAddUser' , 'IN', $stdFilters['idAddUsers']);
		}
		if ($stdFilters['idAddOffices']) {
			$reportCopyInfo->addFilter('addUser.idOffice' , 'IN', $stdFilters['idAddOffices']);
		}

		// corp ids
		if ($stdFilters['idCorporates']) {
			$reportCopyInfo->addFilter('corp.id', 'IN', $this->filtersManager->getDuplicateEntitiesGroup('idCorporates'));
		}

		// filter by corporate groups
		if ($stdFilters['corporateGroups']) {
			$corporateGroupsHelper = new \App\ReportsData\Helpers\PartnerGroups\Corporate();
			$corporateGroupsHelper->setGroupElementsLinear($stdFilters['corporateGroups']);
			$corpIds = $corporateGroupsHelper->getPartnerIdsList();
			if ($corpIds) {
				$reportCopyInfo->addFilter('corp.id', 'IN', $corpIds);
			} else {
				$reportCopyInfo->addCustomFilter("0");
			}
		}

		$reportCopyInfo->populate();

	}

}
