application_controller.rb 9.5 KB

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