<?php

namespace App\Reports\Sources\Corporates;


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

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

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

		global $DBTRd_users, $DBTRd_corporates;

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

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

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

		// CREATE SQL FOR COPYING CORPORATES LIST /////////////////////////////////////////////////

		// refresh report status
		$this->reportGenerationStatusUpdate('extractMainDataFromDb', 'runningMainSQL', 0);
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Insert($reportTable);
		$reportCopyInfo->setMainTable($DBTRd_corporates,	'corp');
		$reportCopyInfo->addJoinTable($DBTRd_users,			'addUser',	'corp.idAddUser=addUser.id', 'LEFT');

		$cols = [
			'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_alertLimit'			=> 'corp.creditLimit_alertLimit',
			'corp_blockLimit'			=> 'corp.creditLimit_blockLimit',

			'corp_addTime'				=> 'corp.addTime',
			'corp_idAddUser'			=> 'corp.idAddUser',
			'corp_addOfficeId'			=> 'addUser.idOffice',
			'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))");
		$reportCopyInfo->addStructureColumn("corp_companyFullLoc", 		"TRIM(CONCAT(corp.companyPrefixLoc, ' ', corp.companyNameLoc, ' ', corp.companySuffixLoc))");
		$reportCopyInfo->addStructureColumn("corp_duplicated", 			"IF(corp.idRealCorporate, 1, 0)");
		$reportCopyInfo->addStructureColumn("corp_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('corp.deleted', '=', 0);

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

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

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

		// 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();

	}

}
