用期间计算费率

时间:2014-08-07 16:00:00

标签: ruby-on-rails date jointable

我是Ruby新手,但我尝试制作预订系统!!!

我有一个名为“房间”的模型,名称和价格(已完成):

room 1 | $100
room 2 | $150
room 3 | $200
...

我还有一个名为“Rate”的模型,名称为begin_period,ending_period和%_price(已完成)

Season1_standard | 08/01/2014 | 09/30/2014 | 100%
OtherSeason1_standard | 08/15/2014 | 10/25/2014 | 70%
Season2_standard | 10/01/2014 | 12/31/2014 | 80%
Season3_standard | 01/01/2015 | 03/31/2015 |90%
...

我做了一个联接表(完成):

room has_and_belongs_to_many :rates
rate has_and_belongs_to_many :rooms

所以我可以为房间和房间选择费率。

现在我的问题是:

我有一个名为“预订”的模型,包含room,start_date和end_date,价格,我想计算价格价格:

例如:

我想从2014年9月28日到2014年4月10日(6晚)预订1号房间

room1与season1_standard和season2_standard

相关联

所以我试着做这样的事情:

09/28/2014 = $100 * 100% = $100
09/29/2014 = $100 * 100% = $100
09/30/2014 = $100 * 100% = $100
10/01/2014 = $100 * 80% = $80
10/02/2014 = $100 * 80% = $80
10/03/2014 = $100 * 80% = $80

我的价格将是总和= $ 540

我迷路了。我不知道从哪里开始!我想我必须在我的预订模型中做一些事情,但我不知道如何从连接表rates_rooms中取回价值?

如果您有任何想法或链接来学习一些新概念,那将是我的最佳选择

很多

编辑:表格和代码

所以这是真正的法语文本代码!

create_table "villas", force: true do |t|
t.string   "nom"
t.float    "prix_nuit_hs"

create_table "tarifications", force: true do |t|
t.string   "nom"
t.date     "periode_debut", limit: 255
t.date     "periode_fin",   limit: 255
t.integer  "pct_prix_base"

create_table "reservations", force: true do |t|
t.string   "date_debut"
t.string   "date_fin"
t.integer  "prix_location"

所以在reservation.rb

def calcul
  dd = Date.strptime(date_debut, "%m/%d/%Y")
  df = Date.strptime(date_fin, "%m/%d/%Y")
  prix_location = 0
  [dd..df].each do |date|
    tarification = villa.tarifications.where('periode_debut >= ? AND periode_fin <= ?',date,date).first
    prix_location += tarification.pct_prix_base * villa.prix_nuit_hs
  end
  return prix_location
end

我尝试在日期中更改date_de但et date_fin而没有Date.strptime代码,但我有同样的错误非常感谢您的帮助

修改

抱歉,我没有看到所有的修改。所以我尝试了这个新代码:

  def calcul
  dd = Date.strptime(date_debut, "%m/%d/%Y")
  df = Date.strptime(date_fin, "%m/%d/%Y")
  prix_location = 0
  [dd..df].each do |date|
    tarification = Tarification.where('periode_debut >= ? AND periode_fin <= ?',date,date).select{|r| r.in?(villa.tarifications)}.first
    prix_location += tarification.pct_prix_base * villa.prix_nuit_hs
  end
  return prix_location
end

但我仍然有这条消息错误:

SQLite3::SQLException: near ",": syntax error: SELECT "tarifications".* FROM "tarifications" WHERE (periode_debut >= '2014-09-03','2014-09-04','2014-09-05','2014-09-06' AND periode_fin <= '2014-09-03','2014-09-04','2014-09-05','2014-09-06')

韩国社交协会

编辑:解决方案

朋友找到解决方案:

每个日期

dd.upto(df)!

很多

1 个答案:

答案 0 :(得分:0)

total_price = 0
[start_date..end_date].each do |date|
  rate = Rate.where('beginning_period >= ? AND ending_period <= ?',date,date).select{|r| r.in?(room.rates)}.first
  total_price += rate.%_price * room.price
end
return total_price
相关问题