<?php
    /**
    * This class is responsible for creating, updating, and retrieving bank accounts.
    *
    * @copyright Trusto Tech 2015-2016
    * @author  Taha Sayyad <info@trustotech.com>
    * @package Account Receivable\libs\Classes
    *
    * @version 1.0.0
    */
    class BankAccount {
        /**
        * @var array Holds an array of data
        */
        protected $info = array();

        /**
        * @var obj Holds the database adapter object
        */
        private $db;

        /**
        * The constructor will create an instant of the chosen database adapter (MySQL, MySQLi, etc)
        * and saves it in a private variable $db as an object to be used throughout the class.
        *
        * @author  Taha Sayyad <info@trustotech.com>
        * @uses DBConnectorFactory::loadDriver() to load the database adapter
        */
        public function __construct() {
            $this->db = DBConnectorFactory::loadDriver();
        }

        public function setData($data) {
            $this->info = $data;
            return $this;
        }

        /**
        * Creates a new bank account.
        *
        * @author  Taha Sayyad <info@trustotech.com>
        * @uses BankAccount::validateBankAccount() to validate bank account data
        * @return bool True on success, otherwise false.
        */
        public function addNew() {
            $this->db->query("START TRANSACTION");
            $data = [
                &$this->info['Bank_Name'],
                &$this->info['Bank_Account_Name'],
                &$this->info['Bank_Account_Number'],
                &$this->info['Currency_ID'],
                &$this->info['Credit_Card_Number']
            ];
            $this->db->prepare_Stmt("
                    INSERT INTO
                        tbl_accounting_bank_accounts
                        (
                            Bank_Name,
                            Bank_Account_Name,
                            Bank_Account_Number,
                            Currency_ID,
                            Credit_Card_Number
                        )
                    VALUES
                        (
                            ?,?,?,?,?
                        )
                ",$data);
            $error = $this->db->error();
//            die(print_r($error));
            if(empty($error)){
                $this->db->query('COMMIT');
            }else{
                $this->db->query('ROLLBACK');
            }
            return array_unique($error);
        }

        /**
        * Returns bank account with different features.<br />
        * 1) If bank account id is not present, all bank accounts will be returned.<br />
        * 2) When the bank account id is present, the data of this specific bank account will be returned.
        * All data are returned in an array format.
        *
        * @author  Taha Sayyad <info@trustotech.com>
        * @param int $bank_account_id of bank account id
        * @return array An Array of bank account data
        */
        public function displayInfo($bank_account_id=null) {
            $condition = '';
            $data = array();
            if (!is_null($bank_account_id))
                $condition .= ' AND Bank_Account_ID = "' . $bank_account_id .'"';
            $query = $this->db->query(' SELECT 
                                            * 
                                        FROM 
                                            bank_list
                                        WHERE
                                            1 = 1' . $condition);
            $countRows = $this->db->rowsno($query);
            while ($row = $this->db->fetch_row($query)){
                $data[] = $row;
            }
            return $data;
        }

        public function getBankInfo() {
            $bank_account_id = $this->info['bank_account_id'];
            $data = array();
            $condition = '';
            if (!is_null($bank_account_id))
                $condition .= ' AND Bank_Account_ID = "' . $bank_account_id .'"';
            $query = $this->db->query(' SELECT 
                                            * 
                                        FROM 
                                            bank_list
                                        WHERE
                                            1 = 1' . $condition);
            while ($row = $this->db->assoc($query)){
                $data[0]['Bank_Name'] = $row['Bank_Name'];
                $data[0]['Bank_Account_Name'] = $row['Bank_Account_Name'];
                $data[0]['Bank_Account_Number'] = $row['Bank_Account_Number'];
                $data[0]['Credit_Card_Number'] = $row['Credit_Card_Number'];
                $data[0]['Currency_Name'] = $row['Currency_Name'];
                $data[0]['Bank_Account_ID'] = $row['Bank_Account_ID'];
            }
            return $data;
        }

        /**
        * Updates an bank account data.
        *
        * @author  Taha Sayyad <info@trustotech.com>
        * @param int $id of bank account
        * @uses BankAccount::validateBankAccount() to validate bank account data
        * @return bool True on success, otherwise false
        */
        public function updateInfo($id) {
            $this->db->query("START TRANSACTION");
            $data = [
                &$this->info['Bank_Account_Name'],
                &$this->info['Bank_Name'],
                &$this->info['Currency_ID'],
                &$this->info['Bank_Account_Number'],
                &$this->info['Credit_Card_Number'],
                &$id
            ];
              
            $this->db->prepare_Stmt("
                UPDATE
                    tbl_accounting_bank_accounts
              SET
                  Bank_Account_Name = ?,
                  Bank_Name = ?,
                  Currency_ID = ?,
                  Bank_Account_Number = ?,
                  Credit_Card_Number = ?
              WHERE
                  Bank_Account_ID = ?
              ",$data);
            $error = $this->db->error();
            if(empty($error)){
                $this->db->query('COMMIT');
            }else{
                $this->db->query('ROLLBACK');
            }
            return array_unique($error);
        }

  

        public function getBankID(){
        $query = $this->db->query('SELECT
                                      Bank_Account_ID
                                   FROM
                                      tbl_accounting_bank_accounts
                                   ORDER BY
                                      Bank_Account_ID DESC
                                   LIMIT 1
            ');
        while ($row = $this->db->assoc($query)){
            $rows = $row['Bank_Account_ID'];
        }
        return $rows;
    }
    }
