/** * @param $request * @return AB_Query */ public function createQuery($request) { $query = AB_Payment::query()->select('r.*, c.name customer, st.full_name provider, s.title service, ca.coupon_code coupon, a.start_date')->leftJoin('AB_CustomerAppointment', 'ca', 'ca.id = r.customer_appointment_id')->leftJoin('AB_Customer', 'c', 'c.id = ca.customer_id')->leftJoin('AB_Appointment', 'a', 'a.id = ca.appointment_id')->leftJoin('AB_Service', 's', 's.id = a.service_id')->leftJoin('AB_Staff', 'st', 'st.id = a.staff_id'); if (isset($request['type']) && $request['type'] != -1) { $query->where('r.type', $request['type']); } if (isset($request['customer']) && $request['customer'] != -1) { $query->where('c.name', $request['customer']); } if (isset($request['provider']) && $request['provider'] != -1) { $query->where('st.full_name', $request['provider']); } if (isset($request['service']) && $request['service'] != -1) { $query->where('s.title', $request['service']); } if (isset($request['range']) && !empty($request['range'])) { $dates = explode(' - ', $request['range'], 2); $start_date_timestamp = strtotime($dates[0]); $end_date_timestamp = strtotime($dates[1]); $start = date('Y-m-d', $start_date_timestamp); $end = date('Y-m-d', strtotime('+1 day', $end_date_timestamp)); $query->whereBetween('r.created', $start, $end); } if (!empty($request['sort_order']) && in_array($request['order_by'], array('created', 'type', 'customer', 'provider', 'service', 'total', 'start_date', 'coupon'))) { $query->sortBy($request['order_by'])->order($request['sort_order'] == 'desc' ? AB_Query::ORDER_DESCENDING : AB_Query::ORDER_ASCENDING); } return $query; }