Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Admin::SpreeTrackingTntController < ApplicationController
- require_role "admin" # You might want to remove this, and add security in the /config/easy_role_permissions.yml file
- layout 'admin'
- # Generate TNT File for shipment id
- def generate
- # Retrieve shipment object
- tmp_shipment = Shipment.find(params[:id])
- # Retrieve text concate and store into a variable
- tnt_csv_string = process_fields(tmp_shipment)
- end
- ###################
- # Private Section #
- ###################
- private
- # Concate Fields into a single text variable
- # Via XML Pattern Configuration File
- # shipment : represent shipment object
- def process_fields(shipment)
- # Return variable
- result = ''
- # Retrieve fields for this shipment
- hash_fields = define_hash_fields(shipment)
- p "#{hash_fields}"
- # Defile path of xml configuration
- xml_file = "#{SPREE_ROOT}/vendor/extensions/spree_tracking_tnt/config/spree_tracking_tnt_pattern.xml"
- # If the configuration file is present
- if File.exist?(xml_file)
- # Read XML File
- xml = File.read(xml_file)
- # Parse XML File
- doc = Hpricot::XML(xml)
- # Process XML File
- (doc/:spree_tracking_tnt).each do |root|
- (root/:field).each_with_index do |field, ind|
- # Retrieve processing field
- processing_field = process_field(field, hash_fields)
- message = ''
- message = "'#{processing_field}' : #{processing_field.length}" unless processing_field.nil?
- p "FIELD #{ind} : #{message}"
- # Concate to the result
- result += processing_field unless processing_field.nil?
- end
- end
- end
- p "#{result}"
- end
- #########################
- # GENERAL FIELDS GETTER #
- #########################
- # Process field
- def process_field(field, hash_fields)
- # Instanciate variable
- result = ''
- p "PROCESS FIELD #{hash_fields}"
- # Retrieve name field
- name = field.at("/name").inner_html.to_s unless field.at("/name").nil?
- # 2 kind of field
- # PADDING with the only value property and (e.g. '-') and length properties
- # Field including name and (perhaps) value / min-length - max-length / padding property
- if name.nil?
- # First Case
- # Padding of VALUE while LENGTH times
- result = process_field_padding(field)
- p "PROCESS WITHOUT NAME"
- else
- # Second Case
- # Including some property
- # Instanciate value
- value = field.at("/value").inner_html.to_s unless field.at("/value").nil?
- # If there aren t value
- if value.nil?
- # Retrieve Hash fields
- value = hash_fields[:city]
- end
- p "RETRIEVES NAME : #{name}"
- p "RETRIEVES VALUEE : #{value}"
- p "RETRIEVES MANUAL VALUE : #{hash_fields['city']}"
- # AT THIS POINT, WE HAVE STORE "VALUE" VALUE
- # NEXT STEP CHECK IF THERE ARE A CONSTRAINT PADDING PROPERTY
- # Boolean test
- value_min_length = field.at("min-length").inner_html.to_s unless field.at("min-length").nil?
- value_max_length = field.at("max-length").inner_html.to_s unless field.at("max-length").nil?
- # If the value is constraint by a length condition
- unless value_min_length.nil? || value_max_length.nil?
- p "PLOOOOp"
- # Define constraint part
- constraint_part = (field/:pattern)
- # Unless value_min is nil?
- unless value_min_length.nil?
- # Kind of condition => MIN-LENGTH => PADDING MODE
- kind = 'min'
- # Define length condition
- length_condition = value_min_length
- else
- # Kind of condition => MAX-LENGTH => TRUNCATE MODE
- kind = 'max'
- # Define length condition
- length_condition = value_max_length
- end
- # Process constraint on the value
- result = process_constraint_length_condition(value,length_condition,constraint_part, kind)
- else
- # No Length condition
- # Define result equal to value
- result = value
- end
- end
- # Return value for this field
- return result
- end
- # Process a constraint field
- # value : Applying constraint value
- # length_condition : number of (MIN|MAX) length for "value"
- # constraint : Constraint XML Node
- # kind : max or min
- def process_constraint_length_condition(value,length_condition, constraint, kind)
- # Instanciate variable
- result = ''
- # Display message
- p "#{value} : #{length_condition} : #{kind}"
- # If Constraint is present
- unless constraint.nil?
- # Constraint is to perfrom a padding (left or right) with a character or truncate (left or right) the value
- value_constraint_character = constraint.at("value").inner_html.to_s
- position_pattern = constraint.at("position").inner_html.to_s
- end
- # If kind is max
- if kind.eql?('max')
- # If the length condition is not respected
- if value.length > length_condition
- # Truncate the value (left or right)
- result = process_max_condition_on_value(value,length_condition, position_pattern)
- else
- # Condition MAX is respected
- result = value
- end
- else
- # Kind is Min
- #
- # MIN is different than MAX
- # MIN Check the minimum character
- # If the length condition is not respected
- if value.length < length_condition
- result = process_min_condition_on_value(value,length_condition, value_constraint_character, position_pattern)
- else
- # Condition MIN is respected
- result = value
- end
- end
- # Return
- return result
- end
- # Process MIN condition on value
- def process_min_condition_on_value(value, min_length, padding_character, position_pattern)
- # Instanciate variable
- result = ''
- required_character = 0
- p "MIN CONDITION #{value} : #{min_length}"
- # Count the number of times needed to pass the condition
- required_character = min_length - value.length
- # If they don t have position_padding
- if position_pattern.nil?
- # Use default position_padding
- position_pattern = "right"
- end
- # Perform truncate (LEFT or RIGHT)
- case position_pattern
- when "left" then required_character.times { result =+ padding_character }
- when "right" then required_character.times { result += padding_character }
- end
- # Return result
- return result
- end
- # Process MAX condition on value
- def process_max_condition_on_value(value, max_length, position_pattern)
- # Instanciate variable
- result = ''
- # If they don t have position_padding
- if position_pattern.nil?
- # Use default position_padding
- position_pattern = "right"
- end
- # Perform truncate (LEFT or RIGHT)
- case position_pattern
- when "left" then result = value[max_length, (value.length-1)]
- when "right" then result = value[0, max_length]
- end
- # Return result
- return result
- end
- # Process a padding field
- def process_field_padding(field)
- # Instanciate variable
- result = ''
- # Retrieve required variable
- max_i = field.at("length").inner_html.to_i
- value = field.at("value").inner_html.to_s
- max_i.times { result += value}
- return result
- end
- # Define fields for a shipment
- # shipment : Represent the shipment object
- def define_hash_fields(shipment)
- # Instanciate Hash
- fields = Hash.new
- ##################
- # EDIT THIS HASH #
- ##################
- # Initialize fields Hash
- # Each key of Fields Hash MUST match XML Fields Value
- # Link Each Key Field with a function to retrieve value from ActiveRecord
- fields = {
- "shipment_saturday_or_week" => "#{retrieve_shipment_saturday_or_week(shipment)}",
- "shipment_weight" => "#{retrieve_shipment_weight(shipment)}",
- 'client_id' => "#{retrieve_client_id(shipment)}",
- 'number_shipment_package' => "#{retrieve_number_shipment_package(shipment)}",
- 'last_name_first_name' => "#{retrieve_last_name_first_name(shipment)}",
- 'address' => "#{retrieve_address(shipment)}",
- 'zipcode' => "#{retrieve_zipcode(shipment)}",
- "city" => "#{retrieve_city(shipment).to_s}",
- 'country_code' => "#{retrieve_country_code(shipment)}",
- 'phone' => "#{retrieve_phone(shipment)}"
- }
- ##############
- # END EDIT #
- ##############
- return fields
- end
- #################
- # FIELDS GETTER #
- #################
- # Retrieve shipment method
- # Saturday (S) or week (C)
- def retrieve_shipment_saturday_or_week(shipment)
- end
- # Retrieve shipement weight
- def retrieve_shipment_weight(shipment)
- return '0.1'
- end
- # Retrieve client ID
- def retrieve_client_id(shipment)
- return shipment.order.user.id
- end
- # Number of shipment package
- def retrieve_number_shipment_package(shipment)
- return shipment.order.id
- end
- # Concate Last_name and First_name
- def retrieve_last_name_first_name(shipment)
- # Instanciate variable
- result = nil
- # Retrieve variable lastname and firstname
- lastname = shipment.order.ship_address.lastname
- firstname = shipment.order.ship_address.firstname
- # Return variable
- result = lastname + ' ' + firstname
- return result
- end
- # Retrieve User Address
- def retrieve_address(shipment)
- # Instanciate variable
- result = nil
- # Retrieve variable lastname and firstname
- address_line1 = shipment.order.ship_address.address1
- address_line2 = shipment.order.ship_address.address2
- # Return variable
- result = address_line1 + ' ' + address_line2
- return result
- end
- # Retrieve Zipcode
- def retrieve_zipcode(shipment)
- return shipment.order.ship_address.zipcode
- end
- # Retrieve Town
- def retrieve_city(shipment)
- return shipment.order.ship_address.city
- end
- # Retrieve country code
- def retrieve_country_code(shipment)
- return shipment.order.ship_address.country.iso
- end
- # Retrieve customer phone number
- def retrieve_phone(shipment)
- return shipment.order.ship_address.phone
- end
- end
Add Comment
Please, Sign In to add comment