saleTable n credit sale collection

This commit is contained in:
SUM2046
2026-07-03 15:37:24 +05:30
parent ad9c6f93b4
commit b7b2ba7b56
2 changed files with 354 additions and 271 deletions

View File

@@ -23,6 +23,8 @@ class DailySalePaymentReportModuleController extends Controller
->orderBy('id') ->orderBy('id')
->get(['id', 'name']); ->get(['id', 'name']);
$creditCollections = $this->getCreditSaleCollections($report_date);
$sales = DB::table('sales') $sales = DB::table('sales')
->leftJoin('users', 'sales.user_id', '=', 'users.id') ->leftJoin('users', 'sales.user_id', '=', 'users.id')
@@ -95,7 +97,85 @@ class DailySalePaymentReportModuleController extends Controller
]; ];
} }
return view('dailysalepaymentreportmodule::index', compact('sales_data', 'report_date', 'paymentOptions')); return view('dailysalepaymentreportmodule::index', compact('sales_data', 'report_date', 'paymentOptions', 'creditCollections'));
}
private function getCreditSaleCollections($date)
{
$paymentsTable = DB::table('payments')
->whereNotNull('sale_id')
->whereDate('created_at', $date)
->get(['id', 'sale_id', 'amount', 'payment_option_id']);
$usedPaymentIds = $paymentsTable->pluck('id')->all();
$salesPaymentsTable = DB::table('sales_payments')
->whereDate('created_at', $date)
->where(function ($q) use ($usedPaymentIds) {
$q->whereNull('payment_id');
if (!empty($usedPaymentIds)) {
$q->orWhereNotIn('payment_id', $usedPaymentIds);
}
})
->get(['id', 'sale_id', 'amount', 'payment', 'payment_id']);
$linkedPaymentIds = $salesPaymentsTable->whereNotNull('payment_id')->pluck('payment_id')->unique();
$linkedOptionMap = DB::table('payments')
->whereIn('id', $linkedPaymentIds)
->pluck('payment_option_id', 'id');
$saleIds = $paymentsTable->pluck('sale_id')
->merge($salesPaymentsTable->pluck('sale_id'))
->unique()
->values();
if ($saleIds->isEmpty()) {
return collect();
}
$sales = DB::table('sales')
->whereIn('id', $saleIds)
->whereDate('created_at', '<', $date)
->select('id', 'reference_no', 'user_id', 'customer_id', 'grand_total', 'paid_amount', 'created_at')
->get()
->keyBy('id');
if ($sales->isEmpty()) {
return collect();
}
$users = DB::table('users')->whereIn('id', $sales->pluck('user_id')->unique())->pluck('name', 'id');
$customers = DB::table('customers')->whereIn('id', $sales->pluck('customer_id')->unique())->pluck('name', 'id');
$rows = [];
foreach ($sales as $saleId => $sale) {
$methodTotals = [];
foreach ($paymentsTable->where('sale_id', $saleId) as $p) {
$optionId = $p->payment_option_id;
$methodTotals[$optionId] = ($methodTotals[$optionId] ?? 0) + $p->amount;
}
foreach ($salesPaymentsTable->where('sale_id', $saleId) as $sp) {
$optionId = $sp->payment_id ? ($linkedOptionMap[$sp->payment_id] ?? null) : null;
if ($optionId) {
$methodTotals[$optionId] = ($methodTotals[$optionId] ?? 0) + $sp->amount;
}
}
$rows[] = [
'date' => $sale->created_at,
'reference' => $sale->reference_no,
'cashier' => $users[$sale->user_id] ?? '-',
'customer' => $customers[$sale->customer_id] ?? '-',
'grand_total' => $sale->grand_total - $sale->paid_amount,
'methods' => $methodTotals,
];
}
return collect($rows);
} }
/** /**

View File

@@ -1,28 +1,28 @@
@extends('backend.layout.main') @extends('backend.layout.main')
@section('content') @section('content')
@if ($errors->has('title')) @if ($errors->has('title'))
<div class="alert alert-danger alert-dismissible text-center"> <div class="alert alert-danger alert-dismissible text-center">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span
aria-hidden="true">&times;</span></button>{{ $errors->first('title') }} aria-hidden="true">&times;</span></button>{{ $errors->first('title') }}
</div> </div>
@endif @endif
@if (session()->has('message')) @if (session()->has('message'))
<div class="alert alert-success alert-dismissible text-center"> <div class="alert alert-success alert-dismissible text-center">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"> <button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span> <span aria-hidden="true">&times;</span>
</button>{{ session()->get('message') }} </button>{{ session()->get('message') }}
</div> </div>
@endif @endif
@if (session()->has('not_permitted')) @if (session()->has('not_permitted'))
<div class="alert alert-danger alert-dismissible text-center"> <div class="alert alert-danger alert-dismissible text-center">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"> <button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span> <span aria-hidden="true">&times;</span>
</button>{{ session()->get('not_permitted') }} </button>{{ session()->get('not_permitted') }}
</div> </div>
@endif @endif
<section> <section>
<div class="container-fluid"> <div class="container-fluid">
<div style="border-radius: 30px;" class="card pb-4 "> <div style="border-radius: 30px;" class="card pb-4 ">
<link rel="stylesheet" href="{{ asset('public/css/cloudma.css') }}"> <link rel="stylesheet" href="{{ asset('public/css/cloudma.css') }}">
@@ -152,29 +152,32 @@
<th>Cashier</th> <th>Cashier</th>
<th>Customer</th> <th>Customer</th>
<th>Grand Total</th> <th>Grand Total</th>
<th>Cash</th> @foreach ($paymentOptions as $option)
<th>Card 1</th> <th>{{ $option->name }}</th>
<th>Card 2</th> @endforeach
<th>Card 3</th>
<th>Online Transfer</th>
<th>Link Pay</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{{-- @foreach ($credit_data as $credit) --}} @foreach ($creditCollections as $row)
<!-- Data rows will come here --> <tr>
{{-- @endforeach --}} <td>{{ \Carbon\Carbon::parse($row['date'])->format('Y-m-d') }}</td>
<td>{{ $row['reference'] }}</td>
<td>{{ $row['cashier'] }}</td>
<td>{{ $row['customer'] }}</td>
<td>{{ number_format($row['grand_total'], 2) }}</td>
@foreach ($paymentOptions as $option)
<td>{{ number_format($row['methods'][$option->id] ?? 0, 2) }}</td>
@endforeach
</tr>
@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>0.00</td>
@foreach ($paymentOptions as $option)
<td>0.00</td> <td>0.00</td>
<td>0.00</td> @endforeach
<td>0.00</td>
<td>0.00</td>
<td>0.00</td>
<td>0.00</td>
</tr> </tr>
</tfoot> </tfoot>
</table> </table>
@@ -245,9 +248,9 @@
</table> </table>
</div> </div>
</div> </div>
</section> </section>
<style> <style>
.bootstrap-select.form-control, .bootstrap-select.form-control,
.form-control, .form-control,
.input-group-text { .input-group-text {
@@ -270,12 +273,12 @@
table { table {
border-collapse: collapse !important; border-collapse: collapse !important;
} }
</style> </style>
@endsection @endsection
@push('scripts') @push('scripts')
<script type="text/javascript"> <script type="text/javascript">
$.ajaxSetup({ $.ajaxSetup({
headers: { headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
@@ -292,5 +295,5 @@
window.location.href = exportUrl; window.location.href = exportUrl;
} }
</script> </script>
@endpush @endpush