diff --git a/app/controllers/public/product_customers_controller.rb b/app/controllers/public/product_customers_controller.rb index 0c8e9d7..0eafd0e 100755 --- a/app/controllers/public/product_customers_controller.rb +++ b/app/controllers/public/product_customers_controller.rb @@ -1,7 +1,7 @@ class Public::ProductCustomersController < ApplicationController layout "public" - before_filter :auth_product_customer, :except => [:new, :create, :confirm] + before_filter :auth_product_customer, :except => [:new, :create, :confirm, :find_parrain] def index @@ -31,6 +31,14 @@ class Public::ProductCustomersController < ApplicationController @product_customer = ProductCustomer.new() + + if cookies[:mlm_token] and @parent = ProductCustomer.find_parrain(cookies[:mlm_token]) + @product_customer.parent_code = @parent.mlm_token.upcase + + elsif params[:p] and @parent = ProductCustomer.find_parrain(params[:p]) + @product_customer.parent_code = @parent.mlm_token.upcase + + end end @@ -112,6 +120,24 @@ class Public::ProductCustomersController < ApplicationController end end end + + def find_parrain + @parent = ProductCustomer.find_parrain(params[:parent_code]) + + if @parent + cookies.permanent[:mlm_token] = @parent.mlm_token + else + cookies.permanent[:mlm_token] = nil + end + render layout: false + end + + + def parrainage + cookies.permanent[:mlm_token] = params[:mlm_token] + redirect_to "/" + end + diff --git a/app/models/product_customer.rb b/app/models/product_customer.rb index 0d3be3c..35cb037 100755 --- a/app/models/product_customer.rb +++ b/app/models/product_customer.rb @@ -16,12 +16,18 @@ class ProductCustomer < ActiveRecord::Base before_create do self.generate_token + self.generate_mlm_token self.particular_bill = Particular.create(:particular_type => "facturation", :skip_validation => true) self.particular_idem = true self.particular_send = Particular.create(:particular_type => "expedition", :skip_validation => true) + end + + def pseudo + self.particular_bill.firstname.to_s+" "+self.particular_bill.name.to_s + end def send_password_reset @@ -43,5 +49,42 @@ class ProductCustomer < ActiveRecord::Base end while ProductCustomer.exists?(:token => self.token) end + + def generate_mlm_token + self.mlm_token = loop do + + mlm_token = SecureRandom.hex(3).upcase + break mlm_token unless ProductCustomer.exists?(mlm_token: mlm_token) + end + end + + + def self.find_parrain(token) + + + if token.to_s != "" + ProductCustomer.find_by_mlm_token(token.upcase) + else + nil + end + end + + before_validation do + + if !self.id + if self.parent_code? + + if mlm_parent = ProductCustomer.find_parrain(self.parent_code) + self.parent_id = mlm_parent.id + self.parent_at_create = true + else + errors.add(:parent_code, "Ce code de recommandation n'est pas valide, merci de vérifier son exactitude. ") + end + end + end + + end + + end diff --git a/app/views/layouts/public.html.haml b/app/views/layouts/public.html.haml index 982b73c..57125fb 100755 --- a/app/views/layouts/public.html.haml +++ b/app/views/layouts/public.html.haml @@ -28,10 +28,7 @@ = stylesheet_link_tag 'public' = stylesheet_link_tag '/fonts/Advent/stylesheet.css' - - - - + =javascript_include_tag "http://maps.google.com/maps/api/js?sensor=false®ion=FR" diff --git a/app/views/public/product_customers/_find_parrain.html.haml b/app/views/public/product_customers/_find_parrain.html.haml new file mode 100644 index 0000000..11ec92b --- /dev/null +++ b/app/views/public/product_customers/_find_parrain.html.haml @@ -0,0 +1,10 @@ +-if @parent + + %p + =qit("recommended-by-form", "Recommandé par") + =":" + + %strong + = @parent.pseudo + %br + %br \ No newline at end of file diff --git a/app/views/public/product_customers/_new_form.haml b/app/views/public/product_customers/_new_form.haml index 58fbb1a..fbdd202 100755 --- a/app/views/public/product_customers/_new_form.haml +++ b/app/views/public/product_customers/_new_form.haml @@ -33,11 +33,29 @@ =qit("your-password","Votre mot de passe") = error + =f.label :parent_code, "Code de recommandation " + %p + =qit "offre-10-credits-offerts-parainnage" do + Profitez d'un bon d'achat de 5€ avec un code de recommandation valide. Si vous n'en avez pas et que quelqu'un vous a parlé du site demandez lui, il en a certainement un ! + .form-group{:class => ("has-error has-feedback" if @product_customer.errors[:parent_code].size > 0)} + =f.text_field :parent_code, :class => "form-control", :placeholder => qit("code-de-recommandation","Code de recommandation"), :onchange => "find_parrain( $(this).val());", :onkeyup => "find_parrain( $(this).val());", :onkeydown => "find_parrain( $(this).val());" + -if @product_customer.errors[:parent_code].uniq + -@product_customer.errors[:parent_code].uniq.each do |error| + .error= error + + + .parent + =render :partial => "public/product_customers/find_parrain" =f.submit qit("account-create-save","Créer mon compte !"), :class => "btn btn-primary order_button" %br %br + + :coffeescript + @find_parrain = (value) -> + $.ajax({url : '/fr/public/product_customers/find_parrain.js', data : {parent_code :value}}) + %br %br diff --git a/app/views/public/product_customers/find_parrain.js.erb b/app/views/public/product_customers/find_parrain.js.erb new file mode 100644 index 0000000..5d9f95c --- /dev/null +++ b/app/views/public/product_customers/find_parrain.js.erb @@ -0,0 +1 @@ +$('.parent').html("<%= escape_javascript(render(:partial => "find_parrain"))%>"); \ No newline at end of file diff --git a/db/migrate/20160428093204_add_mlm_to_product_customers.rb b/db/migrate/20160428093204_add_mlm_to_product_customers.rb new file mode 100644 index 0000000..f510c70 --- /dev/null +++ b/db/migrate/20160428093204_add_mlm_to_product_customers.rb @@ -0,0 +1,9 @@ +class AddMlmToProductCustomers < ActiveRecord::Migration + def change + add_column :product_customers, :parent_id, :integer + add_column :product_customers, :mlm_token, :string + add_column :product_customers, :parent_at_create, :boolean + add_column :product_customers, :parent_code, :string + + end +end diff --git a/db/schema.rb b/db/schema.rb index c125d8e..a71cad8 100755 --- 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: 20160426151106) do +ActiveRecord::Schema.define(version: 20160428093204) do create_table "admins", force: :cascade do |t| t.string "name", limit: 255 @@ -574,6 +574,10 @@ ActiveRecord::Schema.define(version: 20160426151106) do t.boolean "particular_idem", limit: 1, default: false t.integer "particular_bill_id", limit: 4 t.integer "particular_send_id", limit: 4 + t.integer "parent_id", limit: 4 + t.string "mlm_token", limit: 255 + t.boolean "parent_at_create", limit: 1 + t.string "parent_code", limit: 255 end create_table "product_fdps", force: :cascade do |t| @@ -680,7 +684,7 @@ ActiveRecord::Schema.define(version: 20160426151106) do t.string "particular_send_id", limit: 255 t.string "token", limit: 255 t.boolean "completed_finish", limit: 1, default: false - t.integer "product_order_payment_ok", limit: 4 + t.integer "product_order_payment_ok_id", limit: 4 t.integer "product_order_payment_type_ok_id", limit: 4 t.integer "bill_index", limit: 4 t.string "bill_number", limit: 255