<?php
class ModelEntegrasyonManufacturer extends Model {


    public function manufacturerSettingDelete($code){


       

        if ($code == "all" ){

            $sql="DELETE FROM manufacturer_setting ";


        }else {
            $sql="UPDATE manufacturer_setting SET ".$code."= ''";

        }

        $this->db->query($sql);

    }

    public function deleteManufacturer($manufacturer_id) {
        $this->db->query("DELETE FROM `" . DB_PREFIX . "manufacturer_setting` WHERE manufacturer_id = '" . (int)$manufacturer_id . "'");
        $this->db->query("DELETE FROM `" . DB_PREFIX . "manufacturer_to_store` WHERE manufacturer_id = '" . (int)$manufacturer_id . "'");

        $this->cache->delete('manufacturer');
    }

    public function getManufacturers($data = array()) {
        $sql = "select p.manufacturer_id, p.manufacturer_name as name ,m.n11,m.ty,m.hb,m.eptt,m.gg,m.cs from product p left JOIN manufacturer_setting m on(p.manufacturer_id = m.manufacturer_id)  ";

        if (!empty($data['filter_manufacturer'])) {
            $sql .= " WHERE m.manufacturer_id = '" . $data['filter_manufacturer'] . "'";
        }

        $sort_data = array(
            'name',
            'sort_order'
        );

            $sql .= " GROUP BY p.manufacturer_id ";
            
        if (isset($data['order']) && ($data['order'] == 'DESC')) {
            $sql .= " DESC";
        } else {
            $sql .= " ASC";
        }

        if (isset($data['start']) || isset($data['limit'])) {
            if ($data['start'] < 0) {
                $data['start'] = 0;
            }

            if ($data['limit'] < 1) {
                $data['limit'] = 20;
            }

            $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
        }



        try {
            $query = $this->udb->query($sql);

            return $query->rows;
        }catch (Exception $exception){

            echo $exception->getMessage();
        }


    }
    
    public function getTotalManufacturers($data) {
        $sql = "select count(*) as total from product p left JOIN manufacturer_setting m on(p.manufacturer_id = m.manufacturer_id)  ";
        if (!empty($data['filter_manufacturer'])) {

            $sql .= " WHERE manufacturer_id='" . $this->db->escape($data['filter_manufacturer']) . "'";
        }
        $query = $this->udb->query($sql);
        return $query->num_rows;
    }
}
