# Generate time sheet

**URL:** <https://www.ruby-forum.com/t/generate-time-sheet/111685>\
**Category:** Rails\
**Created:** [August 9, 2007, 8:22pm UTC](https://www.ruby-forum.com/t/generate-time-sheet/111685 "2007-08-09T20:22:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ajit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@Ajit](https://www.ruby-forum.com/u/Ajit)\
**Post date:** [August 9, 2007, 8:22pm UTC](https://www.ruby-forum.com/t/generate-time-sheet/111685/1 "2007-08-09T20:22:43Z")

</div>

Hi

does anybody know how to generate a time sheet?

## like starts from 8:00 Am 8:15 AM 8:30 AM

- 
- 
- 
- 
- 
- 

6:00 PM

Please advice

Thanks

Ajit

---

<div class="post-metadata">

**Author:** ![Ajit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@Ajit](https://www.ruby-forum.com/u/Ajit)\
**Post date:** [August 9, 2007, 8:24pm UTC](https://www.ruby-forum.com/t/generate-time-sheet/111685/2 "2007-08-09T20:24:44Z")

</div>

On Aug 9, 2:21 pm, Ajit [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> does anybody know how to generate a time sheet?

How about using pencil and paper? 🙂

–  
A

---

<div class="post-metadata">

**Author:** ![Ajit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@Ajit](https://www.ruby-forum.com/u/Ajit)\
**Post date:** [August 9, 2007, 8:28pm UTC](https://www.ruby-forum.com/t/generate-time-sheet/111685/3 "2007-08-09T20:28:44Z")

</div>

ha… pretty funny…

but thanks… i will try another option

---

<div class="post-metadata">

**Author:** ![Ajit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@Ajit](https://www.ruby-forum.com/u/Ajit)\
**Post date:** [August 9, 2007, 9:40pm UTC](https://www.ruby-forum.com/t/generate-time-sheet/111685/4 "2007-08-09T21:40:53Z")

</div>

On Aug 9, 2:26 pm, Ajit [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ha… pretty funny…

Well you kind of asked a silly question: “how to generate a time  
sheet?”.

Are you asking what software people use?  
Are you asking if there’s some Rails software that can do that?  
Are you asking about coding software to generate timesheets?  
Are you asking about generating documents? PDFs?

When asking on a list, its often better to BE SPECIFIC.

So what EXACTLY are you asking?

---

<div class="post-metadata">

**Author:** ![Ajit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@Ajit](https://www.ruby-forum.com/u/Ajit)\
**Post date:** [August 9, 2007, 10:05pm UTC](https://www.ruby-forum.com/t/generate-time-sheet/111685/5 "2007-08-09T22:05:55Z")

</div>

def self.showtime()  
time\_options = [’----’]  
step\_val = 15

```
start_val = "#{Time.now.strftime('%d/%m/%Y')}

```

8:00".to\_time(:local)  
end\_val = “#{Time.now.strftime(’%d/%m/%Y’)}  
18:00”.to\_time(:local)

```
  start_val.step(end_val, step_val) do |val|
    time_options << val
  end
  @showtime = time_options

```

end

i want output as follows:

## 8:00 AM 8:15 AM

- 
- 
- 

5:45 PM  
6:00 PM

please advice

---

<div class="post-metadata">

**Author:** ![Ajit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@Ajit](https://www.ruby-forum.com/u/Ajit)\
**Post date:** [August 9, 2007, 9:53pm UTC](https://www.ruby-forum.com/t/generate-time-sheet/111685/6 "2007-08-09T21:53:54Z")

</div>

I am trying to generate a list which will start from 8:00 AM and goes  
upto 6:00 PM

e.g.

## 8:00 AM 8:15 AM 8:30 AM

- 
- 
- 
- 

6:00 PM

thats all… using a for loop or do loop

Ajit

---

<div class="post-metadata">

**Author:** ![Ajit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@Ajit](https://www.ruby-forum.com/u/Ajit)\
**Post date:** [August 10, 2007, 12:20pm UTC](https://www.ruby-forum.com/t/generate-time-sheet/111685/7 "2007-08-10T12:20:13Z")

</div>

@Eno

Don’t be such a dickhead. No one has time for that.

It was obvious what Ajit was asking for. This is a Ruby on Rails  
discussion group,. It goes without saying that he wants to generate a  
timesheet with Rails.

---

<div class="post-metadata">

**Author:** ![Ajit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@Ajit](https://www.ruby-forum.com/u/Ajit)\
**Post date:** [August 10, 2007, 3:06pm UTC](https://www.ruby-forum.com/t/generate-time-sheet/111685/8 "2007-08-10T15:06:41Z")

</div>

On Aug 10, 6:19 am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It was obvious what Ajit was asking for.

After he explained what he meant, yes.

> This is a Ruby on Rails discussion group

And his question was a Ruby question.

Next time, you can keep your personal abusive remarks to yourself,  
thank you.

–

---

<div class="post-metadata">

**Author:** ![Ajit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@Ajit](https://www.ruby-forum.com/u/Ajit)\
**Post date:** [August 9, 2007, 10:06pm UTC](https://www.ruby-forum.com/t/generate-time-sheet/111685/9 "2007-08-09T22:06:55Z")

</div>

On Thu, 2007-08-09 at 19:53 +0000, Ajit wrote:

> - 
> - 
> - 
> 
> 6:00 PM
> 
> thats all… using a for loop or do loop

* * *

a loop would make more sense, presuming you want to create an array of  
values to populate a selection list.

not that I really believe that this is going to help much, I am  
including my “InOuts” controller code which uses a substantial amount of  
this type of coding for recording like a punch clock (time in and time  
out)…I record actual time and then reduce it to 15 minute increments  
after subtracting the time\_in from the time\_out.

watch out for artificial line wrapping caused by e-mail client…

also note that I switched from using RAILS date\_time helpers to  
flextimes plugin and the RAILS date\_time helpers are now commented out.

Craig

class InOutsController \< ApplicationController

skip\_before\_filter :authorize, :authenticate, :only =\> ‘list’

# layout “users”

session :off, :only =\> ‘list’

def index  
list  
render :action =\> ‘list’, :layout =\> “in\_outs\_forms”  
end

# GETs should be safe (see

[URIs, Addressability, and the use of HTTP GET and POST](http://www.w3.org/2001/tag/doc/whenToUseGet.html))  
verify :method =\> :post, :only =\> [:destroy, :create, :update],  
:redirect\_to =\> { :action =\> :list }

def list  
if params[:fi\_facility].to\_s != ‘’  
fi\_facility = params[:fi\_facility]  
else  
fi\_facility = ‘Main Office’  
end  
@pager = ::Paginator.new(User.count(:all,  
:conditions =\> [“personnels.facility = ?”, fi\_facility],  
:joins =\> ‘LEFT JOIN personnels on personnels.id = personnel\_id’  
), 7) do |offset, per\_page|  
User.find(:all,  
:conditions =\> [“personnels.facility = ?”, fi\_facility],  
:joins =\> ‘LEFT JOIN personnels on personnels.id = personnel\_id’,  
:order =\> ‘users.name’,  
:select =\> ‘users.id AS id, users.personnel\_id AS personnel\_id,  
users.name AS name, users.in\_out AS in\_out, users.will\_return AS  
will\_return, personnels.facility AS facility, personnels.image AS image,  
users.personnel\_id AS personnel’,  
:limit =\> per\_page,  
:offset =\> offset)  
end  
@page = @pager.page(params[:page])  
@in\_outs = @pager.page(params[:page])  
@fi\_facility = fi\_facility  
calc\_date = (Time.now.monday - 2.days).to\_date  
@date\_range = [calc\_date - 84, calc\_date - 77, calc\_date - 70,  
calc\_date - 63, calc\_date - 56, calc\_date - 49, calc\_date - 42,  
calc\_date - 35, calc\_date - 28, calc\_date - 21, calc\_date - 14]  
render :action =\> ‘list’, :layout =\> “in\_outs\_forms”  
end

def new  
@in\_out = InOut.new  
render :action =\> ‘new’, :layout =\> “in\_outs\_forms”  
end

def create  
@in\_out = InOut.new(params[:in\_out])  
if @in\_out.save  
flash[:notice] = ‘InOut was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

def edit  
@in\_out = InOut.find(params[:id])  
render :action =\> ‘edit’, :layout =\> “in\_outs\_forms”  
end

def edit\_io  
@facility = Valuelist.find(:all, :conditions =\> [“list\_name =  
‘Facilities’”], :order =\> ‘list\_value’)  
@user = User.find\_by\_id(params[:id])

```
if @user.in_out == nil
  @in_out = InOut.new
else
  @in_out = InOut.find(:first, :conditions => ["user_id = ?",

```

params[:id] ], :order =\> ‘pair\_id DESC, id DESC’)  
end

```
if @in_out == nil
  @in_out = InOut.new
end

if @in_out.time_in != nil
  @user.in_out = "Out"
  @in_out.message = 'Your last recorded time sheet entry ' +

```

@in\_out.time\_in.strftime(“%I:%M %p”) + " on " +  
@in\_out.time\_in.strftime(“%m-%d-%Y”) + “  
**You are currently  
marked as ‘In’, you probably want to clock ‘Out’ from your last  
entry**”  
@user.pair\_id = @in\_out.pair\_id  
@user.ufacility = @in\_out.facility

```
  # 2 choices for time automatically presented to users
  @user.data_time_date = Time.now # presents actual time

```

# @user.data\_time\_date = @in\_out.time\_in # presents their clock in

time  
elsif @in\_out.time\_out != nil  
@user.ufacility = @user.personnel.facility  
@user.data\_time\_date = Time.now  
@user.in\_out = “In”  
@user.pair\_id = @in\_out.pair\_id + 1

# @in\_out.message = "Your last recorded time sheet entry was " +

format\_hours\_minutes(@in\_out.time\_out) + "   
You are currently  
marked as ‘Out’, you probably want to check ‘In’"  
@in\_out.message = “Your last recorded time sheet entry was " +  
@in\_out.time\_out.strftime(”%I:%M %p") + " on " +  
@in\_out.time\_out.strftime(“%m-%d-%Y”) +"   
**You are currently  
marked as ‘Out’, you probably want to check ‘In’**"  
else  
@user.ufacility = @user.personnel.facility  
@user.data\_time\_date = Time.now  
@user.in\_out = “In”  
@user.pair\_id = 1  
flash[:notice] = ‘You have no previously recorded time sheet  
entries   
 You want to check “In”’  
end

```
@user.rec_time = "Yes"
@user.will_return = ""

```

end

def edit\_in\_out  
@in\_out = InOut.new  
@in\_out.user\_id = params[:id]

```
if (params['user'][:data_time_date]['ampm'] == "PM" &&

```

params[‘user’][:data\_time\_date][‘hour’].to\_i \< 12)  
params[‘user’][:data\_time\_date][‘hour’] =  
params[‘user’][:data\_time\_date][‘hour’].to\_i + 12  
end

```
this_time = Time.local(params['user'][:data_time_date]['year'],
                       params['user'][:data_time_date]['month'],
                       params['user'][:data_time_date]['day'],
                       params['user'][:data_time_date]['hour'].to_s,
                       params['user'][:data_time_date]['minute'])

```

# this\_time = Time.local(params[‘user’][‘data\_time\_date(1i)’].to\_i,

# params[‘user’][‘data\_time\_date(2i)’].to\_i,

# params[‘user’][‘data\_time\_date(3i)’].to\_i,

# params[‘user’][‘data\_time\_date(4i)’].to\_i,

# params[‘user’][‘data\_time\_date(5i)’].to\_i)

```
if params[:user][:in_out] == "In"
   @in_out.time_in = this_time
elsif params[:user][:in_out] == "Out"
   @in_out.time_out = this_time
end
@in_out.facility = params[:user][:ufacility]
params[:user].delete("ufacility")

```

# params[:user].delete(“data\_time\_date(1i)”)

# params[:user].delete(“data\_time\_date(2i)”)

# params[:user].delete(“data\_time\_date(3i)”)

# params[:user].delete(“data\_time\_date(4i)”)

# params[:user].delete(“data\_time\_date(5i)”)

```
@in_out.subj_date = this_time.strftime("%m/%d/%Y")
@in_out.pair_id = params[:user][:pair_id]
@in_out.will_return = params[:user][:will_return]
@in_out.updated_by = session[:user_name]
@user = User.find_by_id(@in_out.user_id)

```

# session[:test] = @in\_out.user\_id

```
if params[:user][:rec_time] == "Yes"

  if params[:user][:in_out] == "Out" &&

```

InOut.find(:first, :conditions =\> [“user\_id = ? AND pair\_id = ?”,  
params[:id], params[:user][:pair\_id] ]) == nil  
flash[:notice] = “Your last recorded time sheet entry has you  
marked as ‘Out’  
You probably want to check ‘In’”  
redirect\_to :action =\> ‘edit\_io’, :id =\> @in\_out.user\_id  
return  
elsif params[:user][:in\_out] == “In” &&  
InOut.find(:first, :conditions =\> [“user\_id = ? AND pair\_id = ?”,  
params[:id], params[:user][:pair\_id] ]) != nil  
flash[:notice] = “Your last recorded time sheet entry has you  
marked as ‘In’  
You probably want to check ‘Out’”

# redirect\_to :action =\> ‘edit\_io’, :id =\> @in\_out.user\_id

```
  redirect_to :back
    return
  end

  if params[:user][:in_out] == "Out" &&

```

InOut.find(:first, :conditions =\> [“user\_id = ? AND pair\_id = ?”,  
params[:id], params[:user][:pair\_id] ]).time\_in \> @in\_out.time\_out  
flash[:notice] = “You are trying to record an entry for” +  
format\_date\_and\_time(@in\_out.time\_out) + "   
 which is earlier than  
your last ‘Time In’ entry shown below.   
 It is assumed that this is  
not what you intended - try again"

# redirect\_to :action =\> ‘edit\_io’, :id =\> @in\_out.user\_id

```
  redirect_to :back
    return
  end

  if @in_out.save
    @user.update_attributes(params[:user])
    flash[:notice] = 'In/Out was successfully recorded.'

```

# redirect\_to :action =\> ‘edit\_io’

```
    redirect_to :back
  else

```

# render :action =\> ‘edit\_io’, :id =\> @in\_out.user\_id

```
    redirect_to :back
  end
else
  @user.update_attributes(params[:user])
  flash[:notice] = 'In/Out was successfully recorded.'

```

# redirect\_to :action =\> ‘edit\_io’, :id =\> @in\_out.user\_id

```
  redirect_to :back
end

```

end

def time\_sheet\_selected\_week  
start\_date = params[:in\_out][:time\_sheet\_selected\_week].to\_date

# start\_date = start\_date.strftime(“%m/%d/%Y”).to\_date

```
time_sheet start_date

```

end

def time\_sheet\_this\_week  
start\_date = Time.now.monday - 2.days  
start\_date = start\_date.strftime(“%m/%d/%Y”).to\_date  
time\_sheet start\_date  
end

def time\_sheet\_last\_week  
start\_date = Time.now.monday - 9.days  
start\_date = start\_date.strftime(“%m/%d/%Y”).to\_date  
time\_sheet start\_date  
session[:st] = start\_date  
end

def time\_sheet(start\_day)  
@sat\_day = start\_day  
@sun\_day = @sat\_day + 1  
@mon\_day = @sat\_day + 2  
@tue\_day = @sat\_day + 3  
@wed\_day = @sat\_day + 4  
@thu\_day = @sat\_day + 5  
@fri\_day = @sat\_day + 6  
@user = User.find\_by\_id(params[:id])

```
@in_out_sat = InOut.find(:all,
  :conditions => ["user_id = ? AND subj_date = ?", params[:id],

```

@sat\_day],  
:order =\> “pair\_id, updated\_at”)  
@in\_out\_sun = InOut.find(:all,  
:conditions =\> [“user\_id = ? AND subj\_date = ?”, params[:id],  
@sun\_day],  
:order =\> “pair\_id, updated\_at”)  
@in\_out\_mon = InOut.find(:all,  
:conditions =\> [“user\_id = ? AND subj\_date = ?”, params[:id],  
@mon\_day],  
:order =\> “pair\_id, updated\_at”)  
@in\_out\_tue = InOut.find(:all,  
:conditions =\> [“user\_id = ? AND subj\_date = ?”, params[:id],  
@tue\_day],  
:order =\> “pair\_id, updated\_at”)  
@in\_out\_wed = InOut.find(:all,  
:conditions =\> [“user\_id = ? AND subj\_date = ?”, params[:id],  
@wed\_day],  
:order =\> “pair\_id, updated\_at”)  
@in\_out\_thu = InOut.find(:all,  
:conditions =\> [“user\_id = ? AND subj\_date = ?”, params[:id],  
@thu\_day],  
:order =\> “pair\_id, updated\_at”)  
@in\_out\_fri = InOut.find(:all,  
:conditions =\> [“user\_id = ? AND subj\_date = ?”, params[:id],  
@fri\_day],  
:order =\> “pair\_id, updated\_at”)

```
@tot_hrs_sat = tot_hrs_day @sat_day, params[:id]
@tot_hrs_sun = tot_hrs_day @sun_day, params[:id]
@tot_hrs_mon = tot_hrs_day @mon_day, params[:id]
@tot_hrs_tue = tot_hrs_day @tue_day, params[:id]
@tot_hrs_wed = tot_hrs_day @wed_day, params[:id]
@tot_hrs_thu = tot_hrs_day @thu_day, params[:id]
@tot_hrs_fri = tot_hrs_day @fri_day, params[:id]
@tot_hrs_week = @tot_hrs_mon + @tot_hrs_tue + @tot_hrs_wed +

```

@tot\_hrs\_thu + @tot\_hrs\_fri + @tot\_hrs\_sat + @tot\_hrs\_sun  
end

def update  
@in\_out = InOut.find(params[:id])  
if @in\_out.update\_attributes(params[:in\_out])  
flash[:notice] = ‘InOut was successfully updated.’  
redirect\_to :action =\> ‘show’, :id =\> @in\_out  
else  
render :action =\> ‘edit’  
end  
end

def destroy  
InOut.find(params[:id]).destroy  
redirect\_to :action =\> ‘list’  
end

def show\_person\_week  
start\_date = params[:in\_out][:time\_sheet\_selected\_week].to\_date  
end\_date = (params[:in\_out][:time\_sheet\_selected\_week].to\_date + 7)  
@person = params[:in\_out][:personnel]  
employee = User.find(:first, :conditions =\> [“name = ?”,  
@person]).id  
cond = EZ::Where::Condition.new do  
user\_id == employee  
subj\_date \<=\> (start\_date… end\_date)  
end  
@start\_date = start\_date  
@end\_date = end\_date  
@in\_outs = InOut.find(:all, :conditions =\> cond.to\_sql, :order =\>  
‘pair\_id, id’)  
render :action =\> ‘show\_person\_week’, :layout =\> “utilities”  
end

def edit\_io\_record\_in  
@facility = Valuelist.find(:all, :conditions =\> [“list\_name =  
‘Facilities’”], :order =\> ‘list\_value’)  
@user = User.find\_by\_id(params[:u\_id])  
@in\_out = InOut.find(params[:id])  
@in\_out.message = “Now editing time record”  
render :action =\> ‘edit\_io\_record\_in’  
end

def edit\_io\_record\_out  
@facility = Valuelist.find(:all, :conditions =\> [“list\_name =  
‘Facilities’”], :order =\> ‘list\_value’)  
@user = User.find\_by\_id(params[:u\_id])  
@in\_out = InOut.find(params[:id])  
@in\_out.message = “Now editing time record”  
render :action =\> ‘edit\_io\_record\_out’  
end

def update\_in  
@in\_out = InOut.find(params[:id])  
if (params[‘in\_out’][:time\_in][‘ampm’] == “PM” &&  
params[‘in\_out’][:time\_in][‘hour’].to\_i \< 12)  
params[‘in\_out’][:time\_in][‘hour’] =  
params[‘in\_out’][:time\_in][‘hour’].to\_i + 12  
end

```
this_time = Time.local(params['in_out'][:time_in]['year'],
                       params['in_out'][:time_in]['month'],
                       params['in_out'][:time_in]['day'],
                       params['in_out'][:time_in]['hour'].to_s,
                       params['in_out'][:time_in]['minute'])

@in_out.time_in = this_time
@in_out.subj_date = this_time.strftime("%m/%d/%Y")
@in_out.facility = params[:user][:ufacility]
params[:user].delete("ufacility")
@in_out.updated_by = session[:user_name]
if @in_out.save
  flash[:notice] = 'In/Out was successfully recorded.'

```

# redirect\_to :action =\> ‘edit\_io’

```
  redirect_to :back
else

```

# render :action =\> ‘edit\_io’, :id =\> @in\_out.user\_id

```
  flash[:notice] = 'There was a problem recording your changes.'
  redirect_to :back
end

```

end

def update\_out  
@in\_out = InOut.find(params[:id])  
if (params[‘in\_out’][:time\_out][‘ampm’] == “PM” &&  
params[‘in\_out’][:time\_out][‘hour’].to\_i \< 12)  
params[‘in\_out’][:time\_out][‘hour’] =  
params[‘in\_out’][:time\_out][‘hour’].to\_i + 12  
end

```
this_time = Time.local(params['in_out'][:time_out]['year'],
                       params['in_out'][:time_out]['month'],
                       params['in_out'][:time_out]['day'],
                       params['in_out'][:time_out]['hour'].to_s,
                       params['in_out'][:time_out]['minute'])

@in_out.time_out = this_time
@in_out.subj_date = this_time.strftime("%m/%d/%Y")
@in_out.facility = params[:user][:ufacility]
params[:user].delete("ufacility")
@in_out.updated_by = session[:user_name]
if @in_out.save
  flash[:notice] = 'In/Out was successfully recorded.'

```

# redirect\_to :action =\> ‘edit\_io’

```
  redirect_to :back
else

```

# render :action =\> ‘edit\_io’, :id =\> @in\_out.user\_id

```
  flash[:notice] = 'There was a problem recording your changes.'
  redirect_to :back
end

```

end

private

def tot\_hrs\_day(start\_date, this\_id)  
total\_time = 0  
@recs = InOut.find(:all, :conditions =\> [“user\_id = ? AND subj\_date  
= ? AND time\_in IS NOT NULL”, this\_id, start\_date])  
for pair\_total in @recs  
pair\_amount = hrs\_pair pair\_total.pair\_id, start\_date, this\_id  
total\_time = pair\_amount + total\_time  
end

# if total\_time == 0

```
  total_time

```

# else

# total\_time.to\_fl(2)

# end

end

def hrs\_pair(pair\_id, start\_date, this\_id)  
@io = InOut.find(:all, :conditions =\> [“pair\_id = ? AND subj\_date  
= ? AND user\_id = ?”, pair\_id, start\_date, this\_id], :order =\>  
‘updated\_at DESC’)  
if @io[0][:time\_out] == nil  
my\_time = 0  
else  
my\_time = ((@io[0][:time\_out] - @io[1][:time\_in]) / 900.00 ).round

- 0.25  
end  
my\_time  
end

end

–  
Craig W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
