<?php
require "global.ajax.php";

switch ($_GET["op"]) {

	case 'ajaxSaveUpdate':

		// POST VARIABLES
		$fis_rec_id = isset($_POST["fis_rec_id"]) ? funCheckInput($_POST["fis_rec_id"]) : "";
		$fis_rec_ent_id = isset($_POST["fis_rec_ent_id"]) ? funCheckInput($_POST["fis_rec_ent_id"]) : "";
		$fis_rec_type = isset($_POST["fis_rec_type"]) ? funCheckInput($_POST["fis_rec_type"]) : "";
		$fis_rec_description = isset($_POST["fis_rec_description"]) ? funCheckInput($_POST["fis_rec_description"]) : "";

		$fis_rec_type = mb_strtoupper($fis_rec_type);

		try {

			$pdo->beginTransaction();

			$isNew = empty(trim($fis_rec_id));

			if ($isNew) {

				$stmt = $pdo->prepare("INSERT INTO tb_fiscal_receipt_list (
					fis_rec_ent_id,
					fis_rec_type,
					fis_rec_description,
					fis_rec_created_by
					) VALUES (
					:ent_id,
					:type,
					:description,
					:created_by
				)");

				$stmt->execute([
					':ent_id'     => $fis_rec_ent_id,
					':type'       => $fis_rec_type,
					':description' => $fis_rec_description,
					':created_by' => $session_user_id
				]);

				$response = array("message" => $translate['saved_successfully'], "status" => "success");
			} else {

				$stmt = $pdo->prepare("UPDATE tb_fiscal_receipt_list SET
					fis_rec_type = :type,
					fis_rec_description = :description
					WHERE fis_rec_id = :id
				");

				$stmt->execute([
					':type'        => $fis_rec_type,
					':description' => $fis_rec_description,
					':id'          => $fis_rec_id
				]);

				$response = array("message" => $translate['updated_successfully'], "status" => "success");
			}

			$pdo->commit();
		} catch (\RuntimeException | \InvalidArgumentException $e) {
			if ($pdo->inTransaction()) $pdo->rollBack();
			$response = array("status" => "error", "message" => "Database error. {$translate['execution_failed_please_contact_the_system_vendor_for_technical_assistance']} <br>" . $e->getMessage(), "redirect" => true);
		} catch (\PDOException $e) {
			if ($pdo->inTransaction()) $pdo->rollBack();
			$response = array("status" => "error", "message" => "Database error. {$translate['execution_failed_please_contact_the_system_vendor_for_technical_assistance']} <br>" . $e->getMessage(), "redirect" => true);
		}

		echo json_encode($response);

		break;


	case 'ajaxSaveNewRequest':

		try {

			$pdo->beginTransaction();

			$fiscal_entry_fis_rec_id = isset($_POST["fiscal_entry_fis_rec_id"]) ? funCheckInput($_POST["fiscal_entry_fis_rec_id"]) : "";
			$fiscal_entry_ent_id = isset($_POST["fiscal_entry_ent_id"]) ? funCheckInput($_POST["fiscal_entry_ent_id"]) : "";
			$fiscal_entry_type = isset($_POST["fiscal_entry_type"]) ? funCheckInput($_POST["fiscal_entry_type"]) : "";
			$fiscal_entry_description = isset($_POST["fiscal_entry_description"]) ? funCheckInput($_POST["fiscal_entry_description"]) : "";
			$fiscal_entry_from = isset($_POST["fiscal_entry_from"]) ? funCheckInput($_POST["fiscal_entry_from"]) : "";
			$fiscal_entry_to = isset($_POST["fiscal_entry_to"]) ? funCheckInput($_POST["fiscal_entry_to"]) : "";
			$fiscal_entry_approved = isset($_POST["fiscal_entry_approved"]) ? funCheckInput($_POST["fiscal_entry_approved"]) : "";
			$fiscal_entry_expire_date = isset($_POST["fiscal_entry_expire_date"]) ? funCheckInput($_POST["fiscal_entry_expire_date"]) : "";

			$stmt = $pdo->prepare("INSERT INTO tb_fiscal_receipt_entry (fiscal_entry_fis_rec_id,fiscal_entry_ent_id,fiscal_entry_type,fiscal_entry_description,fiscal_entry_from,fiscal_entry_to,fiscal_entry_approved,fiscal_entry_expire_date)
			VALUES (:fis_rec_id,:ent_id,:type,:description,:from_val,:to_val,:approved,:expire_date)");

			$stmt->execute([
				':fis_rec_id'   => $fiscal_entry_fis_rec_id,
				':ent_id'       => $fiscal_entry_ent_id,
				':type'         => $fiscal_entry_type,
				':description'  => $fiscal_entry_description,
				':from_val'     => $fiscal_entry_from,
				':to_val'       => $fiscal_entry_to,
				':approved'     => $fiscal_entry_approved,
				':expire_date'  => $fiscal_entry_expire_date
			]);

			$response = array("message" => $translate['saved_successfully'], "status" => "success");

			$pdo->commit();
		} catch (\RuntimeException | \InvalidArgumentException $e) {
			if ($pdo->inTransaction()) $pdo->rollBack();
			$response = array("status" => "error", "message" => "Database error. {$translate['execution_failed_please_contact_the_system_vendor_for_technical_assistance']} <br>" . $e->getMessage(), "redirect" => true);
		} catch (\PDOException $e) {
			if ($pdo->inTransaction()) $pdo->rollBack();
			$response = array("status" => "error", "message" => "Database error. {$translate['execution_failed_please_contact_the_system_vendor_for_technical_assistance']} <br>" . $e->getMessage(), "redirect" => true);
		}

		echo json_encode($response);

		break;


	case 'ajaxSwitchStatus':

		$fis_rec_id = isset($_POST["fis_rec_id"]) ? funCheckInput($_POST["fis_rec_id"]) : "";
		$status = isset($_POST["status"]) ? funCheckInput($_POST["status"]) : "";

		$sql = "UPDATE tb_fiscal_receipt_list SET fis_rec_status = '{$status}' WHERE fis_rec_id = '$fis_rec_id';";
		$result = executeQueryPDO($sql, $translate['successfully_executed'], $translate['execution_failed']);
		echo json_encode($result);

		break;

	case 'ajaxTaxReceiptList':

		$ent_id = isset($_GET["ent_id"]) ? funCheckInput($_GET["ent_id"]) : "";

		$sql = "SELECT
		fis_rec_id
		,fis_rec_ent_id
		,fis_rec_type
		,fis_rec_description
		,fis_rec_status
		,(SELECT IFNULL(SUM(fiscal_entry_approved),0) FROM tb_fiscal_receipt_entry WHERE fiscal_entry_ent_id = {$ent_id} AND fiscal_entry_fis_rec_id = fis_rec_id)-(SELECT COUNT(*) FROM tb_fiscal_receipt_transactions WHERE fiscal_ent_id = {$ent_id} AND fiscal_type = fis_rec_type) AS fiscal_stock_actual
		,(SELECT COUNT(*) FROM tb_fiscal_receipt_transactions WHERE fiscal_ent_id = {$ent_id} AND fiscal_type = fis_rec_type) AS fiscal_stock_used
		FROM tb_fiscal_receipt_list
		WHERE fis_rec_ent_id = {$ent_id};";

		$rows = executePDOFetchAllAPI($sql);
		echo json_encode($rows);

		break;

	case 'ajaxTaxReceiptRequestHistory':

		$fis_rec_id = isset($_GET["fis_rec_id"]) ? funCheckInput($_GET["fis_rec_id"]) : "";
		$fis_rec_ent_id = isset($_GET["fis_rec_ent_id"]) ? funCheckInput($_GET["fis_rec_ent_id"]) : "";

		$sql = "SELECT
		fiscal_entry_id
		,fiscal_entry_type
		,fiscal_entry_from
		,fiscal_entry_to
		,fiscal_entry_approved
		,DATE_FORMAT(fiscal_entry_expire_date, '%d-%b-%Y') AS fiscal_entry_expire_date
		FROM tb_fiscal_receipt_entry WHERE fiscal_entry_ent_id = {$fis_rec_ent_id} AND fiscal_entry_fis_rec_id = {$fis_rec_id};";

		// exit($sql);
		$rows = executePDOFetchAllAPI($sql);
		echo json_encode($rows);

		break;


	case 'ajax_selectFiscalList':

		$ent_id = isset($_POST["ent_id"]) ? funCheckInput($_POST["ent_id"]) : "";
		$fis_rec_type = isset($_POST["sale_ncf_type"]) ? funCheckInput($_POST["sale_ncf_type"]) : "";

		$customWhere = "WHERE fis_rec_ent_id = {$ent_id} AND fis_rec_status = 'Enabled' AND
		(SELECT IFNULL(SUM(fiscal_entry_approved),0) FROM tb_fiscal_receipt_entry WHERE fiscal_entry_ent_id = {$ent_id} AND fiscal_entry_fis_rec_id = fis_rec_id)-(SELECT COUNT(*) FROM tb_fiscal_receipt_transactions WHERE fiscal_ent_id = {$ent_id} AND fiscal_type = fis_rec_type) > 0
		GROUP BY fis_rec_id ORDER BY fis_rec_type";

		if (!empty($fis_rec_type)) $customWhere = "WHERE fis_rec_ent_id = {$ent_id} AND fis_rec_type = '$fis_rec_type'";

		$sql = "SELECT
		fis_rec_id
		,fis_rec_type
		,fis_rec_description
		FROM tb_fiscal_receipt_list
		$customWhere;";
		// exit($sql);
		$answer = executePDOFetchAll($sql, $translate['could_not_display_data']);

		if (empty($fis_rec_type)) {
			echo "<option value='' >-- {$translate['none']} --</option>";
		}

		foreach ($answer as $key => $value) {

			$selected = "";

			if ($fis_rec_type === $value['fis_rec_type']) {
				$selected = "selected";
			}

			echo "<option value='{$value['fis_rec_type']}' {$selected} > {$value['fis_rec_type']} - {$value['fis_rec_description']}</option>";
		}

		break;


	case 'ajax_selectFiscalListReport':

		$ent_id = isset($_POST["ent_id"]) ? funCheckInput($_POST["ent_id"]) : "";

		$sql = "SELECT
		fis_rec_id
		,fis_rec_type
		,fis_rec_description
		FROM tb_fiscal_receipt_list
		WHERE fis_rec_ent_id = {$ent_id};";
		// exit($sql);
		$answer = executePDOFetchAll($sql, $translate['could_not_display_data']);
		echo json_encode($answer);

		break;


	case 'ajaxCheckIfNotExist':

		$ent_id = isset($_GET["ent_id"]) ? funCheckInput($_GET["ent_id"]) : "";
		$fis_rec_id = isset($_GET["fis_rec_id"]) ? funCheckInput($_GET["fis_rec_id"]) : "";
		$fis_rec_type = isset($_GET["fis_rec_type"]) ? funCheckInput($_GET["fis_rec_type"]) : "";
		$fis_rec_type = mb_strtoupper($fis_rec_type);

		$whereFisRecId = "";

		if (!empty($fis_rec_id)) {
			$whereFisRecId = "AND fis_rec_id != '$fis_rec_id'";
		}

		$sql = "SELECT COUNT(fis_rec_type) AS fis_rec_type_quantity FROM tb_fiscal_receipt_list WHERE fis_rec_ent_id = {$ent_id} AND fis_rec_type = '$fis_rec_type' $whereFisRecId;";
		// exit($sql);

		$result = executePDOFetchOneAPI($sql);
		echo json_encode($result);

		break;


	case 'ajaxResetTaxReceipt':

		$fis_rec_id = isset($_POST["fis_rec_id"]) ? funCheckInput($_POST["fis_rec_id"]) : "";
		$fis_rec_ent_id = isset($_POST["fis_rec_ent_id"]) ? funCheckInput($_POST["fis_rec_ent_id"]) : "";

		$sql = "DELETE FROM tb_fiscal_receipt_entry WHERE fiscal_entry_fis_rec_id = '$fis_rec_id' AND fiscal_entry_ent_id = '$fis_rec_ent_id';
		DELETE FROM tb_fiscal_receipt_transactions WHERE fiscal_fis_rec_id = '$fis_rec_id' AND fiscal_ent_id = '$fis_rec_ent_id';";
		// exit($sql);
		$result = executeQueryPDO($sql, $translate['successfully_executed'], $translate['execution_failed']);
		echo json_encode($result);

		break;
}
