<?php
class ControllerReportSaleOrder extends Controller {
	public function index() {
		$this->language->load('report/sale_order');

		$this->load->type('tipocomum');
    $this->load->helper('ftr_geral');

		$this->data['ftr_geral'] = new ftr_geral();

		$this->data['Type_OrigemSistema'] = $this->load->typeForTwig('Type_OrigemSistema');

    $this->document->addScript(array(
      'view/javascript/report-sale-order.js'
    ));

		$this->document->setTitle($this->language->get('heading_title'));

		if (isset($this->request->get['filter_date_start'])) {
			$filter_date_start = $this->request->get['filter_date_start'];
		} else {
			$filter_date_start = '';
		}

		if (isset($this->request->get['filter_date_end'])) {
			$filter_date_end = $this->request->get['filter_date_end'];
		} else {
			$filter_date_end = '';
		}

		if (isset($this->request->get['filter_group'])) {
			$filter_group = $this->request->get['filter_group'];
		} else {
			$filter_group = 'week';
		}

		if (isset($this->request->get['filter_order_status_id'])) {
			$filter_order_status_id = $this->request->get['filter_order_status_id'];
		} else {
			$filter_order_status_id = 0;
		}

		if (isset($this->request->get['filter_order_payment_method'])) {
			$filter_order_payment_method = $this->request->get['filter_order_payment_method'];
		} else {
			$filter_order_payment_method = "";
		}

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		$url = '';

		if (isset($this->request->get['filter_date_start'])) {
			$url .= '&filter_date_start=' . $this->request->get['filter_date_start'];
		}

		if (isset($this->request->get['filter_date_end'])) {
			$url .= '&filter_date_end=' . $this->request->get['filter_date_end'];
		}

		if (isset($this->request->get['filter_group'])) {
			$url .= '&filter_group=' . $this->request->get['filter_group'];
		}

		if (isset($this->request->get['filter_order_status_id'])) {
			$url .= '&filter_order_status_id=' . $this->request->get['filter_order_status_id'];
		}

		if (isset($this->request->get['filter_order_payment_method'])) {
			$url .= '&filter_order_payment_method=' . $this->request->get['filter_order_payment_method'];
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

   		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('report/sale_order', 'token=' . $this->session->data['token'] . $url, 'SSL'),
      		'separator' => ' :: '
   		);

		$this->load->model('report/sale');

		$this->data['orders'] = array();
		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['text_no_results'] = $this->language->get('text_no_results');
    $this->data['text_all_status'] = $this->language->get('text_all_status');
		$this->data['text_no_paymentMethod'] = $this->language->get('text_no_paymentMethod');
    $this->data['text_origem_pedido'] = $this->language->get('text_origem_pedido');
		$this->data['text_all'] = $this->language->get('text_all');
		$this->data['column_date_start'] = $this->language->get('column_date_start');
		$this->data['column_date_end'] = $this->language->get('column_date_end');
    $this->data['column_orders'] = $this->language->get('column_orders');
		$this->data['column_products'] = $this->language->get('column_products');
		$this->data['column_discount'] = $this->language->get('column_discount');
    $this->data['column_order_shipping'] = $this->language->get('column_order_shipping');
    $this->data['column_tax'] = $this->language->get('column_tax');
    $this->data['column_price_products'] = $this->language->get('column_price_products');
    $this->data['column_price_shippings'] = $this->language->get('column_price_shippings');
		$this->data['column_total'] = $this->language->get('column_total');
		$this->data['entry_date_start'] = $this->language->get('entry_date_start');
		$this->data['entry_date_end'] = $this->language->get('entry_date_end');
		$this->data['entry_group'] = $this->language->get('entry_group');
		$this->data['entry_status'] = $this->language->get('entry_status');
    $this->data['entry_payment_method'] = $this->language->get('entry_payment_method');
    $this->data['button_filter'] = $this->language->get('button_filter');
    $this->data['text_total'] = $this->language->get('text_total');
    $this->data['token'] = $this->session->data['token'];



		$this->load->model('localisation/order_status');
		$this->data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();
                $this->data['payment_methods'] = $this->paymentMethod();

		$this->data['groups'] = array();
		$this->data['groups'][] = array(
			'text'  => $this->language->get('text_year'),
			'value' => 'year',
		);

		$this->data['groups'][] = array(
			'text'  => $this->language->get('text_month'),
			'value' => 'month',
		);

		$this->data['groups'][] = array(
			'text'  => $this->language->get('text_week'),
			'value' => 'week',
		);

		$this->data['groups'][] = array(
			'text'  => $this->language->get('text_day'),
			'value' => 'day',
		);

		$url = '';

		if (isset($this->request->get['filter_date_start'])) {
			$url .= '&filter_date_start=' . $this->request->get['filter_date_start'];
		}

		if (isset($this->request->get['filter_date_end'])) {
			$url .= '&filter_date_end=' . $this->request->get['filter_date_end'];
		}

		if (isset($this->request->get['filter_group'])) {
			$url .= '&filter_group=' . $this->request->get['filter_group'];
		}

		if (isset($this->request->get['filter_order_status_id'])) {
			$url .= '&filter_order_status_id=' . $this->request->get['filter_order_status_id'];
		}

		$this->data['pagination'] = '';

		$this->data['filter_date_start'] = $filter_date_start;
		$this->data['filter_date_end'] = $filter_date_end;
		$this->data['filter_group'] = $filter_group;
		$this->data['filter_order_status_id'] = $filter_order_status_id;
    $this->data['filter_order_payment_method'] = $filter_order_payment_method;

		$this->children = array(
			'common/header',
			'common/footer'
		);

		$this->response->setOutput($this->render('report/sale_order'));
	}


	public function filter() {
		$this->language->load('report/sale_order');

		$this->load->type('tipocomum');
    $this->load->helper('ftr_geral');

		$this->data['ftr_geral'] = new ftr_geral();

		$this->data['Type_OrigemSistema'] = $this->load->typeForTwig('Type_OrigemSistema');

    $this->document->addScript(array(
      'view/javascript/report-sale-order.js'
    ));

		$this->document->setTitle($this->language->get('heading_title'));

		if (isset($this->request->get['filter_date_start'])) {
			$filter_date_start = $this->request->get['filter_date_start'];
		} else {
			$filter_date_start = '';
		}

		if (isset($this->request->get['filter_date_end'])) {
			$filter_date_end = $this->request->get['filter_date_end'];
		} else {
			$filter_date_end = '';
		}

		if (isset($this->request->get['filter_group'])) {
			$filter_group = $this->request->get['filter_group'];
		} else {
			$filter_group = 'week';
		}

		if (isset($this->request->get['filter_order_status_id'])) {
			$filter_order_status_id = $this->request->get['filter_order_status_id'];
		} else {
			$filter_order_status_id = 0;
		}

		if (isset($this->request->get['filter_order_payment_method'])) {
			$filter_order_payment_method = $this->request->get['filter_order_payment_method'];
		} else {
			$filter_order_payment_method = "";
		}

		if (isset($this->request->get['filter_origem_pedido'])) {
      $filter_origem_pedido = $this->request->get['filter_origem_pedido'];
    } else {
      $filter_origem_pedido = null;
    }

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		$url = '';

		if (isset($this->request->get['filter_date_start'])) {
			$url .= '&filter_date_start=' . $this->request->get['filter_date_start'];
		}

		if (isset($this->request->get['filter_date_end'])) {
			$url .= '&filter_date_end=' . $this->request->get['filter_date_end'];
		}

		if (isset($this->request->get['filter_group'])) {
			$url .= '&filter_group=' . $this->request->get['filter_group'];
		}

		if (isset($this->request->get['filter_order_status_id'])) {
			$url .= '&filter_order_status_id=' . $this->request->get['filter_order_status_id'];
		}

		if (isset($this->request->get['filter_order_payment_method'])) {
			$url .= '&filter_order_payment_method=' . $this->request->get['filter_order_payment_method'];
		}

		if (isset($this->request->get['filter_origem_pedido']) && $this->request->get['filter_origem_pedido']<>3 ) {
      $url .= '&filter_origem_pedido=' . $this->request->get['filter_origem_pedido'];
    }

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

   		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('report/sale_order', 'token=' . $this->session->data['token'] . $url, 'SSL'),
      		'separator' => ' :: '
   		);

