available_products_controller.rb 9.7 KB

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