File: /www/exchange0old/exchange/custom_actions/bug_271.php
<?php
class Bug_271
{
private $query;
private $investor_actions;
private $investment_group;
private $investment_id;
private $user_id;
function __construct()
{
include_once('../Bin/constants.php');
include_once('../Bin/init.php');
require_once(ROOT_FOLDER.'Bin/Security.php');
$this->query = new Bin_Query();
include_once('../admin/classes/Model/investor_actions.php');
$this->investor_actions= new Investor_actions();
$this->investor_actions->mode='import';
$_SESSION['admin']=1;
}
function loadData()
{
$file=fopen('investements.csv','r');
fgetcsv($file);
$this->query->updateQuery('TRUNCATE investor_investments;');
while($data=fgetcsv($file))
{
$de2=$data;
if (strpos($data[0],'-'))
{
$data[0]=preg_replace('/[-0-9]{2}$/si','',trim($data[0]));
}
if (trim($de2[0])!='20130624000021')
{
//continue;
}
$user_id=$this->query->executeQuery('SELECT user_id FROM temp_registration WHERE ref_id="'.trim($data[0]).'"');
if (count($this->query->records)==0)
{
echo ("Investor not found - ".$data[0].serialize($de2)."\r\n");
continue;
}
$this->user_id=$this->query->records[0]['user_id'];
$currencies=array('EUR'=>1,'GBP'=>2,'USD'=>3);
if (!in_array($data[4],array_keys($currencies)))
{
echo("Wrong currency for investor - ".$data[0].' - '.$data[4]."\r\n");
continue;
}
if (empty($data[5]) OR empty($data[6]) OR empty($data[7]) OR !isset($data[8]) OR empty($data[9]))
{
echo('No data for first RPA for -'.$data[0].'-'.$data[5].'-'.$data[6].'-'.$data[7].'-'.$data[8].'-'.$data[9]."\r\n");
continue;
}
//First RPA
$_POST=array(
'trans_year'=>date('Y',strtotime($data[6])),
'trans_month'=>date('m',strtotime($data[6])),
'trans_date'=>date('d',strtotime($data[6])),
'investment_type'=>INVESTMENT_RPA,
'investment_group'=>'new',
'investor_id'=>$this->user_id,
'currency_id'=>$currencies[$data[4]],
'amount'=>$data[5],
'buy_rate'=>$data[7]*100,
'commission'=>$data[8]*100,
'months'=>$data[9],
'notes'=>''
);
$this->saveTransaction();
if (empty($data[11]))
{
continue;
}
$this->query->executeQuery('SELECT DATE_ADD("'.$data[6].'",INTERVAL '.$data[9].' MONTH) as next_date');
if($data[11]=='Redeem' AND empty($data[14]) AND strtotime($data[10])>strtotime($this->query->records[0]['next_date']))
{
$this->addNextInvestement('Continue',$this->query->records[0]['next_date'],$data[7],$data[8],$data[9]);
}
if ($data[11]=='Compound' AND !empty($data[14]))
{
}
else
{
$this->addNextInvestement($data[11],$data[10],$data[16],$data[17],$data[18]);
}
switch(TRUE)
{
case $data[11]=='Coupon' AND empty($data[14]):
{
if (empty($data[15]) OR empty($data[16]) OR !isset($data[17]) OR empty($data[18]))
{
echo ('No data for Continue for -'.__LINE__.'-'.$data[0]."\r\n");
continue;
}
$this->addNextInvestement('Continue',$data[15],$data[16],$data[17],$data[18]);
break;
}
case !empty($data[14]):
{
$this->query->executeQuery('SELECT DATE_ADD("'.$data[6].'",INTERVAL '.$data[9].' MONTH) as next_date');
$next_date=$this->query->records[0]['next_date'];
if($next_date==$data[15])
{
if ($data[11]=='Redeem')
{
$_POST=array(
'trans_year'=>date('Y',strtotime($data[15])),
'trans_month'=>date('m',strtotime($data[15])),
'trans_date'=>date('d',strtotime($data[15])),
'investment_type'=>INVESTMENT_RPA,
'investment_group'=>'new',
'investor_id'=>$this->user_id,
'currency_id'=>$currencies[$data[4]],
'amount'=>$data[14],
'buy_rate'=>$data[16]*100,
'commission'=>$data[17]*100,
'months'=>$data[18],
'notes'=>''
);
$this->saveTransaction();
}
else
{
$_POST=array(
'trans_year'=>date('Y',strtotime($data[15])),
'trans_month'=>date('m',strtotime($data[15])),
'trans_date'=>date('d',strtotime($data[15])),
'investment_type'=>INVESTMENT_ADDITION,
'investment_group'=>$this->investment_group,
'investor_id'=>$this->user_id,
'currency_id'=>$currencies[$data[4]],
'amount'=>$data[14],
'notes'=>''
);
$this->saveTransaction();
$this->query->updateQuery('UPDATE investor_investments SET
change_principal='.$data[14].',
change_date="'.$data[15].'"
WHERE investment_id='.$this->investment_id);
$this->addNextInvestement('Continue',$data[15],$data[16],$data[17],$data[18]);
}
}
else
{
if ($data[11]!='Redeem')
{
$details=$this->addNextInvestement('Redeem',$next_date);
}
$_POST=array(
'trans_year'=>date('Y',strtotime($data[15])),
'trans_month'=>date('m',strtotime($data[15])),
'trans_date'=>date('d',strtotime($data[15])),
'investment_type'=>INVESTMENT_RPA,
'investment_group'=>'new',
'investor_id'=>$this->user_id,
'currency_id'=>$currencies[$data[4]],
'amount'=>$data[14],
'buy_rate'=>$data[16]*100,
'commission'=>$data[17]*100,
'months'=>$data[18],
'notes'=>''
);
$this->saveTransaction();
if ($data[11]!='Redeem')
{
$_POST=array(
'trans_year'=>date('Y',strtotime($next_date)),
'trans_month'=>date('m',strtotime($next_date)),
'trans_date'=>date('d',strtotime($next_date)),
'investment_type'=>INVESTMENT_ADDITION,
'investment_group'=>$this->investment_group,
'investor_id'=>$this->user_id,
'currency_id'=>$currencies[$data[4]],
'amount'=>$details['amount']+$details['yield'],
'notes'=>''
);
$this->saveTransaction();
$this->query->executeQuery('SELECT amount,investment_date
FROM investor_investments
WHERE investor_investments.investment_group='.$details['investment_group'].' AND investor_investments.investment_type='.INVESTMENT_RPA);
$this->query->updateQuery('UPDATE investor_investments SET
change_principal='.$this->query->records[0]['amount'].',
change_date="'.$this->query->records[0]['investment_date'].'"
WHERE investment_id='.$this->investment_id);
}
}
break;
}
}
if (empty($data[20]))
{
continue;
}
if ($data[20]=='Coupon' AND !empty($data[19]) AND !empty($data[23]))
{
if (trim($data[0])=='20130624000021')
{
//$_REQUEST['end_date']=$data[19];
$this->addNextInvestement('Redeem',$data[24]);
$_POST=array(
'trans_year'=>date('Y',strtotime($data[24])),
'trans_month'=>date('m',strtotime($data[24])),
'trans_date'=>date('d',strtotime($data[24])),
'investment_type'=>INVESTMENT_RPA,
'investment_group'=>'new',
'investor_id'=>$this->user_id,
'currency_id'=>$currencies[$data[4]],
'amount'=>$data[23],
'buy_rate'=>$data[25]*100,
'commission'=>$data[26]*100,
'months'=>$data[27],
'notes'=>''
);
$this->saveTransaction();
$_POST=array(
'trans_year'=>date('Y',strtotime($data[24])),
'trans_month'=>date('m',strtotime($data[24])),
'trans_date'=>date('d',strtotime($data[24])),
'investment_type'=>INVESTMENT_ADDITION,
'investment_group'=>$this->investment_group,
'investor_id'=>$this->user_id,
'currency_id'=>$currencies[$data[4]],
'amount'=>$data[5],
'notes'=>''
);
$this->saveTransaction();
$this->query->updateQuery('UPDATE investor_investments SET
change_principal='.$data[5].',
change_date="'.$data[24].'"
WHERE investment_id='.$this->investment_id);
}
}
else
{
$this->query->executeQuery('SELECT DATE_ADD("'.$data[15].'",INTERVAL '.$data[18].' MONTH) as next_date');
if($data[20]=='Redeem' AND empty($data[23]) AND strtotime($data[19])>strtotime($this->query->records[0]['next_date']))
{
$this->addNextInvestement('Continue',$this->query->records[0]['next_date'],$data[16],$data[17],$data[18]);
}
if ($data[20]=='Compound' AND !empty($data[23]))
{
}
else
{
$this->addNextInvestement($data[20],$data[19],$data[25],$data[26],$data[27]);
}
switch(TRUE)
{
case $data[20]=='Coupon' AND empty($data[23]):
{
if (empty($data[24]) OR empty($data[25]) OR empty($data[26]) OR empty($data[27]))
{
echo ('No data for Continue for -'.__LINE__.'-'.$data[0]."\r\n");
continue;
}
$this->addNextInvestement('Continue',$data[24],$data[25],$data[26],$data[27]);
break;
}
case $data[20]=='Redeem' AND !empty($data[23]):
{
$_POST=array(
'trans_year'=>date('Y',strtotime($data[24])),
'trans_month'=>date('m',strtotime($data[24])),
'trans_date'=>date('d',strtotime($data[24])),
'investment_type'=>INVESTMENT_RPA,
'investment_group'=>'new',
'investor_id'=>$this->user_id,
'currency_id'=>$currencies[$data[4]],
'amount'=>$data[23],
'buy_rate'=>$data[25]*100,
'commission'=>$data[26]*100,
'months'=>$data[27],
'notes'=>''
);
$this->saveTransaction();
break;
}
case $data[20]=='Compound' AND !empty($data[23]):
{
$details=$this->addNextInvestement('Redeem',$data[24]);
$_POST=array(
'trans_year'=>date('Y',strtotime($data[24])),
'trans_month'=>date('m',strtotime($data[24])),
'trans_date'=>date('d',strtotime($data[24])),
'investment_type'=>INVESTMENT_RPA,
'investment_group'=>'new',
'investor_id'=>$this->user_id,
'currency_id'=>$currencies[$data[4]],
'amount'=>$data[23],
'buy_rate'=>$data[25]*100,
'commission'=>$data[26]*100,
'months'=>$data[27],
'notes'=>''
);
$this->saveTransaction();
$_POST=array(
'trans_year'=>date('Y',strtotime($data[24])),
'trans_month'=>date('m',strtotime($data[24])),
'trans_date'=>date('d',strtotime($data[24])),
'investment_type'=>INVESTMENT_ADDITION,
'investment_group'=>$this->investment_group,
'investor_id'=>$this->user_id,
'currency_id'=>$currencies[$data[4]],
'amount'=>$details['amount']+$details['yield'],
'notes'=>''
);
$this->saveTransaction();
$this->query->executeQuery('SELECT amount,investment_date
FROM investor_investments
WHERE investor_investments.investment_group='.$details['investment_group'].' AND investor_investments.investment_type='.INVESTMENT_RPA);
$this->query->updateQuery('UPDATE investor_investments SET
change_principal='.$this->query->records[0]['amount'].',
change_date="'.$this->query->records[0]['investment_date'].'"
WHERE investment_id='.$this->investment_id);
break;
}
}
}
if (empty($data[29]))
{
continue;
}
$this->query->executeQuery('SELECT DATE_ADD("'.$data[24].'",INTERVAL '.$data[27].' MONTH) as next_date');
if($data[29]=='Redeem' AND empty($data[32]) AND strtotime($data[28])>strtotime($this->query->records[0]['next_date']))
{
$this->addNextInvestement('Continue',$this->query->records[0]['next_date'],$data[25],$data[26],$data[27]);
}
if (empty($data[34]) AND empty($data[35]) AND empty($data[36]))
{
//continue;
}
$this->addNextInvestement($data[29],$data[28],$data[34],$data[35],$data[36]);
switch(TRUE)
{
case $data[29]=='Coupon' AND empty($data[32]):
{
if (empty($data[33]) OR empty($data[34]) OR empty($data[35]) OR empty($data[36]))
{
echo ('No data for Continue for -'.__LINE__.'-'.$data[0]."\r\n");
continue;
}
$this->addNextInvestement('Continue',$data[33],$data[34],$data[35],$data[36]);
break;
}
case !empty($data[32]):{
$_POST=array(
'trans_year'=>date('Y',strtotime($data[33])),
'trans_month'=>date('m',strtotime($data[33])),
'trans_date'=>date('d',strtotime($data[33])),
'investment_type'=>INVESTMENT_RPA,
'investment_group'=>'new',
'investor_id'=>$this->user_id,
'currency_id'=>$currencies[$data[4]],
'amount'=>$data[32],
'buy_rate'=>$data[34]*100,
'commission'=>$data[35]*100,
'months'=>$data[36],
'notes'=>''
);
$this->saveTransaction();
break;
}
}
}
}
function addNextInvestement($type,$date=NULL,$buy_rate=NULL,$commission=NULL,$months=NULL)
{
$type=ucfirst(strtolower($type));
switch($type)
{
case 'Coupon':{
$details=$this->investor_actions->getInvestmentDetails($this->investment_group,$this->user_id);
if (!empty($date))
{
if ($commission=='20130624000021')
{
$_POST=array(
'trans_year'=>date('Y',strtotime($date)),
'trans_month'=>date('m',strtotime($date)),
'trans_date'=>date('d',strtotime($date)),
'investment_type'=>INVESTMENT_COUPON,
'investment_group'=>$this->investment_group,
'investor_id'=>$this->user_id,
'currency_id'=>$details['currency_id'],
'amount'=>$details['amount']-$buy_rate,
'buy_rate'=>NULL,
'commission'=>NULL,
'months'=>NULL,
'notes'=>''
);
$this->saveTransaction();
$_POST=array(
'trans_year'=>date('Y',strtotime($date)),
'trans_month'=>date('m',strtotime($date)),
'trans_date'=>date('d',strtotime($date)),
'investment_type'=>INVESTMENT_REDEEMED,
'investment_group'=>$this->investment_group,
'investor_id'=>$this->user_id,
'currency_id'=>$details['currency_id'],
'amount'=>$buy_rate,
'notes'=>''
);
$this->saveTransaction();
}
}
else
{
$_POST=array(
'trans_year'=>date('Y',strtotime($details['completed_at'])),
'trans_month'=>date('m',strtotime($details['completed_at'])),
'trans_date'=>date('d',strtotime($details['completed_at'])),
'investment_type'=>INVESTMENT_COUPON,
'investment_group'=>$this->investment_group,
'investor_id'=>$this->user_id,
'currency_id'=>$details['currency_id'],
'amount'=>$details['yield'],
'buy_rate'=>NULL,
'commission'=>NULL,
'months'=>NULL,
'notes'=>''
);
$this->saveTransaction();
}
break;
}
case 'Continue':{
$details=$this->investor_actions->getInvestmentDetails($this->investment_group,$this->user_id);
$_POST=array(
'trans_year'=>date('Y',strtotime($date)),
'trans_month'=>date('m',strtotime($date)),
'trans_date'=>date('d',strtotime($date)),
'investment_type'=>INVESTMENT_CONTINUE,
'investment_group'=>$this->investment_group,
'investor_id'=>$this->user_id,
'currency_id'=>$details['currency_id'],
'amount'=>$details['amount']+$details['yield'],
'buy_rate'=>$buy_rate*100,
'commission'=>$commission*100,
'months'=>$months,
'notes'=>''
);
$this->saveTransaction();
break;
}
case 'Redeem':{
if (!empty($date))
{
$_REQUEST['end_date']=$date;
}
$details=$this->investor_actions->getInvestmentDetails($this->investment_group,$this->user_id);
if (empty($date))
{
$date=$details['completed_at'];
}
$_POST=array(
'trans_year'=>date('Y',strtotime($date)),
'trans_month'=>date('m',strtotime($date)),
'trans_date'=>date('d',strtotime($date)),
'investment_type'=>INVESTMENT_REDEEMED,
'investment_group'=>$this->investment_group,
'investor_id'=>$this->user_id,
'currency_id'=>$details['currency_id'],
'amount'=>$details['amount']+$details['yield'],
'notes'=>''
);
$this->saveTransaction();
break;
}
case 'Compound':{
$details=$this->investor_actions->getInvestmentDetails($this->investment_group,$this->user_id);
$_POST=array(
'trans_year'=>date('Y',strtotime($details['expected_date'])),
'trans_month'=>date('m',strtotime($details['expected_date'])),
'trans_date'=>date('d',strtotime($details['expected_date'])),
'investment_type'=>INVESTMENT_COMPOUND,
'investment_group'=>$this->investment_group,
'investor_id'=>$this->user_id,
'currency_id'=>$details['currency_id'],
'amount'=>$details['amount']+$details['yield'],
'buy_rate'=>$buy_rate*100,
'commission'=>$commission*100,
'months'=>$months,
'notes'=>''
);
$this->saveTransaction();
break;
}
}
if (empty($details))
{
var_dump($details,$type);
}
return $details;
}
function getInvestementId()
{
$this->query->executeQuery('SELECT MAX(investment_id) as investment_id, MAX(investment_group) as investment_group
FROM investor_investments');
$this->investment_group=$this->query->records[0]['investment_group'];
$this->investment_id=$this->query->records[0]['investment_id'];
}
function saveTransaction()
{
$this->investor_actions->saveTransaction();
$this->getInvestementId();
$_REQUEST=array(
'investment_id'=>$this->investment_id
);
$this->investor_actions->authoriseTransaction();
}
}
$action = new Bug_271();
$action->loadData();
?>