<commit_before><?php

require_once $rootpath . 'includes/inc_transactions.php';

function processqueue()
{
	global $db, $systemtag;

	echo "Running Interlets System\n\n";

	$transactions = $db->fetchAll('SELECT * FROM interletsq');

	foreach ($transactions AS $key => $value)
	{
		$transid = $value['transid'];
		$group_id = $value['letsgroup_id'];
		$id_from = $value['id_from'];
		$letscode_to = trim($value['letscode_to']);
		$amount = $value['amount'];
		$description = $value['description'];
		$signature = $value['signature'];
		$retry_until = $value['retry_until'];
		$count = $value['retry_count'];

		echo 'Processing transaction' .  $transid . "\t";

		$my_group = $db->fetchAssoc('select * from letsgroups where id = ?', [$group_id]);

		$myuser = readuser($value['id_from']);
		$real_from = $myuser['letscode'] . ' ' . $myuser['name'];

		$soapurl = ($my_group['elassoapurl']) ?: $my_group['url'] . '/soap';
		$soapurl .= '/wsdlelas.php?wsdl';

		// Make the SOAP connection, send our API key and the transaction details
		$myapikey = $my_group['remoteapikey'];
		$from = $my_group['myremoteletscode'];
		$client = new nusoap_client($soapurl, true);
		$err = $client->getError();
		if (!$err)
		{
			$result = $client->call('dopayment', [
				'apikey' => $myapikey,
				'from' => $from,
				'real_from' => $real_from,
				'to' => $letscode_to,
				'description' => $description,
				'amount' => $amount,
				'transid' => $transid,
				'signature' => $signature
			]);
			$err = $client->getError();
			if (!$err)
			{
				//return $result;
				// Process the result statusa
				echo $result;
				echo "\n";
				switch ($result)
				{
					case 'SUCCESS':
						//Commit locally
						if(localcommit($my_group, $transid, $id_from, $amount, $description, $letscode_to) == 'FAILED')
						{
							update_queue($transid, $count, 'LOCALFAIL');
						}
						break;
					case 'OFFLINE':
						//Do nothing
						update_queue($transid,$count,$result);
						log_event('soap', 'Remote site offline ' . $transid);
						break;
					case 'FAILED':
						//Handle error and remove transaction
						mail_failed_interlets($transid, $id_from, $amount, $description, $letscode_to, $result);
						unqueue($transid);
						break;
					case 'SIGFAIL':
						//Handle the error and remove transaction
						mail_failed_interlets($transid, $id_from, $amount, $description, $letscode_to, $result);
						unqueue($transid);
						break;
					case 'DUPLICATE':
						//Commit locally
						if(localcommit($my_group, $transid, $id_from, $amount, $description, $letscode_to) == 'FAILED'){
							update_queue($transid,$count,'LOCALFAIL');
						}
						break;
					case 'NOUSER':
						//Handle the error and remove transaction
						mail_failed_interlets($transid, $id_from, $amount, $description, $letscode_to, $result);
						unqueue($transid);
						break;
					case 'APIKEYFAIL':
						update_queue($transid,$count,$result);
						break;
					default:
						//Evaluate the date and pop the transaction if needed, handling the error.
						echo 'Default handling';
						update_queue($transid, $count, 'DEFAULT');
				}
			}
			else
			{
				if(strtotime($value['retry_until']) < time())
				{
					echo 'EXPIRED';
					echo "\n";
				}
				update_queue($transid, $count, 'UNKNOWN');
			}
		}

	}

	echo "\nDone processing.\n\n";
}


function unqueue($transid)
{
	global $db;
	$db->delete('interletsq', ['transid' => $transid]);
	log_event('trans','Removing ' . $transid . 'from queue');	
}

function update_queue($transid,$count,$result)
{
	global $db;
	$count++;
	$db->update('interletsq', ['retry_count' => $count, 'last_status' => $result], ['transid' => $transid]);
}

