From d28d62a937d0e9c002fbd87f9611c4eed3a3e42e Mon Sep 17 00:00:00 2001 From: phyusin Date: Tue, 5 Jun 2018 10:49:12 +0630 Subject: [PATCH] check customer contact no --- app/controllers/crm/customers_controller.rb | 487 ++++++++++---------- app/models/customer.rb | 2 +- 2 files changed, 257 insertions(+), 232 deletions(-) diff --git a/app/controllers/crm/customers_controller.rb b/app/controllers/crm/customers_controller.rb index 97f75135..00a6ee16 100644 --- a/app/controllers/crm/customers_controller.rb +++ b/app/controllers/crm/customers_controller.rb @@ -98,14 +98,154 @@ class Crm::CustomersController < BaseCrmController # POST /crm/customers.json def create # Remove "" default first - params[:customer][:tax_profiles].delete_at(0) - @crm_customers = Customer.new(customer_params) + 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) - respond_to do |format| - if @crm_customers.save - # update tax profile - customer = Customer.find(@crm_customers.customer_id) - customer.update_attributes(tax_profiles: params[:customer][:tax_profiles]) + if @crm_customers.save + # update tax profile + customer = Customer.find(@crm_customers.customer_id) + customer.update_attributes(tax_profiles: params[:customer][:tax_profiles]) + name = customer_params[:name] + phone = customer_params[:contact_no] + email = customer_params[:email] + dob = customer_params[:date_of_birth] + address = customer_params[:address] + nrc = customer_params[:nrc_no] + card_no = customer_params[:card_no] + paypar_account_no = customer_params[:paypar_account_no] + member_group_id = params[:member_group_id] + + if member_group_id.present? + membership = MembershipSetting.find_by_membership_type("paypar_url") + memberaction = MembershipAction.find_by_membership_type("create_membership_customer") + merchant_uid = memberaction.merchant_account_id.to_s + auth_token = memberaction.auth_token.to_s + url = membership.gateway_url.to_s + memberaction.gateway_url.to_s + + member_params = { name: name,phone: phone,email: email, + dob: dob,address: address,nrc:nrc,card_no:card_no, + member_group_id: member_group_id, + merchant_uid:merchant_uid,auth_token:auth_token}.to_json + + # Check for paypar account exists + # if paypar_account_no != nil || paypar_account_no != '' + if paypar_account_no.present? + member_params = { name: name,phone: phone,email: email, + dob: dob,address: address,nrc:nrc,card_no:card_no, + paypar_account_no: paypar_account_no, + member_group_id: member_group_id, + merchant_uid:merchant_uid,auth_token:auth_token}.to_json + end + + begin + response = HTTParty.post(url, + :body => member_params, + :headers => { + 'Content-Type' => 'application/json', + 'Accept' => 'application/json; version=3' + }, + :timeout => 10 + ) + + rescue HTTParty::Error + response = {"status" => false, "message" => "Can't open membership server "} + + rescue Net::OpenTimeout + response = {"status" => false, "message" => "Can't open membership server "} + + rescue OpenURI::HTTPError + response = {"status" => false, "message" => "Can't open membership server "} + + rescue SocketError + response = {"status" => false, "message" => "Can't open membership server "} + end + Rails.logger.debug "--------------Create Member---------" + Rails.logger.debug response.to_json + if response["status"] == true + customer = Customer.find(@crm_customers.customer_id) + status = customer.update_attributes(membership_id: response["customer_datas"]["id"],membership_type:member_group_id ) + + if params[:sale_id] + format.html { redirect_to '/origami/'+params[:sale_id]+'/'+params[:type]+'/customers/'+params[:page], notice: 'Customer was successfully created.' } + else + format.html { redirect_to crm_customers_path, notice: 'Customer was successfully created'} + end + # format.json { render :index, status: :created, location: @crm_customers } + else + customer = Customer.find(@crm_customers.customer_id) + + # Check membership id and bind to user + if response["membership_id"] != nil + status = customer.update_attributes(membership_id: response["membership_id"],membership_type:member_group_id ) + else + status = customer.update_attributes(membership_type:member_group_id) + end + + # When paypar account no not exist in paypar + if response["message"] == "Account has not exist." + customer.destroy + if params[:sale_id] + format.html { redirect_to '/origami/'+params[:sale_id]+'/'+params[:type]+'/customers/'+params[:page], notice: 'Customer cannot created.' + response["message"]} + else + format.html { redirect_to crm_customers_path, notice: 'Customer cannot created.' + response["message"] } + end + end + + if params[:sale_id] + format.html { redirect_to '/origami/'+params[:sale_id]+'/'+params[:type]+'/customers/'+params[:page], notice: 'Customer was successfully created.' + response["message"]} + else + format.html { redirect_to crm_customers_path, notice: 'Customer was successfully created.' + response["message"] } + end + end + else + if params[:sale_id] + format.html { redirect_to '/origami/'+params[:sale_id]+'/'+params[:type]+'/customers/'+params[:page], notice: 'Customer was successfully created. noted'} + else + format.html { redirect_to crm_customers_path, notice: 'Customer was successfully created. ' } + end + end + else + if params[:sale_id] + flash[:errors] = @crm_customers.errors + format.html { redirect_to '/origami/'+params[:sale_id]+'/'+params[:type]+'/customers/'+params[:page]} + format.json { render json: @crm_customers.errors, status: :unprocessable_entity } + else + flash[:errors] = @crm_customers.errors + format.html { redirect_to crm_customers_path} + format.json { render json: @crm_customers.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 of contact no checked + end + # PATCH/PUT /crm/customers/1 + # PATCH/PUT /crm/customers/1.json + def update + # Remove "" default first + 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| + if @crm_customer.update(customer_params) + # update tax profile + @crm_customer.update_attributes(tax_profiles: params[:customer][:tax_profiles]) name = customer_params[:name] phone = customer_params[:contact_no] email = customer_params[:email] @@ -114,9 +254,10 @@ class Crm::CustomersController < BaseCrmController nrc = customer_params[:nrc_no] card_no = customer_params[:card_no] paypar_account_no = customer_params[:paypar_account_no] - member_group_id = params[:member_group_id] + id = @crm_customer.membership_id + member_group_id = params[:member_group_id] - if member_group_id.present? + if !id.present? && !member_group_id.nil? membership = MembershipSetting.find_by_membership_type("paypar_url") memberaction = MembershipAction.find_by_membership_type("create_membership_customer") merchant_uid = memberaction.merchant_account_id.to_s @@ -124,255 +265,139 @@ class Crm::CustomersController < BaseCrmController url = membership.gateway_url.to_s + memberaction.gateway_url.to_s member_params = { name: name,phone: phone,email: email, - dob: dob,address: address,nrc:nrc,card_no:card_no, - member_group_id: member_group_id, - merchant_uid:merchant_uid,auth_token:auth_token}.to_json + dob: dob,address: address,nrc:nrc,card_no:card_no, + member_group_id: member_group_id, + id:id, + merchant_uid:merchant_uid,auth_token:auth_token}.to_json # Check for paypar account exists # if paypar_account_no != nil || paypar_account_no != '' if paypar_account_no.present? member_params = { name: name,phone: phone,email: email, - dob: dob,address: address,nrc:nrc,card_no:card_no, - paypar_account_no: paypar_account_no, - member_group_id: member_group_id, - merchant_uid:merchant_uid,auth_token:auth_token}.to_json - end + dob: dob,address: address,nrc:nrc,card_no:card_no, + paypar_account_no: paypar_account_no, + member_group_id: member_group_id, + id:id, + merchant_uid:merchant_uid,auth_token:auth_token}.to_json + end begin response = HTTParty.post(url, :body => member_params, :headers => { - 'Content-Type' => 'application/json', - 'Accept' => 'application/json; version=3' + 'Content-Type' => 'application/json', + 'Accept' => 'application/json; version=3' }, :timeout => 10 - ) + ) + rescue Net::OpenTimeout + response = { status: false } - rescue HTTParty::Error - response = {"status" => false, "message" => "Can't open membership server "} + rescue OpenURI::HTTPError + response = { status: false} - rescue Net::OpenTimeout - response = {"status" => false, "message" => "Can't open membership server "} - - rescue OpenURI::HTTPError - response = {"status" => false, "message" => "Can't open membership server "} - - rescue SocketError - response = {"status" => false, "message" => "Can't open membership server "} + rescue SocketError + response = { status: false} end - Rails.logger.debug "--------------Create Member---------" + customer = Customer.find(@crm_customer.customer_id) + Rails.logger.debug "--------Update Member response -------" Rails.logger.debug response.to_json if response["status"] == true - customer = Customer.find(@crm_customers.customer_id) - status = customer.update_attributes(membership_id: response["customer_datas"]["id"],membership_type:member_group_id ) + + status = customer.update_attributes(membership_id: response["customer_datas"]["id"],membership_type:member_group_id ) - if params[:sale_id] - format.html { redirect_to '/origami/'+params[:sale_id]+'/'+params[:type]+'/customers/'+params[:page], notice: 'Customer was successfully created.' } - else - format.html { redirect_to crm_customers_path, notice: 'Customer was successfully created'} - end - # format.json { render :index, status: :created, location: @crm_customers } + format.html { redirect_to crm_customers_path, notice: 'Customer was successfully updated'} else - customer = Customer.find(@crm_customers.customer_id) - - # Check membership id and bind to user - if response["membership_id"] != nil - status = customer.update_attributes(membership_id: response["membership_id"],membership_type:member_group_id ) - else - status = customer.update_attributes(membership_type:member_group_id) - end - - # When paypar account no not exist in paypar - if response["message"] == "Account has not exist." - customer.destroy - if params[:sale_id] - format.html { redirect_to '/origami/'+params[:sale_id]+'/'+params[:type]+'/customers/'+params[:page], notice: 'Customer cannot created.' + response["message"]} - else - format.html { redirect_to crm_customers_path, notice: 'Customer cannot created.' + response["message"] } - end - end - - if params[:sale_id] - format.html { redirect_to '/origami/'+params[:sale_id]+'/'+params[:type]+'/customers/'+params[:page], notice: 'Customer was successfully created.' + response["message"]} - else - format.html { redirect_to crm_customers_path, notice: 'Customer was successfully created.' + response["message"] } - end - end - else - if params[:sale_id] - format.html { redirect_to '/origami/'+params[:sale_id]+'/'+params[:type]+'/customers/'+params[:page], notice: 'Customer was successfully created. noted'} + # Check membership id and bind to user + if response["membership_id"] != nil + status = customer.update_attributes(membership_id: response["membership_id"],membership_type:member_group_id ) else - format.html { redirect_to crm_customers_path, notice: 'Customer was successfully created. ' } - end - end - else + status = customer.update_attributes(membership_type:member_group_id) + end - if params[:sale_id] - flash[:errors] = @crm_customers.errors - format.html { redirect_to '/origami/'+params[:sale_id]+'/'+params[:type]+'/customers/'+params[:page]} - format.json { render json: @crm_customers.errors, status: :unprocessable_entity } + format.html { redirect_to crm_customers_path, notice: response["error"] } + end else - flash[:errors] = @crm_customers.errors - format.html { redirect_to crm_customers_path} - format.json { render json: @crm_customers.errors, status: :unprocessable_entity } + membership = MembershipSetting.find_by_membership_type("paypar_url") + memberaction = MembershipAction.find_by_membership_type("update_membership_customer") + merchant_uid = memberaction.merchant_account_id.to_s + auth_token = memberaction.auth_token.to_s + url = membership.gateway_url.to_s + memberaction.gateway_url.to_s + + member_params = { name: name,phone: phone,email: email, + dob: dob,address: address,nrc:nrc,card_no:card_no, + member_group_id: member_group_id, + id:id, + merchant_uid:merchant_uid,auth_token:auth_token}.to_json + + # Check for paypar account exists + if paypar_account_no.present? + member_params = { name: name,phone: phone,email: email, + dob: dob,address: address,nrc:nrc,card_no:card_no, + paypar_account_no: paypar_account_no, + member_group_id: member_group_id, + id:id, + merchant_uid:merchant_uid,auth_token:auth_token}.to_json + end + + begin + response = HTTParty.post(url, + :body => member_params, + :headers => { + 'Content-Type' => 'application/json', + 'Accept' => 'application/json; version=3' + }, + :timeout => 10 + ) + rescue Net::OpenTimeout + response = { status: false } + + rescue OpenURI::HTTPError + response = { status: false} + + rescue SocketError + response = { status: false} + end + Rails.logger.debug "--------Update Member response -------" + Rails.logger.debug response.to_json + if response["status"] == true + customer = Customer.find(@crm_customer.customer_id) + # Check membership id and bind to user + if response["membership_id"] != nil + status = customer.update_attributes(membership_id: response["membership_id"],membership_type:member_group_id ) + else + status = customer.update_attributes(membership_type:member_group_id ) + end + format.html { redirect_to crm_customers_path, notice: 'Customer was successfully updated.' } + format.json { render :show, status: :ok, location: @crm_customer } + else + customer = Customer.find(@crm_customer.customer_id) + # Check membership id and bind to user + if response["membership_id"] != nil + status = customer.update_attributes(membership_id: response["membership_id"],membership_type:member_group_id ) + else + status = customer.update_attributes(membership_type:member_group_id ) + end + format.html { redirect_to crm_customers_path, notice: response["message"] } + end end - end - - end -end - # PATCH/PUT /crm/customers/1 - # PATCH/PUT /crm/customers/1.json - def update - # Remove "" default first - params[:customer][:tax_profiles].delete_at(0) - respond_to do |format| - if @crm_customer.update(customer_params) - # update tax profile - @crm_customer.update_attributes(tax_profiles: params[:customer][:tax_profiles]) - name = customer_params[:name] - phone = customer_params[:contact_no] - email = customer_params[:email] - dob = customer_params[:date_of_birth] - address = customer_params[:address] - nrc = customer_params[:nrc_no] - card_no = customer_params[:card_no] - paypar_account_no = customer_params[:paypar_account_no] - id = @crm_customer.membership_id - member_group_id = params[:member_group_id] - - if !id.present? && !member_group_id.nil? - membership = MembershipSetting.find_by_membership_type("paypar_url") - memberaction = MembershipAction.find_by_membership_type("create_membership_customer") - merchant_uid = memberaction.merchant_account_id.to_s - auth_token = memberaction.auth_token.to_s - url = membership.gateway_url.to_s + memberaction.gateway_url.to_s - - member_params = { name: name,phone: phone,email: email, - dob: dob,address: address,nrc:nrc,card_no:card_no, - member_group_id: member_group_id, - id:id, - merchant_uid:merchant_uid,auth_token:auth_token}.to_json - - # Check for paypar account exists - # if paypar_account_no != nil || paypar_account_no != '' - if paypar_account_no.present? - member_params = { name: name,phone: phone,email: email, - dob: dob,address: address,nrc:nrc,card_no:card_no, - paypar_account_no: paypar_account_no, - member_group_id: member_group_id, - id:id, - merchant_uid:merchant_uid,auth_token:auth_token}.to_json - end - - begin - response = HTTParty.post(url, - :body => member_params, - :headers => { - 'Content-Type' => 'application/json', - 'Accept' => 'application/json; version=3' - }, - :timeout => 10 - ) - rescue Net::OpenTimeout - response = { status: false } - - rescue OpenURI::HTTPError - response = { status: false} - - rescue SocketError - response = { status: false} - end - customer = Customer.find(@crm_customer.customer_id) - Rails.logger.debug "--------Update Member response -------" - Rails.logger.debug response.to_json - if response["status"] == true - - status = customer.update_attributes(membership_id: response["customer_datas"]["id"],membership_type:member_group_id ) - - format.html { redirect_to crm_customers_path, notice: 'Customer was successfully updated'} - else - # Check membership id and bind to user - if response["membership_id"] != nil - status = customer.update_attributes(membership_id: response["membership_id"],membership_type:member_group_id ) - else - status = customer.update_attributes(membership_type:member_group_id) - end - - format.html { redirect_to crm_customers_path, notice: response["error"] } - end else - membership = MembershipSetting.find_by_membership_type("paypar_url") - memberaction = MembershipAction.find_by_membership_type("update_membership_customer") - merchant_uid = memberaction.merchant_account_id.to_s - auth_token = memberaction.auth_token.to_s - url = membership.gateway_url.to_s + memberaction.gateway_url.to_s - - member_params = { name: name,phone: phone,email: email, - dob: dob,address: address,nrc:nrc,card_no:card_no, - member_group_id: member_group_id, - id:id, - merchant_uid:merchant_uid,auth_token:auth_token}.to_json - - # Check for paypar account exists - if paypar_account_no.present? - member_params = { name: name,phone: phone,email: email, - dob: dob,address: address,nrc:nrc,card_no:card_no, - paypar_account_no: paypar_account_no, - member_group_id: member_group_id, - id:id, - merchant_uid:merchant_uid,auth_token:auth_token}.to_json - end - - begin - response = HTTParty.post(url, - :body => member_params, - :headers => { - 'Content-Type' => 'application/json', - 'Accept' => 'application/json; version=3' - }, - :timeout => 10 - ) - rescue Net::OpenTimeout - response = { status: false } - - rescue OpenURI::HTTPError - response = { status: false} - - rescue SocketError - response = { status: false} - end - Rails.logger.debug "--------Update Member response -------" - Rails.logger.debug response.to_json - if response["status"] == true - customer = Customer.find(@crm_customer.customer_id) - # Check membership id and bind to user - if response["membership_id"] != nil - status = customer.update_attributes(membership_id: response["membership_id"],membership_type:member_group_id ) - else - status = customer.update_attributes(membership_type:member_group_id ) - end - format.html { redirect_to crm_customers_path, notice: 'Customer was successfully updated.' } - format.json { render :show, status: :ok, location: @crm_customer } - else - customer = Customer.find(@crm_customer.customer_id) - # Check membership id and bind to user - if response["membership_id"] != nil - status = customer.update_attributes(membership_id: response["membership_id"],membership_type:member_group_id ) - else - status = customer.update_attributes(membership_type:member_group_id ) - end - format.html { redirect_to crm_customers_path, notice: response["message"] } - end + flash[:errors] = @crm_customer.errors + format.html { redirect_to crm_customers_path} + format.json { render json: @crm_customer.errors, status: :unprocessable_entity } end - - else - - flash[:errors] = @crm_customer.errors - format.html { redirect_to crm_customers_path} - format.json { render json: @crm_customer.errors, status: :unprocessable_entity } 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 diff --git a/app/models/customer.rb b/app/models/customer.rb index b05caaf6..a88f79c0 100755 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -10,7 +10,7 @@ class Customer < ApplicationRecord has_many :sales 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 :card_no, uniqueness: true # validates :paypar_account_no, uniqueness: true