CustomerController.php 17.4 KB
Newer Older
1 2 3 4
<?php

namespace App\Http\Controllers;

Juliper committed
5
use App\Feedback;
Juliper committed
6
use App\Homestay;
Juliper committed
7
use App\ListBook;
Juliper committed
8
use App\RequestHomestay;
Juliper committed
9 10
use App\Room;
use App\Transaksi;
11 12 13 14 15
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\User;
use App\Customer;
use Illuminate\Support\Facades\DB;
Juliper committed
16
use Illuminate\Contracts\Auth\Guard;
Juliper committed
17 18 19
use DateTime;
use PhpParser\Node\Stmt\Const_;
use Psy\Command\ListCommand\ConstantEnumerator;
Juliper committed
20 21 22 23
use Illuminate\Notifications\Messages\NexmoMessage;
use SimpleSoftwareIO\SMS\Drivers\NexmoSMS;
use Nexmo\Laravel\Facade\Nexmo;

24 25 26

class CustomerController extends Controller
{
27

Juliper committed
28 29 30 31
    public function __construct(){
        $this->middleware('customer');
    }

Juliper committed
32
    //Pembatalan Pemesanan/Pembookingan Homestay
Juliper committed
33 34
    public function cancelPemesanan($id){

Juliper committed
35 36
        $dataTrsaksi = Transaksi::find($id);

Juliper committed
37 38 39 40 41 42 43
        $dataTrsaksi->status = 3;


        $dataTrsaksi->update();


        //$dataTrsaksi->delete();
Juliper committed
44 45 46 47

        DB::table('daftar_book')->where('id_transaksi', '=', $id)->delete();

        return redirect('customerHistory');
Juliper committed
48 49
    }

Juliper committed
50 51
    //Memperbaharui pesanan
    public function updatePesanan($id){
Juliper committed
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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
        $data = Transaksi::find($id);
        //$time = explode('-', $request['tanggal']);
        $tanggalMulai = explode('-',$data->tanggal_mulai);

        $tanggalBerakhir = explode('-',$data->tanggal_berakhir);

        $lamaMenginap = $tanggalBerakhir[2] - $tanggalMulai[2];

        //dd($lamaMenginap);



        return view('adminlte::layouts.customers.UpdatePemesanan')->with('data',$data)->with('lama_menginap',$lamaMenginap);
    }

