Skip to content

Instantly share code, notes, and snippets.

@elrok123
Created April 24, 2015 21:45
Show Gist options
  • Save elrok123/966611c76f5318d699bb to your computer and use it in GitHub Desktop.
Save elrok123/966611c76f5318d699bb to your computer and use it in GitHub Desktop.
Sell shares method, from an investor portfolio application that I wrote for university
# Sell method for the sell view StockServ, this method is used to sell the clients shares
def sell
# Define client variable with the client ID, used to locate specific client info from database
@client = Client.find(params[:client_id])
# This if statement checks if the user is selling a specific number of shares or whether they just wish to sell all of their shares
#
# Check to see if all required paramaters have been sent, if not check to see if sell all has been set,
# if so perform else, if none of these conditions are met, the else returns the user back to the original page and displays an error
if !params[:sell_quantity].nil? && !params[:owned_share_id].nil? && !params[:shares_quantity].nil? && !params[:sell_quantity].nil?
# Define company details (only available within the controller) use the sent share id to pull the company
# information from the database, and use the relations defined in the model/schema to access and retrieve the
# company details
company_details = get_company_details(@client.owned_shares.find(params[:owned_share_id].to_i).share.company_tag)
# Set the clients funds to the current close of the stock, multiplied by the number of shares the user wishes to sell
@client.funds = @client.funds + (company_details[0].close.to_f * params[:shares_quantity].to_f)
# Change the clients shares owned to the new quantity
@client.owned_shares.find(params[:owned_share_id]).update(quantity: (@client.owned_shares.find(params[:owned_share_id]).quantity - params[:shares_quantity].to_i))
#Check to see if the details have been updated, if not it returns an error and sends the user back to the porfolios page
if @client.save
redirect_to "/portfolios/" + params[:client_id].to_s, flash: {success: "Successfully sold shares..."}
else
redirect_to "/portfolios/" + params[:client_id].to_s, flash: {error: "Problem selling shares.. "}
end
elsif !params[:sell_all].nil? && !params[:owned_share_id].nil?
# Define company details (only available within the controller) use the sent share id to pull the company
# information from the database, and use the relations defined in the model/schema to access and retrieve the
# company details
company_details = get_company_details(@client.owned_shares.find(params[:owned_share_id].to_i).share.company_tag)
# Define quantity of shares to sell by retrieving the clients owned quantity of shares
quantity_to_sell = @client.owned_shares.find(params[:owned_share_id]).quantity
# Remove the owned share from the database as the client no longer owns any shares
@client.owned_shares.find(params[:owned_share_id]).destroy
# Set the clients funds to the current close of the stock, multiplied by the number of shares the client owned
@client.funds = @client.funds + (company_details[0].close.to_f * quantity_to_sell.to_f)
#Check to see if the details have been updated, if not it returns an error and sends the user back to the porfolios page
if @client.save
redirect_to "/portfolios/" + params[:client_id].to_s, flash: {success: "Successfully sold shares..."}
else
redirect_to "/portfolios/" + params[:client_id].to_s, flash: {error: "Problem selling shares.. "}
end
else
redirect_to "/portfolios/" + params[:client_id].to_s, flash: {error: "Problem selling shares.. "}
end
end
#Simple method making use of the yahoo finance gem for Ruby to return company/stock
#details using the stock symbol of the company in question. If no symbol passed, default to Google's 'GOOG' symbol
def get_company_details(search_term="GOOG")
YahooFinance.quotes([search_term.to_s.upcase], [:high, :name, :symbol, :low, :open, :close])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment