1

在构建计费系统时,我遇到了以下问题:

在 scheduler.rake :

@users.each do |user|
begin
  puts "CALCULATING COSTS =========="
  costs = user.total_billable_price_per_month(Time.now)
  lead_count = user.total_billable_leads_count(Time.now)
  click_count = user.total_billable_clicks_count(Time.now)

  puts "CREATING NEW INVOICES ======="
  @invoice = user.invoices.new

  # if user.free_credits
  #         invoice.total_price_in_cents = 0
  #       else
  #         invoice.total_price_in_cents = costs.cents
  #       end



  @invoice.total_leads = lead_count
  @invoice.total_clicks = click_count
  @invoice.total_price_in_cents = costs.cents

  # Als de vorige factuur onder de 10 euro was, dan geld bij huidige factuur optellen
  if user.invoices.last && user.invoices.last.total_price_in_cents < 1000
    puts "Bedrag onder 10 euro"
    puts "Last invoice = #{user.invoices.last.total_price_in_cents}"
    @invoice.total_price_in_cents += user.invoices.last.total_price_in_cents
  end

  puts "SAVING INVOICE FOR #{user.name} with ID = #{user.id}"
  # Factuur saven
  @invoice.save

  #Als de factuur hoger of gelijk is als 10euro, dan factuur aanmaken
  if @invoice.total_price_in_cents >= 1000
    #Moneybird factuur versturen
    puts "COSTS ARE HIGHER THAN 10 EURO, CREATING MONEYBIRD INVOICE"

    moneybird_invoice = MoneybirdInvoice.new
    sleep(15)
    moneybird_invoice.contact_id = MoneybirdContact.find_by_customer_id(user.id).id
    sleep(15)
    moneybird_invoice.details_attributes = [
         { :description => "Aantal leads", :amount => lead_count, :price => user.lead_price.cents.to_f/100},
         { :description => "Aantal clicks", :amount => click_count, :price => user.click_price.cents.to_f/100}
    ]

    puts "TRYING TO SAVE MONEYBIRD INVOICE"
    if moneybird_invoice.save && moneybird_invoice.put(:send_invoice)
      puts "SUCCESFULLY SAVED INVOICE"
      #GET UPDATED PAY URL
      @sent_mb_invoice =  MoneybirdInvoice.get(moneybird_invoice.id)
      sleep(15)
      @invoice.update_attributes(:moneybird_invoice_id => @sent_mb_invoice['invoice_id'], :moneybird_pay_url => @sent_mb_invoice['pay_url'])
    else
      puts "MONEYBIRD INVOICE FAILED"
      puts moneybird_invoice.errors.inspect
    end
  else
    # GEEN MONEYBIRD FACTUUR AANMAKEN
  end

rescue
  puts "ER IS IETS FOUT GEGAAN MET FACTUREREN USER ID = #{user.id} & NAME = #{user.name}"
  puts @invoice.errors.inspect
end
end

这段代码应该在每次运行 rake 任务时不断增加,除非总量达到 > 1000。

if user.invoices.last && user.invoices.last.total_price_in_cents < 1000
  puts "Bedrag onder 10 euro"
  puts "Last invoice = #{user.invoices.last.total_price_in_cents}"
  @invoice.total_price_in_cents += user.invoices.last.total_price_in_cents
end

上面的代码总是把“Last invoice = 100”=> 每次运行 rake 任务时都会增加

每张新发票仍然具有相同的 total_price_in_cents (当我期望它应该增加时)。

到底是怎么回事 ?

4

1 回答 1

1

编辑:在代码更新后添加:

在您更新的代码中,看起来您在调用 user.invoices.new 之后调用了 user.invoices.last,这就是为什么它总是返回相同的值。

在调用 user.invoices.new 之前创建一个变量 @last_invoice = user.invoices.last。

原始答案:

save在您的原始代码发布中,您的调用似乎@invoice发生在循环之外——我相信您只保存了一次。

task :create_invoices => :environment do  

  # Begin the loop for each use
  User.all.each do |user|
    @invoice = user.invoices.build
    #If last bill < 1000
    if user.invoices.last && user.invoices.last.total_price_in_cents < 1000

      puts "Last invoice = #{user.invoices.last.total_price_in_cents}"
      @invoice.total_price_in_cents += user.invoices.last.total_price_in_cents
      @invoice.save

    end # end 'if' statement
  end # end loop for all users
end # end task definition

因此,您循环遍历 users 表,但从不保存更新 - 除了您退出循环后的最后一次

于 2012-05-21T14:58:09.973 回答