<?php

namespace App\Reports\Sources\ClientInvoices;

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

	public function populate() :void
	{

 	global $DBTRd_clientInvoiceCashings, $DBTRd_corporates, $DBTRd_trusts, $DBTRd_corporateSegments, $DBTRd_users, $DBTRd_customers, $DBTRd_customerSegments, $DBTRd_countries, $DBTRd_cities, $DBTRd_activityDomains, $DBTRd_activityDomainCategories;

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

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

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

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


		// ADD LAST CASHING DOC TYPE //////////////////////////////////////////////////////////////
		$this->reportGenerationStatusUpdate('extractMainDataFromDb', 'updateLastCashingDocType', 0);

		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addStructureColumn('lastCashingDocType', '(SELECT docType FROM `'.$DBTRd_clientInvoiceCashings.'` WHERE rep.invoiceId = idInvoice ORDER BY id DESC LIMIT 1)', 'rep');
		$reportCopyInfo->populate();

		// CORPORATE ACTIVITY DOMAIN / CATEGORY NAMES ///////////////////////////////////////////////
		$this->reportGenerationStatusUpdate('extractMainDataFromDb', 'updateCorporateActivityDomain', 0);
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $activeColumns);
		$reportCopyInfo->populateSimple($DBTRd_activityDomains, 'corporateDomainName', 'corporateDomainId', 'name');

		$this->reportGenerationStatusUpdate('extractMainDataFromDb', 'updateCorporateActivityDomainCategory', 0);
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $activeColumns);
		$reportCopyInfo->populateSimple($DBTRd_activityDomainCategories, 'corporateDomainCategoryName', 'corporateDomainCategoryId', 'name');

		// update the last cashing doc type name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addCaseStructureColumn("lastCashingDocTypeName", "lastCashingDocType", \App\Config\CashingDocuments::getInstance()->getList());
		$reportCopyInfo->populate();

		// update the last cashing doc id
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addStructureColumn("lastCashingDocId", "(SELECT id FROM `".$DBTRd_clientInvoiceCashings."` WHERE rep.invoiceId = idInvoice ORDER BY id DESC LIMIT 1)", "rep");
		$reportCopyInfo->populate();

		// update the invoice cashed status name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addCaseStructureColumn("cashedStatusName", "cashedStatus", (Array)$_LANG['invoices']['status']);
		$reportCopyInfo->populate();

		// ADD CUSTOMER / CORPORATE DATA //////////////////////////////////////////////////////////
		$this->reportGenerationStatusUpdate('extractMainDataFromDb', 'updateClientDetails', 0);

		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addJoinTable($DBTRd_corporates,        'corp',        'rep.corporateId=corp.id', 'LEFT');
		$reportCopyInfo->addJoinTable($DBTRd_trusts,            'trust',       'corp.idTrust=trust.id', 'LEFT');
		$reportCopyInfo->addJoinTable($DBTRd_corporateSegments, 'corpSeg',     'corp.idSegment=corpSeg.id', 'LEFT');
		$reportCopyInfo->addJoinTable($DBTRd_users, 			'kamUser',     'corp.idAccountManager=kamUser.id', 'LEFT');
		$reportCopyInfo->addJoinTable($DBTRd_customers,         'cust',        'rep.customerId=cust.id', 'LEFT');
		$reportCopyInfo->addJoinTable($DBTRd_customerSegments,  'custSeg',     'cust.idSegment=custSeg.id', 'LEFT');

		$cols = [
			'corporateAlias' => 				'corp.alias',
			'corporateTrustId' => 				'corp.idTrust',
			'corporateTrustName' => 			'trust.name',
			'corporateSegmentId' => 			'corp.idSegment',
			'corporateSegmentName' => 			'corpSeg.name',
			'corporateKamId' => 				'corp.idAccountManager',
			'corporateRemarks' => 				'corp.remarks',
			'corporateProcessedRemarks' => 		'corp.processedRemarks',

			'customerSegmentId' => 				'cust.idSegment',
			'customerSegmentName' => 			'custSeg.name',
			'customerRemarks' => 				'cust.remarks',
			'customerProcessedRemarks' =>		'cust.processedRemarks',
			'customerCountryCode' =>			'cust.countryCode',
			'customerCityId' =>					'cust.cityId',
		];

		$reportCopyInfo->setStructureColumns($cols);
		$reportCopyInfo->addStructureColumn("corporateKamName", 	"CONCAT(kamUser.lastName, ' ', kamUser.firstName)", "kamUser");
		$reportCopyInfo->addStructureColumn("corporateCompanyName", "TRIM(CONCAT(corp.companyNameStd, ' ', corp.companySuffixStd))", "corp");
		$reportCopyInfo->addStructureColumn("customerName", 		"CONCAT(cust.lastNameStd, ' ', cust.firstNameStd)", "cust");
		$reportCopyInfo->addStructureColumn("clientId", 			"IF(rep.corporateId, rep.corporateId, rep.customerId)", "rep");
		$reportCopyInfo->addStructureColumn("clientName", 			"IF(rep.corporateId, corp.alias, CONCAT(cust.lastNameStd, ' ', cust.firstNameStd))", "rep");
		$reportCopyInfo->addStructureColumn("clientTypeCode", 		"IF(rep.corporateId, corp.type, IF(rep.customerId, 'DC', ''))", "corp");
		$reportCopyInfo->addStructureColumn("clientCountryCode", 	"IF(rep.corporateId, rep.corporateCountryCode, cust.countryCode)", "cust");
		$reportCopyInfo->addStructureColumn("clientCityId", 		"IF(rep.corporateId, rep.corporateCityId, cust.cityId)", "cust");

		$reportCopyInfo->populate();

		// update the invoice client type name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addCaseStructureColumn('clientTypeName',  "clientTypeCode", (Array)$_LANG['generalData']['clientType']);
		$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('clientTypeCode', '!=', 'CC');
				$reportCopyInfo->populate();
			}
			elseif (in_array('SA', $stdFilters['clientType'])) {
				$reportCopyInfo = new \App\Reports\Db\ManageRecords\Delete($reportTable);
				$reportCopyInfo->addFilter('clientTypeCode', '!=', '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('clientTypeCode', '=', 'CC');
				$reportCopyInfo->populate();
			}
			elseif (!in_array('SA', $stdFilters['clientType'])) {
				$reportCopyInfo = new \App\Reports\Db\ManageRecords\Delete($reportTable);
				$reportCopyInfo->addFilter('clientTypeCode', '=', 'SA');
				$reportCopyInfo->populate();
			}
		}

		// ADD LEGAL ENTITY COUNTRY AND CITY NAMES ////////////////////////////////////////////////
		$this->reportGenerationStatusUpdate('extractMainDataFromDb', 'countryAndCityCorporate', 0);

		// update invoice corporate country
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $activeColumns);
		$reportCopyInfo->populateSimple($DBTRd_countries, 'corporateCountryName', 'corporateCountryCode', 'nameStd', 'code');

		// update invoice corporate city
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $activeColumns);
		$reportCopyInfo->populateSimple($DBTRd_cities, 'corporateCityName', 'corporateCityId', "nameStd");


		// ADD INDIVIDUAL COUNTRY AND CITY NAMES //////////////////////////////////////////////////
		$this->reportGenerationStatusUpdate('extractMainDataFromDb', 'countryAndCityCustomer', 0);

		// update invoice corporate country
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $activeColumns);
		$reportCopyInfo->populateSimple($DBTRd_countries, 'customerCountryName', 'customerCountryCode', 'nameStd', 'code');

		// update invoice corporate city
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $activeColumns);
		$reportCopyInfo->populateSimple($DBTRd_cities, 'customerCityName', 'customerCityId', "nameStd");

		// ADD UNIFIED CLIENT COUNTRY AND CITY NAMES //////////////////////////////////////////////
		$this->reportGenerationStatusUpdate('extractMainDataFromDb', 'countryAndCityClient', 0);

		// update invoice client country
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $activeColumns);
		$reportCopyInfo->populateSimple($DBTRd_countries, 'clientCountryName', 'clientCountryCode', 'nameStd', 'code');

		// update invoice client city
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $activeColumns);
		$reportCopyInfo->populateSimple($DBTRd_cities, 'clientCityName', 'clientCityId', "nameStd");

		// INVOICE DETAILS ////////////////////////////////////////////////////////////////////////

		$this->reportGenerationStatusUpdate('extractMainDataFromDb', 'updateInvoiceDetails', 0);

		// update the invoice form of payment name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addCaseStructureColumn('invoiceFormOfPaymentName',  "invoiceFormOfPayment", \App\Config\FormsOfPayment::getInstance()->getAll());
		$reportCopyInfo->populate();

		// update the invoice void reason name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addCaseStructureColumn('invoiceVoidReasonName',  "invoiceVoidReasonCode", (array)$_LANG['invoiceVoidReason']);
		$reportCopyInfo->populate();

	}

}

