Single database for multiple shops

Use ActsAsTenant as Multi-tenancy for shops

See below files:
- app/controllers/concern/multi_tenancy.rb
- app/models/application_record.rb
- app/models/shop.rb

An initializer can be created to control option in ActsAsTenant.
config/initializers/acts_as_tenant.rb
require 'acts_as_tenant/sidekiq'
ActsAsTenant.configure do |config|
  config.require_tenant = false # true
end

more details: https://github.com/ErwinM/acts_as_tenant
This commit is contained in:
Thein Lin Kyaw
2019-12-02 17:19:28 +06:30
parent 5d10d4b6a1
commit 937f40e7c1
78 changed files with 215 additions and 173 deletions

View File

@@ -1,3 +1,14 @@
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
def self.inherited(subclass)
super
return unless subclass.superclass == self
return unless subclass.column_names.include? 'shop_id'
subclass.class_eval do
acts_as_tenant(:shop)
end
end
end

View File

@@ -3,13 +3,13 @@ class KbzPay
KBZ_PAY = 'KBZPay'
def self.pay(amount, receipt_no, url, key, app_id, code)
shop = Shop.first
shop = Shop.current_shop
prefix = shop.shop_code
receipt_no = "#{prefix}#{receipt_no}"
datetime = DateTime.now.strftime("%d%m%Y%H%M")
kbz_app_id = app_id
kbz_merch_code = code
kbz_merch_code = code
kbz_api_key = key
kbz_provider_url = "#{url}/precreate"
@@ -58,17 +58,17 @@ class KbzPay
# Rails.logger.debug result['Response']
return false, result['Response']
end
end
def self.query(receipt_no, current_user, url, key, app_id, code)
shop = Shop.first
shop = Shop.current_shop
prefix = shop.shop_code
receipt_no = "#{prefix}#{receipt_no}"
amount = 0
datetime = DateTime.now.strftime("%d%m%Y%H%M")
kbz_app_id = app_id
kbz_merch_code = code
kbz_merch_code = code
kbz_api_key = key
kbz_provider_url = "#{url}/queryorder"
@@ -122,7 +122,7 @@ class KbzPay
# return true, "successfully paid by KBZ PAY"
elsif result['Response']['trade_status'] == "PAY_FAILED"
# return false, "pay failed by KBZ PAY"
elsif result['Response']['trade_status'] == "WAIT_PAY"
# return false , "Waiting to pay by KBZ PAY"
@@ -135,4 +135,4 @@ class KbzPay
return amount
end
end
end

View File

@@ -141,7 +141,7 @@ class Menu < ApplicationRecord
end
else
# Menu by Menu Import
@shop = Shop.first
@shop = Shop.current_shop
shop_code = ""
if !@shop.nil?
if @shop.shop_code

View File

@@ -297,7 +297,7 @@ class OrderQueueStation < ApplicationRecord
@type = (DiningFacility.find(change_to)).type
@moved_by = current_user
@date = DateTime.now
@shop = Shop.first
@shop = Shop.current_shop
unique_code = "MoveTablePdf"
# pdf_no = PrintSetting.where(:unique_code => unique_code).count
print_settings = PrintSetting.find_by_unique_code(unique_code)

View File

@@ -157,7 +157,7 @@ class OrderReservation < ApplicationRecord
def self.update_doemal_payment(order,current_user,receipt_bill)
if(Sale.exists?(order.sale_id))
saleObj = Sale.find(order.sale_id)
shop_details = Shop.first
shop_details = Shop.current_shop
# rounding adjustment
if shop_details.is_rounding_adj
a = saleObj.grand_total % 25 # Modulus
@@ -194,7 +194,7 @@ class OrderReservation < ApplicationRecord
shift = ShiftSale.find(saleObj.shift_sale_id)
cashier_terminal = CashierTerminal.find(shift.cashier_terminal_id)
shop_detail = Shop.first
shop_detail = Shop.current_shop
order_reservation = OrderReservation.get_order_reservation_info(saleObj.sale_id)
if !cashier_terminal.nil?
# Calculate Food and Beverage Total
@@ -400,7 +400,7 @@ class OrderReservation < ApplicationRecord
end
def self.check_new_order
shop = Shop.find_by_id(1)
shop = Shop.current_shop
if !shop.shop_code.nil?
shop_code = shop.shop_code
else
@@ -418,7 +418,7 @@ class OrderReservation < ApplicationRecord
end
def self.check_order_send_to_kitchen
shop = Shop.find_by_id(1)
shop = Shop.current_shop
if !shop.shop_code.nil?
shop_code = shop.shop_code
else
@@ -436,7 +436,7 @@ class OrderReservation < ApplicationRecord
end
def self.check_order_ready_to_delivery
shop = Shop.find_by_id(1)
shop = Shop.current_shop
if !shop.shop_code.nil?
shop_code = shop.shop_code
else

View File

@@ -38,7 +38,7 @@ class Printer::OrderQueuePrinter < Printer::PrinterWorker
end
# end
shop = Shop.first
shop = Shop.current_shop
directory_name = 'public/orders_'+shop.shop_code
Dir.mkdir(directory_name) unless File.exists?(directory_name)
@@ -80,7 +80,7 @@ class Printer::OrderQueuePrinter < Printer::PrinterWorker
print_setting = PrintSetting.all.order("id ASC")
order=print_query('order_summary', order_id)
shop = Shop.first
shop = Shop.current_shop
directory_name = 'public/orders_'+shop.shop_code
Dir.mkdir(directory_name) unless File.exists?(directory_name)
@@ -192,7 +192,7 @@ class Printer::OrderQueuePrinter < Printer::PrinterWorker
order=print_query('booking_summary', booking_id)
shop = Shop.first
shop = Shop.current_shop
directory_name = 'public/orders_'+shop.shop_code
Dir.mkdir(directory_name) unless File.exists?(directory_name)

View File

@@ -1,4 +1,3 @@
class Room < DiningFacility
has_many :bookings, :foreign_key => 'dining_facility_id'
end

View File

@@ -440,7 +440,7 @@ class Sale < ApplicationRecord
#compute - invoice total
def compute_by_sale_items(total_discount, discount_type=nil, order_source=nil, tax_type=nil, type=nil)
shop = Shop.first
shop = Shop.current_shop
#Computation Fields
subtotal_price = 0
@@ -507,7 +507,7 @@ class Sale < ApplicationRecord
# Tax Re-Calculte
def compute_tax(total_taxable, total_discount = 0, order_source = nil, tax_type=nil)
shop = Shop.first
shop = Shop.current_shop
#if tax is not apply create new record
# SaleTax.where("sale_id='#{sale.sale_id}'").find_each do |existing_tax|
@@ -627,7 +627,7 @@ class Sale < ApplicationRecord
# Tax Calculate
def apply_tax(total_taxable, order_source = nil, tax_type = nil)
shop = Shop.first
shop = Shop.current_shop
#if tax is not apply create new record
# SaleTax.where("sale_id='#{self.sale_id}'").find_each do |existing_tax|
@@ -645,7 +645,7 @@ class Sale < ApplicationRecord
order_source = "cashier"
end
# tax_data = TaxProfile.unscoped.where("group_type=?",order_source).pluck(:id)
# tax_data = TaxProfile.unscope(:order).where("group_type=?",order_source).pluck(:id)
# customer = Customer.find(self.customer_id).tax_profiles
tax_profiles = unique_tax_profiles(order_source, self.customer_id)
@@ -710,7 +710,7 @@ class Sale < ApplicationRecord
end
def adjust_rounding
shop_details = Shop.first
shop_details = Shop.current_shop
# rounding adjustment
if shop_details.is_rounding_adj
new_total = Sale.get_rounding_adjustment(self.grand_total)
@@ -726,7 +726,7 @@ class Sale < ApplicationRecord
#Generate new Receipt No when it is not assigned
def generate_receipt_no
#shop_code and client_code
shop_details = Shop.first
shop_details = Shop.current_shop
#Date-Shift-
if self.receipt_no.nil?
prefix = DateTime.now().utc
@@ -1239,7 +1239,7 @@ end
#product sale report query
def self.get_menu_item_query(order_by)
query = MenuItem.unscoped.select("acc.id as account_id,
query = MenuItem.unscope(:order).select("acc.id as account_id,
acc.title as account_name,
mii.item_instance_code as item_code, " +
"(CASE WHEN si.qty IS NOT NULL THEN SUM(si.qty) ELSE 0 END) as total_item," +

View File

@@ -1,13 +1,17 @@
class Shop < ApplicationRecord
#ShopDetail = Shop.find_by_id(1)
#ShopDetail = Shop.current_shop
# Shop Image Uploader
mount_uploader :logo, ShopImageUploader
mount_uploader :logo, ShopImageUploader
has_many :display_images
has_many :display_images
accepts_nested_attributes_for :display_images
def file_data=(input_data)
self.data = input_data.read
end
def self.current_shop
ActsAsTenant.current_tenant
end
end