From 31644676cfd1559820d844c2e7477856d3f657ac Mon Sep 17 00:00:00 2001 From: Faqih Muntashir Date: Mon, 27 Jul 2020 09:35:51 +0700 Subject: [PATCH] Menambah riwayat pembayaran pada edit penginapan --- app/Bill.php | 5 ++ app/Http/Controllers/LodgingsController.php | 13 +++- app/Invoice.php | 5 ++ app/Lodging.php | 18 ++++++ composer.json | 1 + composer.lock | 45 +++++++++++++- resources/js/Pages/Lodgings/Edit.jsx | 69 ++++++++++++--------- 7 files changed, 124 insertions(+), 32 deletions(-) diff --git a/app/Bill.php b/app/Bill.php index 1b3ab9d..ddfbff6 100644 --- a/app/Bill.php +++ b/app/Bill.php @@ -16,6 +16,11 @@ public function logdging() { return $this->belongsTo(Lodging::class); } + + public function invoice() + { + return $this->hasOne(Invoice::class); + } public function scopeFilter($query, array $filters) { diff --git a/app/Http/Controllers/LodgingsController.php b/app/Http/Controllers/LodgingsController.php index 336b796..f61ad74 100644 --- a/app/Http/Controllers/LodgingsController.php +++ b/app/Http/Controllers/LodgingsController.php @@ -29,7 +29,6 @@ public function index() 'deleted_at' => $lodging->deleted_at, ]; }), - // ->only('id', 'room_id', 'renter_id', 'start_at', 'end_at', 'deleted_at') ]); } @@ -64,7 +63,17 @@ public function edit(Lodging $lodging) 'room' => $lodging->room, 'start_at' => $lodging->start_at->format('Y-m-d'), 'end_at' => $lodging->end_at->format('Y-m-d'), - 'deleted_at' => $lodging->deleted_at + 'deleted_at' => $lodging->deleted_at, + 'payments' => $lodging->payments->transform(function ($payment) { + return [ + 'id' => $payment->id, + 'item' => $payment->invoice->bill->name, + 'amount' => $payment->amount, + 'issued_at' => $payment->invoice->created_at->format('d F Y'), + 'created_at' => $payment->created_at->format('d F Y'), + 'deleted_at' => $payment->deleted_at + ]; + }) ], 'rooms' => Room::all(), 'renters' => Renter::all(), diff --git a/app/Invoice.php b/app/Invoice.php index 9fa49ae..60c1134 100644 --- a/app/Invoice.php +++ b/app/Invoice.php @@ -14,6 +14,11 @@ public function bill() return $this->belongsTo(Bill::class); } + public function payment() + { + return $this->hasOne(Payment::class); + } + public function scopeFilter($query, array $filters) { $query->when($filters['search'] ?? null, function ($query, $search) { diff --git a/app/Lodging.php b/app/Lodging.php index 2286d8f..65c8adf 100644 --- a/app/Lodging.php +++ b/app/Lodging.php @@ -4,10 +4,13 @@ use Carbon\Carbon; use Illuminate\Database\Eloquent\SoftDeletes; +use Staudenmeir\EloquentHasManyDeep\HasRelationships; class Lodging extends Model { use SoftDeletes; + use HasRelationships; + protected $dates = ['start_at', 'end_at']; public function renter() @@ -20,6 +23,21 @@ public function room() return $this->belongsTo(Room::class); } + public function bills() + { + return $this->hasMany(Bill::class); + } + + public function invoices() + { + return $this->hasManyThrough(Invoice::class, Bill::class); + } + + public function payments() + { + return $this->hasManyDeep(Payment::class, [Bill::class, Invoice::class]); + } + public function scopeFilter($query, array $filters) { $query->when($filters['search'] ?? null, function ($query, $search) { diff --git a/composer.json b/composer.json index e32acd9..1feebb3 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,7 @@ "laravel/ui": "^2.0", "league/glide": "2.0.x-dev", "reinink/remember-query-strings": "^0.1.0", + "staudenmeir/eloquent-has-many-deep": "^1.7", "tightenco/ziggy": "^0.8.0" }, "autoload": { diff --git a/composer.lock b/composer.lock index b756fee..37d3806 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "c2cabc698cee475cf02a0e11643754f0", + "content-hash": "8ef7e7cf18d778986b7a3e106843cc2c", "packages": [ { "name": "brick/math", @@ -1971,6 +1971,49 @@ ], "time": "2019-04-02T17:35:40+00:00" }, + { + "name": "staudenmeir/eloquent-has-many-deep", + "version": "v1.12", + "source": { + "type": "git", + "url": "https://github.com/staudenmeir/eloquent-has-many-deep.git", + "reference": "7417572873c9fb4fa84e894ebbf324629cbc63c0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/staudenmeir/eloquent-has-many-deep/zipball/7417572873c9fb4fa84e894ebbf324629cbc63c0", + "reference": "7417572873c9fb4fa84e894ebbf324629cbc63c0", + "shasum": "" + }, + "require": { + "illuminate/database": "^7.0", + "php": "^7.2.5" + }, + "require-dev": { + "illuminate/pagination": "^7.0", + "laravel/homestead": "^10.0", + "phpunit/phpunit": "^8.5", + "staudenmeir/eloquent-eager-limit": "^1.5" + }, + "type": "library", + "autoload": { + "psr-4": { + "Staudenmeir\\EloquentHasManyDeep\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jonas Staudenmeir", + "email": "mail@jonas-staudenmeir.de" + } + ], + "description": "Laravel Eloquent HasManyThrough relationships with unlimited levels", + "time": "2020-01-31T12:37:57+00:00" + }, { "name": "swiftmailer/swiftmailer", "version": "v6.2.3", diff --git a/resources/js/Pages/Lodgings/Edit.jsx b/resources/js/Pages/Lodgings/Edit.jsx index 18fd5ae..77d3ea8 100644 --- a/resources/js/Pages/Lodgings/Edit.jsx +++ b/resources/js/Pages/Lodgings/Edit.jsx @@ -9,6 +9,7 @@ import TextInput from '@/Shared/TextInput'; import SelectInput from '@/Shared/SelectInput'; import TrashedMessage from '@/Shared/TrashedMessage'; import Icon from '@/Shared/Icon'; +import { currency } from '@/utils'; export default () => { const { errors, lodging, renters, rooms } = usePage(); @@ -73,7 +74,7 @@ export default () => {
- { onChange={handleChange} > - { - renters.map((renter, index) => ( - - )) - } + { + renters.map((renter, index) => ( + + )) + } { onChange={handleChange} > - { - rooms.map((room, index) => ( - - )) - } + { + rooms.map((room, index) => ( + + )) + } { /> {
- {/*

Contacts

+

Riwayat Pembayaran

- - + + + - {lodging.contacts.map( - ({ id, name, phone, city, deleted_at }) => { + {lodging.payments.map( + ({ id, item, amount, issued_at, created_at, deleted_at }) => { return ( { > + )}
NameCityItem TagihanJumlahTanggal Penagihan - Phone + Tanggal Pembayaran
- {name} + {item} {deleted_at && ( { - {city} + {currency(amount)} - {phone} + {issued_at} + + + + {created_at} { ); } )} - {lodging.contacts.length === 0 && ( + {lodging.payments.length === 0 && (
- No contacts found. + Belum ada data pembayaran
-
*/} + );