<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Home extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
        if (!$this->session->userdata('login')) {
            redirect('auth');
        }
        $this->load->model('pembeli_model');
        $this->load->model('produk_model');
        $this->load->model('penjualan_model');
    }

    public function index()
    {
        $data['datauser'] = $this->pembeli_model->countall();
        $data['dataproduk'] = $this->produk_model->countall();
        $data['datapenjualan'] = $this->penjualan_model->countall();
        
        date_default_timezone_set("Asia/Jakarta");
		$year = date('Y');
		$month = date('m');
		
        $data['datapendapatan'] = $this->db->query("SELECT sum(subtotal) as jumlah from penjualan_produk")->result_array();

		$data['minggu'] = $this->db->query("SELECT sum(subtotal) as jumlah from penjualan_produk WHERE DAYOFWEEK(date_pemesanan) = 1 AND YEAR(date_pemesanan)='$year' AND MONTH(date_pemesanan)='$month'")->row_array();
		$data['senin'] = $this->db->query("SELECT sum(subtotal) as jumlah from penjualan_produk WHERE DAYOFWEEK(date_pemesanan) = 2 AND YEAR(date_pemesanan)='$year' AND MONTH(date_pemesanan)='$month'")->row_array();
		$data['selasa'] = $this->db->query("SELECT sum(subtotal) as jumlah from penjualan_produk WHERE DAYOFWEEK(date_pemesanan) = 3 AND YEAR(date_pemesanan)='$year' AND MONTH(date_pemesanan)='$month'")->row_array();
		$data['rabu'] = $this->db->query("SELECT sum(subtotal) as jumlah from penjualan_produk WHERE DAYOFWEEK(date_pemesanan) = 4 AND YEAR(date_pemesanan)='$year' AND MONTH(date_pemesanan)='$month'")->row_array();
		$data['kamis'] = $this->db->query("SELECT sum(subtotal) as jumlah from penjualan_produk WHERE DAYOFWEEK(date_pemesanan) = 5 AND YEAR(date_pemesanan)='$year' AND MONTH(date_pemesanan)='$month'")->row_array();
		$data['jumat'] = $this->db->query("SELECT sum(subtotal) as jumlah from penjualan_produk WHERE DAYOFWEEK(date_pemesanan) = 6 AND YEAR(date_pemesanan)='$year' AND MONTH(date_pemesanan)='$month'")->row_array();
		$data['sabtu'] = $this->db->query("SELECT sum(subtotal) as jumlah from penjualan_produk WHERE DAYOFWEEK(date_pemesanan) = 7 AND YEAR(date_pemesanan)='$year' AND MONTH(date_pemesanan)='$month'")->row_array();


		$data['januari'] = $this->db->query("SELECT sum(subtotal) as jumlah from penjualan_produk WHERE MONTH(date_pemesanan) = 1 AND YEAR(date_pemesanan)='$year'")->row_array();
		$data['februari'] = $this->db->query("SELECT sum(subtotal) as jumlah from penjualan_produk WHERE MONTH(date_pemesanan) = 2 AND YEAR(date_pemesanan)='$year'")->row_array();
		$data['maret'] = $this->db->query("SELECT sum(subtotal) as jumlah from penjualan_produk WHERE MONTH(date_pemesanan) = 3 AND YEAR(date_pemesanan)='$year'")->row_array();
		$data['april'] = $this->db->query("SELECT sum(subtotal) as jumlah from penjualan_produk WHERE MONTH(date_pemesanan) = 4 AND YEAR(date_pemesanan)='$year'")->row_array();
		$data['mei'] = $this->db->query("SELECT sum(subtotal) as jumlah from penjualan_produk WHERE MONTH(date_pemesanan) = 5 AND YEAR(date_pemesanan)='$year'")->row_array();
		$data['juni'] = $this->db->query("SELECT sum(subtotal) as jumlah from penjualan_produk WHERE MONTH(date_pemesanan) = 6 AND YEAR(date_pemesanan)='$year'")->row_array();
		$data['juli'] = $this->db->query("SELECT sum(subtotal) as jumlah from penjualan_produk WHERE MONTH(date_pemesanan) = 7 AND YEAR(date_pemesanan)='$year'")->row_array();
		$data['agustus'] = $this->db->query("SELECT sum(subtotal) as jumlah from penjualan_produk WHERE MONTH(date_pemesanan) = 8 AND YEAR(date_pemesanan)='$year'")->row_array();
		$data['september'] = $this->db->query("SELECT sum(subtotal) as jumlah from penjualan_produk WHERE MONTH(date_pemesanan) = 9 AND YEAR(date_pemesanan)='$year'")->row_array();
		$data['oktober'] = $this->db->query("SELECT sum(subtotal) as jumlah from penjualan_produk WHERE MONTH(date_pemesanan) = 10 AND YEAR(date_pemesanan)='$year'")->row_array();
		$data['november'] = $this->db->query("SELECT sum(subtotal) as jumlah from penjualan_produk WHERE MONTH(date_pemesanan) = 11 AND YEAR(date_pemesanan)='$year'")->row_array();
		$data['desember'] = $this->db->query("SELECT sum(subtotal) as jumlah from penjualan_produk WHERE MONTH(date_pemesanan) = 12 AND YEAR(date_pemesanan)='$year'")->row_array();
        
        $this->load->view('template/header');
        $this->load->view('home', $data);
        $this->load->view('template/js');
        $this->load->view('template/chart', $data);
        $this->load->view('template/footer');
    }
}
