Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some range filters #100

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ gem 'ledermann-rails-settings'
# ======
# Accounting
gem 'has_accounts'
gem 'has_accounts_engine', '~> 3.0.0.beta13'
gem 'has_accounts_engine', '~> 3.0.0.beta13',
git: 'https://github.com/huerlisi/has_accounts_engine',
branch: 'bookings-timeframe'

# Addresses
gem 'has_vcards'
Expand Down
30 changes: 18 additions & 12 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ GIT
specs:
mt940_parser (1.1.0)

GIT
remote: https://github.com/huerlisi/has_accounts_engine
revision: 9f9a4684883f6385169ececa4625430ea125329f
branch: bookings-timeframe
specs:
has_accounts_engine (3.0.0.beta14)
acts-as-taggable-on
anjlab-bootstrap-rails (~> 2.1.0)
has_accounts (~> 3.0.0.beta0)
has_scope
has_vcards
i18n_rails_helpers
inherited_resources
rails (~> 3.2)
simple_form
validates_timeliness

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -178,17 +195,6 @@ GEM
has_accounts (3.0.0.beta3)
rails (~> 3.1)
validates_timeliness
has_accounts_engine (3.0.0.beta14)
acts-as-taggable-on
anjlab-bootstrap-rails (~> 2.1.0)
has_accounts (~> 3.0.0.beta0)
has_scope
has_vcards
i18n_rails_helpers
inherited_resources
rails (~> 3.2)
simple_form
validates_timeliness
has_scope (0.6.0)
actionpack (>= 3.2, < 5)
activesupport (>= 3.2, < 5)
Expand Down Expand Up @@ -481,7 +487,7 @@ DEPENDENCIES
grape-entity
haml-rails
has_accounts
has_accounts_engine (~> 3.0.0.beta13)
has_accounts_engine (~> 3.0.0.beta13)!
has_scope
has_vcards
i18n_rails_helpers
Expand Down
17 changes: 17 additions & 0 deletions app/controllers/invoices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ class InvoicesController < AuthorizedController
has_scope :invoice_state, :default => proc {|controller| 'booked' if controller.params[:by_text].nil?}, :only => :index
has_scope :by_text
has_scope :overdue, :type => :boolean
has_scope :by_value_date, :using => [:from, :to], :default => proc { |c| c.session[:has_scope] }
has_scope :by_due_date, :using => [:from, :to], :default => proc { |c| c.session[:has_scope] }

respond_to :html, :pdf

before_filter :set_search, :only => [:index]

# has_many :line_items
def new_line_item
if invoice_id = params[:id]
Expand Down Expand Up @@ -43,4 +47,17 @@ def copy

render 'edit'
end

def set_search
by_text = params[:by_text]
by_value_date = OpenStruct.new(
:from => params.fetch(:by_value_date, {}).fetch(:from, ''),
:to => params.fetch(:by_value_date, {}).fetch(:to, ''),
)
by_due_date = OpenStruct.new(
:from => params.fetch(:by_due_date, {}).fetch(:from, ''),
:to => params.fetch(:by_due_date, {}).fetch(:to, ''),
)
@search = OpenStruct.new(:by_text => by_text, :by_value_date => by_value_date, :by_due_date => by_due_date)
end
end
21 changes: 21 additions & 0 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ class Invoice < ActiveRecord::Base
validates_date :due_date, :value_date
validates_presence_of :customer, :company, :title, :state

# Scope filter for date range
scope :by_period_of_field, lambda {|field, date_from, date_to|
date_from = date_from.to_date rescue nil
date_to = date_to.to_date rescue nil

if date_from.present? && date_to.present?
where(field => date_from..date_to)
elsif date_from.present?
where("#{field} >= ?", date_from)
elsif date_to.present?
where("#{field} <= ?", date_to)
end
}

scope :by_value_date, lambda { |date_from, date_to|
by_period_of_field :value_date, date_from, date_to
}
scope :by_due_date, lambda { |date_from, date_to|
by_period_of_field :due_date, date_from, date_to
}

# String
def to_s(format = :default)
return "" if amount.nil?
Expand Down
13 changes: 13 additions & 0 deletions app/views/invoices/_search_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
= simple_form_for '', :method => :get do |f|
.container-fluid
.row-fluid
.span6= f.input :by_text, label: t(:by_text, scope: 'invoices.search'), required: false, input_html: { value: params[:by_text] }
.row-fluid
.span6= f.input 'by_value_date[from]', label: t(:by_value_date_from, scope: 'invoices.search'), required: false, input_html: { value: try_parse_date(@search.by_value_date.from), class: 'date-picker' }
.span6= f.input 'by_value_date[to]', label: t(:by_value_date_to, scope: 'invoices.search'), required: false, input_html: { value: try_parse_date(@search.by_value_date.to), class: 'date-picker' }
.row-fluid
.span6= f.input 'by_due_date[from]', label: t(:by_due_date_from, scope: 'invoices.search'), required: false, input_html: { value: try_parse_date(@search.by_due_date.from), class: 'date-picker' }
.span6= f.input 'by_due_date[to]', label: t(:by_due_date_to, scope: 'invoices.search'), required: false, input_html: { value: try_parse_date(@search.by_due_date.to), class: 'date-picker' }

.form-actions
= f.button :submit, t_action(:search)
6 changes: 6 additions & 0 deletions config/locales/bookyt.de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,12 @@ de:
invoices:
list:
title: Rechnungen
search:
by_text: Inhalt
by_value_date_from: Rechnungsdatum Von
by_value_date_to: Rechnungsdatum Bis
by_due_date_from: Fälligkeit Von
by_due_date_to: Fälligkeit Bis
letters:
debit_invoice:
closing: "Bitte begleichen Sie diese Rechnung mit beiliegendem Einzahlungsschein oder per E-Banking."
Expand Down
6 changes: 6 additions & 0 deletions config/locales/bookyt.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,12 @@ en:
invoices:
list:
title: Invoices
search:
by_text: Content
by_value_date_from: Value date from
by_value_date_to: Value date to
by_due_date_from: Due date from
by_due_date_to: Due date to
letters:
debit_invoice:
closing: "It was a pleasure working together. Please pay this invoice up to %{due_date} using the payment slip or by e-banking to below account."
Expand Down
6 changes: 6 additions & 0 deletions config/locales/bookyt.nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,12 @@ nl:
invoices:
list:
title: Rechnungen
search:
by_text: Inhalt
by_value_date_from: Rechnungsdatum Von
by_value_date_to: Rechnungsdatum Bis
by_due_date_from: Fälligkeit Von
by_due_date_to: Fälligkeit Bis
letters:
debit_invoice:
closing: "Bitte begleichen Sie diese Rechnung bis zum %{due_date} mit beiliegendem Einzahlungsschein oder per E-Banking."
Expand Down