available_products_controller.rb 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. @location_id = current_user.pointsale_id.present? ? current_user.pointsale_id : current_user.warehouse_id
  9. respond_to do |format|
  10. format.html
  11. format.json { render json: StocksDatatable.new(view_context, current_user, @showcolumns) }
  12. end
  13. end
  14. def initial_stock
  15. @showcolumns = "initial"
  16. @location_id = current_user.pointsale_id.present? ? current_user.pointsale_id : current_user.warehouse_id
  17. # se utiliza para mandarle al datatable el numero de columnas y en que orden se deben de acomodar
  18. @column_definition = [{ "data": "0" }, { "data": "1" }, { "data": "2" }, { "data": "3" }, { "data": "4" }, { "data": "5" }, { "data": "6" }].to_json
  19. respond_to do |format|
  20. format.html
  21. format.json { render json: StocksDatatable.new(view_context, current_user, @showcolumns) }
  22. end
  23. end
  24. def stock_by_pointsale
  25. add_breadcrumb "Existencias", :stock_by_pointsale_path
  26. @selected = current_user.usertype == "S" ? "W-#{current_user.warehouse_id}" : "P-#{current_user.pointsale_id}"
  27. respond_to do |format|
  28. format.html
  29. format.json { render json: StockByPointsaleDatatable.new(view_context, current_user, @selected) }
  30. end
  31. end
  32. def edit_price
  33. @product = @available_product.product
  34. end
  35. def update_price
  36. @product = @available_product.product
  37. 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}."
  38. respond_to do |format|
  39. if @available_product.update_attributes params[:available_product]
  40. @available_product.audit_comment = message
  41. format.html { redirect_to products_url, success: message }
  42. format.json { head :no_content }
  43. format.js { flash[:success] = message }
  44. else
  45. format.js { render :edit_price }
  46. format.json { render json: @available_product.errors, status: :unprocessable_entity }
  47. end
  48. end
  49. end
  50. def update_all_variants_prices
  51. @product = Product.find(params[:product_id])
  52. respond_to do |format|
  53. if @product.is_parent
  54. children_ids = @product.children.pluck(:id)
  55. AvailableProduct.where('pointsale_id = (?) and product_id IN (?)', params[:pointsale_id], children_ids).update_all(price_sale: params[:new_price])
  56. format.json { head :ok }
  57. else
  58. format.json { render json: @available_product.errors, status: :unprocessable_entity }
  59. end
  60. end
  61. end
  62. # POST /pointsales/5/available_products
  63. def create
  64. @available_product = AvailableProduct.new(available_product_params)
  65. respond_to do |format|
  66. if @available_products.save
  67. 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") }
  68. else
  69. format.html { render pointsale_available_product_path(@available_product.pointsale_id) }
  70. end
  71. end
  72. end
  73. def update_stock
  74. min = params[:stock_min]
  75. max = params[:stock_max]
  76. respond_to do |format|
  77. if current_user.usertype == 'S'
  78. WarehouseStock.where(id: params[:ids]).update_all(stock_min: min, stock_max: max)
  79. else
  80. AvailableProduct.where(id: params[:ids]).update_all(stock_min: min, stock_max: max)
  81. end
  82. format.json { head :ok }
  83. end
  84. end
  85. def set_initial_stock
  86. respond_to do |format|
  87. if current_user.usertype == 'S'
  88. WarehouseStock.where(id: params[:ids]).update_all(stock: params[:stock])
  89. else
  90. AvailableProduct.where(id: params[:ids]).update_all(stock: params[:stock])
  91. end
  92. format.json { head :ok }
  93. end
  94. end
  95. # rubocop:disable Metrics/BlockLength
  96. def print_stock
  97. respond_to do |format|
  98. location = params[:location]
  99. category = params[:category]
  100. sub_category = params[:sub_category]
  101. search = params[:search]
  102. size = params[:size].to_i
  103. location_id = params[:location][2, params[:location].length] unless location.blank?
  104. if (current_user.usertype == 'S' && location.blank?) || (!location.blank? && location.first == 'W')
  105. stock = WarehouseStock.activos.includes(:warehouse, :categories).where(warehouse_id: (location_id.blank? ? current_user.warehouse_id : location_id)).where('stock > 0')
  106. elsif (current_user.usertype != 'S' && location.blank?) || (!location.blank? && location.first == 'P')
  107. stock = AvailableProduct.activos.includes(:pointsale, :categories).where(pointsale_id: (location_id.blank? ? current_user.pointsale_id : location_id)).where('stock > 0')
  108. end
  109. if sub_category.present?
  110. stock = stock.joins(:categories).where('categories.id = ?', sub_category)
  111. elsif category.present?
  112. subs_ids = Category.activos.where(parent_id: category).pluck(:id)
  113. stock = stock.joins(:categories).where('categories.id IN (?)', subs_ids)
  114. end
  115. if size != 0
  116. stock = stock.limit(size)
  117. end
  118. unless search.blank?
  119. stock = stock.where("products.sku ilike :search or products.name ilike :search", search: "%#{search}%")
  120. end
  121. if location.first == 'W'
  122. location = Warehouse.find(location_id)
  123. elsif location.first == 'P'
  124. location = Pointsale.find(location_id)
  125. end
  126. format.pdf do
  127. 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'
  128. end
  129. end
  130. end
  131. # rubocop:enable Metrics/BlockLength
  132. def total_products_by_pointsale
  133. location = params[:location]
  134. category = params[:category]
  135. sub_category = params[:sub_category]
  136. location_id = params[:location][2, params[:location].length] unless location.blank?
  137. if location.first == 'P'
  138. stock = AvailableProduct.activos.where(pointsale_id: location_id)
  139. elsif location.first == 'W'
  140. stock = WarehouseStock.activos.where(warehouse_id: location_id)
  141. end
  142. if sub_category.present?
  143. stock = stock.joins(:categories).where('categories.id = ?', sub_category)
  144. elsif category.present?
  145. subs_ids = Category.activos.where(parent_id: category).pluck(:id)
  146. stock = stock.joins(:categories).where('categories.id IN (?)', subs_ids)
  147. end
  148. total_prods = stock.sum(:stock)
  149. render json: total_prods
  150. end
  151. def total_invested_in_pointsale
  152. location = params[:location]
  153. category = params[:category]
  154. sub_category = params[:sub_category]
  155. location_id = params[:location][2, params[:location].length] unless location.blank?
  156. if location.first == 'P'
  157. stock = AvailableProduct.activos.where(pointsale_id: location_id).where('stock > 0')
  158. elsif location.first == 'W'
  159. stock = WarehouseStock.activos.where(warehouse_id: location_id).where('stock > 0')
  160. end
  161. if sub_category.present?
  162. stock = stock.joins(:categories).where('categories.id = ?', sub_category)
  163. elsif category.present?
  164. subs_ids = Category.activos.where(parent_id: category).pluck(:id)
  165. stock = stock.joins(:categories).where('categories.id IN (?)', subs_ids)
  166. end
  167. obj = Hash.new
  168. obj['invested_in_pesos'] = location.first == 'P' ? stock.where('products.is_in_dollars = false').sum('available_products.stock * products.price_base') : stock.where('products.is_in_dollars = false').sum('warehouse_stocks.stock * products.price_base')
  169. obj['invested_in_dollars'] = location.first == 'P' ? stock.where('products.is_in_dollars = true').sum('available_products.stock * products.price_base_dollars') : stock.where('products.is_in_dollars = true').sum('warehouse_stocks.stock * products.price_base_dollars')
  170. render json: obj
  171. end
  172. # DELETE //pointsales/5/available_products/1
  173. def destroy
  174. @available_product.destroy
  175. # respond_to do |format|
  176. # format.html { redirect_to foods_url, notice: 'Food was successfully destroyed.' }
  177. # format.json { head :no_content }
  178. # end
  179. end
  180. private
  181. # Use callbacks to share common setup or constraints between actions.
  182. def get_products
  183. @products = Product.activos_children
  184. end
  185. # Use callbacks to share common setup or constraints between actions.
  186. def set_available_product
  187. @available_product = AvailableProduct.find(params[:available_product_id])
  188. end
  189. # Never trust parameters from the scary internet, only allow the white list through.
  190. def available_product_params
  191. params.require(:available_product).permit(:pointsale_id, :product_id, :stock_min, :stock_max, :stock, :ids, :price_sale)
  192. end
  193. end