| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- 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|
- if available.product.img_product?
- img = available.product.img_product.url(:small)
- else
- img = "/images/small/missing.png"
- end
- category = available.categories[0]
- if @current_user.usertype == 'C'
- [
- (image_tag img),
- get_display_name(available.product),
- (category.parent_id == 0 ? category.category : category.parent.category),
- (category.parent_id != 0 ? category.category : ' '),
- # available.stock.round,
- "<h3> $#{get_price_sale(available.product)} </h3>"
- ].compact
- else
- [
- (image_tag img),
- get_display_name(available.product),
- (category.parent_id == 0 ? category.category : category.parent.category),
- (category.parent_id != 0 ? category.category : ' '),
- available.stock_min.round,
- available.stock_max.round,
- available.stock.round,
- "<h3> $#{get_price_sale(available.product)} </h3>"
- ].compact
- end
- end
- end
- def get_display_name(product)
- name = "<label style='margin-bottom:0px'><strong>#{product.name}</strong></label> <br>"
- if product.display_attributes.present?
- name += "#{product.display_attributes} <br>"
- end
- name += "SKU: #{product.sku}"
- if product.barcode.present?
- name += "<br> <i class='fa fa-barcode'></i>: #{product.barcode}"
- end
- return name
- end
- def get_price_sale(product)
- if @current_user.usertype != 'S'
- location_id = params[:location][2, params[:location].length] if !params[:location].blank?
- return product.get_price_sale(location_id)
- else
- return ''
- end
- 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] if !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
- unless params[:busqueda].blank?
- stock = stock.where("products.sku ilike :search or products.name ilike :search", search: "%#{params[:busqueda]}%")
- end
- 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
|