<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AkuntansiController extends Controller
{
    function getSaldoPerkiraan(Request $request)
    {
        $tanggal = $request->tanggal ?? date('Y-m-d');
        $kode_perkiraan = $request->kode_perkiraan;
        $perkiraan = DB::table('ref_perkiraan')
            ->where('kode_perkiraan', $kode_perkiraan)
            ->select([DB::raw("HitungPerkSaldo(kode_perkiraan, '$tanggal', jurnal, level) AS saldo")])
            ->first();
        return response()->json($perkiraan->saldo);
    }

    function deleteTransaksiKas(Request $request)
    {
        $trans_id = $request->trans_id;

        DB::beginTransaction();
        try {
            DB::table('data_trans_akuntansi')->where('trans_id', $trans_id)->update(['status' => 9]);
            DB::table('data_trans_jurnal')->where('trans_id', $trans_id)->update(['status' => 9]);

            DB::commit();
            return response()->json([
                "success" => true,
                "message" => "Transaksi berhasil dibatalkan"
            ]);
        } catch (Exception $error) {
            DB::rollBack();
            return response()->json([
                "success" => false,
                "error" => $error->getMessage()
            ]);
        }
    }

    function validasiTransaksiKas(Request $request)
    {
        $trans_id = $request->trans_id;

        DB::beginTransaction();
        try {
            DB::table('data_trans_akuntansi')->where('trans_id', $trans_id)->update(['status' => 1]);
            DB::table('data_trans_jurnal')->where('trans_id', $trans_id)->update(['status' => 1]);

            DB::commit();
            return response()->json([
                "success" => true,
                "message" => "Transaksi berhasil divalidasi",
                "trans_id" => $trans_id
            ]);
        } catch (Exception $error) {
            DB::rollBack();
            return response()->json([
                "success" => false,
                "error" => $error->getMessage()
            ]);
        }
    }

    function deleteTransaksiNonKas(Request $request)
    {
        $trans_id = $request->trans_id;

        DB::beginTransaction();
        try {
            DB::table('data_trans_akuntansi_nonkas')->where('trans_id', $trans_id)->update(['status' => 9]);
            DB::table('data_trans_jurnal')->where('trans_id', $trans_id)->update(['status' => 9]);

            DB::commit();
            return response()->json([
                "success" => true,
                "message" => "Transaksi berhasil dibatalkan"
            ]);
        } catch (Exception $error) {
            DB::rollBack();
            return response()->json([
                "success" => false,
                "error" => $error->getMessage()
            ]);
        }
    }

    function validasiTransaksiNonKas(Request $request)
    {
        $trans_id = $request->trans_id;

        DB::beginTransaction();
        try {
            DB::table('data_trans_akuntansi_nonkas')->where('trans_id', $trans_id)->update(['status' => 1]);
            DB::table('data_trans_jurnal')->where('trans_id', $trans_id)->update(['status' => 1]);

            DB::commit();
            return response()->json([
                "success" => true,
                "message" => "Transaksi berhasil divalidasi"
            ]);
        } catch (Exception $error) {
            DB::rollBack();
            return response()->json([
                "success" => false,
                "error" => $error->getMessage()
            ]);
        }
    }
}
