Class: Backend::JournalsController

Inherits:
BaseController show all
Includes:
JournalEntriesCondition
Defined in:
app/controllers/backend/journals_controller.rb

Constant Summary collapse

@@journal_views =
%w(items entries mixed)

Instance Attribute Summary

Attributes inherited from ApplicationController

#current_theme

Instance Method Summary collapse

Methods inherited from BaseController

accounts_range_crit, crit_params, journal_entries_states_crit, journal_period_crit, journals_crit, search_conditions

Methods inherited from ApplicationController

#after_sign_in_path_for, #authorized?, human_action_name, #human_action_name

Instance Method Details

#bookkeepObject


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'app/controllers/backend/journals_controller.rb', line 136

def bookkeep
  params[:stopped_on] = begin
                          params[:stopped_on].to_date
                        rescue
                          Time.zone.today
                        end
  params[:started_on] = begin
                          params[:started_on].to_date
                        rescue
                          (params[:stopped_on] - 1.year).beginning_of_month
                        end
  @natures = [:sale, :incoming_payment, :deposit, :purchase, :outgoing_payment, :cash_transfer, :affair] # , transfer

  if request.get?
    notify_now(:bookkeeping_works_only_with, list: @natures.collect { |x| x.to_s.classify.constantize.model_name.human }.to_sentence)
    @step = 1
  elsif request.put?
    @step = 2
  elsif request.post?
    @step = 3
  end

  if @step >= 2
    session[:stopped_on] = params[:stopped_on]
    session[:started_on] = params[:started_on]
    @records = {}
    for nature in @natures
      conditions = ['created_at BETWEEN ? AND ?', session[:started_on].to_time.beginning_of_day, session[:stopped_on].to_time.end_of_day]
      @records[nature] = nature.to_s.classify.constantize.where(conditions)
    end

    if @step == 3
      state = (params[:save_in_draft].to_i == 1 ? :draft : :confirmed)
      for nature in @natures
        for record in @records[nature]
          record.bookkeep(:create, state)
        end
      end
      notify_success(:bookkeeping_is_finished)
      redirect_to (state == :draft ? { controller: :draft_journals, action: :show } : { action: :bookkeep })
    end
  end
end

#closeObject


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/controllers/backend/journals_controller.rb', line 104

def close
  return unless @journal = find_and_check
  unless @journal.closable?
    notify(:no_closable_journal)
    redirect_to action: :index
    return
  end
  if request.post?
    if @journal.close(params[:closed_on].to_date)
      notify_success(:journal_closed_on, closed_on: @journal.closed_on.l, journal: @journal.name)
      redirect_to action: :index
    end
  end
  t3e @journal
end

#reopenObject


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/controllers/backend/journals_controller.rb', line 120

def reopen
  return unless @journal = find_and_check
  unless @journal.reopenable?
    notify(:no_reopenable_journal)
    redirect_to action: :index
    return
  end
  if request.post?
    if @journal.reopen(params[:closed_on].to_date)
      notify_success(:journal_reopened_on, closed_on: @journal.closed_on.l, journal: @journal.name)
      redirect_to action: :index
    end
  end
  t3e @journal
end

#showObject

Displays details of one journal selected with params


92
93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/backend/journals_controller.rb', line 92

def show
  return unless @journal = find_and_check
  journal_view = current_user.preference("interface.journal.#{@journal.code}.view")
  journal_view.value = journal_views[0] unless journal_views.include? journal_view.value
  if view = journal_views.detect { |x| params[:view] == x }
    journal_view.value = view
    journal_view.save
  end
  @journal_view = journal_view.value
  t3e @journal
end