Refactor DailySalePaymentReportModule Table 1 logic with date filtering
This commit is contained in:
@@ -5,6 +5,8 @@ namespace Modules\DailySalePaymentReportModule\Http\Controllers;
|
|||||||
use Illuminate\Contracts\Support\Renderable;
|
use Illuminate\Contracts\Support\Renderable;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Routing\Controller;
|
use Illuminate\Routing\Controller;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Carbon\Carbon;
|
||||||
|
|
||||||
class DailySalePaymentReportModuleController extends Controller
|
class DailySalePaymentReportModuleController extends Controller
|
||||||
{
|
{
|
||||||
@@ -12,9 +14,79 @@ class DailySalePaymentReportModuleController extends Controller
|
|||||||
* Display a listing of the resource.
|
* Display a listing of the resource.
|
||||||
* @return Renderable
|
* @return Renderable
|
||||||
*/
|
*/
|
||||||
public function index()
|
public function index(Request $request)
|
||||||
{
|
{
|
||||||
return view('dailysalepaymentreportmodule::index');
|
$report_date = $request->input('report_date', Carbon::today()->toDateString());
|
||||||
|
|
||||||
|
// Helper function (Closure) to calculate payment breakdown from a collection of payments
|
||||||
|
$calculatePayments = function ($payments) {
|
||||||
|
$breakdown = [
|
||||||
|
'cash' => 0,
|
||||||
|
'card1' => 0,
|
||||||
|
'card2' => 0,
|
||||||
|
'card3' => 0,
|
||||||
|
'online_transfer' => 0,
|
||||||
|
'link_pay' => 0
|
||||||
|
];
|
||||||
|
foreach ($payments as $payment) {
|
||||||
|
$method = strtolower(trim($payment->paying_method));
|
||||||
|
if ($method == 'cash') {
|
||||||
|
$breakdown['cash'] += $payment->total_amount;
|
||||||
|
} elseif ($method == 'card 1' || $method == 'card1') {
|
||||||
|
$breakdown['card1'] += $payment->total_amount;
|
||||||
|
} elseif ($method == 'card 2' || $method == 'card2') {
|
||||||
|
$breakdown['card2'] += $payment->total_amount;
|
||||||
|
} elseif ($method == 'card 3' || $method == 'card3') {
|
||||||
|
$breakdown['card3'] += $payment->total_amount;
|
||||||
|
} elseif (str_contains($method, 'online')) {
|
||||||
|
$breakdown['online_transfer'] += $payment->total_amount;
|
||||||
|
} elseif (str_contains($method, 'link')) {
|
||||||
|
$breakdown['link_pay'] += $payment->total_amount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $breakdown;
|
||||||
|
};
|
||||||
|
|
||||||
|
// --- TABLE 1: Daily Sales ---
|
||||||
|
$sales = DB::table('sales')
|
||||||
|
->leftJoin('users', 'sales.user_id', '=', 'users.id')
|
||||||
|
->leftJoin('customers', 'sales.customer_id', '=', 'customers.id')
|
||||||
|
->whereDate('sales.created_at', $report_date)
|
||||||
|
->select(
|
||||||
|
'sales.id',
|
||||||
|
'sales.created_at as date',
|
||||||
|
'sales.reference_no',
|
||||||
|
'users.name as cashier',
|
||||||
|
'customers.name as customer',
|
||||||
|
'sales.grand_total',
|
||||||
|
'sales.due_amount'
|
||||||
|
)->get();
|
||||||
|
|
||||||
|
$sales_data = [];
|
||||||
|
foreach ($sales as $sale) {
|
||||||
|
// Get payments ONLY made on the report_date
|
||||||
|
$payments = DB::table('payments')
|
||||||
|
->where('sale_id', $sale->id)
|
||||||
|
->whereDate('created_at', $report_date)
|
||||||
|
->select('paying_method', DB::raw('SUM(amount) as total_amount'))
|
||||||
|
->groupBy('paying_method')
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$breakdown = $calculatePayments($payments);
|
||||||
|
|
||||||
|
$sales_data[] = (object) array_merge([
|
||||||
|
'date' => Carbon::parse($sale->date)->format('Y-m-d'),
|
||||||
|
'reference_no' => $sale->reference_no,
|
||||||
|
'cashier' => $sale->cashier ?? 'N/A',
|
||||||
|
'customer' => $sale->customer ?? 'N/A',
|
||||||
|
'grand_total' => $sale->grand_total,
|
||||||
|
'due_amount' => $sale->due_amount
|
||||||
|
], $breakdown);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return view('dailysalepaymentreportmodule::index', compact('sales_data', 'report_date'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -90,21 +90,58 @@
|
|||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{{-- @foreach ($sales_data as $sale) --}}
|
@php
|
||||||
<!-- Data rows will come here -->
|
// යටින් පෙන්නන්න ඕන එකතුවට බින්දුවෙන් විචල්යයන් හදාගන්නවා
|
||||||
{{-- @endforeach --}}
|
$tot_grand = 0;
|
||||||
|
$tot_cash = 0;
|
||||||
|
$tot_card1 = 0;
|
||||||
|
$tot_card2 = 0;
|
||||||
|
$tot_card3 = 0;
|
||||||
|
$tot_online = 0;
|
||||||
|
$tot_link = 0;
|
||||||
|
$tot_due = 0;
|
||||||
|
@endphp
|
||||||
|
|
||||||
|
@foreach ($sales_data as $sale)
|
||||||
|
<tr>
|
||||||
|
<td>{{ $sale->date }}</td>
|
||||||
|
<td>{{ $sale->reference_no }}</td>
|
||||||
|
<td>{{ $sale->cashier }}</td>
|
||||||
|
<td>{{ $sale->customer }}</td>
|
||||||
|
<td class="text-right">{{ number_format($sale->grand_total, 2) }}</td>
|
||||||
|
<td class="text-right">{{ number_format($sale->cash, 2) }}</td>
|
||||||
|
<td class="text-right">{{ number_format($sale->card1, 2) }}</td>
|
||||||
|
<td class="text-right">{{ number_format($sale->card2, 2) }}</td>
|
||||||
|
<td class="text-right">{{ number_format($sale->card3, 2) }}</td>
|
||||||
|
<td class="text-right">{{ number_format($sale->online_transfer, 2) }}</td>
|
||||||
|
<td class="text-right">{{ number_format($sale->link_pay, 2) }}</td>
|
||||||
|
<td class="text-right">{{ number_format($sale->due_amount, 2) }}</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
@php
|
||||||
|
// ටේබල් එක යටින් පෙන්වන්න ඔක්කොම එකතු කරනවා
|
||||||
|
$tot_grand += $sale->grand_total;
|
||||||
|
$tot_cash += $sale->cash;
|
||||||
|
$tot_card1 += $sale->card1;
|
||||||
|
$tot_card2 += $sale->card2;
|
||||||
|
$tot_card3 += $sale->card3;
|
||||||
|
$tot_online += $sale->online_transfer;
|
||||||
|
$tot_link += $sale->link_pay;
|
||||||
|
$tot_due += $sale->due_amount;
|
||||||
|
@endphp
|
||||||
|
@endforeach
|
||||||
</tbody>
|
</tbody>
|
||||||
<tfoot style="background-color: #cdd4da; font-weight: bold;">
|
<tfoot style="background-color: #cdd4da; font-weight: bold;">
|
||||||
<tr>
|
<tr>
|
||||||
<td colspan="4" class="text-right">Total</td>
|
<td colspan="4" class="text-right">Total</td>
|
||||||
<td>0.00</td>
|
<td class="text-right">{{ number_format($tot_grand, 2) }}</td>
|
||||||
<td>0.00</td>
|
<td class="text-right">{{ number_format($tot_cash, 2) }}</td>
|
||||||
<td>0.00</td>
|
<td class="text-right">{{ number_format($tot_card1, 2) }}</td>
|
||||||
<td>0.00</td>
|
<td class="text-right">{{ number_format($tot_card2, 2) }}</td>
|
||||||
<td>0.00</td>
|
<td class="text-right">{{ number_format($tot_card3, 2) }}</td>
|
||||||
<td>0.00</td>
|
<td class="text-right">{{ number_format($tot_online, 2) }}</td>
|
||||||
<td>0.00</td>
|
<td class="text-right">{{ number_format($tot_link, 2) }}</td>
|
||||||
<td>0.00</td>
|
<td class="text-right">{{ number_format($tot_due, 2) }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
</tfoot>
|
</tfoot>
|
||||||
</table>
|
</table>
|
||||||
|
|||||||
Reference in New Issue
Block a user