<?php

namespace App\Reports\Sources\Customers;

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

	public function populate() :void
	{

		// INITIALIZATIONS ////////////////////////////////////////////////////////////////////////
		$reportTable = $this->tableManager->getTableName();
		$activeColumns = array_keys( $this->columnsManager->getStdColumns() );

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

		global $DBTRd_customers, $DBTRd_customerSegments, $DBTRd_customerPassports, $DBTRd_countries, $DBTRd_cities;
		global $DBTRd_corporates, $DBTRd_departments, $DBTRd_offices, $DBTRd_users;

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


		///////////////////////////////////////////////////////////////////////////////////////////
		// UPDATE CUSTOMER DETAILS ////////////////////////////////////////////////////////////////
		///////////////////////////////////////////////////////////////////////////////////////////
		$this->reportGenerationStatusUpdate('extractMainDataFromDb', 'updateCustDetails', 0);

		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addJoinTable($DBTRd_customers,	'realCust',		'rep.cust_idRealCustomer=realCust.id',	'LEFT');

		$cols = [
			'cust_lastNameReal'			=> 'realCust.lastNameStd',
			'cust_middleNameReal'		=> 'realCust.middleNameStd',
			'cust_firstNameReal'		=> 'realCust.firstNameStd',
		];
		$reportCopyInfo->setStructureColumns($cols);
		$reportCopyInfo->populate();

		// update the segment name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $activeColumns);
		$reportCopyInfo->populateSimple($DBTRd_customerSegments, 'cust_segmentName', 'cust_idSegment', 'name');

		// update the title name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addCaseStructureColumn("cust_titleName", "cust_titleCode", (Array)$_LANG['generalData']['personTitles']);
		$reportCopyInfo->populate();

		// update the type name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addCaseStructureColumn("cust_typeName", "cust_typeCode", (Array)$_LANG['generalData']['personTypes']);
		$reportCopyInfo->populate();

		// update passport details
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addJoinTable($DBTRd_customerPassports,	'pass',		'rep.cust_id=pass.idCustomer',	'LEFT');

		$cols = [
			'pass_number' 		=> 'pass.number',
			'pass_typeCode' 	=> 'pass.type',
			'pass_expireDate' 	=> 'pass.expireDate',
			'pass_issueDate' 	=> 'pass.issueDate',
			'pass_issuedBy' 	=> 'pass.issuedBy',
		];
		$reportCopyInfo->setStructureColumns($cols);
		$reportCopyInfo->addFilter('pass.defaultPassport', '!=', '0');

		$reportCopyInfo->populate();

		// update passport type
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addCaseStructureColumn("pass_typeName", "pass_typeCode", (Array)$_LANG['generalData']['passportTypes']['type']);
		$reportCopyInfo->populate();

		// update country details
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addJoinTable($DBTRd_countries,       'countries',        'rep.cust_countryCode=countries.code', 		'LEFT');

		$cols = [
			'cust_countryName'			=> 'countries.nameStd',
			'cust_countryZoneCode'		=> 'countries.zone',
		];
		$reportCopyInfo->setStructureColumns($cols);
		$reportCopyInfo->populate();

		// update city details
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addJoinTable($DBTRd_cities,         	'cities',        	'rep.cust_cityId=cities.id', 				'LEFT');

		$cols = [
			'cust_cityName'				=> 'cities.nameStd',
			'cust_cityDistrict'			=> 'cities.districtStd',
			'cust_cityRegion'			=> 'cities.regionStd',
		];
		$reportCopyInfo->setStructureColumns($cols);
		$reportCopyInfo->populate();

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

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

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


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

		$cols = [
			'corp_alias'	=> 'corp.alias',
		];
		$reportCopyInfo->setStructureColumns($cols);
		$reportCopyInfo->addStructureColumn("corp_companyFull", 	"TRIM(CONCAT(corp.companyNameStd, ' ', corp.companySuffixStd))", 	"corp");
		$reportCopyInfo->populate();

		// update client type
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addJoinTable($DBTRd_corporates,	'corp',		'rep.corp_idCorporate=corp.id',	'LEFT');
		$reportCopyInfo->addStructureColumn("client_typeCode", 		"IF(rep.corp_idCorporate, corp.type, 'DC')", "corp");
		$reportCopyInfo->populate();


		// REFILTER BY CLIENT TYPE ////////////////////////////////////////////////////////////////
		// one option client type
		if ($stdFilters['clientType']??null and count($stdFilters['clientType'])==1) {
			if (in_array('CC', $stdFilters['clientType'])) {
				$reportCopyInfo = new \App\Reports\Db\ManageRecords\Delete($reportTable);
				$reportCopyInfo->addFilter('client_typeCode', '!=', 'CC');
				$reportCopyInfo->populate();
			}
			elseif (in_array('SA', $stdFilters['clientType'])) {
				$reportCopyInfo = new \App\Reports\Db\ManageRecords\Delete($reportTable);
				$reportCopyInfo->addFilter('client_typeCode', '!=', 'SA');
				$reportCopyInfo->populate();
			}
		}
		// two options client type
		if ($stdFilters['clientType']??null and count($stdFilters['clientType'])==2) {
			if (!in_array('CC', $stdFilters['clientType'])) {
				$reportCopyInfo = new \App\Reports\Db\ManageRecords\Delete($reportTable);
				$reportCopyInfo->addFilter('client_typeCode', '=', 'CC');
				$reportCopyInfo->populate();
			}
			elseif (!in_array('SA', $stdFilters['clientType'])) {
				$reportCopyInfo = new \App\Reports\Db\ManageRecords\Delete($reportTable);
				$reportCopyInfo->addFilter('client_typeCode', '=', 'SA');
				$reportCopyInfo->populate();
			}
		}

		// update the client type name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addCaseStructureColumn("client_typeName", "client_typeCode", (Array)$_LANG['generalData']['clientType']);
		$reportCopyInfo->populate();

	}

}

