| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- class ProductsDatatable
- delegate :params, :link_to, :number_to_currency, :image_tag,:fa_icon,
- :products_path, :edit_product_path, :product_update_status_path, :product_prices_path,
- :available_products_path, :available_product_edit_price_path, :product_edit_variants_path, :product_labels_path,
- to: :@view
- def initialize(view, user)
- @view = view
- @current_user = user
- end
- def as_json(options = {})
- {
- sEcho: params[:sEcho].to_i,
- iTotalRecords: Product.vigentes_parents.size,
- iTotalDisplayRecords: products.total_entries,
- aaData: data
- }
- end
- private
- def data
- products.map.with_index do |product, index|
- img = product.img_product? ? product.img_product.url(:medium) : img = "/images/small/missing.png"
- product_available = @current_user.usertype == "A" || @current_user.usertype == "SS" ? nil : product.get_available_in_pointsale(@current_user.pointsale_id)
- arr = [
- (index + 1),
- (image_tag img),
- display_name(product)
- ]
- arr << dollar_price(product) if @current_user.usertype == "A" || @current_user.usertype == "SS"
- arr << [
- product_price(product, product_available),
- product.categories[0].category,
- product.active? ? "<i class='fa fa-check fa-2 font-green'></i>" : "<i class='fa fa-times fa-2 font-red'></i>",
- get_actions(product, product_available)
- ]
- arr.flatten
- end
- end
- def display_name(product)
- display_product = "<label>#{product.name} </label> <br> SKU: <b> #{product.sku} </b>"
- display_product += "<i class='fa fa-cubes font-yellow-gold'></i>" if product.is_parent
- display_product += "<br>" + (product.parent_id.nil? ? "" : "<small>#{product.display_attributes}</small><br>") + "<i class='fa fa-barcode'></i>: <b> #{product.barcode} </b> <br>"
- display_product += "<p> #{product.description} </p>"
- display_product
- end
- def dollar_price(product)
- if @current_user.usertype == "A" || @current_user.usertype == "SS"
- product.is_in_dollars? ? "#{number_to_currency(product.price_base_dollars, precision: 2)} USD" : "#{number_to_currency(product.price_base, precision: 2)} MXN"
- else
- '.'
- end
- end
- def product_price(product, product_available)
- if @current_user.usertype == "G"
- price_sale = product_available.present? && product_available.price_sale.present? ? product_available.price_sale : product.price_sale
- price = "<h3> #{number_to_currency(price_sale, precision: 2)}</h3>"
- price += 'Precio de venta base <br>' + number_to_currency(product.price_sale, precision: 2)
- else
- ''
- end
- end
- def products
- @products ||= fetch_products
- end
- def fetch_products
- products =
- if @current_user.usertype == 'G'
- Product.activos_children
- else
- Product.vigentes_parents.includes(:categories)
- end
- products = products.page(page).per_page(per_page)
- unless params[:busqueda].blank?
- products = products.where("sku ilike :search or name ilike :search", search: "%#{params[:busqueda]}%")
- 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
- def get_actions(product, product_available)
- links = '<div class="clearfix">'
- links += link_to(fa_icon('search'), product, { class: 'btn btn-icon-only default filtros', title: "Ver producto" })
- if @current_user.usertype == 'G'
- 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
- elsif @current_user.usertype == "A" || @current_user.usertype == "SS"
- links += link_to(fa_icon('edit'), edit_product_path(product), { class: 'btn btn-icon-only btn-primary filtros', title: "Modificar producto" })
- 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
- 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" })
- links += link_to(fa_icon('ticket'), product_labels_path(product), { remote: true, class: 'btn btn-icon-only purple', title: "Imprimir etiquetas" })
- 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?
- 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?
- 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?
- end
- links += '</div>'
- return links
- end
- end
|