diff --git a/app/Http/Controllers/Admin/PirepController.php b/app/Http/Controllers/Admin/PirepController.php index 6bfacc7f..c0b04fef 100644 --- a/app/Http/Controllers/Admin/PirepController.php +++ b/app/Http/Controllers/Admin/PirepController.php @@ -12,6 +12,7 @@ use App\Models\PirepComment; use App\Repositories\AircraftRepository; use App\Repositories\AirlineRepository; use App\Repositories\AirportRepository; +use App\Repositories\JournalRepository; use App\Repositories\PirepFieldRepository; use App\Repositories\PirepRepository; use App\Repositories\SubfleetRepository; @@ -33,6 +34,7 @@ class PirepController extends BaseController $airlineRepo, $aircraftRepo, $fareSvc, + $journalRepo, $pirepSvc, $pirepRepo, $pirepFieldRepo, @@ -44,7 +46,10 @@ class PirepController extends BaseController * @param AirportRepository $airportRepo * @param AirlineRepository $airlineRepo * @param AircraftRepository $aircraftRepo + * @param FareService $fareSvc + * @param JournalRepository $journalRepo * @param PirepRepository $pirepRepo + * @param PirepFieldRepository $pirepFieldRepo * @param PIREPService $pirepSvc * @param SubfleetRepository $subfleetRepo * @param UserService $userSvc @@ -54,6 +59,7 @@ class PirepController extends BaseController AirlineRepository $airlineRepo, AircraftRepository $aircraftRepo, FareService $fareSvc, + JournalRepository $journalRepo, PirepRepository $pirepRepo, PirepFieldRepository $pirepFieldRepo, PIREPService $pirepSvc, @@ -64,6 +70,7 @@ class PirepController extends BaseController $this->airlineRepo = $airlineRepo; $this->aircraftRepo = $aircraftRepo; $this->fareSvc = $fareSvc; + $this->journalRepo = $journalRepo; $this->pirepRepo = $pirepRepo; $this->pirepFieldRepo = $pirepFieldRepo; $this->pirepSvc = $pirepSvc; @@ -266,6 +273,7 @@ class PirepController extends BaseController * Show the form for editing the specified Pirep. * @param int $id * @return Response + * @throws \InvalidArgumentException */ public function edit($id) { @@ -293,6 +301,8 @@ class PirepController extends BaseController $pirep->{$field_name} = $fare->count; } + $journal = $this->journalRepo->getAllForObject($pirep, $pirep->airline->journal); + return view('admin.pireps.edit', [ 'pirep' => $pirep, 'read_only' => $read_only, @@ -300,6 +310,7 @@ class PirepController extends BaseController 'aircraft_list' => $this->aircraftList(), 'airports_list' => $this->airportRepo->selectBoxList(), 'airlines_list' => $this->airlineRepo->selectBoxList(), + 'journal' => $journal, ]); } diff --git a/app/Models/Pirep.php b/app/Models/Pirep.php index f0930793..916b9d2d 100644 --- a/app/Models/Pirep.php +++ b/app/Models/Pirep.php @@ -318,6 +318,12 @@ class Pirep extends BaseModel ->latest(); } + public function transactions() + { + return $this->hasMany(JournalTransaction::class, 'ref_class_id') + ->where('ref_class', __CLASS__); + } + public function user() { return $this->belongsTo(User::class, 'user_id'); diff --git a/app/Repositories/JournalRepository.php b/app/Repositories/JournalRepository.php index f5cbd7b8..d8aa45ef 100644 --- a/app/Repositories/JournalRepository.php +++ b/app/Repositories/JournalRepository.php @@ -171,16 +171,21 @@ class JournalRepository extends BaseRepository implements CacheableInterface /** * Return all transactions for a given object * @param $object + * @param null $journal * @return array - * @throws \UnexpectedValueException - * @throws \InvalidArgumentException */ - public function getAllForObject($object) + public function getAllForObject($object, $journal=null) { - $transactions = $this->findWhere([ + $where = [ 'ref_class' => \get_class($object), 'ref_class_id' => $object->id, - ]); + ]; + + if($journal) { + $where['journal_id'] = $journal->id; + } + + $transactions = $this->findWhere($where); return [ 'credits' => new Money($transactions->sum('credit')), diff --git a/resources/views/admin/pireps/edit.blade.php b/resources/views/admin/pireps/edit.blade.php index 3e3dffc9..677154c9 100644 --- a/resources/views/admin/pireps/edit.blade.php +++ b/resources/views/admin/pireps/edit.blade.php @@ -42,6 +42,13 @@ +
{!! $entry->memo !!} | ++ @if($entry->credit) + {!! money($entry->credit, config('phpvms.currency')) !!} + @endif + | ++ @if($entry->debit) + {!! money($entry->debit, config('phpvms.currency')) !!} + @endif + | +
+ | + {!! $journal['credits'] !!} + | ++ ({!! $journal['debits'] !!}) + | +
+ | Total | ++ {!! $journal['credits']->subtract($journal['debits']) !!} + | +