application_controller.rb 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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? ':' # search with attributes
  31. query_array = query.split(':')
  32. product_name = query_array[0]
  33. query_array.shift # delete the name of the product from the array to iterate the attributes
  34. attrs_query_string = ''
  35. query_array.each do |attribute|
  36. if attribute.present?
  37. attr_type = case attribute[0]
  38. when 'c'
  39. 'colors'
  40. when 't'
  41. 'sizes'
  42. when 'e'
  43. 'styles'
  44. end
  45. attribute[0] = "" # delete the attribute type character
  46. attrs_query_string.concat(" AND attributes_json::json->>'#{attr_type}' ilike '%#{attribute}%'")
  47. else
  48. next
  49. end
  50. end
  51. else
  52. product_name = query
  53. end
  54. render json: query.include?(":") ? Product.name_sku_barcode_attribute_like(product_name, attrs_query_string).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])
  55. end
  56. # para special_prices
  57. def find_sp
  58. query = params[:query]
  59. product_name = query
  60. render json: Product.name_sku_barcode_like_sp(product_name).limit(30).to_json(methods: [:small_img])
  61. end
  62. def find_from_stock
  63. query = params[:query]
  64. location =
  65. if current_user.usertype == "S"
  66. Warehouse.find(current_user.warehouse_id).products
  67. else
  68. Pointsale.find(current_user.pointsale_id).products
  69. end
  70. if query.include? ':' # search with attributes
  71. query_array = query.split(':')
  72. product_name = query_array[0]
  73. query_array.shift # delete the name of the product from the array to iterate the attributes
  74. attrs_query_string = ''
  75. query_array.each do |attribute|
  76. next if attribute.nil?
  77. attr_type =
  78. case attribute[0]
  79. when 'c'
  80. 'colors'
  81. when 't'
  82. 'sizes'
  83. when 'e'
  84. 'styles'
  85. end
  86. attribute[0] = "" # delete the attribute type character
  87. attrs_query_string.concat(" AND attributes_json::json->>'#{attr_type}' ilike '%#{attribute}%'")
  88. end
  89. consult = location.name_sku_barcode_attribute_like(product_name, attrs_query_string)
  90. else
  91. product_name = query
  92. consult = location.name_sku_barcode_like(params[:query])
  93. end
  94. render json: consult.where("stock > 0").limit(30).to_json(methods: [:small_img, :display_attributes])
  95. end
  96. # rubocop:disable Metrics/BlockNesting
  97. def find_from_stock_by_pointsale
  98. if params[:pointsale_id].present?
  99. id = params[:pointsale_id][2, params[:pointsale_id].length]
  100. query = params[:query]
  101. if query.include? ':' # search with attributes
  102. query_array = query.split(':')
  103. product_name = query_array[0]
  104. query_array.shift # delete the name of the product from the array to iterate the attributes
  105. attrs_query_string = ''
  106. query_array.each do |attribute|
  107. if attribute.present?
  108. attr_type = case attribute[0]
  109. when 'c'
  110. 'colors'
  111. when 't'
  112. 'sizes'
  113. when 'e'
  114. 'styles'
  115. end
  116. attribute[0] = "" # delete the attribute type character
  117. attrs_query_string.concat(" AND attributes_json::json->>'#{attr_type}' ilike '%#{attribute}%'")
  118. else
  119. next
  120. end
  121. end
  122. else
  123. product_name = query
  124. end
  125. if params[:pointsale_id].first == 'P'
  126. render json: query.include?(":") ? Pointsale.find(id).products.name_sku_barcode_attribute_like(product_name, attrs_query_string).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])
  127. else
  128. render json: query.include?(":") ? Warehouse.find(id).products.name_sku_barcode_attribute_like(product_name, attrs_query_string).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])
  129. end
  130. else
  131. render json: {}
  132. end
  133. end
  134. # rubocop:enable Metrics/BlockNesting
  135. def get_subcategories
  136. render json: params[:category_id] != '0' ? Category.activos.where("parent_id = ?", params[:category_id]) : Category.activos.where('parent_id != 0')
  137. end
  138. def set_pos_config
  139. @pos_config = PosConfig.first
  140. end
  141. def user_time_zone(&block)
  142. Time.use_zone(@pos_config.time_zone, &block)
  143. end
  144. def delete_pre_sales
  145. PreSale.where(user_id: current_user.id).destroy_all
  146. respond_to do |format|
  147. format.json { head :no_content }
  148. end
  149. end
  150. def delete_pre_purchases
  151. PrePurchase.where(user_id: current_user.id).destroy_all
  152. # render head :no_content
  153. respond_to do |format|
  154. format.json { head :no_content }
  155. end
  156. end
  157. def delete_pre_transfers
  158. respond_to do |format|
  159. pre_transfers = PreTransfer.where(user_id: current_user.id)
  160. pre_transfers.each do |pre|
  161. # rubocop:disable Style/Next
  162. if pre.destroy
  163. 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)
  164. stock.stock += pre.quantity
  165. stock.save
  166. end
  167. # rubocop:enable Style/Next
  168. end
  169. format.json { head :ok }
  170. end
  171. end
  172. def get_max_product_id
  173. render json: Product.maximum(:id).to_i.next
  174. end
  175. def get_max_purchaseid_by_pointsale
  176. prefix = Pointsale.find(params[:pointsale_id]).prefix
  177. next_id = Purchase.where(pointsale_id: params[:pointsale_id]).count.next
  178. render json: "#{prefix}-C-#{next_id}"
  179. end
  180. def get_max_purchaseid_by_warehouse
  181. prefix = Warehouse.find(params[:warehouse_id]).prefix
  182. next_id = Purchase.where(warehouse_id: params[:warehouse_id]).count.next
  183. render json: "#{prefix}-C-#{next_id}"
  184. end
  185. def get_next_sale_code
  186. pointsale = OpenCashRegister.find(params[:open_cash_register_id]).cash_register.pointsale
  187. next_id = pointsale.sales.count.next
  188. render json: "#{pointsale.prefix}-V-#{next_id}"
  189. end
  190. def get_next_expense_code
  191. code =
  192. if current_user.usertype == "A" || current_user.usertype == "SS"
  193. next_id = Expense.where("expense_code ilike ?", '%ADM%').count.next
  194. "ADM-E-#{next_id}"
  195. else
  196. pointsale = OpenCashRegister.find(params[:open_cash_register_id]).cash_register.pointsale
  197. next_id = pointsale.expenses.count.next
  198. "#{pointsale.prefix}-E-#{next_id}"
  199. end
  200. render json: code
  201. end
  202. def products_by_category_pointsale
  203. products = Array.new
  204. products_by_line = Array.new
  205. category_id = params[:category_id]
  206. id = params[:pointsale_id][2, params[:pointsale_id].length]
  207. categories = Category.find(category_id).self_and_descendents
  208. categories.each do |category|
  209. products_by_line += category.products
  210. end
  211. if params[:pointsale_id].first == 'P'
  212. Pointsale.find(id).products.each do |p|
  213. if products_by_line.include?(p)
  214. products << p
  215. end
  216. end
  217. else
  218. Warehouse.find(id).products.each do |p|
  219. if products_by_line.include?(p)
  220. products << p
  221. end
  222. end
  223. end
  224. render json: products
  225. end
  226. protected
  227. def configure_permitted_parameters
  228. devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:userid, :first_name, :last_name, :email, :password, :remember_me) }
  229. end
  230. def respond_modal_with(*args, &blk)
  231. options = args.extract_options!
  232. options[:responder] = ModalResponder
  233. respond_with(*args, options, &blk)
  234. end
  235. end