available_products_controller.rb 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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.includes(: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.includes(: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. category = Category.find(category)
  129. children = category.children
  130. stock = stock.joins(:categories).where('categories.id IN (?)', children.present? ? children.pluck(:id) : category.id)
  131. end
  132. if size != 0
  133. stock = stock.limit(size)
  134. end
  135. unless search.blank?
  136. stock = stock.where("products.sku ilike :search or products.name ilike :search", search: "%#{search}%")
  137. end
  138. if location.first == 'W'
  139. location = Warehouse.find(location_id)
  140. elsif location.first == 'P'
  141. location = Pointsale.find(location_id)
  142. end
  143. format.pdf do
  144. 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'
  145. end
  146. end
  147. end
  148. # rubocop:enable Metrics/BlockLength
  149. def total_products_by_pointsale
  150. location = params[:location]
  151. category = params[:category]
  152. sub_category = params[:sub_category]
  153. location_id = params[:location][2, params[:location].length] unless location.blank?
  154. if location.first == 'P'
  155. stock = AvailableProduct.activos.where(pointsale_id: location_id)
  156. elsif location.first == 'W'
  157. stock = WarehouseStock.activos.where(warehouse_id: location_id)
  158. end
  159. if sub_category.present?
  160. stock = stock.joins(:categories).where('categories.id = ?', sub_category)
  161. elsif category.present?
  162. category = Category.find(category)
  163. children = category.children
  164. stock = stock.joins(:categories).where('categories.id IN (?)', children.present? ? children.pluck(:id) : category.id)
  165. end
  166. total_prods = stock.sum(:stock)
  167. render json: total_prods
  168. end
  169. def total_invested_in_pointsale
  170. location = params[:location]
  171. category = params[:category]
  172. sub_category = params[:sub_category]
  173. location_id = params[:location][2, params[:location].length] unless location.blank?
  174. if location.first == 'P'
  175. stock = AvailableProduct.activos.where(pointsale_id: location_id).where('stock > 0')
  176. elsif location.first == 'W'
  177. stock = WarehouseStock.activos.where(warehouse_id: location_id).where('stock > 0')
  178. end
  179. if sub_category.present?
  180. stock = stock.joins(:categories).where('categories.id = ?', sub_category)
  181. elsif category.present?
  182. category = Category.find(category)
  183. children = category.children
  184. stock = stock.joins(:categories).where('categories.id IN (?)', children.present? ? children.pluck(:id) : category.id)
  185. end
  186. obj = Hash.new
  187. 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')
  188. 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')
  189. render json: obj
  190. end
  191. # DELETE //pointsales/5/available_products/1
  192. def destroy
  193. @available_product.destroy
  194. # respond_to do |format|
  195. # format.html { redirect_to foods_url, notice: 'Food was successfully destroyed.' }
  196. # format.json { head :no_content }
  197. # end
  198. end
  199. def get_availables_by_product
  200. product = Product.find(params[:product_id])
  201. 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])
  202. render json: availables
  203. end
  204. private
  205. # Use callbacks to share common setup or constraints between actions.
  206. def get_products
  207. @products = Product.activos_children
  208. end
  209. # Use callbacks to share common setup or constraints between actions.
  210. def set_available_product
  211. @available_product = AvailableProduct.find(params[:available_product_id])
  212. end
  213. # Never trust parameters from the scary internet, only allow the white list through.
  214. def available_product_params
  215. params.require(:available_product).permit(:pointsale_id, :product_id, :stock_min, :stock_max, :stock, :ids, :price_sale)
  216. end
  217. end