Created
August 28, 2015 13:29
-
-
Save aristotelesbr/e343c86aeaf05734eeb3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Expense < ActiveRecord::Base | |
validates :value, presence: true | |
belongs_to :user | |
has_many :portions | |
accepts_nested_attributes_for :portions, reject_if: :all_blank, allow_destroy: true | |
def self.total | |
where(created_at: Date.today.beginning_of_month..Date.today.end_of_day).sum(:value) | |
end | |
def self.expense_current_sum | |
where(created_at: Date.today.beginning_of_month..Date.today.end_of_day).sum(:value) | |
end | |
def self.expense_month_ago | |
where(created_at: 1.month.ago.beginning_of_month..1.month.ago.end_of_day).sum(:value) | |
end | |
def self.expense_current_month | |
where(created_at: (Date.today.beginning_of_month..Date.today.end_of_month)) | |
end | |
# Controller/reports | |
def self.desp | |
where(created_at: Date.today.beginning_of_month..Date.today.end_of_day).sum(:value) | |
end | |
before_save :validate_salary | |
after_create :create_invoices | |
protected | |
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, | |
description: description, user_id: user_id) | |
portions = self.value / self.quantity | |
puts current_date | |
puts portions | |
current_date += interval | |
end | |
end | |
def validate_salary | |
self.value < Income.sum(:salary) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Income < ActiveRecord::Base | |
def self.income_current | |
where(created_at: Date.today.beginning_of_month..Date.today.end_of_month).sum(:salary) | |
end | |
def self.income_mont_ago | |
where(created_at: 1.month.ago.beginning_of_month..1.month.ago.end_of_day).sum(:salary) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment