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,296 +1,299 @@
@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') }}">
<div style="background: linear-gradient(to right, #00c1e3, #00489f); border-radius: 30px 30px 0px 0px; font-weight: bold; color: white;" <div style="background: linear-gradient(to right, #00c1e3, #00489f); border-radius: 30px 30px 0px 0px; font-weight: bold; color: white;"
class="card-header mt-2"> class="card-header mt-2">
<h3 class="text-center">Daily Sale Payment Report</h3> <h3 class="text-center">Daily Sale Payment Report</h3>
</div> </div>
<form action="{{ url('dailysalepaymentreportmodule') }}" method="GET"> <form action="{{ url('dailysalepaymentreportmodule') }}" method="GET">
<div class="row mr-1 mt-4 pl-4 align-items-center"> <div class="row mr-1 mt-4 pl-4 align-items-center">
<div class="col-md-4"> <div class="col-md-4">
<label><strong>Select Date</strong> &nbsp;</label> <label><strong>Select Date</strong> &nbsp;</label>
<div class="input-group"> <div class="input-group">
<input type="date" name="report_date" id="report_date" <input type="date" name="report_date" id="report_date"
value="{{ request()->input('report_date', date('Y-m-d')) }}" class="form-control" value="{{ request()->input('report_date', date('Y-m-d')) }}" class="form-control"
required /> required />
</div>
</div>
<div class="col-md-3 pt-md-4">
<button type="submit" class="btn btn-primary" id="generate-report-btn">Generate Report</button>
</div> </div>
</div> </div>
</form> <div class="col-md-3 pt-md-4">
</div> <button type="submit" class="btn btn-primary" id="generate-report-btn">Generate Report</button>
</div> </div>
<div class="container-fluid mt-4">
<!-- අලුතෙන් දාපු Single Export Buttons ටික -->
<div class="row mb-3">
<div class="col-md-12 text-right">
<button class="btn btn-danger" onclick="exportFullReport('pdf')" style="border-radius: 10px;">
<i class="fa fa-file-pdf-o"></i> Export PDF
</button>
<button class="btn btn-success" onclick="exportFullReport('excel')" style="border-radius: 10px;">
<i class="dripicons-document-new"></i> Export Excel
</button>
<button class="btn btn-info" onclick="exportFullReport('csv')" style="border-radius: 10px;">
<i class="fa fa-file-text-o"></i> Export CSV
</button>
<button class="btn btn-secondary" onclick="window.print()" style="border-radius: 10px;">
<i class="fa fa-print"></i> Print Report
</button>
</div> </div>
</form>
</div>
</div>
<div class="container-fluid mt-4">
<!-- අලුතෙන් දාපු Single Export Buttons ටික -->
<div class="row mb-3">
<div class="col-md-12 text-right">
<button class="btn btn-danger" onclick="exportFullReport('pdf')" style="border-radius: 10px;">
<i class="fa fa-file-pdf-o"></i> Export PDF
</button>
<button class="btn btn-success" onclick="exportFullReport('excel')" style="border-radius: 10px;">
<i class="dripicons-document-new"></i> Export Excel
</button>
<button class="btn btn-info" onclick="exportFullReport('csv')" style="border-radius: 10px;">
<i class="fa fa-file-text-o"></i> Export CSV
</button>
<button class="btn btn-secondary" onclick="window.print()" style="border-radius: 10px;">
<i class="fa fa-print"></i> Print Report
</button>
</div> </div>
</div>
<!-- Sales Table --> <!-- Sales Table -->
<h4 class="mb-3" style="color: #00489f; font-weight: bold;">Sales Table (Daily Sale)</h4> <h4 class="mb-3" style="color: #00489f; font-weight: bold;">Sales Table (Daily Sale)</h4>
<div class="table-responsive"> <div class="table-responsive">
<table style="border-radius: 15px; background-color: #e0e5ea;" class="table table-hover table-bordered"> <table style="border-radius: 15px; background-color: #e0e5ea;" class="table table-hover table-bordered">
<thead <thead
style="border-radius: 50px; background: linear-gradient(to right, #078bce, #1a1d1e); color: white;"> style="border-radius: 50px; background: linear-gradient(to right, #078bce, #1a1d1e); color: white;">
<tr> <tr>
<th>Date</th> <th>Date</th>
<th>Reference Number</th> <th>Reference Number</th>
<th>Cashier</th> <th>Cashier</th>
<th>Customer</th> <th>Customer</th>
<th>Grand Total</th> <th>Grand Total</th>
@foreach($paymentOptions as $option) @foreach($paymentOptions as $option)
<th>{{ $option->name }}</th> <th>{{ $option->name }}</th>
@endforeach
<th>Due Amount</th>
</tr>
</thead>
<tbody>
@php
$tot_grand = 0;
$tot_due = 0;
// Initialize dynamic option totals
$option_totals = [];
foreach ($paymentOptions as $opt) {
$option_totals[$opt->id] = 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>
@foreach($paymentOptions as $option)
<td class="text-right">{{ number_format($sale->methods[$option->id] ?? 0, 2) }}</td>
@endforeach
<td class="text-right">{{ number_format($sale->due_amount, 2) }}</td>
</tr>
@php
$tot_grand += $sale->grand_total;
$tot_due += $sale->due_amount;
// Accumulate dynamic option totals
foreach ($paymentOptions as $opt) {
$option_totals[$opt->id] += ($sale->methods[$opt->id] ?? 0);
}
@endphp
@endforeach @endforeach
</tbody> <th>Due Amount</th>
<tfoot style="background-color: #cdd4da; font-weight: bold;"> </tr>
<tr> </thead>
<td colspan="4" class="text-right">Total</td> <tbody>
<td class="text-right">{{ number_format($tot_grand, 2) }}</td> @php
$tot_grand = 0;
$tot_due = 0;
// Initialize dynamic option totals
$option_totals = [];
foreach ($paymentOptions as $opt) {
$option_totals[$opt->id] = 0;
}
@endphp
@foreach($paymentOptions as $option) @foreach ($sales_data as $sale)
<td class="text-right">{{ number_format($option_totals[$option->id], 2) }}</td> <tr>
@endforeach <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($tot_due, 2) }}</td> @foreach($paymentOptions as $option)
</tr> <td class="text-right">{{ number_format($sale->methods[$option->id] ?? 0, 2) }}</td>
</tfoot> @endforeach
</table>
</div> <td class="text-right">{{ number_format($sale->due_amount, 2) }}</td>
</tr>
@php
$tot_grand += $sale->grand_total;
$tot_due += $sale->due_amount;
// Accumulate dynamic option totals
foreach ($paymentOptions as $opt) {
$option_totals[$opt->id] += ($sale->methods[$opt->id] ?? 0);
}
@endphp
@endforeach
</tbody>
<tfoot style="background-color: #cdd4da; font-weight: bold;">
<tr>
<td colspan="4" class="text-right">Total</td>
<td class="text-right">{{ number_format($tot_grand, 2) }}</td>
@foreach($paymentOptions as $option)
<td class="text-right">{{ number_format($option_totals[$option->id], 2) }}</td>
@endforeach
<td class="text-right">{{ number_format($tot_due, 2) }}</td>
</tr>
</tfoot>
</table>
</div> </div>
</div>
<div class="container-fluid mt-5"> <div class="container-fluid mt-5">
<!-- Credit Sale Collections Table --> <!-- Credit Sale Collections Table -->
<h4 class="mb-3" style="color: #00489f; font-weight: bold;">Credit Sale Collections</h4> <h4 class="mb-3" style="color: #00489f; font-weight: bold;">Credit Sale Collections</h4>
<div class="table-responsive"> <div class="table-responsive">
<table style="border-radius: 15px; background-color: #e0e5ea;" class="table table-hover table-bordered"> <table style="border-radius: 15px; background-color: #e0e5ea;" class="table table-hover table-bordered">
<thead <thead
style="border-radius: 50px; background: linear-gradient(to right, #078bce, #1a1d1e); color: white;"> style="border-radius: 50px; background: linear-gradient(to right, #078bce, #1a1d1e); color: white;">
<tr> <tr>
<th>Original Bill Date</th> <th>Original Bill Date</th>
<th>Reference Number</th> <th>Reference Number</th>
<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> </tr>
<th>Online Transfer</th> </thead>
<th>Link Pay</th> <tbody>
</tr> @foreach ($creditCollections as $row)
</thead> <tr>
<tbody> <td>{{ \Carbon\Carbon::parse($row['date'])->format('Y-m-d') }}</td>
{{-- @foreach ($credit_data as $credit) --}} <td>{{ $row['reference'] }}</td>
<!-- Data rows will come here --> <td>{{ $row['cashier'] }}</td>
{{-- @endforeach --}} <td>{{ $row['customer'] }}</td>
</tbody> <td>{{ number_format($row['grand_total'], 2) }}</td>
<tfoot style="background-color: #cdd4da; font-weight: bold;"> @foreach ($paymentOptions as $option)
<tr> <td>{{ number_format($row['methods'][$option->id] ?? 0, 2) }}</td>
<td colspan="4" class="text-right">Total</td> @endforeach
<td>0.00</td> </tr>
<td>0.00</td> @endforeach
<td>0.00</td> </tbody>
<td>0.00</td> <tfoot style="background-color: #cdd4da; font-weight: bold;">
<td>0.00</td> <tr>
<td>0.00</td> <td colspan="4" class="text-right">Total</td>
<td>0.00</td> <td>0.00</td>
</tr> @foreach ($paymentOptions as $option)
</tfoot> <td>0.00</td>
</table> @endforeach
</div> </tr>
</tfoot>
</table>
</div> </div>
</div>
<div class="container-fluid mt-5 mb-5"> <div class="container-fluid mt-5 mb-5">
<!-- Payment Method Summary Table --> <!-- Payment Method Summary Table -->
<h4 class="mb-3" style="color: #00489f; font-weight: bold;">Payment Method Summary</h4> <h4 class="mb-3" style="color: #00489f; font-weight: bold;">Payment Method Summary</h4>
<div class="table-responsive"> <div class="table-responsive">
<table style="border-radius: 15px; background-color: #e0e5ea; width: 60%;" <table style="border-radius: 15px; background-color: #e0e5ea; width: 60%;"
class="table table-hover table-bordered"> class="table table-hover table-bordered">
<thead <thead
style="border-radius: 50px; background: linear-gradient(to right, #078bce, #1a1d1e); color: white;"> style="border-radius: 50px; background: linear-gradient(to right, #078bce, #1a1d1e); color: white;">
<tr> <tr>
<th>Payment Option</th> <th>Payment Option</th>
<th>Sale Amount</th> <th>Sale Amount</th>
<th>Received Amount</th> <th>Received Amount</th>
<th>Total Amount</th> <th>Total Amount</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr> <tr>
<td>Cash</td> <td>Cash</td>
<td>0.00</td> <td>0.00</td>
<td>0.00</td> <td>0.00</td>
<td>0.00</td> <td>0.00</td>
</tr> </tr>
<tr> <tr>
<td>Card 1</td> <td>Card 1</td>
<td>0.00</td> <td>0.00</td>
<td>0.00</td> <td>0.00</td>
<td>0.00</td> <td>0.00</td>
</tr> </tr>
<tr> <tr>
<td>Card 2</td> <td>Card 2</td>
<td>0.00</td> <td>0.00</td>
<td>0.00</td> <td>0.00</td>
<td>0.00</td> <td>0.00</td>
</tr> </tr>
<tr> <tr>
<td>Card 3</td> <td>Card 3</td>
<td>0.00</td> <td>0.00</td>
<td>0.00</td> <td>0.00</td>
<td>0.00</td> <td>0.00</td>
</tr> </tr>
<tr> <tr>
<td>Online Transfer</td> <td>Online Transfer</td>
<td>0.00</td> <td>0.00</td>
<td>0.00</td> <td>0.00</td>
<td>0.00</td> <td>0.00</td>
</tr> </tr>
<tr> <tr>
<td>Link Pay</td> <td>Link Pay</td>
<td>0.00</td> <td>0.00</td>
<td>0.00</td> <td>0.00</td>
<td>0.00</td> <td>0.00</td>
</tr> </tr>
</tbody> </tbody>
<tfoot style="background-color: #cdd4da; font-weight: bold;"> <tfoot style="background-color: #cdd4da; font-weight: bold;">
<tr> <tr>
<td class="text-right">Grand Total</td> <td class="text-right">Grand Total</td>
<td>0.00</td> <td>0.00</td>
<td>0.00</td> <td>0.00</td>
<td>0.00</td> <td>0.00</td>
</tr> </tr>
</tfoot> </tfoot>
</table> </table>
</div>
</div> </div>
</section> </div>
</section>
<style> <style>
.bootstrap-select.form-control, .bootstrap-select.form-control,
.form-control, .form-control,
.input-group-text { .input-group-text {
background-color: #fdfdff; background-color: #fdfdff;
border-color: #002250; border-color: #002250;
border-radius: 15px; border-radius: 15px;
} }
.btn-primary { .btn-primary {
background-color: #002250; background-color: #002250;
border-color: #002250; border-color: #002250;
border-radius: 15px; border-radius: 15px;
padding: 10px 25px; padding: 10px 25px;
} }
.btn-primary:hover { .btn-primary:hover {
background-color: #0068e3; background-color: #0068e3;
} }
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')
}
});
// මුළු රිපෝට් එකම එක්ස්පෝර්ට් කරන්න හදපු Function එක
function exportFullReport(exportType) {
var selectedDate = $('#report_date').val();
// ඔයාගේ Controller එකේ export route එකට මේක හරවන්න ඕනේ.
// උදාහරණයක් විදිහට: url('/dailysalepaymentreportmodule/export')
var exportUrl = "{{ url('/dailysalepaymentreportmodule/export') }}" + "?date=" + selectedDate + "&type=" + exportType;
window.location.href = exportUrl;
} }
</script> });
// මුළු රිපෝට් එකම එක්ස්පෝර්ට් කරන්න හදපු Function එක
function exportFullReport(exportType) {
var selectedDate = $('#report_date').val();
// ඔයාගේ Controller එකේ export route එකට මේක හරවන්න ඕනේ.
// උදාහරණයක් විදිහට: url('/dailysalepaymentreportmodule/export')
var exportUrl = "{{ url('/dailysalepaymentreportmodule/export') }}" + "?date=" + selectedDate + "&type=" + exportType;
window.location.href = exportUrl;
}
</script>
@endpush @endpush