Skip to content

Instantly share code, notes, and snippets.

@aristotelesbr
Last active August 29, 2015 14:28
Show Gist options
  • Save aristotelesbr/74e5437ba2e6b5090669 to your computer and use it in GitHub Desktop.
Save aristotelesbr/74e5437ba2e6b5090669 to your computer and use it in GitHub Desktop.
# Deve criar no banco de dados de um valor(despesa) parcelado, onde
# o valor de cada parcela será cobrado nos messes subsequentes.
def create_invoices
interval = 1.month
cicles = self.quantity
start_date = Date.today
current_date = start_date
cicles.times do
Expense.create!(value: "#{value}", quantity:"#{quantity}",
descripition: "#{description}", user_id: "#{user_id}")
portions = self.value / self.quantity
puts current_date
puts portions
current_date += interval
end
end
@aristotelesbr
Copy link
Author

Opa, desculpe a demora em postar ai esta, fiz exatamente um after_create . Deu uma arrumada e esta funcionando sem aquele loop infinito, agora tudo que eu preciso é da um insert no banco com o valor das parcelas no outros messes.

 class Expense < ActiveRecord::Base

  after_create :create_invoices

  protected

  after_create :create_invoices

  protected

  def create_invoices
    if quantity.present?
        interval = 1.month
        cicles = self.quantity
        start_date = Date.today
        current_date = start_date
        portions = self.value / self.quantity

        cicles.times do
          self.update(value: portions) # Da um update no valor total 

          # Preciso criar uma parcela em cada mês. 
          # Ex. Fogão => 100 / 5 = 20.0 
          # Jan. => Fogão: valor: 20.0"
          # Fev. => Fogão: valor: 20.0
          # Mar => .....     Valor: 20.0
          #Abr => ........

          puts current_date
          puts portions

          current_date += interval
        end
      end
  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment