jeudi 5 novembre 2015

How to loop through each user and grab all of the data I need to determine something.

I have a Rails application that I wrote for my employees to use for requesting time off. I am having a tough time coming up with something to do a double check on the request.

Each employee has a adj_service_date so each year they will receive a set amount of days:

  • Joe was hired '21-OCT-15'.
  • Bill was hired '21-FEB-15'.
  • Justin was hired '02-JAN-15'.

Each employee has a set amount of vacation days:

  • Joe has 40 vacation hours.
  • Bill has 80 vacation hours.
  • Justin has 16 vacation hours.

I gave them the ability to request days after their adj_service_date, which changes each year.

If Joe requested to take off '22-OCT-16', which will be after his hire-date anniversary so he will receive 40 vacation hours.

I want to check he has enough hours after his hire-date anniversary to make sure he is not requesting more than he should be.

The problem I am having is trying to do it on multiple people which will all have different hire-date anniversaries and vacation amounts.

This is my Entry model:

class Entry < ActiveRecord::Base

   #-----FURTURE DOBLE CHECK ---------------------------------------------------------------------------------------------------------

    def self.future_double_check
      f_request = Entry.future_req_double_check.pluck(:emp_id)
      f_request.map do |f_s|
        f_request_person = User.where("emp_id = ?", f_s).pluck(:person_id)
        f_request_person.map do |f_dub|
          my_anv = Empcomp.where("person_id = ?", f_dub).pluck(:adj_service_date).first.change(:year => Time.now.year)
          if my_anv < Time.now
            my_anv = Empcomp.where("person_id = ?", f_dub).pluck(:adj_service_date).first.change(:year => Time.now.year + 1)
         else
            my_anv = Empcomp.where("person_id = ?", f_dub).pluck(:adj_service_date).first.change(:year => Time.now.year)
         end
         get_sum_vac = Entry.where("emp_id = ? and indirect_id = ? and leave_start >= ? and future = ? and approve_disapprove = ?", f_s, 'VAC', my_anv, 'YES', '1').sum :hours


         get_acc_vac = MyGroupAcc.where("person_id = ? and is_active = ? and accrual_code LIKE ?", f_dub, "Y", "%VAC%").pluck(:accum_accrued)

         q = get_sum_vac.to_s.split('')
         t = get_acc_vac.map(&:to_s)
         r = t.each {|a| a.strip! if a.respond_to? :strip! }


         #get_acc_vac.each do |v_dub|
         #get_sum_vac.each do |ve_dub|
           if q > get_acc_vac
             EntryMailer.f_double_check_notification_fail(entries).deliver
           else
             EntryMailer.f_double_check_notification_okay(entries).deliver
           end
         end
       end
     end   
     #-----------------------------------------------------------------------------------------------------------------------------------

How can I check each person after their hire-date anniversary and then send out an email if they are using more or less than they are supposed to?

Aucun commentaire:

Enregistrer un commentaire