File: /www/exchange0old/exchange/admin/validator/Parser.php
<?php
/**
* validator - Parser.php
*
* Initial version by: MisterX
* Initial version created on: 09.12.2015
*/
class Parser extends Batch
{
protected $batches=array();
protected $accounts = null;
protected $original_accounts = null;
public function __construct()
{
if(!class_exists('Bin_Query'))
{
require_once('../../Bin/init.php');
require_once(ROOT_FOLDER . 'Bin/Security.php');
require_once(ROOT_FOLDER . 'admin/classes/Model/base_actions.php');
}
function initHolidays($year)
{
$result=array($year.'-01-01',$year.'-03-17',$year.'-12-15',$year.'-12-26');
foreach(array('05','06','08') as $month_id)
{
$date_not_found=TRUE;
$date=strtotime($year.'-'.$month_id.'-01');
while($date_not_found)
{
if (date('l',$date)=='Monday')
{
$date_not_found=FALSE;
$result[]=date('Y-m-d',$date);
}
else
{
$date+=86400;
}
}
}
$date=strtotime($year.'-10-31');
$date_not_found=TRUE;
while($date_not_found)
{
if (date('l',$date)=='Monday')
{
$date_not_found=FALSE;
$result[]=date('Y-m-d',$date);
}
else
{
$date-=86400;
}
}
return $result;
}
$query = new Bin_Query();
$date_diff = -1;
switch(date('l'))
{
case 'Monday':
{
$query->executeQuery('SELECT id
FROM daily_summary
WHERE DATE(date)=DATE_ADD(CURDATE(),INTERVAL -1 DAY)');
$date_diff = (count($query->records) > 0) ? -1 : -3;
break;
}
}
$bank_holidays=explode(",",Base_actions::getSiteSettings('bank_holidays'));
$bank_holidays=array_merge($bank_holidays, initHolidays(date('Y')));
if (in_array(date('Y-m-d',time() + $date_diff * 86400),$bank_holidays))
{
$query->executeQuery('SELECT `date`
FROM daily_summary
WHERE DATE(date) < DATE_ADD(CURDATE(),INTERVAL '.$date_diff.' DAY) ORDER BY id DESC LIMIT 1');
$date_diff = intval((strtotime($query->records[0]['date']) - time()) / 86400);
}
$spool_date = date('Y-m-d');
$query->executeQuery("SELECT * FROM daily_summary LEFT JOIN bank_account ON daily_summary.account_id = bank_account.account_id WHERE DATE(`date`) = DATE_ADD('".$spool_date."', INTERVAL $date_diff DAY)");
$summary = count($query->records) ? $query->records :false;
$accounts = array();
foreach($summary as $s)
{
$accounts[(int)$s['bank_account_no']] = (float)$s['daily_summary'];
}
$this->accounts = $accounts;
$this->original_accounts = $accounts;
}
public function parseFromFile($filename)
{
$this->parseFromString(file_get_contents($filename));
}
public function parseFromString($string)
{
$transactions = explode("\n-",$string);
foreach($transactions as $transactionRaw)
{
$transactionRaw = trim($transactionRaw);
if(!$transactionRaw) continue;
$transaction = new Transaction($transactionRaw);
$this->addTransaction($transaction);
if($transaction->isBatch())
{
$account_from = $transaction->getAccountFrom();
$account_to = $transaction->getAccountTo();
if($account_to == 0)
{
$account_to = $transaction->getAccountTo();
foreach($this->accounts as $key => $v)
{
if( 'IE86BARC990212'.$key == $account_to )
{
$account_to = $key;
break;
}
}
}
if( array_key_exists($account_from, $this->accounts) )
{
$this->accounts[ $account_from ] -= (float)$transaction->getAmount();
}
else
{
//die('Account from '.$account_from.' is missing');
}
if( array_key_exists($account_to, $this->accounts) )
{
$this->accounts[ $account_to ] += (float)$transaction->getAmount();
}
/**
echo "From $account_from to $account_to ".(float)$transaction->getAmount()."<br>";
echo "$account_from: ". $this->accounts[ $account_from ]."<br>";
echo "$account_to: ". $this->accounts[ $account_to ]."<br>";
echo "<br>";
**/
$this->getBatch($transaction->getBatchNumber())->addTransaction($transaction);
}
else
{
$account_from = $transaction->getAccountFrom();
$account_to = $transaction->getAccountTo();
if( array_key_exists($account_from, $this->accounts) )
{
$this->accounts[ $account_from ] -= (float)$transaction->getAmount();
}
if( array_key_exists($account_to, $this->accounts) )
{
$this->accounts[ $account_to ] += (float)$transaction->getAmount();
}
}
}
echo "<pre>";
echo "=====================================================================================================<br>";
echo "\nValidation of balances AFTER spooling is EXPERIMENTAL, please consider it as additional information\n";
echo "\nBefore Spool:\n\n";
foreach($this->original_accounts as $acc => $amount)
{
if($acc == 0)
{
continue;
}
echo "Account $acc: ".$amount." \n";
}
echo "\nAFTER Spool:\n\n";
foreach($this->accounts as $acc => $amount)
{
if($acc == 0)
{
continue;
}
echo "Account $acc: ".$amount." \t...\t ";
if( $amount < 0)
{
echo "[WARNING, NEGATIVE VALUE]";
}
else
{
echo "[OK]";
}
echo "\n";
}
echo "\n===================================================================================================<br>";
echo "</pre>";
}
/**
* @param $number
* @return Batch
*/
protected function getBatch($number){
if(!array_key_exists($number,$this->batches)){
$this->batches[$number] = new Batch();
$this->batches[$number]->setId($number);
}
return $this->batches[$number];
}
public function getBatches(){
return $this->batches;
}
public function getFieldValue($field)
{
if($field=='transactions'){
return $this->transactions;
}elseif($field=='batches'){
return $this->batches;
}
}
}