コード例 #1
0
 /**
  * Display a listing of the resource.
  *
  * @return \Illuminate\Http\Response
  */
 public function index(Request $request)
 {
     if (Gate::denies('customer.read')) {
         return view(config('app.template') . '.error.403');
     }
     $type = $request->get('type') ? $request->get('type') : 'registered';
     if ($type == 'registered') {
         $customers = Customer::leftJoin(DB::raw("\n                            (SELECT orders.`id`, orders.`customer_id`, SUM(order_details.`harga_jual` * (order_details.`qty` - ifnull(order_detail_returns.qty, 0)))\n                            AS total_penjualan FROM orders INNER JOIN order_details ON orders.`id` = order_details.`order_id`\n                            LEFT JOIN order_detail_returns on order_details.id = order_detail_returns.order_detail_id\n                            WHERE orders.`state` = 'Closed' GROUP BY orders.`id`)AS temp_orders\n                        "), 'customers.id', '=', 'temp_orders.customer_id')->whereNotNull('customers.nama')->select(['customers.*', DB::raw('count(temp_orders.id) as jumlah_kunjungan'), DB::raw('sum(temp_orders.total_penjualan)as total')])->groupBy('customers.id')->paginate(2);
         $data = ['customers' => $customers];
         return view(config('app.template') . '.customer.table', $data);
     } elseif ($type == 'unregistered') {
         $customers = Customer::whereNull('nama')->get();
         $data = ['customers' => $customers];
         return view(config('app.template') . '.customer.table-empty', $data);
     } else {
         abort(404);
     }
 }