<?php

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

class Reportpackage extends MX_Controller {

    const TOKENPW = 'violet';

    public function __construct() {
        parent::__construct();
        $this->load->helper('cookie');
        $this->load->helper('language');
        $this->lang->load('messages', 'message');
    }

    public function index() {
        $data = array();
        $admin_info = $this->session->userdata('adminInfo');
        if ($admin_info) {
            $data['content'] = $this->getProvince();
            $this->load->view('reportpackage/index', $data);
        } else {
            $this->load->view('login');
        }
    }

    public function getProvince($filters = array()) {

        $this->load->model('reportmodel');
        $data['month'] = date("m");
        $data['year'] = date("Y");
        if ($this->input->post('month')) {
            $data['month'] = $this->input->post('month');
            $data['year'] = $this->input->post('year');
        }
        $data = $this->getdays($data);

        $result = $this->reportmodel->getPackages($data);
        $data['provinces'] = $result['provinces'];
        $data['packages'] = $result['packages'];

        if ($this->input->is_ajax_request()) {
            return $this->load->view('reportpackage/province', $data);
        }
        return $this->load->view('reportpackage/province', $data, true);
    }

    public function getdays($data) {
        $data['month_days'] = 1;
        if ("" . $data['year'] . "-" . $data['month'] . "" == date("Y-m")) {
            $data['month_days'] = date("d");
        } else {
            $data['month_days'] = cal_days_in_month(CAL_GREGORIAN, $data['month'], $data['year']); // 31  
        }
        $data['year_days'] = 1;
        if ($data['year'] == date("Y")) {
            $data['year_days'] = date('z') + 1;
        } else {
            $data['year_days'] = date("z", mktime(0, 0, 0, 12, 31, $data['year'])) + 1;
        }
        return $data;
    }

    public function export() {
        $this->load->model('reportmodel');
        $data['month'] = $this->uri->segment(4);
        $data['year'] = $this->uri->segment(5);
        $data = $this->getdays($data);
        $result = $this->reportmodel->getPackages($data);
        $data['provinces'] = $result['provinces'];
        $data['packages'] = $result['packages'];

        $this->load->library("PHPExcel");
        $sheet = $this->phpexcel->getActiveSheet();
        $sheet->setTitle("bao cao thue bao " . $data['month'] . " - " . $data['year']);
        $sheet->getColumnDimension('A')->setWidth(10);
        $sheet->getColumnDimension('B')->setWidth(25);
        $sheet->getColumnDimension('C')->setWidth(15);
        $sheet->getColumnDimension('D')->setWidth(15);
        $sheet->getColumnDimension('E')->setWidth(15);
        $sheet->getColumnDimension('F')->setWidth(15);
        $sheet->getColumnDimension('G')->setWidth(15);
        $sheet->getColumnDimension('H')->setWidth(15);
        $sheet->setCellValue('A1', "Báo cáo doanh thu theo tỉnh");
        $sheet->setCellValue('A2', "Thời gian: " . $data['month'] . " - " . $data['year']);
        $sheet->setCellValue('A4', "STT");
        $sheet->setCellValue('B4', "Tỉnh/Thành phố");
        $chars = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L");
        $dem = 1;
        foreach ($data['packages'] as $package) {
            $dem++;
            $sheet->setCellValue($chars[$dem] . ($index + 4), $package['p_name']);
            $dem++;
            $sheet->setCellValue($chars[$dem] . ($index + 4), "Tỉ lệ %");
        }
        $dem++;
        $sheet->setCellValue($chars[$dem] . ($index + 4), "Tổng TB");


        foreach ($data['provinces'] as $index => $provinces) {

            $sheet->setCellValue('A' . ($index + 5), $provinces['stt']);
            $sheet->setCellValue('B' . ($index + 5), $provinces['province']);
            $dem = 1;
            foreach ($provinces['packages'] as $package) {
                $dem++;
                $sheet->setCellValue($chars[$dem] . ($index + 5), $package['total']);
                $dem++;
                $sheet->setCellValue($chars[$dem] . ($index + 5), $package['percent']);
            }
            $dem++;
            $sheet->setCellValue($chars[$dem] . ($index + 5), $provinces['total']);
        }
        $writer = new PHPExcel_Writer_Excel5($this->phpexcel);
        header('Content-Disposition: attachment; filename="doanh_thu_' . $data['month'] . "_" . $data['year'] . '.xls"');
        $writer->save('php://output');
    }

}
