application_controller.rb 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. class ApplicationController < ActionController::Base
  2. # Prevent CSRF attacks by raising an exception.
  3. # For APIs, you may want to use :null_session instead.
  4. before_filter do
  5. resource = controller_path.singularize.tr('/', '_').to_sym
  6. method = "#{resource}_params"
  7. params[resource] &&= send(method) if respond_to?(method, true)
  8. end
  9. before_filter :set_pos_config
  10. around_filter :user_time_zone, if: :set_pos_config
  11. protect_from_forgery with: :exception
  12. ##--- Breadcrum_rails
  13. add_breadcrumb I18n.t("breadcrumbs.dashboard"), :root_path
  14. ##--- Restriccion para autentificacion
  15. before_action :authenticate_user!
  16. ##--- Notes boxes
  17. add_flash_types :success, :warning, :danger, :info
  18. ##--- Parametros permitidos para los usuarios
  19. before_action :configure_permitted_parameters, if: :devise_controller?
  20. ##--- Redireccionamiento para los permisos a modulos
  21. rescue_from CanCan::AccessDenied do |exception|
  22. redirect_to root_url, alert: exception.message
  23. end
  24. ##--- Funciones personalizadas
  25. def getcounties
  26. render json: SpmxCounty.where("state_id = ?", params[:state_id])
  27. end
  28. def find
  29. query = params[:query]
  30. if query.include? ':'
  31. # buscar con atributos
  32. product_name = query[0, query.index(':') - 1]
  33. attribute = query[query.index(':') + 1, query.length]
  34. else
  35. product_name = query
  36. end
  37. render json: query.include?(":") ? Product.name_sku_barcode_attribute_like(product_name, attribute).limit(30).to_json(methods: [:small_img, :display_attributes]) : Product.name_sku_barcode_like(params[:query]).limit(30).to_json(methods: [:small_img, :display_attributes])
  38. end
  39. # para special_prices
  40. def find_sp
  41. query = params[:query]
  42. product_name = query
  43. render json: Product.name_sku_barcode_like_sp(product_name).limit(30).to_json(methods: [:small_img])
  44. end
  45. def find_from_stock
  46. query = params[:query]
  47. if query.include? ':'
  48. # buscar con atributos
  49. product_name = query[0, query.index(':') - 1]
  50. attribute = query[query.index(':') + 1, query.length]
  51. else
  52. product_name = query
  53. end
  54. if current_user.usertype == 'S'
  55. render json: query.include?(":") ? Warehouse.find(current_user.warehouse_id).products.name_sku_barcode_attribute_like(product_name, attribute).where("stock > 0").limit(30).to_json(methods: [:small_img, :display_attributes]) : Warehouse.find(current_user.warehouse_id).products.name_sku_barcode_like(params[:query]).where("stock > 0").limit(30).to_json(methods: [:small_img, :display_attributes])
  56. else
  57. render json: query.include?(":") ? Pointsale.find(current_user.pointsale_id).products.name_sku_barcode_attribute_like(product_name, attribute).where("stock > 0").limit(30).to_json(methods: [:small_img, :display_attributes]) : Pointsale.find(current_user.pointsale_id).products.name_sku_barcode_like(params[:query]).where("stock > 0").limit(30).to_json(methods: [:small_img, :display_attributes])
  58. end
  59. end
  60. def find_from_stock_by_pointsale
  61. id = params[:pointsale_id][2, params[:pointsale_id].length]
  62. query = params[:query]
  63. if query.include? ':'
  64. # buscar con atributos
  65. product_name = query[0, query.index(':') - 1]
  66. attribute = query[query.index(':') + 1, query.length]
  67. else
  68. product_name = query
  69. end
  70. if params[:pointsale_id].first == 'P'
  71. render json: query.include?(":") ? Pointsale.find(id).products.name_sku_barcode_attribute_like(product_name, attribute).where("stock > 0").limit(30).to_json(methods: [:small_img, :display_attributes]) : Pointsale.find(id).products.name_sku_barcode_like(params[:query]).where("stock > 0").limit(30).to_json(methods: [:small_img, :display_attributes])
  72. else
  73. render json: query.include?(":") ? Warehouse.find(id).products.name_sku_barcode_attribute_like(product_name, attribute).where("stock > 0").limit(30).to_json(methods: [:small_img, :display_attributes]) : Warehouse.find(id).products.name_sku_barcode_like(params[:query]).where("stock > 0").limit(30).to_json(methods: [:small_img, :display_attributes])
  74. end
  75. end
  76. def get_subcategories
  77. render json: params[:category_id] != '0' ? Category.activos.where("parent_id = ?", params[:category_id]) : Category.activos.where('parent_id != 0')
  78. end
  79. def set_pos_config
  80. @pos_config = PosConfig.first
  81. end
  82. def user_time_zone(&block)
  83. Time.use_zone(@pos_config.time_zone, &block)
  84. end
  85. def delete_pre_sales
  86. PreSale.where(user_id: current_user.id).destroy_all
  87. respond_to do |format|
  88. format.json { head :no_content }
  89. end
  90. end
  91. def delete_pre_purchases
  92. PrePurchase.where(user_id: current_user.id).destroy_all
  93. # render head :no_content
  94. respond_to do |format|
  95. format.json { head :no_content }
  96. end
  97. end
  98. def delete_pre_transfers
  99. respond_to do |format|
  100. pre_transfers = PreTransfer.where(user_id: current_user.id)
  101. pre_transfers.each do |pre|
  102. # rubocop:disable Style/Next
  103. if pre.destroy
  104. stock = pre.origin_is_pointsale == 1 ? AvailableProduct.find_by(pointsale_id: pre.origin_id, product_id: pre.product_id) : WarehouseStock.find_by(warehouse_id: pre.origin_id, product_id: pre.product_id)
  105. stock.stock += pre.quantity
  106. stock.save
  107. end
  108. # rubocop:enable Style/Next
  109. end
  110. format.json { head :ok }
  111. end
  112. end
  113. def get_max_product_id
  114. render json: Product.maximum(:id).to_i.next
  115. end
  116. def get_max_purchaseid_by_pointsale
  117. prefix = Pointsale.find(params[:pointsale_id]).prefix
  118. next_id = Purchase.where(pointsale_id: params[:pointsale_id]).count.next
  119. render json: "#{prefix}-C-#{next_id}"
  120. end
  121. def get_max_purchaseid_by_warehouse
  122. prefix = Warehouse.find(params[:warehouse_id]).prefix
  123. next_id = Purchase.where(warehouse_id: params[:warehouse_id]).count.next
  124. render json: "#{prefix}-C-#{next_id}"
  125. end
  126. def get_next_sale_code
  127. pointsale = OpenCashRegister.find(params[:open_cash_register_id]).cash_register.pointsale
  128. next_id = pointsale.sales.count.next
  129. render json: "#{pointsale.prefix}-V-#{next_id}"
  130. end
  131. def get_next_expense_code
  132. if current_user.usertype == 'A'
  133. next_id = Expense.where("expense_code ilike ?", '%ADM%').count.next
  134. render json: "ADM-E-#{next_id}"
  135. else
  136. pointsale = OpenCashRegister.find(params[:open_cash_register_id]).cash_register.pointsale
  137. next_id = pointsale.expenses.count.next
  138. render json: "#{pointsale.prefix}-E-#{next_id}"
  139. end
  140. end
  141. def products_by_category_pointsale
  142. products = Array.new
  143. products_by_line = Array.new
  144. category_id = params[:category_id]
  145. id = params[:pointsale_id][2, params[:pointsale_id].length]
  146. categories = Category.find(category_id).self_and_descendents
  147. categories.each do |category|
  148. products_by_line += category.products
  149. end
  150. if params[:pointsale_id].first == 'P'
  151. Pointsale.find(id).products.each do |p|
  152. if products_by_line.include?(p)
  153. products << p
  154. end
  155. end
  156. else
  157. Warehouse.find(id).products.each do |p|
  158. if products_by_line.include?(p)
  159. products << p
  160. end
  161. end
  162. end
  163. render json: products
  164. end
  165. protected
  166. def configure_permitted_parameters
  167. devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:userid, :first_name, :last_name, :email, :password, :remember_me) }
  168. end
  169. def respond_modal_with(*args, &blk)
  170. options = args.extract_options!
  171. options[:responder] = ModalResponder
  172. respond_with(*args, options, &blk)
  173. end
  174. end