class Transaction < ApplicationRecord belongs_to :stored_product belongs_to :user belongs_to :from_stockroom, class_name: 'Stockroom', foreign_key: 'from_stockroom_id', optional: true belongs_to :to_stockroom, class_name: 'Stockroom', foreign_key: 'to_stockroom_id', optional: true attr_accessor :user_selection
class TransactionsDatatable < AjaxDatatablesRails::ActiveRecord
extend Forwardable
include Rails.application.routes.url_helpers
def_delegators :@view, :current_user
def initialize(params, opts = {})
@view = opts[:view_context]
super
end
def view_columns
@view_columns ||= {
id: { source: 'Transaction.id', cond: :eq, searchable: true },
stored_product_id: { source: 'StoredProduct.name', searchable: true },
user_id: { source: 'User.name', cond: :like, searchable: true },
from_stockroom_id: { source: 'from_stockroom_name', cond: :like, searchable: true, orderable: true },
to_stockroom_id: { source: 'to_stockroom_name', cond: :like, searchable: true, orderable: true },
quantity: { source: 'Transaction.quantity', searchable: false, orderable: false },
comment: { source: 'Transaction.comment', cond: :like, searchable: true, orderable: false },
created_at: { source: 'Transaction.created_at', searchable: false }
}
end
def data
records.map do |record|
{
id: record.id,
stored_product_id: record.stored_product.name,
user_id: record.user.name,
from_stockroom_id: record.from_stockroom.present? ? record.from_stockroom.name : 'Поставка',
to_stockroom_id: record.to_stockroom.present? ? record.to_stockroom.name : 'Списание',
quantity: record.quantity,
comment: record.comment,
created_at: record.created_at.strftime('%d.%m.%Y'),
DT_RowId: record.id
}
end
end
def get_raw_records
Transaction.where(deleted: false)
.joins(:stored_product, :user)
.joins('LEFT JOIN stockrooms AS from_stockrooms ON from_stockrooms.id = transactions.from_stockroom_id')
.joins('LEFT JOIN stockrooms AS to_stockrooms ON to_stockrooms.id = transactions.to_stockroom_id')
.select('transactions.*, from_stockrooms.name as from_stockroom_name, to_stockrooms.name as to_stockroom_name')
end
def filter_records(records)
to_search_value = params[:columns]["4"]["search"]["value"]
from_search_value = params[:columns]["3"]["search"]["value"]
records = records.where("LOWER(from_stockrooms.name) LIKE ?", "%#{to_search_value.downcase}%") if to_search_value.present?
records = records.where("LOWER(to_stockrooms.name) LIKE ?", "%#{from_search_value.downcase}%") if from_search_value.present?
records = records.where(build_conditions)
return records unless search_value.present?
records.or(records.where("LOWER(to_stockrooms.name) LIKE ?", "%#{params["search"]["value"].downcase}%"))
.or(records.where("LOWER(from_stockrooms.name) LIKE ?", "%#{params["search"]["value"].downcase}%"))
end
end