Guest User

Untitled

a guest
Jun 21st, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.20 KB | None | 0 0
  1. class Admin::SpreeTrackingTntController < ApplicationController
  2. require_role "admin" # You might want to remove this, and add security in the /config/easy_role_permissions.yml file
  3. layout 'admin'
  4.  
  5. # Generate TNT File for shipment id
  6. def generate
  7. # Retrieve shipment object
  8. tmp_shipment = Shipment.find(params[:id])
  9.  
  10. # Retrieve text concate and store into a variable
  11. tnt_csv_string = process_fields(tmp_shipment)
  12. end
  13.  
  14. ###################
  15. # Private Section #
  16. ###################
  17.  
  18. private
  19.  
  20. # Concate Fields into a single text variable
  21. # Via XML Pattern Configuration File
  22. # shipment : represent shipment object
  23. def process_fields(shipment)
  24. # Return variable
  25. result = ''
  26.  
  27. # Retrieve fields for this shipment
  28. hash_fields = define_hash_fields(shipment)
  29.  
  30. p "#{hash_fields}"
  31.  
  32. # Defile path of xml configuration
  33. xml_file = "#{SPREE_ROOT}/vendor/extensions/spree_tracking_tnt/config/spree_tracking_tnt_pattern.xml"
  34.  
  35. # If the configuration file is present
  36. if File.exist?(xml_file)
  37. # Read XML File
  38. xml = File.read(xml_file)
  39.  
  40. # Parse XML File
  41. doc = Hpricot::XML(xml)
  42.  
  43. # Process XML File
  44. (doc/:spree_tracking_tnt).each do |root|
  45. (root/:field).each_with_index do |field, ind|
  46. # Retrieve processing field
  47. processing_field = process_field(field, hash_fields)
  48. message = ''
  49. message = "'#{processing_field}' : #{processing_field.length}" unless processing_field.nil?
  50. p "FIELD #{ind} : #{message}"
  51. # Concate to the result
  52. result += processing_field unless processing_field.nil?
  53. end
  54. end
  55. end
  56.  
  57. p "#{result}"
  58.  
  59. end
  60.  
  61. #########################
  62. # GENERAL FIELDS GETTER #
  63. #########################
  64.  
  65. # Process field
  66. def process_field(field, hash_fields)
  67. # Instanciate variable
  68. result = ''
  69.  
  70. p "PROCESS FIELD #{hash_fields}"
  71.  
  72. # Retrieve name field
  73. name = field.at("/name").inner_html.to_s unless field.at("/name").nil?
  74.  
  75. # 2 kind of field
  76. # PADDING with the only value property and (e.g. '-') and length properties
  77. # Field including name and (perhaps) value / min-length - max-length / padding property
  78. if name.nil?
  79. # First Case
  80. # Padding of VALUE while LENGTH times
  81. result = process_field_padding(field)
  82.  
  83. p "PROCESS WITHOUT NAME"
  84. else
  85. # Second Case
  86. # Including some property
  87.  
  88. # Instanciate value
  89. value = field.at("/value").inner_html.to_s unless field.at("/value").nil?
  90.  
  91. # If there aren t value
  92. if value.nil?
  93. # Retrieve Hash fields
  94. value = hash_fields[:city]
  95. end
  96.  
  97. p "RETRIEVES NAME : #{name}"
  98. p "RETRIEVES VALUEE : #{value}"
  99. p "RETRIEVES MANUAL VALUE : #{hash_fields['city']}"
  100.  
  101. # AT THIS POINT, WE HAVE STORE "VALUE" VALUE
  102.  
  103. # NEXT STEP CHECK IF THERE ARE A CONSTRAINT PADDING PROPERTY
  104.  
  105. # Boolean test
  106. value_min_length = field.at("min-length").inner_html.to_s unless field.at("min-length").nil?
  107. value_max_length = field.at("max-length").inner_html.to_s unless field.at("max-length").nil?
  108.  
  109. # If the value is constraint by a length condition
  110. unless value_min_length.nil? || value_max_length.nil?
  111. p "PLOOOOp"
  112.  
  113. # Define constraint part
  114. constraint_part = (field/:pattern)
  115.  
  116. # Unless value_min is nil?
  117. unless value_min_length.nil?
  118. # Kind of condition => MIN-LENGTH => PADDING MODE
  119. kind = 'min'
  120.  
  121. # Define length condition
  122. length_condition = value_min_length
  123. else
  124. # Kind of condition => MAX-LENGTH => TRUNCATE MODE
  125. kind = 'max'
  126.  
  127. # Define length condition
  128. length_condition = value_max_length
  129. end
  130.  
  131. # Process constraint on the value
  132. result = process_constraint_length_condition(value,length_condition,constraint_part, kind)
  133. else
  134. # No Length condition
  135. # Define result equal to value
  136. result = value
  137. end
  138. end
  139.  
  140. # Return value for this field
  141. return result
  142. end
  143.  
  144. # Process a constraint field
  145. # value : Applying constraint value
  146. # length_condition : number of (MIN|MAX) length for "value"
  147. # constraint : Constraint XML Node
  148. # kind : max or min
  149. def process_constraint_length_condition(value,length_condition, constraint, kind)
  150. # Instanciate variable
  151. result = ''
  152.  
  153. # Display message
  154. p "#{value} : #{length_condition} : #{kind}"
  155.  
  156. # If Constraint is present
  157. unless constraint.nil?
  158. # Constraint is to perfrom a padding (left or right) with a character or truncate (left or right) the value
  159. value_constraint_character = constraint.at("value").inner_html.to_s
  160. position_pattern = constraint.at("position").inner_html.to_s
  161. end
  162.  
  163. # If kind is max
  164. if kind.eql?('max')
  165. # If the length condition is not respected
  166. if value.length > length_condition
  167. # Truncate the value (left or right)
  168. result = process_max_condition_on_value(value,length_condition, position_pattern)
  169. else
  170. # Condition MAX is respected
  171. result = value
  172. end
  173. else
  174. # Kind is Min
  175. #
  176. # MIN is different than MAX
  177. # MIN Check the minimum character
  178. # If the length condition is not respected
  179. if value.length < length_condition
  180. result = process_min_condition_on_value(value,length_condition, value_constraint_character, position_pattern)
  181. else
  182. # Condition MIN is respected
  183. result = value
  184. end
  185. end
  186.  
  187. # Return
  188. return result
  189. end
  190.  
  191. # Process MIN condition on value
  192. def process_min_condition_on_value(value, min_length, padding_character, position_pattern)
  193. # Instanciate variable
  194. result = ''
  195. required_character = 0
  196.  
  197. p "MIN CONDITION #{value} : #{min_length}"
  198.  
  199. # Count the number of times needed to pass the condition
  200. required_character = min_length - value.length
  201.  
  202. # If they don t have position_padding
  203. if position_pattern.nil?
  204. # Use default position_padding
  205. position_pattern = "right"
  206. end
  207.  
  208. # Perform truncate (LEFT or RIGHT)
  209. case position_pattern
  210. when "left" then required_character.times { result =+ padding_character }
  211. when "right" then required_character.times { result += padding_character }
  212. end
  213.  
  214. # Return result
  215. return result
  216. end
  217.  
  218. # Process MAX condition on value
  219. def process_max_condition_on_value(value, max_length, position_pattern)
  220. # Instanciate variable
  221. result = ''
  222.  
  223. # If they don t have position_padding
  224. if position_pattern.nil?
  225. # Use default position_padding
  226. position_pattern = "right"
  227. end
  228.  
  229. # Perform truncate (LEFT or RIGHT)
  230. case position_pattern
  231. when "left" then result = value[max_length, (value.length-1)]
  232. when "right" then result = value[0, max_length]
  233. end
  234.  
  235. # Return result
  236. return result
  237. end
  238.  
  239. # Process a padding field
  240. def process_field_padding(field)
  241. # Instanciate variable
  242. result = ''
  243.  
  244. # Retrieve required variable
  245. max_i = field.at("length").inner_html.to_i
  246. value = field.at("value").inner_html.to_s
  247.  
  248. max_i.times { result += value}
  249.  
  250. return result
  251. end
  252.  
  253. # Define fields for a shipment
  254. # shipment : Represent the shipment object
  255. def define_hash_fields(shipment)
  256. # Instanciate Hash
  257. fields = Hash.new
  258.  
  259. ##################
  260. # EDIT THIS HASH #
  261. ##################
  262.  
  263. # Initialize fields Hash
  264. # Each key of Fields Hash MUST match XML Fields Value
  265. # Link Each Key Field with a function to retrieve value from ActiveRecord
  266. fields = {
  267. "shipment_saturday_or_week" => "#{retrieve_shipment_saturday_or_week(shipment)}",
  268. "shipment_weight" => "#{retrieve_shipment_weight(shipment)}",
  269. 'client_id' => "#{retrieve_client_id(shipment)}",
  270. 'number_shipment_package' => "#{retrieve_number_shipment_package(shipment)}",
  271. 'last_name_first_name' => "#{retrieve_last_name_first_name(shipment)}",
  272. 'address' => "#{retrieve_address(shipment)}",
  273. 'zipcode' => "#{retrieve_zipcode(shipment)}",
  274. "city" => "#{retrieve_city(shipment).to_s}",
  275. 'country_code' => "#{retrieve_country_code(shipment)}",
  276. 'phone' => "#{retrieve_phone(shipment)}"
  277. }
  278.  
  279. ##############
  280. # END EDIT #
  281. ##############
  282.  
  283. return fields
  284. end
  285.  
  286. #################
  287. # FIELDS GETTER #
  288. #################
  289.  
  290. # Retrieve shipment method
  291. # Saturday (S) or week (C)
  292. def retrieve_shipment_saturday_or_week(shipment)
  293.  
  294. end
  295.  
  296. # Retrieve shipement weight
  297. def retrieve_shipment_weight(shipment)
  298. return '0.1'
  299. end
  300.  
  301. # Retrieve client ID
  302. def retrieve_client_id(shipment)
  303. return shipment.order.user.id
  304. end
  305.  
  306. # Number of shipment package
  307. def retrieve_number_shipment_package(shipment)
  308. return shipment.order.id
  309. end
  310.  
  311. # Concate Last_name and First_name
  312. def retrieve_last_name_first_name(shipment)
  313. # Instanciate variable
  314. result = nil
  315.  
  316. # Retrieve variable lastname and firstname
  317. lastname = shipment.order.ship_address.lastname
  318. firstname = shipment.order.ship_address.firstname
  319.  
  320. # Return variable
  321. result = lastname + ' ' + firstname
  322.  
  323. return result
  324. end
  325.  
  326. # Retrieve User Address
  327. def retrieve_address(shipment)
  328. # Instanciate variable
  329. result = nil
  330.  
  331. # Retrieve variable lastname and firstname
  332. address_line1 = shipment.order.ship_address.address1
  333. address_line2 = shipment.order.ship_address.address2
  334.  
  335. # Return variable
  336. result = address_line1 + ' ' + address_line2
  337.  
  338. return result
  339. end
  340.  
  341. # Retrieve Zipcode
  342. def retrieve_zipcode(shipment)
  343. return shipment.order.ship_address.zipcode
  344. end
  345.  
  346. # Retrieve Town
  347. def retrieve_city(shipment)
  348. return shipment.order.ship_address.city
  349. end
  350.  
  351. # Retrieve country code
  352. def retrieve_country_code(shipment)
  353. return shipment.order.ship_address.country.iso
  354. end
  355.  
  356. # Retrieve customer phone number
  357. def retrieve_phone(shipment)
  358. return shipment.order.ship_address.phone
  359. end
  360. end
Add Comment
Please, Sign In to add comment