<?php
class ControllerSaleToday extends Controller {
	private $error = array();
	private $category_id = 0;
	private $path = array();

	public function index() {
		$this->language->load('sale/today');

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

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

		$this->getList();
	}

    public function getForm() {
        $this->language->load('sale/today');
        $this->load->model('sale/today');
        if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'firstname';
		}

        if (isset($this->request->get['product_id'])) {
			$product_id = $this->request->get['product_id'];
            $data['product_id']=$product_id;
		}
        if (isset($this->request->get['category_id'])) {
			$category_id = $this->request->get['category_id'];
		}
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
        if(isset($this->request->post['status'])){
            $product_status_id= $this->request->post['status'];
        }else{
            $product_status_id=0;
        }
        if(isset($this->request->post['status'])){
            $product_status_id= $this->request->post['status'];
        }elseif(isset($this->request->get['status'])){
            $product_status_id=$this->request->get['status'];
        }else{
            $product_status_id=0;
        }
        if(isset($this->request->post['report_id'])){
            $report_id= $this->request->post['report_id'];
        }elseif(isset($this->request->get['report_id'])){
            $report_id=$this->request->get['report_id'];
        }else{
            $report_id=0;
        }
        if (isset($this->request->get['category_name'])) {
			$category_name = html_entity_decode($this->request->get['category_name']);
		} else {
			$category_name = '';
		}
        if (isset($this->request->post['date'])) {
            $booking_date = $this->request->post['date'];
            $data['booking_date']=$booking_date;
            $booking_date2 =  date('Y-m-d',strtotime("$booking_date +1 day"));
        } else{
        if (isset($this->request->get['date'])) {
			$booking_date = $this->request->get['date'];
            $data['booking_date']=$booking_date;
            $booking_date2 =  date('Y-m-d',strtotime("$booking_date +1 day"));
		} else {
			$booking_date = date('Y-m-d');
		}
        }
        if (isset($this->request->post['user'])) {
			$data['username']=$this->request->post['user'];
            $username = $this->request->post['user'];

		} else {
			$username= '';
            $data['username']='';
		}

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

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

		$url = '';


