<?php

class CommonInventory {

    protected $info = array();
    private $year_id;
    private $controller_permission;

    public function __construct() {
        $this->db = DBConnectorFactory::loadDriver();
        $this->year_id = Session::getCurrentYearID();
        $this->controller_permission = Session::getPermissionsObject();
    }

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

    public function get_db() {
        return $this->db;
    }

    public function submitGroup() {
        $group_id = $this->info['group_id'];
        if ($group_id == 0) {
            $this->db->prepare_Stmt('INSERT INTO tbl_general_groups
                                     (group_name,group_type)
                                     VALUES
                                    (?,?)
                                         ', [&$this->info['group_name'], &$this->info['group_type']]);
        } else {
            $this->db->prepare_Stmt('UPDATE tbl_general_groups
                                     SET
                                            group_name = ?
                                     WHERE 
                                            group_id =?
                                         ', [&$this->info['group_name'],&$group_id]);
        }
        $error = $this->db->error();
        return $error;
    } 
    public function deleteGroup() {
        $customers_query = $this->db->query('select * from tbl_customers where customer_group_id = "'.$this->info['group_id'].'"');
        $customers_row_count = $this->db->rowsno($customers_query);
        if ($customers_row_count > 0) {
            return [CommonLanguage::getMessage('customer_linked_group')];
        }  
        $suppliers_query = $this->db->query('select * from tbl_stock_suppliers where supplier_group_id = "'.$this->info['group_id'].'"');
        $suppliers_row_count = $this->db->rowsno($suppliers_query);
        if ($suppliers_row_count > 0) {
            return [CommonLanguage::getMessage('supplier_linked_group')];
        }
        $this->db->prepare_Stmt('Delete from tbl_general_groups WHERE group_id =? ', [&$this->info['group_id']]);
        $error = $this->db->error();
        return $error;
    }
    public function submitClass() {
        $class_id = $this->info['class_id'];
        if ($class_id == 0) {
            $this->db->prepare_Stmt('INSERT INTO tbl_general_classes
                                     (class_name,class_type)
                                     VALUES
                                    (?,?)
                                         ', [&$this->info['class_name'], &$this->info['class_type']]);
        } else {
            $this->db->prepare_Stmt('UPDATE tbl_general_classes
                                     SET
                                            class_name = ?
                                     WHERE 
                                            class_id =?
                                         ', [&$this->info['class_name'],&$class_id]);
        }
        $error = $this->db->error();
        return $error;
    }
    public function deleteClass() {
        $customers_query = $this->db->query('select * from tbl_customers where customer_class_id = "'.$this->info['class_id'].'"');
        $customers_row_count = $this->db->rowsno($customers_query);
        if ($customers_row_count > 0) {
            return [CommonLanguage::getMessage('customer_linked_class')];
        }  
        $suppliers_query = $this->db->query('select * from tbl_stock_suppliers where supplier_class_id = "'.$this->info['class_id'].'"');
        $suppliers_row_count = $this->db->rowsno($suppliers_query);
        if ($suppliers_row_count > 0) {
            return [CommonLanguage::getMessage('supplier_linked_class')];
        }
        $this->db->prepare_Stmt('Delete from tbl_general_classes WHERE class_id =? ', [&$this->info['class_id']]);
        $error = $this->db->error();
        return $error;
    }
    
     public function general_group_Search($term = null, $page, $limit, $cat = 0) {
        $data = array();
        $data['is_more'] = false;
        $data['data'] = array();
        $cond = '';
        $data_filter = array();
        if (!is_null($term)) {
            $cond .= ' and group_name LIKE ?';
            $data_filter[] = "%" . $term . "%";
        }
        $query = $this->db->prepare_Stmt('
        select 
            group_id as id,group_name as text
        from tbl_general_groups 
        where group_type ="'.$this->info['group_type'].'" 
        ' . $cond . ' limit ' . ($page * $limit) . ',' . ($limit + 1), $data_filter);
        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 general_class_Search($term = null, $page, $limit, $cat = 0) {
        $data = array();
        $data['is_more'] = false;
        $data['data'] = array();
        $cond = '';
        $data_filter = array();
        if (!is_null($term)) {
            $cond .= ' and class_name LIKE ?';
            $data_filter[] = "%" . $term . "%";
        }
        $query = $this->db->prepare_Stmt('
        select 
            class_id as id,class_name as text
        from tbl_general_classes
        where class_type ="'.$this->info['class_type'].'" 
        ' . $cond . ' limit ' . ($page * $limit) . ',' . ($limit + 1), $data_filter);
        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 submit_driver() {
        if ($this->info['driver_id'] == 0) {
            $this->db->prepare_Stmt('INSERT INTO `tbl_drivers`( `driver_name`) 
                VALUES (?) ', [&$this->info['driver_name']]);
        } else {
            $this->db->prepare_Stmt('UPDATE tbl_drivers SET driver_name =?
              WHERE driver_id =? ', [&$this->info['driver_name'], &$this->info['driver_id']]);
        }
        $error = $this->db->error();
        return array_unique($error);
    }

    public function delete_driver() {

        $query = $this->db->prepare_stmt('select * from tbl_invoices where driver_id = ?', [&$this->info['entry_value']]);
        $row_count = $this->db->rowsno($query);
        if ($row_count == 0) {
            $this->db->prepare_Stmt('Delete from tbl_drivers where driver_id = ?
                                         ', [&$this->info['entry_value']]);
            $error = $this->db->error();
        } else {
            $error = CommonLanguage::getMessage('driver_used');
        }

        return $error;
    }

}
