month = (isset($request->month)) ? $request->query('month') : date('m'); $this->dealer = (isset($request->dealer)) ? $request->query('dealer') : 'all'; $this->sa = (isset($request->sa)) ? $request->query('sa') : 'all'; } /** * @return \Illuminate\Support\Collection */ public function collection() { $sa_work_trx = DB::statement("SET @sql = NULL"); $sql = "SELECT IF(work_id IS NOT NULL, GROUP_CONCAT(DISTINCT CONCAT('SUM(IF(work_id = \"', work_id,'\", qty,\"\")) AS \"',CONCAT(w.name, '|',w.id),'\"')), 's.work_id') INTO @sql FROM transactions t JOIN works w ON w.id = t.work_id WHERE month(t.date) = '". $this->month ."' and year(t.date) = '". date('Y') ."' and t.deleted_at is null"; if(isset($this->dealer) && $this->dealer != 'all') { $sql .= " and t.dealer_id = '". $this->dealer ."'"; } if(isset($this->sa) && $this->sa != 'all') { $sql .= " and t.user_sa_id = '". $this->sa ."'"; } $sa_work_trx = DB::statement($sql); if(isset($this->dealer) && $this->dealer != 'all') { if(isset($this->sa) && $this->sa != 'all') { $sa_work_trx = DB::statement("SET @sql = IF(@sql != 's.work_id' ,CONCAT(\"SELECT sa.name as SA, sa.id as sa_id, \", @sql, \"FROM transactions s JOIN users sa ON sa.id = s.user_sa_id WHERE month(s.date) = '". $this->month ."' and year(s.date) = '". date('Y') ."' and s.deleted_at is null and s.dealer_id = '". $this->dealer ."' and s.user_sa_id = '". $this->sa ."' GROUP BY s.user_sa_id ORDER BY s.user_sa_id ASC\"), CONCAT(\"SELECT sa.name as SA, sa.id as sa_id \", \"FROM transactions s JOIN users sa ON sa.id = s.user_sa_id WHERE month(s.date) = '". $this->month ."' and year(s.date) = '". date('Y') ."' and s.deleted_at is null and s.dealer_id = '". $this->dealer ."' and s.user_sa_id = '". $this->sa ."' GROUP BY s.`user_sa_id` ORDER BY s.`user_sa_id` ASC\"))"); }else{ $sa_work_trx = DB::statement("SET @sql = IF(@sql != 's.work_id' ,CONCAT(\"SELECT sa.name as SA, sa.id as sa_id, \", @sql, \"FROM transactions s JOIN users sa ON sa.id = s.user_sa_id WHERE month(s.date) = '". $this->month ."' and year(s.date) = '". date('Y') ."' and s.deleted_at is null and s.dealer_id = '". $this->dealer ."' GROUP BY s.user_sa_id ORDER BY s.user_sa_id ASC\"), CONCAT(\"SELECT sa.name as SA, sa.id as sa_id \", \"FROM transactions s JOIN users sa ON sa.id = s.user_sa_id WHERE month(s.date) = '". $this->month ."' and year(s.date) = '". date('Y') ."' and s.deleted_at is null and s.dealer_id = '". $this->dealer ."' GROUP BY s.`user_sa_id` ORDER BY s.`user_sa_id` ASC\"))"); } }else{ if(isset($this->sa) && $this->sa != 'all') { $sa_work_trx = DB::statement("SET @sql = IF(@sql != 's.work_id' ,CONCAT(\"SELECT sa.name as SA, sa.id as sa_id, \", @sql, \"FROM transactions s JOIN users sa ON sa.id = s.user_sa_id WHERE month(s.date) = '". $this->month ."' and year(s.date) = '". date('Y') ."' and s.deleted_at is null and s.user_sa_id = '". $this->sa ."' GROUP BY s.user_sa_id ORDER BY s.user_sa_id ASC\"), CONCAT(\"SELECT sa.name as SA, sa.id as user_sa_id \", \"FROM transactions s JOIN dealers d ON d.id = s.user_sa_id WHERE month(s.date) = '". $this->month ."' and year(s.date) = '". date('Y') ."' and s.deleted_at is null and s.user_sa_id = '". $this->sa ."' GROUP BY s.`user_sa_id` ORDER BY s.`user_sa_id` ASC\"))"); }else{ $sa_work_trx = DB::statement("SET @sql = IF(@sql != 's.work_id' ,CONCAT(\"SELECT sa.name as SA, sa.id as sa_id, \", @sql, \"FROM transactions s JOIN users sa ON sa.id = s.user_sa_id WHERE month(s.date) = '". $this->month ."' and year(s.date) = '". date('Y') ."' and s.deleted_at is null GROUP BY s.user_sa_id ORDER BY s.user_sa_id ASC\"), CONCAT(\"SELECT sa.name as SA, sa.id as user_sa_id \", \"FROM transactions s JOIN dealers d ON d.id = s.user_sa_id WHERE month(s.date) = '". $this->month ."' and year(s.date) = '". date('Y') ."' and s.deleted_at is null GROUP BY s.`user_sa_id` ORDER BY s.`user_sa_id` ASC\"))"); } } $sa_work_trx = DB::statement("PREPARE stmt FROM @sql"); $sa_work_trx = DB::select(DB::raw("EXECUTE stmt")); DB::statement('DEALLOCATE PREPARE stmt'); $work_trx = []; foreach($sa_work_trx as $index => $sa_work) { $sa_work_2 = (array) $sa_work; unset($sa_work_2['sa_id']); $work_names = array_keys($sa_work_2); if($index == 0) { $work_names_e = []; foreach($work_names as $work) { $arr_work = explode('|', $work); $work_names_e[] = $arr_work[0]; } $work_trx[] = $work_names_e; $work_trx[] = array_values($sa_work_2); }else{ $work_trx[] = array_values($sa_work_2); } } return collect($work_trx); } // public function headings(): array // { // return $this->theads; // } // public function actions(Request $request) // { // return [ // (new DownloadExcel)->withHeadings('Tanggal', 'Mekanik', 'SA', 'No.Polisi', 'Warranty', 'Kategori', 'Qty') // ]; // } }