| Class | Public |
| In: |
app/models/public.rb
|
| Parent: | ActiveRecord::Base |
************************************************************************/
************************************************************************/
Get an array of free rooms
# File app/models/public.rb, line 17
17: def self.get_free_rooms(user, arrival, departure, adults, children)
18: #Convert arrival and departure in date object
19: convert_arrival = arrival
20: convert_departure = departure
21:
22: if (departure < arrival || arrival < ::Date.today || departure < ::Date.today)
23: return false
24: end
25:
26: data = Array.new
27: rooms = Array.new
28: user_id = user.id
29:
30: #get all blocked rooms
31: blocked_rooms = Contract.find(:all, :conditions => ["user_id = ? && (arrival < ? AND ? < departure) || (arrival < ? AND ? < departure) || (arrival < ? && departure > ?)",user_id, arrival, arrival, departure, departure, arrival, departure])
32:
33: blocked_rooms.each do |room|
34: data << room.room_id
35: end
36:
37: #Filter I => blocked rooms and filter person schema
38: if data.size > 0
39: all_rooms = Room.find(:all, :conditions => ["r.user_id = ? && r.id not in (?) && " +
40: "(p.adults <= ? && p.children <= ?)", user_id, data, adults, children],
41: :joins => "as r inner join prices as p on r.id = p.room_id")
42:
43: else
44: all_rooms = Room.find(:all, :conditions => ["r.user_id = ? && " +
45: "(p.adults <= ? && p.children <= ?)", user_id, adults, children],
46: :joins => "as r inner join prices as p on r.id = p.room_id")
47: end
48:
49: #Calculate Price and return room array
50: all_rooms.each do |room|
51: dow = room.days_of_week
52: ms = room.minimum_stay
53: #Filter II => day of week and minimum stay
54: if dow["#{convert_arrival.wday}"].to_i == 1 && (convert_departure - convert_arrival).to_i >= ms["#{convert_arrival.month}"]["#{convert_arrival.mday}"].to_i
55:
56: #push room to array
57: rooms << {"room", room, "price", get_price(user, room, arrival, departure, adults, children)}
58: end
59:
60: end
61: return rooms
62: end
# File app/models/public.rb, line 64
64: def self.get_price(user, room, arrival, departure, adults, children)
65:
66: user_id = user.id
67: if Price.count(["user_id = ? && room_id = ? && adults = ? && children = ?", user_id, room.id, adults, children]) >= 1
68: getprice = Price.find(:first, :conditions => ["user_id = ? && room_id = ? && adults = ? && children = ?", user_id, room.id, adults, children])
69: price = getprice.price
70: else
71: #Fallback
72: getprice = Price.find(:first,
73: :conditions => ["user_id = ? && room_id = ? && ? <= (adults+children)", user_id, room.id, adults.to_i + children.to_i],
74: :order_by => "adults desc, children desc")
75: price = getprice.price
76: end
77:
78: convert_arrival = arrival
79: convert_departure = departure
80: days_to_stay = (convert_departure - convert_arrival).to_i
81: counter = convert_arrival
82: addprice = 0
83: while counter < convert_departure do
84:
85: check_price = price["#{counter.month}"]["#{counter.mday}"]
86:
87: #Check if price is array, something like 1,3,50;4,10,40
88: if check_price.class == Array
89:
90: check_price.each do |array_price|
91: if (array_price[0].to_i <= days_to_stay && array_price[1].to_i >= days_to_stay)
92: addprice = addprice.to_i + array_price[2].to_i
93: end
94: end
95: else
96: addprice = addprice.to_i + check_price.to_i
97: end
98: counter = counter + 1
99: end
100:
101: #Add prices of addons
102: addons = Addon.find(:all, :conditions => ["user_id = ? && room_id = ? && force_cart = ?", user_id, room.room_id, true])
103: onetime = 1
104: per_person = 1
105: addons.each do |addon|
106: if !addon.onetime?
107: onetime = days_to_stay.to_i
108: end
109: if addon.per_person?
110: per_person = (adults.to_i + children.to_i)
111: end
112: addprice = addprice.to_i + (per_person * onetime * addon.price).to_i
113: onetime = per_person = 1
114: end
115:
116: #Return price including force addons, and room price depending on duration
117: return addprice
118:
119: end