This commit is contained in:
Myat Zin Wai Maw
2019-11-19 14:59:45 +06:30
parent 8b2da5e3da
commit 5a3f328789
16 changed files with 299 additions and 238 deletions

View File

@@ -375,7 +375,5 @@ DEPENDENCIES
web-console (>= 3.3.0)
whenever
RUBY VERSION
ruby 2.6.3p62
BUNDLED WITH
2.0.2

View File

@@ -4,7 +4,7 @@ class ApplicationController < ActionController::Base
#before_action :check_installation
protect_from_forgery with: :exception
helper_method :shop_detail, :order_reservation, :bank_integration
helper_method :order_reservation, :bank_integration
# lookup domain for db from provision
# before_action :set_locale
# helper_method :current_company,:current_login_employee,:current_user
@@ -18,6 +18,3 @@ class ApplicationController < ActionController::Base
redirect_to root_path
end
end

View File

@@ -1,6 +1,5 @@
module LoginVerification
extend ActiveSupport::Concern
included do
before_action :authenticate_session_token
helper_method :current_company,:current_shop, :current_login_employee, :current_user, :get_cashier, :order_reservation, :bank_integration, :shop_detail
@@ -19,34 +18,37 @@ module LoginVerification
def current_shop
begin
return Shop.first
shop_code ='262'
@shop =Shop.find_by_shop_code(shop_code)
return @shop
rescue
return nil
end
end
def current_login_employee
@employee = Employee.find_by_token_session(session[:session_token])
@employee = @shop.employees.find_by_token_session(session[:session_token])
end
def current_user
@current_user ||= Employee.find_by_token_session(session[:session_token]) if session[:session_token]
@current_user ||= @shop.employees.find_by_token_session(session[:session_token]) if session[:session_token]
end
# Get current Cashiers
def get_cashier
@cashier = Employee.where("role = 'cashier' AND token_session <> ''")
@cashier = @shop.employees.where("role = 'cashier' AND token_session <> ''")
end
#Shop Name in Navbor
def shop_detail
@shop = Shop.first
shop_code ='262'
@shop = Shop.find_by_shop_code(shop_code)
end
#check order reservation used
def order_reservation
order_reserve = Lookup.collection_of('order_reservation')
order_reserve = @shop.lookups.collection_of('order_reservation')
status = false
if !order_reserve.empty?
order_reserve.each do |order|
@@ -62,7 +64,7 @@ module LoginVerification
#check bank integration used
def bank_integration
bank_integration = Lookup.collection_of('bank_integration')
bank_integration = @shop.lookups.collection_of('bank_integration')
status = false
if !bank_integration.empty?
bank_integration.each do |bank|
@@ -88,7 +90,7 @@ module LoginVerification
#@current_user = User.find_by(api_key: token)
#Rails.logger.debug "token - " + token.to_s
@user = Employee.authenticate_by_token(token)
@user =Employee.authenticate_by_token(token,current_shop)
if @user
return true
#Maybe log - login?

View File

