Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # frozen_string_literal: true
- module Planner
- module Export
- module Excelx
- class UnassignedStopExporter
- attr_accessor :sheet
- attr_reader :object, :language
- def initialize(params)
- @object = params[:object]
- @sheet = params[:sheet]
- @language = params[:language]
- end
- def export
- data = []
- return unless object
- stop_tags = tag_service
- stop_tags.tag_headers
- sheet.add_row(headers(language, tag_service), types: :string) if sheet.present?
- unassigned_stops.each do |stop|
- row = []
- stop_delivery_times = ::Exporter::CalculateDeliveryTimeService.new(stop)
- stop_hours = stop_delivery_times.build_hours
- stop.items.each do |item|
- row += build_stop_coordinates(stop)
- row += build_item_information(item)
- row += build_delivery_times(stop_delivery_times)
- row += build_stop_hours(stop_hours)
- row += build_item_costs(item)
- row += build_stop_information(stop)
- row += stop_tags.build_tag_row(stop)
- end
- if sheet.present?
- sheet.add_row(row.map(&:to_s), types: :string)
- else
- data << row
- end
- end
- data
- end
- private
- def build_stop_coordinates(stop)
- [
- stop.identification,
- stop.latitude,
- stop.longitude,
- stop.address
- ]
- end
- def build_item_information(item)
- [
- item.name,
- item.quantity,
- item.identification
- ]
- end
- def build_delivery_times(stop_delivery_times)
- [
- stop_delivery_times.min_delivery_time,
- stop_delivery_times.max_delivery_time
- ]
- end
- def build_stop_hours(stop_hours)
- [
- stop_hours.min_delivery_time_one.to_s,
- stop_hours.max_delivery_time_one.to_s,
- stop_hours.min_delivery_time_two.to_s,
- stop_hours.max_delivery_time_two.to_s
- ]
- end
- def build_item_costs(item)
- [
- item.format_cost,
- item.format_capacity_one,
- item.format_capacity_two
- ]
- end
- def build_stop_information(stop)
- [
- stop.service_time,
- stop.priority,
- stop.contact_identification,
- stop.contact_name,
- stop.contact_phone,
- stop.contact_email,
- stop.place_name
- ]
- end
- def unassigned_stops
- Stop.unassigned_stops(plan_solution: object)
- end
- def tag_service
- ::Exporter::TagService.new(entity_ids: unassigned_stops.pluck(:id), entity_type: :stop)
- end
- def headers(language, tag_service)
- default_headers = ::Importer::HeaderProvider.new(:log, language).execute.map {|_key, value| value}
- (default_headers + tag_service.tag_headers).map(&:upcase)
- end
- end
- end
- end
- end
- # frozen_string_literal: true
- module Planner
- module Export
- module Excelx
- class UnassignedStopAction
- def export(context)
- return unless context
- language = context.object.account_language
- context.package.workbook.add_worksheet(name: I18n.t('planner.exporter.unassigned_stops_sheet')) do |sheet|
- ::Planner::Export::Excelx::UnassignedStopExporter.new(object: context.try(:object),
- sheet: sheet,
- language: language).export
- end
- context.package.serialize("#{context.temp_folder}/#{context.file_name}")
- end
- end
- end
- end
- end
- ::Planner::Export::Excelx::UnassignedStopExporter.new(object: ::Planner::PlanSolution.find(115), language: "es").export
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement