module Logistics module Core class OfferRequest < ApplicationRecord validates :communication_channel_id, :no_of_convoy,:no_of_items, presence: true belongs_to :user, class_name: 'Mks::Auth::User' belongs_to :communication_channel belongs_to :agent, optional: true belongs_to :carrier, optional: true belongs_to :contract, optional: true has_many :review_notes, as: :correspondence belongs_to :warehouse, optional: true belongs_to :transaction_type belongs_to :client belongs_to :operation_type delegate(:name, to: :transaction_type, prefix: true, allow_nil: true) delegate( :contract_number, to: :contract, prefix: false, allow_nil: true ) delegate(:name, to: :client, prefix: true, allow_nil: true) delegate(:name, to: :communication_channel, prefix: true, allow_nil: true) delegate(:name, to: :operation_type, prefix: true, allow_nil: true) def to_json JSON.parse( Jbuilder.encode do |json| json.id self.id json.request_no self.request_no json.transaction_type_id self.transaction_type.id json.transaction_type_name self.transaction_type.name json.status self.status json.client_id self.client.id json.client self.client.name json.communication_channel_id self.communication_channel_id json.communication_channel_name self.communication_channel.name json.source_detail self.source_detail json.point_of_recipient self.point_of_recipient json.date_of_recipient self.date_of_recipient json.general_remark self.general_remark json.freight_forwarding self.freight_forwarding json.customs_clearing self.customs_clearing json.transport self.transport json.rate_level self.rate_level json.security_status self.security_status json.operation_type_id self.operation_type.id json.operation_type_name self.operation_type.name json.no_of_items self.no_of_items json.no_of_convoy self.no_of_convoy json.is_bulk self.is_bulk end ) end def get_bill_of_loading_by_status_and_user(status, user_id) bill_of_loadings = OfferRequest.where(status: status, user_id: user_id).order(:id) return bill_of_loadings end def get_bill_of_loading_by_status(status) bill_of_loadings = OfferRequest.includes(:client, :operation_type, :transaction_type, :communication_channel, :contract).where(status: status).order('id DESC') return bill_of_loadings end def change_to_hash(items) data = [] items.each { |item| data.push item.to_json } return data end def get_request_with_additional_service offer_requests = OfferRequest.where.not(additional_service_status: nil).order(:id) return offer_requests end def get_requests_by_security_status(security_status) offer_requests = OfferRequest.where(security_status: security_status) return offer_requests end def offer_by_ids(ids) OfferRequest.includes(:transaction_type, :contract, :client, :communication_channel, :operation_type).where(id: ids) end def self.fetch_all(bill_of_loading_ids, user, filtering_criteria) result = [] @bill_of_loadings = nil if filtering_criteria != 'none' if filtering_criteria == 'request' @bill_of_loadings = OfferRequest.where('(user_id =:user_id and status = :status_one)',{user_id:user.id,status_one:'draft'} ) else @bill_of_loadings = OfferRequest.where('(user_id =:user_id and status = :status_one)',{user_id:user.id,status_one:filtering_criteria} ) end else @bill_of_loadings = OfferRequest.where.not(id: bill_of_loading_ids).where(user_id: user.id) end @bill_of_loadings.each do |bill_of_loading| result.push({:id => bill_of_loading.id, :bill_of_loading_number => bill_of_loading.bill_of_loading_number, :transaction_type_id => bill_of_loading.transaction_type_id, :transaction_type_name => bill_of_loading.transaction_type_name, :operation_type_id => bill_of_loading.operation_type_id, :operation_type_name => bill_of_loading.operation_type_name, :user_id => bill_of_loading.user.id, :status => bill_of_loading.status, :entry_status => bill_of_loading.entry_status, :client_id => bill_of_loading.client_id, :client => bill_of_loading.client_name, :communication_channel_id => bill_of_loading.communication_channel_id, :communication_channel_name => bill_of_loading.communication_channel_name, :source_detail => bill_of_loading.source_detail, :point_of_recipient => bill_of_loading.point_of_recipient, :date_of_recipient => bill_of_loading.date_of_recipient, :general_remark => bill_of_loading.general_remark, :freight_forwarding => bill_of_loading.freight_forwarding, :customs_clearing => bill_of_loading.customs_clearing, :transport => bill_of_loading.transport, :rate_level => bill_of_loading.rate_level, :route_id => bill_of_loading.route_id, :route_name => bill_of_loading.route_name, :positioning_route_id => bill_of_loading.positioning_route_id, :positioning_route_name => bill_of_loading.positioning_route_name, :return_fee_route_id => bill_of_loading.return_fee_route_id, :return_fee_route_name => bill_of_loading.return_fee_route_name, :review_count =>bill_of_loading.review_count, :no_of_items => bill_of_loading.no_of_items, :no_of_convoy => bill_of_loading.no_of_convoy}) end return result end end end end