distinct('seksyen_kesalahan')->groupBy('seksyen_kesalahan')->get(); foreach ($compound as $key => $c) { $temp[] = $c->faulty; } }else { $dateS = Carbon::createFromFormat('Y-m-d', $start_date); $start = $dateS->copy()->startOfDay(); if($end_date != 'null'){ $dateE = Carbon::createFromFormat('Y-m-d', $end_date); $end = $dateE->copy()->endOfDay(); }else{ $end = $dateS->copy()->endOfDay(); } $compound = Compound::with('Faulty')->distinct('seksyen_kesalahan')->whereBetween('created_at', array($start, $end)) ->groupBy('seksyen_kesalahan')->get(); foreach ($compound as $key => $c) { $temp[] = $c->faulty; } } return $temp; } private function kadarKompaunDiBayar($id,$start_date,$end_date){ $total = 0; if($start_date == 'null'){ $compound = Compound::where('seksyen_kesalahan', $id)->where('status','Berbayar')->get(); foreach ($compound as $key => $c) { $total += $c->amount_payment; } }else{ $dateS = Carbon::createFromFormat('Y-m-d', $start_date); $start = $dateS->copy()->startOfDay(); if($end_date != 'null'){ $dateE = Carbon::createFromFormat('Y-m-d', $end_date); $end = $dateE->copy()->endOfDay(); }else{ $end = $dateS->copy()->endOfDay(); } $compound = Compound::with('Faulty')->where('seksyen_kesalahan', $id)->where('status','Berbayar') ->whereBetween('created_at', array($start, $end))->get(); foreach ($compound as $key => $c) { $total += ($c->Faulty->amount - $c->amount_payment); } } return $total; } private function kadarKompaunKurang($id,$start_date,$end_date){ $total = 0; if($start_date == 'null'){ $compound = Compound::with('Faulty')->where('seksyen_kesalahan', $id)->where('status','Berbayar')->get(); foreach ($compound as $key => $c) { $total += ($c->Faulty->amount - $c->amount_payment); } }else{ $dateS = Carbon::createFromFormat('Y-m-d', $start_date); $start = $dateS->copy()->startOfDay(); if($end_date != 'null'){ $dateE = Carbon::createFromFormat('Y-m-d', $end_date); $end = $dateE->copy()->endOfDay(); }else{ $end = $dateS->copy()->endOfDay(); } $compound = Compound::with('Faulty')->where('seksyen_kesalahan', $id)->where('status','Berbayar') ->whereBetween('created_at', array($start, $end))->get(); foreach ($compound as $key => $c) { $total += ($c->Faulty->amount - $c->amount_payment); } } return $total; } public function reportCompoundList($start_date, $end_date) { $nested_data = array(); $noFaulty = $this->compoundBasedFaulty($start_date, $end_date); if($start_date == 'null'){ foreach ($noFaulty as $key => $f) { $jumlah_kompaun = 0; $jumlah_kadar_kompaun = 0; $jumlah_kompaun_dibayar = 0; $jumlah_kadar_kompaun_dibayar = 0; $jumlah_kompaun_belum_bayar = 0; $jumlah_tunggakkan = 0; $jumlah_kompaun_kurg = 0; $jumlah_kadar_kompaun_kurg = 0; $jumlah_kompaun = count(Compound::where('seksyen_kesalahan', $f->_id)->get()); $jumlah_kadar_kompaun = floatval($f->amount) * $jumlah_kompaun; $jumlah_kompaun_dibayar = count(Compound::where('seksyen_kesalahan', $f->_id)->where('status','Berbayar')->get()); $jumlah_kompaun_belum_bayar = count(Compound::where('seksyen_kesalahan', $f->_id)->where('status','Belum Bayar')->get()); $jumlah_tunggakkan = ($jumlah_kompaun_belum_bayar - $jumlah_kompaun_dibayar) * floatval($f->amount); $jumlah_kompaun_kurg = count(Compound::where('seksyen_kesalahan', $f->_id)->where('jumlah_kemaskini_kompaun','!=','')->get()); $jumlah_kadar_kompaun_dibayar = $this->kadarKompaunDiBayar($f->_id,$start_date,$end_date); $jumlah_kadar_kompaun_kurg = $this->kadarKompaunKurang($f->_id,$start_date,$end_date); array_push($nested_data, array( 'kesalahan' => $f->nama, 'jumlahK' => $jumlah_kompaun, 'amaunK' => $jumlah_kadar_kompaun, 'jumlahB' => $jumlah_kompaun_dibayar, 'amaunB' => $jumlah_kadar_kompaun_dibayar, 'belumB' => $jumlah_kompaun_belum_bayar, 'amaunT' => $jumlah_tunggakkan, 'kBatal' => '0', 'amaunBatal' => '0', 'kompaunKurang' => $jumlah_kompaun_kurg, 'amaunKurang' => $jumlah_kadar_kompaun_kurg, )); } }else{ $dateS = Carbon::createFromFormat('Y-m-d', $start_date); $start = $dateS->copy()->startOfDay(); if($end_date != 'null'){ $dateE = Carbon::createFromFormat('Y-m-d', $end_date); $end = $dateE->copy()->endOfDay(); }else{ $end = $dateS->copy()->endOfDay(); } foreach ($noFaulty as $key => $f) { $jumlah_kompaun = 0; $jumlah_kadar_kompaun = 0; $jumlah_kompaun_dibayar = 0; $jumlah_kadar_kompaun_dibayar = 0; $jumlah_kompaun_belum_bayar = 0; $jumlah_tunggakkan = 0; $jumlah_kompaun_kurg = 0; $jumlah_kadar_kompaun_kurg = 0; $jumlah_kompaun = count(Compound::where('seksyen_kesalahan', $f->_id)->whereBetween('created_at', array($start, $end))->get()); $jumlah_kadar_kompaun = floatval($f->amount) * $jumlah_kompaun; $jumlah_kompaun_dibayar = count(Compound::where('seksyen_kesalahan', $f->_id)->where('status','Berbayar')->whereBetween('created_at', array($start, $end))->get()); $jumlah_kompaun_belum_bayar = count(Compound::where('seksyen_kesalahan', $f->_id)->where('status','Belum Bayar')->whereBetween('created_at', array($start, $end))->get()); $jumlah_tunggakkan = ($jumlah_kompaun_belum_bayar - $jumlah_kompaun_dibayar) * floatval($f->amount); $jumlah_kompaun_kurg = count(Compound::where('seksyen_kesalahan', $f->_id)->where('jumlah_kemaskini_kompaun','!=','')->whereBetween('created_at', array($start, $end))->get()); $jumlah_kadar_kompaun_dibayar = $this->kadarKompaunDiBayar($f->_id,$start_date,$end_date); $jumlah_kadar_kompaun_kurg = $this->kadarKompaunKurang($f->_id,$start_date,$end_date); array_push($nested_data, array( 'kesalahan' => $f->nama, 'jumlahK' => $jumlah_kompaun, 'amaunK' => $jumlah_kadar_kompaun, 'jumlahB' => $jumlah_kompaun_dibayar, 'amaunB' => $jumlah_kadar_kompaun_dibayar, 'belumB' => $jumlah_kompaun_belum_bayar, 'amaunT' => $jumlah_tunggakkan, 'kBatal' => '0', 'amaunBatal' => '0', 'kompaunKurang' => $jumlah_kompaun_kurg, 'amaunKurang' => $jumlah_kadar_kompaun_kurg, )); } } return \DataTables::of($nested_data)->addIndexColumn()->make(true); } }