public function index(Request $request)
 {
     $members = Member::where('status', 1)->orWhere('status', 2)->get();
     $login = Login::where('remember_token', '=', $request->header('token'))->where('login_from', '=', $request->ip())->join('members', 'members.id', '=', 'logins.member_id')->where('logins.status', '=', '1')->first();
     $server_status = MetaData::select('meta_value')->where('meta_key', 'server_status')->first();
     if ($login->mtype == 3 || $login->mtype == 1) {
         foreach ($members as $member) {
             $accounts = Account::where('memberId', '=', $member->id)->get();
             $amount = 0;
             foreach ($accounts as $account) {
                 if ($account->type == 1) {
                     $amount += $account->amount;
                 } else {
                     $amount -= $account->amount;
                 }
             }
             $clientStocks = ClientStock::where('memberId', $member->id)->where('status', '<', 6)->get();
             $pending = 0;
             if (count($clientStocks) > 0) {
                 foreach ($clientStocks as $clientStock) {
                     if ($clientStock->status == 1) {
                         $pending += $clientStock->margin + $clientStock->commission;
                     } elseif ($clientStock->status == 2 || $clientStock->status == 5) {
                         $pending += $clientStock->margin + $clientStock->commission + $clientStock->holding_cost;
                     } elseif ($clientStock->status == 4 || $clientStock->status == 6) {
                         $pending += $clientStock->margin + $clientStock->commission + $clientStock->holding_cost + $clientStock->delivery_charge;
                     } else {
                     }
                 }
             }
             $member->pending = $pending;
             $member->amount = $amount;
             $user[] = $member;
         }
     } else {
         //$user = $members;
         foreach ($members as $member) {
             if ($member->id == $login->member_id) {
                 $accounts = Account::where('memberId', '=', $member->id)->get();
                 $amount = 0;
                 foreach ($accounts as $account) {
                     if ($account->type == 1) {
                         $amount += $account->amount;
                     } else {
                         $amount -= $account->amount;
                     }
                 }
                 $clientStocks = ClientStock::where('memberId', $member->id)->where('status', '<', 5)->get();
                 $pending = 0;
                 if (count($clientStocks) > 0) {
                     foreach ($clientStocks as $clientStock) {
                         if ($clientStock->status == 1) {
                             $pending += $clientStock->margin + $clientStock->commission;
                         } elseif ($clientStock->status == 2 || $clientStock->status == 5) {
                             $pending += $clientStock->margin + $clientStock->commission + $clientStock->holding_cost;
                         } elseif ($clientStock->status == 4 || $clientStock->status == 6) {
                             $pending += $clientStock->margin + $clientStock->commission + $clientStock->holding_cost + $clientStock->delivery_charge;
                         } else {
                         }
                     }
                 }
                 $member->pending = $pending;
                 $member->amount = $amount;
             }
             $user[] = $member;
         }
     }
     $returnData = array('status' => 'ok', 'members' => $user, 'user' => $login->member_id, 'server_status' => $server_status['meta_value'], 'code' => 200);
     return $returnData;
 }
 public function transferToDelivery(Request $request)
 {
     $metaData = MetaData::where('meta_key', 'server_status')->first();
     if ($metaData['meta_value'] == 0) {
         $errorData = array('status' => 'fail', 'message' => 'Sorry! Server is closed. Please try later.', 'code' => '422');
         return Response::json($errorData, 422);
     }
     $login = Login::where('remember_token', '=', $request->header('token'))->where('login_from', '=', $request->ip())->join('members', 'members.id', '=', 'logins.member_id')->where('logins.status', '=', '1')->first();
     $clientStock = ClientStock::find($request['clientStockId']);
     $account = new Account();
     if ($account->getAccount($login->member_id) < $clientStock->remaining_cost + $clientStock->delivery_charge) {
         $errorData = array('status' => 'fail', 'message' => 'Insufficient Balance!', 'code' => '422');
         return Response::json($errorData, 422);
     }
     if ($clientStock->memberId != $login->member_id) {
         $errorData = array('status' => 'fail', 'message' => 'Invalid Request!', 'code' => '422');
         return Response::json($errorData, 422);
     } else {
         $clientStock->status = 4;
         $clientStock->save();
         $account = new Account();
         $account->ticket = $clientStock->ticket;
         $account->amount = $clientStock->remaining_cost + $clientStock->delivery_charge;
         $account->type = 0;
         $account->addedBy = $login->member_id;
         $account->memberId = $clientStock->memberId;
         $account->save();
         $returnData = array('status' => 'ok', 'code' => '200', 'clientStock' => $clientStock, 'account' => $account);
         return Response::json($returnData, 200);
     }
 }