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

switch ($_GET["op"]) {

    case 'ajaxSaveUpdate':

        $sale_type_id = isset($_POST["sale_type_id"]) ? funCheckInput($_POST["sale_type_id"]) : "";
        $sale_type_ent_id = isset($_POST["sale_type_ent_id"]) ? funCheckInput($_POST["sale_type_ent_id"]) : "";
        $sale_type_pos_id = isset($_POST["sale_type_pos_id"]) ? funCheckInput($_POST["sale_type_pos_id"]) : "";
        $sale_type_name = isset($_POST["sale_type_name"]) ? funCheckInput($_POST["sale_type_name"]) : "";
        $sale_type_tax = isset($_POST["sale_type_tax"]) ? funCheckInput($_POST["sale_type_tax"]) : "";
        $sale_type_tax_law = isset($_POST["sale_type_tax_law"]) ? funCheckInput($_POST["sale_type_tax_law"]) : "";
        $sale_type_add_tax = isset($_POST["sale_type_add_tax"]) ? funCheckInput($_POST["sale_type_add_tax"]) : "";

        $sale_type_name = ucwords(strtolower($sale_type_name));

        if (empty($sale_type_ent_id)) $sale_type_ent_id = $session_user_ent_id;
        if (empty($sale_type_pos_id)) $sale_type_pos_id = $session_user_pos_id;

        try {

            $pdo->beginTransaction();

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

            if ($isNew) {

                $stmt = $pdo->prepare("INSERT INTO tb_sale_type (
                    sale_type_ent_id,
                    sale_type_pos_id,
                    sale_type_name,
                    sale_type_tax,
                    sale_type_tax_law,
                    sale_type_add_tax,
                    sale_type_created_by
                    ) VALUES (
                    :ent_id,
                    :pos_id,
                    :name,
                    :tax,
                    :tax_law,
                    :add_tax,
                    :user_id
                )");

                $stmt->execute([
                    ':ent_id'   => $sale_type_ent_id,
                    ':pos_id'   => $sale_type_pos_id,
                    ':name'     => $sale_type_name,
                    ':tax'      => $sale_type_tax,
                    ':tax_law'  => $sale_type_tax_law,
                    ':add_tax'  => $sale_type_add_tax,
                    ':user_id'  => $session_user_id
                ]);

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

                $stmt = $pdo->prepare("UPDATE tb_sale_type SET
                    sale_type_name     = :name,
                    sale_type_tax      = :tax,
                    sale_type_tax_law  = :tax_law,
                    sale_type_add_tax  = :add_tax
                    WHERE sale_type_id = :id
                ");

                $stmt->execute([
                    ':name'     => $sale_type_name,
                    ':tax'      => $sale_type_tax,
                    ':tax_law'  => $sale_type_tax_law,
                    ':add_tax'  => $sale_type_add_tax,
                    ':id'       => $sale_type_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 'ajaxSaleTypeList':

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

        $sql = "SELECT *
		,(select pos_name FROM tb_pos WHERE sale_type_pos_id = pos_id) AS pos_name
        FROM tb_sale_type
        WHERE sale_type_ent_id = '{$ent_id}' AND sale_type_pos_id = '{$pos_id}';";
        // exit($sql);
        $rows = executePDOFetchAllAPI($sql);
        echo json_encode($rows);

        break;


    case 'ajaxSelectSaleType':

        $pos_id = isset($_GET["pos_id"]) ? funCheckInput($_GET["pos_id"]) : "";
        $sale_type_name = isset($_GET["sale_type_name"]) ? funCheckInput($_GET["sale_type_name"]) : "";
        $fromSection = isset($_GET["section"]) ? funCheckInput($_GET["section"]) : "";

        $customWhere = "AND sale_type_status = 'Enabled'";
        if (!empty($sale_type_name)) $customWhere = "AND sale_type_name = '$sale_type_name'";
        if ($fromSection == "sales-report") $customWhere = "";

        $sql = "SELECT sale_type_name FROM tb_sale_type WHERE sale_type_pos_id = '$pos_id' $customWhere;";

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


    case 'ajaxGetSaleTypeTax':

        $sale_type_ent_id = isset($_GET["sale_type_ent_id"]) ? funCheckInput($_GET["sale_type_ent_id"]) : "";
        $sale_type_pos_id = isset($_GET["sale_type_pos_id"]) ? funCheckInput($_GET["sale_type_pos_id"]) : "";
        $sale_type_name = isset($_GET["sale_type_name"]) ? funCheckInput($_GET["sale_type_name"]) : "";

        $sql = "SELECT * FROM tb_sale_type WHERE sale_type_ent_id = '{$sale_type_ent_id}' AND sale_type_pos_id = '{$sale_type_pos_id}' AND sale_type_name = '{$sale_type_name}';";

        $answer = executePDOFetchOne($sql, $translate['could_not_display_data']);
        echo json_encode($answer);
        break;


    case 'ajaxSwitchStatus':

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

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

        break;
}
