available_products_controller.rb 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. class AvailableProductsController < ApplicationController
  2. before_action :set_available_product, only: [:edit_price, :update_price]
  3. autocomplete :available_product, :name, :full => true
  4. def stock
  5. @showcolumns = "minMax"
  6. # se utiliza para mandarle al datatable el numero de columnas y en que orden se deben de acomodar
  7. @column_definition = [{ "data": "0"}, { "data": "1" }, { "data": "2" }, { "data": "3" }, { "data": "4" }, { "data": "5" }, { "data": "6" }, { "data": "7" }].to_json
  8. respond_to do |format|
  9. format.html
  10. format.json { render json: StocksDatatable.new(view_context, current_user, @showcolumns) }
  11. end
  12. end
  13. def initial_stock
  14. @showcolumns = "initial"
  15. # se utiliza para mandarle al datatable el numero de columnas y en que orden se deben de acomodar
  16. @column_definition = [{ "data": "0"}, { "data": "1" }, { "data": "2" }, { "data": "3" }, { "data": "4" }, { "data": "5" }, { "data": "6" }].to_json
  17. respond_to do |format|
  18. format.html
  19. format.json { render json: StocksDatatable.new(view_context, current_user, @showcolumns) }
  20. end
  21. end
  22. def stock_by_pointsale
  23. add_breadcrumb "Existencias", :stock_by_pointsale_path
  24. if current_user.usertype == "S"
  25. @selected = "W-#{current_user.warehouse_id}"
  26. else
  27. @selected = "P-#{current_user.pointsale_id}"
  28. end
  29. respond_to do |format|
  30. format.html
  31. format.json { render json: StockByPointsaleDatatable.new(view_context, current_user, @selected) }
  32. end
  33. end
  34. def edit_price
  35. @product = @available_product.product
  36. end
  37. def update_price
  38. @product = @available_product.product
  39. message = "El precio del venta para el producto #{@available_product.product.name} se actualizo a $#{params[:available_product][:price_sale]} en punto de venta #{@available_product.pointsale.name}."
  40. respond_to do |format|
  41. if @available_product.update_attributes params[:available_product]
  42. @available_product.audit_comment = message
  43. format.html { redirect_to products_url, success: message }
  44. format.json { head :no_content }
  45. format.js { flash[:success] = message }
  46. else
  47. format.js { render :edit_price }
  48. format.json { render json: @available_product.errors, status: :unprocessable_entity }
  49. end
  50. end
  51. end
  52. #POST /pointsales/5/available_products
  53. def create
  54. @available_product = AvailableProduct.new(available_product_params)
  55. respond_to do |format|
  56. if @available_products.save
  57. format.html { redirect_to pointsale_available_product_url, notice: 'Se agregó un producto disponible para el punto de venta.' + Pointsale.find(pointsale_id).select("name") }
  58. else
  59. format.html { render pointsale_available_product_path(@available_product.pointsale_id) }
  60. end
  61. end
  62. end
  63. def updateStock
  64. min = params[:stock_min]
  65. max = params[:stock_max]
  66. respond_to do |format|
  67. if current_user.usertype == 'S'
  68. WarehouseStock.where(id: params[:ids]).update_all(:stock_min => min, :stock_max => max)
  69. else
  70. AvailableProduct.where(id: params[:ids]).update_all(:stock_min => min, :stock_max => max)
  71. end
  72. format.json { head :ok }
  73. end
  74. end
  75. def initialStock
  76. respond_to do |format|
  77. if current_user.usertype == 'S'
  78. WarehouseStock.where(id: params[:ids]).update_all(:stock => params[:stock])
  79. else
  80. AvailableProduct.where(id: params[:ids]).update_all(:stock => params[:stock])
  81. end
  82. format.json { head :ok }
  83. end
  84. end
  85. def print_stock
  86. respond_to do |format|
  87. location = params[:location]
  88. category = params[:category]
  89. sub_category = params[:sub_category]
  90. search = params[:search]
  91. size = params[:size].to_i
  92. location_id = params[:location][2, params[:location].length] if !location.blank?
  93. if ( current_user.usertype == 'S' && location.blank? ) || ( !location.blank? && location.first == 'W' )
  94. stock = WarehouseStock.activos.includes(:warehouse, :categories).where(:warehouse_id => (location_id.blank? ? current_user.warehouse_id : location_id)).where('stock > 0')
  95. elsif ( current_user.usertype != 'S' && location.blank? ) || ( !location.blank? && location.first == 'P' )
  96. stock = AvailableProduct.activos.includes(:pointsale, :categories).where(:pointsale_id => (location_id.blank? ? current_user.pointsale_id : location_id) ).where('stock > 0')
  97. end
  98. if sub_category.present?
  99. stock = stock.joins(:categories).where('categories.id = ?', sub_category)
  100. elsif category.present?
  101. subs_ids = Category.activos.where(:parent_id => category).pluck(:id)
  102. stock = stock.joins(:categories).where('categories.id IN (?)', subs_ids)
  103. end
  104. if size != 0
  105. stock = stock.limit(size)
  106. end
  107. unless search.blank?
  108. stock = stock.where("products.sku ilike :search or products.name ilike :search", search: "%#{search}%")
  109. end
  110. if location.first == 'W'
  111. location = Warehouse.find(location_id)
  112. elsif location.first == 'P'
  113. location = Pointsale.find(location_id)
  114. end
  115. format.pdf do
  116. render pdf: "existencias",
  117. template: "available_products/stock_by_pointsale.pdf.erb",
  118. layout: "pdf_base.pdf.erb",
  119. locals: { :stock => stock, :location => location, :category => category, :sub_category => sub_category, :search => search },
  120. show_as_html: params.key?('debug'),
  121. page_size: 'Letter'
  122. end
  123. end
  124. end
  125. def total_products_by_pointsale
  126. location = params[:location]
  127. category = params[:category]
  128. sub_category = params[:sub_category]
  129. location_id = params[:location][2, params[:location].length] if !location.blank?
  130. if location.first == 'P'
  131. stock = AvailableProduct.activos.where(:pointsale_id => location_id )
  132. elsif location.first == 'W'
  133. stock = WarehouseStock.activos.where(:warehouse_id => location_id )
  134. end
  135. if sub_category.present?
  136. stock = stock.joins(:categories).where('categories.id = ?', sub_category)
  137. elsif category.present?
  138. subs_ids = Category.activos.where(:parent_id => category).pluck(:id)
  139. stock = stock.joins(:categories).where('categories.id IN (?)', subs_ids)
  140. end
  141. total_prods = stock.sum(:stock)
  142. render :json => total_prods
  143. end
  144. # DELETE //pointsales/5/available_products/1
  145. def destroy
  146. @available_product.destroy
  147. # respond_to do |format|
  148. # format.html { redirect_to foods_url, notice: 'Food was successfully destroyed.' }
  149. # format.json { head :no_content }
  150. # end
  151. end
  152. private
  153. # Use callbacks to share common setup or constraints between actions.
  154. def get_products
  155. @products = Product.activos_children
  156. end
  157. # Use callbacks to share common setup or constraints between actions.
  158. def set_available_product
  159. @available_product = AvailableProduct.find(params[:available_product_id])
  160. end
  161. # Never trust parameters from the scary internet, only allow the white list through.
  162. def available_product_params
  163. params.require(:available_product).permit(:pointsale_id, :product_id, :stock_min, :stock_max, :stock, :ids, :price_sale)
  164. end
  165. end