<?php

// CHECK IF ANY OF THE LOOP RECORD COLUMNS WERE SELECTED //////////////////////////////////////////
$loopRecColumns = [
	'supp_bankAccountNumber', 'supp_bankAccountBankId', 'supp_bankAccountBankName',
 	'supp_bankAccountBankOfficeId', 'supp_bankAccountBankOfficeName',
];
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['supp_bankAccountNumber'] or $tableColumns['supp_bankAccountBankId'] or $tableColumns['supp_bankAccountBankName'] or
			$tableColumns['supp_bankAccountBankOfficeId'] or $tableColumns['supp_bankAccountBankOfficeName'])
			and $thisRow['supp_bankAccountId']
		) {

			$suppObj = new \App\ReportsData\EntityFields\Supplier($thisRow['supp_originalId']);
			$suppBas = $suppObj->getValue('bankAccounts');
			$baDet = $suppBas[$thisRow['supp_bankAccountId']];
			if ($suppBas and $baDet) {
				if ($tableColumns['supp_bankAccountNumber']) {
					$updateColumns['supp_bankAccountNumber'] = $baDet['account'];
				}
				if ($tableColumns['supp_bankAccountBankId']) {
					$updateColumns['supp_bankAccountBankId'] = $baDet['idBank'];
				}
				if ($tableColumns['supp_bankAccountBankName'] and $baDet['idBank']) {
					$bankObj = new \App\ReportsData\EntityFields\Bank($baDet['idBank']);
					$updateColumns['supp_bankAccountBankName'] = $bankObj->getValue('nameStd');
				}
				if ($tableColumns['supp_bankAccountBankOfficeId']) {
					$updateColumns['supp_bankAccountBankOfficeId'] = $baDet['idBankOffice'];
				}
				if ($tableColumns['supp_bankAccountBankOfficeName'] and $baDet['idBankOffice']) {
					$bankOffObj = new \App\ReportsData\EntityFields\BankOffice($baDet['idBankOffice']);
					$updateColumns['supp_bankAccountBankOfficeName'] = $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 inv_id='".$thisRow['inv_id']."'";
			$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);