function localcommit($my_group, $transid, $id_from, $amount, $description, $letscode_to)
{
	global $db, $systemtag;

	//FIXME Add data validation and clear error message for bug #321
	//FIXME output debug info when elasdebug = 1
	echo 'Local commiting ' . $transid . "\t\t";
	$ratio = readconfigfromdb('currencyratio');
	$transaction['amount'] = round($amount * $ratio);
	$transaction['description'] = $description;
	$transaction['id_from'] = $id_from;
	//Lookup id_to first
	$to_user = $db->fetchAssoc('SELECT * FROM users WHERE letscode = ?', [$my_group['localletscode']]);

	$transaction['id_to'] = $to_user['id'];
	//Real_to has to be set by a soap call
	$mysoapurl = $my_group['elassoapurl'] .'/wsdlelas.php?wsdl';
	$myapikey = $my_group['remoteapikey'];
	$client = new nusoap_client($mysoapurl, true);
	$result = $client->call('userbyletscode', ['apikey' => $myapikey, 'letscode' => $letscode_to]);
	$err = $client->getError();
	if (!$err)
	{
		$transaction['real_to'] = $result;
	}

	$transaction['transid'] = $transid;
	$transaction['date'] = date('Y-m-d H:i:s');

	$error_list = [];

	if (!isset($transaction['description']) || (trim($transaction['description'] ) == ''))
	{
		$error_list['description'] = 'Dienst is niet ingevuld';
	}

	//amount may not be empty
	$transaction['amount'] = trim($transaction['amount']);

	if (!isset($transaction['amount']) || (trim($transaction['amount'] ) == '' || !$transaction['amount']))
	{
		$error_list['amount'] = 'Bedrag is niet ingevuld';
	}

	//userfrom must exist
	$fromuser = readuser($transaction['id_from']);
	if(!$fromuser)
	{
		$error_list['id_from'] = 'Gebruiker bestaat niet';
	}

	//userto must exist
	$touser = readuser($transaction['id_to']);
	if(!$touser)
	{
		$error_list['id_to'] = 'Gebruiker bestaat niet';
	}

	//userfrom and userto should not be the same
	if($fromuser['letscode'] == $touser['letscode']){
		$error_list['id']='Van en Aan zijn hetzelfde';
	}

	if (!isset($transaction['date'])|| (trim($transaction['date'] )==''))
	{
		$error_list['date']='Datum is niet ingevuld';
	}
	else if(strtotime($transaction['date']) == -1)
	{
		$error_list['date']='Fout in datumformaat (jjjj-mm-dd)';
	}

	if(!empty($error_list))
	{
		echo "\nVALIDATION ERRORS\n";
		var_dump($error_list);
		echo "\Tried to commit:\n";
		var_dump($transaction);
		echo "\n";
	}
	else
	{
		$id = insert_transaction($transaction);
	}

	if($id)
	{
		$result = 'SUCCESS';
		log_event('trans', 'Local commit of interlets transaction succeeded');
		$transaction['amount'] = round($transaction['amount']);
		$transaction['id'] = $id;
		mail_transaction($transaction);
		unqueue($transid);
	}
	else
	{
		$result = 'FAILED';
		log_event('trans','Local commit of $transid failed');
		//FIXME Replace with something less spammy (1 mail per 15 minutes);

		$subject = 'Interlets FAILURE!';

		$text = 'WARNING: LOCAL COMMIT OF TRANSACTION $transid FAILED!!!  This means the transaction is not balanced now!';
<commit_msg>debug local commit transaction<commit_after><?php

require_once $rootpath . 'includes/inc_transactions.php';

function processqueue()
{
	global $db, $systemtag;

	echo "Running Interlets System\n\n";

	$transactions = $db->fetchAll('SELECT * FROM interletsq');

	foreach ($transactions AS $key => $value)
	{
		$transid = $value['transid'];
		$group_id = $value['letsgroup_id'];
		$id_from = $value['id_from'];
		$letscode_to = trim($value['letscode_to']);
		$amount = $value['amount'];
		$description = $value['description'];
		$signature = $value['signature'];
		$retry_until = $value['retry_until'];
		$count = $value['retry_count'];

		echo 'Processing transaction' .  $transid . "\t";

		$my_group = $db->fetchAssoc('select * from letsgroups where id = ?', [$group_id]);

		$myuser = readuser($value['id_from']);
		$real_from = $myuser['letscode'] . ' ' . $myuser['name'];

		$soapurl = ($my_group['elassoapurl']) ?: $my_group['url'] . '/soap';
		$soapurl .= '/wsdlelas.php?wsdl';

		// Make the SOAP connection, send our API key and the transaction details
		$myapikey = $my_group['remoteapikey'];
		$from = $my_group['myremoteletscode'];
		$client = new nusoap_client($soapurl, true);
		$err = $client->getError();
		if (!$err)
		{
			$result = $client->call('dopayment', [
				'apikey' => $myapikey,
				'from' => $from,
				'real_from' => $real_from,
				'to' => $letscode_to,
				'description' => $description,
				'amount' => $amount,
				'transid' => $transid,
				'signature' => $signature
			]);
			$err = $client->getError();
			if (!$err)
			{
				//return $result;
				// Process the result statusa
				echo $result;
				echo "\n";
				switch ($result)
				{
					case 'SUCCESS':
						//Commit locally
						if(localcommit($my_group, $transid, $id_from, $amount, $description, $letscode_to) == 'FAILED')
						{
							update_queue($transid, $count, 'LOCALFAIL');
						}
						break;
					case 'OFFLINE':
						//Do nothing
						update_queue($transid,$count,$result);
						log_event('soap', 'Remote site offline ' . $transid);
						break;
					case 'FAILED':
						//Handle error and remove transaction
						mail_failed_interlets($transid, $id_from, $amount, $description, $letscode_to, $result);
						unqueue($transid);
						break;
					case 'SIGFAIL':
						//Handle the error and remove transaction
						mail_failed_interlets($transid, $id_from, $amount, $description, $letscode_to, $result);
						unqueue($transid);
						break;
					case 'DUPLICATE':
						//Commit locally
						if(localcommit($my_group, $transid, $id_from, $amount, $description, $letscode_to) == 'FAILED'){
							update_queue($transid,$count,'LOCALFAIL');
						}
						break;
					case 'NOUSER':
						//Handle the error and remove transaction
						mail_failed_interlets($transid, $id_from, $amount, $description, $letscode_to, $result);
						unqueue($transid);
						break;
					case 'APIKEYFAIL':
						update_queue($transid,$count,$result);
						break;
					default:
						//Evaluate the date and pop the transaction if needed, handling the error.
						echo 'Default handling';
						update_queue($transid, $count, 'DEFAULT');
				}
			}
			else
			{
				if(strtotime($value['retry_until']) < time())
				{
					echo 'EXPIRED';
					echo "\n";
				}
				update_queue($transid, $count, 'UNKNOWN');
			}
		}

	}

	echo "\nDone processing.\n\n";
}


function unqueue($transid)
{
	global $db;
	$db->delete('interletsq', ['transid' => $transid]);
	log_event('trans','Removing ' . $transid . 'from queue');	
}

function update_queue($transid,$count,$result)
{
	global $db;
	$count++;
	$db->update('interletsq', ['retry_count' => $count, 'last_status' => $result], ['transid' => $transid]);
}

function localcommit($my_group, $transid, $id_from, $amount, $description, $letscode_to)
{
	global $db, $systemtag;

	//FIXME Add data validation and clear error message for bug #321
	//FIXME output debug info when elasdebug = 1
	echo 'Local commiting ' . $transid . "\t\t";
	$ratio = readconfigfromdb('currencyratio');
	$transaction['amount'] = round($amount * $ratio);
	$transaction['description'] = $description;
	$transaction['id_from'] = $id_from;
	//Lookup id_to first
	$to_user = $db->fetchAssoc('SELECT * FROM users WHERE letscode = ?', [$my_group['localletscode']]);

	$transaction['id_to'] = $to_user['id'];
	//Real_to has to be set by a soap call
	$mysoapurl = $my_group['elassoapurl'] .'/wsdlelas.php?wsdl';
	$myapikey = $my_group['remoteapikey'];
	$client = new nusoap_client($mysoapurl, true);
	$result = $client->call('userbyletscode', ['apikey' => $myapikey, 'letscode' => $letscode_to]);
	$err = $client->getError();
	if (!$err)
	{
		$transaction['real_to'] = $result;
	}

	$transaction['transid'] = $transid;
	$transaction['date'] = date('Y-m-d H:i:s');

	$error_list = [];

	if (!isset($transaction['description']) || (trim($transaction['description'] ) == ''))
	{
		$error_list['description'] = 'Dienst is niet ingevuld';
	}

	//amount may not be empty
	$transaction['amount'] = trim($transaction['amount']);

	if (!isset($transaction['amount']) || (trim($transaction['amount'] ) == '' || !$transaction['amount']))
	{
		$error_list['amount'] = 'Bedrag is niet ingevuld';
	}

	//userfrom must exist
	$fromuser = readuser($transaction['id_from']);
	if(!$fromuser)
	{
		$error_list['id_from'] = 'Gebruiker bestaat niet';
	}

	//userto must exist
	$touser = readuser($transaction['id_to']);
	if(!$touser)
	{
		$error_list['id_to'] = 'Gebruiker bestaat niet';
	}

	//userfrom and userto should not be the same
	if($fromuser['letscode'] == $touser['letscode']){
		$error_list['id']='Van en Aan zijn hetzelfde';
	}

	if (!isset($transaction['date'])|| (trim($transaction['date'] )==''))
	{
		$error_list['date']='Datum is niet ingevuld';
	}
	else if(strtotime($transaction['date']) == -1)
	{
		$error_list['date']='Fout in datumformaat (jjjj-mm-dd)';
	}

	if(!empty($error_list))
	{
		echo "\nVALIDATION ERRORS\n";
		var_dump($error_list);
		echo "\Tried to commit:\n";
		var_dump($transaction);
		echo "\n";
	}
	else
	{
		echo '---   ' . http_build_query($transaction) . ' ----';
		$id = insert_transaction($transaction);
	}

	if($id)
	{
		$result = 'SUCCESS';
		log_event('trans', 'Local commit of interlets transaction succeeded');
		$transaction['amount'] = round($transaction['amount']);
		$transaction['id'] = $id;
		mail_transaction($transaction);
		unqueue($transid);
	}
	else
	{
		$result = 'FAILED';
		log_event('trans','Local commit of $transid failed');
		//FIXME Replace with something less spammy (1 mail per 15 minutes);

		$subject = 'Interlets FAILURE!';

		$text = 'WARNING: LOCAL COMMIT OF TRANSACTION $transid FAILED!!!  This means the transaction is not balanced now!';

{'fraction_non_alphanumeric': 0.1848405253283302, 'fraction_numerical': 0.001200750469043152, 'mean_word_length': 4.099885189437428, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5911508', 'n_tokens_mistral': 5340, 'n_tokens_neox': 4647, 'n_words': 1001}