Class: Logistics::Core::SelfDrivingRatesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/logistics/core/self_driving_rates_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /self_driving_rates POST /self_driving_rates.json



202
203
204
205
206
207
208
209
210
211
212
# File 'app/controllers/logistics/core/self_driving_rates_controller.rb', line 202

def create
  route_type = params[:route_type]
  generated = SelfDrivingRate.generate_self_driving_rate route_type
  if generated[:ind] == 1
    response = Mks::Common::MethodResponse.new(true, generated[:message], nil, generated[:errors], nil)
  else
    errors = generated[:message]
    response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil)
  end
  render json: response
end

#create_min_max_data(min_max_rate) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'app/controllers/logistics/core/self_driving_rates_controller.rb', line 186

def create_min_max_data min_max_rate
  data = {
      route_id: min_max_rate.route_id,
      route_name: min_max_rate.route.route_name,
      break_bulk_unit_id: min_max_rate.break_bulk_unit_id,
      break_bulk_unit_type: min_max_rate.break_bulk_unit.name,
      transporter_id: min_max_rate.transporter_id,
      transporter_name: min_max_rate.transporter.name,
      rate: min_max_rate.rate,
      margin: min_max_rate.route.margin
  }
  return data
end

#create_working_rateObject



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'app/controllers/logistics/core/self_driving_rates_controller.rb', line 214

def create_working_rate
  self_driving_rates = params[:self_driving_rate]
  rate_type = params[:rate_type]
  route_id = self_driving_rates[0][:route_id]
  new_self_driving_rate_arr = []
  errors = []
  rate_period = TransportRatePeriod.where(current_period: true)
  default_transporter = Transporter.where(name: "Reference Transporter")
  if default_transporter.length > 0 && rate_period.length > 0
    default_transporter_id = default_transporter[0].id
    rate_period_id = rate_period[0].id
    self_driving_rates_all = SelfDrivingRate.where( route_id: route_id,
                                                    rate_type: rate_type,
                                                    rate_period_id: rate_period_id)
    if self_driving_rates_all.length > 0
      self_driving_rates.each do |self_driving_rate|
        route_id = self_driving_rate[:route_id]
        transporter_id = self_driving_rate[:transporter_id]
        rate_type = self_driving_rate[:rate_type]
        margin = self_driving_rate[:margin]
        break_bulk_unit_id = self_driving_rate[:break_bulk_unit_id]
        selected = self_driving_rates_all.where({ transporter_id: transporter_id,
                                                  break_bulk_unit_id: break_bulk_unit_id })
        new_self_driving_rate = { route_id: route_id,
                                  transporter_id: default_transporter_id,
                                  break_bulk_unit_id: break_bulk_unit_id,
                                  rate_type: rate_type,
                                  selected_transporter_id: transporter_id}
        check_existing = SelfDrivingRate.check_for_existing_rate self_driving_rates_all, new_self_driving_rate
        if !check_existing
          rate = selected[0].rate + (selected[0].rate * margin/100)
          new_self_driving_rate = { route_id: route_id,
                                    transporter_id: default_transporter_id,
                                    break_bulk_unit_id: break_bulk_unit_id,
                                    rate_period_id: rate_period_id,
                                    rate_type: rate_type,
                                    selected_transporter_id: transporter_id,
                                    rate: rate,
                                    margin: margin }
          new_self_driving_rate_arr << new_self_driving_rate
        else
          errors << "Maccfa Rate for Break Bulk Unit type <b>" + self_driving_rate[:break_bulk_unit_type] + "</b> already exist. So it is skipped!"
        end
      end
      if new_self_driving_rate_arr.length > 0
        SelfDrivingRate.create! new_self_driving_rate_arr
        response = Mks::Common::MethodResponse.new(true, "Maccfa Self Driving Rate information saved successfully!", nil, errors, nil)
      else
        response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil)
      end
    else
      errors << ["Error on saving <b>" + rate_type + "</b> Self Driving Working Rate. No Rate record found!"]
      response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil)
    end
  else
    errors << ["Error on saving <b>" + rate_type + "</b> Self Driving Working Rate!"]
    response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil)
  end
  render json: response
end

#get_all_fields(self_driving_rates) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/logistics/core/self_driving_rates_controller.rb', line 27

