<?php

class syncModel extends mainModel {

//    protected $info = array();
    private $db;

    public function __construct() {
//        $registry = Registry::getInstance();
//        $registry->customer = new Customer();
        $this->db = DBConnectorFactory::loadDriver();
    }

//    public function setData($data) {
//        $this->info = $data;
//        return $this;
//    }
    public function customerSearch($term = null, $page, $limit) {
        $data = array();
        $data['is_more'] = false;
        $data['data'] = array();
        $cond = '';
        if (!is_null($term)) {
            $cond .=' where Customer_Name LIKE "%' . $term . '%"';
        }
        $query = $this->db->query('select 
                                    Customer_ID as id,
                                    Customer_Name as text
                                from 
                                    tbl_customers
                                ' . $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 accountSearch($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 . '%"  or Account_Arabic_Name LIKE  "%' . $term . '%")';
        }
        $query = $this->db->query('select 
                                    Account_ID as id,
                                    concat_ws(" - ",Account_Name,Account_Code,Account_Arabic_Name) as text
                                from 
                                    tbl_chart_of_accounts
                                    where Status_ID = 1
                                ' . $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 get_customer_data($customers) {
        $data = array('customer' => array(), 'accounts' => array());
        $query_main = $this->db->query('select * from tbl_customers where Customer_ID in (' . $customers . ')');
        while ($row = $this->db->assoc($query_main)) {
            $data['customer'][$row['Customer_ID']] = $row;
        }
        $query_acc = $this->db->query('select * from tbl_chart_of_accounts 
            where main_id in (' . $customers . ') and main_type="cust"');
        while ($row_acc = $this->db->assoc($query_acc)) {
            $data['accounts'][$row_acc['main_id']][] = $row_acc;
        }
        $query_contact = $this->db->query('select * from tbl_customer_supplier_contacts 
            where main_id in (' . $customers . ') and type_id=1');
        while ($row_contact = $this->db->assoc($query_contact)) {
            $data['contacts'][$row_contact['main_id']][] = $row_acc;
        }
//        echo"<pre>";
//        die(print_r($data));
        return $data;
    }

}
