Files
sx-fc/app/controllers/home_controller.rb
Myat Zin Wai Maw d1ab2c194d shop code
2019-11-22 18:24:02 +06:30

248 lines
10 KiB
Ruby
Executable File

class HomeController < ApplicationController
# layout "application", except: [:index, :show]
# skip_before_action :authenticate, only: [:index, :show, :create, :update, :destroy]
include PrecisionHelper
before_action :check_user, only: :dashboard
# Special check for only dashboard
def check_user
if current_user.nil?
redirect_to root_path
end
end
##already check current_user with helper_method
# 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("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")
@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(current_shop,@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(current_shop,@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, @from_time, @to_time = get_date_range_from_params
today = DateTime.now.strftime('%Y-%m-%d')
if !@from.nil? && !@to.nil?
if !@from_time.nil? && @to_time.nil?
@orders = Sale::where("shop_code='#{@shop.shop_code}' and payment_status='new' and sale_status='bill' and DATE_FORMAT(receipt_date,'%Y-%m-%d') between '#{@from}' and '#{@to}' and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%H:%m') between '#{@from_time}' and '#{@to_time}'").count()
else
@orders = Sale::where("shop_code='#{@shop.shop_code}' and payment_status='new' and sale_status='bill' and DATE_FORMAT(receipt_date,'%Y-%m-%d') between '#{@from}' and '#{@to}'").count()
end
else
@orders = Sale::where("shop_code='#{@shop.shop_code}' and payment_status='new' and sale_status='bill' and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'").count()
end
if !@from.nil? && !@to.nil?
if !@from_time.nil? && @to_time.nil?
@sales = Sale::where("shop_code='#{@shop.shop_code}' and payment_status='paid' and sale_status='completed' and DATE_FORMAT(receipt_date,'%Y-%m-%d') between '#{@from}' and '#{@to}' and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%H:%m') between '#{@from_time}' and '#{@to_time}'").count()
else
@sales = Sale::where("shop_code='#{@shop.shop_code}' and payment_status='paid' and sale_status='completed' and DATE_FORMAT(receipt_date,'%Y-%m-%d') between '#{@from}' and '#{@to}'").count()
end
else
@sales = Sale::where("shop_code='#{@shop.shop_code}' and payment_status='paid' and sale_status='completed' and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'").count()
end
@top_products = Sale.top_bottom_products(today,current_user,@from,@to,@from_time,@to_time,"top",@shop).sum('i.qty')
@bottom_products = Sale.top_bottom_products(today,current_user,@from,@to,@from_time,@to_time,"bottom",@shop).sum('i.qty')
@hourly_sales = Sale.hourly_sales(today,current_user,@from,@to,@from_time,@to_time,@shop).sum(:grand_total)
employee_sales = Sale.employee_sales(today,current_user,@from,@to,@from_time,@to_time,@shop)
@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(today,@from,@to,@from_time,@to_time,@shop).sum(:balance)
@total_trans = Sale.total_trans(today,current_user,@from,@to,@from_time,@to_time,@shop)
@total_card = Sale.total_card_sale(today,current_user,@from,@to,@from_time,@to_time,@shop)
@total_credit = Sale.credit_payment(today,current_user,@from,@to,@from_time,@to_time,@shop)
@sale_data = Array.new
@total_payment_methods = Sale.total_payment_methods(@shop,today,current_user,@from,@to,@from_time,@to_time)
if !@total_payment_methods.nil?
@total_payment_methods.each do |payment|
if payment.payment_method == "mpu" || payment.payment_method == "visa" || payment.payment_method == "master" || payment.payment_method == "jcb" || payment.payment_method == "unionpay" || payment.payment_method == "alipay"
pay = Sale.payment_sale(@shop,'card', today, current_user,@from,@to,@from_time,@to_time)
@sale_data.push({'card' => pay.payment_amount})
else
pay = Sale.payment_sale(@shop,payment.payment_method, today, current_user,@from,@to,@from_time,@to_time)
@sale_data.push({payment.payment_method => pay.payment_amount})
end
end
end
@summ_sale = Sale.summary_sale_receipt(@shop,today,current_user,@from,@to,@from_time,@to_time)
@total_customer, @total_dinein, @total_takeaway, @total_membership = Sale.total_customer(@shop,today,current_user,@from,@to,@from_time,@to_time)
@total_order = Sale.total_order(@shop,today,current_user,@from,@to,@from_time,@to_time)
@total_accounts = Sale.total_account(@shop,today,current_user,@from,@to,@from_time,@to_time)
@account_data = Array.new
if !@total_accounts.nil?
@total_accounts.each do |account|
acc = Sale.account_data(@shop,account.account_id, today,current_user,@from,@to,@from_time,@to_time)
if !acc.nil?
@account_data.push({account.title => acc.cnt_acc, account.title + '_amount' => acc.total_acc})
end
end
end
@top_items = Sale.top_items(@shop,today,current_user,@from,@to,@from_time,@to_time)
@total_foc_items = Sale.total_foc_items(@shop,today,current_user,@from,@to,@from_time,@to_time)
# get printer info
# @print_settings = get_precision_delimiter
end
def destroy
# clear in employee session
Employee.logout(@shop,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)
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)
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
from = params[:from]
to = params[:to]
from_time = params[:from_time]
to_time = params[:to_time]
if from.present? && to.present?
# f_date = DateTime.parse(from)
# t_date = DateTime.parse(to)
# f_time = Time.mktime(f_date.year,f_date.month,f_date.day,f_date.hour,f_date.min,f_date.sec)
# t_time = Time.mktime(t_date.year,t_date.month,t_date.day,t_date.hour,t_date.min,t_date.sec)
# from = f_time.beginning_of_day.utc.getlocal
# to = t_time.end_of_day.utc.getlocal
from = DateTime.parse(from).utc.getlocal.strftime('%Y-%m-%d')
to = DateTime.parse(to).utc.getlocal.strftime('%Y-%m-%d')
end
return from, to, from_time, to_time
end
end