2025-12-03 10:35:20 +05:30
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Exports;
|
|
|
|
|
|
|
|
|
|
use App\Models\Order;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Maatwebsite\Excel\Concerns\FromCollection;
|
|
|
|
|
use Maatwebsite\Excel\Concerns\WithHeadings;
|
2025-12-19 11:12:06 +05:30
|
|
|
use Carbon\Carbon;
|
2025-12-03 10:35:20 +05:30
|
|
|
|
|
|
|
|
class OrdersExport implements FromCollection, WithHeadings
|
|
|
|
|
{
|
2025-12-19 11:12:06 +05:30
|
|
|
protected Request $request;
|
2025-12-03 10:35:20 +05:30
|
|
|
|
|
|
|
|
public function __construct(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$this->request = $request;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function buildQuery()
|
|
|
|
|
{
|
2025-12-19 11:12:06 +05:30
|
|
|
$query = Order::query()->with([
|
|
|
|
|
'markList',
|
|
|
|
|
'invoice',
|
|
|
|
|
'shipments',
|
|
|
|
|
]);
|
2025-12-03 10:35:20 +05:30
|
|
|
|
2025-12-19 11:12:06 +05:30
|
|
|
// SEARCH
|
2025-12-03 10:35:20 +05:30
|
|
|
if ($this->request->filled('search')) {
|
2025-12-19 11:12:06 +05:30
|
|
|
$search = trim($this->request->search);
|
|
|
|
|
|
|
|
|
|
$query->where(function ($q) use ($search) {
|
|
|
|
|
$q->where('orders.order_id', 'like', "%{$search}%")
|
|
|
|
|
->orWhereHas('markList', function ($q2) use ($search) {
|
2025-12-03 10:35:20 +05:30
|
|
|
$q2->where('company_name', 'like', "%{$search}%")
|
2025-12-19 11:12:06 +05:30
|
|
|
->orWhere('customer_id', 'like', "%{$search}%")
|
|
|
|
|
->orWhere('origin', 'like', "%{$search}%")
|
|
|
|
|
->orWhere('destination', 'like', "%{$search}%");
|
2025-12-03 10:35:20 +05:30
|
|
|
})
|
2025-12-19 11:12:06 +05:30
|
|
|
->orWhereHas('invoice', function ($q3) use ($search) {
|
2025-12-03 10:35:20 +05:30
|
|
|
$q3->where('invoice_number', 'like', "%{$search}%");
|
2025-12-19 11:12:06 +05:30
|
|
|
})
|
|
|
|
|
->orWhereHas('shipments', function ($q4) use ($search) {
|
|
|
|
|
// ✅ FIXED
|
|
|
|
|
$q4->where('shipments.shipment_id', 'like', "%{$search}%");
|
2025-12-03 10:35:20 +05:30
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-19 11:12:06 +05:30
|
|
|
// INVOICE STATUS
|
|
|
|
|
// INVOICE STATUS (FIXED)
|
2025-12-03 10:35:20 +05:30
|
|
|
if ($this->request->filled('status')) {
|
2025-12-19 11:12:06 +05:30
|
|
|
$query->where(function ($q) {
|
|
|
|
|
$q->whereHas('invoice', function ($q2) {
|
|
|
|
|
$q2->where('status', $this->request->status);
|
|
|
|
|
})
|
|
|
|
|
->orWhereDoesntHave('invoice');
|
2025-12-03 10:35:20 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-19 11:12:06 +05:30
|
|
|
|
|
|
|
|
// SHIPMENT STATUS (FIXED)
|
2025-12-03 10:35:20 +05:30
|
|
|
if ($this->request->filled('shipment')) {
|
2025-12-19 11:12:06 +05:30
|
|
|
$query->where(function ($q) {
|
|
|
|
|
$q->whereHas('shipments', function ($q2) {
|
|
|
|
|
$q2->where('status', $this->request->shipment);
|
|
|
|
|
})
|
|
|
|
|
->orWhereDoesntHave('shipments');
|
2025-12-03 10:35:20 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-19 11:12:06 +05:30
|
|
|
|
|
|
|
|
// DATE RANGE
|
|
|
|
|
if ($this->request->filled('from_date')) {
|
|
|
|
|
$query->whereDate('orders.created_at', '>=', $this->request->from_date);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($this->request->filled('to_date')) {
|
|
|
|
|
$query->whereDate('orders.created_at', '<=', $this->request->to_date);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $query->latest('orders.id');
|
2025-12-03 10:35:20 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function collection()
|
|
|
|
|
{
|
2025-12-19 11:12:06 +05:30
|
|
|
return $this->buildQuery()->get()->map(function ($order) {
|
2025-12-03 10:35:20 +05:30
|
|
|
|
2025-12-19 11:12:06 +05:30
|
|
|
$mark = $order->markList;
|
|
|
|
|
$invoice = $order->invoice;
|
|
|
|
|
$shipment = $order->shipments->first();
|
2025-12-03 10:35:20 +05:30
|
|
|
|
|
|
|
|
return [
|
2025-12-19 11:12:06 +05:30
|
|
|
'Order ID' => $order->order_id ?? '-',
|
|
|
|
|
'Shipment ID' => $shipment?->shipment_id ?? '-',
|
|
|
|
|
'Customer ID' => $mark?->customer_id ?? '-',
|
|
|
|
|
'Company' => $mark?->company_name ?? '-',
|
|
|
|
|
'Origin' => $mark?->origin ?? $order->origin ?? '-',
|
|
|
|
|
'Destination' => $mark?->destination ?? $order->destination ?? '-',
|
|
|
|
|
'Order Date' => $order->created_at
|
|
|
|
|
? $order->created_at->format('d-m-Y')
|
|
|
|
|
: '-',
|
|
|
|
|
'Invoice No' => $invoice?->invoice_number ?? '-',
|
|
|
|
|
'Invoice Date' => $invoice?->invoice_date
|
|
|
|
|
? Carbon::parse($invoice->invoice_date)->format('d-m-Y')
|
|
|
|
|
: '-',
|
|
|
|
|
'Amount' => $invoice?->final_amount !== null
|
|
|
|
|
? number_format($invoice->final_amount, 2)
|
|
|
|
|
: '0.00',
|
|
|
|
|
'Amount + GST' => $invoice?->final_amount_with_gst !== null
|
|
|
|
|
? number_format($invoice->final_amount_with_gst, 2)
|
|
|
|
|
: '0.00',
|
|
|
|
|
'Invoice Status' => ucfirst($invoice?->status ?? 'pending'),
|
|
|
|
|
'Shipment Status' => ucfirst(str_replace('_', ' ', $shipment?->status ?? 'pending')),
|
2025-12-03 10:35:20 +05:30
|
|
|
];
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function headings(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'Order ID',
|
|
|
|
|
'Shipment ID',
|
|
|
|
|
'Customer ID',
|
|
|
|
|
'Company',
|
|
|
|
|
'Origin',
|
|
|
|
|
'Destination',
|
|
|
|
|
'Order Date',
|
|
|
|
|
'Invoice No',
|
|
|
|
|
'Invoice Date',
|
|
|
|
|
'Amount',
|
|
|
|
|
'Amount + GST',
|
|
|
|
|
'Invoice Status',
|
|
|
|
|
'Shipment Status',
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|