module Logistics module Core class OutOfGaugeBbRate < ApplicationRecord belongs_to :break_bulk_unit belongs_to :route validates :length_from, :length_to, :height_from, :height_to, :width_from, :width_to, :weight_from, :weight_to, :rate, :route_id, :margin, presence: true, allow_blank: false validates :break_bulk_unit, uniqueness: {scope: [:length_from, :width_from, :height_from, :weight_from, :length_to, :width_to, :height_to, :weight_to, :route_id]} validate :length_from_is_greater_than_length_to validate :width_from_is_greater_than_width_to validate :height_from_is_greater_than_height_to validate :weight_from_is_greater_than_weight_to validate :effective_date_is_greater_than_valid_until def break_bulk_unit_name if self.break_bulk_unit_id break_bulk_unit = BreakBulkUnit.find self.break_bulk_unit_id break_bulk_unit.name else nil end end def route_name if self.route_id route = Route.find self.route_id route.route_name else nil end end def length if self.length_from && self.length_to self.length_from.to_s + ' - ' + self.length_to.to_s else nil end end def width if self.width_from && self.width_to self.width_from.to_s + ' - ' + self.width_to.to_s else nil end end def height if self.height_from && self.height_to self.height_from.to_s + ' - ' + self.height_to.to_s else nil end end def weight if self.weight_from && self.weight_to self.weight_from.to_s + ' - ' + self.weight_to.to_s else nil end end def self.fetch_all result = [] OutOfGaugeBbRate.all.each do |out_of_gauge_bb_rate| result.push({id: out_of_gauge_bb_rate.id, break_bulk_unit_id: out_of_gauge_bb_rate.break_bulk_unit_id, break_bulk_unit_name: out_of_gauge_bb_rate.break_bulk_unit_name, length: out_of_gauge_bb_rate.length, length_from: out_of_gauge_bb_rate.length_from, length_to: out_of_gauge_bb_rate.length_to, height: out_of_gauge_bb_rate.height, height_from: out_of_gauge_bb_rate.height_from, height_to: out_of_gauge_bb_rate.height_to, width: out_of_gauge_bb_rate.width, width_from: out_of_gauge_bb_rate.width_from, width_to: out_of_gauge_bb_rate.width_to, weight: out_of_gauge_bb_rate.weight, weight_from: out_of_gauge_bb_rate.weight_from, weight_to: out_of_gauge_bb_rate.weight_to, rate: out_of_gauge_bb_rate.rate, route_id: out_of_gauge_bb_rate.route_id, route_name: out_of_gauge_bb_rate.route_name, margin: out_of_gauge_bb_rate.margin, effective_date: out_of_gauge_bb_rate.effective_date, valid_until: out_of_gauge_bb_rate.valid_until }) end return result end private def length_from_is_greater_than_length_to return if length_from.blank? || length_to.blank? if length_to < length_from errors.add(:length_from, "cannot be greater than length to") end end def width_from_is_greater_than_width_to return if width_from.blank? || width_to.blank? if width_to < width_from errors.add(:width_from, "cannot be greater than width to") end end def height_from_is_greater_than_height_to return if height_from.blank? || height_to.blank? if height_to < height_from errors.add(:height_from, "cannot be greater than height to") end end def weight_from_is_greater_than_weight_to return if weight_from.blank? || weight_to.blank? if weight_to < weight_from errors.add(:weight_from, "cannot be greater than weight to") end end 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