<commit_before>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
    
    # check box for inactive staff members
    if params[:show_disabled] and params[:show_disabled].to_i == 1
      hash[:active] = [true, false]
    else
      hash[:active] = true
    end
    @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}<commit_msg>Feature #665 Linking Insurance with specific loan<commit_after>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
    
    # check box for inactive staff members
    if params[:show_disabled] and params[:show_disabled].to_i == 1
      hash[:active] = [true, false]
    else
      hash[:active] = true
    end
    @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
      folder   = File.join(Merb.root, "public", "pdfs")
      FileUtils.mkdir_p(folder)
      filename = File.join(folder, "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}
{'fraction_non_alphanumeric': 0.13438284280173396, 'fraction_numerical': 0.001026694045174538, 'mean_word_length': 3.8117453347969263, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6517498', 'n_tokens_mistral': 3109, 'n_tokens_neox': 3032, 'n_words': 573}