Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
3188678
created budgets controller
elherzt Nov 8, 2013
e89751b
created budgeted scope for categories
elherzt Nov 8, 2013
9e268eb
ignored database
elherzt Nov 8, 2013
9cc502a
added link to budgets without correct url for now
elherzt Nov 8, 2013
df02d6d
created view with real data but without filters
elherzt Nov 9, 2013
1a28679
added filter to splits active
elherzt Nov 11, 2013
51f0582
deleted unnecesary variable
elherzt Nov 11, 2013
7cee04a
moved class interval to a file into lib directory
elherzt Nov 11, 2013
99ba96e
moved filter class to lib folder
elherzt Nov 11, 2013
20128c2
required filter class in split model
elherzt Nov 11, 2013
1f1a4ae
deleted code comented
elherzt Nov 11, 2013
3968764
added filters to budgets view
elherzt Nov 11, 2013
3535b0b
added intervals to budgets, only per month
elherzt Nov 11, 2013
7711d67
change view of budgets
elherzt Nov 11, 2013
fe184c6
added name of the month in the view
elherzt Nov 11, 2013
e8f004b
added task to get category_id in transactions
elherzt Nov 11, 2013
ec80a03
refactored code in order to the change in transactions table
elherzt Nov 11, 2013
65a853c
removed unnecesary code
elherzt Nov 11, 2013
5883c87
added spent at table
elherzt Nov 11, 2013
2d79a61
changed instance of filter
elherzt Nov 11, 2013
2aefad4
removed commented code
elherzt Nov 11, 2013
280227d
added period to budgets
elherzt Nov 12, 2013
f05188c
removed unnecesary code
elherzt Nov 12, 2013
d09b29d
fixed problem with dates
elherzt Nov 13, 2013
f583f87
refacored code
elherzt Nov 13, 2013
431c977
changed interval to las day of month
elherzt Nov 13, 2013
40e6778
added income and expenses budgets, also total values of budget
elherzt Nov 14, 2013
311f561
refactored code
elherzt Nov 14, 2013
222b9f4
fix error in the view
elherzt Nov 14, 2013
729ab43
refactored code
elherzt Nov 14, 2013
1c02e6d
added sql view budget_categories
elherzt Nov 19, 2013
e6eabbd
deleted budgets_helper
elherzt Nov 19, 2013
d967650
changed routes for budgets
elherzt Nov 19, 2013
e3dba8c
add BudgetCategory model into Category model
elherzt Nov 19, 2013
495935b
removed views to budgets
elherzt Nov 19, 2013
4f15800
added views to categories
elherzt Nov 19, 2013
5a72a5a
deleted unnnecesary budgets controller
elherzt Nov 19, 2013
d6e85f3
added categories helper
elherzt Nov 19, 2013
ba6e407
added categories controller
elherzt Nov 19, 2013
4036602
deleted duplicated code
elherzt Nov 19, 2013
97fc795
fecator code
elherzt Nov 19, 2013
67010b0
moved categories to pocketmoney namespce
elherzt Nov 19, 2013
087af2d
deleted budgets table
elherzt Nov 19, 2013
b42f566
change route to categories.rb
elherzt Nov 19, 2013
1be1df8
moved filter class
elherzt Nov 19, 2013
f7d44bc
renamed style file to budgets
elherzt Nov 19, 2013
e1c23ca
moved set_period method at application helper
elherzt Nov 19, 2013
9ac4a78
deleted unnecesary code
elherzt Nov 19, 2013
81a4ff0
deleted unnecesary code
elherzt Nov 19, 2013
eacb6fd
added draper gem
elherzt Dec 4, 2013
9166583
generated decorator for categories
elherzt Dec 4, 2013
856cf64
added collection decorated
elherzt Dec 4, 2013
fa98af4
created new partial to intervals
elherzt Dec 5, 2013
0bcf160
added identation to sql view
elherzt Dec 5, 2013
a5b6475
decorated collection to budgets categories
elherzt Dec 5, 2013
da4550e
decorator to budgets category
elherzt Dec 5, 2013
6aaa93b
removed code from categories_helper
elherzt Dec 5, 2013
e174d65
change code in views in order to use new decorator
elherzt Dec 5, 2013
01f2e68
fix double substraction
elherzt Dec 5, 2013
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

config/database.yml
config/config.yml
PocketMoneyDB.sql


**/.DS_Store
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ gem 'turbolinks'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.0.1'

gem 'draper'
gem 'haml'
gem 'twitter-bootstrap-rails', :git => 'git://github.com/seyhunak/twitter-bootstrap-rails.git'
gem 'less-rails'
Expand Down
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ GEM
execjs
coffee-script-source (1.6.2)
commonjs (0.2.6)
draper (1.3.0)
actionpack (>= 3.0)
activemodel (>= 3.0)
activesupport (>= 3.0)
request_store (~> 1.0.3)
erubis (2.7.0)
execjs (1.4.0)
multi_json (~> 1.0)
Expand Down Expand Up @@ -104,6 +109,7 @@ GEM
rdoc (3.12.2)
json (~> 1.4)
ref (1.0.5)
request_store (1.0.5)
ruby-progressbar (1.1.1)
sass (3.2.9)
sass-rails (4.0.0.rc1)
Expand Down Expand Up @@ -148,6 +154,7 @@ PLATFORMS