def get_all_fields self_driving_rates
  result = []
  self_driving_rates.all.each do |self_driving_rate|
    result.push({id: self_driving_rate.id,
                 route_id: self_driving_rate.route_id,
                 route_name: self_driving_rate.route.route_name,
                 break_bulk_unit_id: self_driving_rate.break_bulk_unit_id,
                 break_bulk_unit_type: self_driving_rate.break_bulk_unit.name,
                 rate: self_driving_rate.rate,
                 margin: self_driving_rate.margin,
                 transporter_id: self_driving_rate.transporter_id,
                 transporter_name: self_driving_rate.transporter.name,
                 rate_period_id: self_driving_rate.rate_period_id,
                 effective_date: self_driving_rate.rate_period.effective_date,
                 valid_until: self_driving_rate.rate_period.valid_until })
  end
  result
end

#get_min_max_transporter_ratesObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/controllers/logistics/core/self_driving_rates_controller.rb', line 95

def get_min_max_transporter_rates
  route_id = params[:route_id]
  rate_type = params[:rate_type]
  data_min = []
  data_max = []
  result = []
  default_transporter = Transporter.where(name: "Reference Transporter")
  rate_period = TransportRatePeriod.where(current_period: true)
  if default_transporter.length > 0 && rate_period.length > 0
    default_transporter_id = default_transporter[0].id
    rate_period_id = rate_period[0].id
    self_driving_rates = SelfDrivingRate.where("route_id = ? and rate_type = ? and rate_period_id = ? and rate > 0",
                                               "#{route_id}",
                                               "#{rate_type}",
                                               "#{rate_period_id}")
    current_self_driving_rates = self_driving_rates.where(transporter_id: default_transporter_id)
    break_bulk_unit_ids = []
    current_self_driving_rates.each do |bb_rate|
      break_bulk_unit_ids.push(bb_rate[:break_bulk_unit_id])
    end
    self_driving_rates = self_driving_rates.where.not(transporter_id: default_transporter_id,
                                                      break_bulk_unit_id: break_bulk_unit_ids)

    self_driving_rate_uniques = self_driving_rates.select(:break_bulk_unit_id).distinct
    if self_driving_rate_uniques.length > 0
      self_driving_rate_uniques.each do |self_driving_rate|
        break_bulk_unit_id = self_driving_rate.break_bulk_unit_id

        rate = self_driving_rates.where({ break_bulk_unit_id: break_bulk_unit_id })
        rate = rate.order("rate ASC")

        if rate.length > 0
          data_min.push(create_min_max_data rate.first)
          data_max.push(create_min_max_data rate.last)
        end
      end
    end
    result.push({min_rate: data_min, max_rate: data_max})
  end
  response = Mks::Common::MethodResponse.new(true, nil, result, nil, nil)
  render json: response
end

#get_self_driving_ratesObject



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
# File 'app/controllers/logistics/core/self_driving_rates_controller.rb', line 54

def get_self_driving_rates
  start = params[:start]
  limit = params[:limit]
  rate_type = params[:rate_type]
  result = []
  transporter = Transporter.where(name: "Reference Transporter")
  transport_rate_period = TransportRatePeriod.where(current_period: true)
  if transporter.length > 0 && transport_rate_period.length > 0
    transporter_id = transporter[0].id
    transport_rate_period_id = transport_rate_period[0].id
    self_driving_rates_all = SelfDrivingRate.where(rate_type: rate_type,
                                                   transporter_id: transporter_id,
                                                   rate_period_id: transport_rate_period_id)
    count = self_driving_rates_all.count
    self_driving_rates = self_driving_rates_all.offset(start).limit(limit).order("transporter_id, route_id, break_bulk_unit_id")
    if rate_type == "ICDriving"
      rate_type_type = "IC Driving"
    else
      rate_type_type = "WC Driving"
    end
    self_driving_rates.each do |self_driving_rate|
      selected_transporter = Transporter.find(self_driving_rate.selected_transporter_id)
      result.push({id: self_driving_rate.id,
                   route_id: self_driving_rate.route_id,
                   route_name: self_driving_rate.route.route_name,
                   break_bulk_unit_id: self_driving_rate.break_bulk_unit_id,
                   break_bulk_unit_type: self_driving_rate.break_bulk_unit.name,
                   rate: self_driving_rate.rate,
                   margin: self_driving_rate.margin,
                   transporter_id: self_driving_rate.selected_transporter_id,
                   transporter_name: selected_transporter.name,
                   rate_period_id: self_driving_rate.rate_period_id,
                   effective_date: self_driving_rate.rate_period.effective_date,
                   valid_until: self_driving_rate.rate_period.valid_until,
                   rate_type: rate_type_type})
    end
  end
  response = Mks::Common::MethodResponse.new(true, nil, result, nil, count)
  render json: response
