customers_controller.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.where("customers.status != 0").includes(:sales, :credits).order('credits.id DESC')
  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; end
  47. # POST /customers
  48. # POST /customers.json
  49. def create
  50. @customer = Customer.new(customer_params)
  51. @customer.audit_comment = "El cliente " + @customer.nick_name + " fue registrado."
  52. respond_to do |format|
  53. if @customer.save
  54. format.html { redirect_to customers_url, success: "El cliente " + @customer.nick_name + " fue registrado." }
  55. format.json { render :show, status: :created, location: @customer }
  56. else
  57. format.html { render :new }
  58. format.json { render json: @customer.errors, status: :unprocessable_entity }
  59. end
  60. format.js
  61. end
  62. end
  63. # PATCH/PUT /customers/1
  64. # PATCH/PUT /customers/1.json
  65. def update
  66. respond_to do |format|
  67. @customer.audit_comment = "El cliente " + params[:customer][:nick_name] + " fue registrado."
  68. if @customer.update(customer_params)
  69. format.html { redirect_to customers_url, success: "El cliente " + @customer.nick_name + " fue modificado." }
  70. format.json { render :show, status: :ok, location: @customer }
  71. else
  72. format.html { render :edit }
  73. format.json { render json: @customer.errors, status: :unprocessable_entity }
  74. end
  75. end
  76. end
  77. def update_status
  78. customer = Customer.find(params[:customer_id])
  79. if customer.active?
  80. customer.status = 2
  81. elsif customer.inactive?
  82. customer.status = 1
  83. end
  84. respond_to do |format|
  85. if customer.save(validate: false)
  86. format.html { redirect_to customers_url, warning: "El cliente " + customer.nick_name + " fue " + (customer.active? ? "activado" : "desactivado") + "." }
  87. format.json { head :no_content }
  88. else
  89. format.html { redirect_to customers_url }
  90. format.json { render json: @customer.errors, status: :unprocessable_entity }
  91. end
  92. end
  93. end
  94. # DELETE /customers/1
  95. # DELETE /customers/1.json
  96. def destroy
  97. respond_to do |format|
  98. if @customer.update_attributes(status: :erased)
  99. @customer.audit_comment = "El cliente " + @customer.nick_name + " fue dado de baja."
  100. format.html { redirect_to customers_url, warning: "El cliente " + @customer.nick_name + " fue dado de baja." }
  101. format.json { head :no_content }
  102. end
  103. end
  104. end
  105. private
  106. # Use callbacks to share common setup or constraints between actions.
  107. def set_customer
  108. @customer = Customer.find(params[:id])
  109. end
  110. def get_filters
  111. @current_page = params[:current_page].blank? ? 1 : params[:current_page]
  112. @filter = params[:filter]
  113. end
  114. # Never trust parameters from the scary internet, only allow the white list through.
  115. def customer_params
  116. 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])
  117. end
  118. end