Skip to content

Commit

Permalink
Merge pull request #4 from BrandiPhillips/sale_and_product_loops
Browse files Browse the repository at this point in the history
Sale and product loops added with basic functionality
  • Loading branch information
eabrash committed Oct 6, 2016
2 parents 894199e + a74f55e commit 6a7aa39
Show file tree
Hide file tree
Showing 15 changed files with 8,222 additions and 32 deletions.
18 changes: 18 additions & 0 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,35 @@ def show
end

def new
@product = Product.new
end

def create
Product.create(name: params[:product][:name], vendor_id: current_user.id)
redirect_to controller: "products", action: "index"
end

def edit
@product = Product.find(params[:id])
end

def update
@product = Product.find(params[:id])
@product.update(name: params[:product][:name])
redirect_to controller: "products", action: "index"
end

# Deletes a product and all its associated sales

def destroy
product = Product.find(params[:id])

product.sales.each do |sale|
sale.destroy
end

product.delete

redirect_to controller: "products", action: "index"
end
end
6 changes: 6 additions & 0 deletions app/controllers/sales_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ def index
end

def show
@vendor = Vendor.find(current_user.id)
@time_period = params[:period]
end

def new
@sale = Sale.new
@vendor = Vendor.find(current_user.id)
end

def create
@sale = Sale.create(amount: (Float(params[:sale][:amount])*100).round, vendor_id: current_user.id.to_i, product_id: params[:sale][:product_id].to_i, purchase_time: DateTime.now)
redirect_to sales_show_path(:period => :all)
end

def edit
Expand Down
1 change: 1 addition & 0 deletions app/controllers/vendors_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class VendorsController < ApplicationController
def index
@vendor = Vendor.find(current_user.id)
end

def show
Expand Down
1 change: 1 addition & 0 deletions app/models/sale.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Sale < ActiveRecord::Base
belongs_to :vendor
belongs_to :product

end
27 changes: 27 additions & 0 deletions app/models/vendor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,31 @@ class Vendor < ActiveRecord::Base
belongs_to :market
has_many :sales
has_many :products

# total sales for a vendor, in cents

def total_sales
total = 0

self.sales.each do |sale|
total += sale.amount
end

return total

end

def this_months_sales
total = 0

self.sales.each do |sale|
if sale.purchase_time.month == DateTime.now.month && sale.purchase_time.year == DateTime.now.year
total += sale.amount
end
end

return total

end

end
14 changes: 12 additions & 2 deletions app/views/products/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
<h1>Products#edit</h1>
<p>Find me in app/views/products/edit.html.erb</p>
<h1>Edit your product</h1>

<p>Please edit the name of your new product in the space below. When you are finished, press submit!</p>

<section>
<%= form_for @product, method: 'put', url: products_update_path do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
</section>
15 changes: 7 additions & 8 deletions app/views/products/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<h1>Products#index</h1>
<h1>My products</h1>

<p><%= "Hi #{current_user.name}! Here are the products you currently sell: " %></p>
<p><%= "Hello, #{current_user.name}! Here are the products you currently sell: " %></p>

<p><% current_user.products.each do |product| %>
<p><%= "Name: #{product.name}, ID: #{product.id}" %>
<%= button_to "Edit", products_edit_path(product.id)%>
<%= button_to "Delete", products_destroy_path(product.id)%></p>
<%= button_to "Edit", products_edit_path(product.id), method: :get %>
<%= button_to("Delete", products_destroy_path(product.id), :data => {:confirm => 'Are you sure? Deleting a product also deletes all sales associated with that product.'}, :method => 'delete') %></li>
<%end%></p>

<p>What would you like to do?</p>
<p><%= link_to "Add", markets_new_path %></p>
<p><%= link_to "See my own market's stats", markets_show_path(current_user.id)%></p>
<p><%= link_to "Add a vendor to my market", vendors_new_path %></p>
<p><%= button_to "Add a product", products_new_path, method: :get %></p>

<p><%= link_to "Back to vendor home", vendors_index_path %> </p>
13 changes: 11 additions & 2 deletions app/views/products/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
<h1>Products#new</h1>
<p>Find me in app/views/products/new.html.erb</p>
<h1>Add a new product!</h1>
<p>Please enter the name of your new product in the space below. When you are finished, press submit!</p>

