<?php
class Weborder extends Controller
{
	private $access_id;
	private $ctrID;

	function __construct()
	{
		parent::__construct();

		header('location: https://partners.staging.cadline.hu/');

		$this->load->library('session');
		$this->load->library('Product');
		$this->load->model('mweborder');
		$this->lang->load('site');

		if ($this->phpsession->get('access_id')) {
			$this->access_id	= $this->phpsession->get('additional_access_id') != '' ? $this->phpsession->get('additional_access_id') : $this->phpsession->get('access_id');
			$this->ctrID 		= $this->mweborder->getCtrID($this->access_id);
		}
	}

	function login()
	{
		if (isset($_SESSION['error']) || isset($_SESSION['username'])) {
			unset($_SESSION['error']);
			unset($_SESSION['username']);
		}
		$username 	= $this->input->post('username');
		$password	= $this->input->post('password');
		$url 		= $this->input->post('current_url');

		if ($_POST['login-btn']) {
			$user = $this->mweborder->login($username, $password);

			if (!$user) {
				$_SESSION['error'] 		= 'Wrong username or password!';
				$_SESSION['username']	= $username;
			} else {
				$this->phpsession->save('bugtracker_access', true);
				$this->phpsession->save('access_id', $user->username);
			}

			redirect($url);
		}
	}

	function index()
	{
		$needProduct 		= false;
		$data 				= array();
		$data['country'] 	= $this->ctrID;

		if (!$this->mweborder->canOrder($data['country']) && $this->access_id != '') {
			show_404();
			return;
		}

		if (!$this->mweborder->weborderAccess()) {
			$this->mweborder->loadLoginTemplate();
			return;
		}

		/*if ($this->ctrID == '43') {
			$_SESSION['error_login'] = 'Online orders temporarily suspended. For further details, please contact <a href="mailto:office@cadline.hu">office@cadline.hu</a>.';
			redirect('partners');
		}*/

		if ($this->ctrID == '86') {
			$_SESSION['error_login'] = 'Online orders temporarily suspended. For further details, please contact <a href="mailto:office@cadline.hu">office@cadline.hu</a>.';
			redirect('partners');
		}

		if (isset($_POST['prod_id'])) {
			$needProduct				= true;
			$needSubscriptionComment 	= false;
			$data['selected_product'] 	= $_POST['prod_id'];
			$data['selected_category'] 	= $_POST['category'];
			$data['product'] 			= $this->mweborder->getSelectedProduct($_POST['prod_id'], $data['country']);
			$data['params']				= json_decode($data['product']->params);
			$data['latest_versions']	= $this->mweborder->getTheFourLatestVersions();
			$data['currency']			= $this->mweborder->getCurrency($data['country']);
			$data['needLive']			= $this->mweborder->needLiveChkbox($data['product']->kategoria, $data['country'], $data['product']->termek, $data['params']);
			$data['livePrice']			= Product::getLivePrice($data['country']);
			$data['floatingPrice']		= Product::getFloatingPrice($data['country']);
			$data['prev_sw_price']		= Product::getPrevSoftwarePrice($data['country']);
			$data['inst']				= $this->mweborder->getInstalments($data['country'], $data['product']->termek);
			$data['modules'] 			= $this->mweborder->getModules($data['country'], $data['product']->termek, $data['product']->kategoria);
			$data['lanPrice'] 			= Product::getLanPrice($data['country'], $data['product']->type);

			if ($data['product']->kategoria == 'perp' || $data['product']->kategoria == 'hardlock') {
				$data['hl_price'] 			= Product::getHardlockPrice($data['country']);
				$data['empty_hardlocks']	= $this->mweborder->getHardlockKeys($data['country']);

				if ($data['product']->incl_live == 1) {
					$data['bundle']	= $this->mweborder->getBundle($data['country']);
					$profPrice 		= 0;
					$livePrice 		= 0;
					$bundlePrice 	= 0;
					$prodName 		= '';
					$liveName 		= '';

					foreach ($data['bundle'] as $bundle) {
						$price = substr($data['country'], 0, 2) == '11' ? $bundle->usd : $bundle->euro;

						if ($bundle->kategoria == 'perp') {
							$profPrice = $price;
							$prodName = $bundle->termek;
						} else {
							$livePrice = $price;
							$liveName = $bundle->termek;
						}

						$bundlePrice += $price;
					}

					$data['product']->price 	= round($bundlePrice * 0.85);
					$data['product']->termek 	= $prodName;
					$data['bundleLivePrice'] 	= round($livePrice * 0.85);
					$data['bundleProfPrice']	= round($profPrice * 0.85);
					$data['bundleLiveName']		= $liveName;
				}
			}

			if (Product::needCertification($data['product']->id, $data['product']->kategoria))
				$data['cert_info'] = Product::getFileInfo($data['product']->id, $data['product']->kategoria);

			if ($data['product']->kategoria == 'nonp') {
				$data['client']	= date('d/m/Y', strtotime(date("Y-m-d") . '+30 days'));
				$data['date'] 	= date('d/m/Y', strtotime(date("Y-m-d") . '+60 days'));
				$data['nonp']	= Product::getNonprofitVersion($data['product']->termek, $data['product']->id);
			}

			if ($data['product']->kategoria == 'subscription') {
				$data['date'] 		= Product::getSubscriptionExpirationDate($data['product']->termek);
				$data['prodInfo']	= Product::getSubscriptionInfo($data['product']->termek);

				if (strpos($data['product']->termek, "monthly") && $data['product']->type != 'L' && $data['product']->type != '2') {
					if (!strpos($data['product']->termek, "Professional") && !strpos($data['product']->termek, "LIVE")) {
						$needSubscriptionComment	= true;
						$data['sub_price'] 			= $data['product']->price / 3;
						$data['modules']->price 	*= 3;
					}
				}
			}

			$data['sub_comment'] = $needSubscriptionComment;
		}

		$this->mweborder->loadTemplate('weborder/index.php', $data, 'Weborder', 'index', $needProduct);
	}

