public function getBalance()
 {
     $getbalance = Rechargeurl::balance();
     $balance = $getbalance[0];
     $date = $getbalance[1];
     $status = $getbalance[2];
     return Response::json(array('balance' => $balance, 'status' => $status));
 }
 public function postDashboard()
 {
     $userID = Input::get('created_by');
     $userIDPK = Input::get('userIDPK');
     $userRole = User::where('UD_USER_ID', '=', $userID)->get();
     foreach ($userRole as $ur) {
         $userType = $ur->UD_USER_TYPE;
     }
     if ($userType == "SA" || $userType == "SAS") {
         $successRecAmount = 0;
         $failureRecAmount = 0;
         $startDate = date('Y-m-d') . " 00:00:00";
         $endDate = date('Y-m-d') . " 23:59:59";
         //recharge stats
         $successRechargeRecords = Ledgerreport::where('lr_comment', '=', 'Recharge')->where('lr_status', '=', 'success')->whereBetween('lr_date', array($startDate, $endDate))->get();
         foreach ($successRechargeRecords as $srr) {
             $successRecAmount = $successRecAmount + $srr->lr_debit_amount;
         }
         $failureRechargeRecords = Ledgerreport::where('lr_comment', '=', 'Recharge')->where('lr_status', '=', 'failure')->whereBetween('lr_date', array($startDate, $endDate))->get();
         foreach ($failureRechargeRecords as $srr) {
             $failureRecAmount = $failureRecAmount + $srr->lr_debit_amount;
         }
         //icash balance
         $icashBalance = Userfinance::where('ufin_user_id', '=', $userID)->get();
         $icashBal = 0;
         foreach ($icashBalance as $bal) {
             $icashBal = $icashBal + $bal->ufin_icash_balance;
         }
         //User count
         $SA = count(User::where('UD_USER_TYPE', '=', 'SA')->get());
         $SAS = count(User::where('UD_USER_TYPE', '=', 'SAS')->get());
         $SP = count(User::where('UD_USER_TYPE', '=', 'SP')->get());
         $SPS = count(User::where('UD_USER_TYPE', '=', 'SPS')->get());
         $SD = count(User::where('UD_USER_TYPE', '=', 'SD')->get());
         $SDS = count(User::where('UD_USER_TYPE', '=', 'SDS')->get());
         $D = count(User::where('UD_USER_TYPE', '=', 'D')->get());
         $DS = count(User::where('UD_USER_TYPE', '=', 'DS')->get());
         $FR = count(User::where('UD_USER_TYPE', '=', 'FR')->get());
         $FRS = count(User::where('UD_USER_TYPE', '=', 'FRS')->get());
         $SFR = count(User::where('UD_USER_TYPE', '=', 'SFR')->get());
         $SFRS = count(User::where('UD_USER_TYPE', '=', 'SFRS')->get());
         //User Balance
         $SAbalance = Commonmodel::fetchUserdetails('SA');
         $SASbalance = Commonmodel::fetchUserdetails('SAS');
         $SPbalance = Commonmodel::fetchUserdetails('SP');
         $SPSbalance = Commonmodel::fetchUserdetails('SPS');
         $SDbalance = Commonmodel::fetchUserdetails('SD');
         $SDSbalance = Commonmodel::fetchUserdetails('SDS');
         $Dbalance = Commonmodel::fetchUserdetails('D');
         $DSbalance = Commonmodel::fetchUserdetails('DS');
         $FRbalance = Commonmodel::fetchUserdetails('FR');
         $FRSbalance = Commonmodel::fetchUserdetails('FRS');
         $SFRbalance = Commonmodel::fetchUserdetails('SFR');
         $SFRSbalance = Commonmodel::fetchUserdetails('SFRS');
         //Ezypay
         $getbalance = Rechargeurl::balance();
         $ezbalance = $getbalance[0];
         $output = array('Success_Recharge_Amount' => $successRecAmount, 'Failure_Recharge_Amount' => $failureRecAmount, 'SA' => $SA, 'SAS' => $SAS, 'SP' => $SP, 'SPS' => $SPS, 'SD' => $SD, 'SDS' => $SDS, 'D' => $D, 'DS' => $DS, 'FR' => $FR, 'FRS' => $FRS, 'SFR' => $SFR, 'SFRS' => $SFRS, 'Ezpay_Balance' => $ezbalance, 'icash_Balance' => $icashBal, 'SA_Balance' => $SAbalance, 'SAS_Balance' => $SASbalance, 'SP_Balance' => $SPbalance, 'SPS_Balance' => $SPSbalance, 'SD_Balance' => $SDbalance, 'SDS_Balance' => $SDSbalance, 'D_Balance' => $Dbalance, 'DS_Balance' => $DSbalance, 'FR_Balance' => $FRbalance, 'FRS_Balance' => $FRSbalance, 'SFR_Balance' => $SFRbalance, 'SFRS_Balance' => $SFRSbalance);
     } elseif ($userType == "SP") {
         $SPbalance = DB::table('adt_user_finance')->leftjoin('adt_user_details', 'adt_user_finance.ufin_user_id', '=', 'adt_user_details.UD_USER_ID')->where('adt_user_details.UD_PARENT_ID', '=', $userIDPK)->get();
         $spartBalance = 0;
         if ($SPbalance) {
             foreach ($SPbalance as $sp) {
                 $spaBalance = $sp->ufin_main_balance;
             }
             $Daccounts = count(User::where('UD_PARENT_ID', '=', $userIDPK)->get());
             $SDbalance = DB::table('adt_user_finance')->leftjoin('adt_user_details', 'adt_user_finance.ufin_user_id', '=', 'adt_user_details.UD_USER_ID')->where('adt_user_details.UD_PARENT_ID', '=', $userIDPK)->get();
             $FRtotalregamount = Commonmodel::fetchRechargedetails($userID);
             $disbal = 0;
             if ($SDbalance) {
                 foreach ($SDbalance as $frs) {
                     $disbal = $disbal + $frs->ufin_main_balance;
                 }
                 $output = array('Statepartner_Balance' => $spaBalance, 'Distributor_accounts' => $Daccounts, 'Distributor_balance' => $disbal);
             } else {
                 $output = array('Statepartner_Balance' => $spaBalance, 'Distributor_accounts' => $Daccounts, 'Distributor_balance' => '0');
             }
         } else {
             $output = array('status' => 'failure', 'message' => 'No results found');
         }
     } elseif ($userType == "SPS") {
         $SPbalance = DB::table('adt_user_finance')->leftjoin('adt_user_details', 'adt_user_finance.ufin_user_id', '=', 'adt_user_details.UD_USER_ID')->where('adt_user_details.UD_ID_PK', '=', $userIDPK)->get();
         $spartBalance = 0;
         if ($SPbalance) {
             foreach ($SPbalance as $sp) {
                 $spaBalance = $sp->ufin_main_balance;
                 $parentid = $sp->UD_PARENT_ID;
             }
             $Daccounts = count(User::where('UD_PARENT_ID', '=', $parentid)->get());
             $SDbalance = DB::table('adt_user_finance')->leftjoin('adt_user_details', 'adt_user_finance.ufin_user_id', '=', 'adt_user_details.UD_USER_ID')->where('adt_user_finance.ufin_user_id_pk_fk', '=', $parentid)->get();
             $disbal = 0;
             if ($SDbalance) {
                 foreach ($SDbalance as $frs) {
                     $disbal = $disbal + $frs->ufin_main_balance;
                 }
                 $output = array('Statepartner_Balance' => $spaBalance, 'Distributor_accounts' => $Daccounts, 'Distributor_balance' => $disbal);
             } else {
                 $output = array('Statepartner_Balance' => $spaBalance, 'Distributor_accounts' => $Daccounts, 'Distributor_balance' => '0');
             }
         } else {
             $output = array('status' => 'failure', 'message' => 'No results found');
         }
     } elseif ($userType == "SD") {
         $SDbalance = DB::table('adt_user_finance')->leftjoin('adt_user_details', 'adt_user_finance.ufin_user_id', '=', 'adt_user_details.UD_USER_ID')->where('adt_user_details.UD_USER_ID', '=', $userID)->get();
         $sdiBalance = 0;
         if ($SDbalance) {
             foreach ($SDbalance as $sp) {
                 $sdiBalance = $sp->ufin_main_balance;
             }
             $Daccounts = count(User::where('UD_PARENT_ID', '=', $userIDPK)->get());
             $Disbalance = DB::table('adt_user_finance')->leftjoin('adt_user_details', 'adt_user_finance.ufin_user_id', '=', 'adt_user_details.UD_USER_ID')->where('adt_user_details.UD_PARENT_ID', '=', $userIDPK)->get();
             $FRtotalregamount = Commonmodel::fetchRechargedetails($userID);
             $disbal = 0;
             if ($Disbalance) {
                 foreach ($Disbalance as $frs) {
                     $disbal = $disbal + $frs->ufin_main_balance;
                 }
                 $output = array('Super_Distributer_Balance' => $sdiBalance, 'Distributor_accounts' => $Daccounts, 'Distributor_balance' => $disbal);
             } else {
                 $output = array('status' => 'failure', 'message' => 'No results found');
             }
         } else {
             $output = array('status' => 'failure', 'message' => 'No results found');
         }
     } elseif ($userType == "SDS") {
         $SDbalance = DB::table('adt_user_finance')->leftjoin('adt_user_details', 'adt_user_finance.ufin_user_id', '=', 'adt_user_details.UD_USER_ID')->where('adt_user_details.UD_USER_ID', '=', $userID)->get();
         $sdiBalance = 0;
         if ($SDbalance) {
             foreach ($SDbalance as $sp) {
                 $sdiBalance = $sp->ufin_main_balance;
                 $parentid = $sp->UD_PARENT_ID;
             }
             $Daccounts = count(User::where('UD_PARENT_ID', '=', $parentid)->get());
             $Disbalance = DB::table('adt_user_finance')->leftjoin('adt_user_details', 'adt_user_finance.ufin_user_id', '=', 'adt_user_details.UD_USER_ID')->where('adt_user_finance.ufin_user_id_pk_fk', '=', $parentid)->get();
             $FRtotalregamount = Commonmodel::fetchRechargedetails($userID);
             $disbal = 0;
             if ($Disbalance) {
                 foreach ($Disbalance as $frs) {
                     $disbal = $disbal + $frs->ufin_main_balance;
                 }
                 $output = array('Super_Distributer_Balance' => $sdiBalance, 'Distributor_accounts' => $Daccounts, 'Distributor_balance' => $disbal);
             } else {
                 $output = array('status' => 'failure', 'message' => 'No results found');
             }
         } else {
             $output = array('status' => 'failure', 'message' => 'No results found');
         }
     } elseif ($userType == "D") {
         $SDbalance = DB::table('adt_user_finance')->leftjoin('adt_user_details', 'adt_user_finance.ufin_user_id', '=', 'adt_user_details.UD_USER_ID')->where('adt_user_details.UD_USER_ID', '=', $userID)->get();
         $sdiBalance = 0;
         if ($SDbalance) {
             foreach ($SDbalance as $sp) {
                 $sdiBalance = $sp->ufin_main_balance;
             }
             $Daccounts = count(User::where('UD_PARENT_ID', '=', $userIDPK)->get());
             $Disbalance = DB::table('adt_user_finance')->leftjoin('adt_user_details', 'adt_user_finance.ufin_user_id', '=', 'adt_user_details.UD_USER_ID')->where('adt_user_details.UD_PARENT_ID', '=', $userIDPK)->get();
             $FRtotalregamount = Commonmodel::fetchRechargedetails($userID);
             $disbal = 0;
             if ($Disbalance) {
                 foreach ($Disbalance as $frs) {
                     $disbal = $disbal + $frs->ufin_main_balance;
                 }
                 $output = array('Distributer_Balance' => $sdiBalance, 'Franchise_accounts' => $Daccounts, 'Franchise_balance' => $disbal);
             } else {
                 $output = array('status' => 'failure', 'message' => 'No results found');
             }
         } else {
             $output = array('status' => 'failure', 'message' => 'No results found');
         }
     } elseif ($userType == "DS") {
         $SDbalance = DB::table('adt_user_finance')->leftjoin('adt_user_details', 'adt_user_finance.ufin_user_id', '=', 'adt_user_details.UD_USER_ID')->where('adt_user_details.UD_USER_ID', '=', $userID)->get();
         $sdiBalance = 0;
         if ($SDbalance) {
             foreach ($SDbalance as $sp) {
                 $sdiBalance = $sp->ufin_main_balance;
                 $parentid = $sp->UD_PARENT_ID;
             }
             $Daccounts = count(User::where('UD_PARENT_ID', '=', $parentid)->get());
             $Disbalance = DB::table('adt_user_finance')->leftjoin('adt_user_details', 'adt_user_finance.ufin_user_id', '=', 'adt_user_details.UD_USER_ID')->where('adt_user_finance.ufin_user_id', '=', $parentid)->get();
             $FRtotalregamount = Commonmodel::fetchRechargedetails($userID);
             $disbal = 0;
             if ($Disbalance) {
                 foreach ($Disbalance as $frs) {
                     $disbal = $disbal + $frs->ufin_main_balance;
                 }
                 $output = array('Distributer_Balance' => $sdiBalance, 'Franchise_accounts' => $Daccounts, 'Franchise_balance' => $disbal);
             } else {
                 $output = array('status' => 'failure', 'message' => 'No results found');
             }
         } else {
             $output = array('status' => 'failure', 'message' => 'No results found');
         }
     } elseif ($userType == "FR") {
         $FRbalance = DB::table('adt_user_finance')->leftjoin('adt_user_details', 'adt_user_finance.ufin_user_id', '=', 'adt_user_details.UD_USER_ID')->where('adt_user_details.UD_USER_ID', '=', $userID)->get();
         $fraBalance = 0;
         if ($FRbalance) {
             foreach ($FRbalance as $fr) {
                 $fraBalance = $fr->ufin_main_balance;
                 $icashBalance = $fr->ufin_icash_balance;
             }
             $FRSaccounts = count(User::where('UD_PARENT_ID', '=', $userIDPK)->get());
             $SFRbalance = DB::table('adt_user_finance')->leftjoin('adt_user_details', 'adt_user_finance.ufin_user_id', '=', 'adt_user_details.UD_USER_ID')->where('adt_user_finance.ufin_user_id', '=', $userID)->get();
             $FRtotalregamount = Commonmodel::fetchRechargedetails($userID);
             $sfrbal = 0;
             if ($FRbalance) {
                 foreach ($SFRbalance as $frs) {
                     $sfrbal = $sfrbal + $frs->ufin_main_balance;
                 }
                 $output = array('Franchise_Balance' => $fraBalance, 'icash_Balance' => $icashBalance, 'Sub_franchise_accounts' => $FRSaccounts, 'Sub_franchise_balance' => $sfrbal, 'Total_recharge_amount' => $FRtotalregamount);
             } else {
                 $output = array('status' => 'failure', 'message' => 'No results found');
             }
         } else {
             $output = array('status' => 'failure', 'message' => 'No results found');
         }
     } elseif ($userType == "FRS") {
         $FRbalance = DB::table('adt_user_finance')->leftjoin('adt_user_details', 'adt_user_finance.ufin_user_id', '=', 'adt_user_details.UD_USER_ID')->where('adt_user_details.UD_USER_ID', '=', $userID)->get();
         $fraBalance = 0;
         if ($FRbalance) {
             foreach ($FRbalance as $fr) {
                 $fraBalance = $fr->ufin_main_balance;
                 $icashBalance = $fr->ufin_icash_balance;
                 $parentid = $fr->UD_PARENT_ID;
             }
             $FRSaccounts = count(User::where('UD_PARENT_ID', '=', $parentid)->get());
             $SFRbalance = DB::table('adt_user_finance')->leftjoin('adt_user_details', 'adt_user_finance.ufin_user_id', '=', 'adt_user_details.UD_USER_ID')->where('adt_user_finance.ufin_user_id', '=', $parentid)->get();
             $FRtotalregamount = Commonmodel::fetchRechargedetails($userID);
             $sfrbal = 0;
             if ($SFRbalance) {
                 foreach ($SFRbalance as $frs) {
                     $sfrbal = $sfrbal + $frs->ufin_main_balance;
                 }
                 $output = array('Franchise_Balance' => $fraBalance, 'icash_Balance' => $icashBalance, 'Sub_franchise_accounts' => $FRSaccounts, 'Sub_franchise_balance' => $sfrbal, 'Total_recharge_amount' => $FRtotalregamount);
             } else {
                 $output = array('status' => 'failure', 'message' => 'No results found');
             }
         } else {
             $output = array('status' => 'failure', 'message' => 'No results found');
         }
     } elseif ($userType == "SFR") {
         $FRbalance = DB::table('adt_user_finance')->leftjoin('adt_user_details', 'adt_user_finance.ufin_user_id', '=', 'adt_user_details.UD_USER_ID')->where('adt_user_details.UD_USER_ID', '=', $userID)->get();
         $fraBalance = 0;
         if ($FRbalance) {
             $FRtotalregamount = Commonmodel::fetchRechargedetails($userID);
             foreach ($FRbalance as $fr) {
                 $fraBalance = $fr->ufin_main_balance;
                 $icashBalance = $fr->ufin_icash_balance;
             }
             $output = array('Franchise_Balance' => $fraBalance, 'icash_Balance' => $icashBalance, 'Total_recharge_amount' => $FRtotalregamount);
         } else {
             $output = array('status' => 'failure', 'message' => 'No results found');
         }
     } elseif ($userType == "SFRS") {
         $FRbalance = DB::table('adt_user_finance')->leftjoin('adt_user_details', 'adt_user_finance.ufin_user_id', '=', 'adt_user_details.UD_USER_ID')->where('adt_user_details.UD_USER_ID', '=', $userID)->get();
         $fraBalance = 0;
         if ($FRbalance) {
             foreach ($FRbalance as $fr) {
                 $fraBalance = $fr->ufin_main_balance;
                 $icashBalance = $fr->ufin_icash_balance;
                 $parentid = $fr->UD_PARENT_ID;
             }
             $parentIDname = User::where('UD_ID_PK', '=', $parentid)->get();
             foreach ($parentIDname as $prntname) {
                 $parent_name = $prntname->UD_USER_ID;
             }
             $SFRbalance = DB::table('adt_user_finance')->leftjoin('adt_user_details', 'adt_user_finance.ufin_user_id', '=', 'adt_user_details.UD_USER_ID')->where('adt_user_finance.ufin_user_id_pk_fk', '=', $parentid)->get();
             $FRtotalregamount = Commonmodel::fetchRechargedetails($parent_name);
             $sfrbal = 0;
             if ($SFRbalance) {
                 foreach ($SFRbalance as $frs) {
                     $sfrbal = $sfrbal + $frs->ufin_main_balance;
                 }
                 $output = array('Franchise_Balance' => $sfrbal, 'icash_Balance' => $icashBalance, 'Total_recharge_amount' => $FRtotalregamount);
             } else {
                 $output = array('status' => 'failure', 'message' => 'No results found');
             }
         } else {
             $output = array('status' => 'failure', 'message' => 'No results found');
         }
     }
     return Response::json($output);
 }