available_products_controller.rb 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. @selected = current_user.usertype == "S" ? "W-#{current_user.warehouse_id}" : "P-#{current_user.pointsale_id}"
  25. respond_to do |format|
  26. format.html
  27. format.json { render json: StockByPointsaleDatatable.new(view_context, current_user, @selected) }
  28. end
  29. end
  30. def edit_price
  31. @product = @available_product.product
  32. end
  33. def update_price
  34. @product = @available_product.product
  35. 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}."
  36. respond_to do |format|
  37. if @available_product.update_attributes params[:available_product]
  38. @available_product.audit_comment = message
  39. format.html { redirect_to products_url, success: message }
  40. format.json { head :no_content }
  41. format.js { flash[:success] = message }
  42. else
  43. format.js { render :edit_price }
  44. format.json { render json: @available_product.errors, status: :unprocessable_entity }
  45. end
  46. end
  47. end
  48. def update_all_variants_prices
  49. @product = Product.find(params[:product_id])
  50. respond_to do |format|
  51. if @product.is_parent
  52. children_ids = @product.children.pluck(:id)
  53. AvailableProduct.where('pointsale_id = (?) and product_id IN (?)', params[:pointsale_id], children_ids).update_all(price_sale: params[:new_price])
  54. format.json { head :ok }
  55. else
  56. format.json { render json: @available_product.errors, status: :unprocessable_entity }
  57. end
  58. end
  59. end
  60. # POST /pointsales/5/available_products
  61. def create
  62. @available_product = AvailableProduct.new(available_product_params)
  63. respond_to do |format|
  64. if @available_products.save
  65. 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") }
  66. else
  67. format.html { render pointsale_available_product_path(@available_product.pointsale_id) }
  68. end
  69. end
  70. end
  71. def update_stock
  72. min = params[:stock_min]
  73. max = params[:stock_max]
  74. respond_to do |format|
  75. if current_user.usertype == 'S'
  76. WarehouseStock.where(id: params[:ids]).update_all(stock_min: min, stock_max: max)
  77. else
  78. AvailableProduct.where(id: params[:ids]).update_all(stock_min: min, stock_max: max)
  79. end
  80. format.json { head :ok }
  81. end
  82. end
  83. def set_initial_stock
  84. respond_to do |format|
  85. if current_user.usertype == 'S'
  86. WarehouseStock.where(id: params[:ids]).update_all(stock: params[:stock])
  87. else
  88. AvailableProduct.where(id: params[:ids]).update_all(stock: params[:stock])
  89. end
  90. format.json { head :ok }
  91. end
  92. end
  93. # rubocop:disable Metrics/BlockLength
  94. def print_stock
  95. respond_to do |format|
  96. location = params[:location]
  97. category = params[:category]
  98. sub_category = params[:sub_category]
  99. search = params[:search]
  100. size = params[:size].to_i
  101. location_id = params[:location][2, params[:location].length] unless location.blank?
  102. if (current_user.usertype == 'S' && location.blank?) || (!location.blank? && location.first == 'W')
  103. stock = WarehouseStock.activos.includes(:warehouse, :categories).where(warehouse_id: (location_id.blank? ? current_user.warehouse_id : location_id)).where('stock > 0')
  104. elsif (current_user.usertype != 'S' && location.blank?) || (!location.blank? && location.first == 'P')
  105. stock = AvailableProduct.activos.includes(:pointsale, :categories).where(pointsale_id: (location_id.blank? ? current_user.pointsale_id : location_id)).where('stock > 0')
  106. end
  107. if sub_category.present?
  108. stock = stock.joins(:categories).where('categories.id = ?', sub_category)
  109. elsif category.present?
  110. subs_ids = Category.activos.where(parent_id: category).pluck(:id)
  111. stock = stock.joins(:categories).where('categories.id IN (?)', subs_ids)
  112. end
  113. if size != 0
  114. stock = stock.limit(size)
  115. end
  116. unless search.blank?
  117. stock = stock.where("products.sku ilike :search or products.name ilike :search", search: "%#{search}%")
  118. end
  119. if location.first == 'W'
  120. location = Warehouse.find(location_id)
  121. elsif location.first == 'P'
  122. location = Pointsale.find(location_id)
  123. end
  124. format.pdf do
  125. render pdf: "existencias", template: "available_products/stock_by_pointsale.pdf.erb", layout: "pdf_base.pdf.erb", locals: { stock: stock, location: location, category: category, sub_category: sub_category, search: search }, show_as_html: params.key?('debug'), page_size: 'Letter'
  126. end
  127. end
  128. end
  129. # rubocop:enable Metrics/BlockLength
  130. def total_products_by_pointsale
  131. location = params[:location]
  132. category = params[:category]
  133. sub_category = params[:sub_category]
  134. location_id = params[:location][2, params[:location].length] unless location.blank?
  135. if location.first == 'P'
  136. stock = AvailableProduct.activos.where(pointsale_id: location_id)
  137. elsif location.first == 'W'
  138. stock = WarehouseStock.activos.where(warehouse_id: location_id)
  139. end
  140. if sub_category.present?
  141. stock = stock.joins(:categories).where('categories.id = ?', sub_category)
  142. elsif category.present?
  143. subs_ids = Category.activos.where(parent_id: category).pluck(:id)
  144. stock = stock.joins(:categories).where('categories.id IN (?)', subs_ids)
  145. end
  146. total_prods = stock.sum(:stock)
  147. render json: total_prods
  148. end
  149. # DELETE //pointsales/5/available_products/1
  150. def destroy
  151. @available_product.destroy
  152. # respond_to do |format|
  153. # format.html { redirect_to foods_url, notice: 'Food was successfully destroyed.' }
  154. # format.json { head :no_content }
  155. # end
  156. end
  157. private
  158. # Use callbacks to share common setup or constraints between actions.
  159. def get_products
  160. @products = Product.activos_children
  161. end
  162. # Use callbacks to share common setup or constraints between actions.
  163. def set_available_product
  164. @available_product = AvailableProduct.find(params[:available_product_id])
  165. end
  166. # Never trust parameters from the scary internet, only allow the white list through.
  167. def available_product_params
  168. params.require(:available_product).permit(:pointsale_id, :product_id, :stock_min, :stock_max, :stock, :ids, :price_sale)
  169. end
  170. end