show response errors,sync and update member

This commit is contained in:
Myat Zin Wai Maw
2019-05-08 17:59:57 +06:30
parent 8f81eb8e4a
commit b2532ce5f3
5 changed files with 203 additions and 45 deletions

View File

@@ -1,6 +1,6 @@
class Crm::CustomersController < BaseCrmController
load_and_authorize_resource except: [:create]
before_action :set_crm_customer, only: [:show, :edit, :update, :destroy]
before_action :set_crm_customer, only: [:show, :edit, :update, :destroy,:sync]
# GET /crm/customers
# GET /crm/customers.json
@@ -8,6 +8,7 @@ class Crm::CustomersController < BaseCrmController
filter = params[:filter]
filter_card_no = params[:filter_card_no]
type = params[:type]
@customer_update_phone_email =false
if filter_card_no==""
@crm_customers = Customer.all
elsif !filter_card_no.nil?
@@ -16,25 +17,44 @@ class Crm::CustomersController < BaseCrmController
@crm_customers = Customer.all
else
@crm_customers = Customer.search(filter)
paymal_customer = Customer.search_paypar_account_no(filter)
# search account no from paypar
if !@crm_customers.present? && type == "card"
response = Customer.search_paypar_account_no(filter)
if response["status"] == true
@crm_customers = Customer.new
@crm_customers.name = response["customer_data"]["name"]
@crm_customers.contact_no = response["customer_data"]["phone"]
@crm_customers.email = response["customer_data"]["email"]
@crm_customers.date_of_birth = response["customer_data"]["DOB"]
@crm_customers.nrc_no = response["customer_data"]["NRC"]
@crm_customers.address = response["customer_data"]["address"]
@crm_customers.card_no = response["customer_data"]["customer_card_no"]
@crm_customers.paypar_account_no = filter
@crm_customers.membership_id = response["customer_data"]["id"]
@crm_customers.membership_type = response["customer_data"]["member_group_id"]
@crm_customers.customer_type = "Dinein"
@crm_customers.tax_profiles = ["1", "2"]
@crm_customers.save
@crm_customers = Customer.search(filter)
if type == "card"
response = Customer.search_paypar_account_no(filter)
if !@crm_customers.present?
if response["status"] == true
@crm_customers = Customer.new
@crm_customers.name = response["customer_data"]["name"]
@crm_customers.contact_no = response["customer_data"]["phone"]
@crm_customers.email = response["customer_data"]["email"]
@crm_customers.date_of_birth = response["customer_data"]["DOB"]
@crm_customers.nrc_no = response["customer_data"]["NRC"]
@crm_customers.address = response["customer_data"]["address"]
@crm_customers.card_no = response["customer_data"]["customer_card_no"]
@crm_customers.paypar_account_no = filter
@crm_customers.membership_id = response["customer_data"]["id"]
@crm_customers.membership_type = response["customer_data"]["member_group_id"]
@crm_customers.customer_type = "Dinein"
@crm_customers.tax_profiles = ["1", "2"]
@crm_customers.save
@crm_customers = Customer.search(filter)
flash[:notice]='Customer was successfully created.'
else
flash[:error]=response["message"]
end
else
@crm_customers.each do |customer|
if customer.contact_no != response["customer_data"]["phone"]
@customer_update_phone_email =true
end
if customer.email != response["customer_data"]["email"]
@customer_update_phone_email =true
end
if customer.name != response["customer_data"]["name"]
@customer_update_phone_email =true
end
end
end
end
end
@@ -69,6 +89,7 @@ class Crm::CustomersController < BaseCrmController
# if flash["errors"]
# @crm_customer.valid?
# end
# puts @crm_customers.to_json
respond_to do |format|
format.html # index.html.erb
format.json { render json: @crm_customers }
@@ -120,7 +141,91 @@ class Crm::CustomersController < BaseCrmController
def edit
@customer = Customer.find(params[:id])
end
def sync
@customer = Customer.find(params[:id])
respond_to do |format|
name = @customer.name
phone = @customer.contact_no
email = @customer.email
dob = @customer.date_of_birth
address = @customer.address
nrc = @customer.nrc_no
card_no = @customer.card_no
paypar_account_no = @customer.paypar_account_no
id = @crm_customer.membership_id
member_group_id = @customer.membership_type
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 "--------Sync 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 }
flash[:member_notice] ='Member was successfully synced'
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 )
format.html { redirect_to crm_customers_path }
flash[:member_notice] ='Member was successfully synced'
else
status = customer.update_attributes(membership_type:member_group_id)
end
# When paypar account no not exist in paypar
if response["message"] == "Account does not exist."
customer.destroy
format.html { redirect_to crm_customers_path }
flash[:member_error] ='Member cannot created.Invalid Account.'
end
format.html { redirect_to crm_customers_path }
flash[:member_error] =response["message"]
end
end
end
end
# POST /crm/customers
# POST /crm/customers.json
def create
@@ -196,9 +301,11 @@ class Crm::CustomersController < BaseCrmController
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.' }
format.html { redirect_to '/origami/'+params[:sale_id]+'/'+params[:type]+'/customers/'+params[:page] }
flash[:member_notice]='Member was successfully created.'
else
format.html { redirect_to crm_customers_path, notice: 'Customer was successfully created'}
format.html { redirect_to crm_customers_path }
flash[:member_notice]='Member was successfully created.'
end
# format.json { render :index, status: :created, location: @crm_customers }
else
@@ -207,24 +314,28 @@ class Crm::CustomersController < BaseCrmController
# 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 )
format.html { redirect_to crm_customers_path }
flash[:member_notice]='Member was successfully created.'
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."
if response["message"] == "Account does 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"]}
format.html { redirect_to '/origami/'+params[:sale_id]+'/'+params[:type]+'/customers/'+params[:page] }
flash[:member_error]='Member cannot created.Invalid Account.'
else
format.html { redirect_to crm_customers_path, notice: 'Customer cannot created.' + response["message"] }
format.html { redirect_to crm_customers_path }
flash[:member_error]='Member cannot created.Invalid Account.'
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"]}
format.html { redirect_to '/origami/'+params[:sale_id]+'/'+params[:type]+'/customers/'+params[:page] }
flash[:member_notice]='Member was successfully created.' + response["message"]
else
format.html { redirect_to crm_customers_path, notice: 'Customer was successfully created.' }
format.html { redirect_to crm_customers_path }
# flash[:member_notice]='Member was successfully created.'
end
end
else
@@ -333,16 +444,19 @@ class Crm::CustomersController < BaseCrmController
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'}
format.html { redirect_to crm_customers_path }
flash[:member_notice]='Member 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 )
format.html { redirect_to crm_customers_path }
flash[:member_notice]='Member was successfully updated'
else
status = customer.update_attributes(membership_type:member_group_id)
format.html { redirect_to crm_customers_path }
flash[:member_error]=response["message"]
end
format.html { redirect_to crm_customers_path, notice: response["error"] }
end
else
membership = MembershipSetting.find_by_membership_type("paypar_url")
@@ -395,7 +509,8 @@ class Crm::CustomersController < BaseCrmController
else
status = customer.update_attributes(membership_type:member_group_id )
end
format.html { redirect_to crm_customers_path, notice: 'Customer was successfully updated.' }
format.html { redirect_to crm_customers_path }
flash[:member_notice] ='Member was successfully updated.'
format.json { render :show, status: :ok, location: @crm_customer }
else
customer = Customer.find(@crm_customer.customer_id)
@@ -405,7 +520,8 @@ class Crm::CustomersController < BaseCrmController
else
status = customer.update_attributes(membership_type:member_group_id )
end
format.html { redirect_to crm_customers_path, notice: response["message"] }
format.html { redirect_to crm_customers_path }
flash[:member_error] =response["message"]
end
end
else
@@ -438,6 +554,23 @@ class Crm::CustomersController < BaseCrmController
end
end
def customer_update_phone_email
@customer = Customer.find(params[:id])
response = Customer.search_paypar_account_no(@customer.paypar_account_no)
if response["status"] == true
@customer.name =response["customer_data"]["name"]
@customer.contact_no =response["customer_data"]["phone"]
@customer.email =response["customer_data"]["email"]
@customer.save
respond_to do |format|
format.html { redirect_to crm_customers_path }
flash[:member_notice]='Membership was successfully updated'
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_crm_customer
@@ -452,3 +585,4 @@ class Crm::CustomersController < BaseCrmController
end
end