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) web-console (>= 3.3.0)
whenever whenever
RUBY VERSION
ruby 2.6.3p62
BUNDLED WITH BUNDLED WITH
2.0.2 2.0.2

View File

@@ -1,10 +1,10 @@
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
include LoginVerification include LoginVerification
#before_action :check_installation #before_action :check_installation
protect_from_forgery with: :exception 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 # lookup domain for db from provision
# before_action :set_locale # before_action :set_locale
# helper_method :current_company,:current_login_employee,:current_user # helper_method :current_company,:current_login_employee,:current_user
@@ -18,6 +18,3 @@ class ApplicationController < ActionController::Base
redirect_to root_path redirect_to root_path
end end
end end

View File

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

View File

@@ -3,7 +3,7 @@ class HomeController < ApplicationController
# skip_before_action :authenticate, only: [:index, :show, :create, :update, :destroy] # skip_before_action :authenticate, only: [:index, :show, :create, :update, :destroy]
include PrecisionHelper include PrecisionHelper
before_action :check_user, only: :dashboard before_action :check_user, only: :dashboard
# Special check for only dashboard # Special check for only dashboard
def check_user def check_user
if current_user.nil? if current_user.nil?
@@ -11,14 +11,15 @@ class HomeController < ApplicationController
end end
end end
def current_user ##already check current_user with helper_method
@current_user ||= Employee.find_by_token_session(session[:session_token]) if session[:session_token] # def current_user
end # @current_user ||= Employee.find_by_token_session(session[:session_token]) if session[:session_token]
# end
def index def index
# @employees = Employee.all_emp_except_waiter.order("name asc") # @employees = Employee.all_emp_except_waiter.order("name asc")
@employees = Employee.all.where("is_active = true").order("name asc") @employees = @shop.employees.all.where("is_active = true").order("name asc")
@roles = Employee.distinct.pluck(:role) @roles = @shop.employees.distinct.pluck(:role)
# byebug # byebug
# @roles = Lookup.collection_of("employee_roles") # @roles = Lookup.collection_of("employee_roles")
@login_form = LoginForm.new() @login_form = LoginForm.new()
@@ -35,7 +36,7 @@ class HomeController < ApplicationController
@login_form = LoginForm.new() @login_form = LoginForm.new()
@login_form.emp_id = params[:emp_id] @login_form.emp_id = params[:emp_id]
@login_form.password = params[:login_form][:password] @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 != nil
session[:session_token] = @employee.token_session session[:session_token] = @employee.token_session
@@ -50,7 +51,7 @@ class HomeController < ApplicationController
@login_form = LoginForm.new() @login_form = LoginForm.new()
@login_form.emp_id = params[:login_form][:emp_id] @login_form.emp_id = params[:login_form][:emp_id]
@login_form.password = params[:login_form][:password] @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 != nil
if @employee.is_active if @employee.is_active
@@ -90,31 +91,30 @@ class HomeController < ApplicationController
def dashboard def dashboard
@from, @to, @from_time, @to_time = get_date_range_from_params @from, @to, @from_time, @to_time = get_date_range_from_params
@shop = Shop.first
today = DateTime.now.strftime('%Y-%m-%d') today = DateTime.now.strftime('%Y-%m-%d')
if !@from.nil? && !@to.nil? if !@from.nil? && !@to.nil?
if !@from_time.nil? && @to_time.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 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 end
else 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 end
if !@from.nil? && !@to.nil? if !@from.nil? && !@to.nil?
if !@from_time.nil? && @to_time.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 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 end
else 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 end
@top_products = Sale.top_bottom_products(today,current_user,@from,@to,@from_time,@to_time,"top").sum('i.qty') @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").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).sum(:grand_total) @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 = [] @employee_sales = []
if !employee_sales.nil? if !employee_sales.nil?
employee_sales.each do |emp| employee_sales.each do |emp|
@@ -126,43 +126,43 @@ class HomeController < ApplicationController
end end
end 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_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) @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) @total_credit = Sale.credit_payment(today,current_user,@from,@to,@from_time,@to_time,@shop)
@sale_data = Array.new @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? if !@total_payment_methods.nil?
@total_payment_methods.each do |payment| @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" 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}) @sale_data.push({'card' => pay.payment_amount})
else 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}) @sale_data.push({payment.payment_method => pay.payment_amount})
end end
end end
end end
@summ_sale = Sale.summary_sale_receipt(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(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_order = Sale.total_order(@shop,today,current_user,@from,@to,@from_time,@to_time)
@total_accounts = Sale.total_account(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 @account_data = Array.new
if !@total_accounts.nil? if !@total_accounts.nil?
@total_accounts.each do |account| @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? if !acc.nil?
@account_data.push({account.title => acc.cnt_acc, account.title + '_amount' => acc.total_acc}) @account_data.push({account.title => acc.cnt_acc, account.title + '_amount' => acc.total_acc})
end end
end end
end end
@top_items = Sale.top_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(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 # get printer info
# @print_settings = get_precision_delimiter # @print_settings = get_precision_delimiter
@@ -170,7 +170,7 @@ class HomeController < ApplicationController
def destroy def destroy
# clear in employee session # clear in employee session
Employee.logout(session[:session_token]) Employee.logout(@shop,session[:session_token])
session[:session_token] = nil session[:session_token] = nil
# redirect_to root_path # redirect_to root_path
render :json => {:status=> "Success", :url => root_path }.to_json render :json => {:status=> "Success", :url => root_path }.to_json
@@ -195,7 +195,7 @@ class HomeController < ApplicationController
def route_by_role(employee) def route_by_role(employee)
if employee.role == "administrator" if employee.role == "administrator"
# redirect_to dashboard_path # redirect_to dashboard_path
shift = ShiftSale.current_open_shift(employee.id) shift = ShiftSale.current_open_shift(employee.id,@shop)
if !shift.nil? if !shift.nil?
redirect_to origami_root_path redirect_to origami_root_path
else else
@@ -203,7 +203,7 @@ class HomeController < ApplicationController
end end
elsif employee.role == "cashier" elsif employee.role == "cashier"
#check if cashier has existing open 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? if !shift.nil?
redirect_to origami_dashboard_path redirect_to origami_dashboard_path
# redirect_to origami_root_path # redirect_to origami_root_path

View File

@@ -21,7 +21,7 @@ class Inventory::InventoryController < BaseInventoryController
end end
end end
#Shop Name in Navbor #Shop Name in Navbor
helper_method :shop_detail helper_method :shop_detail

View File

@@ -11,7 +11,7 @@ class Origami::AlipayController < BaseOrigamiController
end end
total = 0 total = 0
@alipaycount = 0 @alipaycount = 0
@shop = Shop.first @shop = Shop.first
@rounding_adj = 0 @rounding_adj = 0
@can_alipay = 0 @can_alipay = 0
@member_discount = 0 @member_discount = 0
@@ -20,15 +20,15 @@ class Origami::AlipayController < BaseOrigamiController
@receipt_no = nil @receipt_no = nil
if !sale_data.nil? if !sale_data.nil?
total = sale_data.grand_total total = sale_data.grand_total
others = 0 others = 0
if @shop.is_rounding_adj if @shop.is_rounding_adj
new_total = Sale.get_rounding_adjustment(sale_data.grand_total) new_total = Sale.get_rounding_adjustment(sale_data.grand_total)
else else
new_total = sale_data.grand_total new_total = sale_data.grand_total
end end
@rounding_adj = new_total-sale_data.grand_total @rounding_adj = new_total-sale_data.grand_total
if path.include? ("credit_payment") if path.include? ("credit_payment")
sale_payment_data = SalePayment.get_sale_payment_for_credit(sale_data) sale_payment_data = SalePayment.get_sale_payment_for_credit(sale_data)
else else
@@ -65,12 +65,12 @@ class Origami::AlipayController < BaseOrigamiController
if(Sale.exists?(sale_id)) if(Sale.exists?(sale_id))
saleObj = Sale.find(sale_id) saleObj = Sale.find(sale_id)
shop_details = Shop.first shop_details = Shop.first
# rounding adjustment # rounding adjustment
# if shop_details.is_rounding_adj # if shop_details.is_rounding_adj
# new_total = Sale.get_rounding_adjustment(saleObj.grand_total) # new_total = Sale.get_rounding_adjustment(saleObj.grand_total)
# rounding_adj = new_total-saleObj.grand_total # rounding_adj = new_total-saleObj.grand_total
# saleObj.update_attributes(grand_total: new_total,old_grand_total: saleObj.grand_total,rounding_adjustment:rounding_adj) # saleObj.update_attributes(grand_total: new_total,old_grand_total: saleObj.grand_total,rounding_adjustment:rounding_adj)
# end # end
# saleObj = Sale.find(sale_id) # saleObj = Sale.find(sale_id)

View File

@@ -9,13 +9,13 @@ class Origami::CreditPaymentsController < BaseOrigamiController
@creditcount = 0 @creditcount = 0
others = 0 others = 0
@shop = Shop.first @shop = Shop.first
if @shop.is_rounding_adj if @shop.is_rounding_adj
new_total = Sale.get_rounding_adjustment(sale_data.grand_total) new_total = Sale.get_rounding_adjustment(sale_data.grand_total)
else else
new_total = sale_data.grand_total new_total = sale_data.grand_total
end end
@rounding_adj = new_total-sale_data.grand_total @rounding_adj = new_total-sale_data.grand_total
sale_data.sale_payments.each do |sale_payment| sale_data.sale_payments.each do |sale_payment|
if sale_payment.payment_method == "creditnote" if sale_payment.payment_method == "creditnote"
@@ -33,12 +33,12 @@ class Origami::CreditPaymentsController < BaseOrigamiController
if(Sale.exists?(sale_id)) if(Sale.exists?(sale_id))
saleObj = Sale.find(sale_id) saleObj = Sale.find(sale_id)
shop_details = Shop.first shop_details = Shop.first
# rounding adjustment # rounding adjustment
# if shop_details.is_rounding_adj # if shop_details.is_rounding_adj
# new_total = Sale.get_rounding_adjustment(saleObj.grand_total) # new_total = Sale.get_rounding_adjustment(saleObj.grand_total)
# rounding_adj = new_total-saleObj.grand_total # rounding_adj = new_total-saleObj.grand_total
# saleObj.update_attributes(grand_total: new_total,old_grand_total: saleObj.grand_total,rounding_adjustment:rounding_adj) # saleObj.update_attributes(grand_total: new_total,old_grand_total: saleObj.grand_total,rounding_adjustment:rounding_adj)
# end # end
saleObj = Sale.find(sale_id) saleObj = Sale.find(sale_id)
@@ -71,7 +71,7 @@ class Origami::CreditPaymentsController < BaseOrigamiController
else else
render :json => {status: false, message: 'No current shift open for this employee!'} render :json => {status: false, message: 'No current shift open for this employee!'}
end end
end end
end end

View File

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

View File

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

View File

@@ -30,13 +30,13 @@ class Menu < ApplicationRecord
cats = MenuCategory.where("menu_id=?",menu.id) cats = MenuCategory.where("menu_id=?",menu.id)
cats.each do |cat| cats.each do |cat|
abc = MenuCategory.destroyCategory(cat) abc = MenuCategory.destroyCategory(cat)
end end
menu.destroy menu.destroy
return false return false
end end
def self.to_csv def self.to_csv
m_attributes = %w{name is_active valid_days valid_time_from valid_time_to created_by created_at updated_at} m_attributes = %w{name is_active valid_days valid_time_from valid_time_to created_by created_at updated_at}
CSV.generate(headers: true, row_sep: "\r\n") do |csv| CSV.generate(headers: true, row_sep: "\r\n") do |csv|
csv << m_attributes csv << m_attributes
menu = Menu.all menu = Menu.all
@@ -46,8 +46,8 @@ class Menu < ApplicationRecord
end end
end end
def self.import(file, created_by) def self.import(file, created_by)
status = "" status = ""
spreadsheet = open_spreadsheet(file) spreadsheet = open_spreadsheet(file)
if spreadsheet.sheets.count > 1 if spreadsheet.sheets.count > 1
sheet_count = spreadsheet.sheets.count-1 sheet_count = spreadsheet.sheets.count-1
@@ -59,7 +59,7 @@ class Menu < ApplicationRecord
row = Hash[[header,spreadsheet.sheet(i).row(ii)].transpose] row = Hash[[header,spreadsheet.sheet(i).row(ii)].transpose]
if sheet_name == "Account" if sheet_name == "Account"
# Account.create(id:row["id"], title: row["title"],account_type: row["account_type"],discount: row["discount"],point: row["point"],bonus: row["bonus"],rebate: row["rebate"]) # Account.create(id:row["id"], title: row["title"],account_type: row["account_type"],discount: row["discount"],point: row["point"],bonus: row["bonus"],rebate: row["rebate"])
account = Account.find_by_id(row["id"]) account = Account.find_by_id(row["id"])
if account if account
Account.create(title: row["title"],account_type: row["account_type"],discount: row["discount"],point: row["point"],bonus: row["bonus"],rebate: row["rebate"]) Account.create(title: row["title"],account_type: row["account_type"],discount: row["discount"],point: row["point"],bonus: row["bonus"],rebate: row["rebate"])
else else
@@ -67,7 +67,7 @@ class Menu < ApplicationRecord
end end
elsif sheet_name == "Item Set" elsif sheet_name == "Item Set"
# ItemSet.create(id:row["id"], name: row[name], alt_name: row[alt_name], min_selectable_qty: row[min_selectable_qty], max_selectable_qty: row[max_selectable_qty]) # ItemSet.create(id:row["id"], name: row[name], alt_name: row[alt_name], min_selectable_qty: row[min_selectable_qty], max_selectable_qty: row[max_selectable_qty])
item_set = ItemSet.find_by_id(row["id"]) item_set = ItemSet.find_by_id(row["id"])
if item_set if item_set
ItemSet.create( name: row["name"], alt_name: row["alt_name"], min_selectable_qty: row["min_selectable_qty"], max_selectable_qty: row["max_selectable_qty"]) ItemSet.create( name: row["name"], alt_name: row["alt_name"], min_selectable_qty: row["min_selectable_qty"], max_selectable_qty: row["max_selectable_qty"])
else else
@@ -128,7 +128,7 @@ class Menu < ApplicationRecord
else else
MenuInstanceItemSet.create(id:row["id"], item_set_id: row["item_set_id"], menu_item_instance_id: row["menu_item_instance_id"]) MenuInstanceItemSet.create(id:row["id"], item_set_id: row["item_set_id"], menu_item_instance_id: row["menu_item_instance_id"])
end end
elsif sheet_name == "Menu Item Set" elsif sheet_name == "Menu Item Set"
# MenuItemSet.create(id:row["id"], item_set_id: row["item_set_id"], menu_item_id: row["menu_item_id"]) # MenuItemSet.create(id:row["id"], item_set_id: row["item_set_id"], menu_item_id: row["menu_item_id"])
menu_item_set = MenuItemSet.find_by_id(row["id"]) menu_item_set = MenuItemSet.find_by_id(row["id"])
if menu_item_set if menu_item_set
@@ -149,24 +149,24 @@ class Menu < ApplicationRecord
end end
end end
sheet = spreadsheet.sheet(0) sheet = spreadsheet.sheet(0)
menu = sheet.row(1)[1] menu = sheet.row(1)[1]
is_ordering = sheet.row(1)[3]?sheet.row(1)[3]:0 is_ordering = sheet.row(1)[3]?sheet.row(1)[3]:0
imported_menu = Menu.create({name: menu, is_active: true, is_ordering: is_ordering, valid_days: "1,2,3,4,5,6,7",valid_time_from: "00:00:00", valid_time_to: "23:59:59", created_by: created_by}) imported_menu = Menu.create({name: menu, is_active: true, is_ordering: is_ordering, valid_days: "1,2,3,4,5,6,7",valid_time_from: "00:00:00", valid_time_to: "23:59:59", created_by: created_by})
(4..sheet.last_row).each do |ii| (4..sheet.last_row).each do |ii|
row = Hash[[sheet.row(3),sheet.row(ii)].transpose] row = Hash[[sheet.row(3),sheet.row(ii)].transpose]
menu_cat = MenuCategory.find_by_code(row["Category Code"]) menu_cat = MenuCategory.find_by_code(row["Category Code"])
if !menu_cat if !menu_cat
menu_cat = MenuCategory.create({menu_id: imported_menu.id, code: row["Category Code"], name: row["Category Name"], alt_name: '', order_by: (ii - 3), created_by: created_by, menu_category_id: nil, is_available: 1}) menu_cat = MenuCategory.create({menu_id: imported_menu.id, code: row["Category Code"], name: row["Category Name"], alt_name: '', order_by: (ii - 3), created_by: created_by, menu_category_id: nil, is_available: 1})
end end
# Menu Item Attributes # Menu Item Attributes
item_attrs = [] item_attrs = []
if !row["Attributes"].nil? if !row["Attributes"].nil?
attributes = row["Attributes"].split(',') attributes = row["Attributes"].split(',')
attributes.each do |attr| attributes.each do |attr|
attribute = MenuItemAttribute.find_by_name(attr) attribute = MenuItemAttribute.find_by_name(attr)
if attribute.nil? if attribute.nil?
attribute = MenuItemAttribute.create({ attribute_type:"any", name: attr.capitalize, value: attr.downcase }) attribute = MenuItemAttribute.create({ attribute_type:"any", name: attr.capitalize, value: attr.downcase })
@@ -188,7 +188,7 @@ class Menu < ApplicationRecord
end end
end end
menu_itm = MenuItem.find_by_item_code(row["Item Code"]) menu_itm = MenuItem.find_by_item_code(row["Item Code"])
if !menu_itm if !menu_itm
account = Account.find_by_title(row["Account"]) account = Account.find_by_title(row["Account"])
if account.nil? if account.nil?
@@ -205,22 +205,22 @@ class Menu < ApplicationRecord
end end
instance_attr = [] instance_attr = []
menu_inst = MenuItemInstance.find_by_item_instance_code(row["Instance Code"]) menu_inst = MenuItemInstance.find_by_item_instance_code(row["Instance Code"])
if !menu_inst if !menu_inst
instance_name = '' instance_name = ''
if !row["Instance Name"].nil? if !row["Instance Name"].nil?
instance_name = row["Instance Name"] instance_name = row["Instance Name"]
end end
if !row["Attributes"].nil? if !row["Attributes"].nil?
if !row["Instance Attribute"].nil? if !row["Instance Attribute"].nil?
attributes = row["Attributes"].split(',') attributes = row["Attributes"].split(',')
instance_attributes = row["Instance Attribute"].split(',') instance_attributes = row["Instance Attribute"].split(',')
attributes.each do |attr| attributes.each do |attr|
if attr == instance_attributes[0] if attr == instance_attributes[0]
ins_attr = MenuItemAttribute.find_by_name(attr) ins_attr = MenuItemAttribute.find_by_name(attr)
instance_attr.push(ins_attr.id) instance_attr.push(ins_attr.id)
end end
end end
end end
end end
@@ -231,7 +231,7 @@ class Menu < ApplicationRecord
end end
imported_instance = MenuItemInstance.create(menu_item_id: menu_itm.id, item_instance_code: row["Instance Code"], item_instance_name: instance_name, item_attributes: instance_attr, price: row["Price"], is_on_promotion: false, promotion_price: 0, is_available: true, is_default: is_default) imported_instance = MenuItemInstance.create(menu_item_id: menu_itm.id, item_instance_code: row["Instance Code"], item_instance_name: instance_name, item_attributes: instance_attr, price: row["Price"], is_on_promotion: false, promotion_price: 0, is_available: true, is_default: is_default)
end end
end end
end end
# if status == '' # if status == ''
status="Menu Imported!" status="Menu Imported!"
@@ -249,4 +249,4 @@ class Menu < ApplicationRecord
end end
end end
end end

View File

@@ -5,7 +5,7 @@
class OrderQueueStation < ApplicationRecord class OrderQueueStation < ApplicationRecord
has_many :assigned_order_items has_many :assigned_order_items
has_many :order_items has_many :order_items
has_many :order_queue_process_by_zones has_many :order_queue_process_by_zones
has_many :zones, through: :order_queue_process_by_zones has_many :zones, through: :order_queue_process_by_zones
belongs_to :employee belongs_to :employee
@@ -17,21 +17,21 @@ class OrderQueueStation < ApplicationRecord
def process_order (order, table_id, order_source = nil, pdf_status = nil ,change_to=nil,current_user=nil) def process_order (order, table_id, order_source = nil, pdf_status = nil ,change_to=nil,current_user=nil)
oqs_stations = OrderQueueStation.active oqs_stations = OrderQueueStation.active
order_items = order.order_items order_items = order.order_items
if table_id.to_i > 0 if table_id.to_i > 0
# get dining # get dining
dining = DiningFacility.find(table_id) dining = DiningFacility.find(table_id)
oqs_by_zones = OrderQueueProcessByZone.where("zone_id=#{dining.zone_id}") oqs_by_zones = OrderQueueProcessByZone.where("zone_id=#{dining.zone_id}")
booking = Booking.find_by_dining_facility_id(dining.id) booking = Booking.find_by_dining_facility_id(dining.id)
# ToDo per item per printer # ToDo per item per printer
oqs_by_zones.each do |oqpbz| oqs_by_zones.each do |oqpbz|
oqs = OrderQueueStation.find(oqpbz.order_queue_station_id) oqs = OrderQueueStation.find(oqpbz.order_queue_station_id)
is_auto_printed = false is_auto_printed = false
oqs_order_items = [] oqs_order_items = []
if oqs.is_active if oqs.is_active
@@ -44,22 +44,22 @@ class OrderQueueStation < ApplicationRecord
if (pq_item == order_item.item_code) if (pq_item == order_item.item_code)
# if oqs.id == oqpbz.order_queue_station_id # if oqs.id == oqpbz.order_queue_station_id
# #Same Order_items can appear in two location. # #Same Order_items can appear in two location.
# AssignedOrderItem.assigned_order_item(order, order_item.item_code, oqs) # AssignedOrderItem.assigned_order_item(order, order_item.item_code, oqs)
# else # else
if (order_item.qty > 0) if (order_item.qty > 0)
if pdf_status.nil? if pdf_status.nil?
AssignedOrderItem.assigned_order_item(order, order_item.item_code, order_item.item_instance_code, oqs) AssignedOrderItem.assigned_order_item(order, order_item.item_code, order_item.item_instance_code, oqs)
end end
oqs_order_items.push(order_item) oqs_order_items.push(order_item)
end end
# end # end
end end
end end
end end
if oqs.auto_print && order_source != "quick_service" if oqs.auto_print && order_source != "quick_service"
if oqs_order_items.length > 0 if oqs_order_items.length > 0
if oqs.cut_per_item if oqs.cut_per_item
print_slip_item(oqs, order, oqs_order_items,pdf_status,change_to,current_user,table_id) print_slip_item(oqs, order, oqs_order_items,pdf_status,change_to,current_user,table_id)
else else
@@ -67,12 +67,12 @@ class OrderQueueStation < ApplicationRecord
end end
is_auto_printed = true is_auto_printed = true
end end
end end
end end
end end
else else
oqs_stations.each do |oqs| oqs_stations.each do |oqs|
is_auto_printed = false is_auto_printed = false
oqs_order_items = [] oqs_order_items = []
if oqs.is_active if oqs.is_active
@@ -85,39 +85,39 @@ class OrderQueueStation < ApplicationRecord
if (pq_item == order_item.item_code) if (pq_item == order_item.item_code)
# if oqs.id == oqpbz.order_queue_station_id # if oqs.id == oqpbz.order_queue_station_id
# #Same Order_items can appear in two location. # #Same Order_items can appear in two location.
# AssignedOrderItem.assigned_order_item(order, order_item.item_code, oqs) # AssignedOrderItem.assigned_order_item(order, order_item.item_code, oqs)
# else # else
if (order_item.qty > 0) if (order_item.qty > 0)
if pdf_status.nil? if pdf_status.nil?
AssignedOrderItem.assigned_order_item(order, order_item.item_code, order_item.item_instance_code, oqs) AssignedOrderItem.assigned_order_item(order, order_item.item_code, order_item.item_instance_code, oqs)
end end
oqs_order_items.push(order_item) oqs_order_items.push(order_item)
end end
# end # end
end end
end end
end end
if oqs.auto_print && order_source != "quick_service" if oqs.auto_print && order_source != "quick_service"
if oqs_order_items.length > 0 if oqs_order_items.length > 0
if oqs.cut_per_item if oqs.cut_per_item
print_slip_item(oqs, order, oqs_order_items,pdf_status,change_to,current_user,table_id) print_slip_item(oqs, order, oqs_order_items,pdf_status,change_to,current_user,table_id)
else else
print_slip(oqs, order, oqs_order_items,pdf_status,change_to,current_user,table_id) print_slip(oqs, order, oqs_order_items,pdf_status,change_to,current_user,table_id)
end end
is_auto_printed = true is_auto_printed = true
end end
end end
end end
end end
end #end else end #end else
end end
def pay_process_order_queue (order_id, table_id) def pay_process_order_queue (order_id, table_id)
oqs_stations = OrderQueueStation.active oqs_stations = OrderQueueStation.active
order = Order.find(order_id) order = Order.find(order_id)
order_items = order.order_items order_items = order.order_items
@@ -129,22 +129,22 @@ class OrderQueueStation < ApplicationRecord
# assign_order = AssignedOrderItem.assigned_order_item_by_job(order_id) # assign_order = AssignedOrderItem.assigned_order_item_by_job(order_id)
# if ENV["SERVER_MODE"] == 'cloud' # if ENV["SERVER_MODE"] == 'cloud'
# from = request.subdomain + "." + request.domain # from = request.subdomain + "." + request.domain
# else # else
# from = "" # from = ""
# end # end
# ActionCable.server.broadcast "order_queue_station_channel",order: assign_order,from:from # ActionCable.server.broadcast "order_queue_station_channel",order: assign_order,from:from
if table_id.to_i > 0 if table_id.to_i > 0
# get dining # get dining
dining = DiningFacility.find(table_id) dining = DiningFacility.find(table_id)
oqs_by_zones = OrderQueueProcessByZone.where("zone_id=#{dining.zone_id}") oqs_by_zones = OrderQueueProcessByZone.where("zone_id=#{dining.zone_id}")
booking = Booking.find_by_dining_facility_id(dining.id) booking = Booking.find_by_dining_facility_id(dining.id)
# ToDo per item per printer # ToDo per item per printer
oqs_by_zones.each do |oqpbz| oqs_by_zones.each do |oqpbz|
oqs = OrderQueueStation.find(oqpbz.order_queue_station_id) oqs = OrderQueueStation.find(oqpbz.order_queue_station_id)
is_auto_printed = false is_auto_printed = false
oqs_order_items = [] oqs_order_items = []
if oqs.is_active if oqs.is_active
@@ -157,26 +157,26 @@ class OrderQueueStation < ApplicationRecord
if (pq_item == order_item.item_code) if (pq_item == order_item.item_code)
if (order_item.qty > 0) if (order_item.qty > 0)
oqs_order_items.push(order_item) oqs_order_items.push(order_item)
end end
end end
end end
end end
if oqs.auto_print if oqs.auto_print
if oqs_order_items.length > 0 if oqs_order_items.length > 0
if oqs.cut_per_item if oqs.cut_per_item
print_slip_item(oqs, order, oqs_order_items) print_slip_item(oqs, order, oqs_order_items)
else else
print_slip(oqs, order, oqs_order_items) print_slip(oqs, order, oqs_order_items)
end end
is_auto_printed = true is_auto_printed = true
end end
end end
end end
end end
else else
oqs_stations.each do |oqs| oqs_stations.each do |oqs|
is_auto_printed = false is_auto_printed = false
oqs_order_items = [] oqs_order_items = []
if oqs.is_active if oqs.is_active
@@ -186,27 +186,27 @@ class OrderQueueStation < ApplicationRecord
pq_items.each do |pq_item| pq_items.each do |pq_item|
#Processing through the looping items #Processing through the looping items
order_items.each do |order_item| order_items.each do |order_item|
if (pq_item == order_item.item_code) if (pq_item == order_item.item_code)
if (order_item.qty > 0) if (order_item.qty > 0)
oqs_order_items.push(order_item) oqs_order_items.push(order_item)
end end
end end
end end
end end
if oqs.auto_print if oqs.auto_print
if oqs_order_items.length > 0 if oqs_order_items.length > 0
if oqs.cut_per_item if oqs.cut_per_item
print_slip_item(oqs, order, oqs_order_items) print_slip_item(oqs, order, oqs_order_items)
else else
print_slip(oqs, order, oqs_order_items) print_slip(oqs, order, oqs_order_items)
end end
is_auto_printed = true is_auto_printed = true
end end
end end
end end
end end
end #end else end #end else
end end
private private
@@ -217,8 +217,8 @@ class OrderQueueStation < ApplicationRecord
unique_code="OrderSummaryPdf" unique_code="OrderSummaryPdf"
if !printer.empty? if !printer.empty?
printer.each do |printer_setting| printer.each do |printer_setting|
if printer_setting.unique_code == 'OrderSummaryPdf' if printer_setting.unique_code == 'OrderSummaryPdf'
unique_code="OrderSummaryPdf" unique_code="OrderSummaryPdf"
elsif printer_setting.unique_code == 'OrderSummarySlimPdf' elsif printer_setting.unique_code == 'OrderSummarySlimPdf'
unique_code="OrderSummarySlimPdf" unique_code="OrderSummarySlimPdf"
@@ -227,12 +227,12 @@ class OrderQueueStation < ApplicationRecord
elsif printer_setting.unique_code == 'OrderSummaryCustomisePdf' elsif printer_setting.unique_code == 'OrderSummaryCustomisePdf'
unique_code="OrderSummaryCustomisePdf" unique_code="OrderSummaryCustomisePdf"
elsif printer_setting.unique_code == 'OrderSummarySetCustomisePdf' elsif printer_setting.unique_code == 'OrderSummarySetCustomisePdf'
unique_code="OrderSummarySetCustomisePdf" unique_code="OrderSummarySetCustomisePdf"
elsif printer_setting.unique_code == 'OrderSummarySlimCustomisePdf' elsif printer_setting.unique_code == 'OrderSummarySlimCustomisePdf'
unique_code="OrderSummarySlimCustomisePdf" unique_code="OrderSummarySlimCustomisePdf"
end end
end end
end end
print_settings=PrintSetting.find_by_unique_code(unique_code) print_settings=PrintSetting.find_by_unique_code(unique_code)
order_queue_printer= Printer::OrderQueuePrinter.new(print_settings) order_queue_printer= Printer::OrderQueuePrinter.new(print_settings)
@@ -240,21 +240,21 @@ class OrderQueueStation < ApplicationRecord
else else
move_print_pdf(change_to,current_user,table_id,order_items,oqs) move_print_pdf(change_to,current_user,table_id,order_items,oqs)
end end
assigned =AssignedOrderItem.where("order_id = '#{ order.order_id }'").pluck(:assigned_order_item_id) assigned =AssignedOrderItem.where("order_id = '#{ order.order_id }'").pluck(:assigned_order_item_id)
AssignedOrderItem.where('assigned_order_item_id IN (?)', assigned).update_all(print_status: true) AssignedOrderItem.where('assigned_order_item_id IN (?)', assigned).update_all(print_status: true)
end end
#Print order_item in 1 slip per item #Print order_item in 1 slip per item
def print_slip_item(oqs, order, assigned_items,pdf_status=nil,change_to=nil,current_user=nil,table_id=nil) def print_slip_item(oqs, order, assigned_items,pdf_status=nil,change_to=nil,current_user=nil,table_id=nil)
if pdf_status.nil? if pdf_status.nil?
printer = PrintSetting.all.order("id ASC") printer = PrintSetting.all.order("id ASC")
unique_code="OrderItemPdf" unique_code="OrderItemPdf"
if !printer.empty? if !printer.empty?
printer.each do |printer_setting| printer.each do |printer_setting|
if printer_setting.unique_code == 'OrderItemPdf' if printer_setting.unique_code == 'OrderItemPdf'
unique_code="OrderItemPdf" unique_code="OrderItemPdf"
elsif printer_setting.unique_code == 'OrderItemStarPdf' elsif printer_setting.unique_code == 'OrderItemStarPdf'
unique_code="OrderItemStarPdf" unique_code="OrderItemStarPdf"
@@ -265,15 +265,15 @@ class OrderQueueStation < ApplicationRecord
elsif printer_setting.unique_code == 'OrderItemCustomisePdf' elsif printer_setting.unique_code == 'OrderItemCustomisePdf'
unique_code="OrderItemCustomisePdf" unique_code="OrderItemCustomisePdf"
elsif printer_setting.unique_code == 'OrderSetItemCustomisePdf' elsif printer_setting.unique_code == 'OrderSetItemCustomisePdf'
unique_code="OrderSetItemCustomisePdf" unique_code="OrderSetItemCustomisePdf"
elsif printer_setting.unique_code == 'OrderItemSlimCustomisePdf' elsif printer_setting.unique_code == 'OrderItemSlimCustomisePdf'
unique_code="OrderItemSlimCustomisePdf" unique_code="OrderItemSlimCustomisePdf"
end end
end end
end end
# order_item = OrderItem.where("order_id='#{assigned_item.order_id}' AND item_instance_code='#{assigned_item.instance_code}'").first() # order_item = OrderItem.where("order_id='#{assigned_item.order_id}' AND item_instance_code='#{assigned_item.instance_code}'").first()
# print when complete click # print when complete click
print_settings=PrintSetting.find_by_unique_code(unique_code) print_settings=PrintSetting.find_by_unique_code(unique_code)
order_queue_printer= Printer::OrderQueuePrinter.new(print_settings) order_queue_printer= Printer::OrderQueuePrinter.new(print_settings)
if !assigned_items.nil? if !assigned_items.nil?
@@ -284,7 +284,7 @@ class OrderQueueStation < ApplicationRecord
else else
move_print_pdf(change_to,current_user,table_id,assigned_items,oqs) move_print_pdf(change_to,current_user,table_id,assigned_items,oqs)
end end
assigned =AssignedOrderItem.where("order_id = '#{ order.order_id }'").pluck(:assigned_order_item_id) assigned =AssignedOrderItem.where("order_id = '#{ order.order_id }'").pluck(:assigned_order_item_id)
AssignedOrderItem.where('assigned_order_item_id IN (?)', assigned).update_all(print_status: true) AssignedOrderItem.where('assigned_order_item_id IN (?)', assigned).update_all(print_status: true)
end end
@@ -298,7 +298,7 @@ class OrderQueueStation < ApplicationRecord
@moved_by = current_user @moved_by = current_user
@date = DateTime.now @date = DateTime.now
@shop = Shop.first @shop = Shop.first
unique_code = "MoveTablePdf" unique_code = "MoveTablePdf"
# pdf_no = PrintSetting.where(:unique_code => unique_code).count # pdf_no = PrintSetting.where(:unique_code => unique_code).count
print_settings = PrintSetting.find_by_unique_code(unique_code) print_settings = PrintSetting.find_by_unique_code(unique_code)
# printer_array = [] # printer_array = []
@@ -307,10 +307,10 @@ class OrderQueueStation < ApplicationRecord
# for i in 0..pdf_no # for i in 0..pdf_no
# if i != pdf_no # if i != pdf_no
# print_settings = printer_array[i] # print_settings = printer_array[i]
printer = Printer::ReceiptPrinter.new(print_settings) printer = Printer::ReceiptPrinter.new(print_settings)
printer.print_move_table(print_settings,@to,@from ,@shop,@date,@type,@moved_by,order_items,oqs) printer.print_move_table(print_settings,@to,@from ,@shop,@date,@type,@moved_by,order_items,oqs)
# end # end
# end # end
end end
end end
end end

View File

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

View File

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

View File

@@ -3,8 +3,36 @@ class Shop < ApplicationRecord
# Shop Image Uploader # Shop Image Uploader
mount_uploader :logo, ShopImageUploader 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 :display_images
has_many :shift_sales
accepts_nested_attributes_for :display_images accepts_nested_attributes_for :display_images
def file_data=(input_data) def file_data=(input_data)

View File

@@ -1,5 +1,5 @@
class StockJournal < ApplicationRecord class StockJournal < ApplicationRecord
belongs_to :shop
SALES_TRANS = "sale" SALES_TRANS = "sale"
ORDER_TRANS = "order" ORDER_TRANS = "order"
STOCK_CHECK_TRANS = "stock_check" STOCK_CHECK_TRANS = "stock_check"
@@ -52,9 +52,9 @@ class StockJournal < ApplicationRecord
journal.save journal.save
end 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? 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") .joins("join menu_item_instances mii on mii.item_instance_code=stock_journals.item_code")
if !from_time.nil? && !to_time.nil? 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}'") 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") query = query.group("mii.item_instance_name")
.order("mii.item_instance_name ASC") .order("mii.item_instance_name ASC")
else 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") .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}'") .where("DATE_FORMAT(stock_journals.created_at,'%Y-%m-%d') = '#{today}'")
.group("mii.item_instance_name") .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