<?php
error_reporting(1);
if (login_control()) {
	if ($user["user_authority"] == 2) {

		if (!permission('access', 'reports')) {
			$page_title = "Yetkisiz Erişim";
			require admin_view("unauthorized");
			exit();
		}

		$servisler = $db->prepare("SELECT * FROM services");
		$servisler->execute();
		$servisler = $servisler->fetchAll();

		function hesap($x)
		{
			if (is_array($x)) {
				return $x;
			} else {
				return "0";
			}
		}

		if (isset($_GET['services']) || isset($_GET['statuses'])) {
			$whereservis = "WHERE ";
			$wheredurum = "";
			if (isset($_GET['services'])) {
				$servis = $_GET['services'];
				$i = 0;
				foreach ($servis as $s) {
					if ($i == 0) {
						$whereservis .= "(service_id = " . $s;
					} else {
						$whereservis .= " or service_id = " . $s;
					}
					$i++;
				}
				$whereservis .= ")";
			}
			if (isset($_GET['statuses'])) {
				$durum = $_GET['statuses'];
				$e = 0;
				foreach ($durum as $durum) {
					if ($e == 0) {
						$wheredurum .= '(order_status = \'' . $durum . '\'';
					} else {
						$wheredurum .= ' or order_status = \'' . $durum . '\'';
					}
					$e++;
				}
				$wheredurum .= ")";
			}
			if (isset($_GET['statuses']) && isset($_GET['services'])) {
				$wheretotal = $whereservis . " and " . $wheredurum;
			} elseif (isset($_GET['services'])) {
				$wheretotal = $whereservis;
			} elseif (isset($_GET['statuses'])) {
				$wheretotal =  $whereservis.' '. $wheredurum;
			}
		
			$select = "SELECT * FROM `orders` " . $wheretotal;
			
			$bul = $db->prepare($select);
			$bul->execute();
			$reportdata = $bul->fetchAll();
			$aygun = array();
			$apigun = array();
			foreach ($reportdata as $key => $value) {
				$reportdata[$key]['gun'] = idate('d', $reportdata[$key]['order_date']);
				$reportdata[$key]['ay'] = idate('m', $reportdata[$key]['order_date']);
				$reportdata[$key]['yil'] = idate('Y', $reportdata[$key]['order_date']);
				if (is_array($aygun[$reportdata[$key]['yil']])) {
					if (is_array($aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']])) {
						if (is_array($aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']])) {
							$aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] -= floatval($value['api_order_amount']);
						} else {
							$aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] -= floatval($value['api_order_amount']);
						}
					} else {
						$aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']] = array();
						if (is_array($aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']])) {
							$aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] -= floatval($value['api_order_amount']);
						} else {
							$aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] -= floatval($value['api_order_amount']);
						}
					}
				} else {
					$aygun[$reportdata[$key]['yil']] = array();
					if (is_array($aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']])) {
						if (is_array($aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']])) {
							$aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] -= floatval($value['api_order_amount']);
						} else {
							$aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] -= floatval($value['api_order_amount']);
						}
					} else {
						$aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']] = array();
						if (is_array($aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']])) {
							$aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] -= floatval($value['api_order_amount']);
						} else {
							$aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] -= floatval($value['api_order_amount']);
						}
					}
				}
			}
			
		} else {
			//echo $wheretotal;
			$select = "SELECT * FROM `orders`";
			//echo $select;
			$bul = $db->prepare($select);
			$bul->execute();
			$reportdata = $bul->fetchAll();
			$aygun = array();
			$apigun = array();
			foreach ($reportdata as $key => $value) {
				$reportdata[$key]['gun'] = idate('d', $reportdata[$key]['order_date']);
				$reportdata[$key]['ay'] = idate('m', $reportdata[$key]['order_date']);
				$reportdata[$key]['yil'] = idate('Y', $reportdata[$key]['order_date']);
				if (is_array($aygun[$reportdata[$key]['yil']])) {
					if (is_array($aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']])) {
						if (is_array($aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']])) {
							$aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] -= floatval($value['api_order_amount']);
						} else {
							$aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] -= floatval($value['api_order_amount']);
						}
					} else {
						$aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']] = array();
						if (is_array($aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']])) {
							$aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] -= floatval($value['api_order_amount']);
						} else {
							$aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] -= floatval($value['api_order_amount']);
						}
					}
				} else {
					$aygun[$reportdata[$key]['yil']] = array();
					if (is_array($aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']])) {
						if (is_array($aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']])) {
							$aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] -= floatval($value['api_order_amount']);
						} else {
							$aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] -= floatval($value['api_order_amount']);
						}
					} else {
						$aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']] = array();
						if (is_array($aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']])) {
							$aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] -= floatval($value['api_order_amount']);
						} else {
							$aygun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] += floatval($value['order_amount']);
							$apigun[$reportdata[$key]['yil']][$reportdata[$key]['ay']][$reportdata[$key]['gun']] -= floatval($value['api_order_amount']);
						}
					}
				}
				//echo $whereservis."<br>".$wheredurum;
			}
		}
		$services = $db->from('services')
			->join('categories', 'categories.category_id = services.category_id')
			->leftjoin('apis', 'apis.api_id = services.api_id')
			->orderby('service_number', 'ASC')
			->all();
		$serviceList    = array_group_by($services, 'category_name');
		$apis = $db->from('apis')->orderby('api_id', 'ASC')->all();
		$categories = $db->from('categories')->orderby('category_number', 'ASC')->all();
		$tempCategories = [];
		foreach ($categories as $category) {
			$category_name = json_decode($category["category_name"], true);
			$name = null;
			foreach ($category_name as $details) {
				if ($details["lang"] == 1) {
					$name = $details["name"];
				}
			}
			$category["category_name"] = $name;
			$tempCategories[] = $category;
		}
		$categories = $tempCategories;
		$tempServices = [];
		foreach ($services as $service) {
			$category_name = json_decode($service["category_name"], true);
			$name = null;
			foreach ($category_name as $details) {
				if ($details["lang"] == 1) {
					$name = $details["name"];
				}
			}
			$service["category_name"] = $name;
			$service_name = json_decode($service["service_name"], true);
			$name = null;
			foreach ($service_name as $details) {
				if ($details["lang"] == 1) {
					$name = $details["name"];
				}
			}
			$service["service_name"] = $name;
			$tempServices[] = $service;
		}
		$services = $tempServices;
		$title = "İstatistikler";
		$page_title = "İstatistikler";
		require admin_view('reports');
	} else {
		require controller("404");
	}
} else {
	require controller("404");
}
