From 43efa302502299d6b1c8eb7b6651811b025601e5 Mon Sep 17 00:00:00 2001 From: Nicolas Bally Date: Tue, 26 Mar 2019 10:36:34 +0100 Subject: [PATCH] suite --- .../public/contact_products_controller.rb | 26 +++++++++++ app/controllers/public/contacts_controller.rb | 32 +++++++++---- app/models/contact.rb | 36 +++++++++++++-- app/models/contact_product.rb | 6 +++ app/views/admin/d_products/_form.html.haml | 4 ++ app/views/public/contacts/_form.html.haml | 45 ++++++++++++++++++- app/views/public/contacts/_thank.html.haml | 16 +++++-- app/views/public/contacts/create.js.erb | 1 - app/views/public/contacts/new.html.haml | 10 +++-- app/views/public/d_products/show.html.haml | 3 ++ app/views/public/shared/_first_menu.html.haml | 2 +- config/routes.rb | 1 + .../20190325195120_create_contact_products.rb | 15 +++++++ ...90325230219_add_orderable_to_d_products.rb | 6 +++ db/schema.rb | 21 ++++++++- test/fixtures/contact_products.yml | 19 ++++++++ test/models/contact_product_test.rb | 7 +++ 17 files changed, 227 insertions(+), 23 deletions(-) create mode 100644 app/controllers/public/contact_products_controller.rb create mode 100644 app/models/contact_product.rb create mode 100644 db/migrate/20190325195120_create_contact_products.rb create mode 100644 db/migrate/20190325230219_add_orderable_to_d_products.rb create mode 100644 test/fixtures/contact_products.yml create mode 100644 test/models/contact_product_test.rb diff --git a/app/controllers/public/contact_products_controller.rb b/app/controllers/public/contact_products_controller.rb new file mode 100644 index 0000000..c3546b1 --- /dev/null +++ b/app/controllers/public/contact_products_controller.rb @@ -0,0 +1,26 @@ +class Public::ContactProductsController < ApplicationController + + layout :get_public_layout + + + + def new + session[:product_ids] = [] if !session[:product_ids] + + session[:product_ids] << params[:token] + + session[:product_ids] = session[:product_ids].uniq + + redirect_to new_public_contact_path(:raison_id => 4) + end + + def destroy + session[:product_ids] = [] if !session[:product_ids] + + session[:product_ids].delete(params[:id]) + + redirect_to new_public_contact_path(:raison_id => 4) + + end + +end diff --git a/app/controllers/public/contacts_controller.rb b/app/controllers/public/contacts_controller.rb index 440f991..a3569da 100755 --- a/app/controllers/public/contacts_controller.rb +++ b/app/controllers/public/contacts_controller.rb @@ -6,11 +6,19 @@ class Public::ContactsController < ApplicationController end def new - @contact = Contact.new(:raison_id => params[:raison_id], :survey_set_id => params[:id]) + @contact = Contact.new(:raison_id => params[:raison_id], :survey_set_id => params[:id], :provenance_id => 3) - if @contact.raison_id == 3 and session[:order_document_ids] and session[:order_document_ids].size > 0 - session[:order_document_ids].each do |token| - @contact.document_orders << DocumentOrder.new(:data_file => DataFile.find_by_token(token)) + session[:product_qtes] = session[:product_qtes] || {} + + + + if @contact.raison_id == 4 and session[:product_ids] and session[:product_ids].size > 0 + session[:product_ids].each do |token| + + + + @contact.contact_products << ContactProduct.new(:d_product => DProduct.find(token), :qte => session[:product_qtes][token]) + end end @@ -19,12 +27,20 @@ class Public::ContactsController < ApplicationController def create @contact = Contact.new(params.require(:contact).permit!) - + if @contact.raison_id == 4 + + @contact.contact_products.each do |contact_product| + + session[:product_qtes][contact_product.d_product_id.to_s] = contact_product.qte + + + end + end if @contact.save - if @contact.raison_id == 3 - @contact.notes = session[:order_document_ids].join(",") - session[:order_document_ids] = [] + if @contact.raison_id == 4 + @contact.notes = session[:product_ids].join(",") + session[:product_ids] = [] end @contact.save QuestionMailer.send_contact(@contact).deliver diff --git a/app/models/contact.rb b/app/models/contact.rb index 74ec773..1cb7a13 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -3,12 +3,40 @@ class Contact < ActiveRecord::Base belongs_to :admin has_many :contact_actions validates :name, :presence => true - validates :email, :presence => true, :format => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i + #validates :email, :presence => true, :if => :email_needed? + validates :email, :format => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :if => :email_needed? #validates :message, :presence => true - validates :phone, :presence => true + #validates :phone, :presence => true has_many :contact_files + validates :address, :presence => true, :if => :address_needed? + validates :cp, :presence => true, :if => :address_needed? + validates :city, :presence => true, :if => :address_needed? + + has_many :contact_products + has_many :d_products, :through => :contact_products + + accepts_nested_attributes_for :contact_products + + def email_needed? + true if self.email? + end + + def address_needed? + true if self.raison_id == 4 + end + + before_validation do + if (true or ((raison_id == 4 )) and !self.email? and !self.phone?) + errors.add(:email, "Vous devez indiquer votre numéro de téléphone et/ou votre email") + errors.add(:phone, "Vous devez indiquer votre numéro de téléphone et/ou votre email") + + end + + + + end @@ -31,8 +59,8 @@ class Contact < ActiveRecord::Base :provenance_id => self.provenance_id } - api_url = "https://groupe-payre.fr/admin/contacts/api" - #api_url ="http://localhost:3030/admin/contacts/api" + #api_url = "https://groupe-payre.fr/admin/contacts/api" + api_url ="http://localhost:3030/admin/contacts/api" @c = Curl::Easy.new(api_url) do |curl| curl.verbose = true diff --git a/app/models/contact_product.rb b/app/models/contact_product.rb new file mode 100644 index 0000000..99593bd --- /dev/null +++ b/app/models/contact_product.rb @@ -0,0 +1,6 @@ +class ContactProduct < ActiveRecord::Base + belongs_to :contact + belongs_to :d_product + + validates :qte, :presence => true +end diff --git a/app/views/admin/d_products/_form.html.haml b/app/views/admin/d_products/_form.html.haml index b00c0fd..091e51b 100644 --- a/app/views/admin/d_products/_form.html.haml +++ b/app/views/admin/d_products/_form.html.haml @@ -9,6 +9,10 @@ =form.input :name, :label => "Nom :" =form.input :description, :label => "Description :" + =form.input :orderable, :label => "Demande de devis en ligne ?" + + =form.input :unit, :label => "Unité :" + .actions diff --git a/app/views/public/contacts/_form.html.haml b/app/views/public/contacts/_form.html.haml index aa7735b..a959ba2 100755 --- a/app/views/public/contacts/_form.html.haml +++ b/app/views/public/contacts/_form.html.haml @@ -4,7 +4,32 @@ =f.inputs do =f.hidden_field :provenance_id - + =f.hidden_field :raison_id + + -if @contact.raison_id == 4 + -p_ids = [] + %table + =f.semantic_fields_for :contact_products do |f| + %tr + %td + =f.hidden_field :d_product_id + =image_tag f.object.d_product.icon.file.url + %td + =f.input :qte, :label => "Quantité souhaitée (#{f.object.d_product.unit}) :" + -p_ids << f.object.d_product_id + =link_to "supprimer ce produit", public_contact_product_path(:id => f.object.d_product_id), :method => :delete + + + -if p_ids.size < DProduct.where(:orderable => true).where("icon_id is not null").count.to_i + %h3 + Demander un devis pour les produits suivants également : + #products + -DProduct.where(:orderable => true).where("icon_id is not null").where("id not in (?)", p_ids).order(:name).each do |d_product| + =link_to new_public_contact_product_path(:token => d_product.id) do + =image_tag d_product.icon.file.url + + + %table{:style => "border-collapse:collapse;width:100%;"} %tr @@ -20,7 +45,23 @@ %tr %td{:colspan => 2} =f.input :corporate, :label => false, :placeholder => qit("contact organisation","Organisation*") - + + -if @contact.raison_id == 4 + %tr + %td{:colspan => 2} + =f.input :address, :label => false, :placeholder => "Adresse*" + %tr + %td{:colspan => 2} + =f.input :address2, :label => false, :placeholder => "Adresse (suite)" + + %tr + %td{:style => "width:50%;vertical-align:top;padding-right:6px;"} + + =f.input :cp, :label => false, :placeholder => "Code postal*" + + %td{:style => "width:50%;vertical-align:top;padding-left:6px;"} + =f.input :city, :label => false, :placeholder => "Ville*" + %tr %td{:style => "width:50%;vertical-align:top;padding-right:6px;"} diff --git a/app/views/public/contacts/_thank.html.haml b/app/views/public/contacts/_thank.html.haml index b55bdc9..db7d901 100755 --- a/app/views/public/contacts/_thank.html.haml +++ b/app/views/public/contacts/_thank.html.haml @@ -1,7 +1,17 @@ -.submit{:style => "color:white;text-align:left;min-height:300px;"} - %h3 Merci pour votre message ! +-if @contact.raison_id == 4 + .submit{:style => "text-align:left;min-height:300px;"} - %p Nous mettons tout en œuvre pour vous répondre dans les meilleurs délais. + %h3 Merci pour votre demande ! + + %p Nous mettons tout en œuvre pour vous répondre dans les meilleurs délais. + + +-else + .submit{:style => "color:white;text-align:left;min-height:300px;"} + + %h3 Merci pour votre message ! + + %p Nous mettons tout en œuvre pour vous répondre dans les meilleurs délais. \ No newline at end of file diff --git a/app/views/public/contacts/create.js.erb b/app/views/public/contacts/create.js.erb index dfee6a5..cbc2c79 100755 --- a/app/views/public/contacts/create.js.erb +++ b/app/views/public/contacts/create.js.erb @@ -2,6 +2,5 @@ $("#contact_form").html("<%= escape_javascript(render(:partial => "inline_thank")) %>"); <% else %> $("#form").html("<%= escape_javascript(render(:partial => "thank")) %>"); - <% end %> diff --git a/app/views/public/contacts/new.html.haml b/app/views/public/contacts/new.html.haml index 0b068eb..381f147 100644 --- a/app/views/public/contacts/new.html.haml +++ b/app/views/public/contacts/new.html.haml @@ -1,10 +1,14 @@ -if !@new_site .contact_page - %h1 Contacter 3P + %h1 Demander un devis - #form=render :partial => "form" - + #contact_form + #form=render :partial => "form" + :scss + .submit{ + color:black !important; + } -else -if @contact.raison_id == 3 diff --git a/app/views/public/d_products/show.html.haml b/app/views/public/d_products/show.html.haml index a9dee29..6270248 100644 --- a/app/views/public/d_products/show.html.haml +++ b/app/views/public/d_products/show.html.haml @@ -16,5 +16,8 @@ .render_block =render @d_product.blocks.find_by_lang_site_id(@lang.id) + + .add_product_to_cart + =link_to "Demander un devis pour ce produit !", new_public_contact_product_path(:token => @d_product.id), :class => "btn btn-primary" diff --git a/app/views/public/shared/_first_menu.html.haml b/app/views/public/shared/_first_menu.html.haml index 2708a6e..512b1f8 100644 --- a/app/views/public/shared/_first_menu.html.haml +++ b/app/views/public/shared/_first_menu.html.haml @@ -2,5 +2,5 @@ -MenuItem.where(:parent_id => nil, :menu_id => 1).order(:position).each do |menu_item| =menu_item_link(menu_item) -=link_to "Demander un devis","#", :class => "devis" +=link_to "Demander un devis",new_public_contact_path(:raison_id => 4), :class => "devis" diff --git a/config/routes.rb b/config/routes.rb index 5f48702..0f57075 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -61,6 +61,7 @@ Rails.application.routes.draw do namespace :public do resources :contacts + resources :contact_products resources :specific_maps resources :specific_map_items diff --git a/db/migrate/20190325195120_create_contact_products.rb b/db/migrate/20190325195120_create_contact_products.rb new file mode 100644 index 0000000..0887dfb --- /dev/null +++ b/db/migrate/20190325195120_create_contact_products.rb @@ -0,0 +1,15 @@ +class CreateContactProducts < ActiveRecord::Migration + def change + create_table :contact_products do |t| + t.references :contact, index: true, foreign_key: true + t.references :d_product, index: true, foreign_key: true + t.integer :qte + t.decimal :price_ht + t.decimal :price_ttc + t.decimal :price_tot_ht + t.decimal :price_tot_ttc + + t.timestamps null: false + end + end +end diff --git a/db/migrate/20190325230219_add_orderable_to_d_products.rb b/db/migrate/20190325230219_add_orderable_to_d_products.rb new file mode 100644 index 0000000..c984d5e --- /dev/null +++ b/db/migrate/20190325230219_add_orderable_to_d_products.rb @@ -0,0 +1,6 @@ +class AddOrderableToDProducts < ActiveRecord::Migration + def change + add_column :d_products, :orderable, :boolean, :default => true + add_column :d_products, :unit, :string, :default => "L" + end +end diff --git a/db/schema.rb b/db/schema.rb index 0da97da..fda7fea 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: 20180801225533) do +ActiveRecord::Schema.define(version: 20190325230219) do create_table "admin_admin_roles", force: :cascade do |t| t.integer "admin_id", limit: 4 @@ -247,6 +247,21 @@ ActiveRecord::Schema.define(version: 20180801225533) do add_index "contact_actions", ["contact_id"], name: "index_contact_actions_on_contact_id", using: :btree + create_table "contact_products", force: :cascade do |t| + t.integer "contact_id", limit: 4 + t.integer "d_product_id", limit: 4 + t.integer "qte", limit: 4 + t.decimal "price_ht", precision: 10 + t.decimal "price_ttc", precision: 10 + t.decimal "price_tot_ht", precision: 10 + t.decimal "price_tot_ttc", precision: 10 + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "contact_products", ["contact_id"], name: "index_contact_products_on_contact_id", using: :btree + add_index "contact_products", ["d_product_id"], name: "index_contact_products_on_d_product_id", using: :btree + create_table "contacts", force: :cascade do |t| t.string "civilite", limit: 255 t.string "firstname", limit: 255 @@ -291,6 +306,8 @@ ActiveRecord::Schema.define(version: 20180801225533) do t.integer "icon_id", limit: 4 t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.boolean "orderable", default: true + t.string "unit", limit: 255, default: "L" end create_table "data_file_categories", force: :cascade do |t| @@ -1085,6 +1102,8 @@ ActiveRecord::Schema.define(version: 20180801225533) do add_foreign_key "articles", "article_authors" add_foreign_key "block_contents", "image_files" add_foreign_key "category_categoryables", "categories" + add_foreign_key "contact_products", "contacts" + add_foreign_key "contact_products", "d_products" add_foreign_key "data_file_passwords", "data_files" add_foreign_key "data_files", "image_files" add_foreign_key "edit_watchers", "admins" diff --git a/test/fixtures/contact_products.yml b/test/fixtures/contact_products.yml new file mode 100644 index 0000000..e57d968 --- /dev/null +++ b/test/fixtures/contact_products.yml @@ -0,0 +1,19 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + contact_id: + d_product_id: + qte: 1 + price_ht: 9.99 + price_ttc: 9.99 + price_tot_ht: 9.99 + price_tot_ttc: 9.99 + +two: + contact_id: + d_product_id: + qte: 1 + price_ht: 9.99 + price_ttc: 9.99 + price_tot_ht: 9.99 + price_tot_ttc: 9.99 diff --git a/test/models/contact_product_test.rb b/test/models/contact_product_test.rb new file mode 100644 index 0000000..bca54d9 --- /dev/null +++ b/test/models/contact_product_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ContactProductTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end