Merge pull request #611 from klittle81/EnhanceExpenseReport

Enhance Expense Report - Grouped itemized Expenses By Expense Category
This commit is contained in:
mchev
2026-04-07 12:58:09 +02:00
committed by GitHub
3 changed files with 126 additions and 39 deletions

View File

@@ -7,11 +7,12 @@ use App\Models\Company;
use App\Models\CompanySetting;
use App\Models\Currency;
use App\Models\Expense;
use Barryvdh\DomPDF\Facade\Pdf;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\App;
use PDF;
class ExpensesReportController extends Controller
{
@@ -19,7 +20,7 @@ class ExpensesReportController extends Controller
* Handle the incoming request.
*
* @param string $hash
* @return JsonResponse
* @return View|Response
*/
public function __invoke(Request $request, $hash)
{
@@ -31,14 +32,26 @@ class ExpensesReportController extends Controller
App::setLocale($locale);
$expenseCategories = Expense::with('category')
// Fetch individual expenses (filtered and ordered by date), then group by category
$expenses = Expense::with('category')
->whereCompanyId($company->id)
->applyFilters($request->only(['from_date', 'to_date']))
->expensesAttributes()
->applyFilters($request->only(['from_date', 'to_date', 'expense_category_id']))
->orderBy('expense_date', 'asc')
->get();
$totalAmount = 0;
foreach ($expenseCategories as $category) {
$totalAmount += $category->total_amount;
$totalAmount = $expenses->sum('base_amount');
$grouped = $expenses->groupBy(function ($item) {
return $item->category ? $item->category->name : trans('expenses.uncategorized');
});
$expenseGroups = collect();
foreach ($grouped as $categoryName => $group) {
$expenseGroups->push([
'name' => $categoryName,
'expenses' => $group,
'total' => $group->sum('base_amount'),
]);
}
$dateFormat = CompanySetting::getSetting('carbon_date_format', $company->id);
@@ -62,7 +75,7 @@ class ExpensesReportController extends Controller
->get();
view()->share([
'expenseCategories' => $expenseCategories,
'expenseGroups' => $expenseGroups,
'colorSettings' => $colorSettings,
'totalExpense' => $totalAmount,
'company' => $company,
@@ -70,7 +83,7 @@ class ExpensesReportController extends Controller
'to_date' => $to_date,
'currency' => $currency,
]);
$pdf = PDF::loadView('app.pdf.reports.expenses');
$pdf = Pdf::loadView('app.pdf.reports.expenses');
if ($request->has('preview')) {
return view('app.pdf.reports.expenses');

View File

@@ -652,6 +652,7 @@
"currency": "Currency",
"contact": "Contact",
"category": "Category",
"uncategorized": "Uncategorized",
"from_date": "From Date",
"to_date": "To Date",
"expense_date": "Date",
@@ -1649,6 +1650,7 @@
"pdf_total_tax_label": "TOTAL TAX",
"pdf_tax_types_label": "Tax Types",
"pdf_expenses_label": "Expenses",
"pdf_expense_group_total_label": "Group total:",
"pdf_bill_to": "Bill to,",
"pdf_ship_to": "Ship to,",
"pdf_received_from": "Received from:",

View File

@@ -33,7 +33,7 @@
.heading-date-range {
font-weight: normal;
font-size: 15px;
color: #A5ACC1;
color: #606060;
width: 100%;
text-align: right;
padding: 0px;
@@ -41,7 +41,7 @@
}
.sub-heading-text {
font-weight: normal;
font-weight: bold;
font-size: 16px;
color: #595959;
padding: 0px;
@@ -50,8 +50,7 @@
}
.expenses-title {
margin-top: 60px;
padding-left: 3px;
margin-top: 30px;
font-size: 16px;
line-height: 21px;
color: #040405;
@@ -133,6 +132,84 @@
line-height: 21px;
color: #5851D8;
}
/* -- Items Table -- */
.items-table {
margin-top: 35px;
padding: 0px 30px 10px 30px;
page-break-before: avoid;
page-break-after: auto;
}
.items-table hr {
height: 0.1px;
}
.item-table-heading-left {
font-size: 13.5;
text-align: left;
color: rgba(0, 0, 0, 0.85);
padding: 5px;
padding-bottom: 10px;
}
.item-table-heading-right {
font-size: 13.5;
text-align: right;
color: rgba(0, 0, 0, 0.85);
padding: 5px;
padding-bottom: 10px;
}
tr.item-table-heading-row th {
border-bottom: 0.620315px solid #E8E8E8;
font-size: 12px;
line-height: 18px;
}
.item-table-heading-row {
margin-bottom: 10px;
}
tr.item-row td {
font-size: 12px;
line-height: 18px;
}
.item-cell-left {
font-size: 13;
color: #040405;
text-align: left;
padding: 5px;
padding-top: 10px;
border-color: #d9d9d9;
}
.item-cell-right {
font-size: 13;
color: #040405;
text-align: right;
padding: 5px;
padding-top: 10px;
border-color: #d9d9d9;
}
.item-description {
color: #595959;
font-size: 9px;
line-height: 12px;
}
.item-table-group-total {
font-size: 14px;
font-weight: bold;
text-align: right;
color: rgba(0, 0, 0, 0.85);
padding: 5px;
padding-bottom: 10px;
}
</style>
@if (App::isLocale('th'))
@@ -158,33 +235,28 @@
</tr>
</table>
<p class="expenses-title">@lang('pdf_expenses_label')</p>
<div class="expenses-table-container">
<table class="expenses-table">
@foreach ($expenseCategories as $expenseCategory)
<tr>
<td>
<p class="expense-title">
{{ $expenseCategory->category->name }}
</p>
</td>
<td>
<p class="expense-amount">
{!! format_money_pdf($expenseCategory->total_amount, $currency) !!}
</p>
</td>
@foreach ($expenseGroups as $group)
<p class="expense-title">{{ $group['name'] }}</p>
<table width="100%" style="margin-bottom:18px;">
<tr class="item-table-heading-row">
<th style="width: 15%;" class="text-left item-table-heading-left">@lang('Date')</th>
<th style="width: 70%;" class="text-left item-table-heading-left">@lang('Note')</th>
<th style="width: 15%;" class="text-right item-table-heading-right">@lang('Amount')</th>
</tr>
@foreach ($group['expenses'] as $expense)
<tr class="item-row">
<td style="width: 15%;" class="text-left item-cell-left">{{ $expense->formatted_expense_date }}</td>
<td style="width: 70%;" class="text-left item-cell-left">{{ $expense->notes ? $expense->notes : '-' }}</td>
<td style="width: 15%;" class="text-right item-cell-right">{!! format_money_pdf($expense->base_amount, $currency) !!}</td>
</tr>
@endforeach
</table>
</div>
</div>
<table class="expense-total-table">
<tr>
<td class="expense-total-cell">
<p class="expense-total">{!! format_money_pdf($totalExpense, $currency) !!}</p>
</td>
</tr>
</table>
</table>
<div class="item-table-group-total">
<p>@lang('pdf_expense_group_total_label')&nbsp;&nbsp;&nbsp;<span style="color: #5851D8;">{!! format_money_pdf($group['total'], $currency) !!}</span></p>
</div>
@endforeach
</div>
<table class="report-footer">
<tr>
<td>