<?php
class Transaction{

    public $model;
    
    public function __construct(){
        $this->model = new Model();
    }

    public function getTransactionInfo($condition){
        return $this->model->findOne("accounts", $condition);
    }

    public function getRowsNumber($table){
        return count($this->model->findAll($table));
    }

    public function checkIfTransactionExists($condition){
        return count($this->model->findOne("accounts", $condition)) > 0 ? true : false;
    }

    public function registerTransaction($fields, $values){
        return $this->model->insertdata("accounts", $fields, $values);
    }
    public function retrieveAllTransactions($pageno, $limit){
        $ats = $this->model->paginate("accounts", " 1 ORDER BY transactiondate ASC", $pageno, $limit);
        return $ats;
    }
    public function retrieveTransactionsByStatus($status, $pageno, $limit){
        $ats = $this->model->paginate("accounts", "status LIKE '$status' ORDER BY transactiondate ASC", $pageno, $limit);
        return $ats;
    }
    public function retrieveTransactionByQuery($query, $pageno, $limit,$field = "*"){
        $ats = $this->model->paginate("accounts", $query, $pageno, $limit,$field);
        return $ats;
    }
    
    public function retrieveTransactionByDate($cdate, $pageno, $limit){
        $ats = $this->model->paginate("accounts", "transactiondate = '$cdate' ORDER BY transactiondate ASC", $pageno, $limit);
        return $ats;
    }

    public function retrieveTransaction($id, $location, $field){
        return $this->model->findOne("accounts", "id = '$id'", "$field");
    }

    public function updateTransactionDetails($query, $id){
        return $this->model->update('accounts', $query, "WHERE id = $id");
    }
    
    /***** End ofTransactions **********************************************************************/
    

    
    
    public function executeTransactionByQuerySelector($query){
        $ats = $this->model->exec_query($query);
        return $ats;
    }
    
    public function saveTransaction($accountnumber,$transactiondate,$location,$credit,$debit,$description,$user,$status,$ttype,$debittotal,
    $credittotal,$ref,$ownerid,$currency,$accountname,$feenameid,$propertyid,$discount,$paymentmethod="NONCASH",$otherdetails=""){
        
        $fields = "accountnumber,accountname,description,ownerid,credit,debit,transactiondate,reference,feenameid,propertyid,
        status,tlog,discount,user,ttype,otherdetail,paymentmethod";
        
        $accountnumber = ((isset($accountnumber) && $accountnumber !== "") ? $accountnumber : "-" );
        if($accountnumber === "-"){
            exit(badRequest(204, "Not successful. Bad account number in savetransaction"));
        }        
        $values = "'$accountnumber','$accountname',
        '" . $description . "'," . $ownerid . ",
        $credit,$debit,        
        '" . ((isset($transactiondate) && $transactiondate !== "") ? $transactiondate : date('Y-m-d') ) . "',
        '" . $ref . "',
        " . $feenameid . ",
        " . $propertyid . ",
        '$status',
        '" . $user . " " . date('Y-m-d H:i:s') . "|" . $otherdetails . "',
        '" . $discount . "',
        '" . $user . "',
        '" . $ttype . "',
        '" . $otherdetail . "',
        '" . $paymentmethod . "'";
        
        $result = $this->registerTransaction($fields, $values);   
        return $result;
    }    
}
?>