Class: Logistics::Core::CarCarrierRate

Inherits:
ApplicationRecord show all
Defined in:
app/models/logistics/core/car_carrier_rate.rb

Class Method Summary collapse

Methods inherited from ApplicationRecord

as_json

Class Method Details

.check_for_existing_rate(current_car_carrier_rate, new_car_carrier_rate) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'app/models/logistics/core/car_carrier_rate.rb', line 35

def self.check_for_existing_rate current_car_carrier_rate, new_car_carrier_rate
  car_carrier_rate = current_car_carrier_rate.where(new_car_carrier_rate)

  if car_carrier_rate.count > 0
    return true
  else
    return false
  end
end

.fetch_all(car_carrier_rates_all, start, limit) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/logistics/core/car_carrier_rate.rb', line 13

def self.fetch_all car_carrier_rates_all, start, limit
  result = []
  car_carrier_rates = car_carrier_rates_all.offset(start).limit(limit).order("transporter_id, route_id, break_bulk_unit_id")
  if car_carrier_rates.length > 0
    car_carrier_rates.each do |car_carrier_rate|
      result.push({id: car_carrier_rate.id,
                   route_id: car_carrier_rate.route_id,
                   route_name: car_carrier_rate.route.route_name,
                   break_bulk_unit_id: car_carrier_rate.break_bulk_unit_id,
                   break_bulk_unit_type: car_carrier_rate.break_bulk_unit.name,
                   rate: car_carrier_rate.rate,
                   margin: car_carrier_rate.margin,
                   transporter_id: car_carrier_rate.transporter_id,
                   transporter_name: car_carrier_rate.transporter.name,
                   transport_rate_period_id: car_carrier_rate.transport_rate_period_id,
                   effective_date: car_carrier_rate.transport_rate_period.effective_date,
                   valid_until: car_carrier_rate.transport_rate_period.valid_until })
    end
  end
  return result
end

.generate_car_carrier_rate(route_type) ⇒ Object



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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/logistics/core/car_carrier_rate.rb', line 45

def self.generate_car_carrier_rate route_type
  message = ''
  count = 0
  routes = Route.where(route_type: route_type)
  break_bulk_units = BreakBulkUnit.all
  transporters = Transporter.where.not(name: 'Reference Transporter')
  rate_period = TransportRatePeriod.where(current_period: true)
  if route_type == 'Inter City'
    rate_type = 'ICCarrier'
  else
    rate_type = 'WCCarrier'
  end
  new_car_carrier_rate_arr = []
  current_car_carrier_rate = CarCarrierRate.where rate_type: rate_type
  if transporters.length > 0 && rate_period.length > 0
    transporters.each do |transporter|
      if routes.length > 0
        routes.each do |route|
          if break_bulk_units.length > 0
            break_bulk_units.each do |break_bulk_unit|
              new_car_carrier_rate = { transporter_id: transporter.id, route_id: route.id,
                                       break_bulk_unit_id: break_bulk_unit.id, transport_rate_period_id: rate_period[0].id,
                                       rate_type: rate_type }
              check_existing_rate = check_for_existing_rate current_car_carrier_rate, new_car_carrier_rate
              if !check_existing_rate
                new_car_carrier_rate = { transporter_id: transporter.id, route_id: route.id,
                                         break_bulk_unit_id: break_bulk_unit.id, transport_rate_period_id: rate_period[0].id,
                                         rate_type: rate_type, rate: 0, margin: route.margin }
                new_car_carrier_rate_arr << new_car_carrier_rate
              else
                count += 1
              end
            end
          else
            message = "Break Bulk Unit Types"
            break
          end
        end
      else
        message = route_type + " Transport Route"
        break
      end
    end
    if new_car_carrier_rate_arr.count > 0
      CarCarrierRate.create(new_car_carrier_rate_arr)
      message = route_type + " Car Carrier Rate Generated Successfully!"
    end
  elsif rate_period.length == 0
    message = "Transport Rate Period"
  else
    message = "Transporter"
  end
  if message == route_type + " Car Carrier Rate Generated Successfully!" || count > 0
    message2 = ["A total of <b>" + count.to_s + "</b> Records have been skipped!"]
    return {ind: 1, value: "", message: message, errors: message2}
  else
    error = ["No Record Found for <b>" + message + "</b>!"]
    return {ind: 0, value: "", message: error}
  end
end