Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,20 @@
✔ 身為消費者,當我下單時,我的信箱要能收到一張下單確認信 @done (14-06-13 17:44)
✔ 訂單狀態應分為未結帳、已結帳、出貨中、已出貨、辦理退貨 @done (14-06-13 17:44)
☐ 身為消費者,當我用信用卡結帳後,我的信箱要能收到一張訂購完成信
✔ 身為消費者,當我在結賬後,購物⾞應自動清空 @done (14-06-20 01:01)
✔ 清空購物⾞ @done (14-06-20 00:36)
✔ 身為消費者,我可以從購物車內刪掉某些物品 @done (14-06-20 00:41)
✔ 身為消費者,我可以從購物車內更新購買數量 @done (14-06-20 00:54)
✔ 購物車的總價 => 小計 x 數量 @done (14-06-20 00:54)
✔ 數量為 0 的貨物不能「購買」 @done (14-06-20 00:56)
✔ 訂單支援「多重購買數量」 @done (14-06-20 01:00)
✔ 建立 account/orders 可以看到該使用者過去所有訂單 @done (14-06-20 01:08)
✔ 結賬後跳轉到 account/orders#index @done (14-06-20 01:10)

Admin 管理訂單:
☐ 身為管理者,可以在後台看訂單,訂單狀態分為未結帳、已結帳、出貨中、已出貨、辦理退貨
☐ 身為管理者,我可以在後台對單張訂單做狀態改變
✔ 身為管理者,可以在後台看訂單,訂單狀態分為未結帳、已結帳、出貨中、已出貨、辦理退貨 @done (14-06-20 01:27)
✔ 身為管理者,我可以在後台對單張訂單做狀態改變 @done (14-06-20 01:56)
✔ admin 的 order 列表應要能顯示訂單狀態 @done (14-06-20 01:56)
☐ 身為管理者,當我在將商品設為已出貨時,消費者應該收到一張已出貨的通知信

☐ 身為商家,當消費者確定購物結帳後,該商品的庫存必須按照數量減少

Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/account/orders.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/javascripts/admin/orders.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/javascripts/cart_items.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/account/orders.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the account::orders controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/admin/orders.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the admin::orders controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/cart_items.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the cart_items controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
10 changes: 10 additions & 0 deletions app/controllers/account/orders_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Account::OrdersController < ApplicationController

before_action :authenticate_user!

def index
@orders = current_user.orders.recent
end


end
42 changes: 42 additions & 0 deletions app/controllers/admin/orders_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Admin::OrdersController < AdminController

before_filter :find_order, :except => [:index]

def index
@orders = Order.recent
end


def show

@order_info = @order.info
@order_items = @order.items
end

def ship
@order.ship!
redirect_to :back
end

def shipped
@order.deliver!
redirect_to :back
end

def cancel
@order.cancell_order!
redirect_to :back
end

def return
@order.return_good!
redirect_to :back
end

protected


def find_order
@order = Order.find_by_token(params[:id])
end
end
32 changes: 32 additions & 0 deletions app/controllers/cart_items_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class CartItemsController < ApplicationController
before_action :authenticate_user!


def update
@cart = current_cart
@item = @cart.cart_items.find(params[:id])

@item.update(item_params)

redirect_to carts_path
end


def destroy
@cart = current_cart
@item = @cart.cart_items.find(params[:id])

@item.destroy

flash[:warning] = "成功清空物品"
redirect_to :back

end

private

def item_params
params.require(:cart_item).permit(:quantity)
end

end
4 changes: 2 additions & 2 deletions app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def create
if @order.save
@order.build_item_cache_from_cart(current_cart)
@order.calculate_total!(current_cart)

current_cart.clear!
OrderMailer.notify_order_placed(@order).deliver

redirect_to order_path(@order.token)
Expand All @@ -29,7 +29,7 @@ def pay_with_credit_card

@order.make_payment! # TODO: should move to stripe callbacks

redirect_to root_path, :notice => "成功完成付款"
redirect_to account_orders_path, :notice => "成功完成付款"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

請問 看起來在這個地方 是不是應該還會有 後續處理 寄出 EMAIL 通知 信用卡付款成功的信件的相關動作的程式呢?

Thanks

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

這個在第三周

end

private
Expand Down
10 changes: 8 additions & 2 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ def add_to_cart
@product = Product.find(params[:id])

if !current_cart.items.include?(@product)
current_cart.add_product_to_cart(@product)
flash[:notice] = "你已成功將 #{@product.title} 加入購物車"