	function orders()
	{
		$data = array();

		if (!$this->mweborder->canOrder($this->ctrID) && $this->access_id != '') {
			$this->mweborder->loadLoginTemplate();
			return;
		}

		if (!$this->mweborder->weborderAccess()) {
			$this->mweborder->loadLoginTemplate();
			return;
		}

		$data['orders'] = $this->mweborder->getOrdersForCountry($this->ctrID);

		$this->mweborder->loadTemplate('weborder/orders.php', $data, 'Orders', 'orders');
	}

	function getProducts()
	{
		$products = $this->mweborder->getProducts($_POST['category'], $this->ctrID);

		echo json_encode($products);
	}

	function order()
	{
		$response = array();

		$data 	= json_decode($_POST['data']);
		$order	= $data->order;
		$file 	= isset($_FILES['file']) ? (object)$_FILES['file'] : false;

		$order->file = $file;

		$response = $this->mweborder->order($this->ctrID, $order);

		echo json_encode($response);
	}

	function createUser()
	{
		$response = array();

		$data = json_decode($_POST['data']);
		$user = $data->user;

		$response = $this->mweborder->createUser($user);

		echo json_encode($response);
	}

	function changeDistributor()
	{
		$isAdmin = $this->mweborder->isAdmin();

		$url = $this->uri->segment(4);

		$segments = explode("_", $url);

		$returnedUrl = '/' . $segments[0];

		$partner = $this->mweborder->getPartnerById($this->uri->segment(3));

		if (count($segments) > 1)
			$returnedUrl .= '/' . $segments[1];

		if ($isAdmin && $this->uri->segment(3) != '')
			$this->phpsession->save('additional_access_id', $partner->username);

		redirect($returnedUrl);
	}
}
