<?php
namespace App\Http\Controllers;
use App\Helpers\Helper;
use App\Models\DetailGaransi;
use App\Models\Pembelian;
use App\Models\DetailPembelian;
use App\Models\Garansi;
use App\Models\Supplier;
use App\Models\Item;
use App\Models\Toko;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use DateInterval;
use DatePeriod;
use DateTime;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;
class PembelianController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(): Response
{
// this will default to a time of 00:00:00
$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::with('supplier', 'tokos')->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('pembelian.index', $data);
}
/**
* Show the form for creating a new resource.
*/
public function create(): Response
{
$suppliers = Supplier::all();
$tokos = auth()->user()->role->tokos;
$datas = [
"suppliers" => $suppliers,
'detail' => false,
'tokos' => $tokos
];
return response()->view('pembelian.create', $datas);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request): RedirectResponse
{
$request->validate([
'supplier_id' => 'required|exists:suppliers,id',
'no_pembelian' => 'required|string|max:255',
'metode_bayar' => 'required',
'uang_bayar' => 'required|numeric|min:0',
'keterangan' => 'nullable|string',
'bukti_nota' => 'nullable|file|mimes:jpg,jpeg,png,pdf,tiff,webp',
'toko_id' => 'required|exists:tokos,id',
]);
DB::beginTransaction();
try {
$uang_kembali = (int) str_replace(".", "", $request->kembali);
if ($request->uang_bayar > $request->grand_total) {
$uang_masuk = $request->uang_bayar - $uang_kembali;
} else {
$uang_masuk = $request->uang_bayar;
}
$pembelian = [
"supplier_id" => $request->supplier_id,
"tanggal_pembelian" => $request->tanggal,
"nomor_nota" => $request->no_pembelian,
"metode_bayar" => $request->metode_bayar,
"uang_bayar" => $request->uang_bayar,
"uang_awal" => $request->uang_bayar,
"uang_masuk" => $uang_masuk,
"pengguna_id" => auth()->user()->id,
'keterangan' => $request->keterangan,
];
if ($request->uang_bayar < $request->grand_total) {
$pembelian['status_pembayaran'] = 'belum lunas';
} else {
$pembelian['status_pembayaran'] = 'lunas';
}
if ($request->metode_bayar == "saldo") {
$pembelian['uang_bayar_saldo'] = $request->uang_bayar;
$supplier = Supplier::with('pembelian.detail_pembelian', 'retur_pembelian.detail')->find($request->supplier_id);
if ($supplier->saldo < $request->uang_bayar) {
return redirect()->back()->with('error', 'Saldo supplier tidak cukup')->withInput($request->all());
}
}
if ($request->hasFile('bukti_nota')) {
$nota_file = $request->file('bukti_nota');
$nota_ekstensi = $nota_file->extension();
$nota_baru = "Nota-" . date('ymdhis') . ".$nota_ekstensi";
$nota_file->move(public_path('images/Bukti_nota'), $nota_baru);
$pembelian['bukti_nota'] = $nota_baru;
}
$pembelian = Pembelian::create($pembelian);
$toko = Toko::find($request->toko_id);
$pembelian->tokos()->sync($toko->id);
$pembelian_id = Pembelian::latest()->first();
if (isset($request->jumlah)) {
for ($i = 0; $i < count($request->jumlah); $i++) {
$item = Item::with('stok_item_tokos')->find($request->id[$i]);
DetailPembelian::create([
"pembelian_id" => $pembelian_id->id,
"item_id" => $request->id[$i],
"qty" => $request->jumlah[$i],
"diskon" => 0,
"harga_item" => $request->biaya_item[$i],
"lokasi" => (@$request->lokasi[$i] ?? 1)
]);
$item->update([
"stok" => $item->stok + $request->jumlah[$i],
"biaya_item" => $request->biaya_item[$i],
]);
$item->stok_item_tokos()->updateOrCreate(['toko_id' => $toko->id], [
"stok" => (@$item->stok_item_tokos->where("toko_id", $toko->id)->first()->stok ?? 0) + $request->jumlah[$i],
]);
// if ((@$request->lokasi[$i] ?? 1) == 1) {
// DetailPembelian::create([
// "pembelian_id" => $pembelian_id->id,
// "item_id" => $request->id[$i],
// "qty" => $request->jumlah[$i],
// "diskon" => 0,
// "harga_item" => $request->biaya_item[$i],
// "lokasi" => (@$request->lokasi[$i] ?? 1)
// ]);
// $item->update([
// "stok" => $item->stok + $request->jumlah[$i],
// "biaya_item" => $request->biaya_item[$i],
// ]);
// $item->stok_item_tokos()->updateOrCreate(['toko_id'=>$toko->id],[
// "stok" => $item->stok_item_tokos->where("toko_id", $toko->id)->first()->stok + $request->jumlah[$i],
// ]);
// } else {
// DetailPembelian::create([
// "pembelian_id" => $pembelian_id->id,
// "item_id" => $request->id[$i],
// "qty" => $request->jumlah[$i],
// "diskon" => 0,
// "harga_item" => $request->biaya_item[$i],
// "lokasi" => (@$request->lokasi[$i] ?? 1)
// ]);
// $item->update([
// "stok_gudang" => $item->stok_gudang + $request->jumlah[$i],
// "biaya_item" => $request->biaya_item[$i],
// ]);
// }
}
}
Helper::addUserLog('Menambah pembelian : ' . $pembelian->nomor_nota, $pembelian->toArray());
DB::commit();
return redirect(route("pembelian.index"))->with('success', 'Pembelian berhasil ditambahkan');
} catch (\Throwable $th) {
DB::rollback();
Log::error($th);
return redirect()->back()->with('error', $th->getMessage())->withInput($request->all());
}
}
/**
* Display the specified resource.
*/
public function show(string $id): Response
{
$datas = Pembelian::with('tokos', 'detail_pembelian')->find($id);
$suppliers = Supplier::all();
$data = [
'datas' => $datas,
'suppliers' => $suppliers,
'detail' => true
];
return response()->view('pembelian.detail', $data);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id): Response
{
$datas = Pembelian::with('tokos', 'detail_pembelian')->find($id);
$suppliers = Supplier::all();
$data = [
'datas' => $datas,
'suppliers' => $suppliers,
'detail' => false,
'tokos' => auth()->user()->role->tokos
];
return response()->view('pembelian.edit', $data);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id): RedirectResponse
{
$request->validate([
'supplier_id' => 'required|exists:suppliers,id',
'no_pembelian' => 'required|string|max:255',
'metode_bayar' => 'required',
'uang_bayar' => 'required|numeric|min:0',
'keterangan' => 'nullable|string',
'bukti_nota' => 'nullable|file|mimes:jpg,jpeg,png,pdf,tiff,webp',
'toko_id' => 'required|exists:tokos,id',
]);
$pembelian = Pembelian::findOrFail($id);
DB::beginTransaction();
try {
$data = [
"supplier_id" => $request->supplier_id,
"nomor_nota" => $request->no_pembelian,
"metode_bayar" => $request->metode_bayar,
"uang_bayar" => $request->uang_bayar,
"pengguna_id" => auth()->user()->id,
'keterangan' => $request->keterangan
];
if ($request->uang_bayar < $request->grand_total) {
$data['status_pembayaran'] = 'belum lunas';
} else {
$data['status_pembayaran'] = 'lunas';
}
if ($request->metode_bayar == "saldo") {
$data['uang_bayar_saldo'] = $request->uang_bayar;
$supplier = Supplier::with('pembelian.detail_pembelian', 'retur_pembelian.detail')->find($request->supplier_id);
if ($supplier->saldo < $request->uang_bayar) {
return redirect()->back()->with('error', 'Saldo supplier tidak cukup')->withInput($request->all());
}
}
if ($request->hasFile('bukti_nota')) {
$nota_file = $request->file('bukti_nota');
$nota_ekstensi = $nota_file->extension();
$nota_baru = "Nota-" . date('ymdhis') . ".$nota_ekstensi";
$nota_file->move(public_path('images/Bukti_nota'), $nota_baru);
$data['bukti_nota'] = $nota_baru;
$nota_lama = $pembelian->bukti_nota;
File::delete(public_path('images/Bukti_nota') . "/" . $nota_lama);
}
$pembelian->update($data);
$pembelian->tokos()->sync($request->toko_id);
if (isset($request->jumlah)) {
foreach ($pembelian->detail_pembelian as $detail) {
$item_old = Item::with('stok_item_tokos')->find($detail->item_id);
if (isset($item_old)) {
$stokField = ($detail->lokasi == 1) ? 'stok' : 'stok_gudang';
$item_old->update([
$stokField => $item_old->$stokField - $detail->qty
]);
if ($stokField == 'stok') {
$item_old->stok_item_tokos()->updateOrCreate(['toko_id' => $request->toko_id], [
"stok" => (@$item_old->stok_item_tokos->where("toko_id", $request->toko_id)->first()->stok ?? 0) - $detail->qty,
]);
}
}
}
$old_detail = DetailPembelian::where("pembelian_id", $pembelian->id);
$old_detail->delete();
for ($i = 0; $i < count($request->jumlah); $i++) {
$item = Item::find($request->id[$i]);
$stokField = ((@$request->lokasi[$i] ?? 1) == 1) ? 'stok' : 'stok_gudang';
if (isset($item_old)) {
DetailPembelian::create([
"pembelian_id" => $pembelian->id,
"item_id" => $request->id[$i],
"qty" => $request->jumlah[$i],
"diskon" => 0,
"harga_item" => $request->biaya_item[$i],
"lokasi" => (@$request->lokasi[$i] ?? 1)
]);
$item->update([
$stokField => $item->$stokField + $request->jumlah[$i],
"biaya_item" => $request->biaya_item[$i]
]);
if ($stokField == 'stok') {
$item->stok_item_tokos()->updateOrCreate(['toko_id' => $request->toko_id], [
"stok" => (@$item->stok_item_tokos->where("toko_id", $request->toko_id)->first()->stok ?? 0) + $request->jumlah[$i],
]);
}
} else {
return redirect()->back()->with('error', 'Terdapat barang yang tidak tercatat di Master Data Barang!');
}
}
}
Helper::addUserLog('Mengubah pembelian : ' . $pembelian->nomor_nota, $pembelian->toArray());
DB::commit();
return redirect()->route('pembelian.index')->with('success', 'Pembelian berhasil diedit');
} catch (\Throwable $th) {
//throw $th;
DB::rollBack();
Log::error($th);
return redirect()->back()->with('error', 'Pembelian gagal diedit')->withInput($request->all());
}
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id): RedirectResponse
{
DB::beginTransaction();
try {
$data = Pembelian::with('tokos')->find($id);
$detail = DetailPembelian::where("pembelian_id", $data->id);
File::delete(public_path('images/Bukti_nota') . "/" . $data->bukti_nota);
foreach ($detail->get() as $detail) {
$item_old = Item::with('stok_item_tokos')->find($detail->item_id);
if (isset($item_old)) {
$stokField = ($detail->lokasi == 1) ? 'stok' : 'stok_gudang';
$item_old->update([
$stokField => $item_old->$stokField - $detail->qty
]);
if ($stokField == 'stok') {
$item_old->stok_item_tokos()->updateOrCreate(['toko_id'=>$data->tokos->first()->id], [
"stok" => $item_old->stok_item_tokos->where("toko_id", $data->tokos[0]->id)->first()->stok - $detail->qty,
]);
}
}
}
$data->tokos()->detach();
$detail->delete();
$data->delete();
Helper::addUserLog('Menghapus pembelian : ' . $data->nomor_nota, $data->toArray());
DB::commit();
return redirect()->route('pembelian.index')->with('success', 'Pembelian berhasil dihapus');
} catch (\Throwable $th) {
DB::rollback();
Log::error($th);
return redirect()->route('pembelian.index')->with('error', 'Pembelian gagal dihapus');
}
}
public function multiDelete(Request $request): RedirectResponse
{
if ($request->data_id == "" || $request->data_id == null) {
return redirect()->back()->with('error', 'Silahkan pilih data yang ingin dihapus');
}
DB::beginTransaction();
try {
$data_ids = explode(",", $request->data_id);
foreach ($data_ids as $id) {
$data = Pembelian::with('tokos')->find($id);
$detail = DetailPembelian::where("pembelian_id", $data->id);
File::delete(public_path('images/Bukti_nota') . "/" . $data->bukti_nota);
foreach ($detail->get() as $detail) {
$item_old = Item::with('stok_item_tokos')->find($detail->item_id);
if (isset($item_old)) {
$stokField = ($detail->lokasi == 1) ? 'stok' : 'stok_gudang';
$item_old->update([
$stokField => $item_old->$stokField - $detail->qty
]);
if ($stokField == 'stok') {
$item_old->stok_item_tokos()->updateOrCreate(['toko_id'=>$data->tokos->first()->id], [
"stok" => $item_old->stok_item_tokos->where("toko_id", $data->tokos[0]->id)->first()->stok - $detail->qty,
]);
}
}
}
$data->tokos()->detach();
$detail->delete();
$data->delete();
}
Helper::addUserLog('Menghapus ' . count($data_ids) . ' Pembelian');
DB::commit();
return redirect()->route('pembelian.index')->with('success', count($data_ids) . ' Pembelian berhasil dihapus');
} catch (\Throwable $th) {
DB::rollback();
Log::error($th);
return redirect()->back()->with('error', 'Pembelian gagal dihapus');
}
}
}
Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]