if @product.quantity > 0
current_cart.add_product_to_cart(@product, 1 )
flash[:notice] = "你已成功將 #{@product.title} 加入購物車"
else
flash[:warning] = "此物品已停止銷售,你無法將它加入購物車"
end

else
flash[:warning] = "你的購物車內已有此物品"
end
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/account/orders_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Account::OrdersHelper
end
8 changes: 8 additions & 0 deletions app/helpers/admin/orders_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Admin::OrdersHelper

def render_order_options_for_admin(order)

render :partial => "admin/orders/state_option", :locals => { :order => order}

end
end
2 changes: 2 additions & 0 deletions app/helpers/cart_items_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module CartItemsHelper
end
21 changes: 21 additions & 0 deletions app/helpers/orders_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@
module OrdersHelper

def render_order_link(order)
link_to(order.token, order_path(order.token))
end

def render_admin_order_link(order)
link_to(order.token, admin_order_path(order.token))
end

def render_order_created_time(order)
order.created_at.to_s(:short)
end


def render_order_user_name(user)
user.email
end

def render_order_state(order)
t("orders.order_state.#{order.aasm_state}")
end
end
13 changes: 10 additions & 3 deletions app/models/cart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ class Cart < ActiveRecord::Base
has_many :items, :through => :cart_items, :source => :product


def add_product_to_cart(product)
items << product
def add_product_to_cart(product, amount)
cart_item = cart_items.build
cart_item.product = product
cart_item.quantity = amount
cart_item.save
end

def total_price
items.inject(0) {|sum, item| sum + item.price }
cart_items.inject(0) {|sum, item| sum + (item.product.price * item.quantity) }
end

def clear!
cart_items.delete_all
end
end
9 changes: 5 additions & 4 deletions app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ class Order < ActiveRecord::Base

accepts_nested_attributes_for :info

scope :recent, -> { order("id DESC")}

def build_item_cache_from_cart(cart)
cart.items.each do |cart_item|
cart.cart_items.each do |cart_item|
item = items.build
item.product_name = cart_item.title
item.quantity = 1
item.price = cart_item.price
item.product_name = cart_item.product.title
item.quantity = cart_item.quantity
item.price = cart_item.product.price
item.save
end
end
Expand Down
24 changes: 24 additions & 0 deletions app/views/account/orders/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

<h2>訂單列表 </h2>


<table class="table table-bordered">
<thead>
<tr>
<th>#</th>
<th>生成時間</th>

</tr>
</thead>
<tbody>
<% @orders.each do |order| %>
<tr>
<td> <%= render_order_link(order) %> </td>
<td> <%= render_order_created_time(order) %> </td>
</tr>
<% end %>

</tbody>
</table>


Empty file added app/views/admin/index.html.erb
Empty file.
23 changes: 23 additions & 0 deletions app/views/admin/orders/_state_option.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

<div style="padding:10px; float:right;">
<% case order.aasm_state %>
<% when "order_placed" %>
<%= link_to("取消訂單", cancel_admin_order_path(order.token) , :class => "btn btn-default btn-sm", :method => :post) %>

<% when "paid" %>
<%= link_to("取消訂單", cancel_admin_order_path(order.token) , :class => "btn btn-default btn-sm", :method => :post) %>
<%= link_to("出貨", ship_admin_order_path(order.token) , :class => "btn btn-default btn-sm", :method => :post) %>
<% when "shipping" %>
<%= link_to("設為已出貨", shipped_admin_order_path(order.token), :class => "btn btn-default btn-sm", :method => :post) %>
<% when "shipped" %>
<%= link_to("退貨", return_admin_order_path(order.token), :class => "btn btn-default btn-sm", :method => :post) %>

<% when "order_cancelled" %>
<span class="label label-default">訂單已取消</span>
<% when "good_returned" %>
<span class="label label-default">已退貨</span>

<% end %>

</div>

25 changes: 25 additions & 0 deletions app/views/admin/orders/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

<h2>訂單列表 </h2>


<table class="table table-bordered">
<thead>
<tr>
<th>#</th>
<th>生成時間</th>
<th>訂購者</th>
<th>訂單狀態</th>
</tr>
</thead>
<tbody>
<% @orders.each do |order| %>
<tr>
<td> <%= render_admin_order_link(order) %> </td>
<td> <%= render_order_created_time(order) %> </td>
<td> <%= render_order_user_name(order.user) %></td>
<td> <%= render_order_state(order) %> </td>
</tr>
<% end %>

</tbody>
</table>
Loading