handlers/kinetic_cost_calculate_costs_v1/handler/init.rb
# Creates a record in the KS_SRV_Helper form
#
# This is a required method that is automatically called by the Kinetic Task
# Engine.
#
# ==== Returns
# An Xml formatted String representing the return variable results.
Creates a record in the KS_SRV_Helper form
This is a required method that is automatically called by the Kinetic Task
Engine.

Returns
An Xml formatted String representing the return variable results.
Creates a record in the KS_SRV_Helper form This is a required method that is automatically called by the Kinetic Task Engine . Returns An Xml formatted String representing the return variable results .
def execute()
    entry = @@remedy_forms['KS_SRV_Helper'].find_entries(
      :all,
      :conditions => [%|'Index Field1' = "Kinetic Cost" and 'Index Field2' = "#{@parameters['CostCategory']}"|],
      :fields => ['Character Field1','Character Field2','Character Field3','Character Field4']
    )
    if entry.nil?
      raise(%|A Cost entry can't be found in KS_SRV_Helper|)
    end
	costs_by_question_answer = {}
	answers = {}
	total_cost = 0
	entry.each do |entry|
		qstn_details_obj = {}
		qstn_details_obj['value'] = entry['Character Field3']
		if entry['Character Field4']
			qstn_details_obj['qty_qstn'] = entry['Character Field4']
		end	
		if costs_by_question_answer[entry['Character Field1']].nil?
			costs_by_question_answer[entry['Character Field1']] = {}
		end
		costs_by_question_answer[entry['Character Field1']][entry['Character Field2']] = qstn_details_obj
	end
	puts("Cost Results: #{costs_by_question_answer.inspect}") if @debug_logging_enabled
    REXML::XPath.match(@input_document, '/handler/answers/answer').each do |node|
      answers[node.attribute('name').value] = node.text.to_s
    end
	puts("answers: #{answers.inspect}") if @debug_logging_enabled
	answers.each do |question, value|
		if value != "" and costs_by_question_answer[question]
			qstn_details_obj = costs_by_question_answer[question]
			value_array = value.split(', ');
			value_array.each do |v|
				if !costs_by_question_answer[question][v].nil? and costs_by_question_answer[question][v]['value'] and !costs_by_question_answer[question][v]['qty_qstn']
					puts("Adding $#{costs_by_question_answer[question][v]['value']} to the total for question: #{question} for a #{v}") if @debug_logging_enabled
					total_cost += costs_by_question_answer[question][v]['value'].to_f
				elsif !costs_by_question_answer[question][v].nil? and costs_by_question_answer[question][v]['value'] and costs_by_question_answer[question][v]['qty_qstn']
					qty = answers[costs_by_question_answer[question][v]['qty_qstn']].to_f
					if qty > 0
						item_cost = costs_by_question_answer[question][v]['value'].to_f * qty
						total_cost += item_cost
						puts("Adding $#{item_cost} to the total for question: #{question} for #{qty} #{v}s at $#{costs_by_question_answer[question][v]['value']} each") if @debug_logging_enabled
					end
				end
				puts("Running total is: $#{total_cost}")
			end
		end
	end
	total_cost = sprintf('%.2f', total_cost)
	puts("Total Cost = #{total_cost}")
    results = <<-RESULTS
    <results>
      <result name="Total">#{escape(total_cost)}</result>
    </results>
    RESULTS
    puts("Results: \n#{results}") if @debug_logging_enabled
    return results
  end
def execute ( ) entry = @@remedy_forms [ 'KS_SRV_Helper' ] . find_entries ( :all , :conditions => [ %|'Index Field1' = "Kinetic Cost" and 'Index Field2' = "#{@parameters['CostCategory']}"| ] , :fields => [ 'Character Field1' , 'Character Field2' , 'Character Field3' , 'Character Field4' ] ) if entry . nil? raise ( %|A Cost entry can't be found in KS_SRV_Helper| ) end costs_by_question_answer = { } answers = { } total_cost = 0 entry . each do | entry | qstn_details_obj = { } qstn_details_obj [ 'value' ] = entry [ 'Character Field3' ] if entry [ 'Character Field4' ] qstn_details_obj [ 'qty_qstn' ] = entry [ 'Character Field4' ] end if costs_by_question_answer [ entry [ 'Character Field1' ] ] . nil? costs_by_question_answer [ entry [ 'Character Field1' ] ] = { } end costs_by_question_answer [ entry [ 'Character Field1' ] ] [ entry [ 'Character Field2' ] ] = qstn_details_obj end puts ( "Cost Results: #{costs_by_question_answer.inspect}" ) if @debug_logging_enabled REXML :: XPath . match ( @input_document , '/handler/answers/answer' ) . each do | node | answers [ node . attribute ( 'name' ) . value ] = node . text . to_s end puts ( "answers: #{answers.inspect}" ) if @debug_logging_enabled answers . each do | question , value | if value != "" and costs_by_question_answer [ question ] qstn_details_obj = costs_by_question_answer [ question ] value_array = value . split ( ', ' ) ; value_array . each do | v | if ! costs_by_question_answer [ question ] [ v ] . nil? and costs_by_question_answer [ question ] [ v ] [ 'value' ] and ! costs_by_question_answer [ question ] [ v ] [ 'qty_qstn' ] puts ( "Adding $#{costs_by_question_answer[question][v]['value']} to the total for question: #{question} for a #{v}" ) if @debug_logging_enabled total_cost += costs_by_question_answer [ question ] [ v ] [ 'value' ] . to_f elsif ! costs_by_question_answer [ question ] [ v ] . nil? and costs_by_question_answer [ question ] [ v ] [ 'value' ] and costs_by_question_answer [ question ] [ v ] [ 'qty_qstn' ] qty = answers [ costs_by_question_answer [ question ] [ v ] [ 'qty_qstn' ] ] . to_f if qty > 0 item_cost = costs_by_question_answer [ question ] [ v ] [ 'value' ] . to_f * qty total_cost += item_cost puts ( "Adding $#{item_cost} to the total for question: #{question} for #{qty} #{v}s at $#{costs_by_question_answer[question][v]['value']} each" ) if @debug_logging_enabled end end puts ( "Running total is: $#{total_cost}" ) end end end total_cost = sprintf ( '%.2f' , total_cost ) puts ( "Total Cost = #{total_cost}" ) results = <<-RESULTS 
    <results>
      <result name="Total"> #{ escape ( total_cost ) } </result>
    </results>

     RESULTS puts ( "Results: \n#{results}" ) if @debug_logging_enabled return results end
Creates a record in the KS_SRV_Helper form
This is a required method that is automatically called by the Kinetic Task
Engine.
Creates a record in the KS_SRV_Helper form This is a required method that is automatically called by the Kinetic Task Engine .
# Check for existing KS_SRV_Helper entry. #Create an object to represent the costs #formatted as: "{"chkbox_accessory_power_cable":{"Docking Station":{"value":"10.905"},"Laptop":{"value":"5"}},"chkbox_accessory_network_cable":{"CISCO Phone Cable":{"value":"5","qty_qstn":"accessory_network_cables"},"10 ft Network Cable":{"value":"7","qty_qstn":"accessory_network_cables"}}}" # Retrieve all of the answers #Iterate through the questions of the submission # If there is a price spec for the current question. # Iterate through selected values (could be multiple for check boxes). # If there is a price defined for the currents question's value. # Build the results to be returned by this handler # Return the results String