suppliers_controller.rb 3.9 KB

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