suppliers_controller.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. class SuppliersController < ApplicationController
  2. ##--- Abilities
  3. load_and_authorize_resource
  4. ##--- Breadcrum_rails
  5. add_breadcrumb I18n.t("breadcrumbs." + controller_name), :suppliers_path
  6. add_breadcrumb "Nuevo Proveedor", :new_supplier_path, only: :new
  7. add_breadcrumb "Detalle del Proveedor", :supplier_path, only: :show
  8. add_breadcrumb "Editar Proveedor", :edit_supplier_path, only: :edit
  9. before_action :set_supplier, only: [:show, :edit, :update, :destroy]
  10. before_action :get_filters, only: [:index, :show, :edit, :new]
  11. # GET /suppliers
  12. # GET /suppliers.json
  13. def index
  14. @suppliers = Supplier.vigentes.includes(:billing_information)
  15. end
  16. # GET /suppliers/1
  17. # GET /suppliers/1.json
  18. def show; end
  19. # GET /suppliers/new
  20. def new
  21. @supplier = Supplier.new
  22. end
  23. # GET /suppliers/1/edit
  24. def edit; end
  25. # POST /suppliers
  26. # POST /suppliers.json
  27. def create
  28. @supplier = Supplier.new(supplier_params)
  29. @supplier.audit_comment = "El proveedor " + @supplier.nick_name + " fue registrado."
  30. respond_to do |format|
  31. if @supplier.save
  32. format.html { redirect_to suppliers_url, success: "El proveedor " + @supplier.nick_name + " fue registrado." }
  33. format.json { render :show, status: :created, location: @supplier }
  34. else
  35. format.html { render :new }
  36. format.json { render json: @supplier.errors, status: :unprocessable_entity }
  37. end
  38. end
  39. end
  40. # PATCH/PUT /suppliers/1
  41. # PATCH/PUT /suppliers/1.json
  42. def update
  43. respond_to do |format|
  44. @supplier.audit_comment = "El proveedor " + params[:supplier][:nick_name] + " fue modificado."
  45. if @supplier.update(supplier_params)
  46. format.html { redirect_to suppliers_url, success: "El proveedor " + @supplier.nick_name + " fue modificado." }
  47. format.json { render :show, status: :ok, location: @supplier }
  48. else
  49. format.html { render :edit }
  50. format.json { render json: @supplier.errors, status: :unprocessable_entity }
  51. end
  52. end
  53. end
  54. def update_status
  55. supplier = Supplier.find(params[:supplier_id])
  56. if supplier.active?
  57. supplier.status = 2
  58. elsif supplier.inactive?
  59. supplier.status = 1
  60. end
  61. respond_to do |format|
  62. if supplier.save(:validate => false)
  63. format.html { redirect_to suppliers_url, warning: "El proveedor " + supplier.nick_name + " fue "+ (supplier.active? ? "activado" : "desactivado")+"." }
  64. format.json { head :no_content }
  65. else
  66. format.html { redirect_to suppliers_url }
  67. format.json { render json: @supplier.errors, status: :unprocessable_entity }
  68. end
  69. end
  70. end
  71. # DELETE /suppliers/1
  72. # DELETE /suppliers/1.json
  73. def destroy
  74. respond_to do |format|
  75. if @supplier.update_attributes(status: 'erased')
  76. @supplier.contact.update_attributes(status: 'erased') if @supplier.contact.present?
  77. @supplier.billing_information.update_attributes(status: 'erased') if @supplier.billing_information.present?
  78. @supplier.audit_comment = "El proveedor " + @supplier.nick_name + " fue dado de baja."
  79. format.html { redirect_to suppliers_url, warning: "El proveedor " + @supplier.nick_name + " fue dado de baja." }
  80. format.json { head :no_content }
  81. end
  82. end
  83. end
  84. private
  85. # Use callbacks to share common setup or constraints between actions.
  86. def set_supplier
  87. @supplier = Supplier.find(params[:id])
  88. end
  89. def get_filters
  90. if params[:current_page].blank?
  91. @current_page = 1
  92. else
  93. @current_page = params[:current_page]
  94. end
  95. @filter = params[:filter]
  96. end
  97. # Never trust parameters from the scary internet, only allow the white list through.
  98. def supplier_params
  99. params.require(:supplier).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 ] )
  100. end
  101. end