Class: Logistics::Core::TransportRatePeriod

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

Class Method Summary collapse

Methods inherited from ApplicationRecord

as_json

Class Method Details

.check_range_overlap(rate_period) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'app/models/logistics/core/transport_rate_period.rb', line 8

def self.check_range_overlap rate_period
  transport_rate_periods = TransportRatePeriod.all
  overlap = false
  transport_rate_periods.each do |period|
    if (rate_period.effective_date..rate_period.valid_until).overlaps?(period.effective_date..period.valid_until)
      overlap = true
    end
  end
  return overlap
end

.check_range_overlap_update(rate_period, id) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/logistics/core/transport_rate_period.rb', line 19

def self.check_range_overlap_update rate_period, id
  transport_rate_periods = TransportRatePeriod.all
  result = { 'overlap' => false, 'id' => 0 }
  transport_rate_periods.each do |period|
    if (rate_period.effective_date..rate_period.valid_until).overlaps?(period.effective_date..period.valid_until)
      if period.id != id
        result['overlap'] = true
        result['id'] = period.id
      end
    end
  end
  return result
end

.create_transport_rate_period(rate_period) ⇒ Object



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
# File 'app/models/logistics/core/transport_rate_period.rb', line 33

def self.create_transport_rate_period rate_period
  result = []
  current_rate_period = TransportRatePeriod.where("current_period" => true)
  if current_rate_period.count == 0
    rate_period.current_period = true
    if rate_period.save
      result.push({'success' => true,
                   'message' => "Rate Period information saved successfully!",
                   'data' => rate_period})
    else
      errors = Mks::Common::Util.error_messages rate_period, "Rate Period"
      result.push({'success' => false,
                   'errors' => errors })
    end
  else
    overlap = check_range_overlap rate_period
    if !overlap
      if rate_period.save
        current_rate_period.first.current_period = false
        current_rate_period.first.save
        rate_period.current_period = true
        rate_period.save
        result.push({'success' => true,
                     'message' => "Rate Period information saved successfully!",
                     'data' => rate_period})
      else
        errors = Mks::Common::Util.error_messages rate_period, "Rate Period"
        result.push({'success' => false,
                     'errors' => errors })
      end
    else
      errors = ["Rate Period information overlap with previous entry!"]
      result.push({'success' => false,
                   'errors' => errors })
    end
  end
  return result
end

.update_transport_rate_period(rate_period, new_rate_period, rate_period_params) ⇒ Object



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
# File 'app/models/logistics/core/transport_rate_period.rb', line 72

def self.update_transport_rate_period rate_period, new_rate_period, rate_period_params
  result = []
  if rate_period.effective_date == rate_period_params['effective_date'].to_datetime and rate_period.valid_until == rate_period_params['valid_until'].to_datetime
    if rate_period.update(rate_period_params)
      result.push({'success' => true,
                   'message' => "Rate Period information updated successfully!",
                   'data' => rate_period})
    else
      errors = Mks::Common::Util.error_messages rate_period, "Rate Period"
      result.push({'success' => false,
                   'errors' => errors })
    end
  else
    check_overlap_result = check_range_overlap_update new_rate_period, rate_period.id
    if !check_overlap_result['overlap']
      if rate_period.update(rate_period_params)
        result.push({'success' => true,
                     'message' => "Rate Period information updated successfully!",
                     'data' => rate_period})
      else
        errors = Mks::Common::Util.error_messages rate_period, "Rate Period"
        result.push({'success' => false,
                     'errors' => errors })
      end
    else
      errors = ["Rate Period information overlap with previous entry!"]
      result.push({'success' => false,
                   'errors' => errors })
    end
  end
  return result
end