Class: Deliveries::Couriers::CorreosExpress::Pickups::Create::FormatParams

Inherits:
Object
  • Object
show all
Defined in:
lib/deliveries/couriers/correos_express/pickups/create/format_params.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sender:, receiver:, parcels:, reference_code:, pickup_date:, remarks:, time_interval: nil) ⇒ FormatParams

Returns a new instance of FormatParams.



10
11
12
13
14
15
16
17
18
19
# File 'lib/deliveries/couriers/correos_express/pickups/create/format_params.rb', line 10

def initialize(sender:, receiver:, parcels:, reference_code:,
               pickup_date:, remarks:, time_interval: nil)
  self.sender = sender
  self.receiver = receiver
  self.parcels = parcels
  self.reference_code = reference_code
  self.pickup_date = pickup_date
  self.remarks = remarks
  self.time_interval = time_interval
end

Instance Attribute Details

#parcelsObject

Returns the value of attribute parcels.



7
8
9
# File 'lib/deliveries/couriers/correos_express/pickups/create/format_params.rb', line 7

def parcels
  @parcels
end

#pickup_dateObject

Returns the value of attribute pickup_date.



7
8
9
# File 'lib/deliveries/couriers/correos_express/pickups/create/format_params.rb', line 7

def pickup_date
  @pickup_date
end

#receiverObject

Returns the value of attribute receiver.



7
8
9
# File 'lib/deliveries/couriers/correos_express/pickups/create/format_params.rb', line 7

def receiver
  @receiver
end

#reference_codeObject

Returns the value of attribute reference_code.



7
8
9
# File 'lib/deliveries/couriers/correos_express/pickups/create/format_params.rb', line 7

def reference_code
  @reference_code
end

#remarksObject

Returns the value of attribute remarks.



7
8
9
# File 'lib/deliveries/couriers/correos_express/pickups/create/format_params.rb', line 7

def remarks
  @remarks
end

#senderObject

Returns the value of attribute sender.



7
8
9
# File 'lib/deliveries/couriers/correos_express/pickups/create/format_params.rb', line 7

def sender
  @sender
end

#time_intervalObject

Returns the value of attribute time_interval.



7
8
9
# File 'lib/deliveries/couriers/correos_express/pickups/create/format_params.rb', line 7

def time_interval
  @time_interval
end

Instance Method Details

#executeObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/deliveries/couriers/correos_express/pickups/create/format_params.rb', line 21

def execute
  postcode = format_postcode(sender.postcode, sender.country)
  params = {
    solicitante: CorreosExpress.config(:client_code),
    refRecogida: reference_code,
    fechaRecogida: pickup_date&.strftime('%d%m%Y') || '',
    clienteRecogida: receiver.address_id || CorreosExpress.config(:pickup_receiver_code),
    codRemit: sender.address_id || '',
    nomRemit: sender.name,
    nifRemit: '',
    dirRecog: sender.street,
    poblRecog: sender.city,
    cpRecog: postcode,
    contRecog: sender.name,
    tlfnoRecog: sender.phone,
    emailRecog: sender.email,
    codDest: receiver.address_id || CorreosExpress.config(:pickup_receiver_code),
    nomDest: receiver.name,
    dirDest: receiver.street,
    pobDest: receiver.city,
    cpDest: receiver.postcode,
    paisDest: receiver.country,
    contactoDest: receiver.name,
    tlfnoDest: receiver.phone,
    emailDest: receiver.email,
    bultos: parcels.to_s
  }

  unless CorreosExpress.test?
    custom_product = CorreosExpress.config("countries.#{sender.country.to_s.downcase}.product")
    params[:producto] = custom_product if custom_product
  end

  defaults = Defaults::PARAMS

  defaults = defaults.merge(params)

  if time_interval
    defaults[:horaDesde1] = format '%02d:00', time_interval.first
    defaults[:horaHasta1] = format '%02d:00', time_interval.last
  else
    # Try to set cutoff time for the sender postal code.
    begin
      cutoff_time = CutoffTime.new(country: sender.country, postcode: postcode).execute
      # Set only when cuttoff time is less than 19:00 (the default cutoff time in correos express)
      if cutoff_time.to_i < 19
        defaults[:horaHasta1] = cutoff_time

        # Update start hour if the period if smaller than 2 hours
        min_start_hour = cutoff_time.to_i - 2
        defaults[:horaDesde1] = format('%02d:00', min_start_hour) if defaults[:horaDesde1].to_i > min_start_hour
      end
    rescue Deliveries::Error => e
      Deliveries.logger&.error "Cannot obtain cutoff time: #{e.message}"
    end
  end

  defaults.to_json
end