Calculator controller to build the form and run the calculations.

Individual calculations for different flavours of instrument are packaged into separate files. This code requires those files to be loaded.

 4 require 'calculations/bicameral_both_houses_sitting'
 5 require 'calculations/bicameral_si_either_house_sitting'
 6 require 'calculations/commons_only_si'
 7 require 'calculations/commons_only_sitting_days'
 8 require 'calculations/pnsi'
 9 require 'calculations/treaty'
 10 require 'calculations/interval'

The controller itself.

 13 class CalculatorController < ApplicationController

Include code from each of the modules for the different styles of calculation.

 16   include CALCULATION_BICAMERAL_BOTH_HOUSES_SITTING
 17   include CALCULATION_BICAMERAL_SI_EITHER_HOUSE_SITTING
 18   include CALCULATION_COMMONS_ONLY_SI
 19   include CALCULATION_COMMONS_ONLY_SITTING_DAYS
 20   include CALCULATION_PNSI
 21   include CALCULATION_TREATY
 22   include CALCULATION_INTERVAL

This is the code to provide information for the form that users can fill in.

 25   def index

Set a title for the page.

 28     @title = "Calculate scrutiny periods"

Find all the active procedures in display order - to populate the procedure radio buttons on the form.

 31     @procedures = Procedure.all.where( 'active is true' ).order( 'display_order asc')
 32   end

This is the code to provide information for the form that users wishing to run a specific calculation style can fill in.

 35   def style

Set a title for the page.

 38     @title = "Calculate scrutiny periods"
 39   end

This code runs the calculation.

 42   def calculate

In order to calculate the scrutiny period, we need:

 47     start_date = params['start-date']
 50     day_count = params['day-count']
 53     procedure = params['procedure'].to_i if params['procedure']
 56     calculation_style = params['calculation-style'].to_i if params['calculation-style']

If neither the procedure nor the calculation style have been provided, or the start date has not been provided ...

 59     if ( procedure.nil? and calculation_style.nil? ) or start_date.blank?

... we set an error message ...

 62 	    @title = "Sorry, we need more information to complete the calculation"

... and display the error.

 65       render :template => 'calculator/not_enough_information'

If the start date and either the procedure or the calculation style have been provided by the initial form, we ...

 68     else
 71       @start_date = Date.parse( start_date )

* find the procedure if there is one

 74       @procedure = Procedure.find( procedure ) if procedure

* set the calculation style if there is one

 77       @calculation_style = calculation_style if calculation_style

If the day count has not been provided by the day count form or the day count is 0 ...

 80       if day_count.blank? or day_count.to_i == 0

... we set a title for the page.

 83     	  @title = "Calculate scrutiny period"

We render the day count form.

 86         render :template => 'calculator/day_count_form'

If the day count has been provided by the day count form ...

 89       else

... we set a title for the page.

 92     	  @title = "Calculated scrutiny period"

We get the day count as an integer.

 95         @day_count = day_count.to_i

If the procedure has been selected ...

 98         if @procedure

