check customer contact no

This commit is contained in:
phyusin
2018-06-05 10:49:12 +06:30
parent 7ef461beee
commit d28d62a937
2 changed files with 257 additions and 232 deletions

View File

@@ -99,9 +99,11 @@ class Crm::CustomersController < BaseCrmController
def create def create
# Remove "" default first # Remove "" default first
params[:customer][:tax_profiles].delete_at(0) params[:customer][:tax_profiles].delete_at(0)
@checked_contact = Customer.find_by_contact_no(customer_params[:contact_no])
if @checked_contact.nil?
respond_to do |format|
@crm_customers = Customer.new(customer_params) @crm_customers = Customer.new(customer_params)
respond_to do |format|
if @crm_customers.save if @crm_customers.save
# update tax profile # update tax profile
customer = Customer.find(@crm_customers.customer_id) customer = Customer.find(@crm_customers.customer_id)
@@ -206,7 +208,6 @@ class Crm::CustomersController < BaseCrmController
end end
end end
else else
if params[:sale_id] if params[:sale_id]
flash[:errors] = @crm_customers.errors flash[:errors] = @crm_customers.errors
format.html { redirect_to '/origami/'+params[:sale_id]+'/'+params[:type]+'/customers/'+params[:page]} format.html { redirect_to '/origami/'+params[:sale_id]+'/'+params[:type]+'/customers/'+params[:page]}
@@ -217,14 +218,30 @@ class Crm::CustomersController < BaseCrmController
format.json { render json: @crm_customers.errors, status: :unprocessable_entity } format.json { render json: @crm_customers.errors, status: :unprocessable_entity }
end end
end end
end end
else
flash[:errors] = {"contact_no":["has already been taken"]}
respond_to do |format|
if params[:sale_id]
format.html { redirect_to '/origami/'+params[:sale_id]+'/'+params[:type]+'/customers/'+params[:page]}
else
format.html { redirect_to crm_customers_path}
end
format.json { render json: @crm_customers.errors, status: :unprocessable_entity }
end
end #end of contact no checked
end end
# PATCH/PUT /crm/customers/1 # PATCH/PUT /crm/customers/1
# PATCH/PUT /crm/customers/1.json # PATCH/PUT /crm/customers/1.json
def update def update
# Remove "" default first # Remove "" default first
params[:customer][:tax_profiles].delete_at(0) params[:customer][:tax_profiles].delete_at(0)
@checked_contact = nil
@existed_contact = Customer.find_by_customer_id_and_contact_no(customer_params[:id], customer_params[:contact_no])
if @existed_contact.nil?
@checked_contact = Customer.find_by_contact_no(customer_params[:contact_no])
end
if !@existed_contact.nil? && @checked_contact.nil?
respond_to do |format| respond_to do |format|
if @crm_customer.update(customer_params) if @crm_customer.update(customer_params)
# update tax profile # update tax profile
@@ -363,16 +380,24 @@ end
end end
format.html { redirect_to crm_customers_path, notice: response["message"] } format.html { redirect_to crm_customers_path, notice: response["message"] }
end end
end end
else else
flash[:errors] = @crm_customer.errors flash[:errors] = @crm_customer.errors
format.html { redirect_to crm_customers_path} format.html { redirect_to crm_customers_path}
format.json { render json: @crm_customer.errors, status: :unprocessable_entity } format.json { render json: @crm_customer.errors, status: :unprocessable_entity }
end end
end
else
flash[:errors] = {"contact_no":["has already been taken"]}
respond_to do |format|
if params[:sale_id]
format.html { redirect_to '/origami/'+params[:sale_id]+'/'+params[:type]+'/customers/'+params[:page]}
else
format.html { redirect_to crm_customers_path}
end
format.json { render json: @crm_customers.errors, status: :unprocessable_entity }
end
end end
end end

View File

@@ -10,7 +10,7 @@ class Customer < ApplicationRecord
has_many :sales has_many :sales
validates_presence_of :name, :contact_no, :email #,:card_no validates_presence_of :name, :contact_no, :email #,:card_no
validates :contact_no, uniqueness: true, numericality: true validates :contact_no, numericality: true #uniqueness: true,
validates :email, uniqueness: true,format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, on: :create } validates :email, uniqueness: true,format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, on: :create }
# validates :card_no, uniqueness: true # validates :card_no, uniqueness: true
# validates :paypar_account_no, uniqueness: true # validates :paypar_account_no, uniqueness: true