Files
sx-fc/app/controllers/home_controller.rb
2020-06-10 18:12:15 +06:30

235 lines
8.2 KiB
Ruby
Executable File

class HomeController < ApplicationController
# layout "application", except: [:index, :show]
# skip_before_action :authenticate, only: [:index, :show, :create, :update, :destroy]
before_action :check_user, only: :dashboard
# Special check for only dashboard
def check_user
if current_user.nil?
redirect_to root_path
end
end
def current_user
@current_user ||= Employee.find_by_token_session(session[:session_token]) if session[:session_token]
end
def index
# @employees = Employee.all_emp_except_waiter.order("name asc")
@employees = Employee.all.where("is_active = true").order("name asc")
@roles = Employee.distinct.pluck(:role)
# byebug
# @roles = Lookup.collection_of("employee_roles")
@login_form = LoginForm.new()
render "layouts/login_dashboard", layout: false
end
def show
@login_form = LoginForm.new()
@login_form.emp_id = params[:emp_id]
render "layouts/login", layout: false
end
def update
@login_form = LoginForm.new()
@login_form.emp_id = params[:emp_id]
@login_form.password = params[:login_form][:password]
@employee = Employee.login(@login_form.emp_id, @login_form.password)
if @employee != nil
session[:session_token] = @employee.token_session
route_by_role(@employee)
else
flash[:notice] ="Invalid PIN for Employee. Please try again!"
render :show
end
end
def create
@login_form = LoginForm.new()
@login_form.emp_id = params[:login_form][:emp_id]
@login_form.password = params[:login_form][:password]
@employee = Employee.login(@login_form.emp_id, @login_form.password)
if @employee != nil
if @employee.is_active
if @employee.role == "administrator"
session[:session_token] = @employee.token_session
redirect_to dashboard_path
elsif @employee.role == "cashier"
session[:session_token] = @employee.token_session
route_by_role(@employee)
elsif @employee.role == "manager"
session[:session_token] = @employee.token_session
redirect_to dashboard_path
elsif @employee.role == "supervisor"
session[:session_token] = @employee.token_session
route_by_role(@employee)
# redirect_to origami_root_path
elsif @employee.role == "waiter"
session[:session_token] = @employee.token_session
route_by_role(@employee)
elsif @employee.role == "account"
session[:session_token] = @employee.token_session
redirect_to reports_dailysale_index_path
elsif @employee.role == "kitchen"
session[:session_token] = @employee.token_session
redirect_to oqs_root_path
else
render :index
end
else
redirect_to root_path, :notice => "This Employee is not active"
end
else
redirect_to root_path, :notice => "Username and Password doesn't match!"
end
end
def dashboard
@from, @to = get_date_range_from_params
@shop = Shop.current_shop
@orders = Sale.receipt_date_between(@from, @to).where("payment_status = 'new' and sale_status = 'bill'")
@sales = Sale.receipt_date_between(@from, @to).completed
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
if shift = ShiftSale.current_open_shift(current_user.id)
@orders = @orders.where(shift_sale_id: shift.id)
@sales = @sales.where(shift_sale_id: shift.id)
end
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)
# .group_by_hour(:created_at, :time_zone => 'Asia/Rangoon',format: '%I:%p')
# .sum(:grand_total)
employee_sales = Sale.employee_sales(current_user,@from,@to)
# .sum("(CASE WHEN sp.payment_method='cash' THEN ((sp.payment_amount) - (sales.amount_changed)) ELSE (sp.payment_amount) END)")
@employee_sales = []
if !employee_sales.nil?
employee_sales.each do |emp|
emp_data = []
if emp.payment_amount > 0
emp_data.push([emp.e_name, emp.payment_amount])
@employee_sales.push({'name' => emp.payment_method, 'data' => emp_data})
end
end
end
@inventories = StockJournal.inventory_balances(@from,@to).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)
@sale_data = Array.new
@total_payment_methods = Sale.total_payment_methods(current_user,@from,@to)
if !@total_payment_methods.nil?
@total_payment_methods.each do |payment|
pay = Sale.payment_sale(payment.payment_method, current_user,@from,@to)
@sale_data.push({payment.payment_method => pay.payment_amount})
end
end
@summ_sale = Sale.summary_sale_receipt(current_user,@from,@to)
@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_accounts = Account.select("accounts.id as account_id, accounts.title as title")
@account_data = Array.new
if !@total_accounts.nil?
@total_accounts.each do |account|
acc = Sale.account_data(account.account_id,current_user,@from,@to)
if !acc.nil? && acc.cnt_acc > 0
@account_data.push({account.title => acc.cnt_acc, account.title + '_amount' => acc.total_acc})
end
end
@total_accounts = @total_accounts.reject.with_index { |x, i| @account_data[i].nil? }
end
@top_items = Sale.top_items(current_user,@from,@to)
@total_foc_items = Sale.total_foc_items(current_user,@from,@to)
# get printer info
@print_settings = PrintSetting.get_precision_delimiter()
end
def destroy
# clear in employee session
Employee.logout(session[:session_token])
session[:session_token] = nil
# redirect_to root_path
render :json => {:status=> "Success", :url => root_path }.to_json
end
def run_teamviewer
if File.directory?("/opt/teamviewer")
output = IO.popen('/opt/teamviewer/tv_bin/script/teamviewer')
render :json => {:status=> true}
else
render :json => {:status=> false}
end
end
private
# Never trust parameters from the scary internet, only allow the white list through.
def settings_home_params
params.require(:login_form).permit(:emp_id, :password)
end
def route_by_role(employee)
if employee.role == "administrator"
# redirect_to dashboard_path
shift = ShiftSale.current_open_shift(employee.id)
if !shift.nil?
redirect_to origami_root_path
else
redirect_to dashboard_path
end
elsif employee.role == "cashier"
#check if cashier has existing open cashier
shift = ShiftSale.current_open_shift(employee.id)
if !shift.nil?
redirect_to origami_dashboard_path
# redirect_to origami_root_path
else
redirect_to new_origami_shift_path
end
elsif employee.role == "manager"
redirect_to dashboard_path
elsif employee.role == "waiter"
redirect_to origami_dashboard_path
elsif employee.role == "crm"
redirect_to crm_root_path
elsif employee.role == "supervisor"
redirect_to origami_dashboard_path
elsif employee.role == "account"
redirect_to reports_dailysale_index_path
end
end
def get_date_range_from_params
if params[:from].present? && params[:to].present?
if params[:from_time].present? && params[:to_time].present?
from = Time.parse("#{params[:from]} #{params[:from_time]}")
to = Time.parse("#{params[:to]} #{params[:to_time]}")
else
from = Time.parse(params[:from])
to = Time.parse(params[:to]).end_of_day
end
else
from = Time.now.beginning_of_day
to = Time.now.end_of_day
end
return from, to
end
end