<?php
namespace App\Http\Controllers;
use App\Helpers\Helper;
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')])->filterToko()->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()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*/
public function show(string $id) {}
/**
* 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 {
$pembelian = Pembelian::findOrFail($id);
PembayaranHutang::create([
"uang_bayar" => $request->uang_bayar,
"pembelian_id" => $id,
]);
Helper::addUserLog('Melakukan Pembayaran Hutang Untuk Pembelian '.$pembelian->nomor_nota, $pembelian->toArray());
DB::commit();
return redirect()->route('pembayaran_hutang.index')->with('success', 'Pembayaran Hutang berhasil ditambah');
} catch (\Throwable $th) {
DB::rollback();
return redirect()->back()->with('error', 'Pembayaran Hutang gagal ditambah')->withInput($request->all());
}
}
public function lunas(Request $request)
{
DB::beginTransaction();
try {
if (isset($request->pilih)) {
$pilih = count($request->pilih);
for ($i = 0; $i < $pilih; $i++) {
$pembelian = Pembelian::find($request->pilih[$i]);
PembayaranHutang::create([
"uang_bayar" => $pembelian->total,
"pembelian_id" => $request->pilih[$i],
]);
Helper::addUserLog('Melakukan Pelunasan Hutang Untuk Pembelian '.$pembelian->nomor_nota, $pembelian->toArray());
}
DB::commit();
return redirect()->route('pembayaran_hutang.index')->with('success', 'Pelunasan Hutang berhasil ditambah');
} else {
DB::rollback();
return redirect()->back()->with('error', 'Pilih transaksi terlebih dahulu!')->withInput($request->all());
}
} catch (\Throwable $th) {
DB::rollback();
return redirect()->back()->with('error', 'Pembayaran Hutang gagal ditambah')->withInput($request->all());
}
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}
Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]