<?php

public function buyvoucher(Request $request)
{
    if ($request->isMethod('post')) {
        $data = $request->all();

        $wallet = DB::table('wallet_manages')->where('id', 1)->first();
        $user_data = DB::table('members')->where('id', Auth::guard('member')->user()->id)->first();
        
        if ($user_data->point_bal >= 300) {
            // User Data Insert
            $budata = DB::table('members')->where('id', $user_data->id)->get();
            foreach ($budata as $ud1) {
                $data1['point_bal'] = $ud1->point_bal-300;
                $data1['total_buy'] = $ud1->total_buy+300;
                DB::table('members')->where('id', $ud1->id)->update($data1);

                $repo1['type'] = 'member';
                $repo1['bal_type'] = 'Transfer';
                $repo1['wallet'] = 'point';
                $repo1['action'] = 'Buying';
                $repo1['details'] = 'Deduct Balance for Voucher Buying';
                $repo1['bal_out'] = 300;
                $repo1['bal'] = $ud1->point_bal-300;
                $repo1['user_id'] = $ud1->id;
                DB::table('reports')->insert($repo1);
            }                    

            // Level Bonus
            $refer_data = DB::table('members')->where('username', $user_data->sponsor)->first();
            $ref_data = DB::table('members')->where('username', $user_data->sponsor)->get();

            $lgap = 0;

            if (!empty($ref_data)) {
                foreach ($ref_data as $ref1) {
                    $data2['cash_bal'] = $ref1->cash_bal+50;                            
                    DB::table('members')->where('id', $ref1->id)->update($data2);

                    $repo2['type'] = 'member';
                    $repo2['bal_type'] = 'Receive';
                    $repo2['wallet'] = 'cash';
                    $repo2['action'] = 'Level Bonus';
                    $repo2['details'] = '1st Level Bonus from ';
                    $repo2['bal_in'] = 50;
                    $repo2['bal'] = $ref1->cash_bal+50;
                    $repo2['user_id'] = $ref1->id;
                    $repo2['from_user'] = $user_data->username;
                    DB::table('reports')->insert($repo2);

                    $get2 = DB::table('members')->where('username', $ref1->sponsor)->get();
                }                                           
            }else{
                $lgap = $lgap+90;
            }
               
            if (!empty($get2)) {
                foreach ($get2 as $g2) {
                    if ($g2->lev_two == 1) {
                        $gdata2['cash_bal'] = $g2->cash_bal+10;
                        DB::table('members')->where('id', $g2->id)->update($gdata2);

                        $grepo2['type'] = 'member';
                        $grepo2['bal_type'] = 'Receive';
                        $grepo2['wallet'] = 'cash';
                        $grepo2['action'] = 'Level Bonus';
                        $grepo2['details'] = '2nd Level Bonus from ';
                        $grepo2['bal_in'] = 10;
                        $grepo2['bal'] = $g2->cash_bal+10;
                        $grepo2['user_id'] = $g2->id;
                        $grepo2['from_user'] = $user_data->username;
                        DB::table('reports')->insert($grepo2);

                        $lev2 = DB::table('levels')->where('user_id', $g2->id)->where('name', 'Level-2')->first();
                        if (!empty($lev2)) {
                            $levdata2['earn'] = $lev2->earn+10;
                            DB::table('levels')->where('id', $lev2->id)->update($levdata2);
                        }
                    }else{
                        $lgap = $lgap+10;
                    }  

                    $get3 = DB::table('members')->where('username', $g2->sponsor)->get();
                }                        
            }else{
                $lgap = $lgap+40;
            }

            if (!empty($get3)) {
                foreach ($get3 as $g3) {
                    if ($g3->lev_thr == 1) {
                        $gdata3['cash_bal'] = $g3->cash_bal+10;
                        DB::table('members')->where('id', $g3->id)->update($gdata3);

                        $grepo3['type'] = 'member';
                        $grepo3['bal_type'] = 'Receive';
                        $grepo3['wallet'] = 'cash';
                        $grepo3['action'] = 'Level Bonus';
                        $grepo3['details'] = '3rd Level Bonus from ';
                        $grepo3['bal_in'] = 10;
                        $grepo3['bal'] = $g3->cash_bal+10;
                        $grepo3['user_id'] = $g3->id;
                        $grepo3['from_user'] = $user_data->username;
                        DB::table('reports')->insert($grepo3);

                        $lev3 = DB::table('levels')->where('user_id', $g3->id)->where('name', 'Level-3')->first();
                        if (!empty($lev3)) {
                            $levdata3['earn'] = $lev3->earn+10;
                            DB::table('levels')->where('id', $lev3->id)->update($levdata3);
                        }
                    }else{
                        $lgap = $lgap+10;
                    }

                    $get4 = DB::table('members')->where('username', $g3->sponsor)->get();
                }                        
            }else{
                $lgap = $lgap+30;
            }

            if (!empty($get4)) {
                foreach ($get4 as $g4) {
                    if ($g4->lev_for == 1) {
                        $gdata4['cash_bal'] = $g4->cash_bal+5;
                        DB::table('members')->where('id', $g4->id)->update($gdata4);

                        $grepo4['type'] = 'member';
                        $grepo4['bal_type'] = 'Receive';
                        $grepo4['wallet'] = 'cash';
                        $grepo4['action'] = 'Level Bonus';
                        $grepo4['details'] = '4th Level Bonus from ';
                        $grepo4['bal_in'] = 5;
                        $grepo4['bal'] = $g4->cash_bal+5;
                        $grepo4['user_id'] = $g4->id;
                        $grepo4['from_user'] = $user_data->username;
                        DB::table('reports')->insert($grepo4);

                        $lev4 = DB::table('levels')->where('user_id', $g4->id)->where('name', 'Level-4')->first();
                        if (!empty($lev4)) {
                            $levdata4['earn'] = $lev4->earn+5;
                            DB::table('levels')->where('id', $lev4->id)->update($levdata4);
                        }
                    }else{
                        $lgap = $lgap+5;
                    }

                    $get5 = DB::table('members')->where('username', $g4->sponsor)->get();
                }                        
            }else{
                $lgap = $lgap+20;
            }

            if (!empty($get5)) {
                foreach ($get5 as $g5) {
                    if ($g5->lev_fiv == 1) {
                        $gdata5['cash_bal'] = $g5->cash_bal+5;
                        DB::table('members')->where('id', $g5->id)->update($gdata5);

                        $grepo5['type'] = 'member';
                        $grepo5['bal_type'] = 'Receive';
                        $grepo5['wallet'] = 'cash';
                        $grepo5['action'] = 'Level Bonus';
                        $grepo5['details'] = '5th Level Bonus from ';
                        $grepo5['bal_in'] = 5;
                        $grepo5['bal'] = $g5->cash_bal+5;
                        $grepo5['user_id'] = $g5->id;
                        $grepo5['from_user'] = $user_data->username;
                        DB::table('reports')->insert($grepo5);

                        $lev5 = DB::table('levels')->where('user_id', $g5->id)->where('name', 'Level-5')->first();
                        if (!empty($lev5)) {
                            $levdata5['earn'] = $lev5->earn+5;
                            DB::table('levels')->where('id', $lev5->id)->update($levdata5);
                        }
                    }else{
                        $lgap = $lgap+5;
                    }

                    $get6 = DB::table('members')->where('username', $g5->sponsor)->get();
                }                        
            }else{
                $lgap = $lgap+15;
            }

            if (!empty($get6)) {
                foreach ($get6 as $g6) {
                    if ($g6->lev_six == 1) {
                        $gdata6['cash_bal'] = $g6->cash_bal+5;
                        DB::table('members')->where('id', $g6->id)->update($gdata6);

                        $grepo6['type'] = 'member';
                        $grepo6['bal_type'] = 'Receive';
                        $grepo6['wallet'] = 'cash';
                        $grepo6['action'] = 'Level Bonus';
                        $grepo6['details'] = '6th Level Bonus from ';
                        $grepo6['bal_in'] = 5;
                        $grepo6['bal'] = $g6->cash_bal+5;
                        $grepo6['user_id'] = $g6->id;
                        $grepo6['from_user'] = $user_data->username;
                        DB::table('reports')->insert($grepo6);

                        $lev6 = DB::table('levels')->where('user_id', $g6->id)->where('name', 'Level-6')->first();
                        if (!empty($lev6)) {
                            $levdata6['earn'] = $lev6->earn+5;
                            DB::table('levels')->where('id', $lev6->id)->update($levdata6);
                        }
                    }else{
                        $lgap = $lgap+5;
                    }

                    $get7 = DB::table('members')->where('username', $g6->sponsor)->get();
                }                        
            }else{
                $lgap = $lgap+10;
            }

            if (!empty($get7)) {
                foreach ($get7 as $g7) {
                    if ($g7->lev_sev == 1) {
                        $gdata7['cash_bal'] = $g7->cash_bal+5;
                        DB::table('members')->where('id', $g7->id)->update($gdata7);

                        $grepo7['type'] = 'member';
                        $grepo7['bal_type'] = 'Receive';
                        $grepo7['wallet'] = 'cash';
                        $grepo7['action'] = 'Level Bonus';
                        $grepo7['details'] = '7th Level Bonus from ';
                        $grepo7['bal_in'] = 5;
                        $grepo7['bal'] = $g7->cash_bal+5;
                        $grepo7['user_id'] = $g7->id;
                        $grepo7['from_user'] = $user_data->username;
                        DB::table('reports')->insert($grepo7);

                        $lev7 = DB::table('levels')->where('user_id', $g7->id)->where('name', 'Level-7')->first();
                        if (!empty($lev7)) {
                            $levdata7['earn'] = $lev7->earn+5;
                            DB::table('levels')->where('id', $lev7->id)->update($levdata7);
                        }
                    }else{
                        $lgap = $lgap+5;
                    }
                }                        
            }else{
                $lgap = $lgap+5;
            }

            // Voucher Insert
            $vou_data['user_id'] = $user_data->id;
            if (!empty($refer_data)) {
                $vou_data['ref_id'] = $refer_data->id;
            }else{
                $vou_data['ref_id'] = 0;
            }                
            $vou_data['bonus'] = 0;
            $vou_data['cash'] = 0;
            $vou_data['point'] = 0;
            $vou_data['status'] = 0;
            DB::table('vouchers')->insert($vou_data);

            $vou_no = DB::getPdo()->lastInsertId();

            // Board Insert              
            $brdata1['user_id'] = $user_data->id;
            $brdata1['vou_no'] = $vou_no;
            $brdata1['status'] = 0;
            DB::table('board_one')->insert($brdata1);

            $new_bid = DB::getPdo()->lastInsertId();

            // Board Place Insert
            $board1 = DB::table('board_one')->where('status', 0)->orderBy('id', 'asc')->take(1)->first();
            $lfund = 0;

            if (is_null($board1->count)) {
                $bdata1['pone'] = $new_bid;
            }elseif($board1->count == 1){
                $bdata1['ptwo'] = $new_bid;
            }elseif($board1->count == 2){
                $bdata1['pthr'] = $new_bid;
                $bdata1['status'] = 1;

                // Paid Voucher Level-1 Bonus
                $bmem_data1 = DB::table('members')->where('id', $board1->user_id)->get();
                foreach ($bmem_data1 as $bmem1) {
                    $bmp1['cash_bal'] = $bmem1->cash_bal+120;
                    $bmp1['point_bal'] = $bmem1->point_bal+30;
                    DB::table('members')->where('id', $bmem1->id)->update($bmp1);

                    $vbrepo1['type'] = 'member';
                    $vbrepo1['bal_type'] = 'Receive';
                    $vbrepo1['wallet'] = 'cash';
                    $vbrepo1['action'] = 'Voucher Bonus';
                    $vbrepo1['details'] = 'Voucher Level-1 Cash Bonus ';
                    $vbrepo1['bal_in'] = 120;
                    $vbrepo1['bal'] = $bmem1->cash_bal+120;
                    $vbrepo1['user_id'] = $bmem1->id;
                    DB::table('reports')->insert($vbrepo1);

                    $vbrepo2['type'] = 'member';
                    $vbrepo2['bal_type'] = 'Receive';
                    $vbrepo2['wallet'] = 'point';
                    $vbrepo2['action'] = 'Voucher Bonus';
                    $vbrepo2['details'] = 'Voucher Level-1 Point Bonus ';
                    $vbrepo2['bal_in'] = 30;
                    $vbrepo2['bal'] = $bmem1->point_bal+30;
                    $vbrepo2['user_id'] = $bmem1->id;
                    DB::table('reports')->insert($vbrepo2);
                }

                $voup_data1 = DB::table('vouchers')->where('id', $board1->vou_no)->first();
                $vpdat1['bonus'] = $voup_data1->bonus+150;
                $vpdat1['cash'] = $voup_data1->cash+120;
                $vpdat1['point'] = $voup_data1->point+30;
                DB::table('vouchers')->where('id', $voup_data1->id)->update($vpdat1);

                // Lottery 30% of Point
                $lfund = $lfund+9;

                $board2 = DB::table('board_two')->where('status', 0)->orderBy('id', 'asc')->take(1)->first();
                // Voucher Level-2 Entry
                $brdata2['user_id'] = $board1->user_id;
                $brdata2['vou_no'] = $board1->vou_no;
                $brdata2['status'] = 0;
                DB::table('board_two')->insert($brdata2);

                $bid2 = DB::getPdo()->lastInsertId();

                if (!empty($board2)) {
                    if (is_null($board2->count)) {
                        $bdata2['pone'] = $bid2;
                    }elseif($board2->count == 1){
                        $bdata2['ptwo'] = $bid2;
                    }elseif($board2->count == 2){
                        $bdata2['pthr'] = $bid2;
                        $bdata2['status'] = 1;

                        // Paid Voucher Level-2 Bonus
                        $bmem_data2 = DB::table('members')->where('id', $board2->user_id)->get();
                        foreach ($bmem_data2 as $bmem2) {
                            $bmp2['cash_bal'] = $bmem2->cash_bal+180;
                            $bmp2['point_bal'] = $bmem2->point_bal+270;
                            DB::table('members')->where('id', $bmem2->id)->update($bmp2);

                            $vbrepo3['type'] = 'member';
                            $vbrepo3['bal_type'] = 'Receive';
                            $vbrepo3['wallet'] = 'cash';
                            $vbrepo3['action'] = 'Voucher Bonus';
                            $vbrepo3['details'] = 'Voucher Level-2 Cash Bonus ';
                            $vbrepo3['bal_in'] = 180;
                            $vbrepo3['bal'] = $bmem2->cash_bal+180;
                            $vbrepo3['user_id'] = $bmem2->id;
                            DB::table('reports')->insert($vbrepo3);

                            $vbrepo4['type'] = 'member';
                            $vbrepo4['bal_type'] = 'Receive';
                            $vbrepo4['wallet'] = 'point';
                            $vbrepo4['action'] = 'Voucher Bonus';
                            $vbrepo4['details'] = 'Voucher Level-2 Point Bonus ';
                            $vbrepo4['bal_in'] = 270;
                            $vbrepo4['bal'] = $bmem2->point_bal+270;
                            $vbrepo4['user_id'] = $bmem2->id;
                            DB::table('reports')->insert($vbrepo4);
                        }

                        $voup_data2 = DB::table('vouchers')->where('id', $board2->vou_no)->first();
                        $vpdat2['bonus'] = $voup_data2->bonus+450;
                        $vpdat2['cash'] = $voup_data2->cash+180;
                        $vpdat2['point'] = $voup_data2->point+270;
                        DB::table('vouchers')->where('id', $voup_data2->id)->update($vpdat2);

                        // Lottery 30% of Point
                        $lfund = $lfund+81;

                        $board3 = DB::table('board_three')->where('status', 0)->orderBy('id', 'asc')->take(1)->first();
                        // Voucher Level-3 Entry
                        $brdata3['user_id'] = $board2->user_id;
                        $brdata3['vou_no'] = $board2->vou_no;
                        $brdata3['status'] = 0;
                        DB::table('board_three')->insert($brdata3);

                        $bid3 = DB::getPdo()->lastInsertId();

                        if (!empty($board3)) {
                            if (is_null($board3->count)) {
                                $bdata3['pone'] = $bid3;
                            }elseif($board3->count == 1){
                                $bdata3['ptwo'] = $bid3;
                            }elseif($board3->count == 2){
                                $bdata3['pthr'] = $bid3;
                                $bdata3['status'] = 1;

                                // Paid Voucher Level-3 Bonus
                                $bmem_data3 = DB::table('members')->where('id', $board3->user_id)->get();
                                foreach ($bmem_data3 as $bmem3) {
                                    $bmp3['cash_bal'] = $bmem3->cash_bal+200;
                                    $bmp3['point_bal'] = $bmem3->point_bal+70;
                                    DB::table('members')->where('id', $bmem3->id)->update($bmp3);

                                    $vbrepo5['type'] = 'member';
                                    $vbrepo5['bal_type'] = 'Receive';
                                    $vbrepo5['wallet'] = 'cash';
                                    $vbrepo5['action'] = 'Voucher Bonus';
                                    $vbrepo5['details'] = 'Voucher Level-3 Cash Bonus ';
                                    $vbrepo5['bal_in'] = 200;
                                    $vbrepo5['bal'] = $bmem3->cash_bal+200;
                                    $vbrepo5['user_id'] = $bmem3->id;
                                    DB::table('reports')->insert($vbrepo5);

                                    $vbrepo6['type'] = 'member';
                                    $vbrepo6['bal_type'] = 'Receive';
                                    $vbrepo6['wallet'] = 'point';
                                    $vbrepo6['action'] = 'Voucher Bonus';
                                    $vbrepo6['details'] = 'Voucher Level-3 Point Bonus ';
                                    $vbrepo6['bal_in'] = 70;
                                    $vbrepo6['bal'] = $bmem3->point_bal+70;
                                    $vbrepo6['user_id'] = $bmem3->id;
                                    DB::table('reports')->insert($vbrepo6);
                                }

                                $voup_data3 = DB::table('vouchers')->where('id', $board3->vou_no)->first();
                                $vpdat3['bonus'] = $voup_data3->bonus+270;
                                $vpdat3['cash'] = $voup_data3->cash+200;
                                $vpdat3['point'] = $voup_data3->point+70;
                                $vpdat3['status'] = 1;
                                DB::table('vouchers')->where('id', $voup_data3->id)->update($vpdat3);

                                // Lottery 30% of Point
                                $lfund = $lfund+21;
                            }

                            $bdata3['count'] = $board3->count+1;
                            DB::table('board_three')->where('id', $board3->id)->update($bdata3);
                        }                                

                    }

                    $bdata2['count'] = $board2->count+1;
                    DB::table('board_two')->where('id', $board2->id)->update($bdata2);
                }                       

            }

            $bdata1['count'] = $board1->count+1;
            DB::table('board_one')->where('id', $board1->id)->update($bdata1);

            if ($lgap+$lfund > 0) {
                if ($lgap > 0) {
                    $wdata1['profit'] = $wallet->profit+$lgap;

                    $wrepo1['type'] = 'admin';
                    $wrepo1['bal_type'] = 'Receive';
                    $wrepo1['wallet'] = 'profit';
                    $wrepo1['action'] = 'Level Gap';
                    $wrepo1['details'] = 'Level Gap Profit from ';
                    $wrepo1['bal_in'] = $lgap;
                    $wrepo1['bal'] = $wallet->profit+$lgap;
                    $wrepo1['user_id'] = 1;
                    $wrepo1['from_user'] = $user_data->username;
                    DB::table('reports')->insert($wrepo1);
                }

                if ($lfund > 0) {
                    $wdata1['lottery_fund'] = $wallet->lottery_fund+$lfund;

                    $wrepo2['type'] = 'admin';
                    $wrepo2['bal_type'] = 'Receive';
                    $wrepo2['wallet'] = 'lottery';
                    $wrepo2['action'] = 'Voucher Bonus';
                    $wrepo2['details'] = 'Receive Fund by Voucher Bonus ';
                    $wrepo2['bal_in'] = $lfund;
                    $wrepo2['bal'] = $wallet->lottery_fund+$lfund;
                    $wrepo2['user_id'] = 1;
                    DB::table('reports')->insert($wrepo2);
                }  

                DB::table('wallet_manages')->where('id', $wallet->id)->update($wdata1);

                    
            }


            toastr()->success('Voucher Buying Successful!');
            return redirect()->back();

        }else{
            toastr()->error('Insufficient Point Balance!');
            return redirect()->back();
        }  
    }
}