99 lines
3.4 KiB
PHP
99 lines
3.4 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Exports;
|
||
|
|
|
||
|
|
use App\Models\Order;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use Maatwebsite\Excel\Concerns\FromCollection;
|
||
|
|
use Maatwebsite\Excel\Concerns\WithHeadings;
|
||
|
|
|
||
|
|
class OrdersExport implements FromCollection, WithHeadings
|
||
|
|
{
|
||
|
|
protected $request;
|
||
|
|
|
||
|
|
public function __construct(Request $request)
|
||
|
|
{
|
||
|
|
$this->request = $request;
|
||
|
|
}
|
||
|
|
|
||
|
|
private function buildQuery()
|
||
|
|
{
|
||
|
|
$query = Order::with(['markList', 'invoice', 'shipments']);
|
||
|
|
|
||
|
|
if ($this->request->filled('search')) {
|
||
|
|
$search = $this->request->search;
|
||
|
|
$query->where(function($q) use ($search) {
|
||
|
|
$q->where('order_id', 'like', "%{$search}%")
|
||
|
|
->orWhereHas('markList', function($q2) use ($search) {
|
||
|
|
$q2->where('company_name', 'like', "%{$search}%")
|
||
|
|
->orWhere('customer_id', 'like', "%{$search}%");
|
||
|
|
})
|
||
|
|
->orWhereHas('invoice', function($q3) use ($search) {
|
||
|
|
$q3->where('invoice_number', 'like', "%{$search}%");
|
||
|
|
});
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($this->request->filled('status')) {
|
||
|
|
$query->whereHas('invoice', function($q) {
|
||
|
|
$q->where('status', $this->request->status);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($this->request->filled('shipment')) {
|
||
|
|
$query->whereHas('shipments', function($q) {
|
||
|
|
$q->where('status', $this->request->shipment);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
return $query->latest('id');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function collection()
|
||
|
|
{
|
||
|
|
$orders = $this->buildQuery()->get();
|
||
|
|
|
||
|
|
// Map to simple array rows suitable for Excel
|
||
|
|
return $orders->map(function($order) {
|
||
|
|
$mark = $order->markList;
|
||
|
|
$invoice = $order->invoice;
|
||
|
|
$shipment = $order->shipments->first() ?? null;
|
||
|
|
|
||
|
|
return [
|
||
|
|
'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\Carbon::parse($invoice->invoice_date)->format('d-m-Y') : '-',
|
||
|
|
'Amount' => $invoice?->final_amount ? number_format($invoice->final_amount, 2) : '-',
|
||
|
|
'Amount + GST' => $invoice?->final_amount_with_gst ? number_format($invoice->final_amount_with_gst, 2) : '-',
|
||
|
|
'Invoice Status' => $invoice->status ? ucfirst($invoice->status) : 'Pending',
|
||
|
|
'Shipment Status' => $shipment?->status ? ucfirst(str_replace('_', ' ', $shipment->status)) : 'Pending',
|
||
|
|
];
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
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',
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|