AdminController.php 10.1 KB
Newer Older
1 2 3 4
<?php

namespace App\Http\Controllers\Admin;

Siti Aisah committed
5 6
use App\Http\Support\ValidationRule;
use Illuminate\Support\Facades\Hash;
7
use App\Http\Controllers\Controller;
Siti Aisah committed
8
use App\Models\Kegiatan;
Siti Aisah committed
9
use App\Models\KegiatanPeserta;
Siti Aisah committed
10
use App\Models\Konferensi;
11
use App\Models\Registrasi;
Siti Aisah committed
12
use App\Models\User;
Siti Aisah committed
13
use App\Models\VRegistrasi;
14
use Carbon\Carbon;
15 16 17 18 19 20 21
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class AdminController extends Controller
{
Siti Aisah committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    public function index() {
        return view('home');
    }

    public function index_user() {
        $user = User::get();
        $data = [
            'user' => $user,
        ];

        return view('admin.user.index', $data);
    }

    public function store_user(Request $request) {

        $rules = [
            'name' => 'required|string',
            'email' => 'required|email',
            'password' => 'string|min:8',
        ];

        $request->validate($rules, ValidationRule::getErrorMessage($rules));

        DB::beginTransaction();

        try{
            $password = Hash::make($request->password);
            $data = [
                'name' => strip_tags($request->name),
                'email' => strip_tags($request->email),
                'password' => $password
            ];

            User::query()->create($data);
            DB::commit();

            return redirect()->back()
            ->with('success', 'Data saved successfully');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return redirect()->back()
                ->with('error', 'Data failed to save');
        }
    }

    public function update_user($id, Request $request){

        DB::beginTransaction();

        try{
            if($request->password){
                $password = Hash::make($request->password);
            }
            else{
                $user = User::query()->where('id', $id)->first();
                $password = $user->password;
            }

            $data = [
                'name' => strip_tags($request->name),
                'email' => strip_tags($request->email),
                'password' => $password
            ];
            User::query()->where('id', $id)->update($data);
            DB::commit();

            return redirect()->back()
                ->with('success', 'Data saved successfully');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return redirect()->back()
                ->with('error', 'Data failed to update');
        }
    }

    public function delete_user($id){
        DB::beginTransaction();

        try{
            User::find($id)->delete();
            DB::commit();

            return redirect()->back()
                ->with('success', 'Data deleted successfully');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return redirect()->back()
                ->with('error', 'Data failed to delete');
        }
    }

    public function index_kegiatan() {
        $kegiatan = Kegiatan::get();
        $data = [
            'kegiatan' => $kegiatan
        ];

        return view('admin.kegiatan.index', $data);
    }

    public function store_kegiatan(Request $request) {
        $rules = [
            'nama' => 'required|string',
            'harga' => 'required|numeric'
        ];

        $request->validate($rules, ValidationRule::getErrorMessage($rules));

        DB::beginTransaction();

        try{
            $data = [
Siti Aisah committed
143
                'nama' => strip_tags($request->nama),
Siti Aisah committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
                'harga' => $request->harga
            ];
            Kegiatan::query()->create($data);
            DB::commit();

            return redirect()->back()
                ->with('success', 'Data saved successfully');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return redirect()->back()
                ->with('error', 'Data failed to create');
        }
    }

    public function update_kegiatan($id, Request $request) {
        $rules = [
            'nama' => 'required|string',
            'harga' => 'required|numeric'
        ];

        $request->validate($rules, ValidationRule::getErrorMessage($rules));

        DB::beginTransaction();

        try{
            $data = [
                'nama' => $request->nama,
                'harga' => $request->harga
            ];

            Kegiatan::query()->where('id', $id)->update($data);
            DB::commit();

            return redirect()->back()
                ->with('success', 'Data saved successfully');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return redirect()->back()
                ->with('error', 'Data failed to update');
        }
    }

    public function delete_kegiatan($id) {
        DB::beginTransaction();
        try{
            Kegiatan::find($id)->delete();
            DB::commit();

            return redirect()->back()
                ->with('success', 'Data deleted successfully');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return redirect()->back()
                ->with('error', 'Data failed to delete');
        }
    }

    public function index_konferensi() {
        $konferensi = Konferensi::get();
        $data = [
            'konferensi' => $konferensi
        ];

        return view('admin.konferensi.index', $data);
    }

    public function store_konferensi(Request $request) {
        $rules = [
Siti Aisah committed
221 222
            'nama' => 'required|string',
            'harga' => 'required|numeric'
Siti Aisah committed
223 224 225 226 227 228 229 230
        ];

        $request->validate($rules, ValidationRule::getErrorMessage($rules));

        DB::beginTransaction();

        try{
            $data = [
Siti Aisah committed
231 232
                'nama' => strip_tags($request->nama),
                'harga' => $request->harga
Siti Aisah committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
            ];
            Konferensi::query()->create($data);
            DB::commit();

            return redirect()->back()
                ->with('success', 'Data saved successfully');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return redirect()->back()
                ->with('error', 'Data failed to update');
        }
    }

    public function update_konferensi($id, Request $request) {
        $rules = [
Siti Aisah committed
251 252
            'nama' => 'required|string',
            'harga' => 'required|numeric'
Siti Aisah committed
253 254 255 256 257 258 259 260
        ];

        $request->validate($rules, ValidationRule::getErrorMessage($rules));

        DB::beginTransaction();

        try{
            $data = [
Siti Aisah committed
261 262
                'nama' => $request->nama,
                'harga' => $request->harga
Siti Aisah committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
            ];

            Konferensi::query()->where('id', $id)->update($data);
            DB::commit();

            return redirect()->back()
                ->with('success', 'Data saved successfully');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return redirect()->back()
                ->with('error', 'Data failed to update');
        }
    }

    public function delete_konferensi($id) {
        DB::beginTransaction();
        try{
            Konferensi::find($id)->delete();
            DB::commit();

            return redirect()->back()
                ->with('success', 'Data deleted successfully');
        }
        catch(Exception $e){
            Log::error($e);
            DB::rollBack();

            return redirect()->back()
                ->with('error', 'Data failed to delete');
        }
    }

298
    public function index_peserta(){
Siti Aisah committed
299 300
        // $peserta = Registrasi::whereHas('pkRegistrasiPeserta')->get();
        $peserta = VRegistrasi::orderby('created_at', 'DESC')->get();
301 302 303 304 305 306 307
        $data = [
            'peserta' => $peserta
        ];

        return view('admin.peserta.index', $data);
    }

Siti Aisah committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
    // public function delete_peserta($id) {
    //     DB::beginTransaction();
    //     try{
    //         KegiatanPeserta::where('id_registrasi',$id)->delete();
    //         Registrasi::where('id',$id)->delete();

    //         DB::commit();

    //         return redirect()->back()
    //             ->with('success', 'Data deleted successfully');
    //     }
    //     catch(Exception $e){
    //         Log::error($e);
    //         DB::rollBack();

    //         return redirect()->back()
    //             ->with('error', 'Data failed to delete');
    //     }
    // }
Siti Aisah committed
327 328 329 330 331 332 333 334 335

    public function download_peserta()
    {
        date_default_timezone_set("Asia/Jakarta");
        $data['peserta']    =  VRegistrasi::orderby('created_at','DESC')->get();
        $data['nama_file'] = 'daftar peserta konaspi'." ".date('Y-m-d');

        return view('admin.peserta.excel', $data);
    }
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369

    public function index_pembayaran(){
        $pembayaranVa = DB::connection('va')->table('neosidata.v_pembayaran_konaspi')->orderBy('va')->get();

        foreach ($pembayaranVa as $key => $value) {
            $nomorVa[] = $value->va;
            $konaspi = Registrasi::where('nomor_va', $value->va)->first();
            $date = Carbon::createFromFormat('dmy', $value->tgl)->format('Y-m-d');

            $data[$key] = [
                'konaspi' => $konaspi,
                'va' => $value,
                'tglbayar' => $date
            ];
        }

        return view('admin.pembayaran.index', compact('data'));
    }

    public function download_data_pembayaran()
    {
        date_default_timezone_set("Asia/Jakarta");
        $pembayaranVa = DB::connection('va')->table('neosidata.v_pembayaran_konaspi')->orderBy('va')->get();

        foreach ($pembayaranVa as $key => $value) {
            $nomorVa[] = $value->va;
            $konaspi = Registrasi::where('nomor_va', $value->va)->first();
            $date = Carbon::createFromFormat('dmy', $value->tgl)->format('Y-m-d');

            $data[$key] = [
                'konaspi' => $konaspi,
                'va' => $value,
                'tglbayar' => $date
            ];
Triyah Fatmawati committed
370 371

            $nama_file = 'Data Pembayaran Konaspi '." ".date('Y-m-d');
372 373
        }

Triyah Fatmawati committed
374
        return view('admin.pembayaran.excel', compact('data', 'nama_file'));
375
    }
376
}