| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- class AvailableProductsDatatable
- delegate :params, :fa_icon, :available_products_path, to: :@view
- def initialize(view, pointsale)
- @view = view
- @pointsale = pointsale
- end
- def as_json(*)
- {
- 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' => '<input class="form-control checkboxes" type="checkbox"/>',
- '1' => available.product.sku,
- '2' => get_display_name(available.product),
- '3' => (category.parent_id.zero? ? 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' => '<input class="form-control checkboxes" type="checkbox"/>',
- '1' => product.sku,
- '2' => get_display_name(product),
- '3' => (category.parent_id.zero? ? 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 = "<label style='margin-bottom:0px'><strong>#{product.name}</strong></label> <br>"
- name += "#{product.display_attributes} <br>" if product.display_attributes.present?
- name += "<i class='fa fa-barcode'></i>: #{product.barcode} <br>" if product.barcode.present?
- name += "Descripción: #{product.description}" if product.barcode.present?
- name
- end
- def products
- @products ||= fetch_products
- end
- def fetch_products
- products =
- if params[:table] == 'in_pointsale'
- AvailableProduct.joins(:product, :categories).activos.where(pointsale_id: params[:id]).activos
- else
- products_in_pointsale = @pointsale.products.activos_children.pluck(:id)
- Product.joins(:categories).activos_children.where.not(id: products_in_pointsale).order('products.name')
- end
- products = products.page(page).per_page(per_page)
- search = params[:busqueda]
- name_searched =
- if search.include? ':'
- search[0, search.index(':') - 1]
- else
- search
- end
- unless search.blank?
- products = products.where("products.sku ilike :search or products.name ilike :search", search: "%#{name_searched}%").order('products.name')
- if search.include? ':'
- attribute = search[search.index(':') + 1, search.length]
- products = products.where('attributes_json ilike :attribute', attribute: "%#{attribute}%")
- end
- 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
|