Move airline transaction code into service layer (#371)
* Move airline transaction code into service layer * Remove unused import
This commit is contained in:
@@ -5,10 +5,9 @@ namespace App\Http\Controllers\Admin;
|
|||||||
use App\Contracts\Controller;
|
use App\Contracts\Controller;
|
||||||
use App\Models\Enums\JournalType;
|
use App\Models\Enums\JournalType;
|
||||||
use App\Models\Journal;
|
use App\Models\Journal;
|
||||||
use App\Models\JournalTransaction;
|
|
||||||
use App\Repositories\AirlineRepository;
|
use App\Repositories\AirlineRepository;
|
||||||
|
use App\Services\FinanceService;
|
||||||
use App\Support\Dates;
|
use App\Support\Dates;
|
||||||
use App\Support\Money;
|
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -17,14 +16,18 @@ use Illuminate\Http\Request;
|
|||||||
class FinanceController extends Controller
|
class FinanceController extends Controller
|
||||||
{
|
{
|
||||||
private $airlineRepo;
|
private $airlineRepo;
|
||||||
|
private $financeSvc;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param AirlineRepository $airlineRepo
|
* @param AirlineRepository $airlineRepo
|
||||||
|
* @param FinanceService $financeSvc
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
AirlineRepository $airlineRepo
|
AirlineRepository $airlineRepo,
|
||||||
|
FinanceService $financeSvc
|
||||||
) {
|
) {
|
||||||
$this->airlineRepo = $airlineRepo;
|
$this->airlineRepo = $airlineRepo;
|
||||||
|
$this->financeSvc = $financeSvc;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -52,34 +55,11 @@ class FinanceController extends Controller
|
|||||||
|
|
||||||
// group by the airline
|
// group by the airline
|
||||||
foreach ($airlines as $airline) {
|
foreach ($airlines as $airline) {
|
||||||
// Return all the transactions, grouped by the transaction group
|
$transaction_groups[] = $this->financeSvc->getAirlineTransactionsBetween(
|
||||||
$transactions = JournalTransaction::groupBy('transaction_group', 'currency')
|
$airline,
|
||||||
->selectRaw('transaction_group, currency,
|
$between[0],
|
||||||
SUM(credit) as sum_credits,
|
$between[1]
|
||||||
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,
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return view('admin.finances.index', [
|
return view('admin.finances.index', [
|
||||||
|
|||||||
53
app/Services/FinanceService.php
Normal file
53
app/Services/FinanceService.php
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services;
|
||||||
|
|
||||||
|
use App\Contracts\Service;
|
||||||
|
use App\Models\Airline;
|
||||||
|
use App\Models\JournalTransaction;
|
||||||
|
use App\Support\Money;
|
||||||
|
|
||||||
|
class FinanceService extends Service
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Get all of the transactions for an airline between two given dates. Returns an array
|
||||||
|
* with `credits`, `debits` and `transactions` fields, where transactions contains the
|
||||||
|
* grouped transactions (e.g, "Fares" and "Ground Handling", etc)
|
||||||
|
*
|
||||||
|
* @param Airline $airline
|
||||||
|
* @param string $start_date YYYY-MM-DD
|
||||||
|
* @param string $end_date YYYY-MM-DD
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getAirlineTransactionsBetween($airline, $start_date, $end_date)
|
||||||
|
{
|
||||||
|
// 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', [$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,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user