  def self.get_carried_over(user_id, year)

    SqlHelper.validate_token([user_id, year])

    yaml = ApplicationHelper.get_config_yaml
    unless yaml[:timecard].nil?
      paidhld_carry_over = yaml[:timecard]['paidhld_carry_over']
    end

    return 0 if paidhld_carry_over.nil? or paidhld_carry_over.empty? or paidhld_carry_over == PaidHoliday::CARRY_OVER_NONE

    begin
      con = "(user_id=#{user_id}) and (year < #{year})"
      paidhlds = PaidHoliday.where(con).order('year ASC').to_a
    rescue
    end
    return 0 if paidhlds.nil? or paidhlds.empty?

    sum = 0
    year_begins_from, month_begins_at = TimecardsHelper.get_fiscal_params

    if paidhld_carry_over == PaidHoliday::CARRY_OVER_1_YEAR

      last_carried_out = 0

      for y in paidhlds.first.year .. year - 1
        paidhld = paidhlds.find { |hld| hld.year == y }
        given_num = (paidhld.nil?)?0:paidhld.num

        start_date, end_date = TimecardsHelper.get_year_span(y, year_begins_from, month_begins_at)
        applied_paid_hlds = Timecard.applied_paid_hlds(user_id, start_date, end_date)

        if applied_paid_hlds >= last_carried_out
          last_carried_out = given_num - (applied_paid_hlds - last_carried_out)
        else
          last_carried_out = given_num
        end
      end

      return last_carried_out

    elsif paidhld_carry_over == PaidHoliday::CARRY_OVER_NO_EXPIRATION

      paidhlds.each do |paidhld|
        sum += paidhld.num
      end

      start_date, dummy = TimecardsHelper.get_year_span(paidhlds.first.year, year_begins_from, month_begins_at)
      dummy, end_date = TimecardsHelper.get_year_span(year - 1, year_begins_from, month_begins_at)
      applied_paid_hlds = Timecard.applied_paid_hlds(user_id, start_date, end_date)

      return (sum - applied_paid_hlds)
    else
      return 0
    end
  end