customers_controller.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. class CustomersController < ApplicationController
  2. ##--- Abilities
  3. load_and_authorize_resource
  4. ##--- Breadcrum_rails
  5. add_breadcrumb I18n.t("breadcrumbs." + controller_name), :customers_path, only: [:index, :new, :show, :edit]
  6. add_breadcrumb "Clientes deudores", :debtors_path, only: [:debtors, :customer_sales]
  7. add_breadcrumb "Detalle de ventas de clientes deudores", :customer_sales_path, only: :customer_sales
  8. add_breadcrumb "Nuevo " + I18n.t("breadcrumbs." + controller_name).singularize, :new_customer_path, only: :new
  9. add_breadcrumb "Detalle del " + I18n.t("breadcrumbs." + controller_name).singularize , :customer_path, only: :show
  10. add_breadcrumb "Editar " + I18n.t("breadcrumbs." + controller_name).singularize , :edit_customer_path, only: :edit
  11. before_action :set_customer, only: [:show, :edit, :update, :destroy]
  12. before_action :get_filters, only: [:index, :show, :edit, :new, :debtors, :customer_sales]
  13. # GET /customers
  14. # GET /customers.json
  15. def index
  16. @customers = Customer.includes(:billing_information).vigentes.where.not(id: 1)
  17. end
  18. def debtors
  19. @customers = Customer.includes(:sales, :credits).vigentes
  20. end
  21. def customer_sales
  22. @custom = Customer.find(params[:customer_id])
  23. @sales = Sale.where(customer_id: @custom.id, saletype: 0).order('created_at desc')
  24. @credit = Credit.where(customer_id: @custom.id)
  25. end
  26. # apartados por cliente
  27. def reserved_sales_by_customer
  28. respond_to do |format|
  29. @customer = Customer.find(params[:customer_id])
  30. @sales = Sale.where(customer_id: @customer.id, saletype: 2, status: 3)
  31. format.js
  32. end
  33. end
  34. # GET /customers/1
  35. # GET /customers/1.json
  36. def show
  37. @sales = Sale.where(customer_id: @customer.id, saletype: 0).order('created_at desc')
  38. @credit = Credit.where(customer_id: @customer.id)
  39. end
  40. # GET /customers/new
  41. def new
  42. @customer = Customer.new
  43. @customer.credit = false
  44. end
  45. # GET /customers/1/edit
  46. def edit
  47. end
  48. # POST /customers
  49. # POST /customers.json
  50. def create
  51. @customer = Customer.new(customer_params)
  52. @customer.audit_comment = "El cliente " + @customer.nick_name + " fue registrado."
  53. respond_to do |format|
  54. if @customer.save
  55. format.html { redirect_to customers_url, success: "El cliente " + @customer.nick_name + " fue registrado." }
  56. format.json { render :show, status: :created, location: @customer }
  57. else
  58. format.html { render :new }
  59. format.json { render json: @customer.errors, status: :unprocessable_entity }
  60. end
  61. format.js
  62. end
  63. end
  64. # PATCH/PUT /customers/1
  65. # PATCH/PUT /customers/1.json
  66. def update
  67. respond_to do |format|
  68. @customer.audit_comment = "El cliente " + params[:customer][:nick_name] + " fue registrado."
  69. if @customer.update(customer_params)
  70. format.html { redirect_to customers_url, success: "El cliente " + @customer.nick_name + " fue modificado." }
  71. format.json { render :show, status: :ok, location: @customer }
  72. else
  73. format.html { render :edit }
  74. format.json { render json: @customer.errors, status: :unprocessable_entity }
  75. end
  76. end
  77. end
  78. def update_status
  79. customer = Customer.find(params[:customer_id])
  80. if customer.active?
  81. customer.status = 2
  82. elsif customer.inactive?
  83. customer.status = 1
  84. end
  85. respond_to do |format|
  86. if customer.save(validate: false)
  87. format.html { redirect_to customers_url, warning: "El cliente " + customer.nick_name + " fue "+ (customer.active? ? "activado" : "desactivado") + "." }
  88. format.json { head :no_content }
  89. else
  90. format.html { redirect_to customers_url }
  91. format.json { render json: @customer.errors, status: :unprocessable_entity }
  92. end
  93. end
  94. end
  95. # DELETE /customers/1
  96. # DELETE /customers/1.json
  97. def destroy
  98. respond_to do |format|
  99. if @customer.update_attributes(status: :erased)
  100. @customer.audit_comment = "El cliente " + @customer.nick_name + " fue dado de baja."
  101. format.html { redirect_to customers_url, warning: "El cliente " + @customer.nick_name + " fue dado de baja." }
  102. format.json { head :no_content }
  103. end
  104. end
  105. end
  106. private
  107. # Use callbacks to share common setup or constraints between actions.
  108. def set_customer
  109. @customer = Customer.find(params[:id])
  110. end
  111. def get_filters
  112. if params[:current_page].blank?
  113. @current_page = 1
  114. else
  115. @current_page = params[:current_page]
  116. end
  117. @filter = params[:filter]
  118. end
  119. # Never trust parameters from the scary internet, only allow the white list through.
  120. def customer_params
  121. params.require(:customer).permit(:nick_name, :phone, :email, :credit, :credit_limit, :time_limit, :notes, :status, billing_information_attributes:[ :id, :name, :rfc, :address, :num_ext, :num_int, :zipcode, :state_id, :county_id, :city, :suburb], contact_attributes:[ :id, :name, :last_name, :phone, :email ] )
  122. end
  123. end