<?php

/*
 * Copyright (c) 2019 Earworks.
 * Codeigniter Project Template Modification
 * Please contact our email for more information :
 *  contact@earworks.co.id
 */
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Description of Offroad_model
 *
 * @author Andhy Taslin <andhy@earworks.co.id>
 * @since 1.0
 */
class Offroad_model extends CI_Model {

    /**
     * define selected table name
     */
    const TABLE = 'offroad';

    public function check($input = array()) {
        $this->db->from(self::TABLE);
        if (isset($input['id']) && !empty($input['id'])) {
            $this->db->where('id', $input['id']);
        }
        if (isset($input['registerid']) && !empty($input['registerid'])) {
            $this->db->where('registerid', $input['registerid']);
        }
        if (isset($input['vehicleid']) && !empty($input['vehicleid'])) {
            $this->db->where('vehicleid', $input['vehicleid']);
        }
        if (isset($input['status']) && !empty($input['status'])) {
            $this->db->where('status', $input['status']);
        }
        return $this->db->count_all_results();
    }

    public function get($input = array()) {
        $select = array(
            'offroad.id', 'offroad.status', 'register.id as registerid', 'offroad.ordernumber',
            'register.name', 'register.company', 'register.type',
            'vehicle.id as vehicleid', 'vehicle.name as vehiclename',
            'vehicle.status as vehiclestatus', 'vehicle.testtype', 'offroad.modified'
        );
        $this->db->select(implode(', ', $select));
        $this->db->from(self::TABLE);
        $this->db->join('register', 'offroad.registerid = register.id', 'left');
        $this->db->join('vehicle', 'offroad.vehicleid = vehicle.id', 'left');
        if (isset($input['status']) && !empty($input['status'])) {
            $this->db->where('offroad.status', $input['status']);
        }
        if (isset($input['cardid']) && !empty($input['cardid'])) {
            $this->db->where('register.cardid', $input['cardid']);
        }
        if (isset($input['vehicleid']) && !empty($input['vehicleid'])) {
            $this->db->where('vehicle.id', $input['vehicleid']);
        }
        if (isset($input['vehiclestatus']) && !empty($input['vehiclestatus'])) {
            $this->db->where('vehicle.status', $input['vehiclestatus']);
        }
        if (isset($input['ids']) && !empty($input['ids'])) {
            $this->db->where_in('offroad.id', $input['ids']);
        }
        if (isset($input['orderby']) && !empty($input['orderby'])) {
            $this->db->order_by($input['orderby']);
        }
        $query = $this->db->get();
        return $query->result_array();
    }

    public function detail($input = array()) {
        $select = array(
            'offroad.id', 'offroad.status', 'register.id as registerid',
            'register.name', 'register.company', 'register.type', 'offroad.ordernumber',
            'vehicle.id as vehicleid', 'vehicle.name as vehiclename', 'vehicle.testtype',
        );
        $this->db->select(implode(', ', $select));
        $this->db->from(self::TABLE);
        $this->db->join('register', 'offroad.registerid = register.id', 'left');
        $this->db->join('vehicle', 'offroad.vehicleid = vehicle.id', 'left');
        if (isset($input['status']) && !empty($input['status'])) {
            $this->db->where('offroad.status', $input['status']);
        }
        $func = 'result_array';
        if (isset($input['id']) && !empty($input['id'])) {
            $this->db->where('offroad.id', $input['id']);
            $func = 'row_array';
        }
        $query = $this->db->get();
        return $query->$func();
    }

    public function getmaxorder($input = array()) {
        $select = array(
            'max(offroad.ordernumber) as `maxvehicleorder`'
        );
        $this->db->select(implode(', ', $select));
        $this->db->from(self::TABLE);
        $this->db->join('register', 'offroad.registerid = register.id', 'left');
        if (isset($input['vehicleid']) && !empty($input['vehicleid'])) {
            $this->db->where('offroad.vehicleid', $input['vehicleid']);
        }
        if (isset($input['cardid']) && !empty($input['cardid'])) {
            $this->db->where('register.cardid', $input['cardid']);
        }
        $query = $this->db->get();
        return $query->row_array();
    }

