| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- class StocksDatatable
- delegate :params, :image_tag, :available_products_path, :products_initial_stock_path, to: :@view
- def initialize(view, user, showcolumns)
- @view = view
- @current_user = user
- @showcolumns = showcolumns
- end
- def as_json(_options = {})
- total =
- if @current_user.usertype == 'S'
- WarehouseStock.activos.where(warehouse_id: @current_user.warehouse_id)
- else
- AvailableProduct.activos.where(pointsale_id: @current_user.pointsale_id.present? ? @current_user.pointsale_id : params[:pointsale])
- end
- {
- sEcho: params[:sEcho].to_i,
- iTotalRecords: total.size,
- iTotalDisplayRecords: stock.total_entries,
- 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"
- {
- 'DT_RowId' => "availableProduct_#{available.id}",
- '0' => '<input class="form-control checkboxes" type="checkbox"/>',
- '1' => (image_tag img),
- '2' => available.product.sku,
- '3' => available.product.name + '<br>' + available.product.display_attributes,
- '4' => (available.product.category.parent_id.zero? ? available.product.category.category : available.product.category.parent.category),
- '5' => (available.product.category.parent_id != 0 ? available.product.category.category : ' '),
- '6' => (@showcolumns == "minMax" ? available.stock_min : available.stock),
- '7' => (@showcolumns == "minMax" ? available.stock_max : nil)
- }.compact.reject { |_k, v| v.nil? }
- end
- end
- def stock
- @stock ||= fetch_stock
- end
- def fetch_stock
- stock =
- if @current_user.usertype == 'S'
- WarehouseStock.activos.includes(:product, :category).where(warehouse_id: @current_user.warehouse_id).order('products.name')
- else
- AvailableProduct.activos.includes(:product, :category).where(pointsale_id: @current_user.pointsale_id.present? ? @current_user.pointsale_id : params[:pointsale]).order('products.name')
- end
- stock = stock.page(page).per_page(per_page)
- stock = stock.where("products.sku ilike :search or products.name ilike :search", search: "%#{params[:busqueda]}%").order('products.name') unless params[:busqueda].blank?
- stock
- 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
|