products_datatable.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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, :product_labels_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. img = product.img_product? ? product.img_product.url(:medium) : img = "/images/small/missing.png"
  22. product_available = @current_user.usertype == "A" || @current_user.usertype == "SS" ? nil : product.get_available_in_pointsale(@current_user.pointsale_id)
  23. arr = [
  24. (index + 1),
  25. (image_tag img),
  26. display_name(product)
  27. ]
  28. arr << dollar_price(product) if @current_user.usertype == "A" || @current_user.usertype == "SS"
  29. arr << [
  30. product_price(product, product_available),
  31. product.category.category,
  32. product.active? ? "<i class='fa fa-check fa-2 font-green'></i>" : "<i class='fa fa-times fa-2 font-red'></i>",
  33. get_actions(product, product_available)
  34. ]
  35. arr.flatten
  36. end
  37. end
  38. def display_name(product)
  39. display_product = "<label>#{product.name} </label> <br> SKU: <b> #{product.sku} </b>"
  40. display_product += "<i class='fa fa-cubes font-yellow-gold'></i>" if product.is_parent
  41. display_product += "<br>" + (product.parent_id.nil? ? "" : "<small>#{product.display_attributes}</small><br>") + "<i class='fa fa-barcode'></i>: <b> #{product.barcode} </b> <br>"
  42. display_product += "<p> #{product.description} </p>"
  43. display_product
  44. end
  45. def dollar_price(product)
  46. if @current_user.usertype == "A" || @current_user.usertype == "SS"
  47. product.is_in_dollars? ? "#{number_to_currency(product.price_base_dollars, precision: 2)} USD" : "#{number_to_currency(product.price_base, precision: 2)} MXN"
  48. else
  49. '.'
  50. end
  51. end
  52. def product_price(product, product_available)
  53. if @current_user.usertype == "G"
  54. price_sale = product_available.present? && product_available.price_sale.present? ? product_available.price_sale : product.price_sale
  55. price = "<h3> #{number_to_currency(price_sale, precision: 2)}</h3>"
  56. price += 'Precio de venta base <br>' + number_to_currency(product.price_sale, precision: 2)
  57. else
  58. ''
  59. end
  60. end
  61. def products
  62. @products ||= fetch_products
  63. end
  64. def fetch_products
  65. products =
  66. if @current_user.usertype == 'G'
  67. Product.activos_children
  68. else
  69. Product.vigentes_parents.includes(:category)
  70. end
  71. products = products.page(page).per_page(per_page)
  72. unless params[:busqueda].blank?
  73. products = products.where("sku ilike :search or name ilike :search", search: "%#{params[:busqueda]}%")
  74. end
  75. products
  76. end
  77. def page
  78. params[:start].to_i / per_page + 1
  79. end
  80. def per_page
  81. params[:length].to_i > 0 ? params[:length].to_i : 20
  82. end
  83. def sort_column
  84. columns = %w[name]
  85. columns[params[:iSortCol_0].to_i]
  86. end
  87. def sort_direction
  88. params[:sSortDir_0] == "desc" ? "desc" : "asc"
  89. end
  90. def get_actions(product, product_available)
  91. links = '<div class="clearfix">'
  92. links += link_to(fa_icon('search'), product, { class: 'btn btn-icon-only default filtros', title: "Ver producto" })
  93. if @current_user.usertype == 'G'
  94. 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 product_available
  95. elsif @current_user.usertype == "A" || @current_user.usertype == "SS"
  96. links += link_to(fa_icon('edit'), edit_product_path(product), { class: 'btn btn-icon-only btn-primary filtros', title: "Modificar producto" })
  97. 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
  98. 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" })
  99. links += link_to(fa_icon('ticket'), product_labels_path(product), { remote: true, class: 'btn btn-icon-only purple', title: "Imprimir etiquetas" })
  100. links += (link_to(fa_icon('toggle-off'), product_update_status_path(product), class: 'btn btn-icon-only default', title: "Desactivar producto", data: { confirm: '¿Está seguro de desactivar el producto?', method: 'post' })) if product.active?
  101. links += (link_to(fa_icon('toggle-on'), product_update_status_path(product), class: 'btn btn-icon-only green-jungle', title: "Activar producto", data: { confirm: '¿Está seguro de activar el producto?', method: 'post' })) if product.inactive?
  102. 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?
  103. end
  104. links += '</div>'
  105. return links
  106. end
  107. end