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

		$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 = date('Y-m-d', strtotime(date('Y') . '-' . date('m') . '-01'));
		}

		if (isset($this->request->get['filter_date_end'])) {
			$filter_date_end = $this->request->get['filter_date_end'];
		} else {
			$filter_date_end = date('Y-m-d');
		}
		
		if (isset($this->request->get['filter_group'])) {
			$filter_group = $this->request->get['filter_group'];
		} else {
			$filter_group = 'week';
		}
		
		if (isset($this->request->get['filter_auction_status_id'])) {
			$filter_auction_status_id = $this->request->get['filter_auction_status_id'];
		} else {
			$filter_auction_status_id = 0;
		}	
		
		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_auction_status_id'])) {
			$url .= '&filter_auction_status_id=' . $this->request->get['filter_auction_status_id'];
		}
								
		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_auction', 'token=' . $this->session->data['token'] . $url, 'SSL'),
      		'separator' => ' :: '
   		);
		
		if ($this->config->get('config_auction_status_id') && is_array($this->config->get('config_auction_status_id'))) {
			$auction_status_ids = $this->config->get('config_auction_status_id');
		} else {
			$auction_status_ids = array('starting' => 1, 'relisting' => 2, 'inprogress' => 3, 'extended' => 4, 'won' => 5, 'expired' => 6, 'sold' => 7, 'voided' => 8);
		}

		$this->load->model('report/sale_auction');
		
		$this->data['auctions'] = array();
		
		$data = array(
			'filter_date_start'	       => $filter_date_start, 
			'filter_date_end'	       => $filter_date_end, 
			'filter_group'             => $filter_group,
			'filter_auction_status_id' => $filter_auction_status_id,
			'auction_status_ids'       => $auction_status_ids,
			'start'                    => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit'                    => $this->config->get('config_admin_limit')
		);
		
		$this->load->model('localisation/auction_status');

		$auction_statuses = $this->model_localisation_auction_status->getAuctionStatuses();

		$this->data['text_auction_statuses'] = array();
		$this->data['auction_status_ids'] = array();
		$this->data['auction_statuses'] = array();

		foreach ($auction_status_ids as $key => $value) {
			foreach ($auction_statuses as $auction_status) {
				if ($auction_status['auction_status_id'] == $value) {
					$this->data['text_auction_statuses'][$key] = $auction_status['name'];
					$this->data['auction_status_ids'][$key] = $value;
					$this->data['auction_statuses'][(string)$value] = $auction_status['name'];
				}
			}
		}

		$auction_total = $this->model_report_sale_auction->getTotalAuctions($data);
		
		$results = $this->model_report_sale_auction->getAuctions($data);
		
		foreach ($results as $result) {
			$this->data['auctions'][] = 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'])),
				'auctions'     => (int)$result['auctions'],
				'auction_bids' => (int)$result['auction_bids'],
				'customers'    => (int)$result['customers'],
				'total_worth'  => $this->currency->format($result['total_worth'], $this->config->get('config_currency')),
				'total_paid'   => $this->currency->format($result['total_paid'], $this->config->get('config_currency'))
			);
		}

		$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['column_date_start'] = $this->language->get('column_date_start');
		$this->data['column_date_end'] = $this->language->get('column_date_end');
    	$this->data['column_auctions'] = $this->language->get('column_auctions');
		$this->data['column_auction_bids'] = $this->language->get('column_auction_bids');
		$this->data['column_customers'] = $this->language->get('column_customers');
		$this->data['column_total_worth'] = $this->language->get('column_total_worth');
		$this->data['column_total_paid'] = $this->language->get('column_total_paid');
		
		$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['button_filter'] = $this->language->get('button_filter');
		
		$this->data['token'] = $this->session->data['token'];
		
		$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_auction_status_id'])) {
			$url .= '&filter_auction_status_id=' . $this->request->get['filter_auction_status_id'];
		}
				
		$pagination = new Pagination();
		$pagination->total = $auction_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_auction', '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_auction_status_id'] = $filter_auction_status_id;
				 
		$this->template = 'report/sale_auction.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}
}
?>