-
Notifications
You must be signed in to change notification settings - Fork 29
/
staff_members.rb
212 lines (192 loc) · 7.68 KB
/
staff_members.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
class StaffMembers < Application
include Pdf::DaySheet if PDF_WRITER
include DateParser
layout :determine_layout
def index
per_page = 25
@date = params[:date] ? parse_date(params[:date]) : Date.today
@branch = Branch.get(params[:branch_id]) if params[:branch_id]
hash = get_staff_members_hash
@staff_members = (@staff_members ? @staff_members : StaffMember.all(hash)).paginate(:page => params[:page], :per_page => per_page)
set_staff_member_counts
display @staff_members
end
#serves info tab for staff member
def moreinfo(id)
@render_form = true
@render_form = false if params[:_target_]
@from_date = params[:from_date] ? parse_date(params[:from_date]) : Date.min_date
@to_date = params[:to_date] ? parse_date(params[:to_date]) : Date.today
allow_nil = (params[:from_date] or params[:to_date]) ? false : true
@staff_member= StaffMember.get(id)
raise NotFound unless @staff_member
if allow_nil
@clients = @center.clients(:fields => [:id])
else
@clients = @center.clients(:fields => [:id], :date_joined.lte => @to_date, :date_joined.gte => @from_date)
end
@groups_count = @center.client_groups(:fields => [:id]).count
@clients_count = @clients.count
@payments = Payment.collected_for(@center, @from_date, @to_date)
@fees = Fee.collected_for(@center, @from_date, @to_date)
@loan_disbursed= LoanHistory.amount_disbursed_for(@center, @from_date, @to_date)
@loan_data = LoanHistory.sum_outstanding_for(@center, @to_date)
@defaulted = LoanHistory.defaulted_loan_info_for(@center, @to_date)
render :file => 'branches/moreinfo', :layout => false
end
def show_centers(id)
@staff_member = StaffMember.get(id)
raise NotFound unless @staff_member
@centers = @staff_member.centers
display @centers
end
def show_clients(id)
@staff_member = StaffMember.get(id)
raise NotFound unless @staff_member
@clients = @staff_member.centers.clients
display @clients
end
def show_disbursed(id)
@staff_member = StaffMember.get(id)
raise NotFound unless @staff_member
@loans = @staff_member.disbursed_loans
display @loans
end
def day_sheet(id)
@staff_member = StaffMember.get(id)
raise NotFound unless @staff_member
@date = params[:date] ? parse_date(params[:date]) : Date.today
@date = @date.holiday_bump
center_ids = LoanHistory.all(:date => [@date, @date.holidays_shifted_today].uniq, :fields => [:loan_id, :date, :center_id], :status => [:disbursed, :outstanding]).map{|x| x.center_id}.uniq
@centers = @staff_member.centers(:id => center_ids).sort_by{|x| x.name}
if params[:format] == "pdf"
generate_pdf
send_data(File.read("#{Merb.root}/public/pdfs/staff_#{@staff_member.id}_#{@date.strftime('%Y_%m_%d')}.pdf"),
:filename => "#{Merb.root}/public/pdfs/staff_#{@staff_member.id}_#{@date.strftime('%Y_%m_%d')}.pdf")
else
display @centers
end
end
def disbursement_sheet(id)
#make it like day_sheet
@staff_member = StaffMember.get(id)
raise NotFound unless @staff_member
@date = params[:date] ? parse_date(params[:date]): Date.today
@date = @date.holiday_bump
center_ids = Loan.all(:scheduled_disbursal_date => @date, :approved_on.not => nil, :rejected_on => nil).map{|x| x.client.center_id}.uniq
@centers = @staff_member.centers(:id => center_ids).sort_by{|x| x.name}
#debugger
if params[:format] == "pdf"
#some problem not working as of now
filename = "#{Merb.root}/public/pdfs/staff_#{@staff_member.id}_disbursement_#{@date.strftime('%Y_%m_%d')}.pdf"
generate_disbursement_pdf(filename)
send_data(File.read(filename), :filename => filename)
else
display @centers
end
end
def show(id)
@staff_member = StaffMember.get(id)
raise NotFound unless @staff_member
@manages = {:regions => @staff_member.regions, :areas => @staff_member.areas, :branches => @staff_member.branches, :centers => @staff_member.centers}
display @staff_member
end
def new
only_provides :html
@staff_member = StaffMember.new
display @staff_member
end
def edit(id)
only_provides :html
@staff_member = StaffMember.get(id)
raise NotFound unless @staff_member
display @staff_member
end
def create(staff_member)
@staff_member = StaffMember.new(staff_member)
if @staff_member.save
redirect resource(:staff_members), :message => {:notice => "StaffMember was successfully created"}
else
message[:error] = "StaffMember failed to be created"
render :new
end
end
def update(id, staff_member)
@staff_member = StaffMember.get(id)
raise NotFound unless @staff_member
if @staff_member.update_attributes(staff_member)
redirect resource(:staff_members)
else
display @staff_member, :edit
end
end
def destroy(id)
@staff_member = StaffMember.get(id)
raise NotFound unless @staff_member
if @staff_member.destroy
redirect resource(:staff_members)
else
raise InternalServerError
end
end
# this redirects to the proper url, used from the router
def redirect_to_show(id)
raise NotFound unless @staff_member = StaffMember.get(id)
redirect resource(@staff_member)
end
private
def determine_layout
return "printer" if params[:layout] and params[:layout]=="printer"
end
def get_staff_members_hash
if session.user.role == :staff_member or session.user.staff_member
st = session.user.staff_member
ids = []
[st.branches, st.centers.branches].flatten.uniq.each{|branch|
staff_members = StaffMember.related_to(branch)
ids += ([staff_members, branch.manager.id] << branch.centers.manager.map{|x| x.id}).flatten.uniq
}
return {:id => ids}
elsif params[:branch_id] and not params[:branch_id].blank?
branch = Branch.get(params[:branch_id])
staff_members = StaffMember.related_to(branch)
ids = ([staff_members, branch.manager.id] << branch.centers.manager.map{|x| x.id}).flatten.uniq
return {:id => ids}
else
return {}
end
end
def set_staff_member_counts
first_of_this_month = Date.new(@date.year, @date.month, 1)
end_of_this_month = @date
client_ids = @branch.client_ids if @branch
{
:branch_managers => Branch, :center_managers => Center, :applied_loans => Loan, :approved_loans => Loan,
:rejected_loans => Loan, :disbursed_loans => Loan, :written_off_loans => Loan
}.each{|type, klass|
if klass==Branch or klass==Center
aggregate_by = :manager_staff_id
overall_cond = {:creation_date.lte => @date}
thismonth_cond = {:creation_date.lte => @date, :creation_date.gte => first_of_this_month}
if @branch
sym = (klass==Branch ? :id : :branch_id)
overall_cond[sym] = @branch.id
thismonth_cond[sym] = @branch.id
end
else
name = (type.to_s.split('_')-["loans"]).join("_")
aggregate_by = (name + "_by_staff_id").to_sym
date_key = name=="disbursed" ? :disbursal_date : (name+"_on").to_sym
overall_cond = {date_key.lte => @date}
thismonth_cond = {date_key.lte => @date, date_key.gte => first_of_this_month}
if @branch
branch_cond = {:client_id => client_ids}
overall_cond += branch_cond
thismonth_cond += branch_cond
end
end
instance_variable_set("@#{type}_overall", klass.all(overall_cond).aggregate(aggregate_by, :all.count))
instance_variable_set("@#{type}_thismonth", klass.all(thismonth_cond).aggregate(aggregate_by, :all.count))
}
end
end # StaffMembers