From 3a85f1e319b5915530a6c73d559592a19ea27d08 Mon Sep 17 00:00:00 2001 From: Nweni Date: Thu, 28 Nov 2019 18:33:56 +0630 Subject: [PATCH] current shop changes --- app/controllers/api/api_controller.rb | 10 ++++- .../concerns/login_verification.rb | 42 +++++++++++------- app/controllers/home_controller.rb | 18 ++++---- .../origami/order_reservation_controller.rb | 2 +- app/models/sale.rb | 27 +++++------ app/models/stock_journal.rb | 2 +- app/views/layouts/login_dashboard.html.erb | 2 +- config/application.rb | 2 +- dump.rdb | Bin 1864 -> 1864 bytes 9 files changed, 59 insertions(+), 46 deletions(-) diff --git a/app/controllers/api/api_controller.rb b/app/controllers/api/api_controller.rb index 0e8ea909..c325c881 100755 --- a/app/controllers/api/api_controller.rb +++ b/app/controllers/api/api_controller.rb @@ -2,9 +2,17 @@ class Api::ApiController < ActionController::API include TokenVerification include ActionController::MimeResponds - # before_action :lookup_domain + before_action :core_allow helper_method :current_token, :current_login_employee, :get_cashier + + def core_allow + headers['Access-Control-Allow-Origin'] = '*' + headers['Access-Control-Allow-Methods'] = 'POST, PUT, DELETE, GET, OPTIONS' + headers['Access-Control-Request-Method'] = '*' + headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization' + end + private #this is base api base controller to need to inherit. #all token authentication must be done here diff --git a/app/controllers/concerns/login_verification.rb b/app/controllers/concerns/login_verification.rb index d6e32250..f8ed1d45 100755 --- a/app/controllers/concerns/login_verification.rb +++ b/app/controllers/concerns/login_verification.rb @@ -17,39 +17,33 @@ module LoginVerification end def current_shop - begin - shop_code ='262' - @shop =Shop.find_by_shop_code(shop_code) - return @shop - rescue - return nil - end + @current_shop end def current_login_employee - @employee = Employee.find_by_token_session_and_shop_code(session[:session_token],@shop.shop_code) + @employee = Employee.find_by_token_session_and_shop_code(session[:session_token],@current_shop.shop_code) end def current_user - @current_user ||= Employee.find_by_token_session_and_shop_code(session[:session_token],@shop.shop_code) if session[:session_token] + @current_user ||= Employee.find_by_token_session_and_shop_code(session[:session_token],@current_shop.shop_code) if session[:session_token] end # Get current Cashiers def get_cashier - @cashier = Employee.where("shop_code='#{@shop.shop_code}' and role = 'cashier' AND token_session <> ''") + @cashier = Employee.where("shop_code='#{@current_shop.shop_code}' and role = 'cashier' AND token_session <> ''") end #Shop Name in Navbor def shop_detail - shop_code ='262' - @shop = Shop.find_by_shop_code(shop_code) - return @shop + # shop_code ='262' + # @shop = Shop.find_by_shop_code(shop_code) + return @current_shop end #check order reservation used def order_reservation - order_reserve = Lookup.where("shop_code='#{@shop.shop_code}'").collection_of('order_reservation') + order_reserve = Lookup.where("shop_code='#{@current_shop.shop_code}'").collection_of('order_reservation') status = false if !order_reserve.empty? order_reserve.each do |order| @@ -65,7 +59,7 @@ module LoginVerification #check bank integration used def bank_integration - bank_integration = Lookup.where("shop_code='#{@shop.shop_code}'").collection_of('bank_integration') + bank_integration = Lookup.where("shop_code='#{@current_shop.shop_code}'").collection_of('bank_integration') status = false if !bank_integration.empty? bank_integration.each do |bank| @@ -90,8 +84,7 @@ module LoginVerification if (token) #@current_user = User.find_by(api_key: token) #Rails.logger.debug "token - " + token.to_s - - @user = Employee.authenticate_by_token(token,current_shop) + @user = Employee.authenticate_by_token(token,@current_shop) if @user return true #Maybe log - login? @@ -99,6 +92,21 @@ module LoginVerification end end + def authenticate_session_token + shop_code = 262 + if (shop_code) + #@current_user = User.find_by(api_key: token) + #Rails.logger.debug "token - " + token.to_s + @current_shop = Shop.find_by_shop_code(shop_code) + if @current_shop + return true + #Maybe log - login? + end + else + @current_shop = Shop.first + end + end + def render_unauthorized redirect_to root_path end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 1821ec9c..50088cab 100755 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -18,8 +18,8 @@ class HomeController < ApplicationController def index # @employees = Employee.all_emp_except_waiter.order("name asc") - @employees = Employee.all.where("shop_code='#{current_shop.shop_code}' and is_active = true").order("name asc") - @roles = Employee.where("shop_code='#{current_shop.shop_code}'").distinct.pluck(:role) + @employees = Employee.all.where("shop_code='#{@current_shop.shop_code}' and is_active = true").order("name asc") + @roles = Employee.where("shop_code='#{@current_shop.shop_code}'").distinct.pluck(:role) # byebug # @roles = Lookup.collection_of("employee_roles") @@ -106,10 +106,10 @@ class HomeController < ApplicationController end - @top_products = Sale.top_bottom_products(current_user,@from,@to,"top") - @bottom_products = Sale.top_bottom_products(current_user,@from,@to,"bottom") - @hourly_sales = Sale.hourly_sales(current_user,@from,@to) - employee_sales = Sale.employee_sales(current_user,@from,@to) + @top_products = Sale.top_bottom_products(current_user,@from,@to,"top",current_shop) + @bottom_products = Sale.top_bottom_products(current_user,@from,@to,"bottom",current_shop) + @hourly_sales = Sale.hourly_sales(current_user,@from,@to,current_shop) + employee_sales = Sale.employee_sales(current_user,@from,@to,current_shop) @employee_sales = [] if !employee_sales.nil? @@ -123,11 +123,11 @@ class HomeController < ApplicationController end end - @inventories = StockJournal.inventory_balances(@from,@to).sum(:balance) + @inventories = StockJournal.inventory_balances(@from,@to, current_shop).sum(:balance) @total_trans = Sale.total_trans(current_user,@from,@to) @total_card = Sale.total_card_sale(current_user,@from,@to) - @total_credit = Sale.credit_payment(current_user,@from,@to) + @total_credit = Sale.credit_payment(current_user,@from,@to, current_shop) @sale_data = Array.new @total_payment_methods = Sale.total_payment_methods(current_user,@from,@to) @@ -143,7 +143,7 @@ class HomeController < ApplicationController @total_customer, @total_dinein, @total_takeaway, @total_membership = Sale.total_customer(current_user,@from,@to) # @total_other_customer = Sale.total_other_customer(today,current_user) - @total_order = Sale.total_order(current_user,@from,@to) + @total_order = Sale.total_order(current_user,@from,@to, current_shop) @total_accounts = Account.select("accounts.id as account_id, accounts.title as title") @account_data = Array.new if !@total_accounts.nil? diff --git a/app/controllers/origami/order_reservation_controller.rb b/app/controllers/origami/order_reservation_controller.rb index 3bde49ec..b20656df 100644 --- a/app/controllers/origami/order_reservation_controller.rb +++ b/app/controllers/origami/order_reservation_controller.rb @@ -98,7 +98,7 @@ class Origami::OrderReservationController < BaseOrigamiController end def get_order_info - order_reservation = OrderReservation.where("status = 'new' and shop_code='#{@shop.shop_code}'").count() + order_reservation = OrderReservation.where("status = 'new' and shop_code='#{current_shop.shop_code}'").count() render :json => order_reservation end diff --git a/app/models/sale.rb b/app/models/sale.rb index 6a6c0c3f..e14c2a95 100644 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -847,7 +847,6 @@ def self.daily_sales_list(from,to,shop_code) ELSE 0 END as credit_amount, SUM(case when (sale_payments.payment_method='giftvoucher') then sale_payments.payment_amount else 0 end) as giftvoucher_amount, SUM(case when (sale_payments.payment_method='foc') then sale_payments.payment_amount else 0 end) as foc_amount") - .along_with_sale_payments_except_void_between(from, to) .where("(sale_status = ? OR sale_status = ?) AND sales.receipt_date between ? AND ? ", 'completed', 'void', from, to) .group("sale_id").to_sql @@ -1580,10 +1579,11 @@ end end - def self.top_bottom_products(current_user,from,to,type) + def self.top_bottom_products(current_user,from,to,type,current_shop) + puts @current_shop query = Sale.joins("JOIN sale_items ON sale_items.sale_id = sales.sale_id") .completed - .where("qty > 0 AND price > 0 AND shop_code='#{shop.shop_code}'") + .where("qty > 0 AND price > 0 AND shop_code='#{current_shop.shop_code}'") .group("SUBSTRING_INDEX(product_name, ' - ', 1)") if !from.nil? && !to.nil? @@ -1607,9 +1607,8 @@ end end - def self.hourly_sales(current_user,from,to) + def self.hourly_sales(current_user,from,to, current_shop) query = Sale.group("date_format(CONVERT_TZ(receipt_date,'+00:00', '#{Time.zone.formatted_offset}'), '%I %p')") - .order('receipt_date').completed if !from.nil? && !to.nil? @@ -1626,7 +1625,7 @@ end end - def self.employee_sales(current_user,from,to) + def self.employee_sales(current_user,from,to, current_shop) shift = if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') ShiftSale.current_open_shift(current_user) @@ -1635,8 +1634,7 @@ end payments_for_credits = SalePayment.joins(:sale_audit).to_sql - query = employee_sale(shift, from, to) - + query = employee_sale(shift, from, to, current_shop) .joins("LEFT JOIN (#{payments_for_credits}) payments_for_credits ON payments_for_credits.sale_id = sales.sale_id") .select("SUM(sale_payments.payment_amount) - CASE WHEN sale_payments.payment_method = 'creditnote' THEN IFNULL(SUM(payments_for_credits.payment_amount), 0) ELSE ABS(SUM(CASE WHEN sale_payments.outstanding_amount < 0 THEN sale_payments.outstanding_amount ELSE 0 END)) END AS payment_amount, CASE WHEN sale_payments.payment_method IN ('mpu', 'visa', 'master', 'jcb', 'unionpay', 'alipay', 'paymal', 'dinga', 'JunctionPay', 'giftvoucher') THEN 'card' @@ -1682,14 +1680,14 @@ end end - def self.credit_payment(current_user=nil,from=nil,to=nil) + def self.credit_payment(current_user=nil,from=nil,to=nil, current_shop) payments_for_credits = SalePayment.joins(:sale_audit).to_sql query = SalePayment.credits .joins(:sale) .joins("LEFT JOIN (#{payments_for_credits}) payments_for_credits ON payments_for_credits.sale_id = sale_payments.sale_id") - .where("sale_payments.payment_method= ? AND sales.sale_status = ? AND sales.shop_code=?", 'creditnote', 'completed',shop.shop_code) + .where("sale_payments.payment_method= ? AND sales.sale_status = ? AND sales.shop_code=?", 'creditnote', 'completed',current_shop.shop_code) if (!from.nil? && !to.nil?) query = query.merge(Sale.receipt_date_between(from, to)) @@ -1727,7 +1725,6 @@ end def self.total_payment_methods(current_user=nil,from=nil,to=nil) query = Sale.select("CASE WHEN sp.payment_method IN ('mpu', 'visa', 'master', 'jcb', 'unionpay', 'alipay', 'paymal', 'dinga', 'JunctionPay', 'giftvoucher') THEN 'card' ELSE sp.payment_method END as payment_method") .where("sales.sale_status = 'completed'") - .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") .group("CASE WHEN sp.payment_method IN ('mpu', 'visa', 'master', 'jcb', 'unionpay', 'alipay', 'paymal', 'dinga', 'JunctionPay', 'giftvoucher') THEN 'card' ELSE sp.payment_method END") @@ -1893,11 +1890,11 @@ end # end - def self.total_order(current_user=nil,from=nil,to=nil) + def self.total_order(current_user=nil,from=nil,to=nil, current_shop) query = Sale.select("count(distinct sale_orders.order_id) as total_order") .joins(:sale_orders) - .where("shop_code='#{shop.shop_code}'") + .where("shop_code='#{current_shop.shop_code}'") .completed if (!from.nil? && !to.nil?) @@ -2109,11 +2106,11 @@ def unique_tax_profiles(order_source, customer_id) end -def self.employee_sale(shift=nil,from=nil,to=nil,from_time=nil,to_time=nil) +def self.employee_sale(shift=nil,from=nil,to=nil,from_time=nil,to_time=nil, current_shop) query = Sale.joins(:cashier) .joins(:sale_payments) - .paid.completed.where("sales.shop_code='#{shop.shop_code}'") + .paid.completed.where("sales.shop_code='#{current_shop.shop_code}'") if !from.nil? && !to.nil? query = query.receipt_date_between(from, to) end diff --git a/app/models/stock_journal.rb b/app/models/stock_journal.rb index 3c987f56..2250b23a 100755 --- a/app/models/stock_journal.rb +++ b/app/models/stock_journal.rb @@ -59,7 +59,7 @@ class StockJournal < ApplicationRecord end - def self.inventory_balances(from,to, shop) + def self.inventory_balances(from,to, current_shop) query = StockJournal.select("mii.item_instance_name as item_instance_name,balance") .joins("join menu_item_instances mii on mii.item_instance_code=stock_journals.item_code") .group("mii.item_instance_name") diff --git a/app/views/layouts/login_dashboard.html.erb b/app/views/layouts/login_dashboard.html.erb index 407b5952..01dff3c0 100755 --- a/app/views/layouts/login_dashboard.html.erb +++ b/app/views/layouts/login_dashboard.html.erb @@ -68,7 +68,7 @@
- <%= shop_detail.name %> + <%= @current_shop.name %>
diff --git a/config/application.rb b/config/application.rb index db3e8071..1f1a4a86 100755 --- a/config/application.rb +++ b/config/application.rb @@ -34,6 +34,6 @@ module SXRestaurants ) end end - + end end diff --git a/dump.rdb b/dump.rdb index afd1b003e76cf87b41aa7900f5fc423b829eea2b..69737e26ff408ca05d0737b209759b9137480b76 100644 GIT binary patch delta 155 zcmV;M0A&Bj4#*CWFc+n^-(C6$b#rB8Ep26O!tkvQ0R8~`6_X(c9+UqC9h2GyD3gE& zB#~nx7&t93GQa=;4sbUJWyZ)0V1b7f?}2a`DlF_Cf;1UM}+Ig`-^7LyzY9g_kF z8j*z>12`=+lb{A10x~wS*Z2eiF*uW<1{nl6Ei*8a(FGlo)CCifpBfK0EipL20RR5{ J1L#<5Hc=c$HV6O! delta 156 zcmX@XcY<$%fw-ahz1UwIrNyZ!y1A*jhd!+3WBAALTXb?DqbQ5HuA#-m)2fr_GD=OB zXP27zfqn84Mo}gs%gr|!f3QrR&nPWwcz}U{uec`BS1UFLv