end

#get_self_driving_routesObject



46
47
48
49
50
51
52
# File 'app/controllers/logistics/core/self_driving_rates_controller.rb', line 46

def get_self_driving_routes
  rate_type = params[:rate_type]
  self_driving_rates = SelfDrivingRate.where(rate_type: rate_type).select("DISTINCT ON (route_id) *")
  result = get_all_fields self_driving_rates
  response = Mks::Common::MethodResponse.new(true, nil, result, nil, nil)
  render json: response
end

#indexObject

GET /self_driving_rates GET /self_driving_rates.json



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/logistics/core/self_driving_rates_controller.rb', line 11

def index
  start = params[:start]
  limit = params[:limit]
  route_type = params[:route_type]
  transporter_id = Transporter.select("id").where("name" => "Reference Transporter").ids.first
  current_period_id = TransportRatePeriod.where(current_period: true).ids.first
  routes = Route.where(route_type: route_type)
  self_driving_rates_all = SelfDrivingRate.where(route_id: routes.ids,
                                                 rate_period_id: current_period_id).where.not("transporter_id" => transporter_id)
  count = self_driving_rates_all.count

  self_driving_rates = SelfDrivingRate.fetch_all self_driving_rates_all, start, limit
  response = Mks::Common::MethodResponse.new(true, nil, self_driving_rates, nil, count)
  render json: response
end

#reload_min_max_transporter_ratesObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'app/controllers/logistics/core/self_driving_rates_controller.rb', line 138

def reload_min_max_transporter_rates
  reload_rate = params[:reload_rate]
  route_id = reload_rate[:route_id]
  rate_type = reload_rate[:rate_type]
  items = reload_rate[:items]
  data_min = []
  data_max = []
  result = []
  default_transporter = Transporter.where(name: "Reference Transporter")
  rate_period = TransportRatePeriod.where(current_period: true)
  break_bulk_unit_ids = []
  items.each do |break_bulk_unit|
    break_bulk_unit_ids << break_bulk_unit[:id]
  end
  if default_transporter.length > 0 && rate_period.length > 0
    default_transporter_id = default_transporter[0].id
    rate_period_id = rate_period[0].id
    self_driving_rates = SelfDrivingRate.where("route_id = ? and rate_type = ? and rate_period_id = ? and rate > 0",
                                               "#{route_id}",
                                               "#{rate_type}",
                                               "#{rate_period_id}")
    current_self_driving_rates = self_driving_rates.where(transporter_id: default_transporter_id)
    current_self_driving_rates.each do |bb_rate|
      break_bulk_unit_ids << bb_rate[:break_bulk_unit_id]
    end
    self_driving_rates = self_driving_rates.where.not(transporter_id: default_transporter_id,
                                                      break_bulk_unit_id: break_bulk_unit_ids)

    self_driving_rate_uniques = self_driving_rates.select(:break_bulk_unit_id).distinct
    if self_driving_rates.length > 0
      self_driving_rate_uniques.each do |self_driving_rate|
        break_bulk_unit_id = self_driving_rate.break_bulk_unit_id

        rate = self_driving_rates.where({ break_bulk_unit_id: break_bulk_unit_id })
        rate = rate.order("rate ASC")

        if rate.length > 0
          data_min.push(create_min_max_data rate.first)
          data_max.push(create_min_max_data rate.last)
        end
      end
    end
    result.push({min_rate: data_min, max_rate: data_max})
  end
  response = Mks::Common::MethodResponse.new(true, nil, result, nil, nil)
  render json: response
end

#updateObject

PATCH/PUT /self_driving_rates/1 PATCH/PUT /self_driving_rates/1.json



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'app/controllers/logistics/core/self_driving_rates_controller.rb', line 277

def update
  route_type = params[:route_type]
  count = 0
  self_driving_rates = params[:updated_self_driving_rates]
  self_driving_rates.each do |self_driving_rate|
    old_driving_rate = SelfDrivingRate.find(self_driving_rate['id'])
    rate = self_driving_rate['rate']
    old_driving_rate.rate = rate
    if old_driving_rate.save
      count += 1
    end
  end
  message = "A total of <b>" + count.to_s + "</b> " + route_type + " Self Driving Rate updated successfully!"
  response = Mks::Common::MethodResponse.new(true, message, self_driving_rates, nil, nil)
  render json: response
end