        $url.='&data='.$booking_date;
        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }
        if (isset($this->request->get['day'])) {
			$url .= '&day=' . $this->request->get['day'];
		}
         if (isset($this->request->get['date'])) {
			$url .= '&date=' . $this->request->get['date'];
		}
        if (isset($this->request->get['product_id'])) {
			$url .= $this->request->get['product_id'];
		}

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

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

		$data['today'] = array();
        $data['breadcrumbs'][] = array(
			'text' => $this->language->get('heading_title'),
			'href' => $this->url->link('sale/today', 'token=' . $this->session->data['token'] . $url, 'SSL')
		);
        $data['breadcrumbs'][] = array(
			'text' => $category_name,
			'href' => $this->url->link('sale/today/getForm', 'token=' . $this->session->data['token'].'&category_name='.$category_name.'&category_id='.$category_id.$url, 'SSL')
		);
		$filter_data = array(
			'sort'  => $sort,
			'order' => $order,
			'report_id' => $report_id
			//'start' => ($page - 1) * $this->config->get('config_limit_admin'),
			//'limit' => $this->config->get('config_limit_admin')
		);
		$data['sort']=$sort;
		$data['order'] = $order;
        $this->load->model('user/user');
        $user_admin_id=$this->user->isLogged();
        $getusername=$this->model_user_user->getUser($user_admin_id);
        $this_user=$getusername['username'];
        if ($this->user->hasPermission('modify', 'sale/today')) {
            $data['modify']=true;
        }else { $data['modify']=false;}
        $data['product_status_id']=$product_status_id;
        $data['services']=$getusername['services'];
        $data['username']=$this_user;
        $data['user_confirm']=$user_admin_id;
        $data['back'] = $this->url->link('sale/today&token=' . $this->session->data['token'], true);
        $data['token']=$this->session->data['token'];
        $data['catalog'] = $this->request->server['HTTPS'] ? HTTPS_CATALOG : HTTP_CATALOG;
        $data['catalog_admin'] = $this->request->server['HTTPS'] ? HTTPS_SERVER : HTTP_SERVER;
        $data['category_id']=$category_id;
        $data['date']=$booking_date;
        $data['action']=$data['catalog_admin'].'?route=sale/today/getForm&token='.$this->session->data['token'].'&category_id='.$category_id.$url;

        $data['all_status'][]=array(
            'id'=>0,
            'name'=>'Не подтвержден'
        );
        $data['all_status'][]=array(
            'id'=>1,
            'name'=>'Подтвержден'
        );
        $data['all_status'][]=array(
            'id'=>2,
            'name'=>'Все'
        );

        $data['report'][]=array(
            'id'=>0,
            'name'=>'Сегодня'
        );
        $data['report'][]=array(
            'id'=>1,
            'name'=>'Въезд'
        );
        $data['report'][]=array(
            'id'=>2,
            'name'=>'Выезд'
        );
        $data['report_id']=$report_id;
		$results = $this->model_sale_today->getToday($filter_data,$booking_date,$category_id,$product_status_id);
		foreach ($results as $result) {
            $options=explode('<br>',$result['options']);
            $options=implode('<br>',array_unique($options));

			$data['today'][] = array(
				'order_product_id'          => $result['order_product_id'],
				'order_id'                  => $result['order_id'],
				'link_order'                => $this->url->link('sale/order/edit', 'token=' . $this->session->data['token'].'&order_id='.$result['order_id'], 'SSL'),
				'order_status_id'           => $result['order_status_id'],
				'product'                   => $result['product'],
				'customer_group_id'         => $result['customer_group_id'],
				'firstname'                 => $result['firstname'],
				'lastname'                  => $result['lastname'],
				'booking_date'              => $result['booking_date'],
				'quantity'                  => $result['quantity'],
				'total'                     => $result['total'],
				'options'                   => $options,
				'product_status_id'         => $result['product_status_id'],
				);
		}
        $users=$this->model_sale_today->getUsers();
        foreach($users as $user){
            $data['users'][] =array(

                'user_id'   =>$user['user_id'],
                'username'   =>$user['firstname'].' '.$user['lastname']
            );

        }

        $data['button_confirm'] = $this->language->get('button_confirm');
        $data['button_delete'] = $this->language->get('button_delete');
		if (isset($this->request->get['path'])) {
			if ($this->request->get['path'] != '') {
				$this->path = explode('_', $this->request->get['path']);
				$this->category_id = end($this->path);
				$this->session->data['path'] = $this->request->get['path'];
			} else {
				unset($this->session->data['path']);
			}
		} elseif (isset($this->session->data['path'])) {
			$this->path = explode('_', $this->session->data['path']);
			$this->category_id = end($this->path);
		}

        // API login
        $this->load->model('user/api');

        $api_info = $this->model_user_api->getApi($this->config->get('config_api_id'));



		$today_total = $this->model_sale_today->getAllProducts($booking_date,$category_id);

		$data['heading_title'] = $this->language->get('heading_title');
	    $this->document->setTitle($this->language->get('heading_title'));
		$data['text_list'] = $this->language->get('text_list');
		$data['text_no_results'] = $this->language->get('text_no_results');
		$data['text_confirm'] = $this->language->get('text_confirm');
		$data['column_name'] = $this->language->get('column_name');
		$data['column_product'] = $this->language->get('column_product');
		$data['column_option'] = $this->language->get('column_option');
		$data['column_quantity'] = $this->language->get('column_quantity');
		$data['column_total'] = $this->language->get('column_total');
		$data['column_action'] = $this->language->get('column_action');
		$data['column_customer_group_id'] = $this->language->get('column_customer_group_id');
		$data['button_today'] = $this->language->get('button_today');
		$data['button_nextday'] = $this->language->get('button_nextday');
		$data['button_back'] = $this->language->get('button_back');
		$data['button_confirm'] = $this->language->get('button_confirm');
		$data['button_allConfirm'] = $this->language->get('button_confirm');
		$data['button_rebuild'] = $this->language->get('button_rebuild');
		$data['button_back'] = $this->language->get('button_back');
		$data['entry_confirm'] = $this->language->get('entry_confirm');


            if(count($results)==0){
                    $data['error_warning'] = $this->language->get('text_no_results');;
                } else{
                if (isset($this->error['warning'])) {
			        $data['error_warning'] = $this->error['warning'];
                }  else {
			$data['error_warning'] = '';
		          }
                }

		if (isset($this->session->data['success'])) {
			$data['success'] = $this->session->data['success'];

			unset($this->session->data['success']);
		} else {
			$data['success'] = '';
		}
        if (isset($this->request->post['selected'])) {
			$data['selected'] = (array)$this->request->post['selected'];
		} else {
			$data['selected'] = array();
		}

		if (isset($this->request->post['selected'])) {
			$data['selected'] = (array)$this->request->post['selected'];
		} else {
			$data['selected'] = array();
		}

		$url = '';
        $url .= '&status=' . $product_status_id;
        $url .= '&report_id=' . $report_id;
		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}

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

		$data['sort_firstname'] = $this->url->link('sale/today/getForm', 'token=' . $this->session->data['token'] . '&sort=firstname'.'&category_name='.$category_name.'&category_id='.$category_id.  '&date='.$booking_date.$url, 'SSL');
        $data['sort_customer_group_id'] = $this->url->link('sale/today/getForm', 'token=' . $this->session->data['token'] . '&sort=customer_group_id'.'&category_name='.$category_name.'&category_id='.$category_id.'&date='.$booking_date. $url, 'SSL');
        $data['sort_product'] = $this->url->link('sale/today/getForm', 'token=' . $this->session->data['token'] . '&sort=product'.'&category_name='.$category_name.'&category_id='.$category_id.'&date='.$booking_date. $url, 'SSL');
        $data['sort_nextdate'] = $this->url->link('sale/today/getForm', 'token=' . $this->session->data['token'] .'&category_name='.$category_name.'&category_id='.$category_id.'&date='.$booking_date2.$url, 'SSL');
        $data['sort_back'] = $this->url->link('sale/today/getForm', 'token=' . $this->session->data['token'] .'&category_name='.$category_name.'&category_id='.$category_id.'&date='.date('Y-m-d',strtotime($booking_date.'-1 day')).$url, 'SSL');
        $data['sort_today'] = $this->url->link('sale/today/getForm', 'token=' . $this->session->data['token'] .'&category_name='.$category_name.'&category_id='.$category_id.'&date='.date('Y-m-d').$url, 'SSL');

		$url = '';

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

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

		if (isset($this->request->get['path'])) {
			$url .= '&path=' . $this->request->get['path'];
		}
		/*$pagination = new Pagination();
		$pagination->total =$today_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_limit_admin');
		$pagination->url = $this->url->link('sale/today/getForm', 'token=' . $this->session->data['token'] .'&category_name='.$category_name.'&category_id='.$category_id.'&date='.$booking_date. $url . '&page={page}', 'SSL');

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

		$data['results'] = sprintf($this->language->get('text_pagination'), ($today_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($today_total - $this->config->get('config_limit_admin'))) ? $today_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $today_total, ceil($today_total / $this->config->get('config_limit_admin')));
*/
		$data['sort'] = $sort;
		$data['order'] = $order;

		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');

		$this->response->setOutput($this->load->view('sale/today_form.tpl', $data));

    }
   
	protected function getList() {
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'name';
		}

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

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

		$url = '';

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

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

		$data['breadcrumbs'] = array();

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

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

		$data['todays_customer'] = array();

		$filter_data = array(
			'sort'  => $sort,
			'order' => $order,
			'limit' => $this->config->get('config_limit_admin')
		);
        $this->load->model('user/user');
        $user=$this->user->isLogged();
        $username=$this->model_user_user->getUser($user);
        $this->load->model('catalog/category');
		$products = $this->model_catalog_category->getCategories($filter_data);

        foreach ($products as $result) {
			$data['products'][] = array(
				'product_id'        => $result['category_id'],
				'model'            => $result['name'],
				'getConfirm'        => $this->url->link('sale/today/getForm', 'token=' . $this->session->data['token'] . '&category_name=' . html_entity_decode($result['name']). '&category_id=' . $result['category_id']. '&date='.date('Y-m-d'), 'SSL'),
				);
		}


		$data['heading_title'] = $this->language->get('heading_title');
		$data['text_list'] = $this->language->get('text_list');
		$data['text_no_results'] = $this->language->get('text_no_results');
		$data['text_confirm'] = $this->language->get('text_confirm');
		$data['column_name'] = $this->language->get('column_name');
		$data['column_product'] = $this->language->get('column_product');
		$data['column_option'] = $this->language->get('column_option');
		$data['column_quantity'] = $this->language->get('column_quantity');
		$data['column_total'] = $this->language->get('column_total');
		$data['column_action'] = $this->language->get('column_action');
		$data['column_group_id'] = $this->language->get('column_group_id');
		$data['button_getConfirm'] = $this->language->get('button_getConfirm');

		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}

		if (isset($this->session->data['success'])) {
			$data['success'] = $this->session->data['success'];

			unset($this->session->data['success']);
		} else {
			$data['success'] = '';
		}

		if (isset($this->request->post['selected'])) {
			$data['selected'] = (array)$this->request->post['selected'];
		} else {
			$data['selected'] = array();
		}

		$url = '';

		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}

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

		$data['sort_name'] = $this->url->link('sale/today', 'token=' . $this->session->data['token'] . '&sort=name' . $url, 'SSL');
		$data['sort_sort_order'] = $this->url->link('sale/today', 'token=' . $this->session->data['token'] . '&sort=sort_order' . $url, 'SSL');

		$url = '';

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

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

		if (isset($this->request->get['path'])) {
			$url .= '&path=' . $this->request->get['path'];
		}
		$data['sort'] = $sort;
		$data['order'] = $order;
		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');
		$this->response->setOutput($this->load->view('sale/today_list.tpl', $data));
	}
	public function  report(){

	    $this->language->load('sale/today');
        $data['heading_title'] = $this->language->get('heading_title');
        $data['text_list'] = $this->language->get('text_list');
        $data['text_no_results'] = $this->language->get('text_no_results');
        $data['text_confirm'] = $this->language->get('text_confirm');
        $data['column_name'] = $this->language->get('column_name');
        $data['column_product'] = $this->language->get('column_product');
        $data['column_option'] = $this->language->get('column_option');
        $data['column_quantity'] = $this->language->get('column_quantity');
        $data['column_total'] = $this->language->get('column_total');
        $data['column_action'] = $this->language->get('column_action');
        $data['column_group_id'] = $this->language->get('column_group_id');
        $data['button_getConfirm'] = $this->language->get('button_getConfirm');
        $data['column_customer_group_id'] = $this->language->get('column_customer_group_id');
        $data['date'] = date('d/m/Y',strtotime($this->request->get['date']));
        if (isset($this->request->get['sort'])) {
            $sort = $this->request->get['sort'];
        } else {
            $sort = 'firstname';
        }
        if (isset($this->request->get['order'])) {
            $order = $this->request->get['order'];
        } else {
            $order = 'ASC';
        }
        $booking_date=$this->request->get['date'];
        $category_id=$this->request->get['category_id'];
        $report_id=$this->request->get['report_id'];
        $status_id=$this->request->get['status_id'];
        $this->load->model('sale/today');
        $filter_data = array(
            'sort'  => $sort,
            'order' => $order,
            'report_id' => $report_id

        );
        $results = $this->model_sale_today->getToday($filter_data,$booking_date,$category_id,$status_id);
        foreach ($results as $result) {
            $options=explode('<br>',$result['options']);
            $options=implode('<br>',array_unique($options));
            $data['today'][] = array(
                'order_product_id'          => $result['order_product_id'],
                'order_id'                  => $result['order_id'],
                'order_status_id'           => $result['order_status_id'],
                'product'                   => $result['product'],
                'customer_group_id'         => $result['customer_group_id'],
                'firstname'                 => $result['firstname'],
                'lastname'                  => $result['lastname'],
                'booking_date'              => $result['booking_date'],
                'quantity'                  => $result['quantity'],
                'total'                     => $result['total'],
                'options'                   => $options,
            );
        }
        if($report_id==0){
            $data['report_text']="Отчет о заказах на";
        }elseif ($report_id==1){
            $data['report_text']="Отчет о въезде на";
        }else{
            $data['report_text']="Отчет о выездe на";
        }
        $this->response->setOutput($this->load->view('sale/report', $data));

    }
}
