<?php

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

class Statistics extends CI_Controller {

    private $data = array();

    public function __construct() {
        parent::__construct();
        $this->load->model('Statistics_model');
        $this->data['nav'] = 'statistics';
        $this->data['nav_header'] =$this->load->view('statistics/s_header',$this->data,true);
        $this->data['page_title'] = '统计管理';
    }

    //订单总数据统计
    public function index()
    {
        //今年数据
        $startdate=new_get($this->input->post('startdate'),date('Y-m-d',time()-30*24*60*60));
        $enddate=new_get($this->input->post('enddate'),date('Y-m-d'));
        $this->data['startdate']=$startdate;
        $this->data['enddate']=$enddate;
        $startdate=date('Y-m-d 00:00:00',strtotime($startdate));
        $enddate=date('Y-m-d 23:59:59',strtotime($enddate));

        $this->data['total_order_count']=$this->Statistics_model->total_order_count($startdate,$enddate);
        $this->data['total_success_count']=$this->Statistics_model->total_success_count($startdate,$enddate);
        $this->data['total_guest_count']=$this->Statistics_model->total_guest_count($startdate,$enddate);
    	$this->data['guest_success_count']=$this->Statistics_model->guest_success_count($startdate,$enddate);
        $this->data['total_money']=$this->Statistics_model->total_money($startdate,$enddate);
        $this->data['total_success_money']=$this->Statistics_model->total_success_money($startdate,$enddate);
        //去年同期
        $last_year=date('Y',strtotime($startdate))-1;
        $last_year_startdate=$last_year.date('-m-d 00:00:00',strtotime($startdate));
        $last_year_enddate=$last_year.date('-m-d 23:59:59',strtotime($enddate));
        $this->data['last_year_order_count']=$this->Statistics_model->total_order_count($last_year_startdate,$last_year_enddate);
        $this->data['last_year_success_count']=$this->Statistics_model->total_success_count($last_year_startdate,$last_year_enddate);
        $this->data['last_year_guest_count']=$this->Statistics_model->total_guest_count($last_year_startdate,$last_year_enddate);
        $this->data['last_year_success_guest']=$this->Statistics_model->guest_success_count($last_year_startdate,$last_year_enddate);
        $this->data['last_year_money']=$this->Statistics_model->total_money($last_year_startdate,$last_year_enddate);
        $this->data['last_year_success_money']=$this->Statistics_model->total_success_money($last_year_startdate,$last_year_enddate);
        //环比数据
        $last_month_startdate=last_month_today(strtotime($startdate));
        $last_month_enddate=last_month_today(strtotime($enddate));
        $this->data['last_month_order_count']=$this->Statistics_model->total_order_count($last_month_startdate,$last_month_enddate);
        $this->data['last_month_success_count']=$this->Statistics_model->total_success_count($last_month_startdate,$last_month_enddate);
        $this->data['last_month_guest_count']=$this->Statistics_model->total_guest_count($last_month_startdate,$last_month_enddate);
        $this->data['last_month_success_guest']=$this->Statistics_model->guest_success_count($last_month_startdate,$last_month_enddate);
        $this->data['last_month_money']=$this->Statistics_model->total_money($last_month_startdate,$last_month_enddate);
        $this->data['last_month_success_money']=$this->Statistics_model->total_success_money($last_month_startdate,$last_month_enddate);

        $this->load->view('header', $this->data);
        $this->load->view('statistics/index');
        $this->load->view('footer');
    }