		$this->load->model('report/sale');

		$this->data['orders'] = array();

		$data = array(
			'filter_date_start'	 => implode('-', array_reverse(explode('/', $filter_date_start))),
			'filter_date_end'	 => implode('-', array_reverse(explode('/', $filter_date_end))),
			'filter_group'           => $filter_group,
			'filter_order_status_id' => $filter_order_status_id,
			'filter_order_payment_method' => $filter_order_payment_method,
            'filter_origem_pedido' => $filter_origem_pedido,
			'start'                  => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit'                  => $this->config->get('config_admin_limit')
		);

        $error = $this->validate();
        if (!$error){
            $order_total = $this->model_report_sale->getTotalOrders($data);
            $results = $this->model_report_sale->getOrders($data);
            $orders= array();
            foreach ($results as $result) {

                $orders[] = array(
                    'date_start' => date($this->language->get('date_format_short'), strtotime($result['date_start'])),
                    'date_end'   => date($this->language->get('date_format_short'), strtotime($result['date_end'])),
                    'orders'     => $result['orders'],
                    'products'   => $result['products'],
                    'tax'               => $this->currency->format($result['tax'], $this->config->get('config_currency')),
                    'preco'             => /*sem mascara*/         $result['prices_products'],
                    'prices_products'   => $this->currency->format($result['prices_products'], $this->config->get('config_currency')),
                    'frete'             => /*sem mascara*/         $result['price_shippings'],
                    'price_shippings'   => $this->currency->format($result['price_shippings'], $this->config->get('config_currency')),
                    'totalSemFrete'     => /*sem mascara*/         $result['order_no_shipping'],
                    'order_no_shipping' => $this->currency->format($result['order_no_shipping'], $this->config->get('config_currency')),
                    'desconto'          => /*sem mascara*/         $result['discount'],
                    'discount'          => $this->currency->format($result['discount'], $this->config->get('config_currency')),
                    'total_no_mask'     => /*sem mascara*/         $result['total'] ,
                    'total'             => $this->currency->format($result['total'], $this->config->get('config_currency'))
                );
            }


            $this->data['orders'] = $orders;

            $this->data['totals'] = array(
            'vTotalOrders' => array_sum(array_column($orders, 'orders')),
            'vTotalProducts' => array_sum(array_column($orders, 'products')),
            'vTotalProdutoPreco' => $this->currency->format(array_sum(array_column($orders, 'preco')), $this->config->get('config_currency')),
            'vTotalFrete' => $this->currency->format(array_sum(array_column($orders, 'frete')), $this->config->get('config_currency')),
            'vTotalSemFrete' => $this->currency->format(array_sum(array_column($orders, 'totalSemFrete')), $this->config->get('config_currency')),
            'vTotalDesconto' => $this->currency->format(array_sum(array_column($orders, 'desconto')), $this->config->get('config_currency')),
            'vTotalTotal' => $this->currency->format(array_sum(array_column($orders, 'total_no_mask')), $this->config->get('config_currency'))
            );
        }else{

            foreach($error as $key => $err){
                $this->data['error'][$key] = $err;
            }

        }
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_no_results'] = $this->language->get('text_no_results');
		$this->data['text_all_status'] = $this->language->get('text_all_status');
		$this->data['text_origem_pedido'] = $this->language->get('text_origem_pedido');
    $this->data['text_no_paymentMethod'] = $this->language->get('text_no_paymentMethod');
		$this->data['text_all'] = $this->language->get('text_all');

		$this->data['column_date_start'] = $this->language->get('column_date_start');
		$this->data['column_date_end'] = $this->language->get('column_date_end');
    $this->data['column_orders'] = $this->language->get('column_orders');
		$this->data['column_products'] = $this->language->get('column_products');
		$this->data['column_discount'] = $this->language->get('column_discount');
		$this->data['column_tax'] = $this->language->get('column_tax');
		$this->data['column_price_products'] = $this->language->get('column_price_products');
		$this->data['column_price_shippings'] = $this->language->get('column_price_shippings');
		$this->data['column_order_shipping'] = $this->language->get('column_order_shipping');
		$this->data['column_total'] = $this->language->get('column_total');