To calculate the anticipated end date, we select the calculation based on the type of procedure:

 101           case @procedure.id
 104           when 1, 17, 18, 19, 2, 4, 21, 22, 23
 106             @start_date_type = "laying date"
 107             @scrutiny_end_date = bicameral_calculation_both_houses_sitting( @start_date, @day_count )
 110           when 3
 112             @start_date_type = "laying date"
 113             @scrutiny_end_date = pnsi_calculation( @start_date, @day_count )
 116           when 5
 118             @start_date_type = "laying date"
 119             @scrutiny_end_date = commons_only_si_calculation( @start_date, @day_count )
 122           when 6, 13, 14
 124             @start_date_type = "laying date"
 125             @scrutiny_end_date = bicameral_si_either_house_sitting_calculation( @start_date, @day_count )
 128           when 7
 130             @start_date_type = "making date"
 131             @scrutiny_end_date = commons_only_si_calculation( @start_date, @day_count )
 134           when 8
 136             @start_date_type = "making date"
 137             @scrutiny_end_date = bicameral_calculation_both_houses_sitting( @start_date, @day_count )
 140           when 9, 15, 16
 142             @start_date_type = "making date"
 143             @scrutiny_end_date = bicameral_si_either_house_sitting_calculation( @start_date, @day_count )
 146           when 10
 148             @start_date_type = "laying date"
 149             @scrutiny_end_date = treaty_calculation( @start_date, @day_count )
 152           when 11
 154             @start_date_type = "date of Ministerial statement"
 155             @scrutiny_end_date = treaty_calculation( @start_date, @day_count )
 158           when 12
 160             @start_date_type = "date of publication"
 161             @scrutiny_end_date = bicameral_calculation_both_houses_sitting( @start_date, @day_count )
 164           when 20
 166             @start_date_type = "laying date"
 167             @scrutiny_end_date = commons_only_sitting_days( @start_date, @day_count )
 168           end

Otherwise, if the calculation style has been selected ...

 171         elsif @calculation_style

... to calculate the anticipated end date, we select the calculation based on the calculation style:

 174           case @calculation_style
 177           when 1
 179             @scrutiny_end_date = bicameral_calculation_both_houses_sitting( @start_date, @day_count )
 182           when 2
 184             @scrutiny_end_date = bicameral_si_either_house_sitting_calculation( @start_date, @day_count )
 187           when 3
 189             @scrutiny_end_date = commons_only_si_calculation( @start_date, @day_count )
 192           when 4
 194             @scrutiny_end_date = pnsi_calculation( @start_date, @day_count )
 197           when 5
 199             @scrutiny_end_date = treaty_calculation( @start_date, @day_count )
 202           when 6
 204             @scrutiny_end_date = commons_only_sitting_days( @start_date, @day_count )
 205           end
 206         end
 207       end
 208       @alternate_title = 'anticipated end date of the scrutiny period'
 209       @json_url = request.original_fullpath.sub '?', '.json?'
 210       @calendar_links = []
 211       calendar_link = ['Anticipated end date of the scrutiny period', request.original_fullpath.sub( '?', '.ics?' )]
 212       @calendar_links << calendar_link
 213     end
 214   end

This is the code to build the interval calculation form.

 217   def interval
 220     @title = "Calculate sitting days during an interval"

We get both Houses.

 223     @houses = House.all
 224   end

This is the code to calculate the number of sitting days during an interval of time.

 227   def interval_calculate

In order to calculate the number of sitting days during an interval, we need:

 232     start_date = params['start-date']
 235     end_date = params['end-date']

If neither the start date nor the ** end date** has not been provided ...

 238     if start_date.blank? or end_date.blank?

... we set an error message ...

 241 	    @title = "Sorry, we need more information to complete the calculation"

... and display the error.

 244       render :template => 'calculator/interval_not_enough_information'

Otherwise, if both the start date and the end date have been provided ...

 247     else

... we set a title for the page.

 250     	@title = "Number of sitting days within the interval"

We make the text of the start date and end date passed into date formats.

 253       @start_date = Date.parse( start_date )
 254       @end_date = Date.parse( end_date )

If the start date is the same as or after the end date ...

 257       if @start_date >= @end_date

... we set a title for the page ...

 260     	  @title = "Unable to calculate sitting days"

... construct an error mesage.

 263         @error_message = "The start date must precede the end date."

... and display the error.

 266         render :template => 'calculator/interval_error'

Otherwise, if the end date is after the start date ...

 269       else

... we set the sitting day counts ...

 272         @commons_sitting_day_count = 0
 273         @lords_sitting_day_count = 0

... and calculate the sitting days in the interval.

 276         calculate_sitting_days_in_interval( @start_date, @end_date )
 277       end
 278     end
 279   end
 280 end