Skip to content

Commit

Permalink
Menambah CRUD Pembayaran
Browse files Browse the repository at this point in the history
  • Loading branch information
itsfaqih committed Jul 1, 2020
1 parent f566373 commit 3425f31
Show file tree
Hide file tree
Showing 8 changed files with 583 additions and 1 deletion.
103 changes: 103 additions & 0 deletions app/Http/Controllers/PaymentsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace App\Http\Controllers;

use App\Invoice;
use Inertia\Inertia;
use App\Payment;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Redirect;

class PaymentsController extends Controller
{
public function index()
{
return Inertia::render('Payments/Index', [
'filters' => Request::all('search', 'trashed'),
'payments' => Payment::orderBy('id', 'desc')
->filter(Request::only('search', 'trashed'))
->paginate()
->transform(function ($payment) {
return [
'id' => $payment->id,
'description' => $payment->description,
'amount' => $payment->amount,
'invoice_id' => $payment->invoice_id,
'invoice_date' => $payment->invoice->created_at->format('d F Y'),
'created_at' => $payment->created_at->format('d F Y'),
'deleted_at' => $payment->deleted_at,
];
}),
// ->only('id', 'room_id', 'renter_id', 'start_at', 'end_at', 'deleted_at')
]);
}

public function create()
{
return Inertia::render('Payments/Create', [
'invoices' => Invoice::all(),
]);
}

public function store()
{
Payment::create(
Request::validate([
'description' => ['required'],
'amount' => ['required', 'numeric'],
'invoice_id' => ['required', 'exists:invoices,id'],
])
);

return Redirect::route('payments.index')->with('success', 'Data Pembayaran berhasil ditambahkan.');
}

public function edit(Payment $payment)
{
return Inertia::render('Payments/Edit', [
'payment' => [
'id' => $payment->id,
'description' => $payment->description,
'amount' => $payment->amount,
'invoice_id' => $payment->invoice_id,
'invoice' => $payment->invoice,
'created_at' => $payment->created_at->format('Y-m-d'),
'deleted_at' => $payment->deleted_at
],
'invoices' => Invoice::all()->transform(function ($invoice) {
return [
'id' => $invoice->id,
'created_at' => $invoice->created_at->format('d F Y'),
];
}),
]);
}

public function update(Payment $payment)
{
$payment->update(
Request::validate([
'description' => ['required'],
'amount' => ['required', 'numeric'],
'invoice_id' => ['required', 'exists:invoices,id'],
])
);

return Redirect::back()->with('success', 'Data Pembayaran berhasil diperbarui.');
}

public function destroy(Payment $payment)
{
$payment->delete();

return Redirect::back()->with('success', 'Data Pembayaran berhasil dihapus.');
}

public function restore(Payment $payment)
{
$payment->restore();

return Redirect::back()->with('success', 'Data Pembayaran berhasil dipulihkan.');
}
}
30 changes: 30 additions & 0 deletions app/Payment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\SoftDeletes;

class Payment extends Model
{
use SoftDeletes;

public function invoice()
{
return $this->belongsTo(Invoice::class);
}

public function scopeFilter($query, array $filters)
{
$query->when($filters['search'] ?? null, function ($query, $search) {
$query->where('invoice_id', 'like', "%$search$");
$query->orWhere('description', 'like', "%$search$");
$query->orWhere('amount', 'like', "%$search$");
})->when($filters['trashed'] ?? null, function ($query, $trashed) {
if ($trashed === 'with') {
$query->withTrashed();
} elseif ($trashed === 'only') {
$query->onlyTrashed();
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function up()
$table->foreignId('invoice_id')->constrained()->onDelete('cascade');
$table->text('description');
$table->integer('amount');
$table->softDeletes();
$table->timestamps();
});
}
Expand Down
100 changes: 100 additions & 0 deletions resources/js/Pages/Payments/Create.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import React, { useState } from 'react';
import Helmet from 'react-helmet';
import { Inertia } from '@inertiajs/inertia';
import { InertiaLink, usePage } from '@inertiajs/inertia-react';
import Layout from '@/Shared/Layout';
import LoadingButton from '@/Shared/LoadingButton';
import TextInput from '@/Shared/TextInput';
import SelectInput from '@/Shared/SelectInput';

export default () => {
const { errors, invoices } = usePage();
const [sending, setSending] = useState(false);

const [values, setValues] = useState({
description: '',
amount: '',
invoice_id: ''
});

function handleChange(e) {
const key = e.target.name;
const value = e.target.value;
setValues(values => ({
...values,
[key]: value
}));
}

function handleSubmit(e) {
e.preventDefault();
setSending(true);
Inertia.post(route('payments.store'), values).then(() => {
setSending(false);
});
}

return (
<Layout>
<Helmet title="Create Payment" />
<div>
<h1 className="mb-8 text-3xl font-bold">
<InertiaLink
href={route('payments.index')}
className="text-indigo-600 hover:text-indigo-700"
>
Pembayaran
</InertiaLink>
<span className="font-medium text-indigo-600"> /</span> Buat
</h1>
<div className="max-w-3xl overflow-hidden bg-white rounded shadow">
<form onSubmit={handleSubmit}>
<div className="flex flex-wrap p-8 -mb-8 -mr-6">
<TextInput
className="w-full pb-8 pr-6 lg:w-1/2"
label="Keterangan"
name="description"
errors={errors.description}
value={values.description}
onChange={handleChange}
/>
<TextInput
className="w-full pb-8 pr-6 lg:w-1/2"
label="Jumlah"
name="amount"
type="number"
errors={errors.amount}
value={values.amount}
onChange={handleChange}
/>
<SelectInput
className="w-full pb-8 pr-6 lg:w-1/2"
label="Penagihan"
name="invoice_id"
errors={errors.invoice_id}
value={values.invoice_id}
onChange={handleChange}
>
<option value="" disabled>Pilih Penagihan</option>
{
invoices.map((invoice) => (
<option key={invoice.id} value={invoice.id}>{invoice.id} - {invoice.created_at}</option>
))
}
</SelectInput>
</div>
<div className="flex items-center justify-end px-8 py-4 bg-gray-100 border-t border-gray-200">
<LoadingButton
loading={sending}
type="submit"
className="btn-indigo"
>
Tambah Pembayaran
</LoadingButton>
</div>
</form>
</div>
</div>
</Layout>
);
};
Loading

0 comments on commit 3425f31

Please sign in to comment.