		$this->data['entry_date_start'] = $this->language->get('entry_date_start');
		$this->data['entry_date_end'] = $this->language->get('entry_date_end');
		$this->data['entry_group'] = $this->language->get('entry_group');
		$this->data['entry_status'] = $this->language->get('entry_status');
                $this->data['entry_payment_method'] = $this->language->get('entry_payment_method');

    $this->data['button_filter'] = $this->language->get('button_filter');
    $this->data['text_total'] = $this->language->get('text_total');

		$this->data['token'] = $this->session->data['token'];

		$this->load->model('localisation/order_status');

		$this->data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();
                $this->data['payment_methods'] = $this->paymentMethod();

		$this->data['groups'] = array();

		$this->data['groups'][] = array(
			'text'  => $this->language->get('text_year'),
			'value' => 'year',
		);

		$this->data['groups'][] = array(
			'text'  => $this->language->get('text_month'),
			'value' => 'month',
		);

		$this->data['groups'][] = array(
			'text'  => $this->language->get('text_week'),
			'value' => 'week',
		);

		$this->data['groups'][] = array(
			'text'  => $this->language->get('text_day'),
			'value' => 'day',
		);

		$url = '';

		if (isset($this->request->get['filter_date_start'])) {
			$url .= '&filter_date_start=' . $this->request->get['filter_date_start'];
		}

		if (isset($this->request->get['filter_date_end'])) {
			$url .= '&filter_date_end=' . $this->request->get['filter_date_end'];
		}

		if (isset($this->request->get['filter_group'])) {
			$url .= '&filter_group=' . $this->request->get['filter_group'];
		}

		if (isset($this->request->get['filter_order_status_id'])) {
			$url .= '&filter_order_status_id=' . $this->request->get['filter_order_status_id'];
		}

        if (isset($order_total)){
            $pagination = new Pagination();
            $pagination->total = $order_total;
            $pagination->page = $page;
            $pagination->limit = $this->config->get('config_admin_limit');
            $pagination->text = $this->language->get('text_pagination');
            $pagination->url = $this->url->link('report/sale_order/filter', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');

            $this->data['pagination'] = $pagination->render();
        }

		$this->data['filter_date_start'] = $filter_date_start;
		$this->data['filter_date_end'] = $filter_date_end;
		$this->data['filter_group'] = $filter_group;
		$this->data['filter_order_status_id'] = $filter_order_status_id;
		$this->data['filter_origem_pedido'] = $filter_origem_pedido;
    $this->data['filter_order_payment_method'] = $filter_order_payment_method;

		$this->children = array(
			'common/header',
			'common/footer'
		);

		$this->response->setOutput($this->render('report/sale_order'));
	}

        public function paymentMethod(){
            $this->load->model('setting/extension');
            $payment_method = array();
            $results = $this->model_setting_extension->getExtensions('payment');
            foreach ($results as $result) {
                $this->language->load('payment/'.$result['code']);
                $payment_method['code'][]  = $result['code'];
                $payment_method['name'][$result['code']] = $this->language->get('heading_title');
            }
            return $payment_method;
        }

        public function validate(){
            $error = array();

            if (isset($this->request->get['filter_date_start'])&& isset($this->request->get['filter_date_end'])){
                $dateIni = new DateTime(ftr_geral::formatDate($this->request->get['filter_date_start'],'us'));
                $dateFim = new DateTime(ftr_geral::formatDate($this->request->get['filter_date_end'],'us'));
                $dias = $dateIni->diff($dateFim);
                if ($dias->days > 180){
                    $error['warning'] = $this->language->get('error_periodo');
                }
            }

            if (!isset($this->request->get['filter_date_start'])){
                $error['date_ini'] = $this->language->get('error_periodo');
                $error['warning'] = $this->language->get('error_periodo');
            }

            if (!isset($this->request->get['filter_date_end'])){
                $error['date_end'] = $this->language->get('error_periodo');
                $error['warning'] = $this->language->get('error_periodo');
            }


            return $error;

        }
}
