<?php

namespace App\Http\Controllers;

use App\Models\Cashtransaction;
use App\Models\CreditCashTransaction;
use App\Models\Akun;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class CashDebtController extends Controller
{
    public function debtCashFrom(Request $request)
    {
        $request->validate([
            'from_id' => 'required',
            'total' => 'required',
            'staff' => 'required',
            'date' => 'required',
        ]);

        DB::beginTransaction();
        try {
            $cash = Cashtransaction::find($request->id);
            $cash->cashout = $cash->cashout + $request->total;
            $cash->save();

            $creditCash = new CreditCashTransaction;
            $creditCash->cashtransaction_id = $cash->id;
            $creditCash->from_id = $request->from_id;
            $creditCash->total = $request->total;
            $creditCash->staff = $request->staff;
            $creditCash->date = date("Y-m-d", strtotime($request->date));
            $creditCash->save();

            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }

        $response = [
            'success' => true,
            'cashtransaction' => $cash,
        ];

        return response($response, 200);
    }

    public function debtCashTo(Request $request)
    {
        $request->validate([
            'to_id' => 'required',
            'total' => 'required',
            'staff' => 'required',
            'date' => 'required',
        ]);

        DB::beginTransaction();
        try {
            $cash = Cashtransaction::find($request->id);
            $cash->cashin = $cash->cashin + $request->total;
            $cash->save();

            $creditCash = new CreditCashTransaction;
            $creditCash->cashtransaction_id = $cash->id;
            $creditCash->to_id = $request->to_id;
            $creditCash->total = $request->total;
            $creditCash->staff = $request->staff;
            $creditCash->date = date("Y-m-d", strtotime($request->date));
            $creditCash->save();

            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }

        $response = [
            'success' => true,
            'cashtransaction' => $cash,
        ];

        return response($response, 200);
    }

    public function pphCashTo(Request $request)
    {
        $request->validate([
            'pph23_amount' => 'nullable',
            'pph22_amount' => 'nullable',
            'pph4_2_amount' => 'nullable',
            'pph4_2_amount_v2' => 'nullable',
            'pph21_amount' => 'nullable',
            'date' => 'required',
            'staff' => 'required',
        ]);

        DB::beginTransaction();
        try {
            $cash = Cashtransaction::find($request->id);
            if (!empty($request->pph23_amount)) {
                $cash->pph23_amount += $request->pph23_amount;
            } elseif (!empty($request->pph22_amount)) {
                $cash->pph22_amount += $request->pph22_amount;
            } elseif (!empty($request->pph4_2_amount)) {
                $cash->pph4_2_amount += $request->pph4_2_amount;
            } elseif (!empty($request->pph4_2_amount_v2)) {
                $cash->pph4_2_amount_v2 += $request->pph4_2_amount_v2;
            } elseif (!empty($request->pph21_amount)) {
                $cash->pph21_amount += $request->pph21_amount;
            }
            $cash->save();

            $akunPPh23 = Akun::where('name', 'Piutang_PPh_Pasal_23')->first();
            $akunPPh22 = Akun::where('name', 'Uang_Muka_PPh_22')->first();
            $akunPPh4_2 = Akun::where('name', 'Biaya_PPh_4_2')->first();
            $akunPPh21 = Akun::where('name', 'Piutang_PPh_21')->first();
            $akunUtangPPh4_2 = Akun::where('name', 'Utang_PPh_Pasal_4_ayat_2')->first();

            if (!empty($request->pph4_2_amount_v2)) {
                $creditCash = new CreditCashTransaction;
                $creditCash->cashtransaction_id = $cash->id;
                $creditCash->to_id = $akunPPh4_2->id;
                $creditCash->total = $request->pph4_2_amount_v2;
                $creditCash->staff = $request->staff;
                $creditCash->date = date("Y-m-d", strtotime($request->date));
                $creditCash->save();

                $creditCash = new CreditCashTransaction;
                $creditCash->cashtransaction_id = $cash->id;
                $creditCash->to_id = $akunUtangPPh4_2->id;
                $creditCash->total = $request->pph4_2_amount_v2;
                $creditCash->staff = $request->staff;
                $creditCash->date = date("Y-m-d", strtotime($request->date));
                $creditCash->save();
            } else {
                $creditCash = new CreditCashTransaction;
                $creditCash->cashtransaction_id = $cash->id;

                if (!empty($request->pph23_amount)) {
                    $creditCash->to_id = $akunPPh23->id;
                    $creditCash->total = $request->pph23_amount;
                } elseif (!empty($request->pph22_amount)) {
                    $creditCash->to_id = $akunPPh22->id;
                    $creditCash->total = $request->pph22_amount;
                } elseif (!empty($request->pph4_2_amount)) {
                    $creditCash->to_id = $akunPPh4_2->id;
                    $creditCash->total = $request->pph4_2_amount;
                } elseif (!empty($request->pph21_amount)) {
                    $creditCash->to_id = $akunPPh21->id;
                    $creditCash->total = $request->pph21_amount;
                }

                $creditCash->staff = $request->staff;
                $creditCash->date = date("Y-m-d", strtotime($request->date));
                $creditCash->save();
            }

            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }
        $response = [
            'success' => true,
            'cashtransaction' => $creditCash,
        ];

        return response($response, 200);
    }

    public function pphCashFrom(Request $request)
    {
        $request->validate([
            'pph23_amount' => 'nullable',
            'pph22_amount' => 'nullable',
            'pph22_amount_v2' => 'nullable',
            'pph4_2_amount' => 'nullable',
            'pph21_amount' => 'nullable',
            'pph26_amount' => 'nullable',
            'date' => 'required',
            'staff' => 'required',
        ]);

        $hasAmount = !empty($request->pph23_amount)
            || !empty($request->pph22_amount)
            || !empty($request->pph22_amount_v2)
            || !empty($request->pph4_2_amount)
            || !empty($request->pph21_amount)
            || !empty($request->pph26_amount);

        if (!$hasAmount) {
            return response([
                'success' => false,
                'message' => 'Salah satu nominal PPh wajib diisi.',
            ], 422);
        }

        DB::beginTransaction();
        try {
            $cash = Cashtransaction::find($request->id);
            if (!empty($request->pph23_amount)) {
                $cash->pph23_amount += $request->pph23_amount;
            } elseif (!empty($request->pph22_amount)) {
                $cash->pph22_amount += $request->pph22_amount;
            } elseif (!empty($request->pph22_amount_v2)) {
                $cash->pph22_amount_v2 += $request->pph22_amount_v2;
            } elseif (!empty($request->pph4_2_amount)) {
                $cash->pph4_2_amount += $request->pph4_2_amount;
            } elseif (!empty($request->pph21_amount)) {
                $cash->pph21_amount += $request->pph21_amount;
            } elseif (!empty($request->pph26_amount)) {
                $cash->pph26_amount += $request->pph26_amount;
            }
            $cash->save();

            $akunUtangPPh23 = Akun::where('name', 'Utang_PPh_23')->first();
            $akunUtangPPh22 = Akun::where('name', 'Utang_PPh_22')->first();
            $akunUangMukaPPh22 = Akun::where('name', 'Uang_Muka_PPh_22')->first();
            $akunUtangPPh4_2 = Akun::where('name', 'Utang_PPh_Pasal_4_ayat_2')->first();
            $akunUtangPPh21 = Akun::where('name', 'Utang_PPh_21')->first();
            $akunUtangPPh26 = Akun::where('name', 'Utang_PPh_26')->first();

            $creditCash = new CreditCashTransaction;
            $creditCash->cashtransaction_id = $cash->id;

            if (!empty($request->pph23_amount)) {
                $creditCash->from_id = $akunUtangPPh23->id;
                $creditCash->total = $request->pph23_amount;
            } elseif (!empty($request->pph22_amount)) {
                $creditCash->from_id = $akunUtangPPh22->id;
                $creditCash->total = $request->pph22_amount;
            } elseif (!empty($request->pph22_amount_v2)) {
                $creditCash->from_id = $akunUangMukaPPh22->id;
                $creditCash->total = $request->pph22_amount_v2;
            } elseif (!empty($request->pph4_2_amount)) {
                $creditCash->from_id = $akunUtangPPh4_2->id;
                $creditCash->total = $request->pph4_2_amount;
            } elseif (!empty($request->pph21_amount)) {
                $creditCash->from_id = $akunUtangPPh21->id;
                $creditCash->total = $request->pph21_amount;
            } elseif (!empty($request->pph26_amount)) {
                $creditCash->from_id = $akunUtangPPh26->id;
                $creditCash->total = $request->pph26_amount;
            }

            $creditCash->staff = $request->staff;
            $creditCash->date = date("Y-m-d", strtotime($request->date));
            $creditCash->save();

            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }
        $response = [
            'success' => true,
            'cashtransaction' => $creditCash,
        ];

        return response($response, 200);
    }

    public function deleteCreditCashTransaction(Request $request)
    {

        DB::beginTransaction();
        try {
            $creditCash = CreditCashTransaction::find($request->id);
            $cash = Cashtransaction::find($creditCash->cashtransaction_id);

            $akunPPh23 = Akun::where('name', 'Piutang_PPh_Pasal_23')->first();
            $akunPPh22 = Akun::where('name', 'Uang_Muka_PPh_22')->first();
            $akunPPh4_2 = Akun::where('name', 'Biaya_PPh_4_2')->first();
            $akunPPh21 = Akun::where('name', 'Piutang_PPh_21')->first();

            $akunUtangPPh23 = Akun::where('name', 'Utang_PPh_23')->first();
            $akunUtangPPh22 = Akun::where('name', 'Utang_PPh_22')->first();
            $akunUtangPPh4_2 = Akun::where('name', 'Utang_PPh_Pasal_4_ayat_2')->first();
            $akunUtangPPh21 = Akun::where('name', 'Utang_PPh_21')->first();
            $akunUtangPPh26 = Akun::where('name', 'Utang_PPh_26')->first();

            if ($creditCash->to_id == $akunPPh23->id) {
                $cash->pph23_amount -= $creditCash->total;
                $cash->save();
            } elseif ($creditCash->to_id == $akunPPh22->id) {
                $cash->pph22_amount -= $creditCash->total;
                $cash->save();
            } elseif ($creditCash->to_id == $akunPPh4_2->id) {
                if ($cash->pph4_2_amount_v2 < 1) {
                    $cash->pph4_2_amount -= $creditCash->total;
                    $cash->save();
                }
            } elseif ($creditCash->to_id == $akunUtangPPh4_2->id) {
                $cash->pph4_2_amount_v2 -= $creditCash->total;
                $cash->save();
            } elseif ($creditCash->to_id == $akunPPh21->id) {
                $cash->pph21_amount -= $creditCash->total;
                $cash->save();
            } elseif ($creditCash->from_id == $akunPPh22->id) {
                $cash->pph22_amount_v2 -= $creditCash->total;
                $cash->save();
            } elseif ($creditCash->from_id == $akunUtangPPh23->id) {
                $cash->pph23_amount -= $creditCash->total;
                $cash->save();
            } elseif ($creditCash->from_id == $akunUtangPPh22->id) {
                $cash->pph22_amount -= $creditCash->total;
                $cash->save();
            } elseif ($creditCash->from_id == $akunUtangPPh4_2->id) {
                $cash->pph4_2_amount_v2 -= $creditCash->total;
                $cash->save();
            } elseif ($creditCash->from_id == $akunUtangPPh21->id) {
                $cash->pph21_amount -= $creditCash->total;
                $cash->save();
            } elseif ($creditCash->from_id == $akunUtangPPh26->id) {
                $cash->pph26_amount -= $creditCash->total;
                $cash->save();
            } else {
                if ($cash->from) { // Cashout
                    $cash = Cashtransaction::find($cash->id);
                    $cash->cashout = $cash->cashout - $creditCash->total;
                    $cash->save();
                } elseif ($cash->to) { // Cashin
                    $cash = Cashtransaction::find($cash->id);
                    $cash->cashin = $cash->cashin - $creditCash->total;
                    $cash->save();
                }
            }

            $creditCash->delete();

            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }

        $response = [
            'success' => true,
            'cashtransaction' => $cash,
        ];

        return response($response, 200);
    }

    public function updateCreditCashTransaction(Request $request, $id)
    {
        $request->validate([
            'from_id' => 'nullable',
            'to_id' => 'nullable',
            'total' => 'required',
            'staff' => 'required',
            'date' => 'required',
        ]);

        DB::beginTransaction();
        try {
            $akunPPh23 = Akun::where('name', 'Piutang_PPh_Pasal_23')->first();
            $akunPPh22 = Akun::where('name', 'Uang_Muka_PPh_22')->first();
            $akunPPh4_2 = Akun::where('name', 'Biaya_PPh_4_2')->first();
            $akunPPh21 = Akun::where('name', 'Piutang_PPh_21')->first();

            $akunUtangPPh23 = Akun::where('name', 'Utang_PPh_23')->first();
            $akunUtangPPh22 = Akun::where('name', 'Utang_PPh_22')->first();
            $akunUtangPPh4_2 = Akun::where('name', 'Utang_PPh_Pasal_4_ayat_2')->first();
            $akunUtangPPh21 = Akun::where('name', 'Utang_PPh_21')->first();
            $akunUtangPPh26 = Akun::where('name', 'Utang_PPh_26')->first();

            $creditCash = CreditCashTransaction::find($id);
            $cash = Cashtransaction::find($creditCash->cashtransaction_id);

            if ($creditCash->to_id == $akunPPh23->id) {
                $cash->pph23_amount -= $creditCash->total;
                $cash->save();

                $creditCash->total = $request->total;
                $creditCash->to_id = $request->to_id;
                $creditCash->from_id = $request->from_id;
                $creditCash->date = date("Y-m-d", strtotime($request->date));
                $creditCash->save();

                $cash->pph23_amount += $creditCash->total;
                $cash->save();
            } elseif ($creditCash->to_id == $akunPPh22->id) {
                $cash->pph22_amount -= $creditCash->total;
                $cash->save();

                $creditCash->total = $request->total;
                $creditCash->to_id = $request->to_id;
                $creditCash->from_id = $request->from_id;
                $creditCash->date = date("Y-m-d", strtotime($request->date));
                $creditCash->save();

                $cash->pph22_amount += $creditCash->total;
                $cash->save();
            } elseif ($creditCash->to_id == $akunPPh4_2->id) {
                $cash->pph4_2_amount -= $creditCash->total;
                $cash->save();

                $creditCash->total = $request->total;
                $creditCash->to_id = $request->to_id;
                $creditCash->from_id = $request->from_id;
                $creditCash->date = date("Y-m-d", strtotime($request->date));
                $creditCash->save();

                $cash->pph4_2_amount += $creditCash->total;
                $cash->save();
            } elseif ($creditCash->to_id == $akunPPh21->id) {
                $cash->pph21_amount -= $creditCash->total;
                $cash->save();

                $creditCash->total = $request->total;
                $creditCash->to_id = $request->to_id;
                $creditCash->from_id = $request->from_id;
                $creditCash->date = date("Y-m-d", strtotime($request->date));
                $creditCash->save();

                $cash->pph21_amount += $creditCash->total;
                $cash->save();
            } elseif ($creditCash->from_id == $akunUtangPPh23->id) {
                $cash->pph23_amount -= $creditCash->total;
                $cash->save();

                $creditCash->total = $request->total;
                $creditCash->to_id = $request->to_id;
                $creditCash->from_id = $request->from_id;
                $creditCash->date = date("Y-m-d", strtotime($request->date));
                $creditCash->save();

                $cash->pph23_amount += $creditCash->total;
                $cash->save();
            } elseif ($creditCash->from_id == $akunPPh22->id) {
                $cash->pph22_amount_v2 -= $creditCash->total;
                $cash->save();

                $creditCash->total = $request->total;
                $creditCash->to_id = $request->to_id;
                $creditCash->from_id = $request->from_id;
                $creditCash->date = date("Y-m-d", strtotime($request->date));
                $creditCash->save();

                $cash->pph22_amount_v2 += $creditCash->total;
                $cash->save();
            } elseif ($creditCash->from_id == $akunUtangPPh22->id) {
                $cash->pph22_amount -= $creditCash->total;
                $cash->save();

                $creditCash->total = $request->total;
                $creditCash->to_id = $request->to_id;
                $creditCash->from_id = $request->from_id;
                $creditCash->date = date("Y-m-d", strtotime($request->date));
                $creditCash->save();

                $cash->pph22_amount += $creditCash->total;
                $cash->save();
            } elseif ($creditCash->from_id == $akunUtangPPh4_2->id) {
                $cash->pph4_2_amount -= $creditCash->total;
                $cash->save();

                $creditCash->total = $request->total;
                $creditCash->to_id = $request->to_id;
                $creditCash->from_id = $request->from_id;
                $creditCash->date = date("Y-m-d", strtotime($request->date));
                $creditCash->save();

                $cash->pph4_2_amount += $creditCash->total;
                $cash->save();
            } elseif ($creditCash->from_id == $akunUtangPPh21->id) {
                $cash->pph21_amount -= $creditCash->total;
                $cash->save();

                $creditCash->total = $request->total;
                $creditCash->to_id = $request->to_id;
                $creditCash->from_id = $request->from_id;
                $creditCash->date = date("Y-m-d", strtotime($request->date));
                $creditCash->save();

                $cash->pph21_amount += $creditCash->total;
                $cash->save();
            } elseif ($creditCash->from_id == $akunUtangPPh26->id) {
                $cash->pph26_amount -= $creditCash->total;
                $cash->save();

                $creditCash->total = $request->total;
                $creditCash->to_id = $request->to_id;
                $creditCash->from_id = $request->from_id;
                $creditCash->date = date("Y-m-d", strtotime($request->date));
                $creditCash->save();

                $cash->pph26_amount += $creditCash->total;
                $cash->save();
            } else {
                if ($cash->from) { // Cashout
                    $cash->cashout = $cash->cashout - $creditCash->total;
                    $cash->save();

                    $creditCash->total = $request->total;
                    $creditCash->from_id = $request->from_id;
                    $creditCash->staff = $request->staff;
                    $creditCash->date = date("Y-m-d", strtotime($request->date));
                    $creditCash->save();

                    $cash->cashout = $cash->cashout + $creditCash->total;
                    $cash->save();
                } elseif ($cash->to) { // Cashin
                    $cash->cashin = $cash->cashin - $creditCash->total;
                    $cash->save();

                    $creditCash->total = $request->total;
                    $creditCash->to_id = $request->to_id;
                    $creditCash->staff = $request->staff;
                    $creditCash->date = date("Y-m-d", strtotime($request->date));
                    $creditCash->save();

                    $cash->cashin = $cash->cashin + $creditCash->total;
                    $cash->save();
                }
            }
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }

        $response = [
            'success' => true,
            'creditcash' => $creditCash,
            'cashtransaction' => $cash
        ];

        return response($response, 200);
    }
}
