Anons79 Mini Shell

Directory : /home/aplikasiposinfo/www/airafone.fixmate.id/app/Http/Controllers/
Upload File :
Current File : /home/aplikasiposinfo/www/airafone.fixmate.id/app/Http/Controllers/PembayaranHutangController.php

<?php

namespace App\Http\Controllers;

use App\Models\Pembelian;
use App\Models\PembayaranHutang;
use DateInterval;
use DatePeriod;
use DateTime;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;

class PembayaranHutangController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(): Response
    {

        $begin = new DateTime('-1 month');
        $end = new DateTime();
        $periode = [
            $begin->format('d/m/Y'),
            $end->format('d/m/Y'),
        ];

        if (request()->periode) {
            $periode = explode(" - ", request()->periode);
            $begin = DateTime::createFromFormat('d/m/Y', $periode[0]);
            $end = DateTime::createFromFormat('d/m/Y', $periode[1]);
        }
        $end->modify('+1 day');

        $interval = new DateInterval('P1D');
        $daterange = new DatePeriod($begin, $interval, $end);
        $periodeTanggals = [];
        foreach ($daterange as $date) {

            $periodeTanggals[] = strftime("%d-%b", strtotime($date->format("Y-m-d")));
        }


        $datas = Pembelian::where('metode_bayar', "kredit")->whereBetween("created_at", [$begin->format('Y-m-d'), $end->format('Y-m-d')])->latest()->get();
        $data = [
            "datas" => $datas,
            "periode" => $periode,
            "periodeTanggals" => $periodeTanggals
        ];

        return response()->view('pembayaran_hutang.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create(): Response
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request): RedirectResponse
    {
        //
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id): Response {}

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id): Response
    {

        $datas = Pembelian::find($id);
        $pembayaran_hutangs = PembayaranHutang::where("pembelian_id", $id)->get();

        $data = [
            "datas" => $datas,
            "pembayaran_hutangs" => $pembayaran_hutangs,
        ];

        return response()->view("pembayaran_hutang.edit", $data);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id): RedirectResponse
    {
        DB::beginTransaction();
        try {
            PembayaranHutang::create([
                "uang_bayar" => $request->uang_bayar,
                "pembelian_id" => $id,
            ]);

            DB::commit();
            return redirect()->route('pembayaran_hutang.index')->with('success', 'Pembayaran Hutang berhasil ditambah');
        } catch (\Throwable $th) {
            DB::rollback();
            return redirect()->route('pembayaran_hutang.index')->with('error', 'Pembayaran Hutang gagal ditambah');
        }
    }

    public function lunas(Request $request)
    {
        DB::beginTransaction();
        try {

            if (isset($request->pilih)) {
                $pilih = count($request->pilih);

                for ($i = 0; $i < $pilih; $i++) {
                    $datas = Pembelian::find($request->pilih[$i]);
                    PembayaranHutang::create([
                        "uang_bayar" => $datas->total,
                        "pembelian_id" => $request->pilih[$i],
                    ]);
                }

                DB::commit();
                return redirect()->route('pembayaran_hutang.index')->with('success', 'Pelunasan Hutang berhasil ditambah');
            } else {
                DB::rollback();
                return redirect()->route('pembayaran_hutang.index')->with('error', 'Pilih transaksi terlebih dahulu!');
            }
        } catch (\Throwable $th) {
            DB::rollback();
            return redirect()->route('pembayaran_hutang.index')->with('error', 'Pembayaran Hutang gagal ditambah');
        }
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id): RedirectResponse
    {
        //
    }
}

Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]