123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- namespace App\Exports;
- use App\Helpers\CommonMethods;
- use App\Models\Orders;
- use App\Models\StaticReports;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Auth;
- use Maatwebsite\Excel\Concerns\FromCollection;
- use Maatwebsite\Excel\Concerns\ShouldAutoSize;
- use Maatwebsite\Excel\Concerns\WithColumnFormatting;
- use Maatwebsite\Excel\Concerns\WithHeadings;
- use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
- use PhpOffice\PhpSpreadsheet\Style\Style;
- class OrderHistoryExport implements FromCollection, WithHeadings,ShouldAutoSize,WithColumnFormatting
- {
- /**
- * @return \Illuminate\Support\Collection
- */
- private $data = null;
- private $logo = "";
- public function __construct($data){
- $this->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
- ];
- }
- }
|