public function CreateTrade() { $userRep = new UsersRepository(); $portfolioRep = new PortfoliosRepository(); $tradeRep = new TradesRepository(); $user = Auth::user(); $message = ""; if ($user) { $profile = Profile::find(Input::get('profile_id')); $portfolio = Portfolio::find(Input::get('portfolio_id')); if ($portfolio->user_id == $user->id) { if ($profile->current_price > 0) { $sharestaken = Input::get('shares_taken'); $totalCost = Input::get('shares_taken') * $profile->current_price; if ($portfolio->balance >= $totalCost) { $trade = $tradeRep->GetActiveProfilePortfolioTrade($profile->id, $portfolio->id); if ($trade !== null) { $trade->shares_taken = $trade->shares_taken + $sharestaken; $trade->price_taken = $profile->current_price; } else { $trade = new Trade(); $trade->portfolio_id = $portfolio->id; $trade->profile_id = $profile->id; $trade->shares_taken = $sharestaken; $trade->price_taken = $profile->current_price; $trade->price_sold = 0; $trade->is_active = 1; } $trade->save(); $portfolioRep->DecreaseBalance($portfolio, $totalCost); return response()->json(array('success' => true, 'trade' => $trade)); } else { $message = "You can't afford this profile"; } } else { $message = "The profile doesn't currently have a price"; } } else { $message = "You don't have access to this portfolio"; } } else { $message = "You're not currently logged in"; } return response()->json(array('success' => false, 'message' => $message)); }
/** * Store a partially/fully filled trade in storage. * * @return \Illuminate\Http\Response */ public function store_final() { $trades = inboundFixTrade::where('new', true)->get(); $message = null; // $trader_name=null; foreach ($trades as $trade) { $trade->new = 0; $trade->update(); $name = $trade->trader_name; $name_array = explode(' ', $name); $last_name = array_pop($name_array); $first_name = array_pop($name_array); $user = User::where('last_name', $last_name)->where('first_name', $first_name)->first(); $trader_id = $user->id; // $message = str_replace(SOH,'^', $trade->fix_message); $message = explode(SOH, $trade->fix_message); $array = array(); foreach ($message as $key) { $key_value = explode('=', $key); $value = array_pop($key_value); $key = array_pop($key_value); $array[$key] = $value; } if ($array['39'] != '0') { //if it is a partial of full fill otherwise ignore // $trade_id = $array['11']; $company = $array['56']; $buy_sell = $array['54']; $lots = $array['38']; if ($buy_sell == '1') { $lots = -$lots; } $contract = $array['55']; $symbol = substr($contract, 0, -2); $dates = substr($contract, -2); $month = substr($dates, 0, 1); $year = substr($dates, -1); $order_type = $array['40']; $price = $array['44']; $fin_trade = new Trade(); $fin_trade->trader_name = $name; $fin_trade->trader_id = $trader_id; $fin_trade->company = $company; $fin_trade->buy_sell = $buy_sell; $fin_trade->lots = $lots; $fin_trade->expiry_month = $month; $fin_trade->expiry_year = $year; $fin_trade->symbol = $symbol; $fin_trade->contract = $contract; $fin_trade->order_type = $order_type; $fin_trade->price = $price; $fin_trade->save(); } $message = $symbol; } return; }