products_datatable.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. class ProductsDatatable
  2. delegate :params, :link_to, :number_to_currency, :image_tag,:fa_icon,
  3. :products_path, :edit_product_path, :product_update_status_path, :product_prices_path,
  4. :available_products_path, :available_product_edit_price_path, :product_edit_variants_path,
  5. to: :@view
  6. def initialize(view, user)
  7. @view = view
  8. @current_user = user
  9. end
  10. def as_json(options = {})
  11. {
  12. sEcho: params[:sEcho].to_i,
  13. iTotalRecords: Product.vigentes_parents.size,
  14. iTotalDisplayRecords: products.total_entries,
  15. aaData: data
  16. }
  17. end
  18. private
  19. def data
  20. products.map.with_index do |product, index|
  21. if product.img_product?
  22. img = product.img_product.url(:medium)
  23. else
  24. img = "/images/small/missing.png"
  25. end
  26. is_product_available = product.available_in_pointsale?(@current_user.pointsale_id)
  27. product_available = product.get_available_in_pointsale(@current_user.pointsale_id) if is_product_available
  28. [
  29. (index +1),
  30. (image_tag img),
  31. "<label>#{product.name} </label> <br> SKU: <b> #{product.sku} </b>
  32. #{ '<i class="fa fa-cubes font-yellow-gold"></i>' if product.is_parent }
  33. <br>" + (product.parent_id.nil? ? "" : "<small>#{product.display_attributes}</small><br>") + "<i class='fa fa-barcode'></i>: <b> #{product.barcode} </b> <br>
  34. <p> #{product.description} </p>",
  35. (@current_user.usertype == 'A' ? (product.is_in_dollars? ? "#{number_to_currency(product.price_base_dollars, precision: 2)} USD" : "#{number_to_currency(product.price_base, precision: 2)} MXN") : nil),
  36. "<h3> #{number_to_currency(product.get_price_sale(@current_user.pointsale_id), precision: 2)}</h3>
  37. #{ is_product_available && (product_available && !product_available.price_sale.nil?) && @current_user.usertype == 'G' ? ('Precio de venta base <br>' + number_to_currency(product.price_sale, precision: 2) ) : ''}",
  38. product.categories[0].category,
  39. product.active? ? "<i class='fa fa-check fa-2 font-green'></i>" : "<i class='fa fa-times fa-2 font-red'></i>",
  40. get_actions(@current_user.usertype, product, is_product_available, product_available)
  41. ].compact
  42. end
  43. end
  44. def products
  45. @products ||= fetch_products
  46. end
  47. def fetch_products
  48. products = Product.vigentes_parents.includes(:categories)
  49. if @current_user.usertype == 'G'
  50. products = Product.activos_children
  51. end
  52. products = products.page(page).per_page(per_page)
  53. unless params[:busqueda].blank?
  54. products = products.where("sku ilike :search or name ilike :search", search: "%#{params[:busqueda]}%")
  55. end
  56. products
  57. end
  58. def page
  59. params[:start].to_i/per_page + 1
  60. end
  61. def per_page
  62. params[:length].to_i > 0 ? params[:length].to_i : 20
  63. end
  64. def sort_column
  65. columns = %w[name]
  66. columns[params[:iSortCol_0].to_i]
  67. end
  68. def sort_direction
  69. params[:sSortDir_0] == "desc" ? "desc" : "asc"
  70. end
  71. def get_actions(usertype, product, is_product_available, product_available)
  72. links = '<div class="clearfix">'
  73. links += link_to(fa_icon('search'), product, {class: 'btn btn-icon-only default filtros', :title=>"Ver producto"})
  74. if usertype == 'G'
  75. links += (link_to(fa_icon('dollar'), available_product_edit_price_path(product_available), { :remote => true, class: 'btn btn-icon-only green-dark', :title=>"Cambiar precio"})) if is_product_available
  76. elsif usertype == 'A'
  77. links += link_to(fa_icon('edit'), edit_product_path(product), {class: 'btn btn-icon-only btn-primary filtros', :title=>"Modificar producto"})
  78. links += link_to(fa_icon('copy'), product_edit_variants_path(product.id), {:class => 'btn btn-icon-only btn-info filtros', :title=>"Modificar variantes del producto"}) if product.presentation
  79. links += link_to(fa_icon('list-alt'), product_prices_path(product), {:remote => true, :class => 'btn btn-icon-only green-dark', :title=>"Precios en punto de venta"})
  80. links += (link_to(fa_icon('toggle-off'), product_update_status_path(product), :class => 'btn btn-icon-only default', :title=>"Desactivar producto", data: { confirm: '¿Esta seguro de desactivar el producto?', method: 'post'})) if product.active?
  81. links += (link_to(fa_icon('toggle-on'), product_update_status_path(product), :class => 'btn btn-icon-only green-jungle', :title=>"Activar producto", data: { confirm: '¿Esta seguro de activar el producto?', method: 'post'})) if product.inactive?
  82. links += (link_to(fa_icon('trash-o'), product, :class => 'btn btn-icon-only btn-danger', :title=>"Eliminar producto", data: { confirm: '¿Esta seguro de eliminar el producto?', method: :delete})) if product.can_be_deleted?
  83. end
  84. links += '</div>'
  85. return links
  86. end
  87. end