<?php

class Rpt_carbooking extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('mpdf/mpdf');
        $this->load->library('mydate');

        $this->load->model('script_m');
        $this->load->model('report_m');
        $this->load->model('carquery_m');

        $s_imeeting['activemenu'] = '009';
        $this->session->set_userdata('s_imeeting', $s_imeeting);
    }

    function index() {
        $data['extraHeadContent'] = $this->script_m->calendar();
        $data['extraHeadContent'].= $this->script_m->textformat();

        $first_date = strftime('%Y') . '-' . strftime('%m') . '-01';
        $data['start_date'] = $this->mydate->dateToText($first_date);
        $data['end_date'] = $this->mydate->lastDate();

        $data['page_title'] = "รายงานการใช้รถ";
        $data['content'] = 'report/carbooking_rpt_v';
        $this->load->view('template_v', $data);
    }

    function preview() {
        $start_date = $this->input->post('start_date');
        $end_date = $this->input->post('end_date');
        $rptstyle = $this->input->post('rptstyle');

        $array = $this->carquery_m->get_all($this->mydate->dateToMySql($start_date), $this->mydate->dateToMySql($end_date), '1', '', '', $offset, 999999);

        $tbl = '<div class="caption"><b>รายงานการใช้รถ</b></div>';
        $tbl.= '<div class="caption_rpt"><b>ประจำวันที่ </b>' . $start_date . " <b>ถึง</b> " . $end_date . "</div>";
        $tbl.="<table>";
        $tbl.="<thead>";
        $tbl.="<tr>";
        $tbl.="<th width='30'>ลำดับ</th>";
        $tbl.="<th width='80'>วันที่</th>";
        $tbl.="<th width='90'>เวลา</th>";
        $tbl.="<th>ใช้รถเพื่อ</th>";
        $tbl.="<th width='120'>ทะเบียน</th>";
        $tbl.="<th width='120'>แผนก</th>";
        $tbl.="</tr>";
        $tbl.="</thead>";

        $i = 0;
        foreach ($array['query'] as $r) {
            $i++;
            $tbl.="<tr>";
            $tbl.="<td style='text-align:center'>$i</td>";
            $tbl.="<td style='text-align:center'>" . $this->mydate->dateToText($r['rdate']) . "</td>";
            $tbl.="<td style='text-align:center'>" . $r['start_time'] . " - " . $r['end_time'] . "</td>";
            $tbl.="<td>" . $r['name'] . "</td>";
            $tbl.="<td class='center'>" . $r['car_code'] . "</td>";
            $tbl.="<td>" . $r['depart_name'] . "</td>";
            $tbl.="</tr>";
        }

        if ($i == 0) {
            $tbl.="<tr><td colspan='6' style='text-align:center'>*** ไม่มีข้อมูลการจอง ***</td></tr>";
        } else {
            $tbl.="<tr><td colspan='6' style='text-align:center'>รวมใช้รถ <b>$i</b> ครั้ง</td></tr>";
        }
        $tbl.="</table>";

        if ($rptstyle == '0') {
            $this->mpdf = new mPDF('th');
            $this->mpdf->WriteHTML($this->report_m->get_css_pdf(), 1);
            $this->mpdf->WriteHTML($tbl);
            $this->mpdf->Output();
        } else {
            $data['tbl'] = $tbl;
            $this->load->view('report/excel_template', $data);
        }
    }

    function rpt_bydepart() {
        $data['extraHeadContent'] = $this->script_m->calendar();
        $data['extraHeadContent'].= $this->script_m->textformat();

        $first_date = strftime('%Y') . '-' . strftime('%m') . '-01';
        $data['start_date'] = $this->mydate->dateToText($first_date);
        $data['end_date'] = $this->mydate->lastDate();

        $this->db->order_by('name', 'asc');
        $data['depart'] = $this->db->get('tbdepart')->result_array();

        $data['page_title'] = "รายงานการใช้รถ-แยกตามแผนก";
        $data['content'] = 'report/carbooking_depart_rpt_v';
        $this->load->view('template_v', $data);
    }

    function depart_preview() {
        $start_date = $this->input->post('start_date');
        $end_date = $this->input->post('end_date');
        $rptstyle = $this->input->post('rptstyle');
        $depart_name = $this->input->post('depart_name');

        $array = $this->carquery_m->get_all($this->mydate->dateToMySql($start_date), $this->mydate->dateToMySql($end_date), '1', 'tbdepart.name', $depart_name, $offset, 999999);

        $tbl = '<div class="caption"><b>รายงานการใช้รถ-แยกตามแผนก</b></div>';
        $tbl.= '<div class="caption_rpt"><b>ประจำวันที่ </b>' . $start_date . " <b>ถึง</b> " . $end_date . "</div>";
        $tbl.= '<div><b>แผนก </b>' . $depart_name;
        $tbl.="<table>";
        $tbl.="<thead>";
        $tbl.="<tr>";
        $tbl.="<th width='30'>ลำดับ</th>";
        $tbl.="<th width='80'>วันที่</th>";
        $tbl.="<th width='90'>เวลา</th>";
        $tbl.="<th>ใช้รถเพื่อ</th>";
        $tbl.="<th width='120'>ทะเบียน</th>";
        $tbl.="<th width='120'>แผนก</th>";
        $tbl.="</tr>";
        $tbl.="</thead>";

        $i = 0;
        foreach ($array['query'] as $r) {
            $i++;
            $tbl.="<tr>";
            $tbl.="<td style='text-align:center'>$i</td>";
            $tbl.="<td style='text-align:center'>" . $this->mydate->dateToText($r['rdate']) . "</td>";
            $tbl.="<td style='text-align:center'>" . $r['start_time'] . " - " . $r['end_time'] . "</td>";
            $tbl.="<td>" . $r['name'] . "</td>";
            $tbl.="<td class='center'>" . $r['car_code'] . "</td>";
            $tbl.="<td>" . $r['depart_name'] . "</td>";
            $tbl.="</tr>";
        }

        if ($i == 0) {
            $tbl.="<tr><td colspan='6' style='text-align:center'>*** ไม่มีข้อมูลการจอง ***</td></tr>";
        } else {
            $tbl.="<tr><td colspan='6' style='text-align:center'>รวมใช้รถ <b>$i</b> ครั้ง</td></tr>";
        }
        $tbl.="</table>";

        if ($rptstyle == '0') {
            $this->mpdf = new mPDF('th');
            $this->mpdf->WriteHTML($this->report_m->get_css_pdf(), 1);
            $this->mpdf->WriteHTML($tbl);
            $this->mpdf->Output();
        } else {
            $data['tbl'] = $tbl;
            $this->load->view('report/excel_template', $data);
        }
    }
    
    
    function rpt_byteam() {
        $data['extraHeadContent'] = $this->script_m->calendar();
        $data['extraHeadContent'].= $this->script_m->textformat();

        $first_date = strftime('%Y') . '-' . strftime('%m') . '-01';
        $data['start_date'] = $this->mydate->dateToText($first_date);
        $data['end_date'] = $this->mydate->lastDate();

        $this->db->order_by('name', 'asc');
        $data['team'] = $this->db->get('tbteam')->result_array();

        $data['page_title'] = "รายงานการใช้รถ-แยกตามหน่วยงาน";
        $data['content'] = 'report/carbooking_team_rpt_v';
        $this->load->view('template_v', $data);
    }
    
    function team_preview() {
        $start_date = $this->input->post('start_date');
        $end_date = $this->input->post('end_date');
        $rptstyle = $this->input->post('rptstyle');
        $team_name = $this->input->post('team_name');

        $array = $this->carquery_m->get_allbyteam($this->mydate->dateToMySql($start_date), $this->mydate->dateToMySql($end_date),$team_name);

        $tbl = '<div class="caption"><b>รายงานการใช้รถ-แยกตามหน่วยงาน</b></div>';
        $tbl.= '<div class="caption_rpt"><b>ประจำวันที่ </b>' . $start_date . " <b>ถึง</b> " . $end_date . "</div>";
        $tbl.= '<div><b>หน่วยงาน </b>' . $team_name;
        $tbl.="<table>";
        $tbl.="<thead>";
        $tbl.="<tr>";
        $tbl.="<th width='30'>ลำดับ</th>";
        $tbl.="<th width='80'>วันที่</th>";
        $tbl.="<th width='90'>เวลา</th>";
        $tbl.="<th>ใช้รถเพื่อ</th>";
        $tbl.="<th width='120'>ทะเบียน</th>";
        $tbl.="</tr>";
        $tbl.="</thead>";

        $i = 0;
        foreach ($array['query'] as $r) {
            $i++;
            $tbl.="<tr>";
            $tbl.="<td style='text-align:center'>$i</td>";
            $tbl.="<td style='text-align:center'>" . $this->mydate->dateToText($r['rdate']) . "</td>";
            $tbl.="<td style='text-align:center'>" . $r['start_time'] . " - " . $r['end_time'] . "</td>";
            $tbl.="<td>" . $r['name'] . "</td>";
            $tbl.="<td>" . $r['car_code'] . "</td>";
            $tbl.="</tr>";
        }

        if ($i == 0) {
            $tbl.="<tr><td colspan='5' style='text-align:center'>*** ไม่มีข้อมูลการจอง ***</td></tr>";
        } else {
            $tbl.="<tr><td colspan='5' style='text-align:center'>รวมใช้รถ <b>$i</b> ครั้ง</td></tr>";
        }
        $tbl.="</table>";

        if ($rptstyle == '0') {
            $this->mpdf = new mPDF('th');
            $this->mpdf->WriteHTML($this->report_m->get_css_pdf(), 1);
            $this->mpdf->WriteHTML($tbl);
            $this->mpdf->Output();
        } else {
            $data['tbl'] = $tbl;
            $this->load->view('report/excel_template', $data);
        }
    }

    

    
}

?>