    //产品数据统计
    public function product($source='product')
    {
        $filed=$source=='product'?'occ_circuitcode':'o_source';
        $startdate=new_get($this->input->post('startdate'),date('Y-m-d',time()-30*24*60*60));
        $enddate=new_get($this->input->post('enddate'),date('Y-m-d'));
        $this->data['startdate']=$startdate;
        $this->data['enddate']=$enddate;
        $this->data['source']=$source;

        $startdate=date('Y-m-d 00:00:00',strtotime($startdate));
        $enddate=date('Y-m-d 23:59:59',strtotime($enddate));

        //今年数据
        $this->data['total_order_count']=$this->Statistics_model->product_order_count($startdate,$enddate,$filed);
        $this->data['total_success_count']=$this->Statistics_model->product_order_count($startdate,$enddate,$filed,true);
        $this->data['total_guest_count']=$this->Statistics_model->product_order_guests($startdate,$enddate,$filed);
        $this->data['guest_success_count']=$this->Statistics_model->product_order_guests($startdate,$enddate,$filed,true);
        $this->data['total_money']=$this->Statistics_model->product_order_money($startdate,$enddate,$filed);
        $this->data['total_success_money']=$this->Statistics_model->product_order_money($startdate,$enddate,$filed,true);
        //去年同期
        $last_year=date('Y',strtotime($startdate))-1;
        $last_year_startdate=$last_year.date('-m-d 00:00:00',strtotime($startdate));
        $last_year_enddate=$last_year.date('-m-d 23:59:59',strtotime($enddate));
        $this->data['last_year_order_count']=$this->Statistics_model->product_order_count($last_year_startdate,$last_year_enddate,$filed);
        $this->data['last_year_success_count']=$this->Statistics_model->product_order_count($last_year_startdate,$last_year_enddate,$filed,true);
        $this->data['last_year_guest_count']=$this->Statistics_model->product_order_guests($last_year_startdate,$last_year_enddate,$filed);
        $this->data['last_year_success_guest']=$this->Statistics_model->product_order_guests($last_year_startdate,$last_year_enddate,$filed,true);
        $this->data['last_year_money']=$this->Statistics_model->product_order_money($last_year_startdate,$last_year_enddate,$filed);
        $this->data['last_year_success_money']=$this->Statistics_model->product_order_money($last_year_startdate,$last_year_enddate,$filed,true);
        //环比数据
        $last_month_startdate=last_month_today(strtotime($startdate));
        $last_month_enddate=last_month_today(strtotime($enddate));
        $this->data['last_month_order_count']=$this->Statistics_model->product_order_count($last_month_startdate,$last_month_enddate,$filed);
        $this->data['last_month_success_count']=$this->Statistics_model->product_order_count($last_month_startdate,$last_month_enddate,$filed,true);
        $this->data['last_month_guest_count']=$this->Statistics_model->product_order_guests($last_month_startdate,$last_month_enddate,$filed);
        $this->data['last_month_success_guest']=$this->Statistics_model->product_order_guests($last_month_startdate,$last_month_enddate,$filed,true);
        $this->data['last_month_money']=$this->Statistics_model->product_order_money($last_month_startdate,$last_month_enddate,$filed);
        $this->data['last_month_success_money']=$this->Statistics_model->product_order_money($last_month_startdate,$last_month_enddate,$filed,true);

        $this->load->view('header', $this->data);
        $this->load->view('statistics/product');
        $this->load->view('footer');
    }

    public function paytype()
    {
        $startdate=new_get($this->input->post('startdate'),date('Y-m-d',time()-30*24*60*60));
        $enddate=new_get($this->input->post('enddate'),date('Y-m-d'));
        $this->data['startdate']=$startdate;
        $this->data['enddate']=$enddate;

        $startdate=date('Y-m-d 00:00:00',strtotime($startdate));
        $enddate=date('Y-m-d 23:59:59',strtotime($enddate));

        $this->data['total_order_count']=$this->Statistics_model->total_order_count($startdate,$enddate);
        $this->data['total_success_count']=$this->Statistics_model->total_success_count($startdate,$enddate);
        $this->data['payment_types']=$this->Statistics_model->payment_type($startdate,$enddate);

        $this->load->view('header', $this->data);
        $this->load->view('statistics/paytype');
        $this->load->view('footer');
    }

}