<section>
<%= form_for @product, method: 'post', url: products_create_path do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
</section>
10 changes: 8 additions & 2 deletions app/views/sales/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
<h1>Sales#index</h1>
<p>Find me in app/views/sales/index.html.erb</p>
<h1>My sales</h1>
<p>See what you've sold and add new sales!</p>


<p><%= link_to "All my sales", sales_show_path(:period => :all), method: :get %></p>
<p><%= link_to "This month's sales", sales_show_path(:period => :this_month), method: :get %></p>
<p><%= link_to "Add a sale", sales_new_path, method: :get %></p>
<p><%= link_to "Return to vendor home", vendors_index_path, method: :get %></p>
15 changes: 13 additions & 2 deletions app/views/sales/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
<h1>Sales#new</h1>
<p>Find me in app/views/sales/new.html.erb</p>
<h1>Add a new sale </h1>

<section>
<%= form_for @sale, method: 'post', url: sales_create_path do |f| %>
<%= f.label :amount %>
<%= f.text_field :amount %>
<%= f.label :product_id %>
<%= f.collection_select :product_id, @vendor.products.all, :id, :name %>
<%= f.submit %>
<% end %>
</section>
22 changes: 20 additions & 2 deletions app/views/sales/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
<h1>Sales#show</h1>
<p>Find me in app/views/sales/show.html.erb</p>
<% if @time_period == 'this_month' %>
<h1>Sales for <%= DateTime.now.strftime("%B %Y")%></h1>

<% @vendor.sales.each do |sale| %>
<% if sale.purchase_time.month == DateTime.now.month && sale.purchase_time.year == DateTime.now.year %>
<p><%= "ID: #{sale.id}, Amount: $#{format("%.2f", sale.amount/100.0)}, Product: #{sale.product.name}, Sold at: #{sale.purchase_time}" %></p>
<% end %>
<% end %>
<% else%>
<h1>All my sales</h1>

<p> Total sales: $<%= @vendor.total_sales/100.0%> </p>

<% @vendor.sales.each do |sale| %>
<p><%= "ID: #{sale.id}, Amount: $#{format("%.2f", sale.amount/100.0)}, Product: #{sale.product.name}, Sold at: #{sale.purchase_time}" %></p>
<% end %>
<% end %>

<p><%= link_to "Back to sales home", sales_index_path, method: :get%></p>
19 changes: 14 additions & 5 deletions app/views/vendors/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
<h1>Vendors#index</h1>
<h1>Welcome, <%= @vendor.name %>!</h1>

<p><%= "Hello, vendor #{current_user.name}! What would you like to do today?"%></p>
<p><%= link_to "See my vendor stats", vendors_show_path(current_user.id)%></p>
<p><%= link_to "Add, edit, or delete products", products_index_path %></p>
<p><%= link_to "Add a new sale", sales_index_path %></p>
<h2>According to our records...</h2>

<ul>
<li>You have <%= @vendor.num_employees %> employees</li>
<li>You sell at <%= @vendor.market.name %> </li>
<li>Your vendor ID is: <%= @vendor.id %> </li>
</ul>

<p>To update any of this information, please contact the administrator of your market. Contact details for your market can be found at <%= link_to "#{@vendor.market.name}", users_show_path(@vendor.market.id) %>.</p>

<h2>What would you like to do today?</h2>
<p><%= link_to "Go to my products", products_index_path %></p>
<p><%= link_to "Go to my sales", sales_index_path %></p>
18 changes: 9 additions & 9 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,29 @@

get 'sales/new'

get 'sales/create'
post 'sales/create'

get 'sales/edit'
get 'sales/:id/edit' => 'sales#edit'

get 'sales/update'
put 'sales/:id/update' => 'sales#update'

get 'sales/destroy'
delete 'sales/:id/destroy' => 'sales#destroy'

# Product controller routes

get 'products/index'

get 'products/show'
get 'products/show/:id' => 'products#show', as: 'products_show'

get 'products/new'

get 'products/create'
post 'products/create'

get 'products/edit'
get 'products/:id/edit' => 'products#edit', as: 'products_edit'

get 'products/update'
put 'products/:id/update' => 'products#update', as: 'products_update'

get 'products/destroy'
delete 'products/:id/destroy' => 'products#destroy', as: 'products_destroy'

# Vendors controller routes

Expand Down
Binary file modified db/development.sqlite3
Binary file not shown.
Loading

0 comments on commit 6a7aa39

Please sign in to comment.