<?php

namespace App\Reports\Sources\SupplierInvoiceRows;

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

	public function populate() :void
	{

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

		global $DBTRd_suppliers, $DBTRd_trusts, $DBTRd_supplierInvoicePayments, $DBTRd_customers, $DBTRd_supplierProducts;
		global $DBTRd_users, $DBTRd_offices, $DBTRd_departments, $DBTRd_countries, $DBTRd_cities;
		global $DBTRd_miceProjects, $DBTRd_services, $DBTRd_offers, $DBTRd_balances;

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

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

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

		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();
		$displayPriceDecimals = $CFG['localization']['maxDecimalsOnPrices'];

		// INVOICE DATA ///////////////////////////////////////////////////////////////////////////////////
		$this->reportGenerationStatusUpdate('extractMainDataFromDb', 'updateSupplierInvDetails', 0);

		// update the invoice operational name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addCaseStructureColumn("inv_statusOpName", "inv_statusOpCode", (Array)$_LANG['supplierInvoices']['statusOp']);
		$reportCopyInfo->populate();

		// update the invoice financial name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addCaseStructureColumn("inv_statusFinName", "inv_statusFinCode", (Array)$_LANG['supplierInvoices']['statusFin']);
		$reportCopyInfo->populate();

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

		// update the invoice reponsible office name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $activeColumns);
		$reportCopyInfo->populateSimple($DBTRd_offices, 'inv_respOfficeName', 'inv_respOfficeId', 'name');

		// update the invoice reponsible department name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $activeColumns);
		$reportCopyInfo->populateSimple($DBTRd_departments, 'inv_respDepartmentName', 'inv_respDepartmentId', 'name');

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

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

		// SUPPLIER DATA //////////////////////////////////////////////////////////////////////////////////
		$this->reportGenerationStatusUpdate('extractMainDataFromDb', 'updateSupplierDetails', 0);
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addJoinTable($DBTRd_suppliers,         'supp',        	'rep.supp_id=supp.id', 				'LEFT');
		$reportCopyInfo->addJoinTable($DBTRd_trusts,            'trust',       	'supp.idTrust=trust.id', 			'LEFT');

		$cols = [
			'supp_alias'					=> 'supp.alias',
			'supp_trustId'					=> 'supp.idTrust',
			'supp_trustName'				=> 'trust.name',
		];

		$reportCopyInfo->setStructureColumns($cols);
		$reportCopyInfo->addStructureColumn("supp_fullName", "TRIM(CONCAT(supp.companyName, ' ', supp.companySuffix))", "supp");

		$reportCopyInfo->populate();

		// INVOICE ROW DATA ///////////////////////////////////////////////////////////////////////////////
		$this->reportGenerationStatusUpdate('extractMainDataFromDb', 'updateSupplierInvRowDetails', 0);

		// update the invoice row service name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addCaseStructureColumn('invRow_serviceName',  "invRow_serviceCode", \App\Config\Services_Basic::getInstance()->getListLinearSimple());
		$reportCopyInfo->populate();

		// update invoice row type name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addCaseStructureColumn("invRow_typeName", "invRow_typeCode", (Array)$_LANG['rowType']);
		$reportCopyInfo->populate();

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

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

		// update invoice row passenger
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $activeColumns);
		$reportCopyInfo->populateSimple($DBTRd_customers, 'invRow_passengerName', 'invRow_passengerId', "CONCAT(joinTable.lastNameStd, ' ', joinTable.firstNameStd)");

		// update supplier product details
		$this->reportGenerationStatusUpdate('extractMainDataFromDb', 'updateSupplierProductDetails', 0);
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addJoinTable($DBTRd_supplierProducts, 'suppProd', 'rep.invRow_supplierProductId=suppProd.id', 'LEFT');
		$reportCopyInfo->setStructureColumns([
			'invRow_supplierProductName' =>     'suppProd.productName',
			'invRow_supplierChainCode' =>      	'suppProd.chainCode',
		]);
		$reportCopyInfo->populate();

		// update the row payed value original currency
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addStructureColumn("invRow_payedValue", "ROUND(rep.invRow_total*rep.invRow_payedPercent, {$displayPriceDecimals})", "rep");
		$reportCopyInfo->populate();

		// update the row not payed value original currency
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addStructureColumn("invRow_notPayedValue", "rep.invRow_total-ROUND(rep.invRow_total*rep.invRow_payedPercent, {$displayPriceDecimals})", "rep");
		$reportCopyInfo->populate();

		// SERVICE DATA ///////////////////////////////////////////////////////////////////////////////////
		$this->reportGenerationStatusUpdate('extractMainDataFromDb', 'updateServiceDetails', 0);

		// update the invoice operational name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addCaseStructureColumn("serv_statusOpName", "serv_statusOpCode", (Array)$_LANG['reservationServices']['statusOpName']);
		$reportCopyInfo->populate();

		// update the invoice financial name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addCaseStructureColumn("serv_statusFinName", "serv_statusFinCode", (Array)$_LANG['reservationServices']['statusFinName']);
		$reportCopyInfo->populate();

		// update offer net company [default curr]
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addJoinTable($DBTRd_services, 'serv', 'rep.serv_id=serv.id', 'INNER');
		$reportCopyInfo->addJoinTable($DBTRd_offers, 'off', 'off.idService=serv.idOffer', 'INNER');
		$reportCopyInfo->addStructureColumn("serv_id", "serv.id"); // this is just to force the services table into the join
		$reportCopyInfo->addStructureColumn("off_netCompanyDefCurr", "off.netCompanyTotal*off.currencyRate", 'off');
		$reportCopyInfo->populate();


		// update the last payment document type
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addStructureColumn("inv_lastPaymentDocTypeCode", "(SELECT docType FROM `".$DBTRd_supplierInvoicePayments."` WHERE rep.inv_id = idInvoice AND deleted=0 ORDER BY id DESC LIMIT 1)", "rep");
		$reportCopyInfo->populate();

		// update the document doc type name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addCaseStructureColumn("inv_lastPaymentDocTypeName", "inv_lastPaymentDocTypeCode", \App\Config\PaymentDocuments::getInstance()->getList());
		$reportCopyInfo->populate();

		// update the last payment document id
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addStructureColumn("inv_lastPaymentDocId", "(SELECT id FROM `".$DBTRd_supplierInvoicePayments."` WHERE rep.inv_id = idInvoice AND deleted=0 ORDER BY id DESC LIMIT 1)", "rep");
		$reportCopyInfo->populate();

		// ORDER DATA /////////////////////////////////////////////////////////////////////////////////////
		$this->reportGenerationStatusUpdate('extractMainDataFromDb', 'updateOrderDetails', 0);

		// update the order mice project name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $activeColumns);
		$reportCopyInfo->populateSimple($DBTRd_miceProjects, 'order_miceProjectName', 'order_miceProjectId', 'name');

		// BALANCE DATA ///////////////////////////////////////////////////////////////////////////////////
		$this->reportGenerationStatusUpdate('extractMainDataFromDb', 'updateBalanceDetails', 0);

		// update balance details
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addJoinTable($DBTRd_balances, 'bal', 'rep.bal_id=bal.id');
		$reportCopyInfo->setStructureColumns([
			'bal_number' 						=> 'bal.balanceNumber',
			'bal_date' 							=> 'bal.balanceDate',
			'bal_code' 							=> 'bal.balanceCode',
			'bal_name' 							=> 'bal.balanceName',
			'bal_typeCode' 						=> 'bal.balanceType',
			'bal_statusCode' 					=> 'bal.balanceStatus',
			'bal_comments' 						=> 'bal.comments',
			'bal_respUserId' 					=> 'bal.idUser',
			'bal_respOfficeId' 					=> 'bal.idOffice',
			'bal_addUserId' 					=> 'bal.idAddUser',
			'bal_confUserId' 					=> 'bal.idConfirmUser',
			'bal_confDateTime' 					=> 'bal.confirmTime'
		]);
		$reportCopyInfo->addStructureColumn("bal_confirmed", "IF(bal.confirmTime IS NOT NULL, 1, 0)", "bal");
		$reportCopyInfo->populate();

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

		// update the balance reponsible office name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\UpdateSimple($reportTable, $activeColumns);
		$reportCopyInfo->populateSimple($DBTRd_offices, 'bal_respOfficeName', 'bal_respOfficeId', 'name');

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

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

		// update balance type name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addCaseStructureColumn("bal_typeName",   "bal_typeCode",   (Array)$_LANG['invoiceBalance']['type']);
		$reportCopyInfo->populate();

		// update balance status name
		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Update($reportTable, 'rep', $activeColumns);
		$reportCopyInfo->addCaseStructureColumn("bal_statusName", "bal_statusCode", (Array)$_LANG['invoiceBalance']['status']);
		$reportCopyInfo->populate();



	}
}

