class AvailableProductsDatatable
delegate :params,:fa_icon, :available_products_path, to: :@view
def initialize(view, pointsale)
@view = view
@pointsale = pointsale
end
def as_json(options = {})
{
sEcho: params[:sEcho].to_i,
iTotalRecords: @pointsale.products.activos_children.size,
iTotalDisplayRecords: products.total_entries,
aaData: data
}
end
private
def data
if params[:table] == 'in_pointsale'
products.map do | available |
category = available.product.categories[0]
{
'DT_RowId' => "available_#{available.id}",
'0' => '',
'1' => available.product.sku,
'2' => get_display_name(available.product),
'3' => (category.parent_id == 0 ? category.category : category.parent.category),
'4' => (category.parent_id != 0 ? category.category : ' '),
'5' => available.stock
}.compact.reject { |k, v| v.nil? }
end
else
products.map do | product |
category = product.categories[0]
{
'DT_RowId' => "product_#{product.id}",
'0' => '',
'1' => product.sku,
'2' => get_display_name(product),
'3' => (category.parent_id == 0 ? category.category : category.parent.category),
'4' => (category.parent_id != 0 ? category.category : ' ')
}.compact.reject { |k, v| v.nil? }
end
end
end
def get_display_name(product)
name = "
"
if product.display_attributes.present?
name += "#{product.display_attributes}
"
end
if product.barcode.present?
name += ": #{product.barcode}
"
end
if product.description.present?
name += "Descripción: #{product.description}"
end
return name
end
def products
@products ||= fetch_products
end
def fetch_products
if params[:table] == 'in_pointsale'
products = AvailableProduct.activos.where(:pointsale_id => params[:id]).activos
else
products_in_pointsale = @pointsale.products.activos_children.pluck(:id)
products = Product.activos_children.where.not(id: products_in_pointsale)
end
products = products.page(page).per_page(per_page)
unless params[:busqueda].blank?
products = products.where("products.sku ilike :search or products.name ilike :search", search: "%#{params[:busqueda]}%").order('products.name')
end
products
end
def page
params[:start].to_i/per_page + 1
end
def per_page
params[:length].to_i > 0 ? params[:length].to_i : 20
end
def sort_column
columns = %w[name]
columns[params[:iSortCol_0].to_i]
end
def sort_direction
params[:sSortDir_0] == "desc" ? "desc" : "asc"
end
end