Advertisement
NameL3ss

service exporter unassigned

Aug 10th, 2021
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. # frozen_string_literal: true
  2.  
  3. module Planner
  4. module Export
  5. module Excelx
  6. class UnassignedStopExporter
  7. attr_accessor :sheet
  8. attr_reader :object, :language
  9.  
  10. def initialize(params)
  11. @object = params[:object]
  12. @sheet = params[:sheet]
  13. @language = params[:language]
  14. end
  15.  
  16. def export
  17. data = []
  18. return unless object
  19.  
  20. stop_tags = tag_service
  21. stop_tags.tag_headers
  22. sheet.add_row(headers(language, tag_service), types: :string) if sheet.present?
  23. unassigned_stops.each do |stop|
  24. row = []
  25. stop_delivery_times = ::Exporter::CalculateDeliveryTimeService.new(stop)
  26. stop_hours = stop_delivery_times.build_hours
  27. stop.items.each do |item|
  28. row += build_stop_coordinates(stop)
  29. row += build_item_information(item)
  30. row += build_delivery_times(stop_delivery_times)
  31. row += build_stop_hours(stop_hours)
  32. row += build_item_costs(item)
  33. row += build_stop_information(stop)
  34. row += stop_tags.build_tag_row(stop)
  35. end
  36.  
  37. if sheet.present?
  38. sheet.add_row(row.map(&:to_s), types: :string)
  39. else
  40. data << row
  41. end
  42. end
  43.  
  44. data
  45. end
  46.  
  47. private
  48.  
  49. def build_stop_coordinates(stop)
  50. [
  51. stop.identification,
  52. stop.latitude,
  53. stop.longitude,
  54. stop.address
  55. ]
  56. end
  57.  
  58. def build_item_information(item)
  59. [
  60. item.name,
  61. item.quantity,
  62. item.identification
  63. ]
  64. end
  65.  
  66. def build_delivery_times(stop_delivery_times)
  67. [
  68. stop_delivery_times.min_delivery_time,
  69. stop_delivery_times.max_delivery_time
  70. ]
  71. end
  72.  
  73. def build_stop_hours(stop_hours)
  74. [
  75. stop_hours.min_delivery_time_one.to_s,
  76. stop_hours.max_delivery_time_one.to_s,
  77. stop_hours.min_delivery_time_two.to_s,
  78. stop_hours.max_delivery_time_two.to_s
  79. ]
  80. end
  81.  
  82. def build_item_costs(item)
  83. [
  84. item.format_cost,
  85. item.format_capacity_one,
  86. item.format_capacity_two
  87. ]
  88. end
  89.  
  90. def build_stop_information(stop)
  91. [
  92. stop.service_time,
  93. stop.priority,
  94. stop.contact_identification,
  95. stop.contact_name,
  96. stop.contact_phone,
  97. stop.contact_email,
  98. stop.place_name
  99. ]
  100. end
  101.  
  102. def unassigned_stops
  103. Stop.unassigned_stops(plan_solution: object)
  104. end
  105.  
  106. def tag_service
  107. ::Exporter::TagService.new(entity_ids: unassigned_stops.pluck(:id), entity_type: :stop)
  108. end
  109.  
  110. def headers(language, tag_service)
  111. default_headers = ::Importer::HeaderProvider.new(:log, language).execute.map {|_key, value| value}
  112. (default_headers + tag_service.tag_headers).map(&:upcase)
  113. end
  114. end
  115. end
  116. end
  117. end
  118.  
  119.  
  120. # frozen_string_literal: true
  121.  
  122. module Planner
  123. module Export
  124. module Excelx
  125. class UnassignedStopAction
  126. def export(context)
  127. return unless context
  128.  
  129. language = context.object.account_language
  130. context.package.workbook.add_worksheet(name: I18n.t('planner.exporter.unassigned_stops_sheet')) do |sheet|
  131. ::Planner::Export::Excelx::UnassignedStopExporter.new(object: context.try(:object),
  132. sheet: sheet,
  133. language: language).export
  134. end
  135.  
  136. context.package.serialize("#{context.temp_folder}/#{context.file_name}")
  137. end
  138. end
  139. end
  140. end
  141. end
  142.  
  143. ::Planner::Export::Excelx::UnassignedStopExporter.new(object: ::Planner::PlanSolution.find(115), language: "es").export
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement