103 lines
3.4 KiB
Ruby
103 lines
3.4 KiB
Ruby
class Customer < ApplicationRecord
|
|
|
|
#self.primary_key = :customer_id
|
|
|
|
before_create :generate_custom_id
|
|
has_many :orders
|
|
has_many :sales
|
|
|
|
validates_presence_of :name, :contact_no, :email,:card_no
|
|
validates :contact_no, uniqueness: true
|
|
validates :email, uniqueness: true
|
|
validates :card_no, uniqueness: true
|
|
|
|
paginates_per 50
|
|
|
|
def self.get_member_account(customer)
|
|
membership = MembershipSetting.find_by_membership_type("paypar_url")
|
|
memberaction = MembershipAction.find_by_membership_type("get_all_member_account")
|
|
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
|
|
# urltest =self.url_exist?(url)
|
|
|
|
begin
|
|
response = HTTParty.get(url, :body => { membership_id: customer.membership_id,merchant_uid:merchant_uid,auth_token:auth_token}.to_json,
|
|
:headers => {
|
|
'Content-Type' => 'application/json',
|
|
'Accept' => 'application/json'
|
|
},
|
|
:timeout => 10
|
|
)
|
|
rescue Net::OpenTimeout
|
|
response = { status: false }
|
|
|
|
rescue OpenURI::HTTPError
|
|
response = { status: false}
|
|
|
|
rescue SocketError
|
|
response = { status: false}
|
|
end
|
|
|
|
return response;
|
|
|
|
end
|
|
|
|
def self.get_membership_transactions(customer)
|
|
membership = MembershipSetting.find_by_membership_type("paypar_url")
|
|
memberaction = MembershipAction.find_by_membership_type("get_member_transactions")
|
|
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
|
|
# urltest =self.url_exist?(url)
|
|
|
|
begin
|
|
response = HTTParty.get(url, :body => { membership_id: customer.membership_id,merchant_uid:merchant_uid,auth_token:auth_token}.to_json,
|
|
:headers => {
|
|
'Content-Type' => 'application/json',
|
|
'Accept' => 'application/json'
|
|
},
|
|
:timeout => 10
|
|
)
|
|
rescue Net::OpenTimeout
|
|
response = { status: false }
|
|
|
|
rescue OpenURI::HTTPError
|
|
response = { status: false}
|
|
|
|
rescue SocketError
|
|
response = { status: false}
|
|
end
|
|
|
|
return response;
|
|
|
|
end
|
|
|
|
def self.search(search)
|
|
if search
|
|
# find(:all, :conditions => ['name LIKE ? OR contact_no LIKE ?', "%#{search}%", "%#{search}%"])
|
|
where("name LIKE ? OR contact_no LIKE ? OR card_no LIKE ?", "%#{search}%", "%#{search}%", "%#{search}%",)
|
|
else
|
|
find(:all)
|
|
end
|
|
end
|
|
|
|
def lastest_invoices
|
|
sales.where(:customer_id => self.id).order("created_at desc").limit(5)
|
|
end
|
|
|
|
def self.count_customer
|
|
all = self.all.count+1
|
|
count = all-2
|
|
|
|
end
|
|
|
|
WALKIN = "CUS-000000000001"
|
|
TAKEAWAY = "CUS-000000000002"
|
|
|
|
private
|
|
def generate_custom_id
|
|
self.customer_id = SeedGenerator.generate_id(self.class.name, "CUS")
|
|
end
|
|
end
|