diff --git a/app/controllers/crm/customers_controller.rb b/app/controllers/crm/customers_controller.rb index 21c2d373..5979ce21 100644 --- a/app/controllers/crm/customers_controller.rb +++ b/app/controllers/crm/customers_controller.rb @@ -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 + diff --git a/app/views/crm/customers/index.html.erb b/app/views/crm/customers/index.html.erb index 76d1dce8..6b9707f5 100644 --- a/app/views/crm/customers/index.html.erb +++ b/app/views/crm/customers/index.html.erb @@ -9,12 +9,27 @@ --> -
+
-
+ + <% end %> <% end %> <% end %> <%= yield %> diff --git a/config/routes.rb b/config/routes.rb index 89963f1e..f25ff1b8 100755 --- a/config/routes.rb +++ b/config/routes.rb @@ -358,6 +358,8 @@ scope "(:locale)", locale: /en|mm/ do namespace :crm do root "home#index" resources :customers + get "/customers/:id/sync" => "customers#sync", as:"customer_sync" + get "/customers/:id/customer_update_phone_email" => "customers#customer_update_phone_email", as:"customer_update_phone_email" resources :dining_queues post "update_booking", to: "bookings#update_booking", as: "update_booking" #assign and cancel get '/print/:id', to: "home#print_order" #print order for crm diff --git a/db/seeds.rb b/db/seeds.rb index 7a139b6e..a2307479 100755 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -209,19 +209,19 @@ queue_no_printer=PrintSetting.create({name: "Queue No", unique_code: "QueueNoPdf zone = Zone.create({id:1, name: "H1", is_active:true, created_by: "SYSTEM DEFAULT"}) -member_setting = MembershipSetting.create({membership_type:"paypar_url",gateway_url: "https://staging.membership.paypar.ws",merchant_account_id:"pZBHXEFbGNj/G"}) +member_setting = MembershipSetting.create({membership_type:"paypar_url",gateway_url: "http://192.168.1.82:3001",merchant_account_id:"NskNY4j3YxEg6h1Fm9AE"}) member_actions= MembershipAction.create([{membership_type:"get_account_balance",is_active:1,gateway_url:"/api/membership_campaigns/get_correspond_account_data",additional_parameter:{campaign_type_id:1},merchant_account_id:"vWSsseoZCzxd6xcNf_uS",auth_token:"pZBHXEFbGNj/G"}, {membership_type:"redeem",is_active:1,gateway_url:"/api/membership_campaigns/redeem",additional_parameter:{campaign_type_id:1},merchant_account_id:"vWSsseoZCzxd6xcNf_uS",auth_token:"pZBHXEFbGNj/G"}, - {membership_type:"create_membership_customer",is_active:1,gateway_url:"/api/generic_customer/create_membership_customer",merchant_account_id:"vWSsseoZCzxd6xcNf_uS",auth_token:"pZBHXEFbGNj/G"}, - {membership_type:"update_membership_customer",is_active:1,gateway_url:"/api/generic_customer/update_membership_customer",merchant_account_id:"vWSsseoZCzxd6xcNf_uS",auth_token:"pZBHXEFbGNj/G"}, + {membership_type:"create_membership_customer",is_active:1,gateway_url:"/api/generic_customer/create_membership_customer",merchant_account_id:"NskNY4j3YxEg6h1Fm9AE",auth_token:"rj0MJ0XI5GsKZehE"}, + {membership_type:"update_membership_customer",is_active:1,gateway_url:"/api/generic_customer/update_membership_customer",merchant_account_id:"NskNY4j3YxEg6h1Fm9AE",auth_token:"rj0MJ0XI5GsKZehE"}, {membership_type:"get_all_member_group",is_active:1,gateway_url:"/api/member_group/get_all_member_group",merchant_account_id:"vWSsseoZCzxd6xcNf_uS",auth_token:"pZBHXEFbGNj/G"}, {membership_type:"rebate",is_active:1,gateway_url:"/api/membership_campaigns/rebate",additional_parameter:{campaign_type_id:1},merchant_account_id:"vWSsseoZCzxd6xcNf_uS",auth_token:"pZBHXEFbGNj/G"}, {membership_type:"get_all_member_account",is_active:1,gateway_url:"/api/generic_customer/get_member_data",merchant_account_id:"vWSsseoZCzxd6xcNf_uS",auth_token:"pZBHXEFbGNj/G"}, {membership_type:"get_member_transactions",is_active:1,gateway_url:"/api/generic_customer/get_membership_transactions",merchant_account_id:"vWSsseoZCzxd6xcNf_uS",auth_token:"pZBHXEFbGNj/G"}, {membership_type:"member_discount",is_active:1,gateway_url:"/api/membership_campaigns/discount",additional_parameter:{campaign_type_id:5},merchant_account_id:"vWSsseoZCzxd6xcNf_uS",auth_token:"pZBHXEFbGNj/G"}, {membership_type:"get_member_campaign",is_active:1,gateway_url:"/api/membership_campaigns/get_member_campaign",additional_parameter:{campaign_type_id:5},merchant_account_id:"vWSsseoZCzxd6xcNf_uS",auth_token:"pZBHXEFbGNj/G"}, - {membership_type:"search_paypar_account_no",is_active:1,gateway_url:"/api/generic_customer/get_membership_customer_data",merchant_account_id:"vWSsseoZCzxd6xcNf_uS",auth_token:"pZBHXEFbGNj/G"} + {membership_type:"search_paypar_account_no",is_active:1,gateway_url:"/api/generic_customer/get_membership_customer_data",merchant_account_id:"NskNY4j3YxEg6h1Fm9AE",auth_token:"rj0MJ0XI5GsKZehE"} ]) payment_methods = PaymentMethodSetting.create({payment_method:"MPU",gateway_url: "http://membership.paypar.ws"})