    public function insert($input = array(), $return_id = false, $batch = false) {
        $user = $this->session->userdata(CI_NAME . 'log');
        $insert = array();
        if ($batch === false) {
            if (isset($input['registerid']) && !empty($input['registerid'])) {
                $insert['registerid'] = $input['registerid'];
            }
            if (isset($input['vehicleid']) && !empty($input['vehicleid'])) {
                $insert['vehicleid'] = $input['vehicleid'];
            }
            if (isset($input['status']) && !empty($input['status'])) {
                $insert['status'] = $input['status'];
            }
            if (isset($input['ordernumber']) && !empty($input['ordernumber'])) {
                $insert['ordernumber'] = $input['ordernumber'];
            }
            $insert['created'] = time();
            $insert['modified'] = time();
            $insert['createdby'] = $user['userid'];
            $insert['modifiedby'] = $user['userid'];
            $res = $this->db->insert(self::TABLE, $insert);
            return ($return_id === true) ? $this->db->insert_id() : $res;
        } else {
            foreach ($input as $k => $v) {
                $insert[$k] = array();
                if (isset($v['registerid']) && !empty($v['registerid'])) {
                    $insert[$k]['registerid'] = $v['registerid'];
                }
                if (isset($v['vehicleid']) && !empty($v['vehicleid'])) {
                    $insert[$k]['vehicleid'] = $v['vehicleid'];
                }
                if (isset($v['status']) && !empty($v['status'])) {
                    $insert[$k]['status'] = $v['status'];
                }
                if (isset($v['ordernumber'])) {
                    $insert[$k]['ordernumber'] = $v['ordernumber'];
                }
                $insert[$k]['created'] = time();
                $insert[$k]['modified'] = time();
                $insert[$k]['createdby'] = $user['userid'];
                $insert[$k]['modifiedby'] = $user['userid'];
            }
            return $this->db->insert_batch(self::TABLE, $insert);
        }
    }

    public function update($input = array(), $where = array()) {
        $user = $this->session->userdata(CI_NAME . 'log');
        $update = array();
        if (isset($input['status']) && !empty($input['status'])) {
            $update['status'] = $input['status'];
        }
        if (isset($input['vehicleid']) && !empty($input['vehicleid'])) {
            $update['vehicleid'] = $input['vehicleid'];
        }
        if (isset($input['ordernumber'])) {
            $update['ordernumber'] = $input['ordernumber'];
        }
        $update['modified'] = time();
        $update['modifiedby'] = $user['userid'];
        $this->db->set($update);
        if (isset($where['ids']) && is_array($where['ids'])) {
            $this->db->where_in('id', $where['ids']);
            unset($where['ids']);
        }
        $this->db->where($where);
        return $this->db->update(self::TABLE);
    }

    public function set_finish($input = array(), $where = array()) {
        $this->db->trans_begin();
        $details = $this->get(array('ids' => $where['ids']));
        $error = true;
        $vehicleids = array();
        if (is_array($details)) {
            foreach ($details as $res) {
                $this->db->set(array('testoffroad' => $res['vehicleid']));
                $this->db->where('id', $res['registerid']);
                $error = ($error === false) ? false : $this->db->update('register');
                $vehicleids[] = $res['vehicleid'];
            }
        }
        $this->update($input, $where);
        $this->update_countdown($vehicleids);
        if ($this->db->trans_status() === false || $error === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
            return false;
        }
    }

    public function update_countdown($vehicleids = array()) {
        $user = $this->session->userdata(CI_NAME . 'log');
        if ($vehicleids) {
            $this->db->select();
            $this->db->from(self::TABLE);
            $this->db->where_in('vehicleid', $vehicleids);
            $this->db->where('status', 1);
            $this->db->group_by('vehicleid');
            $this->db->order_by('vehicleid ASC, ordernumber ASC');
            $query = $this->db->get();
            $vehicle = $query->result_array();
            if ($vehicle) {
                foreach ($vehicle as $res2) {
                    $this->db->set(array('modified' => time(), 'modifiedby' => $user['userid']));
                    $this->db->where('id', $res2['id']);
                    $this->db->update(self::TABLE);
                }
            }
        }
    }

    public function delete($input = array()) {
        if (!isset($input['id'])) {
            return false;
        }
        $func = (is_array($input['id'])) ? 'where_in' : 'where';
        $this->db->$func('id', $input['id']);
        return $this->db->delete(self::TABLE);
    }

}
