<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Market_listings_model extends CI_Model {

    public function get_totalcount() {
        $sta = 'A';
        $this->db->where('status', $sta);
        $num = $this->db->count_all_results('engines_list');
        return $num;
    }
    
    public function get_totalcount_admin() {
        $num = $this->db->count_all_results('engines_list');
        return $num;
    }
    
    public function get_elistings_admin($limit, $start, $sort_by, $sort_order) {

        $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
        $sort_columns = array('manufacturer', 'engine_model', 'esn', 'lister', 'transaction', 'location', 'asking_price', 'monthly_rent', 'available_date', 'contact_name', 'email', 'phone');
        $sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'manufacturer';

//        $this->db->limit($limit, $start);
//        $query = $this->db->order_by($sort_by, $sort_order)
//                ->where('status !=', $st)
//                ->get('engines_list');

        $this->db->select('
            engines_list.list_id,
            engines_list.manufacturer,
            engines_list.engine_model,
            engines_list.esn,
            engines_list.lister,
            engines_list.transaction,
            engines_list.location,
            engines_list.asking_price,
            engines_list.monthly_rent,
            engines_list.available_date,
            users.email,
            users.first_name,
            users.last_name,
            users.phone
        ');
        
        $this->db->from('engines_list');
        $this->db->join('users', 'users.id = engines_list.id');
        $this->db->limit($limit, $start);
        $this->db->order_by($sort_by, $sort_order);
        
        $query = $this->db->get();
        
        
        if ($query->num_rows() > 0) {
            return $query->result();
        }
        return false;
    }

    public function get_elistings($limit, $start, $sort_by, $sort_order) {

        $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
        $sort_columns = array('manufacturer', 'engine_model', 'esn', 'lister', 'transaction', 'location', 'asking_price', 'monthly_rent', 'available_date', 'contact_name', 'email', 'phone');
        $sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'manufacturer';
        $st = 'D';

//        $this->db->limit($limit, $start);
//        $query = $this->db->order_by($sort_by, $sort_order)
//                ->where('status !=', $st)
//                ->get('engines_list');

        $this->db->select('
            engines_list.list_id,
            engines_list.manufacturer,
            engines_list.engine_model,
            engines_list.esn,
            engines_list.lister,
            engines_list.transaction,
            engines_list.location,
            engines_list.asking_price,
            engines_list.monthly_rent,
            engines_list.available_date,
            users.email,
            users.first_name,
            users.last_name,
            users.phone
        ');
        
        $this->db->from('engines_list');
        $this->db->join('users', 'users.id = engines_list.id');
        $this->db->where('status !=', $st);
        $this->db->limit($limit, $start);
        $this->db->order_by($sort_by, $sort_order);
        
        $query = $this->db->get();
        
        
        if ($query->num_rows() > 0) {
            return $query->result();
        }
        return false;
    }

    public function get_elisting_data($listing_id) {
        
        $this->db->select('
            engines_list.list_id,
            engines_list.manufacturer,
            engines_list.engine_model,
            engines_list.esn,
            engines_list.tsn,
            engines_list.csn,
            engines_list.tso,
            engines_list.cso,
            engines_list.first_limiter,
            engines_list.limiting_module,
            engines_list.last_operator,
            engines_list.lister,
            engines_list.transaction,
            engines_list.location,
            engines_list.asking_price,
            engines_list.monthly_rent,
            engines_list.available_date,
            engines_list.date_listed,
            engines_list.mini_pack_folder,
            engines_list.mini_pack_name,
            users.email
        ');
        
        $this->db->from('engines_list');
        $this->db->join('users', 'users.id = engines_list.id');
        $this->db->where('list_id', $listing_id);
        $query = $this->db->get();
        return $query->row();
    }
    
    public function get_selisting_data($listing_id) {
        
        $this->db->select('
            engines_list.list_id,
            engines_list.manufacturer,
            engines_list.engine_model,
            engines_list.esn,
            engines_list.tsn,
            engines_list.csn,
            engines_list.tso,
            engines_list.cso,
            engines_list.first_limiter,
            engines_list.limiting_module,
            engines_list.last_operator,
            engines_list.lister,
            engines_list.transaction,
            engines_list.location,
            engines_list.asking_price,
            engines_list.monthly_rent,
            engines_list.available_date,
            engines_list.date_listed,
            engines_list.mini_pack_folder,
            engines_list.mini_pack_name,
            users.email
        ');
        
        $this->db->from('engines_list');
        $this->db->join('users', 'users.id = engines_list.id');
        
        $this->db->where('list_id', $listing_id);
        $query = $this->db->get();
        return $query->row();
    }

    public function get_ptotalcount() {
        $sta = 'A';
        $this->db->where('status', $sta);
        $num = $this->db->count_all_results('engine_parts_list');
        return $num;
    }
    
    public function get_ptotalcount_admin() {
        $num = $this->db->count_all_results('engine_parts_list');
        return $num;
    }
    
    public function get_plistings_admin($part_c, $crit, $limit, $pstart, $sort_by, $sort_order) {
        
        if($part_c != '' || $crit != '') {
            
        
        $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
        $sort_columns = array('part_number', 'part_description', 'part_condition', 'enginep_model', 'tag', 'quantity', 'lister', 'transaction', 'available_date', 'contact_name', 'email', 'phone');
        $sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'part_number';

        //results query
        $query = $this->db->select('
                 engine_parts_list.list_id,
                 engine_parts_list.part_number,
                 engine_parts_list.part_description,
                 engine_parts_list.part_condition,
                 engine_parts_list.enginep_model,
                 engine_parts_list.tag,
                 engine_parts_list.quantity,
                 engine_parts_list.lister,
                 engine_parts_list.transaction,
                 engine_parts_list.asking_price,
                 engine_parts_list.available_date,
                 users.email,
                 users.first_name,
                 users.last_name,
                 users.phone
                
                ')
                ->from('engine_parts_list')
                ->join('users', 'users.id = engine_parts_list.id')
                ->limit($limit, $pstart)
                ->order_by($sort_by, $sort_order)
                ->like($part_c, $crit);

        $ret['rows'] = $query->get()->result();

//        if ($ret->num_rows() > 0) {
//            return $ret;
//        }
//        return false;

        return $ret;
        }
        
        else
            return false;
    }

    public function get_plistings($part_c, $crit, $limit, $pstart, $sort_by, $sort_order) {
        
        if($part_c != '' || $crit != '') {
            
        
        $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
        $sort_columns = array('part_number', 'part_description', 'part_condition', 'enginep_model', 'tag', 'quantity', 'lister', 'transaction', 'available_date', 'contact_name', 'email', 'phone');
        $sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'part_number';
        $st = 'D';

        //results query
        $query = $this->db->select('
                 engine_parts_list.list_id,
                 engine_parts_list.part_number,
                 engine_parts_list.part_description,
                 engine_parts_list.part_condition,
                 engine_parts_list.enginep_model,
                 engine_parts_list.tag,
                 engine_parts_list.quantity,
                 engine_parts_list.lister,
                 engine_parts_list.transaction,
                 engine_parts_list.asking_price,
                 engine_parts_list.available_date,
                 users.email,
                 users.first_name,
                 users.last_name,
                 users.phone
                
                ')
                ->from('engine_parts_list')
                ->join('users', 'users.id = engine_parts_list.id')
                ->limit($limit, $pstart)
                ->order_by($sort_by, $sort_order)
                ->where('status !=', $st)
                ->like($part_c, $crit);

        $ret['rows'] = $query->get()->result();

//        if ($ret->num_rows() > 0) {
//            return $ret;
//        }
//        return false;

        return $ret;
        }
        
        else
            return false;
    }

    public function get_plisting_data($listing_id) {
        
        $this->db->select('
                 engine_parts_list.list_id,
                 engine_parts_list.part_number,
                 engine_parts_list.part_description,
                 engine_parts_list.part_condition,
                 engine_parts_list.enginep_model,
                 engine_parts_list.tag,
                 engine_parts_list.quantity,
                 engine_parts_list.lister,
                 engine_parts_list.transaction,
                 engine_parts_list.asking_price,
                 engine_parts_list.monthly_rent,
                 engine_parts_list.available_date,
                 engine_parts_list.date_listed,
                 engine_parts_list.record_name,
                 engine_parts_list.record_folder,
                 users.email
                
                ');
        
        
        $this->db->from('engine_parts_list');
        $this->db->join('users', 'users.id = engine_parts_list.id');
        
        $this->db->where('list_id', $listing_id);
        $query = $this->db->get();
      
        return $query->row();
    }

    public function search_plistings_count($part_c, $crit) {
        if($part_c != '' || $crit != '') {
        $st = 'D';
        $query1 = $this->db->select('COUNT(*) as count', FALSE)
                ->from('engine_parts_list')
                ->where('status !=', $st)
//                 ->like($search_term['column'], $search_term['criteria']);
                ->like($part_c, $crit);
        $temp1 = $query1->get()->result();
        $ret1['num_rows'] = $temp1[0]->count;
        return $ret1;
        }
        else
            return false;
    }
    
    public function search_plistings_count_admin($part_c, $crit) {
        if($part_c != '' || $crit != '') {
        $query1 = $this->db->select('COUNT(*) as count', FALSE)
                ->from('engine_parts_list')
//                 ->like($search_term['column'], $search_term['criteria']);
                ->like($part_c, $crit);
        $temp1 = $query1->get()->result();
        $ret1['num_rows'] = $temp1[0]->count;
        return $ret1;
        }
        else
            return false;
    }
    
    
    
    public function search_elistings_count($manuf, $crite) {
        if($manuf != '' || $crite != '') {
        $st = 'D';
        $query2 = $this->db->select('COUNT(*) as count', FALSE)
                ->from('engines_list')
                ->where('status !=', $st)
//                 ->like($search_term['column'], $search_term['criteria']);
                ->like($manuf, $crite);
        $temp2 = $query2->get()->result();
        $ret2['num_rows'] = $temp2[0]->count;
        return $ret2;
        }
        else
            return false;
    }
    
    public function search_elistings_count_admin($manuf, $crite) {
        if($manuf != '' || $crite != '') {
        $query2 = $this->db->select('COUNT(*) as count', FALSE)
                ->from('engines_list')
//                 ->like($search_term['column'], $search_term['criteria']);
                ->like($manuf, $crite);
        $temp2 = $query2->get()->result();
        $ret2['num_rows'] = $temp2[0]->count;
        return $ret2;
        }
        else
            return false;
    }
    
    public function get_selistings($manuf, $crite, $limit, $pstart, $sort_by, $sort_order) {
        if($manuf != '' || $crite != '') {
        $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
        $sort_columns = array('manufacturer', 'engine_model', 'esn', 'lister', 'transaction', 'location', 'asking_price', 'monthly_rent', 'available_date', 'contact_name', 'email', 'phone');
        $sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'manufacturer';
        $st = 'D';

        //results query
  $query3 = $this->db->select('
            engines_list.list_id,
            engines_list.manufacturer,
            engines_list.engine_model,
            engines_list.esn,
            engines_list.lister,
            engines_list.transaction,
            engines_list.location,
            engines_list.asking_price,
            engines_list.monthly_rent,
            engines_list.available_date,
            users.email,
            users.first_name,
            users.last_name,
            users.phone
        ')
                ->from('engines_list')
                ->join('users', 'users.id = engines_list.id')
                ->limit($limit, $pstart)
                ->order_by($sort_by, $sort_order)
                ->where('status !=', $st)
                ->like($manuf, $crite);

        $ret3['rows'] = $query3->get()->result();

//        if ($ret->num_rows() > 0) {
//            return $ret;
//        }
//        return false;

        return $ret3;
    }
    
    return false;
    }
    
    
    public function get_selistings_admin($manuf, $crite, $limit, $pstart, $sort_by, $sort_order) {
        if($manuf != '' || $crite != '') {
        $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
        $sort_columns = array('manufacturer', 'engine_model', 'esn', 'lister', 'transaction', 'location', 'asking_price', 'monthly_rent', 'available_date', 'contact_name', 'email', 'phone');
        $sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'manufacturer';

        //results query
  $query3 = $this->db->select('
            engines_list.list_id,
            engines_list.manufacturer,
            engines_list.engine_model,
            engines_list.esn,
            engines_list.lister,
            engines_list.transaction,
            engines_list.location,
            engines_list.asking_price,
            engines_list.monthly_rent,
            engines_list.available_date,
            users.email,
            users.first_name,
            users.last_name,
            users.phone
        ')
                ->from('engines_list')
                ->join('users', 'users.id = engines_list.id')
                ->limit($limit, $pstart)
                ->order_by($sort_by, $sort_order)
                ->like($manuf, $crite);

        $ret3['rows'] = $query3->get()->result();

//        if ($ret->num_rows() > 0) {
//            return $ret;
//        }
//        return false;

        return $ret3;
    }
    
    return false;
    }
    
    

}