@@ -11,14 +11,15 @@ class HomeController < ApplicationController
end
end
def current_user
@current_user ||= Employee.find_by_token_session(session[:session_token]) if session[:session_token]
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("is_active = true").order("name asc")
@roles = Employee.distinct.pluck(:role)
@employees = @shop.employees.all.where("is_active = true").order("name asc")
@roles = @shop.employees.distinct.pluck(:role)
# byebug
# @roles = Lookup.collection_of("employee_roles")
@login_form = LoginForm.new()
@@ -35,7 +36,7 @@ class HomeController < ApplicationController
@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)
@employee = Employee.login(@shop,@login_form.emp_id, @login_form.password)
if @employee != nil
session[:session_token] = @employee.token_session
@@ -50,7 +51,7 @@ class HomeController < ApplicationController
@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)
@employee = Employee.login(@shop,@login_form.emp_id, @login_form.password)
if @employee != nil
if @employee.is_active
@@ -90,31 +91,30 @@ class HomeController < ApplicationController
def dashboard
@from, @to, @from_time, @to_time = get_date_range_from_params
@shop = Shop.first
today = DateTime.now.strftime('%Y-%m-%d')
if !@from.nil? && !@to.nil?
if !@from_time.nil? && @to_time.nil?
@orders = Sale::where("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()
@orders = @shop.sales::where("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("payment_status='new' and sale_status='bill' and DATE_FORMAT(receipt_date,'%Y-%m-%d') between '#{@from}' and '#{@to}'").count()
@orders = @shop.sales::where("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("payment_status='new' and sale_status='bill' and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'").count()
@orders = @shop.sales::where("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("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()
@sales = @shop.sales::where("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("payment_status='paid' and sale_status='completed' and DATE_FORMAT(receipt_date,'%Y-%m-%d') between '#{@from}' and '#{@to}'").count()
@sales = @shop.sales::where("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("payment_status='paid' and sale_status='completed' and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'").count()
@sales = @shop.sales::where("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").sum('i.qty')
@bottom_products = Sale.top_bottom_products(today,current_user,@from,@to,@from_time,@to_time,"bottom").sum('i.qty')
@hourly_sales = Sale.hourly_sales(today,current_user,@from,@to,@from_time,@to_time).sum(:grand_total)
@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)
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|
@@ -126,43 +126,43 @@ class HomeController < ApplicationController
end
end
end
@inventories = StockJournal.inventory_balances(today,@from,@to,@from_time,@to_time).sum(:balance)
@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)
@total_card = Sale.total_card_sale(today,current_user,@from,@to,@from_time,@to_time)
@total_credit = Sale.credit_payment(today,current_user,@from,@to,@from_time,@to_time)
@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(today,current_user,@from,@to,@from_time,@to_time)
@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('card', today, current_user,@from,@to,@from_time,@to_time)
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(payment.payment_method, today, current_user,@from,@to,@from_time,@to_time)
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(today,current_user,@from,@to,@from_time,@to_time)
@total_customer, @total_dinein, @total_takeaway, @total_membership = Sale.total_customer(today,current_user,@from,@to,@from_time,@to_time)
@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(today,current_user,@from,@to,@from_time,@to_time)
@total_accounts = Sale.total_account(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(account.account_id, today,current_user,@from,@to,@from_time,@to_time)
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(today,current_user,@from,@to,@from_time,@to_time)
@total_foc_items = Sale.total_foc_items(today,current_user,@from,@to,@from_time,@to_time)
@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
@@ -170,7 +170,7 @@ class HomeController < ApplicationController
def destroy
# clear in employee session
Employee.logout(session[:session_token])
Employee.logout(@shop,session[:session_token])
session[:session_token] = nil
# redirect_to root_path
render :json => {:status=> "Success", :url => root_path }.to_json
@@ -195,7 +195,7 @@ class HomeController < ApplicationController
def route_by_role(employee)
if employee.role == "administrator"
# redirect_to dashboard_path
shift = ShiftSale.current_open_shift(employee.id)
shift = ShiftSale.current_open_shift(employee.id,@shop)
if !shift.nil?
redirect_to origami_root_path
else
@@ -203,7 +203,7 @@ class HomeController < ApplicationController
end
elsif employee.role == "cashier"
#check if cashier has existing open cashier
shift = ShiftSale.current_open_shift(employee.id)
shift = ShiftSale.current_open_shift(employee.id,@shop)
if !shift.nil?
redirect_to origami_dashboard_path
# redirect_to origami_root_path

View File

@@ -3,7 +3,7 @@ class Employee < ApplicationRecord
has_many :commissioners
has_many :shit_sales
belongs_to :order_queue_station
belongs_to :shop
validates_presence_of :name, :role
validates_presence_of :password, :on => [:create]
validates :emp_id, uniqueness: true, numericality: true, length: {in: 1..4}, allow_blank: true
@@ -20,9 +20,9 @@ class Employee < ApplicationRecord
Employee.select("id, name").map { |e| [e.name, e.id] }
end
def self.login(emp_id, password)
user = Employee.find_by_emp_id(emp_id)
expiry_time = login_expiry_time
def self.login(shop,emp_id, password)
user = shop.employees.find_by_emp_id(emp_id)
expiry_time = login_expiry_time(shop)
if (user)
#user.authenticate(password)
if (user.authenticate(password))
@@ -37,10 +37,10 @@ class Employee < ApplicationRecord
end
def self.authenticate_by_token(session_token)
def self.authenticate_by_token(session_token,shop)
if (session_token)
user = Employee.find_by_token_session(session_token)
expiry_time = login_expiry_time
user = shop.employees.find_by_token_session(session_token)
expiry_time = login_expiry_time(shop)
if user && user.session_expiry.utc > DateTime.now.utc
#Extend the login time each time authenticatation take place
@@ -55,9 +55,9 @@ class Employee < ApplicationRecord
return false
end
def self.logout(session_token)
def self.logout(shop,session_token)
if (session_token)
user = Employee.find_by_token_session(session_token)
user = shop.employees.find_by_token_session(session_token)
if user
user.token_session = nil
@@ -73,9 +73,9 @@ class Employee < ApplicationRecord
retry
end
def self.login_expiry_time
def self.login_expiry_time(shop)
expiry_time = 30
login_expiry = Lookup.collection_of('expiry_time')
login_expiry = shop.lookups.collection_of('expiry_time')
if !login_expiry.empty?
login_expiry.each do |exp_time|
if exp_time[0].downcase == "login"

View File

@@ -1,6 +1,7 @@
class Lookup < ApplicationRecord
has_many :accounts
belongs_to :shop
def available_types
{'Employee Roles' => 'employee_roles',

View File

@@ -7,6 +7,7 @@ class Sale < ApplicationRecord
belongs_to :cashier, foreign_key: "cashier_id", class_name: "Employee"
belongs_to :customer, :optional => true
belongs_to :shift_sale
belongs_to :shop
has_many :sale_items
has_many :sale_discount_items
has_many :sale_discounts
@@ -1502,10 +1503,10 @@ end
return tax
end
def self.top_bottom_products(today,current_user,from,to,from_time,to_time,type)
def self.top_bottom_products(today,current_user,from,to,from_time,to_time,type,shop)
if (!from.nil? && !to.nil?) && (from != "" && to!="")
if current_user.nil?
query = Sale.top_bottom(today,nil,from,to,from_time,to_time)
query = Sale.top_bottom(shop,today,nil,from,to,from_time,to_time)
if type == "top"
query = query.group('i.product_name')
@@ -1516,7 +1517,7 @@ end
end
else
if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor'
query = Sale.top_bottom(today,nil,from,to,from_time,to_time)
query = Sale.top_bottom(shop,today,nil,from,to,from_time,to_time)
if type == "top"
query = query.group('i.product_name')
.order("SUM(i.qty) DESC").limit(20)
@@ -1525,9 +1526,9 @@ end
.order("SUM(i.qty) ASC").limit(20)
end
else
shift = ShiftSale.current_open_shift(current_user.id)
shift = ShiftSale.current_open_shift(current_user.id,shop)
if !shift.nil?
query = Sale.top_bottom(today,shift,from,to,from_time,to_time)
query = Sale.top_bottom(shop,today,shift,from,to,from_time,to_time)
if type == "top"
query = query.group('i.product_name')
.order("SUM(i.qty) DESC").limit(20)
@@ -1540,7 +1541,7 @@ end
end
else
if current_user.nil?
query = Sale.top_bottom(today).group('i.product_name')
query = Sale.top_bottom(shop,today).group('i.product_name')
if type == "top"
query = query.order("SUM(i.qty) DESC").limit(20)
@@ -1549,16 +1550,16 @@ end
end
else
if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor'
query = Sale.top_bottom(today).group('i.product_name')
query = Sale.top_bottom(shop,today).group('i.product_name')
if type == "top"
query = query.order("SUM(i.qty) DESC").limit(20)
elsif type == "bottom"
query = query.order("SUM(i.qty) ASC").limit(20)
end
else
shift = ShiftSale.current_open_shift(current_user.id)
shift = ShiftSale.current_open_shift(current_user.id,shop)
if !shift.nil?
query = Sale.top_bottom(today,shift).group('i.product_name')
query = Sale.top_bottom(shop,today,shift).group('i.product_name')
if type == "top"
query = query.order("SUM(i.qty) DESC").limit(20)
elsif type == "bottom"
@@ -1570,52 +1571,52 @@ end
end
end
def self.hourly_sales(today,current_user,from,to,from_time,to_time)
def self.hourly_sales(today,current_user,from,to,from_time,to_time,shop)
if (!from.nil? && !to.nil?) && (from != "" && to!="")
if current_user.nil?
query = Sale.hourly_sale_data(today,nil,from,to,from_time,to_time)
query = Sale.hourly_sale_data(shop,today,nil,from,to,from_time,to_time)
else
if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor'
query = Sale.hourly_sale_data(today,nil,from,to,from_time,to_time)
query = Sale.hourly_sale_data(shop,today,nil,from,to,from_time,to_time)
else
shift = ShiftSale.current_open_shift(current_user.id)
shift = ShiftSale.current_open_shift(current_user.id,shop)
if !shift.nil?
query = Sale.hourly_sale_data(today,shift,from,to,from_time,to_time)
query = Sale.hourly_sale_data(shop,today,shift,from,to,from_time,to_time)
end
end
end
else
if current_user.nil?
query = Sale.hourly_sale_data(today)
query = Sale.hourly_sale_data(shop,today)
else
if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor'
query = Sale.hourly_sale_data(today)
query = Sale.hourly_sale_data(shop,today)
else
shift = ShiftSale.current_open_shift(current_user.id)
shift = ShiftSale.current_open_shift(current_user.id,shop)
if !shift.nil?
query = Sale.hourly_sale_data(today,shift)
query = Sale.hourly_sale_data(shop,today,shift)
end
end
end
end
end
def self.employee_sales(today,current_user,from,to,from_time,to_time)
def self.employee_sales(today,current_user,from,to,from_time,to_time,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.id)
ShiftSale.current_open_shift(current_user.id,shop)
end
payments_for_credits = SalePayment.joins(:sale_audit).to_sql
query = employee_sale(today, shift, from, to, from_time, to_time)
query = employee_sale(shop,today, shift, from, to, from_time, to_time)
.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'
ELSE sale_payments.payment_method END AS payment_method, employees.name AS e_name")
end
def self.total_trans(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = Sale.select("SUM(grand_total) as total_sale, COUNT(sales.sale_id) as total_count")
def self.total_trans(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil,shop)
query = shop.sales.select("SUM(grand_total) as total_sale, COUNT(sales.sale_id) as total_count")
.where('sale_status = "completed"')
if (!from.nil? && !to.nil?) && (from != "" && to!="")
@@ -1627,15 +1628,15 @@ end
query = query.date_on(today)
end
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)
if shift = ShiftSale.current_open_shift(current_user.id,shop)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
return query
end
def self.total_card_sale(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = Sale.joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id")
def self.total_card_sale(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil,shop)
query = shop.sales.joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id")
.where('sales.sale_status = "completed" and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay")')
if (!from.nil? && !to.nil?) && (from != "" && to!="")
query = query.date_between(from, to)
@@ -1646,20 +1647,20 @@ end
query = query.date_on(today)
end
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)
if shift = ShiftSale.current_open_shift(current_user.id,shop)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
query = query.sum("sp.payment_amount")
end
def self.credit_payment(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
def self.credit_payment(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil,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 = ?", 'creditnote', 'completed')
.where("sale_payments.payment_method= ? AND sales.sale_status = ? AND sales.shop_code=?", 'creditnote', 'completed',shop.shop_code)
if (!from.nil? && !to.nil?) && (from != "" && to!="")
query = query.merge(Sale.date_between(from, to))
@@ -1670,7 +1671,7 @@ end
query = query.merge(Sale.date_on(today))
end
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)
if shift = ShiftSale.current_open_shift(current_user.id,shop)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -1678,8 +1679,8 @@ end
return query.sum("sale_payments.payment_amount - IFNULL(payments_for_credits.payment_amount, 0)")
end
def self.summary_sale_receipt(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax')
def self.summary_sale_receipt(shop,today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = shop.sales.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax')
.where('sale_status = "completed"')
if (!from.nil? && !to.nil?) && (from != "" && to!="")
query = query.date_between(from, to)
@@ -1690,15 +1691,15 @@ end
query = query.date_on(today)
end
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)
if shift = ShiftSale.current_open_shift(current_user.id,shop)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
query = query.first()
end
def self.total_payment_methods(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = Sale.select("distinct sp.payment_method")
def self.total_payment_methods(shop,today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = shop.sales.select("distinct sp.payment_method")
.where('sales.sale_status = "completed"')
.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id")
if (!from.nil? && !to.nil?) && (from != "" && to!="")
@@ -1710,17 +1711,17 @@ end
query = query.date_on(today)
end
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)
if shift = ShiftSale.current_open_shift(current_user.id,shop)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
return query
end
def self.payment_sale(payment_method, today, current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
def self.payment_sale(shop,payment_method, today, current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
payments_for_credits = SalePayment.joins(:sale_audit).to_sql
query = Sale.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")
query = shop.sales.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")
.joins(:sale_payments)
.joins("LEFT JOIN (#{payments_for_credits}) payments_for_credits ON payments_for_credits.sale_id = sales.sale_id")
.completed
@@ -1741,15 +1742,15 @@ end
end
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)
if shift = ShiftSale.current_open_shift(current_user.id,shop)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
query.first
end
def self.total_customer(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
total_dinein_takeaway = self.total_dinein_takeaway(today,current_user,from,to,from_time,to_time)
def self.total_customer(shop,today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
total_dinein_takeaway = self.total_dinein_takeaway(shop,today,current_user,from,to,from_time,to_time)
dinein_cnt = 0
takeaway_cnt = 0
if !total_dinein_takeaway.nil?
@@ -1758,7 +1759,7 @@ end
takeaway_cnt = total_dinein_takeaway[0].total_take_cus
end
end
membership_cnt = self.total_membership(today,current_user,from,to,from_time,to_time)
membership_cnt = self.total_membership(shop,today,current_user,from,to,from_time,to_time)
member_cnt = 0
if !membership_cnt.nil?
member_cnt = membership_cnt.total_memb_cus
@@ -1771,8 +1772,8 @@ end
return total_cus, dinein_cnt, takeaway_cnt, member_cnt
end
def self.total_dinein_takeaway(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = Sale.select("(CASE WHEN c.customer_type='Dinein' THEN count(sales.customer_id) ELSE 0 END) as total_dinein_cus, (CASE WHEN c.customer_type='Takeaway' THEN count(sales.customer_id) ELSE 0 END) as total_take_cus")
def self.total_dinein_takeaway(shop,today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = shop.sales.select("(CASE WHEN c.customer_type='Dinein' THEN count(sales.customer_id) ELSE 0 END) as total_dinein_cus, (CASE WHEN c.customer_type='Takeaway' THEN count(sales.customer_id) ELSE 0 END) as total_take_cus")
.joins("JOIN customers as c ON c.customer_id = sales.customer_id")
.where('sales.sale_status = "completed" and c.membership_id is null')
if (!from.nil? && !to.nil?) && (from != "" && to!="")
@@ -1784,15 +1785,15 @@ end
query = query.date_on(today)
end
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)
if shift = ShiftSale.current_open_shift(current_user.id,shop)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
query = query.first()
end
def self.total_membership(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = Sale.select("count(distinct sales.customer_id) as total_memb_cus")
def self.total_membership(shop,today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = shop.sales.select("count(distinct sales.customer_id) as total_memb_cus")
.joins("JOIN customers as c ON c.customer_id = sales.customer_id")
.where('sales.sale_status = "completed" and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))')
if (!from.nil? && !to.nil?) && (from != "" && to!="")
@@ -1804,7 +1805,7 @@ end
query = query.date_on(today)
end
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)
if shift = ShiftSale.current_open_shift(current_user.id,shop)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -1857,8 +1858,8 @@ end
# query = query.first()
# end
def self.total_order(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = Sale.select("count(distinct sale_orders.order_id) as total_order")
def self.total_order(shop,today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = shop.sales.select("count(distinct sale_orders.order_id) as total_order")
.joins(:sale_orders)
.completed
if (!from.nil? && !to.nil?) && (from != "" && to!="")
@@ -1871,7 +1872,7 @@ end
end
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)
if shift = ShiftSale.current_open_shift(current_user.id,shop)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -1879,8 +1880,8 @@ end
query = query.first
end
def self.total_account(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = Sale.select("distinct b.id as account_id, b.title as title")
def self.total_account(shop,today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = shop.sales.select("distinct b.id as account_id, b.title as title")
.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id")
.joins("JOIN accounts as b ON b.id = a.account_id")
.where('sales.sale_status = "completed"')
@@ -1893,15 +1894,15 @@ end
query = query.date_on(today)
end
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)
if shift = ShiftSale.current_open_shift(current_user.id,shop)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
return query
end
def self.account_data(account_id, today, current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc")
def self.account_data(shop,account_id, today, current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = shop.sales.select("count(*) as cnt_acc, SUM(a.price) as total_acc")
.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id")
.where("sales.sale_status = 'completed' and a.account_id ='#{account_id}'")
if (!from.nil? && !to.nil?) && (from != "" && to!="")
@@ -1913,15 +1914,15 @@ end
query = query.date_on(today)
end
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)
if shift = ShiftSale.current_open_shift(current_user.id,shop)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
query = query.first
end
def self.top_items(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price")
def self.top_items(shop,today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = shop.sales.select("a.product_name as item_name, SUM(a.price) as item_total_price")
.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id")
.where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed'")
if (!from.nil? && !to.nil?) && (from != "" && to!="")
@@ -1933,7 +1934,7 @@ end
query = query.date_on(today)
end
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)
if shift = ShiftSale.current_open_shift(current_user.id,shop)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -1942,8 +1943,8 @@ end
.first()
end
def self.total_foc_items(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id")
def self.total_foc_items(shop,today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = shop.sales.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id")
.where("sales.sale_status = 'completed' and a.status='foc' and a.product_name like '%FOC%'")
if (!from.nil? && !to.nil?) && (from != "" && to!="")
query = query.date_between(from, to)
@@ -2096,9 +2097,9 @@ def unique_tax_profiles(order_source, customer_id)
return tax_data
end
def self.top_bottom(today,shift=nil,from=nil,to=nil,from_time=nil,to_time=nil)
def self.top_bottom(shop,today,shift=nil,from=nil,to=nil,from_time=nil,to_time=nil)
if !from.nil? && !to.nil?
query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," +
query = shop.sales.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," +
" i.price as unit_price,i.product_name")
.joins("JOIN sale_items i ON i.sale_id = sales.sale_id")
if !from_time.nil? && !to_time.nil?
@@ -2112,7 +2113,7 @@ def self.top_bottom(today,shift=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = query.where("shift_sale_id='#{shift.id}'")
end
else
query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," +
query = shop.sales.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," +
" i.price as unit_price,i.product_name")
.joins("JOIN sale_items i ON i.sale_id = sales.sale_id")
.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'"+
@@ -2124,9 +2125,9 @@ def self.top_bottom(today,shift=nil,from=nil,to=nil,from_time=nil,to_time=nil)
return query
end
def self.hourly_sale_data(today,shift=nil,from=nil,to=nil,from_time=nil,to_time=nil)
def self.hourly_sale_data(shop,today,shift=nil,from=nil,to=nil,from_time=nil,to_time=nil)
if !from.nil? && !to.nil?
query = Sale.select("grand_total")
query = shop.sales.select("grand_total")
if !from_time.nil? && !to_time.nil?
query = query.where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%H:%M") between ? and ?',from,to,from_time,to_time)
else
@@ -2138,7 +2139,7 @@ def self.hourly_sale_data(today,shift=nil,from=nil,to=nil,from_time=nil,to_time=
query = query.group("date_format(CONVERT_TZ(receipt_date,'+00:00', '+06:30'), '%I %p')")
.order('receipt_date')
else
query = Sale.select("grand_total")
query = shop.sales.select("grand_total")
.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today)
if !shift.nil?
query = query.where("shift_sale_id='#{shift.id}'")
@@ -2150,8 +2151,8 @@ def self.hourly_sale_data(today,shift=nil,from=nil,to=nil,from_time=nil,to_time=
return query
end
def self.employee_sale(today,shift=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = Sale.joins(:cashier)
def self.employee_sale(shop,today,shift=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = shop.sales.joins(:cashier)
.joins(:sale_payments)
.paid.completed
if !from.nil? && !to.nil?

View File

@@ -16,6 +16,7 @@ class ShiftSale < ApplicationRecord
belongs_to :cashier_terminal
belongs_to :employee, :foreign_key => 'employee_id'
has_many :sales
belongs_to :shop
def self.current_shift
# today_date = DateTime.now.strftime("%Y-%m-%d")
@@ -23,12 +24,12 @@ class ShiftSale < ApplicationRecord
return shift
end
def self.current_open_shift(current_user)
def self.current_open_shift(current_user,shop)
#if current_user
#find open shift where is open today and is not closed and login by current cashier
#DATE(shift_started_at)=? and
today_date = DateTime.now.strftime("%Y-%m-%d")
shift = ShiftSale.where("shift_started_at is not null and shift_closed_at is null and employee_id = #{current_user}").take
shift = shop.shift_sales.where("shift_started_at is not null and shift_closed_at is null and employee_id = #{current_user}").take
return shift
#end
end

View File

@@ -3,8 +3,36 @@ class Shop < ApplicationRecord
# Shop Image Uploader
mount_uploader :logo, ShopImageUploader
has_many :accounts
has_many :bookings
has_many :cashier_terminals
has_many :commissioners
has_many :commissions
has_many :customers
has_many :dining_facilities
has_many :dining_queues
has_many :employees
has_many :inventory_definitions
has_many :item_sets
has_many :lookups
has_many :membership_settings
has_many :menus
has_many :order_queue_stations
has_many :orders
has_many :payment_journals
has_many :payment_method_settings
has_many :print_settings
has_many :sales
has_many :products
has_many :promotions
has_many :seed_generators
has_many :stock_checks
has_many :stock_journals
has_many :surveys
has_many :tax_profiles
has_many :zones
has_many :display_images
has_many :shift_sales
accepts_nested_attributes_for :display_images
def file_data=(input_data)

View File

@@ -1,5 +1,5 @@
class StockJournal < ApplicationRecord
belongs_to :shop
SALES_TRANS = "sale"
ORDER_TRANS = "order"
STOCK_CHECK_TRANS = "stock_check"
@@ -52,9 +52,9 @@ class StockJournal < ApplicationRecord
journal.save
end
def self.inventory_balances(today,from,to,from_time,to_time)
def self.inventory_balances(today,from,to,from_time,to_time,shop)
if !from.nil? && !to.nil?
query = StockJournal.select("mii.item_instance_name as item_instance_name,balance")
query = shop.stock_journals.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")
if !from_time.nil? && !to_time.nil?
query = query.where("DATE_FORMAT(CONVERT_TZ(stock_journals.created_at,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'")
@@ -64,7 +64,7 @@ class StockJournal < ApplicationRecord
query = query.group("mii.item_instance_name")
.order("mii.item_instance_name ASC")
else
query = StockJournal.select("mii.item_instance_name as item_instance_name,balance")
query = shop.stock_journals.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")
.where("DATE_FORMAT(stock_journals.created_at,'%Y-%m-%d') = '#{today}'")
.group("mii.item_instance_name")

View File

@@ -0,0 +1,33 @@
class AddShopCode < ActiveRecord::Migration[5.1]
def change
add_column :accounts, :shop_code, :string, :default => '262'
add_column :bookings, :shop_code, :string, :default => '262'
add_column :cashier_terminals, :shop_code, :string, :default => '262'
add_column :commissioners, :shop_code, :string, :default => '262'
add_column :commissions, :shop_code, :string, :default => '262'
add_column :customers, :shop_code, :string, :default => '262'
add_column :dining_facilities, :shop_code, :string, :default => '262'
add_column :dining_queues, :shop_code, :string, :default => '262'
add_column :employees, :shop_code, :string, :default => '262'
add_column :inventory_definitions, :shop_code, :string, :default => '262'
add_column :item_sets, :shop_code, :string, :default => '262'
add_column :lookups, :shop_code, :string, :default => '262'
add_column :membership_settings, :shop_code, :string, :default => '262'
add_column :menus, :shop_code, :string, :default => '262'
add_column :order_queue_stations, :shop_code, :string, :default => '262'
add_column :orders, :shop_code, :string, :default => '262'
add_column :payment_journals, :shop_code, :string, :default => '262'
add_column :payment_method_settings, :shop_code, :string, :default => '262'
add_column :print_settings, :shop_code, :string, :default => '262'
add_column :sales, :shop_code, :string, :default => '262'
add_column :products, :shop_code, :string, :default => '262'
add_column :promotions, :shop_code, :string, :default => '262'
add_column :seed_generators, :shop_code, :string, :default => '262'
add_column :shift_sales, :shop_code, :string, :default => '262'
add_column :stock_checks, :shop_code, :string, :default => '262'
add_column :stock_journals, :shop_code, :string, :default => '262'
add_column :surveys, :shop_code, :string, :default => '262'
add_column :tax_profiles, :shop_code, :string, :default => '262'
add_column :zones, :shop_code, :string, :default => '262'
end
end