<?php

// CHECK IF ANY OF THE LOOP RECORD COLUMNS WERE SELECTED //////////////////////////////////////////
$loopRecColumns = [
	'supplierBankAccountNumber', 'supplierBankAccountBankId', 'supplierBankAccountBankName',
 	'supplierBankAccountBankOfficeId', 'supplierBankAccountBankOfficeName',
];
if (!array_intersect($availableTableColumns, $loopRecColumns)) {
	return ;
}

// START THE LOOP RECORDS ROUTINE /////////////////////////////////////////////////////////////////
$recordsCountSQL = "SELECT COUNT(*) FROM `$reportTable`";
$recordsCount = $db->GetOne($recordsCountSQL);

// BREAK INTO RECORDS GROUPS //////////////////////////////////////////////////////////////////////
$groupSize = 500;
for ($startGroup = 0; $startGroup < $recordsCount; $startGroup += $groupSize) {

	// refresh report status
	refreshReportStatus('processingRecords', 'loopResults', $startGroup/$recordsCount*100);

	// GET RECORDS TO BE UPDATED //////////////////////////////////////////////////////////////////
	$getRecordsListSQL = "SELECT * FROM `$reportTable` LIMIT ".$startGroup.", ".$groupSize;
	$recordsList = $db->GetAll($getRecordsListSQL);

	// check SQL
	if ($db->ErrorNo()) killReport("Could not run SQL in report. SQL: ".$getServicesListSQL);

	// UPDATE RECORDS ONE BY ONE //////////////////////////////////////////////////////////////////
	foreach ($recordsList as $thisRow) {

		$updateColumns = [];

		// SUPPLIER BANK ACCOUNT DETAILS ----------------------------------------------------------
		if (
			($tableColumns['supplierBankAccountNumber'] or $tableColumns['supplierBankAccountBankId'] or $tableColumns['supplierBankAccountBankName'] or
			$tableColumns['supplierBankAccountBankOfficeId'] or $tableColumns['supplierBankAccountBankOfficeName'])
			and $thisRow['supplierBankAccountId']
		) {

			$suppObj = new \App\ReportsData\EntityFields\Supplier($thisRow['supplierOriginalId']);
			$suppBas = $suppObj->getValue('bankAccounts');
			$baDet = $suppBas[$thisRow['supplierBankAccountId']];
			if ($suppBas and $baDet) {
				if ($tableColumns['supplierBankAccountNumber']) {
					$updateColumns['supplierBankAccountNumber'] = $baDet['account'];
				}
				if ($tableColumns['supplierBankAccountBankId']) {
					$updateColumns['supplierBankAccountBankId'] = $baDet['idBank'];
				}
				if ($tableColumns['supplierBankAccountBankName'] and $baDet['idBank']) {
					$bankObj = new \App\ReportsData\EntityFields\Bank($baDet['idBank']);
					$updateColumns['supplierBankAccountBankName'] = $bankObj->getValue('nameStd');
				}
				if ($tableColumns['supplierBankAccountBankOfficeId']) {
					$updateColumns['supplierBankAccountBankOfficeId'] = $baDet['idBankOffice'];
				}
				if ($tableColumns['supplierBankAccountBankOfficeName'] and $baDet['idBankOffice']) {
					$bankOffObj = new \App\ReportsData\EntityFields\BankOffice($baDet['idBankOffice']);
					$updateColumns['supplierBankAccountBankOfficeName'] = $bankOffObj->getValue('nameStd');
				}
			}
		}

		// FINISH AND EXECUTE SQL -----------------------------------------------------------------
		if ($updateColumns) {
			$colsList = implode(', ', array_map(function($col){ return "`{$col}` = ?"; }, array_keys($updateColumns)));
			$updateRecordSQL = "UPDATE `$reportTable` SET {$colsList} WHERE invoiceId='".$thisRow['invoiceId']."'";
			$updateRecordEx = $db->Execute($updateRecordSQL, array_values($updateColumns));
			if ($db->ErrorNo()) {
				killReport("Could not run SQL in report. SQL: ".$updateRecordSQL);
			}
		}

	}
}

// cleanup last records list array
unset($recordsList);


