accessrules(); Helpers::check_request($_GET['userid'], $_SERVER); $matchkey = $data['matchkey'] = $_GET['matchkey']; $userid = $data['userid'] = $_GET['userid']; $listmatchestartdate = DB::table('listmatches')->where('matchkey',$matchkey)->select('start_date')->first(); $getcurrentdate = date('Y-m-d H:i:s'); $matchremainingdate = $listmatchestartdate->start_date; if($getcurrentdate>$matchremainingdate){ $Json[0]['message'] = 'match closed'; echo json_encode($Json);die; } $challengeid = $data['challengeid'] = $_GET['challengeid']; $teamid = $data['teamid'] = $_GET['teamid']; $Json = array(); // // New condition for max join limit // $challange_info = DB::table('matchchallenges')->where('id',$challengeid)->first(); // $max_joined = $challange_info->maximum_user; // $cur_joined = $challange_info->joinedusers; // if($max_joined <= $cur_joined) { // $Json[0]['message'] = 'League already full.'; // echo json_encode($Json);die; // } // // New condition for max join limit $userdetailsfind = DB::table('registerusers')->where('id',$userid)->where('activation_status','!=','deactivated')->select('username','email','state')->first(); if(empty($userdetailsfind)) { $Json[0]['message'] = 'There is some error please login again.'; echo json_encode($Json);die; } $refercode = $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $refercode = ''; $max = strlen($characters) - 1; for ($i = 0; $i < 6; $i++){ $refercode.= $characters[mt_rand(0, $max)]; } $data['refercode'] = $refercode; $findchallenge = DB::connection('mysql-write')->table('matchchallenges')->where('id',$challengeid)->first(); if($findchallenge->status=='closed'){ $Json[0]['message'] = 'leauge closed'; echo json_encode($Json);die; } if($findchallenge->win_amount>0){ if($userdetailsfind->state=='Telangana' || $userdetailsfind->state=='Orissa' || $userdetailsfind->state=='Assam'){ $Json[0]['message'] = 'ineligible'; echo json_encode($Json);die; } } $cur_joined = $findchallenge->joinedusers; //find leauges already exist or not // $findexistornot = DB::table('joinedleauges')->where('userid',$userid)->where('challengeid',$challengeid)->where('matchkey',$matchkey)->select('teamid')->get(); if(!empty($findexistornot)){ if($findchallenge->multi_entry==0){ $Json[0]['message'] = 'already joined'; echo json_encode($Json);die; }else{ foreach($findexistornot as $ff){ if($ff->teamid==$teamid){ $Json[0]['message'] = 'team already joined'; echo json_encode($Json);die; } } } } //check for leauge closed or not // $dataused = array(); $dataleft = array(); $updatedata['joinedusers'] = $findchallenge->joinedusers+1; if($findchallenge->challenge_type == 'money'){ if($updatedata['joinedusers']>$findchallenge->maximum_user){ $Json[0]['message'] = 'leauge closed'; echo json_encode($Json);die; } } //start deduct money code for join leauge// $finduserbalance = DB::connection('mysql-write')->table('userbalances')->where('user_id',$userid)->lockForUpdate()->first(); if(!empty($finduserbalance)){ $findentryfee = $findchallenge->entryfee; $dataleft['bonus'] = $findbonusforuser = $finduserbalance->bonus; $dataleft['winning'] = $finduserbalance->winning; $dataleft['balance'] = $finduserbalance->balance; $totalbonus = $finduserbonus = $finduserbalance->bonus; $usedbonus = 0; $canusedbonus = 0; $totalwining = $canusedwining = $finduserbalance->winning; $totalbalance = $canusedbalance = $finduserbalance->balance; $totbalan = $finduserbalance->bonus + $finduserbalance->winning + $finduserbalance->balance; $findusablebalance = $finduserbalance->balance+$finduserbalance->winning; $reminingfee = $findentryfee; //find bonus// if($findchallenge->bonus == 1){ $bonus_precent = $findchallenge->bonus_precent; $bonus_entry_fee = $findentryfee * $bonus_precent / 100; // if(($findusablebalance+($finduserbonus*$bonus_precent/100))<$findentryfee) $findusablebalance = $findusablebalance+ $finduserbonus; // $finduserbonus = ($finduserbonus * $bonus_precent) / 100; if(($finduserbalance->balance+$finduserbalance->winning) < ($findentryfee-$bonus_entry_fee)) { $Json[0]['message'] = 'insufficient balance'; echo json_encode($Json);die; } } //start deduct money section// if($findusablebalance<$findentryfee){ $Json[0]['message'] = 'insufficient balance'; echo json_encode($Json);die; } if($findchallenge->bonus == 1){ if($finduserbonus >= $bonus_entry_fee){ $remainingbonus = $finduserbonus-$bonus_entry_fee; $dataleft['bonus'] = $dataleft['bonus'] - $bonus_entry_fee; $transactiondata['cons_bonus'] = $dataused['bonus'] = $bonus_entry_fee; $reminingfee=$findentryfee - $bonus_entry_fee; }else{ $reminingfee = $findentryfee-$finduserbonus; $remainingbonus = 0; $dataleft['bonus'] = $dataleft['bonus'] - $finduserbonus; $transactiondata['cons_bonus'] = $dataused['bonus'] = $finduserbonus; } } if($totalbalance >= $reminingfee){ $dataleft['balance'] = $totalbalance - $reminingfee; $transactiondata['cons_amount'] = $dataused['balance'] = $reminingfee; $reminingfee=0; }else{ $reminingfee = $reminingfee-$totalbalance; $dataleft['balance'] = 0; $transactiondata['cons_amount'] = $dataused['balance'] = $totalbalance; } if($totalwining >= $reminingfee){ $dataleft['winning'] = $totalwining - $reminingfee; $transactiondata['cons_win'] = $dataused['winning'] = $reminingfee; $reminingfee=0; } else{ $reminingfee = $reminingfee-$totalwining; $dataleft['winning'] = 0; $transactiondata['cons_win'] = $dataused['winning'] = $totalwining; } // yha tk $tranid = time(); // to enter in joined leauges table// $data['transaction_id'] = 'FP11-JL-'.$tranid.'-'.$userid; // check again// // $findexistornot = DB::table('joinedleauges')->where('userid',$userid)->where('challengeid',$challengeid)->where('matchkey',$matchkey)->select('teamid')->get(); // if(!empty($findexistornot)){ // if($findchallenge->multi_entry==0){ // $Json[0]['message'] = 'already joined'; // echo json_encode($Json);die; // } // else{ // foreach($findexistornot as $ff){ // if($ff->teamid==$teamid){ // $Json[0]['message'] = 'team already joined'; // echo json_encode($Json);die; // } // } // } // } //entry in challenges tables// //insert leauge entry// $data['refercode'] = $refercode.'-'.time().''.$userid; // $findchallengeagain = DB::table('matchchallenges')->where('id',$challengeid)->select('status')->first(); // if($findchallengeagain->status=='closed'){ // $Json[0]['message'] = 'leauge closed'; // echo json_encode($Json);die; // } // New condition for max join limit // $challange_info = DB::table('matchchallenges')->where('id',$challengeid)->first(); // $max_joined = $challange_info->maximum_user; // $joined_list = DB::table('joinedleauges')->where('challengeid',$challengeid)->where('matchkey',$matchkey)->select(DB::raw('SQL_NO_CACHE joinedleauges.teamid'))->get(); // $cur_joined = count($joined_list); // if($max_joined <= $cur_joined) { // if($findchallenge->challenge_type == 'money'){ // $updatedata['status']='closed'; // } // $updatedata['joinedusers']=$cur_joined; // // if($findchallenge->is_running==1){ // // //new duplicate challenge// // // $newentry['matchkey'] = $findchallenge->matchkey; // // $newentry['name'] = $findchallenge->name; // // $newentry['challenge_id'] = $findchallenge->id; // // $newentry['entryfee'] = $findchallenge->entryfee; // // $newentry['challenge_type'] = $findchallenge->challenge_type; // // $newentry['win_amount'] = $findchallenge->win_amount; // // $newentry['maximum_user'] = $findchallenge->maximum_user; // // $newentry['bonus'] = $findchallenge->bonus; // // $newentry['multi_entry'] = $findchallenge->multi_entry; // // $newentry['confirmed_challenge'] = $findchallenge->confirmed_challenge; // // $newentry['is_running'] = $findchallenge->is_running; // // //$getcid = DB::table('matchchallenges')->insertGetId($newentry); // // $findpricecards = DB::table('matchpricecards')->where('challenge_id',$findchallenge->id)->where('matchkey',$findchallenge->matchkey)->get(); // // if(!empty($findpricecards)){ // // foreach($findpricecards as $pricec){ // // $pdata = array(); // // $pdata['challenge_id'] = $getcid; // // $pdata['matchkey'] = $findchallenge->matchkey; // // $pdata['winners'] = $pricec->winners; // // $pdata['price'] = $pricec->price; // // $pdata['min_position'] = $pricec->min_position; // // $pdata['max_position'] = $pricec->max_position; // // $pdata['description'] = $pricec->description; // // $pdata['total'] = $pricec->total; // // DB::table('matchpricecards'); // // //DB::table('matchpricecards')->insert($pdata); // // } // // } // // } // if($findchallenge->challenge_type == 'money'){ // DB::table('matchchallenges')->where('id',$challengeid)->update($updatedata); // $Json[0]['message'] = 'League already full.'; // echo json_encode($Json);die; // } // } // // New condition for max join limit // $result = DB::insert("INSERT INTO `joinedleauges` ( `userid`, `challengeid`, `teamid`, `matchkey`, `refercode`, `transaction_id`, `pdfcreate`, `pdfname`) SELECT ".$userid." , ".$challengeid.", ".$teamid.", '".$matchkey."', '".$data['refercode']."', '".$data['transaction_id']."', '0', '' FROM DUAL WHERE (SELECT COUNT(*) FROM joinedleauges WHERE challengeid=$challengeid) < ".$findchallenge->maximum_user); print_r($result); // $challenge_insert_id = DB::select('SELECT LAST_INSERT_ID()'); // print_r($challenge_insert_id); exit; try { if($findchallenge->challenge_type == 'percentage') { $result = DB::insert("INSERT INTO `joinedleauges` ( `userid`, `challengeid`, `teamid`, `matchkey`, `refercode`, `transaction_id`, `pdfcreate`, `pdfname`) SELECT ".$userid." , ".$challengeid.", ".$teamid.", '".$matchkey."', '".$data['refercode']."', '".$data['transaction_id']."', '0', '' FROM DUAL"); //$challenge_insert_id = DB::select('SELECT LAST_INSERT_ID()'); $challenge_insert_id = DB::connection('mysql-write')->table('joinedleauges')->where('teamid',$teamid)->where('challengeid',$challengeid)->where('matchkey',$matchkey)->first(); } else { $result = DB::insert("INSERT INTO `joinedleauges` ( `userid`, `challengeid`, `teamid`, `matchkey`, `refercode`, `transaction_id`, `pdfcreate`, `pdfname`) SELECT ".$userid." , ".$challengeid.", ".$teamid.", '".$matchkey."', '".$data['refercode']."', '".$data['transaction_id']."', '0', '' FROM DUAL WHERE (SELECT COUNT(*) FROM joinedleauges WHERE challengeid=$challengeid) < ".$findchallenge->maximum_user); $challenge_insert_id = DB::connection('mysql-write')->table('joinedleauges')->where('teamid',$teamid)->where('challengeid',$challengeid)->where('matchkey',$matchkey)->first(); //$challenge_insert_id = DB::select('SELECT LAST_INSERT_ID()'); } $getinsertid = @$challenge_insert_id->id ? $challenge_insert_id->id : 0; //echo $getinsertid; exit; if(!$getinsertid) {//if(!$getinsertid) { if($findchallenge->loops==0 && $findchallenge->challenge_type != 'percentage') { $newentry['matchkey'] = $findchallenge->matchkey; $newentry['name'] = $findchallenge->name; $newentry['challenge_id'] = $findchallenge->id; $newentry['entryfee'] = $findchallenge->entryfee; $newentry['challenge_type'] = $findchallenge->challenge_type; $newentry['winning_percentage'] = $findchallenge->winning_percentage; $newentry['win_amount'] = $findchallenge->win_amount; $newentry['maximum_user'] = $findchallenge->maximum_user; $newentry['bonus'] = $findchallenge->bonus; $newentry['bonus_precent'] = $findchallenge->bonus_precent; $newentry['multi_entry'] = $findchallenge->multi_entry; $newentry['confirmed_challenge'] = $findchallenge->confirmed_challenge; $newentry['is_running'] = $findchallenge->is_running; $newentry['challenge_category_id'] = $findchallenge->challenge_category_id; $findifexist = DB::connection('mysql-write')->table('matchchallenges')->where('challenge_id',$findchallenge->id)->get(); if(empty($findifexist)) { $getcid = DB::connection('mysql-write')->table('matchchallenges')->insertGetId($newentry); // $sql = "INSERT INTO `matchchallenges` (`matchkey`, name, challenge_id, entryfee, challenge_type, winning_percentage, win_amount, maximum_user, bonus, bonus_precent, multi_entry, confirmed_challenge, is_running, challenge_category_id) SELECT '".$newentry['matchkey']."', '', '".$newentry['challenge_id']."', ".$newentry['entryfee']." , 'money', ".$newentry['winning_percentage'].", ".$newentry['win_amount'].", ".$newentry['maximum_user'].", ".$newentry['bonus'].", ".$newentry['bonus_precent'].", ".$newentry['multi_entry'].", ".$newentry['confirmed_challenge'].", ".$newentry['is_running'].", ".$newentry['challenge_category_id']." FROM DUAL WHERE (SELECT COUNT(id) FROM matchchallenges WHERE challenge_id = ".$newentry['challenge_id']." AND matchkey = '".$newentry['matchkey']."') < 1"; // DB::insert($sql); // $challenge_insert_id = DB::connection('mysql-write')->table('matchchallenges')->where('challenge_id',$newentry['challenge_id'])->where('matchkey',$newentry['matchkey'])->first(); // $getcid = $challenge_insert_id->id; $findpricecards = DB::connection('mysql-write')->table('matchpricecards')->where('challenge_id',$findchallenge->id)->where('matchkey',$findchallenge->matchkey)->get(); if(!empty($findpricecards)){ $findpricecardexist = DB::connection('mysql-write')->table('matchpricecards')->where('challenge_id',$getcid)->where('matchkey',$findchallenge->matchkey)->get(); if(empty($findpricecardexist)) { foreach($findpricecards as $pricec){ $pdata = array(); $pdata['challenge_id'] = $getcid; $pdata['matchkey'] = $findchallenge->matchkey; $pdata['winners'] = $pricec->winners; $pdata['price'] = $pricec->price; $pdata['min_position'] = $pricec->min_position; $pdata['max_position'] = $pricec->max_position; $pdata['description'] = $pricec->description; $pdata['total'] = $pricec->total; DB::table('matchpricecards'); DB::table('matchpricecards')->insert($pdata); } } } } } $updatedata_new['status'] = 'closed'; $updatedata_new['joinedusers'] = $findchallenge->maximum_user; //print_r($findchallenge->id); exit; try { $affectedRows = DB::table('matchchallenges')->where('id',$findchallenge->id)->update($updatedata_new); } catch(\Illuminate\Database\QueryException $ex) { dd($ex->getMessage()); } $Json[0]['message'] = 'League already full.'; echo json_encode($Json);die; } } catch(\Illuminate\Database\QueryException $ex) { dd($ex->getMessage()); } try { DB::beginTransaction(); //entry in leauges transactions// $dataused['matchkey'] = $matchkey; $dataused['user_id'] = $userid; $dataused['challengeid'] = $challengeid; $dataused['joinid'] = $getinsertid; DB::table('leaugestransactions')->insert($dataused); //updatewallet table// DB::table('userbalances')->where('user_id',$userid)->update($dataleft); DB::commit(); $findnowamount = DB::connection('mysql-write')->table('userbalances')->where('user_id',$userid)->first(); //end deduct money section// //start entry in transaction table// $transactiondata['type'] = 'Challenge Joining Fee'; $transactiondata['amount'] = $findentryfee; $transactiondata['total_available_amt'] = $totbalan - $findentryfee; $transactiondata['transaction_by'] = 'wallet'; $transactiondata['challengeid'] = $challengeid; $transactiondata['userid'] = $userid; $transactiondata['paymentstatus'] = 'confirmed'; $transactiondata['bal_bonus_amt'] = $findnowamount->bonus; $transactiondata['bal_win_amt'] = $findnowamount->winning; $transactiondata['bal_fund_amt'] = $findnowamount->balance; $transactiondata['transaction_id'] = $data['transaction_id']; DB::table('transactions')->insert($transactiondata); // $getinsertid = DB::table('joinedleauges')->insertGetId($data); } catch(\Illuminate\Database\QueryException $ex) { //print_r($ex->getMessage()); exit; DB::rollback(); $message = $ex->getCode(); if($message==45000) { //if(!$getinsertid) { if($findchallenge->loops==0) { $newentry['matchkey'] = $findchallenge->matchkey; $newentry['name'] = $findchallenge->name; $newentry['challenge_id'] = $findchallenge->id; $newentry['entryfee'] = $findchallenge->entryfee; $newentry['challenge_type'] = $findchallenge->challenge_type; $newentry['winning_percentage'] = $findchallenge->winning_percentage; $newentry['win_amount'] = $findchallenge->win_amount; $newentry['maximum_user'] = $findchallenge->maximum_user; $newentry['bonus'] = $findchallenge->bonus; $newentry['bonus_precent'] = $findchallenge->bonus_precent; $newentry['multi_entry'] = $findchallenge->multi_entry; $newentry['confirmed_challenge'] = $findchallenge->confirmed_challenge; $newentry['is_running'] = $findchallenge->is_running; $newentry['challenge_category_id'] = $findchallenge->challenge_category_id; $findifexist = DB::connection('mysql-write')->table('matchchallenges')->where('challenge_id',$findchallenge->id)->get(); if(empty($findifexist)) { $getcid = DB::connection('mysql-write')->table('matchchallenges')->insertGetId($newentry); $findpricecards = DB::connection('mysql-write')->table('matchpricecards')->where('challenge_id',$findchallenge->id)->where('matchkey',$findchallenge->matchkey)->get(); if(!empty($findpricecards)){ foreach($findpricecards as $pricec){ $pdata = array(); $pdata['challenge_id'] = $getcid; $pdata['matchkey'] = $findchallenge->matchkey; $pdata['winners'] = $pricec->winners; $pdata['price'] = $pricec->price; $pdata['min_position'] = $pricec->min_position; $pdata['max_position'] = $pricec->max_position; $pdata['description'] = $pricec->description; $pdata['total'] = $pricec->total; DB::table('matchpricecards'); DB::table('matchpricecards')->insert($pdata); } } } } $updatedata_new['status'] = 'closed'; $updatedata_new['joinedusers'] = $findchallenge->maximum_user; //print_r($findchallenge->id); exit; try { $affectedRows = DB::table('matchchallenges')->where('id',$findchallenge->id)->update($updatedata_new); } catch(\Illuminate\Database\QueryException $ex) { dd($ex->getMessage()); } $Json[0]['message'] = 'League already full.'; echo json_encode($Json);die; } } // //Rollback if joined count exceed // $joined_list_new = DB::table('joinedleauges')->where('challengeid',$challengeid)->where('matchkey',$matchkey)->select(DB::raw('SQL_NO_CACHE joinedleauges.teamid'))->get(); // $cur_joined_new = count($joined_list_new); // if($max_joined < $cur_joined_new) { // DB::rollback(); // $Json[0]['message'] = 'League already full.'; // echo json_encode($Json);die; // } //end entry in transaction table// //entry in notification table// $notificationdata['userid'] = $userid; $notificationdata['title'] = 'Challenge entry fees Rs.'.$findentryfee; //DB::table('notifications')->insert($notificationdata); $titleget = 'Confirmation - joined challenge!'; //Helpers::sendnotification($titleget,$notificationdata['title'],'',$userid); } $updatedata_new = array(); if(($cur_joined+1)>=$findchallenge->maximum_user){ //close challenge// if($findchallenge->is_running==1 && $findchallenge->challenge_type == 'money' && $findchallenge->loops==0){ $updatedata['status']='closed'; $updatedata_new['status'] = 'closed'; //new duplicate challenge// $newentry['matchkey'] = $findchallenge->matchkey; $newentry['name'] = $findchallenge->name; $newentry['challenge_id'] = $findchallenge->id; $newentry['entryfee'] = $findchallenge->entryfee; $newentry['challenge_type'] = $findchallenge->challenge_type; $newentry['win_amount'] = $findchallenge->win_amount; $newentry['maximum_user'] = $findchallenge->maximum_user; $newentry['bonus'] = $findchallenge->bonus; $newentry['bonus_precent'] = $findchallenge->bonus_precent; $newentry['multi_entry'] = $findchallenge->multi_entry; $newentry['confirmed_challenge'] = $findchallenge->confirmed_challenge; $newentry['is_running'] = $findchallenge->is_running; $newentry['challenge_category_id'] = $findchallenge->challenge_category_id; $findifexist = DB::connection('mysql-write')->table('matchchallenges')->where('challenge_id',$findchallenge->id)->get(); if(empty($findifexist)) { $getcid = DB::connection('mysql-write')->table('matchchallenges')->insertGetId($newentry); // $sql = "INSERT INTO `matchchallenges` (`matchkey`, name, challenge_id, entryfee, challenge_type, winning_percentage, win_amount, maximum_user, bonus, bonus_precent, multi_entry, confirmed_challenge, is_running, challenge_category_id) SELECT '".$newentry['matchkey']."', '', '".$newentry['challenge_id']."', ".$newentry['entryfee']." , 'money', ".$newentry['winning_percentage'].", ".$newentry['win_amount'].", ".$newentry['maximum_user'].", ".$newentry['bonus'].", ".$newentry['bonus_precent'].", ".$newentry['multi_entry'].", ".$newentry['confirmed_challenge'].", ".$newentry['is_running'].", ".$newentry['challenge_category_id']." FROM DUAL WHERE (SELECT COUNT(id) FROM matchchallenges WHERE challenge_id = ".$newentry['challenge_id']." AND matchkey = '".$newentry['matchkey']."') < 1"; // DB::insert($sql); // $challenge_insert_id = DB::connection('mysql-write')->table('matchchallenges')->where('challenge_id',$newentry['challenge_id'])->where('matchkey',$newentry['matchkey'])->first(); // $getcid = $challenge_insert_id->id; $findpricecards = DB::connection('mysql-write')->table('matchpricecards')->where('challenge_id',$findchallenge->id)->where('matchkey',$findchallenge->matchkey)->get(); if(!empty($findpricecards)){ $findpricecardexist = DB::connection('mysql-write')->table('matchpricecards')->where('challenge_id',$getcid)->where('matchkey',$findchallenge->matchkey)->get(); if(empty($findpricecardexist)) { foreach($findpricecards as $pricec){ $pdata = array(); $pdata['challenge_id'] = $getcid; $pdata['matchkey'] = $findchallenge->matchkey; $pdata['winners'] = $pricec->winners; $pdata['price'] = $pricec->price; $pdata['min_position'] = $pricec->min_position; $pdata['max_position'] = $pricec->max_position; $pdata['description'] = $pricec->description; $pdata['total'] = $pricec->total; DB::table('matchpricecards'); DB::table('matchpricecards')->insert($pdata); } } } } } } //$updatedata_new['status'] = 'closed'; $updatedata_new['joinedusers'] = $cur_joined+1; DB::table('matchchallenges')->where('id',$challengeid)->update($updatedata_new); $Json[0]['message'] = 'leauge joined'; $Json[0]['status'] = true; $Json[0]['grand'] = $findchallenge->grand; $Json[0]['joinedusers'] = $updatedata_new['joinedusers']; if($findchallenge->is_private!="" || $findchallenge->is_private!=0){ $Json[0]['is_private'] = true; } else{ $Json[0]['is_private'] = false; } if($findchallenge->multi_entry!="" || $findchallenge->multi_entry!=0){ $Json[0]['multi_entry'] = true; } else{ $Json[0]['multi_entry'] = false; } $Json[0]['refercode'] = $data['refercode']; echo json_encode($Json); die; }