155 lines
4.8 KiB
PHP
155 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Models\Supplier;
|
|
use App\Models\Produk;
|
|
use App\Models\Jurnal;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class SupplierController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$data = [
|
|
'subtitle' => 'Suppliers',
|
|
'button' => true,
|
|
'module' => [
|
|
'url' => route('suppliers.create'),
|
|
'name' => 'Tambah Baru'
|
|
]
|
|
];
|
|
|
|
$suppliers = Supplier::all();
|
|
return view('suppliers.index', compact('suppliers', 'data'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$data = [
|
|
'subtitle' => 'Suppliers',
|
|
'button' => true,
|
|
'module' => [
|
|
'url' => route('suppliers.create'),
|
|
'name' => 'Tambah Baru'
|
|
]
|
|
];
|
|
$products = Produk::all();
|
|
return view('suppliers.create', compact('data', 'products' ));
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'nama_supplier' => 'required',
|
|
'nama_produk' => 'required',
|
|
'jumlah' => 'required|integer',
|
|
'harga' => 'required|numeric',
|
|
'payment_method' => 'required|in:cash,hutang',
|
|
]);
|
|
if ($validator->fails()) {
|
|
return redirect()->back()->withInput()->with('error', 'Unexpected error, please try again. code: ' . $validator->errors()->first());
|
|
}
|
|
// Membuat entri supplier
|
|
$supplier = Supplier::create($request->all());
|
|
|
|
// Memperbarui stok produk
|
|
$produk = Produk::where('name', $request->nama_produk)->first();
|
|
|
|
if ($produk) {
|
|
$produk->stok += $request->jumlah;
|
|
$produk->save();
|
|
} else {
|
|
return redirect()->route('suppliers.index')->with('error', 'Produk tidak ditemukan');
|
|
}
|
|
|
|
// Mencatat transaksi di jurnal umum
|
|
$this->createJurnalEntry($request);
|
|
|
|
return redirect()->route('suppliers.index')->with('success', 'Supplier berhasil dibuat dan stok produk berhasil diperbarui.');
|
|
}
|
|
|
|
private function createJurnalEntry(Request $request)
|
|
{
|
|
$persediaanAkunId = 3; // Ganti dengan ID akun persediaan yang benar
|
|
$hutangUsahaAkunId = 14; // Ganti dengan ID akun hutang usaha yang benar
|
|
$kasAkunId = 1; // Ganti dengan ID akun kas/bank yang benar
|
|
|
|
$isCashPayment = $request->payment_method === 'cash'; // Periksa metode pembayaran
|
|
|
|
$reff = $this->generateRandomString();
|
|
|
|
// Mencatat debit ke akun persediaan
|
|
Jurnal::create([
|
|
'id_akun' => $persediaanAkunId,
|
|
'no_reff' => $reff,
|
|
'keterangan' => 'Pembelian persediaan dari supplier ' . $request->nama_supplier,
|
|
'waktu_transaksi' => now(),
|
|
'nominal' => $request->harga * $request->jumlah,
|
|
'tipe' => 'd' // 'd' untuk debit
|
|
]);
|
|
|
|
if ($isCashPayment) {
|
|
// Mencatat kredit ke akun kas untuk pembayaran tunai
|
|
Jurnal::create([
|
|
'id_akun' => $kasAkunId,
|
|
'no_reff' => $reff,
|
|
'keterangan' => 'Pembayaran tunai untuk pembelian persediaan dari supplier ' . $request->nama_supplier,
|
|
'waktu_transaksi' => now(),
|
|
'nominal' => $request->harga * $request->jumlah,
|
|
'tipe' => 'k' // 'k' untuk kredit
|
|
]);
|
|
} else {
|
|
// Mencatat kredit ke akun hutang usaha untuk pembayaran kredit
|
|
Jurnal::create([
|
|
'id_akun' => $hutangUsahaAkunId,
|
|
'no_reff' => $reff,
|
|
'keterangan' => 'Hutang untuk pembelian persediaan dari supplier ' . $request->nama_supplier,
|
|
'waktu_transaksi' => now(),
|
|
'nominal' => $request->harga * $request->jumlah,
|
|
'tipe' => 'k' // 'k' untuk kredit
|
|
]);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function show(Supplier $supplier)
|
|
{
|
|
return view('suppliers.show', compact('supplier'));
|
|
}
|
|
|
|
public function edit(Supplier $supplier)
|
|
{
|
|
return view('suppliers.edit', compact('supplier'));
|
|
}
|
|
|
|
public function update(Request $request, Supplier $supplier)
|
|
{
|
|
$request->validate([
|
|
'nama_supplier' => 'required',
|
|
'nama_produk' => 'required',
|
|
'jumlah' => 'required|integer',
|
|
'harga' => 'required|numeric',
|
|
]);
|
|
|
|
$supplier->update($request->all());
|
|
return redirect()->route('suppliers.index')->with('success', 'Supplier updated successfully.');
|
|
}
|
|
|
|
public function destroy(Supplier $supplier)
|
|
{
|
|
$supplier->delete();
|
|
return redirect()->route('suppliers.index')->with('success', 'Supplier deleted successfully.');
|
|
}
|
|
function generateRandomString($length = 10) {
|
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
$randomString = '';
|
|
for ($i = 0; $i < $length; $i++) {
|
|
$randomString .= $characters[rand(0, strlen($characters) - 1)];
|
|
}
|
|
return $randomString;
|
|
}
|
|
|
|
} |