From fd2f4f2150f227acd80a8a88e1d45b40975b3d27 Mon Sep 17 00:00:00 2001 From: Nabeel S Date: Mon, 26 Aug 2019 14:55:59 -0400 Subject: [PATCH] Move airline transaction code into service layer (#371) * Move airline transaction code into service layer * Remove unused import --- .../Controllers/Admin/FinanceController.php | 42 ++++----------- app/Services/FinanceService.php | 53 +++++++++++++++++++ 2 files changed, 64 insertions(+), 31 deletions(-) create mode 100644 app/Services/FinanceService.php diff --git a/app/Http/Controllers/Admin/FinanceController.php b/app/Http/Controllers/Admin/FinanceController.php index 574871a8..dddca324 100644 --- a/app/Http/Controllers/Admin/FinanceController.php +++ b/app/Http/Controllers/Admin/FinanceController.php @@ -5,10 +5,9 @@ namespace App\Http\Controllers\Admin; use App\Contracts\Controller; use App\Models\Enums\JournalType; use App\Models\Journal; -use App\Models\JournalTransaction; use App\Repositories\AirlineRepository; +use App\Services\FinanceService; use App\Support\Dates; -use App\Support\Money; use Illuminate\Http\Request; /** @@ -17,14 +16,18 @@ use Illuminate\Http\Request; class FinanceController extends Controller { private $airlineRepo; + private $financeSvc; /** * @param AirlineRepository $airlineRepo + * @param FinanceService $financeSvc */ public function __construct( - AirlineRepository $airlineRepo + AirlineRepository $airlineRepo, + FinanceService $financeSvc ) { $this->airlineRepo = $airlineRepo; + $this->financeSvc = $financeSvc; } /** @@ -52,34 +55,11 @@ class FinanceController extends Controller // group by the airline foreach ($airlines as $airline) { - // Return all the transactions, grouped by the transaction group - $transactions = JournalTransaction::groupBy('transaction_group', 'currency') - ->selectRaw('transaction_group, currency, - SUM(credit) as sum_credits, - SUM(debit) as sum_debits') - ->where([ - 'journal_id' => $airline->journal->id, - ]) - ->whereBetween('created_at', $between, 'AND') - ->orderBy('sum_credits', 'desc') - ->orderBy('sum_debits', 'desc') - ->orderBy('transaction_group', 'asc') - ->get(); - - // Summate it so we can show it on the footer of the table - $sum_all_credits = 0; - $sum_all_debits = 0; - foreach ($transactions as $ta) { - $sum_all_credits += $ta->sum_credits ?? 0; - $sum_all_debits += $ta->sum_debits ?? 0; - } - - $transaction_groups[] = [ - 'airline' => $airline, - 'credits' => new Money($sum_all_credits), - 'debits' => new Money($sum_all_debits), - 'transactions' => $transactions, - ]; + $transaction_groups[] = $this->financeSvc->getAirlineTransactionsBetween( + $airline, + $between[0], + $between[1] + ); } return view('admin.finances.index', [ diff --git a/app/Services/FinanceService.php b/app/Services/FinanceService.php new file mode 100644 index 00000000..4ad60c1a --- /dev/null +++ b/app/Services/FinanceService.php @@ -0,0 +1,53 @@ +selectRaw('transaction_group, + currency, + SUM(credit) as sum_credits, + SUM(debit) as sum_debits') + ->where(['journal_id' => $airline->journal->id]) + ->whereBetween('created_at', [$start_date, $end_date], 'AND') + ->orderBy('sum_credits', 'desc') + ->orderBy('sum_debits', 'desc') + ->orderBy('transaction_group', 'asc') + ->get(); + + // Summate it so we can show it on the footer of the table + $sum_all_credits = 0; + $sum_all_debits = 0; + foreach ($transactions as $ta) { + $sum_all_credits += $ta->sum_credits ?? 0; + $sum_all_debits += $ta->sum_debits ?? 0; + } + + return [ + 'airline' => $airline, + 'credits' => new Money($sum_all_credits), + 'debits' => new Money($sum_all_debits), + 'transactions' => $transactions, + ]; + } +}