data = $data; } public function collection() { $start_date = $this->data['start_date']; $end_date = $this->data['end_date']; $file_name = $this->data['file_name']; $resto_id = CommonMethods::getRestuarantID(); $orders = Orders::with(['restaurants','order_with_discounts','customers']) ->whereBetween('created_at',[$start_date,$end_date])->where('status','!=','Initial') ->where('resto_id',$resto_id) ->get(); $order_history = []; $status['Has_Delivered'] = 'Delivered'; $status['Placed'] = 'Placed'; $status['Accepted'] = 'Accepted'; $status['Cancelled'] = "Cancelled"; $status['Close'] = "Close"; $status['Rejected_by_User'] = "Rejected By User"; if(isset($orders) && $orders->count() > 0 ){ foreach($orders as $order){ $discount = isset($order->order_with_discounts)?$order->order_with_discounts->discounted_amount:0; $devliery_discount = isset($order->order_with_discounts)?$order->order_with_discounts->delivery_discount_value:0; $address = $order->selected_area_formatted; $lat = $lng = ""; if(empty($address)){ $address = isset($order->customers) && isset($order->customers->main_address)?$order->customers->main_address->address:""; if(empty($address)){ $address = $order->order_instructions; } } if($this->logo==""){ $this->logo = $order->restaurants->photos->file_name; } $orderStatus = null; if (array_key_exists(ucwords($order->status), $status)){ $orderStatus = $status[ucwords($order->status)]; }else{ $orderStatus=ucwords($order->status); } $order_history[] = array( 'business_name'=>isset($order->restaurants)?$order->restaurants->name:"", 'outlet_name'=>isset($order->outlet)?$order->outlet->name:"", 'order_id'=>str_pad($order->id,6,0,STR_PAD_LEFT), 'placed_at' => Carbon::parse($order->created_at)->format('l, d M Y'), 'order_status'=>$orderStatus, 'order_sub_total'=>number_format($order->total_price), 'discount'=> number_format($discount), 'delivery_fee'=>$order->delivery_fee, 'tax'=>0, 'order_total'=>($order->total_price - $discount) + ($order->delivery_fee - $devliery_discount), 'channel'=>$order->campaign_type!=""?$order->campaign_type:"Direct", 'delivery_date'=> $order->order_deliver_time, 'payment_mode'=>strtolower($order->payment_mode)=="cod"?"Cash on Delivery":"Card", 'customer_name'=>$order->customer_name, 'mobile_number'=>isset($order->customers) && isset($order->customers->users)?'+'.$order->customers->users->email:"", 'address'=> $address, 'latitude'=>'', 'longitude'=>'' ); } } // dd($order_history[0]); $r_order = array( 'file_name'=>$file_name.'.xlsx', 'report_type'=>'orders', 'start_date'=>$start_date, 'end_date'=>$end_date, 'is_data_found'=>count($order_history) > 0?"Yes":"No", 'status'=>'active', 'searched_by'=>Auth::id(), 'resto_id'=>$resto_id ); $r = StaticReports::where('start_date',$start_date)->whereNull('deleted_at')->where('end_date',$end_date)->where('report_type','orders')->where('resto_id',$resto_id)->first(); if(!$r){ $report = StaticReports::insert($r_order); } return collect($order_history); } public function headings(): array { return [ 'Business Name', 'Outlet Name', 'Order_ID', 'Placed at', 'Order Status', 'Order Sub Total', 'Discount', 'Delivery Fee', 'Tax', 'Order Total', 'Channel', 'Delivery Date', 'Payment Mode', 'Customer Name', 'Mobile Number', 'Address', 'Latitude', 'Longitude' ]; } public function columnFormats(): array { return [ 'N' => NumberFormat::FORMAT_TEXT ]; } }