diff --git a/app/Http/Controllers/Api/PirepController.php b/app/Http/Controllers/Api/PirepController.php index d6bd17c5..a282a33f 100644 --- a/app/Http/Controllers/Api/PirepController.php +++ b/app/Http/Controllers/Api/PirepController.php @@ -13,6 +13,7 @@ use App\Http\Requests\Acars\PrefileRequest; use App\Http\Requests\Acars\RouteRequest; use App\Http\Requests\Acars\UpdateRequest; use App\Http\Resources\AcarsRoute as AcarsRouteResource; +use App\Http\Resources\JournalTransaction as JournalTransactionResource; use App\Http\Resources\Pirep as PirepResource; use App\Http\Resources\PirepComment as PirepCommentResource; use App\Models\Acars; @@ -23,7 +24,9 @@ use App\Models\Enums\PirepStatus; use App\Models\Pirep; use App\Models\PirepComment; use App\Repositories\AcarsRepository; +use App\Repositories\JournalRepository; use App\Repositories\PirepRepository; +use App\Services\FinanceService; use App\Services\GeoService; use App\Services\PIREPService; use App\Services\UserService; @@ -33,29 +36,37 @@ use Log; class PirepController extends RestController { - protected $acarsRepo, - $geoSvc, - $pirepRepo, - $pirepSvc, - $userSvc; + private $acarsRepo, + $financeSvc, + $geoSvc, + $journalRepo, + $pirepRepo, + $pirepSvc, + $userSvc; /** * PirepController constructor. * @param AcarsRepository $acarsRepo + * @param FinanceService $financeSvc * @param GeoService $geoSvc + * @param JournalRepository $journalRepo * @param PirepRepository $pirepRepo * @param PIREPService $pirepSvc * @param UserService $userSvc */ public function __construct( AcarsRepository $acarsRepo, + FinanceService $financeSvc, GeoService $geoSvc, + JournalRepository $journalRepo, PirepRepository $pirepRepo, PIREPService $pirepSvc, UserService $userSvc ) { $this->acarsRepo = $acarsRepo; + $this->financeSvc = $financeSvc; $this->geoSvc = $geoSvc; + $this->journalRepo = $journalRepo; $this->pirepRepo = $pirepRepo; $this->pirepSvc = $pirepSvc; $this->userSvc = $userSvc; @@ -436,6 +447,37 @@ class PirepController extends RestController return new PirepCommentResource($comment); } + /** + * @param $id + * @param Request $request + * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection + */ + public function finances_get($id, Request $request) + { + $pirep = $this->pirepRepo->find($id); + $transactions = $this->journalRepo->getAllForObject($pirep); + return JournalTransactionResource::collection($transactions); + } + + /** + * @param $id + * @param Request $request + * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection + * @throws \UnexpectedValueException + * @throws \InvalidArgumentException + * @throws \Exception + * @throws \Prettus\Validator\Exceptions\ValidatorException + */ + public function finances_recalculate($id, Request $request) + { + $pirep = $this->pirepRepo->find($id); + $this->financeSvc->processFinancesForPirep($pirep); + + $pirep->refresh(); + $transactions = $this->journalRepo->getAllForObject($pirep); + return JournalTransactionResource::collection($transactions['transactions']); + } + /** * @param $id * @param Request $request diff --git a/app/Http/Resources/JournalTransaction.php b/app/Http/Resources/JournalTransaction.php new file mode 100644 index 00000000..edadba6e --- /dev/null +++ b/app/Http/Resources/JournalTransaction.php @@ -0,0 +1,14 @@ + ['api.auth']], function () Route::post('pireps/{pirep_id}/comments', 'PirepController@comments_post'); Route::delete('pireps/{pirep_id}/cancel', 'PirepController@cancel'); + Route::get('pireps/{pirep_id}/finances', 'PirepController@finances_get'); + Route::post('pireps/{pirep_id}/finances/recalculate', 'PirepController@finances_recalculate'); + Route::get('pireps/{pirep_id}/route', 'PirepController@route_get'); Route::post('pireps/{pirep_id}/route', 'PirepController@route_post'); Route::delete('pireps/{pirep_id}/route', 'PirepController@route_delete'); diff --git a/resources/views/admin/pireps/edit.blade.php b/resources/views/admin/pireps/edit.blade.php index 677154c9..5ec937cc 100644 --- a/resources/views/admin/pireps/edit.blade.php +++ b/resources/views/admin/pireps/edit.blade.php @@ -44,6 +44,11 @@
+
+ +

transactions

@include('admin.pireps.transactions')
diff --git a/resources/views/admin/pireps/scripts.blade.php b/resources/views/admin/pireps/scripts.blade.php index 3f3a96da..406d2d97 100644 --- a/resources/views/admin/pireps/scripts.blade.php +++ b/resources/views/admin/pireps/scripts.blade.php @@ -15,7 +15,7 @@ const changeStatus = (values, fn) => { }); }; -$(document).ready(function() { +$(document).ready(() => { const select_id = "select#aircraft_select"; const destContainer = $('#fares_container'); @@ -40,7 +40,7 @@ $(document).ready(function() { }); }); - $(document).on('submit', 'form.pjax_form', function (event) { + $(document).on('submit', 'form.pjax_form', (event) => { event.preventDefault(); $.pjax.submit(event, '#pirep_comments_wrapper', {push: false}); }); @@ -49,7 +49,28 @@ $(document).ready(function() { $(".select2").select2(); }); - $(document).on('submit', 'form.pirep_submit_status', function (event) { + /** + * Recalculate finances button is clicked + */ + $('button#recalculate-finances').on('click', (event) => { + event.preventDefault(); + console.log('Sending recalculate finances request'); + const pirep_id = $(event.currentTarget).attr('data-pirep-id'); + + $.ajax({ + url: BASE_URL + '/api/pireps/' + pirep_id + '/finances/recalculate', + type: 'POST', + headers: { + 'x-api-key': PHPVMS_USER_API_KEY + }, + success: (data) => { + console.log(data); + //location.reload(); + } + }); + }); + + $(document).on('submit', 'form.pirep_submit_status', (event) => { console.log(event); event.preventDefault(); @@ -64,7 +85,7 @@ $(document).ready(function() { }); }); - $(document).on('submit', 'form.pirep_change_status', function(event) { + $(document).on('submit', 'form.pirep_change_status', (event) => { event.preventDefault(); changeStatus({ pirep_id: $(this).attr('pirep_id'), diff --git a/resources/views/admin/pireps/transactions.blade.php b/resources/views/admin/pireps/transactions.blade.php index 95d1587c..7a1fa299 100644 --- a/resources/views/admin/pireps/transactions.blade.php +++ b/resources/views/admin/pireps/transactions.blade.php @@ -1,5 +1,5 @@ -@if(count($journal['transactions']) > 0)
+@if(count($journal['transactions']) > 0) @foreach($journal['transactions'] as $entry)