application_controller.rb 11 KB

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