    //Menimpan pesanan yang baru
    public function perbaharui(Request $request,$id){

        $dataTransaksi = DB::table('transaksi')
                         ->select('transaksi.*')
                         ->where('transaksi.id','=',$id)
                         ->get();

        $dataHomestay = DB::table('homestay')
            ->join('kamar','homestay.id','=','kamar.id_homestay')
            ->select('kamar.*','homestay.harga','homestay.jumlah_kamar')
            ->where('homestay.id','=',$dataTransaksi[0]->id_homestay)
            ->get();

        $homestayJumlahKamar = DB::table('homestay')
            ->select('homestay.jumlah_kamar')
            ->where('homestay.id','=',$dataTransaksi[0]->id_homestay)
            ->get();

        $dataBookingMulai = DB::table('daftar_book')
            ->select('daftar_book.*')
            ->where('daftar_book.tanggal_mulai','=',$request['tanggal'])
            ->get();

        $tanggal_baru = explode('-', $request['tanggal']);
        $tanggal_baru[2] +=$request['lama_menginap'];

        $tanggal = join('-',$tanggal_baru);

        $dataBookingBerakhir = DB::table('daftar_book')
            ->select('daftar_book.*')
            ->where('daftar_book.tanggal_berakhir','=',$tanggal)
            ->get();



        //dd($dataTransaksi[0],$dataHomestay[0],$dataPemilik[0],$homestayJumlahKamar[0],$dataBookingBerakhir[0],$dataBookingMulai[0]);

        if($dataHomestay[0]->jumlah_kamar < $request['jumlah_kamar']){
            return redirect('updatepesanan/',$id)->with('message','Maaf Jumalh kamar tidak  sesuai');
        }else {
            if ($dataBookingMulai->count() > 0) {
                $currentJumlahKamar = $homestayJumlahKamar[0]->jumlah_kamar - $dataBookingMulai[0]->jumlah_kamar;

                if ($currentJumlahKamar < $request['jumlah_kamar']) {
                    return redirect()->action('GuestController@detailhomestay', ['id' => $request['id']])->with('message', 'Maaf Anda Tidak Bisa Melakukan Pembookingan di karenakan kamar yang tersisa tidak sesuai dengan permintaan anda!');
                }

            } else {
                if ($dataBookingBerakhir->count() > 0) {
                    $currentJumlahKamar2 = $homestayJumlahKamar[0]->jumlah_kamar - $dataBookingBerakhir[0]->jumlah_kamar;
                    if ($currentJumlahKamar2 < $request['jumlah_kamar']) {
                        return redirect()->action('GuestController@detailhomestay', ['id' => $request['id']])->with('message', 'Maaf Anda Tidak Bisa Melakukan Pembookingan di karenakan kamar yang tersisa tidak sesuai dengan permintaan anda!');
                    }
                }
            }

            $time = explode('-', $request['tanggal']);

            if ($time[1] == 2) {
                $time[2] += $request['lama_menginap'];
                if ($time[2] > 28) {
                    $time[2] = 0;
                    $time[2] += $request['lama_menginap'];
                    $time[2] -= 1;
                    $time[1] += 1;
                    if ($time[1] > 12) {
                        $time[1] = 1;
                    }
                }
            } else {
                if ($time[1] % 2 == 1) {
                    $time[2] += $request['lama_menginap'];
                    if ($time[2] > 31) {
                        if ($request['lama_menginap'] != 1) {
                            $time[2] = 0;
                            $time[2] += $request['lama_menginap'];
                            $time[1] += 1;
                        }
                    }
                    //$time[2] -=1;
                } else {
                    $time[2] += $request['lama_menginap'];
                    if ($time[2] > 30) {
                        if ($request['lama_menginap'] != 1) {
                            $time[2] = 0;
                            $time[2] += $request['lama_menginap'];
                            $time[1] += 1;
                        }
                        //$time[2] -=1;
                    }
                }
                $baru = join('-', $time);

                $transaksi = Transaksi::find($id);
                $transaksi->tanggal_mulai = $request['tanggal'];
                $transaksi->tanggal_berakhir = $baru;
                $transaksi->jumlah_kamar =  $request['jumlah_kamar'];


Juliper committed
167

Juliper committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
                $dafBook = ListBook::where('id_transaksi',$id)->get();

                    /*DB::table('daftar_book')
                           ->where('daftar_book.id_transaksi','=',$id)
                           ->get();*/

                $lb = ListBook::find($dafBook[0]->id);
                //dd($lb);



                $lb->jumlah_kamar = $request['jumlah_kamar'];
                $lb->tanggal_mulai = $request['tanggal'];
                $lb->tanggal_berakhir = $baru;

                //dd($dafBook[0]);

                $transaksi->update();
                $lb->update();

                return redirect('customerHistory');
            }
        }
Juliper committed
191 192
    }

Juliper committed
193
    //Pemberian/Pengiriman Feedback
Juliper committed
194
    public function sendFeedback(Request $request, $id){
Juliper committed
195
   /*     if(Auth::guest()){
Palti Sinaga committed
196
            dd("maaf nggak bisa bang");
Juliper committed
197
        }
Juliper committed
198
*/
Juliper committed
199 200
        $dataPelanggan = DB::table('pelanggan')
                         ->select('pelanggan.id')
Juliper committed
201
                         ->where('pelanggan.id_akun','=',Auth::user()->id)
Juliper committed
202 203 204
                         ->get();

        $dataPemilik = DB::table('homestay')
Juliper committed
205
                       ->select('homestay.id_pemilik')
Juliper committed
206 207 208 209
                       ->where('homestay.id','=',$id)
                       ->get();

        $feed = new Feedback();
Juliper committed
210
        $feed->id_pemilik_homestay = $dataPemilik[0]->id_pemilik;
Juliper committed
211
        $feed->id_pelanggan = $dataPelanggan[0]->id;
Juliper committed
212 213 214
        $feed->feedback = $request['feedback'];

        $feed->save();
Juliper committed
215

Juliper committed
216
        return redirect()->action('GuestController@detailhomestay', ['id' => $id]);
Juliper committed
217 218
    }

Juliper committed
219
    //Upload Bukti Pembayaran
Juliper committed
220 221
    public function upload(Request $request,$id)
    {
Juliper committed
222 223
        $data = Transaksi::find($id);

Juliper committed
224
        if($request->file('bukti_pembayaran')==null){
Juliper committed
225
            $data->bukti_pembayaran = $data->bukti_pembayaran;
Juliper committed
226 227 228 229 230 231 232 233 234 235
        }else{
            $file = $request->file('bukti_pembayaran');
            $fileName = $file->getClientOriginalName();
            $request->file('bukti_pembayaran')->move("img/",$fileName);
            $data->bukti_pembayaran = $fileName;
        }

        $data->update();

        return redirect('customerHistory');
Juliper committed
236 237
    }

Juliper committed
238
    //Mengakses Halaman untuk mengupload Bukti Pembayaran
Juliper committed
239 240 241 242 243 244
    public function bukti($id){
        $data = Transaksi::find($id);

        return view('adminlte::layouts.customers.Upload')->with('data',$data);
    }

Juliper committed
245
    //Mengakses Halaman daftar Pemesanan Pelanggan
Juliper committed
246 247 248
    public function history(){
        $data = DB::table('pelanggan')
            ->select('pelanggan.*')
Juliper committed
249
            ->where('pelanggan.id_akun','=',Auth::user()->id)
Juliper committed
250 251 252 253
            ->get();

       $dataTrans = DB::table('homestay')
                    ->join('transaksi','homestay.id','=','transaksi.id_homestay')
Juliper committed
254 255
                    ->join('pemilikhomestay','homestay.id_pemilik','=','pemilikhomestay.id')
                    ->select('transaksi.*','pemilikhomestay.nama','pemilikhomestay.no_rekening')
Juliper committed
256
                    ->where('transaksi.id_pelanggan','=',$data[0]->id)
Juliper committed
257
                    ->orderBy('transaksi.id', 'desc')
Juliper committed
258 259 260
                    ->get();

        return view('adminlte::layouts.customers.History')->with('data',$dataTrans);
261 262
    }

Juliper committed
263
    //Mengakses Profile Pelanggan
264
    public function profile(){
Juliper committed
265 266
        $data = DB::table('pelanggan')
            ->select('pelanggan.*')
Juliper committed
267
            ->where('pelanggan.id_akun','=',Auth::user()->id)
Juliper committed
268
            ->get();
269

Juliper committed
270
        return view('adminlte::layouts.customers.profiles')->with('data',$data[0]);
271 272
    }

Juliper committed
273
    //Mengakses Halaman untuk mengedit/mengubah profile
Juliper committed
274 275
    public function editProfile($id){
        $data = Customer::find($id);
Juliper committed
276

Juliper committed
277
        return view('adminlte::layouts.customers.editprofiles')->with('data',$data);
Juliper committed
278 279
    }

Juliper committed
280
    //update Profile
Juliper committed
281 282
    public function updateProfile(Request $request,$id){
        $cus = Customer::find($id);
Juliper committed
283 284 285 286 287 288 289 290 291 292 293
        $dataUs = User::find(Auth::user()->id);
        if($request['nama']==""){
            $cus->nama = $cus->nama;
            $dataUs->name = $dataUs->name;
        }else{
            $cus->nama = $request['nama'];
            $dataUs->name=$request['nama'];
        }

        $cus->alamat = $request['alamat'];
        $cus->pekerjaan = $request['pekerjaan'];
Juliper committed
294
        $cus->no_telepon = $request['noTelepon'];
Juliper committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308

        if($request->file('foto')==null){
            $dataUs->foto = $dataUs->foto;
        }else{
            $file = $request->file('foto');
            $fileName = $file->getClientOriginalName();
            $request->file('foto')->move("img/",$fileName);
            $dataUs->foto = $fileName;
        }

        $cus->update();
        $dataUs->update();

        return redirect('customerProfile');
Juliper committed
309 310
    }

Juliper committed
311
    //Melakukan Pembookingan Homestay
Juliper committed
312
    public function booking(Request $request){
Juliper committed
313 314 315 316 317 318 319 320 321 322 323 324
        $this->validate($request,[
            'tanggal' => 'required',
            'jumlah_hari' => 'required|numeric',
            'jumlah_kamar' => 'required|numeric',

        ],[
            'tanggal.required' => 'Tanggal Harus di isi',
            'jumlah_kamar.required' => ' Jumlah Kamar Harus di isi',
            'jumlah_kamar.numeric' => ' Jumlah Kamr harus berupa Angka',
            'jumlah_hari.required' => ' Lama menginap harus Diisi',
            'jumlah_hari.numeric' => ' Lama menginap harus berupa Angka',
        ]);
Juliper committed
325 326

        if(Auth::guest()){
327
            return view('adminlte::errors.Account');
Juliper committed
328
        }
Juliper committed
329

Juliper committed
330
        $lb = new ListBook();
Juliper committed
331

Juliper committed
332 333 334 335 336 337 338 339 340 341 342 343 344
        $dataPelanggan = DB::table('users')
            ->join('pelanggan','users.id','=','pelanggan.id_akun')
            ->select('pelanggan.id','pelanggan.nama')
            ->where('users.id','=',Auth::user()->id)
            ->get();

        $dataHasilValidPelanggan = $this->dataValidation($dataPelanggan[0]->id);

        if($dataHasilValidPelanggan > 0 ){
            //dd($dataHasilValidPelanggan,'nggak boleh');
            return redirect('customerProfile')->with('message','Lengkkapi data diri anda!!');
        }

Juliper committed
345
        $dataHomestay = DB::table('homestay')
Juliper committed
346
            ->join('kamar','homestay.id','=','kamar.id_homestay')
Juliper committed
347
            ->select('kamar.*','homestay.harga','homestay.jumlah_kamar')
Juliper committed
348 349 350
            ->where('homestay.id','=',$request['id'])
            ->get();

Juliper committed
351
        $dataPemilik = DB::table('homestay')
Juliper committed
352 353
            ->join('pemilikhomestay','homestay.id_pemilik','pemilikhomestay.id')
            ->select('pemilikhomestay.no_telepon')
Juliper committed
354 355 356
            ->where('homestay.id','=',$request['id'])
            ->get();

Juliper committed
357 358 359 360
        $homestayJumlahKamar = DB::table('homestay')
                            ->select('homestay.jumlah_kamar')
                            ->where('homestay.id','=',$request['id'])
                            ->get();
Juliper committed
361 362 363 364 365

        $dataBookingMulai = DB::table('daftar_book')
                       ->select('daftar_book.*')
                       ->where('daftar_book.tanggal_mulai','=',$request['tanggal'])
                       ->get();
Juliper committed
366

Juliper committed
367 368
        $dataBookingBerakhir = DB::table('daftar_book')
            ->select('daftar_book.*')
Juliper committed
369
            ->where('daftar_book.tanggal_berakhir','=',$request['tanggal'])
Juliper committed
370 371
            ->get();

Juliper committed
372 373
        if($dataHomestay[0]->jumlah_kamar < $request['jumlah_kamar']){
            return redirect('detailhomestay/',$dataHomestay[0]->id)->with('message','Maaf Jumalh kamar tidak  sesuai');
Juliper committed
374
        }else{
Juliper committed
375 376 377 378
            if($dataBookingMulai->count() > 0){
                $currentJumlahKamar = $homestayJumlahKamar[0]->jumlah_kamar - $dataBookingMulai[0]->jumlah_kamar;

                if($currentJumlahKamar < $request['jumlah_kamar']){
Juliper committed
379
                    return redirect()->action('GuestController@detailhomestay',['id'=>$request['id']])->with('message', 'Maaf Anda Tidak Bisa Melakukan Pembookingan di karenakan kamar yang tersisa tidak sesuai dengan permintaan anda!');
Juliper committed
380
                }
Juliper committed
381

Juliper committed
382 383 384 385 386 387
            }else{
                if($dataBookingBerakhir->count() > 0){
                    $currentJumlahKamar2 = $homestayJumlahKamar[0]->jumlah_kamar - $dataBookingBerakhir[0]->jumlah_kamar;
                    if($currentJumlahKamar2 < $request['jumlah_kamar']){
                        return redirect()->action('GuestController@detailhomestay',['id'=>$request['id']])->with('message', 'Maaf Anda Tidak Bisa Melakukan Pembookingan di karenakan kamar yang tersisa tidak sesuai dengan permintaan anda!');
                    }
388 389
                }
            }
Juliper committed
390 391 392 393

            $time = explode('-', $request['tanggal']);

            if ($time[1]==2){
394
                $time[2] += $request['jumlah_hari'];
Juliper committed
395 396 397 398 399 400 401
                if ($time[2]>28){
                    $time[2] = 0;
                    $time[2] += $request['jumlah_hari'];
                    $time[2] -=1;
                    $time[1] +=1;
                    if($time[1]>12){
                        $time[1] = 1;
Juliper committed
402
                    }
403 404
                }
            }else{
Juliper committed
405 406 407 408 409 410 411 412
                if($time[1]%2==1){
                    $time[2] += $request['jumlah_hari'];
                    if($time[2]>31){
                        if($request['jumlah_hari']!=1){
                            $time[2] = 0;
                            $time[2] += $request['jumlah_hari'];
                            $time[1] +=1;
                        }
Juliper committed
413
                    }
Juliper committed
414 415 416 417 418 419 420 421 422 423 424
                    //$time[2] -=1;
                }else{
                    $time[2] += $request['jumlah_hari'];
                    if($time[2]>30){
                        if($request['jumlah_hari']!=1){
                            $time[2] = 0;
                            $time[2] += $request['jumlah_hari'];
                            $time[1] +=1;
                        }
                    }
                    //$time[2] -=1;
425 426
                }
            }
Juliper committed
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
            $baru = join('-',$time);

            $trans = new Transaksi();
            $trans->id_pelanggan  = $dataPelanggan[0]->id;
            $trans->id_homestay = $request['id'];
            $trans->tanggal_mulai = $request['tanggal'];
            $trans->tanggal_berakhir = $baru;
            $trans->jumlah_kamar =$request['jumlah_kamar'];
            $trans->lama_menginap = $request['jumlah_hari'];
            $trans->total_pembayaran = $request['jumlah_kamar'] * $dataHomestay[0]->harga;
            $trans->status = 0;

            $trans->save();

            $dataTrans = DB::table('transaksi')
                ->select('transaksi.id')
                ->where('transaksi.tanggal_mulai','=',$request['tanggal'])
                ->where('transaksi.tanggal_berakhir','=',$baru)
                ->get();

            //Insert Table Daftar Booking
            $lb->homestay = $request['id'];
            $lb->id_transaksi = $dataTrans[0]->id;
            $lb->nama_pemesan = $dataPelanggan[0]->nama;
            $lb->jumlah_kamar = $request['jumlah_kamar'];
            $lb->tanggal_mulai = $request['tanggal'] ;
            $lb->tanggal_berakhir = $baru;

            //Save
            $lb->save();

            $text = "Ada Pesanan Kamari dari Sdr/i : ";
            $pesan =  $text . ' ' .$dataPelanggan[0]->nama;
            //$pesan = $text + $dataPelanggan[0]->nama;
            //$pesan +=" dengan jumlah kamar"+ ;

            Nexmo::message()->send([
                'to' => $dataPemilik[0]->no_telepon,
                'from' => 'ASDASD',
                'text' => $pesan
            ]);

            return redirect('customerHistory');
470
        }
Juliper committed
471 472
    }

Juliper committed
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
    public function dataValidation($id){

        $dataPelanggan = DB::table('pelanggan')
                        ->select('pelanggan.*')
                        ->where('pelanggan.id','=',$id)
                        ->get();

        $counter = 0;

        if($dataPelanggan[0]->nama == "---"){
            $counter +=1;
        }
        if($dataPelanggan[0]->alamat =="---"){
            $counter +=1;
        }
        if($dataPelanggan[0]->no_telepon =="---"){
            $counter +=1;
        }
        if($dataPelanggan[0]->pekerjaan == "---"){
            $counter +=1;
        }

        return $counter;

    }
Juliper committed
498

499
}