<?php
class Contra {
    protected $info = array();
    public function __construct() {
        $this->db = DBConnectorFactory::loadDriver();
    }
    public function setData($data) {
        $this->info = $data;
        return $this;
    }
    public function create() { 
        $is_actual = 0;
//        $check = JV::checkCurrentYear($this->info['Contra_Date'],$this->info['Contra_Date'],$is_actual);
        $check = CommonFunctions::check_lock_data(['type'=>'contra','start_date'=>$this->info['Contra_Date'],'is_actual'=>$is_actual]);

        if($check !== ''){
            $error = array();
            $error[0] = $check;
            return $error;
        }
        $this->db->query("START TRANSACTION");
        $year_id = Session::getCurrentYearID();
        $data = [
            &$this->info['Transfer_From'],
            &$this->info['Transfer_To'],
            &$this->info['Transfer_Amount'],
            &$this->info['Transfered_Amount'],
            &$this->info['Contra_Date'],
            &$this->info['Contra_Note'],
            &$this->info['To_Currency'],
            &$this->info['From_Currency'],
            &$this->info[0],
            &$year_id
        ];
        $this->db->prepare_Stmt("
                                INSERT
                                INTO
                                    tbl_accounting_contra
                                (
                                    From_Account_ID,
                                    To_Account_ID,
                                    Transfer_Amount,
                                    Transfered_Amount,
                                    Contra_Date,
                                    Contra_Note,
                                    To_Currency,
                                    From_Currency,
                                    Logged_ID,
                                    current_year_id
                                )
                                    VALUES
                                (
                                    ?,?,?,?,?,?,?,?,?,?
                                )
        ",$data);
        $lastid = $this->db->id();
        $jv = JV::returnJV($is_actual);
        $account_from = JV::getAccountNameByID($this->info['Transfer_From']);
        $account_to = JV::getAccountNameByID($this->info['Transfer_To']);
        $desc = 'Transfer from '.$account_from.' to '.$account_to;
        $data_1 =["JV_ID" => $jv,
                    "Transaction_Reference" => 'con '.$lastid ,
                    "Transaction_Account" => $this->info['Transfer_From'],
                    "Transaction_Type" => 'Credit',
                    "Transaction_Amount" => $this->info['Transfer_Amount'],
                    "Transaction_Date" => $this->info['Contra_Date'],
                    "Currency_ID" => $this->info['From_Currency'],
                    "Logged_ID" => Session::getUserID(),
                    "Transaction_Description" => $desc,
                    "Current_Ex_Rate" => "0",
                    "main_id"=>$lastid,
                    "main_type"=>'con']; 
        JV::addTransaction($data_1,$is_actual);
        $data_2 =["JV_ID" => $jv,
                    "Transaction_Reference" => 'con '.$lastid ,
                    "Transaction_Account" => $this->info['Transfer_To'],
                    "Transaction_Type" => 'Debit',
                    "Transaction_Amount" => $this->info['Transfered_Amount'],
                    "Transaction_Date" => $this->info['Contra_Date'],
                    "Currency_ID" => $this->info['To_Currency'],
                    "Logged_ID" => Session::getUserID(),
                    "Transaction_Description" => $desc,
                    "Current_Ex_Rate" => "0",
                    "main_id"=>$lastid,
                    "main_type"=>"con"]; 
        JV::addTransaction($data_2,$is_actual);
        $error = $this->db->error();
        if(empty($error)){
            $this->db->query('COMMIT');
        }else{
            $this->db->query('ROLLBACK');
        }
        return array_unique($error);
    }

//    public function returnAccounts() {
//        $data = array();
//        $query = $this->db->query('SELECT
//                                        *
//                                    FROM
//                                        tbl_chart_of_accounts
//                                    WHERE
//                                        LEFT(Account_Code,3) = 512
//                                    OR
//                                        LEFT(Account_Code,2) = 53
//                ');
//        while ($row = $this->db->assoc($query))
//            $data[] = $row;
//        return $data;
//    }
// public function accSearch_5($term=null,$page,$limit){
//        $data = array();
//        $data['is_more'] = false;
//        $data['data'] = array();
//        $cond = '';
//        if(!is_null($term)){
//            $cond .=' AND ( Account_Name LIKE "%'.$term.'%" OR Account_Code LIKE "%'.$term.'%" )';
//        }
//        $query = $this->db->query('SELECT Account_Code as id,
//                                          concat(Account_Name," - ",Account_Code) as text
//                                    FROM 
//                                        tbl_chart_of_accounts 
//                                    WHERE (LEFT(Account_Code,3) = 512
//                                    OR    LEFT(Account_Code,2) = 53 )
//                                    '.$cond.'
//                                    limit '.($page*$limit).','.($limit+1) );
//
//        while($row = $this->db->assoc($query)){
//            $data['data'][] = $row;
//        }
//        if(count($data['data']) > $limit){
//            array_pop($data['data']);
//            $data['is_more'] = true;
//        }
//        return $data;
//        
//    }
//    public function view($id = null, $date=null, $month = null,$year = null,$from=null,$to=null) {
//        $data = array();
//        $condition = '';
//        if (!is_null($id))
//            $condition .= ' AND c.Contra_ID = "' . $id .'"';
//        if (!is_null($date))
//            $condition .= ' AND c.Contra_Date = "' . $date .'"';
//        if (!is_null($month))
//            $condition .= ' AND MONTH(c.Contra_Date) = "' . $month .'"';
//        if (!is_null($year))
//            $condition .= ' AND YEAR(c.Contra_Date) = "' . $year .'"';
//		if (!is_null($from) and !is_null($to))
//            $condition .= ' AND c.Contra_Date >= "' . $from .'" AND c.Contra_Date <= "' . $to .'"';
//        $query = $this->db->query('SELECT
//                                        f.Account_Name AS T_FROM,
//                                        t.Account_Name AS T_TO,
//                                        c.Transfer_Amount,
//                                        c.Transfered_Amount,
//                                        c.From_Currency,
//                                        c.To_Currency,
//                                        c.Contra_Date,
//                                        c.Contra_Note,
//                                        cy.Currency_Symbol AS Symbol_From,
//                                        ct.Currency_Symbol AS Symbol_To
//                                    FROM
//                                        tbl_accounting_contra c
//                                    JOIN
//                                        tbl_chart_of_accounts f
//                                    ON
//                                        c.Transfer_From = f.Account_Code
//                                    JOIN
//                                        tbl_chart_of_accounts t
//                                    ON
//                                        c.Transfer_To = t.Account_Code
//                                    JOIN
//                                        tbl_common_currency cy
//                                    ON
//                                        c.From_Currency = cy.Currency_ID
//                                    JOIN
//                                        tbl_common_currency ct
//                                    ON
//                                        c.To_Currency = ct.Currency_ID
//                                    WHERE
//                                        1 = 1' . $condition);
//        while ($row = $this->db->assoc($query)){
//            $data[] = $row;
//        }
//        return $data;
//    }
//    public function getContraID(){
//        $query = $this->db->query('SELECT
//                                      Contra_ID
//                                   FROM
//                                      tbl_accounting_contra
//                                   ORDER BY
//                                      Contra_ID DESC
//                                   LIMIT 1
//            ');
//        while ($row = $this->db->assoc($query)){
//            $rows = $row['Contra_ID'];
//        }
//        return $rows;
//    }
}
