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

switch ($_GET["op"]) {

    case 'ajax_showPaymentsDetails':

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

        $sql = "SELECT
            pay_det_date
            ,DATE_FORMAT(pay_det_date, '%d-%b-%Y %H:%i:%s') AS pay_det_date_formatted
            ,pay_det_created_by
            ,(select CONCAT(user_firstname,' ',user_lastname) from tb_users where user_id = pay_det_created_by) AS cashier_full_name
            ,pay_det_payment_type
            ,pay_det_previous_debt
            ,pay_det_value
            ,pay_det_income
            ,pay_det_card_tax
            ,pay_det_change
            ,pay_det_note
            ,pay_det_cashback
            ,pay_det_cashback_status
            FROM tb_payments_details
            WHERE pay_det_sale_id={$sale_id}";

        // exit($sql);

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

        break;


    case 'ajax_paymentsReport':

        $ent_id = isset($_GET["ent_id"]) ? funCheckInput($_GET["ent_id"]) : "";
        $pos_id = isset($_GET["pos_id"]) ? funCheckInput($_GET["pos_id"]) : "";
        $pay_det_sale_type = isset($_GET["pay_det_sale_type"]) ? funCheckInput($_GET["pay_det_sale_type"]) : "";
        $pay_det_payment_type = isset($_GET["pay_det_payment_type"]) ? funCheckInput($_GET["pay_det_payment_type"]) : "";
        $pay_det_created_by = isset($_GET["pay_det_created_by"]) ? funCheckInput($_GET["pay_det_created_by"]) : "";
        $group_by_period = isset($_GET["group_by_period"]) ? funCheckInput($_GET["group_by_period"]) : "";
        $fromDate = isset($_GET["fromDate"]) ? funCheckInput($_GET["fromDate"]) : "";
        $toDate = isset($_GET["toDate"]) ? funCheckInput($_GET["toDate"]) : "";

        if (empty($pos_id)) exit();
        $posInfo = getBranchData($pos_id);

        $whereEnterprise = "WHERE pay_det_ent_id = '$ent_id'";
        $wherePos = "AND pay_det_pos_id = '$pos_id'";

        $DayStartTime = $posInfo["pos_day_start_time"] ?? "07:00:00";
        $DayEndTime = $posInfo["pos_day_end_time"] ?? "06:59:59";

        $fromDate = date("{$fromDate} {$DayStartTime}");
        $toDate = date("Y-m-d", strtotime("{$toDate} +1 day")) . " {$DayEndTime}";

        $whereDate = "AND pay_det_date BETWEEN '{$currentDayStart}' AND '{$currentDayEnd}'";
        if (!empty(trim((string) $fromDate)) && !empty(trim((string) $toDate))) {
            $whereDate = "AND pay_det_date BETWEEN '{$fromDate}' AND '{$toDate}'";
        }

        $whereSaleType = "";
        if (!empty($pay_det_sale_type)) $whereSaleType = "AND pay_det_sale_type = '$pay_det_sale_type'";

        $wherePaymentType = "AND pay_det_payment_type != 'CASHBACK'";
        if (!empty($pay_det_payment_type)) $wherePaymentType = "AND pay_det_payment_type = '$pay_det_payment_type'";

        $whereEmployee = "";
        if (!empty($pay_det_created_by)) $whereEmployee = "AND pay_det_created_by = '$pay_det_created_by'";

        $datePeriodSelected = "DATE_FORMAT(DATE_ADD(pay_det_date, INTERVAL -8 HOUR), '%d-%b-%Y') AS pay_det_period";
        if ($group_by_period == "month") $datePeriodSelected = "DATE_FORMAT(DATE_ADD(pay_det_date, INTERVAL -8 HOUR), '%M %Y') AS pay_det_period";
        if ($group_by_period == "year") $datePeriodSelected = "DATE_FORMAT(DATE_ADD(pay_det_date, INTERVAL -8 HOUR), '%Y') AS pay_det_period";

        $sql = "SELECT
        $datePeriodSelected
        ,pay_det_sale_type
        ,pay_det_payment_type
        ,(select pos_name FROM tb_pos WHERE pos_id = pay_det_pos_id) AS pos_name
        ,IFNULL(SUM(pay_det_income),0) AS total_pay_det_income
        ,IFNULL(SUM(CASE WHEN pay_det_date <= pay_det_sale_close THEN pay_det_income ELSE 0 END), 0) AS pay_det_income
        ,IFNULL(SUM(CASE WHEN pay_det_date > pay_det_sale_close THEN pay_det_income ELSE 0 END), 0) AS pay_det_debt_collect
        FROM tb_payments_details
        $whereEnterprise
        $wherePos
        $whereDate
        $whereSaleType
        $wherePaymentType
        $whereEmployee
        AND pay_det_status = 'Enabled'
        GROUP BY pay_det_period,pay_det_payment_type
        ORDER BY pay_det_id ASC;";
        // exit($sql);

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

        break;
}
