/
home
/
sjslayjy
/
public_html
/
devlok
/
app
/
Exports
/
Upload File
HOME
<?php namespace App\Exports; use App\Order; use App\Location; use App\LoadingSlip; use Illuminate\Support\Collection; use Maatwebsite\Excel\Concerns\FromCollection; use Maatwebsite\Excel\Concerns\WithHeadings; class OrderExport implements FromCollection, WithHeadings { protected $from_date; protected $to_date; protected $order_from; protected $rake_points; protected $godowns; function __construct($from_date, $to_date, $order_from, $rake_points, $godowns) { $this->from_date = $from_date; $this->to_date = $to_date; $this->order_from = $order_from; $this->rake_points = $rake_points; $this->godowns = $godowns; } public function collection() { ini_set('memory_limit', '-1'); $orders = Order::query()->where('is_active', 1); if (!empty($this->from_date) && !empty($this->to_date)) { $orders->whereBetween('created_at', [$this->from_date, $this->to_date]); } if (!empty($this->order_from)) { $orders->where('order_from', $this->order_from); if ($this->order_from == 1 && !empty($this->rake_points)) { $orders->whereIn('rake_point', $this->rake_points); } elseif ($this->order_from == 2 && !empty($this->godowns)) { $orders->whereIn('from_warehouse_id', $this->godowns); } } $orders->orderBy('created_at', 'desc'); $ordersData = $orders->get(); $exportData = $ordersData->map(function ($order) { $dealer = getdealer($order->dealer_id); $retailer = getretailer($order->retailer_id); $dealerDestination = $dealer ? Location::where('location_id', $dealer->destination_code)->first() : null; $retailerDestination = $retailer ? Location::where('location_id', $retailer->destination_code)->first() : null; return [ 'Order Number' => $order->id, 'Order Date' => date('d/m/Y h:i A', strtotime($order->created_at)), 'Rake/Godown' => $order->order_from == 1 ? 'Rake (' . (getModelById("RakePoint", $order->rake_point)->rake_point ?? '') . ')' : 'Godown (' . (getModelById("Warehouse", $order->from_warehouse_id)->name ?? '') . ')', 'SAP Dealer ID' => $dealer->unique_id, 'Dealer Name' => $dealer->name ?? '', 'Retailer Name' => $retailer ? ($retailer->name . " [" . $order->retailer_id . "]") : '', 'Product Name' => getModelById('Product', $order->product_id)->name ?? '', 'Destination' => $order->retailer_address ?? ($dealer ? $dealerDestination->name ?? '' : ($retailer ? $retailerDestination->name ?? '' : '')), 'Qty' => $order->quantity, 'R Qty' => $order->remaining_qty == 0 ? '0' : $order->remaining_qty, 'Unit' => getModelById('Unit', $order->unit_id)->unit ?? '', 'Transporter' => implode(',', LoadingSlip::where('order_id', $order->id)->get()->map(function ($slip) { return getModelById('Transporter', $slip->transporter_id)->name ?? ''; })->toArray()), 'Truck No' => implode(',', LoadingSlip::where('order_id', $order->id)->pluck('vehicle_no')->toArray()), 'Invoice Date' => implode(',', LoadingSlip::where('order_id', $order->id)->where('slip_status', 'dispatched')->groupBy('order_id')->pluck('invoice_date')->map(function ($date) { return date('d/m/Y h:i A', strtotime($date)); })->toArray()), 'Order Status' => $order->order_status, 'Loading Status' => $order->remaining_qty > 0 ? 'Not Generated' : 'Genreated', 'Invoice Status' => $order->invoice_status ? 'Invoice Generated' : 'Invoice Pending' ]; }); return collect($exportData); } public function headings(): array { return [ 'Order Number', 'Order Date', 'Rake/Godown', 'SAP Dealer ID', 'Dealer Name', 'Retailer Name', 'Product Name', 'Destination', 'Qty', 'R Qty', 'Unit', 'Transporter', 'Truck No', 'Invoice Date', 'Order Status', 'Loading Status', 'Invoice Status' ]; } }