application_controller.rb 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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? ':' # search with attributes
  48. query_array = query.split(':')
  49. product_name = query_array[0]
  50. query_array.shift # delete the name of the product from the array to iterate the attributes
  51. attrs_query_string = ''
  52. query_array.each do |attribute|
  53. if attribute.present?
  54. attr_type = case attribute[0]
  55. when 'c'
  56. 'colors'
  57. when 't'
  58. 'sizes'
  59. when 'e'
  60. 'styles'
  61. end
  62. attribute[0] = "" # delete the attribute type character
  63. attrs_query_string.concat(" AND attributes_json::json->>'#{attr_type}' ilike '%#{attribute}%'")
  64. else
  65. next
  66. end
  67. end
  68. else
  69. product_name = query
  70. end
  71. if current_user.usertype == 'S'
  72. render json: query.include?(":") ? Warehouse.find(current_user.warehouse_id).products.name_sku_barcode_multiple_attribute_like(product_name, attrs_query_string).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])
  73. else
  74. render json: query.include?(":") ? Pointsale.find(current_user.pointsale_id).products.name_sku_barcode_multiple_attribute_like(product_name, attrs_query_string).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])
  75. end
  76. end
  77. def find_from_stock_by_pointsale
  78. id = params[:pointsale_id][2, params[:pointsale_id].length]
  79. query = params[:query]
  80. if query.include? ':'
  81. # buscar con atributos
  82. product_name = query[0, query.index(':') - 1]
  83. attribute = query[query.index(':') + 1, query.length]
  84. else
  85. product_name = query
  86. end
  87. if params[:pointsale_id].first == 'P'
  88. 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])
  89. else
  90. 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])
  91. end
  92. end
  93. def get_subcategories
  94. render json: params[:category_id] != '0' ? Category.activos.where("parent_id = ?", params[:category_id]) : Category.activos.where('parent_id != 0')
  95. end
  96. def set_pos_config
  97. @pos_config = PosConfig.first
  98. end
  99. def user_time_zone(&block)
  100. Time.use_zone(@pos_config.time_zone, &block)
  101. end
  102. def delete_pre_sales
  103. PreSale.where(user_id: current_user.id).destroy_all
  104. respond_to do |format|
  105. format.json { head :no_content }
  106. end
  107. end
  108. def delete_pre_purchases
  109. PrePurchase.where(user_id: current_user.id).destroy_all
  110. # render head :no_content
  111. respond_to do |format|
  112. format.json { head :no_content }
  113. end
  114. end
  115. def delete_pre_transfers
  116. respond_to do |format|
  117. pre_transfers = PreTransfer.where(user_id: current_user.id)
  118. pre_transfers.each do |pre|
  119. # rubocop:disable Style/Next
  120. if pre.destroy
  121. 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)
  122. stock.stock += pre.quantity
  123. stock.save
  124. end
  125. # rubocop:enable Style/Next
  126. end
  127. format.json { head :ok }
  128. end
  129. end
  130. def get_max_product_id
  131. render json: Product.maximum(:id).to_i.next
  132. end
  133. def get_max_purchaseid_by_pointsale
  134. prefix = Pointsale.find(params[:pointsale_id]).prefix
  135. next_id = Purchase.where(pointsale_id: params[:pointsale_id]).count.next
  136. render json: "#{prefix}-C-#{next_id}"
  137. end
  138. def get_max_purchaseid_by_warehouse
  139. prefix = Warehouse.find(params[:warehouse_id]).prefix
  140. next_id = Purchase.where(warehouse_id: params[:warehouse_id]).count.next
  141. render json: "#{prefix}-C-#{next_id}"
  142. end
  143. def get_next_sale_code
  144. pointsale = OpenCashRegister.find(params[:open_cash_register_id]).cash_register.pointsale
  145. next_id = pointsale.sales.count.next
  146. render json: "#{pointsale.prefix}-V-#{next_id}"
  147. end
  148. def get_next_expense_code
  149. if current_user.usertype == 'A'
  150. next_id = Expense.where("expense_code ilike ?", '%ADM%').count.next
  151. render json: "ADM-E-#{next_id}"
  152. else
  153. pointsale = OpenCashRegister.find(params[:open_cash_register_id]).cash_register.pointsale
  154. next_id = pointsale.expenses.count.next
  155. render json: "#{pointsale.prefix}-E-#{next_id}"
  156. end
  157. end
  158. def products_by_category_pointsale
  159. products = Array.new
  160. products_by_line = Array.new
  161. category_id = params[:category_id]
  162. id = params[:pointsale_id][2, params[:pointsale_id].length]
  163. categories = Category.find(category_id).self_and_descendents
  164. categories.each do |category|
  165. products_by_line += category.products
  166. end
  167. if params[:pointsale_id].first == 'P'
  168. Pointsale.find(id).products.each do |p|
  169. if products_by_line.include?(p)
  170. products << p
  171. end
  172. end
  173. else
  174. Warehouse.find(id).products.each do |p|
  175. if products_by_line.include?(p)
  176. products << p
  177. end
  178. end
  179. end
  180. render json: products
  181. end
  182. protected
  183. def configure_permitted_parameters
  184. devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:userid, :first_name, :last_name, :email, :password, :remember_me) }
  185. end
  186. def respond_modal_with(*args, &blk)
  187. options = args.extract_options!
  188. options[:responder] = ModalResponder
  189. respond_with(*args, options, &blk)
  190. end
  191. end