2018-03-06 04:18:54 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
use App\Interfaces\Controller;
|
2018-03-06 09:55:48 +08:00
|
|
|
use App\Models\Enums\JournalType;
|
|
|
|
use App\Models\Journal;
|
|
|
|
use App\Models\JournalTransaction;
|
|
|
|
use App\Repositories\AirlineRepository;
|
|
|
|
use App\Support\Dates;
|
|
|
|
use App\Support\Money;
|
2018-03-06 04:18:54 +08:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class FinanceController
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
class FinanceController extends Controller
|
2018-03-06 04:18:54 +08:00
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
private $airlineRepo;
|
2018-03-06 04:18:54 +08:00
|
|
|
|
|
|
|
/**
|
2018-08-27 00:40:04 +08:00
|
|
|
* @param AirlineRepository $airlineRepo
|
2018-03-06 04:18:54 +08:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2018-03-20 09:50:40 +08:00
|
|
|
AirlineRepository $airlineRepo
|
2018-03-06 04:18:54 +08:00
|
|
|
) {
|
2018-03-06 09:55:48 +08:00
|
|
|
$this->airlineRepo = $airlineRepo;
|
2018-03-06 04:18:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-06 09:55:48 +08:00
|
|
|
* Display the summation tables for a given month by airline
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-06 09:55:48 +08:00
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-06 09:55:48 +08:00
|
|
|
* @throws \UnexpectedValueException
|
|
|
|
* @throws \InvalidArgumentException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return mixed
|
2018-03-06 04:18:54 +08:00
|
|
|
*/
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
2018-03-06 09:55:48 +08:00
|
|
|
$month = $request->query('month', date('Y-m'));
|
|
|
|
$between = Dates::getMonthBoundary($month);
|
2018-03-06 04:18:54 +08:00
|
|
|
|
2018-03-06 09:55:48 +08:00
|
|
|
$first_journal = Journal::where(['type' => JournalType::AIRLINE])
|
|
|
|
->orderBy('created_at', 'asc')
|
|
|
|
->limit(1)
|
|
|
|
->first();
|
|
|
|
|
|
|
|
$transaction_groups = [];
|
|
|
|
$airlines = $this->airlineRepo->orderBy('icao')->all();
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// group by the airline
|
2018-03-20 09:50:40 +08:00
|
|
|
foreach ($airlines as $airline) {
|
2018-08-27 00:40:04 +08:00
|
|
|
// Return all the transactions, grouped by the transaction group
|
2018-05-16 14:47:31 +08:00
|
|
|
$transactions = JournalTransaction::groupBy('transaction_group', 'currency')
|
2018-03-06 09:55:48 +08:00
|
|
|
->selectRaw('transaction_group, currency,
|
|
|
|
SUM(credit) as sum_credits,
|
|
|
|
SUM(debit) as sum_debits')
|
|
|
|
->where([
|
2018-08-27 00:40:04 +08:00
|
|
|
'journal_id' => $airline->journal->id,
|
2018-03-06 09:55:48 +08:00
|
|
|
])
|
|
|
|
->whereBetween('created_at', $between, 'AND')
|
|
|
|
->orderBy('sum_credits', 'desc')
|
|
|
|
->orderBy('sum_debits', 'desc')
|
|
|
|
->orderBy('transaction_group', 'asc')
|
|
|
|
->get();
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Summate it so we can show it on the footer of the table
|
2018-03-06 09:55:48 +08:00
|
|
|
$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[] = [
|
2018-03-20 09:50:40 +08:00
|
|
|
'airline' => $airline,
|
|
|
|
'credits' => new Money($sum_all_credits),
|
|
|
|
'debits' => new Money($sum_all_debits),
|
|
|
|
'transactions' => $transactions,
|
2018-03-06 09:55:48 +08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('admin.finances.index', [
|
2018-03-20 09:50:40 +08:00
|
|
|
'current_month' => $month,
|
|
|
|
'months_list' => Dates::getMonthsList($first_journal->created_at),
|
2018-03-06 09:55:48 +08:00
|
|
|
'transaction_groups' => $transaction_groups,
|
|
|
|
]);
|
2018-03-06 04:18:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show a month
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @param mixed $id
|
2018-03-06 04:18:54 +08:00
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|