module Logistics module Core class TransportRatePeriod < ApplicationRecord validates :effective_date, :valid_until, uniqueness: true, allow_blank: false validates :effective_date, uniqueness: {scope: [:effective_date, :valid_until]} validate :effective_date_is_greater_than_valid_until 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 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 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 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 private def effective_date_is_greater_than_valid_until return if effective_date.blank? || effective_date.blank? if valid_until <= effective_date errors.add(:effective_date, "cannot be greater or equal to validity date") end end end end end