Skip to content

Commit

Permalink
refactor booking
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike committed May 3, 2016
1 parent d03cd84 commit f4c2fe3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
17 changes: 12 additions & 5 deletions app/models/booking_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class BookingItem < ActiveRecord::Base
['no_limit', 1, '不限定'],
['time_limit', 2, '限定时间'],
['day_qty_limit', 3, '限定每日量'],
['qty_limit', 4, '限定全部总量'],
]

enum_attr :status, :in => [
Expand All @@ -25,11 +24,19 @@ class BookingItem < ActiveRecord::Base
accepts_nested_attributes_for :booking_item_pictures, allow_destroy: true
validates_associated :booking_item_pictures

def sold_qty
if day_qty_limit?
booking_orders.where(["DATE(created_at) = DATE(?) and status >= ? ", Time.now, 0]).map(&:qty).sum.to_i
else
booking_orders.where(["status >= ?", 0]).map(&:qty).sum.to_i
end
end

def surplus_qty
if no_limit? or time_limit?
qty - booking_orders.where(["status <> ?", BookingOrder::CANCELED]).map(&:qty).sum.to_i
elsif day_qty_limit?
limit_qty - booking_orders.where(["DATE(created_at) = DATE(?) and status <> ? ", Time.now, BookingOrder::CANCELED]).map(&:qty).sum.to_i
if day_qty_limit?
limit_qty - sold_qty
else
qty - sold_qty
end
end

Expand Down
6 changes: 3 additions & 3 deletions app/views/mobile/booking_items/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@
<b><%= @booking_item.name %></b>
<span class="fl">价格:¥<%= f @booking_item.price %></span>
<% if @booking_item.no_limit? %>
<span class="fw tright">总数<%= @booking_item.qty %></span>
<span class="fw tright">剩余<%= @booking_item.surplus_qty %></span>
<span class="fw tright">库存<%= @booking_item.qty %></span>
<span class="fw tright">已售<%= @booking_item.sold_qty %></span>
<% elsif @booking_item.time_limit? %>
<span class="fw tright">预定日期为:<%= @booking_item.start_at %><%= @booking_item.end_at %></span>
<% elsif @booking_item.day_qty_limit? %>
<span class="fw tright">每日:<%= @booking_item.limit_qty %></b></span>
<span class="fw tright">剩余<%= @booking_item.surplus_qty %></b></span>
<span class="fw tright">已售<%= @booking_item.sold_qty %></b></span>
<% end %>
</div>

Expand Down

0 comments on commit f4c2fe3

Please sign in to comment.