class StockByPointsaleDatatable delegate :params, :image_tag, :is_a, :number_to_currency, :available_products_path, :stock_by_pointsale_path, to: :@view def initialize(view, user, selected) @view = view @current_user = user @selected = selected end def as_json(_options = {}) total = WarehouseStock.activos.where('stock > 0').count + AvailableProduct.activos.where('stock > 0').count { sEcho: params[:sEcho].to_i, iTotalRecords: total, iTotalDisplayRecords: (params[:length].to_i != -1 ? stock.total_entries : stock.size), aaData: data } end private def data stock.map do |available| img = available.product.img_product? ? available.product.img_product.url(:small) : "/images/small/missing.png" if @current_user.usertype == 'C' [ (image_tag img), get_display_name(available.product), (available.product.category.parent_id.zero? ? available.product.category.category : available.product.category.parent.category), (available.product.category.parent_id != 0 ? available.product.category.category : ' '), available.stock.round, "

#{get_price_sale(available.product)}

" ].compact else arr = [ (image_tag img), get_display_name(available.product), (available.product.category.parent_id.zero? ? available.product.category.category : available.product.category.parent.category), (available.product.category.parent_id != 0 ? available.product.category.category : ' '), available.stock_min.round, available.stock_max.round, available.stock.round ] arr << get_price_sale(available.product) unless @current_user.usertype == "S" arr.compact end end end def get_display_name(product) name = "
" name += "#{product.display_attributes}
" if product.display_attributes.present? name += "SKU: #{product.sku}" name += "
: #{product.barcode}" if product.barcode.present? name end def get_price_sale(product) location_id = params[:location][2, params[:location].length] unless params[:location].blank? "

$#{product.get_price_sale(location_id)}

" end def stock @stock ||= fetch_stock end def fetch_stock location = params[:location] category = params[:category] sub_category = params[:sub_category] location_id = params[:location][2, params[:location].length] unless location.blank? if (@current_user.usertype == 'S' && location.blank?) || (!location.blank? && location.first == 'W') stock = WarehouseStock.activos.includes(:categories, :warehouse).where(warehouse_id: (location_id.blank? ? @current_user.warehouse_id : location_id)).where('stock > 0') elsif (@current_user.usertype != 'S' && location.blank?) || (!location.blank? && location.first == 'P') stock = AvailableProduct.activos.includes(:categories, :pointsale).where(pointsale_id: (location_id.blank? ? @current_user.pointsale_id : location_id)).where('stock > 0') end if sub_category.present? stock = stock.joins(:categories).where('categories.id = ?', sub_category) elsif category.present? subs_ids = Category.activos.where(parent_id: category).pluck(:id) stock = stock.joins(:categories).where('categories.id IN (?)', subs_ids) end if params[:length].to_i != -1 stock = stock.page(page).per_page(per_page) end search = params[:busqueda] unless search.blank? stock = stock.where(prepare_query_string(search)) end stock end def prepare_query_string(query) product_query = if query.include? ':' # search with attributes query_array = query.split(':') @product_name = query_array[0] query_array.shift # delete the name of the product from the array to iterate the attributes attrs_query_string = '' query_array.each do |attribute| next if attribute.nil? attr_type = case attribute[0] when 'c' 'colors' when 't' 'sizes' when 'e' 'styles' end attribute[0] = "" # delete the attribute type character attrs_query_string.concat(" AND products.attributes_json::json->>'#{attr_type}' ilike '%#{attribute}%'") end "products.sku ilike '%#{@product_name}%' or products.name ilike '%#{@product_name}%' #{attrs_query_string}" else "products.sku ilike '%#{query}%' or products.name ilike '%#{query}%'" end product_query 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