<?php
/*
* @ServerMon v1.1
* @Developed by Maksa988
* @www.Server-Mon.ru
* @Copyright (C) 2014
*/
 if(!defined('SMON'))
	die('Hacking attempt!');
	
class licenses extends Controller {
	public function __construct() {
		global $db;
		
		$this->header();
		$this->data['activeSection'] = 'store';
		$this->data['navbarName'] = 'Заказ услуг для сервера';
		
		if(empty($_SESSION['user_id'])):
			return $this->view(array('common/header', 'user/access', 'common/footer'), $this->data); //Загружаем шаблон
		endif;
		
		$this->model('licenses');
		$this->model('users');
		
		$this->data['price'] = $this->setting('price');
		$user = $this->usersModel->getUser($_SESSION['user_id']);
		
		if($this->GET() >= '1'){
			$license = $this->licensesModel->getLicenseInfo($this->GET());
			$this->data['license'] = $license;
			
			if($license['user_id'] != $_SESSION['user_id']):
				return $this->view(array('common/header', 'user/access', 'common/footer'), $this->data); //Загружаем шаблон
			endif;
			
			if($license){
				if($this->POST('store_license')){
					if($this->POST('month') == '1'){
						$price = $this->setting('price');
					} elseif($this->POST('month') == '3'){
						$price = $this->setting('price')*3;
					} elseif($this->POST('month') == '6'){
						$price = $this->setting('price')*6;
					} elseif($this->POST('month') == '12'){
						$price = $this->setting('price')*12;
					} else {
						$this->data['error'] = 'Выберите срок оплаты';
					}
						
					if($user['user_balance'] >= $price){
						if($this->licensesModel->buyLicense($this->GET(), $this->POST('month'))){
							$this->usersModel->downUserBalance($_SESSION['user_id'], $price);
							$this->usersModel->addInvoice('2', $_SESSION['user_id'], $price, 'Покупка лицензии');
							$this->usersModel->editUser("`user_client` = '1'", $_SESSION['user_id']);
							$this->data['success'] = 'Вы успешно оплатили лицензию';
						} else {
							$this->data['error'] = 'При оплате произошла ошибка';
						}
					} else {
						$this->data['error'] = 'На вашем балансе недостаточно денег';
					}
				}
			} else {
				return $this->view(array('common/header', 'errors/404license', 'common/footer'), $this->data); //Загружаем шаблон
			}
		} else{
			return $this->view(array('common/header', 'errors/404license', 'common/footer'), $this->data); //Загружаем шаблон
		}
		
		return $this->view(array('common/header', 'licenses/store', 'common/footer'), $this->data); //Загружаем шаблон
	}
}