DEPENDENCIES
coffee-rails (~> 4.0.0)
draper
haml
jbuilder (~> 1.0.1)
jquery-rails
Expand Down
2 changes: 2 additions & 0 deletions app/assets/stylesheets/categories.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.budget
width: 700px
6 changes: 6 additions & 0 deletions app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class CategoriesController < ApplicationController
def budgets
@budgets = BudgetCategoriesDecorator.new(Category::BudgetCategory.all)
@filter = Category.filter(params)
end
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this controller for?

3 changes: 3 additions & 0 deletions app/decorators/budget_categories_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class BudgetCategoriesDecorator < Draper::Decorator
delegate_all
end
40 changes: 40 additions & 0 deletions app/decorators/budget_category_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class BudgetCategoryDecorator < Draper::Decorator
delegate_all

def dates_intervals(from, to)
@@from = from
@@to = to+1
end

def amount
amount = Category::BudgetCategory.interval(@@from, @@to)
amount = amount.sum_category(model.id)
amount < 0 ? amount*-1 : amount
end

def limit
(model.amount_day * ((@@to - @@from).to_i+1)).round(0)
end

def balance
(limit - amount).round(0)
end

def available
balance < 0 ? 0 : balance
end

def color
balance < 0 ? "danger" : "warning"
end

def spent_percentage
percentage = (amount * 100) / limit
percentage < 100 ? percentage : 100
end

def available_percentage
100 - spent_percentage
end

end
11 changes: 11 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,15 @@ def progress_bar(type, percentage, &block)
content_tag :div, :class=> "bar bar-#{type}", :style=>"width:#{percentage}%;", &block if percentage > 0
end

def set_period(filter)
case filter.kind
when "month"
filter.from.strftime("%B %Y")
when "week"
"#{filter.from.strftime('%d %B %Y')} - #{filter.to.strftime('%d %B %Y')}"
when "year"
filter.from.year
end
end

end
7 changes: 7 additions & 0 deletions app/helpers/categories_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module CategoriesHelper

def total_budget
Category::BudgetCategory.total_values(@filter.from, @filter.to+1.day)
end

end
75 changes: 75 additions & 0 deletions app/models/category.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,83 @@
class Category < ActiveRecord::Base
include FilterInterval
class BudgetCategory < ActiveRecord::Base
self.table_name = "budget_categories"
self.primary_key = "id"
belongs_to :category, foreign_key: "id"
scope :incomes, -> { budgets.where(budget_type: 1)}
scope :expenses, -> { budgets.where(budget_type: 0)}
scope :sum_incomes, -> { where(budget_type: 1).sum(:amount) }
scope :sum_expenses, -> { where(budget_type: 0).sum(:amount) }
scope :interval, ->(from, to) { where("budget_categories.date >= ? AND budget_categories.date <= ?", from, to) }
scope :budgets, -> {select("id, amount_day, budget_type, name").order("name").uniq}
scope :sum_category, ->(id) { where(id: id).sum(:amount) }

def self.total_values(from, to)
@@days = (to - from).to_i
@@interval = interval(from, to)
@@spent = total_spent * -1
@@entries = total_entries
Hash['saved', saved, 'beat', beat_budget, 'missing', missing_budget, 'deficit', deficit ]
end

private
def self.total_spent
@@interval.sum_expenses
end

def self.total_entries
@@interval.sum_incomes
end

def self.saved
@@entries > @@spent ? @@entries - @@spent : 0
end

def self.beat_budget
value = income_vs_expense
value > 0 ? value : 0
end

def self.missing_budget
value = income_vs_expense
value < 0 ? value : 0
end

def self.deficit
@@entries - @@spent > 0 ? 0 : @@entries - @@spent
end

def self.income_vs_expense
incomes = @@entries - budget_entries
expenses = budget_expenses - @@spent
incomes + expenses
end

def self.budget_entries
((incomes.map(&:amount_day).sum) * @@days).round(0)
end

def self.budget_expenses
((expenses.map(&:amount_day).sum) * @@days).round(0)
end
end

## Budget period
# 0 diario
# 1 Semanal
# 2 Mensual
# 3 Cuatrimestral
# 4 Anual
scope :active, -> { where(deleted:false) }
scope :transaction_ids, ->(ids) { joins(:splits => :transaction).where(transactions: {id: ids}) }
scope :transaction_totals, -> { joins(:splits => :transaction).merge(Transaction.total_amount) }
scope :group_by_name, -> { select('categories.name', 'categories.id').group('categories.name', 'categories.id') }

has_many :splits
has_one :budget_category, foreign_key: "id"

def self.filter(params)
Filter.new(params)
end

end
Loading