diff --git a/Gemfile.lock b/Gemfile.lock index 073bf04c..3a70c5bd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -375,7 +375,5 @@ DEPENDENCIES web-console (>= 3.3.0) whenever -RUBY VERSION - ruby 2.6.3p62 BUNDLED WITH 2.0.2 diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index f1d5f79a..fddcb2f7 100755 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,10 +1,10 @@ class ApplicationController < ActionController::Base include LoginVerification - + #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 - - - diff --git a/app/controllers/concerns/login_verification.rb b/app/controllers/concerns/login_verification.rb index 8eb719cb..85b23e6a 100755 --- a/app/controllers/concerns/login_verification.rb +++ b/app/controllers/concerns/login_verification.rb @@ -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| @@ -78,17 +80,17 @@ module LoginVerification protected # Authenticate the user with token based authentication - def authenticate - authenticate_session_token || render_unauthorized + def authenticate + authenticate_session_token || render_unauthorized end - def authenticate_session_token + def authenticate_session_token token = session[:session_token] if (token) #@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? diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index eaab0511..c16afd83 100755 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -3,7 +3,7 @@ class HomeController < ApplicationController # skip_before_action :authenticate, only: [:index, :show, :create, :update, :destroy] include PrecisionHelper before_action :check_user, only: :dashboard - + # Special check for only dashboard def check_user if current_user.nil? @@ -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 diff --git a/app/controllers/inventory/inventory_controller.rb b/app/controllers/inventory/inventory_controller.rb index a0324b15..4fb2747a 100755 --- a/app/controllers/inventory/inventory_controller.rb +++ b/app/controllers/inventory/inventory_controller.rb @@ -21,7 +21,7 @@ class Inventory::InventoryController < BaseInventoryController end end - + #Shop Name in Navbor helper_method :shop_detail diff --git a/app/controllers/origami/alipay_controller.rb b/app/controllers/origami/alipay_controller.rb index 8c5f4884..768e3e97 100644 --- a/app/controllers/origami/alipay_controller.rb +++ b/app/controllers/origami/alipay_controller.rb @@ -11,7 +11,7 @@ class Origami::AlipayController < BaseOrigamiController end total = 0 @alipaycount = 0 - @shop = Shop.first + @shop = Shop.first @rounding_adj = 0 @can_alipay = 0 @member_discount = 0 @@ -20,15 +20,15 @@ class Origami::AlipayController < BaseOrigamiController @receipt_no = nil if !sale_data.nil? total = sale_data.grand_total - + others = 0 - + if @shop.is_rounding_adj new_total = Sale.get_rounding_adjustment(sale_data.grand_total) else new_total = sale_data.grand_total end - @rounding_adj = new_total-sale_data.grand_total + @rounding_adj = new_total-sale_data.grand_total if path.include? ("credit_payment") sale_payment_data = SalePayment.get_sale_payment_for_credit(sale_data) else @@ -65,12 +65,12 @@ class Origami::AlipayController < BaseOrigamiController if(Sale.exists?(sale_id)) saleObj = Sale.find(sale_id) shop_details = Shop.first - + # rounding adjustment # if shop_details.is_rounding_adj # new_total = Sale.get_rounding_adjustment(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 # saleObj = Sale.find(sale_id) diff --git a/app/controllers/origami/credit_payments_controller.rb b/app/controllers/origami/credit_payments_controller.rb index 05faf4f7..80ed3c28 100755 --- a/app/controllers/origami/credit_payments_controller.rb +++ b/app/controllers/origami/credit_payments_controller.rb @@ -9,13 +9,13 @@ class Origami::CreditPaymentsController < BaseOrigamiController @creditcount = 0 others = 0 - @shop = Shop.first + @shop = Shop.first if @shop.is_rounding_adj new_total = Sale.get_rounding_adjustment(sale_data.grand_total) else new_total = sale_data.grand_total 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| if sale_payment.payment_method == "creditnote" @@ -33,12 +33,12 @@ class Origami::CreditPaymentsController < BaseOrigamiController if(Sale.exists?(sale_id)) saleObj = Sale.find(sale_id) shop_details = Shop.first - + # rounding adjustment # if shop_details.is_rounding_adj # new_total = Sale.get_rounding_adjustment(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 saleObj = Sale.find(sale_id) @@ -71,7 +71,7 @@ class Origami::CreditPaymentsController < BaseOrigamiController else render :json => {status: false, message: 'No current shift open for this employee!'} end - + end end diff --git a/app/models/employee.rb b/app/models/employee.rb index f27adf29..5ff24057 100755 --- a/app/models/employee.rb +++ b/app/models/employee.rb @@ -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" diff --git a/app/models/lookup.rb b/app/models/lookup.rb index b446d4e8..ec7e653e 100755 --- a/app/models/lookup.rb +++ b/app/models/lookup.rb @@ -1,6 +1,7 @@ class Lookup < ApplicationRecord has_many :accounts + belongs_to :shop def available_types {'Employee Roles' => 'employee_roles', diff --git a/app/models/menu.rb b/app/models/menu.rb index a825cc7b..5c7382e8 100755 --- a/app/models/menu.rb +++ b/app/models/menu.rb @@ -30,13 +30,13 @@ class Menu < ApplicationRecord cats = MenuCategory.where("menu_id=?",menu.id) cats.each do |cat| abc = MenuCategory.destroyCategory(cat) - end + end menu.destroy return false end 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 << m_attributes menu = Menu.all @@ -46,8 +46,8 @@ class Menu < ApplicationRecord end end - def self.import(file, created_by) - status = "" + def self.import(file, created_by) + status = "" spreadsheet = open_spreadsheet(file) if 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] 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 = Account.find_by_id(row["id"]) + account = Account.find_by_id(row["id"]) if account Account.create(title: row["title"],account_type: row["account_type"],discount: row["discount"],point: row["point"],bonus: row["bonus"],rebate: row["rebate"]) else @@ -67,7 +67,7 @@ class Menu < ApplicationRecord end 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]) - item_set = ItemSet.find_by_id(row["id"]) + item_set = ItemSet.find_by_id(row["id"]) 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"]) else @@ -128,7 +128,7 @@ class Menu < ApplicationRecord else MenuInstanceItemSet.create(id:row["id"], item_set_id: row["item_set_id"], menu_item_instance_id: row["menu_item_instance_id"]) 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"]) menu_item_set = MenuItemSet.find_by_id(row["id"]) if menu_item_set @@ -149,24 +149,24 @@ class Menu < ApplicationRecord end end - sheet = spreadsheet.sheet(0) + sheet = spreadsheet.sheet(0) menu = sheet.row(1)[1] 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| - row = Hash[[sheet.row(3),sheet.row(ii)].transpose] - menu_cat = MenuCategory.find_by_code(row["Category Code"]) + (4..sheet.last_row).each do |ii| + row = Hash[[sheet.row(3),sheet.row(ii)].transpose] + menu_cat = MenuCategory.find_by_code(row["Category Code"]) 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 # Menu Item Attributes item_attrs = [] if !row["Attributes"].nil? attributes = row["Attributes"].split(',') - attributes.each do |attr| + attributes.each do |attr| attribute = MenuItemAttribute.find_by_name(attr) if attribute.nil? attribute = MenuItemAttribute.create({ attribute_type:"any", name: attr.capitalize, value: attr.downcase }) @@ -188,7 +188,7 @@ class Menu < ApplicationRecord 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 account = Account.find_by_title(row["Account"]) if account.nil? @@ -205,22 +205,22 @@ class Menu < ApplicationRecord end 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 instance_name = '' - if !row["Instance Name"].nil? + if !row["Instance Name"].nil? instance_name = row["Instance Name"] end if !row["Attributes"].nil? - if !row["Instance Attribute"].nil? + if !row["Instance Attribute"].nil? attributes = row["Attributes"].split(',') instance_attributes = row["Instance Attribute"].split(',') - attributes.each do |attr| + attributes.each do |attr| if attr == instance_attributes[0] ins_attr = MenuItemAttribute.find_by_name(attr) - instance_attr.push(ins_attr.id) - end + instance_attr.push(ins_attr.id) + end end end end @@ -231,7 +231,7 @@ class Menu < ApplicationRecord 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) end - end + end end # if status == '' status="Menu Imported!" @@ -249,4 +249,4 @@ class Menu < ApplicationRecord end end -end \ No newline at end of file +end diff --git a/app/models/order_queue_station.rb b/app/models/order_queue_station.rb index 147ebdc4..00eda121 100755 --- a/app/models/order_queue_station.rb +++ b/app/models/order_queue_station.rb @@ -5,7 +5,7 @@ class OrderQueueStation < ApplicationRecord has_many :assigned_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 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) oqs_stations = OrderQueueStation.active - + order_items = order.order_items if table_id.to_i > 0 - # get dining - dining = DiningFacility.find(table_id) + # get dining + dining = DiningFacility.find(table_id) oqs_by_zones = OrderQueueProcessByZone.where("zone_id=#{dining.zone_id}") booking = Booking.find_by_dining_facility_id(dining.id) - + # ToDo per item per printer oqs_by_zones.each do |oqpbz| oqs = OrderQueueStation.find(oqpbz.order_queue_station_id) - is_auto_printed = false + is_auto_printed = false oqs_order_items = [] if oqs.is_active @@ -44,22 +44,22 @@ class OrderQueueStation < ApplicationRecord if (pq_item == order_item.item_code) # if oqs.id == oqpbz.order_queue_station_id # #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 - + if (order_item.qty > 0) 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 oqs_order_items.push(order_item) end - # end + # end end end end - + 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 print_slip_item(oqs, order, oqs_order_items,pdf_status,change_to,current_user,table_id) else @@ -67,12 +67,12 @@ class OrderQueueStation < ApplicationRecord end is_auto_printed = true end - end + end end end else oqs_stations.each do |oqs| - is_auto_printed = false + is_auto_printed = false oqs_order_items = [] if oqs.is_active @@ -85,39 +85,39 @@ class OrderQueueStation < ApplicationRecord if (pq_item == order_item.item_code) # if oqs.id == oqpbz.order_queue_station_id # #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 - + if (order_item.qty > 0) 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 oqs_order_items.push(order_item) end - # end + # end end end end - + 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 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) end is_auto_printed = true end - end + end end end - end #end else + end #end else end - def pay_process_order_queue (order_id, table_id) + def pay_process_order_queue (order_id, table_id) oqs_stations = OrderQueueStation.active - + order = Order.find(order_id) order_items = order.order_items @@ -129,22 +129,22 @@ class OrderQueueStation < ApplicationRecord # assign_order = AssignedOrderItem.assigned_order_item_by_job(order_id) # if ENV["SERVER_MODE"] == 'cloud' # from = request.subdomain + "." + request.domain - # else + # else # from = "" # end # ActionCable.server.broadcast "order_queue_station_channel",order: assign_order,from:from if table_id.to_i > 0 - # get dining - dining = DiningFacility.find(table_id) + # get dining + dining = DiningFacility.find(table_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 oqs_by_zones.each do |oqpbz| oqs = OrderQueueStation.find(oqpbz.order_queue_station_id) - is_auto_printed = false + is_auto_printed = false oqs_order_items = [] if oqs.is_active @@ -157,26 +157,26 @@ class OrderQueueStation < ApplicationRecord if (pq_item == order_item.item_code) if (order_item.qty > 0) oqs_order_items.push(order_item) - end + end end end end - + if oqs.auto_print - if oqs_order_items.length > 0 + if oqs_order_items.length > 0 if oqs.cut_per_item print_slip_item(oqs, order, oqs_order_items) - else + else print_slip(oqs, order, oqs_order_items) end is_auto_printed = true end - end + end end end else oqs_stations.each do |oqs| - is_auto_printed = false + is_auto_printed = false oqs_order_items = [] if oqs.is_active @@ -186,27 +186,27 @@ class OrderQueueStation < ApplicationRecord pq_items.each do |pq_item| #Processing through the looping items 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) oqs_order_items.push(order_item) - end + end end end end - + if oqs.auto_print - if oqs_order_items.length > 0 + if oqs_order_items.length > 0 if oqs.cut_per_item print_slip_item(oqs, order, oqs_order_items) - else + else print_slip(oqs, order, oqs_order_items) end is_auto_printed = true end - end + end end end - end #end else + end #end else end private @@ -217,8 +217,8 @@ class OrderQueueStation < ApplicationRecord unique_code="OrderSummaryPdf" if !printer.empty? - printer.each do |printer_setting| - if printer_setting.unique_code == 'OrderSummaryPdf' + printer.each do |printer_setting| + if printer_setting.unique_code == 'OrderSummaryPdf' unique_code="OrderSummaryPdf" elsif printer_setting.unique_code == 'OrderSummarySlimPdf' unique_code="OrderSummarySlimPdf" @@ -227,12 +227,12 @@ class OrderQueueStation < ApplicationRecord elsif printer_setting.unique_code == 'OrderSummaryCustomisePdf' unique_code="OrderSummaryCustomisePdf" elsif printer_setting.unique_code == 'OrderSummarySetCustomisePdf' - unique_code="OrderSummarySetCustomisePdf" + unique_code="OrderSummarySetCustomisePdf" elsif printer_setting.unique_code == 'OrderSummarySlimCustomisePdf' unique_code="OrderSummarySlimCustomisePdf" end - end - end + end + end print_settings=PrintSetting.find_by_unique_code(unique_code) order_queue_printer= Printer::OrderQueuePrinter.new(print_settings) @@ -240,21 +240,21 @@ class OrderQueueStation < ApplicationRecord else move_print_pdf(change_to,current_user,table_id,order_items,oqs) end - + 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) end #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) - + if pdf_status.nil? printer = PrintSetting.all.order("id ASC") unique_code="OrderItemPdf" if !printer.empty? printer.each do |printer_setting| - if printer_setting.unique_code == 'OrderItemPdf' + if printer_setting.unique_code == 'OrderItemPdf' unique_code="OrderItemPdf" elsif printer_setting.unique_code == 'OrderItemStarPdf' unique_code="OrderItemStarPdf" @@ -265,15 +265,15 @@ class OrderQueueStation < ApplicationRecord elsif printer_setting.unique_code == 'OrderItemCustomisePdf' unique_code="OrderItemCustomisePdf" elsif printer_setting.unique_code == 'OrderSetItemCustomisePdf' - unique_code="OrderSetItemCustomisePdf" + unique_code="OrderSetItemCustomisePdf" elsif printer_setting.unique_code == 'OrderItemSlimCustomisePdf' unique_code="OrderItemSlimCustomisePdf" - end + end end end # 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) order_queue_printer= Printer::OrderQueuePrinter.new(print_settings) if !assigned_items.nil? @@ -284,7 +284,7 @@ class OrderQueueStation < ApplicationRecord else move_print_pdf(change_to,current_user,table_id,assigned_items,oqs) end - + 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) end @@ -298,7 +298,7 @@ class OrderQueueStation < ApplicationRecord @moved_by = current_user @date = DateTime.now @shop = Shop.first - unique_code = "MoveTablePdf" + unique_code = "MoveTablePdf" # pdf_no = PrintSetting.where(:unique_code => unique_code).count print_settings = PrintSetting.find_by_unique_code(unique_code) # printer_array = [] @@ -307,10 +307,10 @@ class OrderQueueStation < ApplicationRecord # for i in 0..pdf_no # if i != pdf_no # 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) - # end - # end + # end + # end end end end diff --git a/app/models/sale.rb b/app/models/sale.rb index 54b7602b..8af6d72b 100644 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -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? diff --git a/app/models/shift_sale.rb b/app/models/shift_sale.rb index 07217103..13ffe052 100755 --- a/app/models/shift_sale.rb +++ b/app/models/shift_sale.rb @@ -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 diff --git a/app/models/shop.rb b/app/models/shop.rb index 75c30dd7..bc827c13 100755 --- a/app/models/shop.rb +++ b/app/models/shop.rb @@ -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) diff --git a/app/models/stock_journal.rb b/app/models/stock_journal.rb index 6e72d94a..b6f289b3 100755 --- a/app/models/stock_journal.rb +++ b/app/models/stock_journal.rb @@ -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") diff --git a/db/migrate/20191119043609_add_shop_code.rb b/db/migrate/20191119043609_add_shop_code.rb new file mode 100644 index 00000000..79bb92fb --- /dev/null +++ b/db/migrate/20191119043609_add_shop_code.rb @@ -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