From 911b941793266eb495d7b7be0d319f33f6c60d8f Mon Sep 17 00:00:00 2001 From: Yan Date: Wed, 23 Aug 2017 13:43:31 +0630 Subject: [PATCH] fix member sync and menu json --- app/models/customer.rb | 61 +++++++++++-------- .../restaurant/menu/_menu_item.json.jbuilder | 49 +++++++++++++-- 2 files changed, 78 insertions(+), 32 deletions(-) diff --git a/app/models/customer.rb b/app/models/customer.rb index dff92ba2..b33b13b4 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -91,41 +91,50 @@ class Customer < ApplicationRecord auth_token = memberaction.auth_token.to_s url = membership.gateway_url.to_s + memberaction.gateway_url.to_s - @customers = Customer.where("membership_type IS NOT NULL AND membership_id IS NULL") + @customers = Customer.where("membership_type IS NOT NULL AND membership_id IS NULL") @customers.each do |customer| - begin - response = HTTParty.post(url, - :body => - { 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,member_group_id: customer.membership_type, - merchant_uid:merchant_uid,auth_token:auth_token - }.to_json, - :headers => { - 'Content-Type' => 'application/json', - 'Accept' => 'application/json' - }) - rescue Net::OpenTimeout - response = { status: false, message: "Server Time out" } - - rescue OpenURI::HTTPError - response = { status: false, message: "Can't connect server"} + member_params = { 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,member_group_id: customer.membership_type, + merchant_uid:merchant_uid,auth_token:auth_token}.to_json - rescue SocketError - response = { status: false, message: "Can't connect server"} - end + # Check for paypar account exists + # if paypar_account_no != nil || paypar_account_no != '' + if paypar_account_no.present? + member_params = { name: customer.name,phone: customer.contact_no, + email: customer.email,dob: customer.date_of_birth, + address: customer.address,nrc:customer.nrc_no, + paypar_account_no: customer.paypar_account_no, + card_no:customer.card_no,member_group_id: customer.membership_type, + merchant_uid:merchant_uid,auth_token:auth_token}.to_json + end - if response["status"] == true - status = customer.update_attributes(membership_id: response["customer_datas"]["id"]) - end + begin + response = HTTParty.post(url, + :body => member_params, + :headers => { + 'Content-Type' => 'application/json', + 'Accept' => 'application/json' + }) + rescue Net::OpenTimeout + response = { status: false, message: "Server Time out" } + + rescue OpenURI::HTTPError + response = { status: false, message: "Can't connect server"} + rescue SocketError + response = { status: false, message: "Can't connect server"} + end + + if response["status"] == true + status = customer.update_attributes(membership_id: response["customer_datas"]["id"]) + end end end def self.update_rebate - sales = Sale.where("rebate_status = 'false'") sales.each do |sale| if sale.customer.membership_id diff --git a/app/views/api/restaurant/menu/_menu_item.json.jbuilder b/app/views/api/restaurant/menu/_menu_item.json.jbuilder index 7a450850..c76a4808 100644 --- a/app/views/api/restaurant/menu/_menu_item.json.jbuilder +++ b/app/views/api/restaurant/menu/_menu_item.json.jbuilder @@ -22,23 +22,59 @@ item.item_attributes.each do|attr_id| param_count -= 1 end +# Format for option json +opt_format = [] +param_count = item.item_options.count +# Format for attributes json +item.item_options.each do|opt| + menu_opt = MenuItemOption.find(opt) + if opt_format.count == 0 + opt_format.push({ type: menu_opt.option_type, value: [] }) + end + + opt_format.each do |af| + if menu_opt.option_type == af[:type] + af[:value].push(menu_opt.value) + else + new_attr = {type: menu_opt.option_type, value: [ menu_opt.value ] } + opt_format.push(new_attr) + end + + break if opt_format.count > param_count + end + + param_count -= 1 +end + #Menu Item Information json.id item.id -json.item_code item.item_code +json.code item.item_code json.name item.name json.alt_name item.alt_name json.image item.image_path.url json.description item.description -json.Information item.information +json.information item.information json.type item.type json.account_id item.account_id json.min_qty item.min_qty json.is_available item.is_available json.is_sub_item item.is_sub_item json.unit item.unit -json.item_sets item.item_sets + +# Item Sets of Menu Item +json.item_sets item.item_sets do |its| + json.id its.id + json.name its.name + json.alt_name its.alt_name + json.min_selectable_qty its.min_selectable_qty + json.max_selectable_qty its.max_selectable_qty + json.instances its.menu_item_instances do |i| + json.id i.id + end +end + json.attributes attr_format -json.options item.item_options +json.options opt_format # json.min_selectable_item item.min_selectable_item # json.max_selectable_item item.max_selectable_item @@ -52,6 +88,7 @@ json.options item.item_options # json.item_attributes = item_instance.item_attributes json.instances item.menu_item_instances do |is| + json.id is.id json.code is.item_instance_code json.name is.item_instance_name json.price is.price @@ -59,8 +96,8 @@ json.options item.item_options json.is_default is.is_default json.is_on_promotion is.is_on_promotion json.promotion_price is.promotion_price - json.item_attributes is.item_attributes - json.item_sets is.item_sets + json.values is.item_attributes + # json.item_sets is.item_sets end #Child Menu items