File: /www/exchange2/exchange/custom_actions/reconcile_war_management.php
<?php
set_time_limit(0);
include_once('../Bin/constants.php');
include_once('../Bin/init.php');
require_once(ROOT_FOLDER.'Bin/Security.php');
require_once(ROOT_FOLDER.'admin/classes/Model/MTransactions.php');
class Reconcile_WarManagement{
private $config = array();
private $debtorsIDs = array();
private $invoicesIDs = array();
private $db;
public function __construct($config)
{
$this->db = Bin_Db::connect();
$this->db->setSaveQueries(true);
$this->initConfig($config);
}
private function initConfig($config){
$lines = explode("\n",$config);
foreach ($lines as $line){
list($debtorId, $tradeId, $amount) = explode("\t",$line);
$this->config[trim($debtorId)][trim($tradeId)] = $amount;
}
//Fetch invoices IDs
$sql = "SELECT trade_reference_id,invoice_id from invoice_master WHERE trade_reference_id IN (:trades)";
$result = $this->db->query($sql,array(':trades'=>implode(',',array_keys($this->getTrades()))));
foreach ($result->getResultArray() as $row){
$this->invoicesIDs[$row['trade_reference_id']] = $row['invoice_id'];
}
//Fetch debtorsID
$sql = "SELECT debtor_id,debtor_reference_id from debtors_detail WHERE debtors_detail.debtor_reference_id IN (:debtors)";
$result = $this->db->query($sql,array(':debtors'=>implode(',',$this->getDebtors())));
foreach ($result->getResultArray() as $row){
$this->debtorsIDs[$row['debtor_reference_id']] = $row['debtor_id'];
}
}
private function getCommission(){
return 2547.77;
}
private function getOCPAId(){
return 71302;
}
private function getOriginatorId(){
return 281;
}
private function getTrades(){
$res = array();
foreach ($this->config as $debtorId=>$trades){
foreach ($trades as $tn=>$tv){
$res[$tn] = $tv;
}
}
return $res;
}
private function getDebtorICPAmount($debtorRef){
$trades = $this->getDebtorTrades($debtorRef);
$debtorAmount = array_sum($trades);
$debtorCommissionAmount = count($trades) * $this->getCommission();
return round($debtorAmount+$debtorCommissionAmount,2);
}
private function getDebtors(){
return array_keys($this->config);
}
private function getDebtorTrades($debtorId){
return $this->config[$debtorId];
}
public function run(){
//echo "Updating Commissions: ";
//$this->updateCommissions($this->getCommission()); echo "Ok <br>";
//echo "Creating OCPA to ICP: ";
//$this->createOCPAtoICPTransactions(); echo "Ok <br>";
echo "Reconciling ICP: ";
$this->createReconcileTransactions(); echo "Ok <br>";
}
private function updateCommissions($value){
$sql = "UPDATE invoice_master SET reserve='$value' WHERE trade_reference_id IN (:trades)";
$this->db->query($sql,array(':trades'=>implode(',',array_keys($this->getTrades()))));
}
private function createOCPAtoICPTransactions(){
$debtorsAmounts = array();
foreach ($this->getDebtors() as $debtor){
$debtorsAmounts[$this->debtorsIDs[$debtor]] = $this->getDebtorICPAmount($debtor);
}
$transaction = new OCPA2ICPTransaction($this->getOriginatorId());
$transaction->setOcpaId($this->getOCPAId());
$transaction->setCreditorsAmounts($debtorsAmounts);
return $transaction->runTransaction(true);
}
private function findDebtorICPTransaction($debtorRef){
$sql = "SELECT manual_transaction_id FROM manual_transactions WHERE debtor_id=:debtor AND amount=:amount AND reconcile_credit!=1";
$result = $this->db->query($sql,array(':debtor'=>$this->debtorsIDs[$debtorRef],':amount'=>$this->getDebtorICPAmount($debtorRef)));
return $result->getColumn('manual_transaction_id');
}
private function createReconcileTransactions(){
foreach ($this->getDebtors() as $debtor){
$debtorId = $this->debtorsIDs[$debtor];
//We need to fetch invoices IDS
$trades = $this->getDebtorTrades($debtor);
//Make invoices
$invoices = array();
foreach ($trades as $tradeRef=>$tradeAmount){
$invoices[] = $this->invoicesIDs[$tradeRef];
}
$tr = new ReconcileCreditorTransaction($this->getOriginatorId());
$tr->setDebtorId($debtorId);
$tr->setAmount($this->getDebtorICPAmount($debtor));
$tr->setInvoices($invoices);
$tr->setICPTransaction($this->findDebtorICPTransaction($debtor));
$tr->runTransaction(true);
}
}
}
abstract class AbstractTransaction extends Model_MTransactions{
private $transactionDate;
private $originatorId;
private $notes = '';
protected function getTransactionDate(){
return $this->transactionDate->format('Y-m-d');
}
public function setOriginatorId($originatorId)
{
$this->originatorId = $originatorId;
}
public function __construct($originatorId)
{
$this->setOriginatorId($originatorId);
$this->transactionDate = new DateTime();
}
protected function getOriginatorID(){
return $this->originatorId;
}
protected function getNotes(){
return $this->notes;
}
protected function getCurrentUser(){
return 1;
}
public function runTransaction($auth=false){
$transactionId = $this->saveManualTransaction();
if($transactionId==false){
echo "Transaction Error(".get_class($this)."):".Bin_Db::connect()->getLastError();
return;
}
if($auth) {
$this->authoriseTransaction($transactionId);
}
return $transactionId;
}
}
class OCPA2ICPTransaction extends AbstractTransaction{
private $ocpaId;
/**
* @var array
*/
private $creditorsAmounts;
/**
* @param array $creditorsAmounts
*/
public function setCreditorsAmounts($creditorsAmounts)
{
$this->creditorsAmounts = $creditorsAmounts;
}
/**
* @param mixed $ocpaId
*/
public function setOcpaId($ocpaId)
{
$this->ocpaId = $ocpaId;
}
protected function getTransactionType(){
return OCPA_TO_ICP;
}
protected function getReferencedTransactionId(){
return $this->ocpaId;
}
protected function getOCPACreditorsAndAmounts(){
//Return []=>[c=>creditor_id, a=>amount]
$res = array();
foreach ($this->creditorsAmounts as $creditorId=>$creditorAmount){
$res[] = array('c'=>$creditorId,'a'=>$creditorAmount);
}
return $res;
}
}
class ReconcileCreditorTransaction extends AbstractTransaction{
private $invoices;
private $debtorId;
private $icpTransaction;
private $amount;
protected function getDebtorId(){
return $this->debtorId;
}
/**
* @param mixed $amount
*/
public function setAmount($amount)
{
$this->amount = $amount;
}
protected function getAmount(){
return $this->amount;
}
/**
* @param mixed $debtorId
*/
public function setDebtorId($debtorId)
{
$this->debtorId = $debtorId;
}
protected function hasSpecificDeductible(){
return false;
}
protected function hasTrailingBalance(){
return false;
}
public function setInvoices(array $invoices){
$this->invoices = $invoices;
}
public function setICPTransaction($transaction){
$this->icpTransaction = $transaction;
}
protected function getReconcileTransactionId(){
return $this->icpTransaction;
}
protected function getSelectedInvoices(){
return $this->invoices;
}
protected function getSelectedTransactions(){
return $this->invoices;
}
protected function getTransactionType()
{
return RECONCILE_CREDITOR_ACCOUNT;
}
}
//Debtor - Trade - FaceValu with conmissions
$config = <<<CONFIG
20150410000724 170410073940 3407.50
20150410000724 170502075305 6893.00
20150410000724 170511076189 5169.33
20150410000727 170502075306 3080.00
20150410000728 170530077595 40005.83
20150410000729 170227070511 1074.50
20150410000729 170426074844 3198.50
20150410000729 170515076382 2733.09
20150410000729 170515076385 1065.09
20150410000730 170109067119 4505.83
20150410000730 170515076375 1403.09
20150410000730 170601077899 85005.83
20150410000731 170308071390 4505.83
20150410000731 170509075932 10003.33
20150410000731 170515076368 10003.09
20150410000731 170515076369 3003.09
20150410000731 170515076370 3793.09
20150410000731 170515076395 4503.09
20150410000733 170303070891 5007.50
20150413000736 161203064712 6003.93
20150413000737 170314071767 7392.50
20150413000738 161212065415 19448.50
20150413000738 170207069108 21352.83
20150413000738 170410073942 12557.50
20150421000757 170202068749 9612.08
20150421000757 170503075395 19217.50
20150421000759 161101061415 10001.00
20150421000759 161203064709 3503.93
20150421000759 161203064715 125003.93
20150421000759 170308071387 12505.83
20150421000759 170505075674 1503.41
20150421000759 170505075680 8003.41
20150421000759 170505075681 11003.41
20150421000759 170505075683 8503.41
20150421000759 170505075684 1503.41
20150421000759 170505075687 15003.41
20150421000759 170505075690 603.41
20150421000759 170505075697 1503.41
20150421000759 170505075708 99362.50
20150421000759 170515076359 603.09
20150421000759 170515076361 1503.09
20150421000759 170515076363 603.09
20150421000759 170515076364 1503.09
20150421000759 170515076392 11003.09
20150421000759 170515076394 15003.09
20150421000763 161101061413 7501.00
20150421000763 161101061414 1111.00
20150421000763 161101061417 22001.00
20150421000763 161203064710 7503.93
20150421000763 161203064714 12003.93
20150421000763 170509075930 27503.33
20150421000763 170524077147 4995.00
20150422000773 161101061409 5001.00
20150422000773 161101061411 2501.00
20150422000773 161101061412 2501.00
20150422000773 161203064711 10003.93
20150424000781 161027061175 44282.00
20150428000786 170503075394 2615.00
20150512000843 170303070890 10007.50
20150512000844 170308071394 10005.83
20150512000845 170505075621 5003.41
20150518000853 161012060022 25840.00
20150518000853 161017060293 66432.50
20150518000853 170301070693 18462.50
20150518000853 170427074972 10012.50
20150518000853 170512076238 10298.33
20150518000853 170512076239 57813.33
20151222001323 170331073013 12512.50
20151222001323 170509075933 12503.33
20151222001323 170509075980 7503.33
20151222001323 170515076365 5003.09
20151222001323 170515076366 1003.09
20160401001939 170202068744 125005.83
20160401001939 170504075576 106003.41
20160415002032 170404073323 5007.50
20160415002032 170524077146 5305.00
20160415002033 170531077672 16605.83
20160806002720 170222070304 6522.50
20161020003091 161102061567 28503.33
20161020003091 161209065239 28012.50
20161020003091 170515076357 48103.09
20161020003091 170523076943 45305.00
20161027003128 161212065416 18348.03
20161028003134 161230066637 4512.50
20161101003139 161101061492 15001.00
20161101003139 161203064713 16503.93
20161101003140 161101061494 3001.00
20161101003140 170109067117 1005.83
20161101003141 161101061495 5001.00
20161101003141 170109067118 5005.83
20161102003148 161102061721 12010.00
20161102003148 170505075620 6003.41
20161102003148 170529077441 29277.50
20161203003286 161207064961 6671.00
20161203003287 161207064962 673.50
20170120003368 170202068762 4755.83
20170202003389 170207069109 14005.83
20170202003389 170208069172 4005.83
20170202003389 170509075935 13903.33
20170303003465 170314071766 10012.50
20170303003465 170404073322 40007.50
20170303003466 170308071380 10012.50
20170308003475 170320072004 1212.50
20170320003494 170321072112 20012.50
20170410003585 170411074048 1612.50
20170505003643 170509075965 6003.33
20170505003644 170509075936 6003.33
20170509003650 170512076264 5003.33
20170509003650 170515076396 8253.09
20170515003684 170524077137 11682.96
20170515003687 170518076599 3505.83
20170516003694 170518076595 10005.83
20170516003696 170518076598 1505.83
CONFIG;
$rwm = new Reconcile_WarManagement($config);
$rwm->run();