<?php
namespace App\Http\Controllers;
use App\Helpers\Helper;
use App\Models\Penjualan;
use App\Models\PembayaranPiutang;
use DateInterval;
use DatePeriod;
use DateTime;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class PembayaranPiutangController 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 = Penjualan::whereBetween("created_at", [$begin->format('Y-m-d'), $end->format('Y-m-d')])->where('status_pembayaran', 'belum lunas')->filterToko()->latest()->get();
$data = [
'datas' => $datas,
'periode' => $periode,
'periodeTanggals' => $periodeTanggals
];
return response()->view('pembayaran_piutang.index', $data);
}
/**
* Show the form for creating a new resource.
*/
public function create(): Response
{
return response()->view('pembayaran_piutang.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 = Penjualan::find($id);
$pembayaran_piutangs = PembayaranPiutang::where("penjualan_id", $id)->get();
$data = [
"datas" => $datas,
"pembayaran_piutangs" => $pembayaran_piutangs,
];
return response()->view("pembayaran_piutang.edit", $data);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id): RedirectResponse
{
DB::beginTransaction();
try {
$uang_bayar = str_replace(
"Rp ",
"",
str_replace(".", "", $request->uang_bayar)
);
$uang_kembali = str_replace(".", "", $request->kembali);
PembayaranPiutang::create([
"uang_bayar" => $uang_bayar,
"penjualan_id" => $id,
]);
$penjualan = Penjualan::find($id);
if ($penjualan->uang_masuk + $uang_bayar < $penjualan->total) {
$penjualan->update([
'uang_bayar' => $penjualan->uang_bayar + $uang_bayar,
'uang_masuk' => $penjualan->uang_masuk + $uang_bayar,
'status_pembayaran' => 'belum lunas',
]);
} else {
$penjualan->update([
'uang_bayar' => $penjualan->uang_bayar + $uang_bayar,
'uang_masuk' => $penjualan->uang_bayar - $uang_kembali + $uang_bayar,
'status_pembayaran' => 'lunas',
'jam_selesai' => now(),
]);
}
Helper::addUserLog('Melakukan Pembayaran Piutang Untuk Penjualan '.$penjualan->nomor_nota.' Atas Nama Pelanggan ' . $penjualan->pelanggan->nama_pelanggan, $penjualan->toArray());
DB::commit();
return redirect()->route('penjualan.index')->with('success', 'Pembayaran Piutang berhasil ditambah');
} catch (\Throwable $th) {
DB::rollback();
Log::error($th);
return redirect()->back()->with('error', 'Pembayaran Piutang 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]