customers_controller.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 = Credit.joins(:customer).activos.select("customers.*").uniq
  20. # @customers = Customer.where("customers.status != 0").includes(:sales, :credits).order('credits.id DESC')
  21. end
  22. def customer_sales
  23. @customer = Customer.includes(:billing_information, :credits, :sales).find(params[:customer_id])
  24. # @sales = Sale.where(customer_id: @custom.id, saletype: 0).order('created_at desc')
  25. @sales = @customer.sales.includes(:seller, :credit)
  26. @credits = @customer.credits
  27. # @abonos = @customer.credit_payments.activos.select("credit_payments.*, sales.sale_code").order("credit_payments.created_at DESC")
  28. # @credit = Credit.where(customer_id: @custom.id)
  29. end
  30. # apartados por cliente
  31. def reserved_sales_by_customer
  32. respond_to do |format|
  33. @customer = Customer.find(params[:customer_id])
  34. @sales = Sale.where(customer_id: @customer.id, saletype: 2, status: 3)
  35. format.js
  36. end
  37. end
  38. # GET /customers/1
  39. # GET /customers/1.json
  40. def show
  41. @sales = Sale.where(customer_id: @customer.id, saletype: 0).order('created_at desc')
  42. @credit = Credit.where(customer_id: @customer.id)
  43. end
  44. # GET /customers/new
  45. def new
  46. @customer = Customer.new
  47. @customer.credit = false
  48. end
  49. # GET /customers/1/edit
  50. def edit; end
  51. # POST /customers
  52. # POST /customers.json
  53. def create
  54. @customer = Customer.new(customer_params)
  55. @customer.audit_comment = "El cliente " + @customer.nick_name + " fue registrado."
  56. respond_to do |format|
  57. if @customer.save
  58. format.html { redirect_to customers_url, success: "El cliente " + @customer.nick_name + " fue registrado." }
  59. format.json { render :show, status: :created, location: @customer }
  60. else
  61. format.html { render :new }
  62. format.json { render json: @customer.errors, status: :unprocessable_entity }
  63. end
  64. format.js
  65. end
  66. end
  67. # PATCH/PUT /customers/1
  68. # PATCH/PUT /customers/1.json
  69. def update
  70. respond_to do |format|
  71. @customer.audit_comment = "El cliente " + params[:customer][:nick_name] + " fue registrado."
  72. if @customer.update(customer_params)
  73. format.html { redirect_to customers_url, success: "El cliente " + @customer.nick_name + " fue modificado." }
  74. format.json { render :show, status: :ok, location: @customer }
  75. else
  76. format.html { render :edit }
  77. format.json { render json: @customer.errors, status: :unprocessable_entity }
  78. end
  79. end
  80. end
  81. def update_status
  82. customer = Customer.find(params[:customer_id])
  83. if customer.active?
  84. customer.status = 2
  85. elsif customer.inactive?
  86. customer.status = 1
  87. end
  88. respond_to do |format|
  89. if customer.save(validate: false)
  90. format.html { redirect_to customers_url, warning: "El cliente " + customer.nick_name + " fue " + (customer.active? ? "activado" : "desactivado") + "." }
  91. format.json { head :no_content }
  92. else
  93. format.html { redirect_to customers_url }
  94. format.json { render json: @customer.errors, status: :unprocessable_entity }
  95. end
  96. end
  97. end
  98. # DELETE /customers/1
  99. # DELETE /customers/1.json
  100. def destroy
  101. respond_to do |format|
  102. if @customer.update_attributes(status: :erased)
  103. @customer.audit_comment = "El cliente " + @customer.nick_name + " fue dado de baja."
  104. format.html { redirect_to customers_url, warning: "El cliente " + @customer.nick_name + " fue dado de baja." }
  105. format.json { head :no_content }
  106. end
  107. end
  108. end
  109. private
  110. # Use callbacks to share common setup or constraints between actions.
  111. def set_customer
  112. @customer = Customer.find(params[:id])
  113. end
  114. def get_filters
  115. @current_page = params[:current_page].blank? ? 1 : params[:current_page]
  116. @filter = params[:filter]
  117. end
  118. # Never trust parameters from the scary internet, only allow the white list through.
  119. def customer_params
  120. 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, :cfdi_use_key], contact_attributes: [:id, :name, :last_name, :phone, :email])
  121. end
  122. end