Files
sx-fc/app/controllers/crm/customers_controller.rb
2017-06-09 12:00:46 +06:30

193 lines
6.6 KiB
Ruby

class Crm::CustomersController < BaseCrmController
before_action :set_crm_customer, only: [:show, :edit, :update, :destroy]
# GET /crm/customers
# GET /crm/customers.json
def index
@sale_id = 0
filter = params[:filter]
if filter.nil?
@crm_customers = Customer.order("name").page(params[:page])
#@products = Product.order("name").page(params[:page]).per(5)
else
@crm_customers = Customer.where("name LIKE ?", "%#{filter}%").order("name").page(params[:page])
end
#@crm_customers = Customer.all
@crm_customer = Customer.new
@membership = Customer.get_member_group
if @membership["status"] == true
@member_group = @membership["data"]
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: @crm_customers }
end
end
# GET /crm/customers/1
# GET /crm/customers/1.json
def show
end
# GET /crm/customers/new
def new
@crm_customer = Customer.new
@membership = Customer.get_member_group()
end
# GET /crm/customers/1/edit
def edit
end
# POST /crm/customers
# POST /crm/customers.json
def create
@crm_customers = Customer.new(customer_params)
respond_to do |format|
if @crm_customers.save
name = customer_params[:name]
phone = customer_params[:contact_no]
email = customer_params[:email]
date_of_birth = customer_params[:date_of_birth]
membership_id = params[:membership_id]
membership = MembershipSetting.find_by_membership_type("smartpay_url")
app_token = membership.auth_token.to_s
url = membership.gateway_url.to_s + "/api/create_membership_customer".to_s
response = HTTParty.post(url, :body => { name: name,phone: phone,email: email,
date_of_birth: date_of_birth,
membership_id: membership_id}.to_json,
:headers => {
'Content-Type' => 'application/json',
'Accept' => 'application/json'
}
)
if response["status"] == true
puts "hhhhhhhhhhhhhhhhhh"
puts params[:sale_id]
puts response.to_json
customer = Customer.find(@crm_customers.customer_id)
status = customer.update_attributes(membership_id: response["customer_datas"]["id"])
if params[:sale_id] != 0
format.html { redirect_to crm_customers_path, notice: 'Customer was successfully created.' }
else
format.html { redirect_to '/crm/customers/'+params[:sale_id]+'/assign_sale_id', notice: 'Customer was successfully created.' }
end
# format.json { render :index, status: :created, location: @crm_customers }
else
@crm_customers.destroy
if params[:sale_id] != 0
format.html { redirect_to crm_customers_path, notice: response["message"] }
else
format.html { redirect_to '/crm/customers/'+params[:sale_id]+'/assign_sale_id', notice: response["message"] }
end
end
# format.json { render :index, status: :created, location: @crm_customers }
else
if params[:sale_id] != 0
format.html { redirect_to crm_customers_path}
format.json { render json: @crm_customers.errors, status: :unprocessable_entity }
else
format.html { redirect_to '/crm/customers/'+params[:sale_id]+'/assign_sale_id', notice: response["message"] }
end
end
end
end
# PATCH/PUT /crm/customers/1
# PATCH/PUT /crm/customers/1.json
def update
respond_to do |format|
if @crm_customer.update(customer_params)
name = customer_params[:name]
phone = customer_params[:contact_no]
email = customer_params[:email]
date_of_birth = customer_params[:date_of_birth]
id = customer_params[:membership_id]
membership = MembershipSetting.find_by_membership_type("smartpay_url")
app_token = membership.auth_token.to_s
url = membership.gateway_url.to_s + "/api/update_membership_customer".to_s
response = HTTParty.post(url, :body => { name: name,phone: phone,email: email,
date_of_birth: date_of_birth,
id: id}.to_json,
:headers => {
'Content-Type' => 'application/json',
'Accept' => 'application/json'
}
)
format.html { redirect_to crm_customers_path, notice: 'Customer was successfully updated.' }
format.json { render :show, status: :ok, location: @crm_customer }
else
format.html { render :index }
format.json { render json: @crm_customer.errors, status: :unprocessable_entity }
end
end
end
# DELETE /crm/customers/1
# DELETE /crm/customers/1.json
def destroy
@crm_customer.destroy
respond_to do |format|
format.html { redirect_to crm_customers_url, notice: 'Customer was successfully destroyed.' }
format.json { head :no_content }
end
end
# DELETE /crm/customers/1
# DELETE /crm/customers/1.json
def get_sale_id
@sale_id = params[:sale_id]
@crm_customers = Customer.all
@crm_customer = Customer.new
@membership = Customer.get_member_group
if @membership["status"] == true
@member_group = @membership["data"]
end
respond_to do |format|
format.html { render action: "index"}
format.json { render json: @crm_customers }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_crm_customer
@crm_customer = Customer.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def customer_params
params.require(:customer).permit(:name, :company, :contact_no, :email, :date_of_birth, :membership_type, :membership_authentication_code)
end
end