diff --git a/app/assets/stylesheets/admin.css.scss b/app/assets/stylesheets/admin.css.scss index 5438d6f..416dc66 100644 --- a/app/assets/stylesheets/admin.css.scss +++ b/app/assets/stylesheets/admin.css.scss @@ -36,20 +36,31 @@ .customer_table{ td, th{ - vertical-align:middle !important; } } -.admin_table{ +.admin-table{ td, th{ - vertical-align:middle !important; - } - + } } +.table tbody tr.success td { + background-color: #dff0d8 !important; +} +.table tbody tr.danger td { + background-color: #f2dede !important; +} +.table tbody tr.info td { + background-color: #d9edf7 !important; +} +.table tbody tr.warning td { + background-color: lightgoldenrodyellow !important; +} + + #admin_nav{ border-radius:0px; @@ -709,7 +720,7 @@ margin-top:0; .table-striped tr:nth-child(odd) td, .table-striped tr:nth-child(odd) th{ -background:rgba(235,244,250,1) !important; + background:rgba(235,244,250,1) !important; } diff --git a/app/controllers/admin/needs_controller.rb b/app/controllers/admin/needs_controller.rb index 47d71d3..bc23bc6 100644 --- a/app/controllers/admin/needs_controller.rb +++ b/app/controllers/admin/needs_controller.rb @@ -55,8 +55,8 @@ class Admin::NeedsController < ApplicationController "Les plus récents" => 'created-desc', "Les plus populaires" => 'wishes-desc', "Les plus commentés" => 'comments-desc', - "Alphabétique (de A à Z)" => 'alpha-asc', - "Alphabétique (de Z à A)" => 'alpha-desc' + "Titre (de A à Z)" => 'alpha-asc', + "Titre (de Z à A)" => 'alpha-desc' } end @@ -69,7 +69,7 @@ class Admin::NeedsController < ApplicationController def create @need = Need.new(need_params) - if @need.update_attributes(need_params) + if @need.save flash[:notice] = "Besoin créé avec succès." @need.validate! redirect_to admin_needs_path @@ -144,12 +144,17 @@ class Admin::NeedsController < ApplicationController def accept @need = Need.find(params[:id]) - if @need.reject! - flash[:notice] = "Le besoin est maintenant négocié" + if @need.offers.length < 1 + flash[:error] = "Vous devez créer au moins une offre avant de passer ce besoin en négocié" else - flash[:error] = "L'état actuel de ce besoin ne permet pas cette action" + if @need.accept! + flash[:notice] = "Le besoin est maintenant négocié" + else + flash[:error] = "L'état actuel de ce besoin ne permet pas cette action" + end end - redirect_to new_admin_need_offer_path(@need) + + redirect_to admin_needs_path end private diff --git a/app/controllers/admin/offers_controller.rb b/app/controllers/admin/offers_controller.rb index ac63498..03c9fb7 100644 --- a/app/controllers/admin/offers_controller.rb +++ b/app/controllers/admin/offers_controller.rb @@ -4,6 +4,55 @@ class Admin::OffersController < ApplicationController def index + # filters default value + params[:o] ||= 'created-desc' + params[:r] ||= 10 + + if params[:need_id] + @need = Need.find(params[:need_id]) + @offers = @need.offers + else + @offers = Offer.all + end + + if(params[:q] and params[:q] != '') + @offers = @offers.joins(:need).where("needs.title like ? or offers.supplier like ?" ,"%#{params[:q]}%","%#{params[:q]}%") + end + + case params[:o] + when 'created-asc' + @offers = @offers.order(created_at: :asc) + when 'created-desc' + @offers = @offers.order(created_at: :desc) + when 'alpha-asc' + @offers = @offers.order(supplier: :asc) + when 'alpha-desc' + @offers = @offers.order(supplier: :desc) + when 'price-asc' + @offers = @offers.order(price: :asc) + when 'price-desc' + @offers = @offers.order(price: :desc) + when 'need-alpha-asc' + @offers = @offers.joins(:need).order('needs.title asc') + when 'need-alpha-desc' + @offers = @offers.joins(:need).order('needs.title desc') + end + + @orders = { + "Les plus récentes" => 'alpha-asc', + "Fournisseurs (de A à Z)" => 'alpha-asc', + "Fournisseurs (de Z à A)" => 'alpha-desc', + "Besoin (de A à Z)" => 'need-alpha-asc', + "Besoin (de Z à A)" => 'need-alpha-desc', + "Prix négociés croissants" => 'price-asc', + "Prix négociés décroissants" => 'price-desc' + } + + + @offers = @offers.page(params[:page]).per(params[:r]) + + + end def new @@ -12,19 +61,58 @@ class Admin::OffersController < ApplicationController end def create - + @need = Need.find(params[:need_id]) + @offer = Offer.new(offer_params) + if @offer.save + @need.offers << @offer + flash[:notice] = "Offre créée avec succès." + @offer.validate! + redirect_to admin_need_offers_path(@need) + else + render :action => "new" + end end def edit - - + @offer = Offer.find(params[:id]) + if params[:need_id] + @need = Need.find(params[:need_id]) + end end def update + @offer = Offer.find(params[:id]) + if @offer.update_attributes(offer_params) + flash[:notice] = "Offre modifiée avec succès." + if params[:need_id] + @need = Need.find(params[:need_id]) + redirect_to admin_need_offers_path(@need) + else + redirect_to admin_offers_path + end + else + render :action => "edit" + end + + end def destroy + @offer = Offer.find(params[:id]) + + if(@offer.destroy) + flash[:notice] = "Offre supprimée avec succès." + else + flash[:error] = "Impossible de supprimer cette offre" + end + + if params[:need_id] + @need = Need.find(params[:need_id]) + redirect_to admin_need_offers_path(@need) + else + redirect_to admin_offers_path + end end @@ -32,8 +120,8 @@ class Admin::OffersController < ApplicationController private - def order_params - params.require(:order).permit(:title, :description, :category_id, :author_id) + def offer_params + params.require(:offer).permit(:supplier, :price, :fee_percentage) end end diff --git a/app/models/need.rb b/app/models/need.rb index 705c65a..0b45d72 100644 --- a/app/models/need.rb +++ b/app/models/need.rb @@ -29,7 +29,7 @@ class Need < ActiveRecord::Base has_many :wishes, dependent: :destroy has_many :customers, -> { uniq }, through: :wishes has_many :messages, dependent: :destroy - has_many :offers + has_many :offers, dependent: :destroy belongs_to :category, class_name: "NeedCategory" validates :title, :presence => true, length: {within: 4..128} @@ -112,7 +112,7 @@ class Need < ActiveRecord::Base when 'negociating' "En négociation" when 'negociated' - "Négociation effecutée" + "Négociation terminée" when 'failed' "Négociation échouée" else diff --git a/app/models/offer.rb b/app/models/offer.rb index a676208..d18f882 100644 --- a/app/models/offer.rb +++ b/app/models/offer.rb @@ -1,4 +1,16 @@ class Offer < ActiveRecord::Base + acts_as_paranoid + belongs_to :need + + validates :supplier, :presence => true, length: {within: 1..128} + validates :price, + :presence => true, + numericality: {greater_than_or_equal_to: 0} + validates :fee_percentage, + :presence => true, + numericality: {:greater_than_or_equal_to => 0, :less_than_or_equal_to => 100} + + end diff --git a/app/views/admin/needs/_need.html.haml b/app/views/admin/needs/_need.html.haml index d9f1cdf..3674eb5 100644 --- a/app/views/admin/needs/_need.html.haml +++ b/app/views/admin/needs/_need.html.haml @@ -1,7 +1,7 @@ -css_class = "warning" if need.negociating? --css_class = "error" if need.failed? +-css_class = "danger" if need.failed? -css_class = "success" if need.negociated? -%tr{:id => need.id} +%tr{:id => need.id, class: css_class} %td =link_to need.title, edit_admin_need_path(need) %td @@ -19,8 +19,8 @@    =link_to i(:"comment-o") + " " + need.messages.length.to_s, admin_need_messages_path(need) %td{style: 'text-align:center' } - - %td{class: css_class} + =link_to i(:"gift") + " " + need.offers.length.to_s, admin_need_offers_path(need) + %td =need.human_state %td.actions{:style => "width:150px;text-align:right"} -if(need.verified?) diff --git a/app/views/admin/needs/index.html.haml b/app/views/admin/needs/index.html.haml index 9160cb3..dcb188a 100644 --- a/app/views/admin/needs/index.html.haml +++ b/app/views/admin/needs/index.html.haml @@ -9,7 +9,7 @@ Si pour une raison ou pour une autre le besoin ne respecte pas la charte, cliquez sur le bouton - %table.table.admin_table.table-hover.table-striped + %table.table.admin-table.table-hover.table-striped %thead.rows_header %tr @@ -49,7 +49,7 @@ = link_to "Créer un besoin", new_admin_need_path, class:"btn btn-primary" .col-md-10 - %table.table.admin_table.table-hover.table-striped + %table.table.admin-table.table-hover.table-striped %thead.rows_header %tr diff --git a/app/views/admin/offers/_form.html.haml b/app/views/admin/offers/_form.html.haml new file mode 100644 index 0000000..41def45 --- /dev/null +++ b/app/views/admin/offers/_form.html.haml @@ -0,0 +1,9 @@ +=semantic_form_for [:admin, @need, @offer] do |f| + .content + + =f.inputs do + =f.input :supplier, :label => "Nom du fournisseur : " + =f.input :price, :label => "Prix négocié : " + =f.input :fee_percentage, :label => "% de frais : " + + .actions= f.submit "Sauvegarder", :class => "btn btn-primary" diff --git a/app/views/admin/offers/_offer.html.haml b/app/views/admin/offers/_offer.html.haml new file mode 100644 index 0000000..e136294 --- /dev/null +++ b/app/views/admin/offers/_offer.html.haml @@ -0,0 +1,17 @@ + +%tr{:id => offer.id} + %td + = link_to offer.need.title, edit_admin_need_path(offer.need) + %td + =offer.supplier + %td + =number_to_currency(offer.price, locale: :fr) + %td + =number_to_percentage(offer.fee_percentage, significant: true ) + %td.actions{:style => "width:150px;text-align:right"} + -if @need + = link_to i(:"trash"), [:admin,@need, offer], :data => {:confirm => 'Voulez-vous vraiment supprimer cette offre ?'}, :method => :delete + = link_to i(:pencil), edit_admin_need_offer_path(@need, offer) + -else + = link_to i(:"trash"), [:admin, offer], :data => {:confirm => 'Voulez-vous vraiment supprimer cette offre ?'}, :method => :delete + = link_to i(:pencil), edit_admin_offer_path(offer) diff --git a/app/views/admin/offers/edit.html.haml b/app/views/admin/offers/edit.html.haml new file mode 100644 index 0000000..812d77d --- /dev/null +++ b/app/views/admin/offers/edit.html.haml @@ -0,0 +1,4 @@ +%h1 + Modifier une offre + +=render :partial => "form" diff --git a/app/views/admin/offers/index.html.haml b/app/views/admin/offers/index.html.haml index e69de29..723a7ef 100644 --- a/app/views/admin/offers/index.html.haml +++ b/app/views/admin/offers/index.html.haml @@ -0,0 +1,68 @@ +-if @need + %h1 + Gestion des offres pour le besoin + %strong= @need.title + %p.alert.alert-info + Seul les offres concernant le besoin + %strong= @need.title + sont affichées sur cette page. + %br + Pour voir toutes les offres, aller dans l'onglet + %strong Gestion des offres + depuis la barre de navigation +-else + %h1 + Gestion des offres + %p.alert.alert-info + Cette page affiche toutes les offres existantes. + %br + Pour créer une offre, il faut d'abord choisir un besoin. Pour cela, passer par l'onglet + %strong Gestion des besoins + puis cliquer sur le bouton + %strong= i(:gift) + en face du besoin de votre choix. + +-if @offers.length < 1 + Aucune offre actuellement +-else + .row + .col-md-2 + = semantic_form_for :search, :html => {id: :search_form, :method => :get } do |f| + = f.inputs do + = f.inputs do + =f.input :q, :as => :search, label: "Recherche", input_html: {value: params[:q], :name => 'q' }, placeholder: "Besoin ou fournisseur" + .clear + = f.input :o, as: :order, selected: params[:o], input_html: {:name => 'o' }, label: "Ordre d'affichage", :include_blank => false , :as => :select, :collection => @orders + .clear + = f.input :r, as: :result, selected: params[:r], input_html: {:name => 'r' }, label: 'Résultats par page', :include_blank => false , :as => :select, :collection => [10,20,50,100] + .clear + + .col-md-10 + %table.table.admin-table.table-hover.table-striped + %thead.rows_header + + %tr + %th + Besoin + %th + Fournisseur + %th + Prix négocié + %th + \% de frais + %th{:style => "width:100px"} +   + + %tbody.rows + + =render @offers + + .pagination.pull-right= paginate @offers + %br + %br +-if @need + =link_to "Ajouter une offre", new_admin_need_offer_path(@need),class:"btn btn-primary" + +:javascript + $('#search_o').change(function(){$('#search_form').submit()}) + $('#search_r').change(function(){$('#search_form').submit()}) diff --git a/app/views/admin/offers/new.html.haml b/app/views/admin/offers/new.html.haml index e69de29..d22684c 100644 --- a/app/views/admin/offers/new.html.haml +++ b/app/views/admin/offers/new.html.haml @@ -0,0 +1,4 @@ +%h1 + Créer une offre pour le besoin + %strong= @need.title +=render :partial => "form" diff --git a/app/views/layouts/admin.html.haml b/app/views/layouts/admin.html.haml index f1fe23d..e63d98c 100644 --- a/app/views/layouts/admin.html.haml +++ b/app/views/layouts/admin.html.haml @@ -47,6 +47,7 @@ %li= link_to " Gestion des besoins", admin_needs_path %li= link_to " Gestion des catégories", admin_need_categories_path + %li= link_to " Gestion des offres", admin_offers_path diff --git a/config/routes.rb b/config/routes.rb index 08c05b9..7974439 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -253,6 +253,7 @@ Rails.application.routes.draw do get :reject end end + resources :offers resources :customers do member do diff --git a/db/migrate/20151209125427_create_offers.rb b/db/migrate/20151209125427_create_offers.rb index 0b8223a..47c5fc6 100644 --- a/db/migrate/20151209125427_create_offers.rb +++ b/db/migrate/20151209125427_create_offers.rb @@ -5,7 +5,7 @@ class CreateOffers < ActiveRecord::Migration t.timestamps null: false t.float :price t.float :fee_percentage - t.references :needs, index: true + t.references :need, index: true t.string :supplier end end diff --git a/db/migrate/20151210134428_add_deleteted_at_to_offers.rb b/db/migrate/20151210134428_add_deleteted_at_to_offers.rb new file mode 100644 index 0000000..2bf9eca --- /dev/null +++ b/db/migrate/20151210134428_add_deleteted_at_to_offers.rb @@ -0,0 +1,6 @@ +class AddDeletetedAtToOffers < ActiveRecord::Migration + def change + add_column :offers, :deleted_at, :datetime + add_index :offers, :deleted_at + end +end diff --git a/db/schema.rb b/db/schema.rb index 6052ebc..ef5b249 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20151209125427) do +ActiveRecord::Schema.define(version: 20151210134428) do create_table "admins", force: :cascade do |t| t.string "name", limit: 255 @@ -440,11 +440,13 @@ ActiveRecord::Schema.define(version: 20151209125427) do t.datetime "updated_at", null: false t.float "price", limit: 24 t.float "fee_percentage", limit: 24 - t.integer "needs_id", limit: 4 + t.integer "need_id", limit: 4 t.string "supplier", limit: 255 + t.datetime "deleted_at" end - add_index "offers", ["needs_id"], name: "index_offers_on_needs_id", using: :btree + add_index "offers", ["deleted_at"], name: "index_offers_on_deleted_at", using: :btree + add_index "offers", ["need_id"], name: "index_offers_on_need_id", using: :btree create_table "pages", force: :cascade do |t| t.text "title", limit: 65535