shop code

This commit is contained in:
Myat Zin Wai Maw
2019-11-22 18:24:02 +06:30
parent 5a3f328789
commit d1ab2c194d
106 changed files with 834 additions and 895 deletions

View File

@@ -36,17 +36,18 @@ class AssignedOrderItem < ApplicationRecord
assigned_order_item.order_queue_station = order_queue_station
assigned_order_item.print_status = false
assigned_order_item.delivery_status = false
assigned_order_item.shop_code =order.shop_code
assigned_order_item.save!
end
def self.assigned_order_item_by_job(order_id)
order_item = AssignedOrderItem.select("assigned_order_items.assigned_order_item_id,
assigned_order_items.order_queue_station_id,
oqs.id as station_id, oqs.station_name,
oqs.is_active, oqpz.zone_id,
df.name as zone, df.type as table_type,
odt.order_id, odt.item_code, odt.item_instance_code, odt.item_name,
odt.price, odt.qty, odt.item_order_by, odt.options,
assigned_order_items.order_queue_station_id,
oqs.id as station_id, oqs.station_name,
oqs.is_active, oqpz.zone_id,
df.name as zone, df.type as table_type,
odt.order_id, odt.item_code, odt.item_instance_code, odt.item_name,
odt.price, odt.qty, odt.item_order_by, odt.options,
cus.name as customer_name, odt.created_at")
.joins(" left join order_queue_stations as oqs on oqs.id = assigned_order_items.order_queue_station_id
left join order_queue_process_by_zones as oqpz on oqpz.order_queue_station_id = oqs.id

View File

@@ -127,4 +127,9 @@ class Booking < ApplicationRecord
self.booking_id = SeedGenerator.generate_id(self.class.name, "BKI")
end
end
def generate_custom_id
if self.booking_id.nil?
self.booking_id = SeedGenerator.generate_id(self.class.name, "BKI")
end
end
end

View File

@@ -32,9 +32,8 @@ class DiningFacility < ApplicationRecord
end
def get_current_booking
checkin_time_lookup = Lookup.get_checkin_time_limit
booking = Booking.where("dining_facility_id = #{self.id} and booking_status ='assign' and (CASE WHEN checkin_at > '#{DateTime.now.utc}' THEN checkin_at >= '#{DateTime.now.utc}' ELSE checkin_at between '#{DateTime.now.utc - checkin_time_lookup.hours}' and '#{DateTime.now.utc}' END) and checkout_by is null").limit(1) #and checkout_at is null
# booking = Booking.where("dining_facility_id = #{self.id} and booking_status ='assign' and checkin_at between '#{DateTime.now.utc - 5.hours}' and '#{DateTime.now.utc}' and checkout_by is null").limit(1) #and checkout_at is null
checkin_time_lookup = Lookup.get_checkin_time_limit(self.shop_code)
booking = Booking.where("shop_code='#{self.shop_code}' and dining_facility_id = #{self.id} and booking_status ='assign' and (CASE WHEN checkin_at > '#{DateTime.now.utc}' THEN checkin_at >= '#{DateTime.now.utc}' ELSE checkin_at between '#{DateTime.now.utc - checkin_time_lookup.hours}' and '#{DateTime.now.utc}' END) and checkout_by is null").limit(1) #and checkout_at is null
if booking.count > 0 then
return booking[0]
else
@@ -43,8 +42,8 @@ class DiningFacility < ApplicationRecord
end
def get_moved_booking
checkin_time_lookup = Lookup.get_checkin_time_limit
booking = Booking.where("dining_facility_id = #{self.id} and booking_status ='moved' and checkin_at between '#{DateTime.now.utc - checkin_time_lookup.hours}' and '#{DateTime.now.utc}' and checkout_at is null").limit(1)
checkin_time_lookup = Lookup.get_checkin_time_limit(self.shop_code)
booking = Booking.where("shop_code='#{self.shop_code}' and dining_facility_id = #{self.id} and booking_status ='moved' and checkin_at between '#{DateTime.now.utc - checkin_time_lookup.hours}' and '#{DateTime.now.utc}' and checkout_at is null").limit(1)
if booking.count > 0 then
return booking[0]
@@ -70,8 +69,8 @@ class DiningFacility < ApplicationRecord
end
def get_current_checkout_booking
checkin_time_lookup = Lookup.get_checkin_time_limit
booking = Booking.where("dining_facility_id = #{self.id} and booking_status ='assign' and checkin_at between '#{DateTime.now.utc - checkin_time_lookup.hours}' and '#{DateTime.now.utc}' and reserved_by is not null and checkout_by is null").limit(1)
checkin_time_lookup = Lookup.get_checkin_time_limit(self.shop_code)
booking = Booking.where("shop_code='#{self.shop_code}' and dining_facility_id = #{self.id} and booking_status ='assign' and checkin_at between '#{DateTime.now.utc - checkin_time_lookup.hours}' and '#{DateTime.now.utc}' and reserved_by is not null and checkout_by is null").limit(1)
if booking.count > 0 then
return booking[0]
else
@@ -82,7 +81,7 @@ class DiningFacility < ApplicationRecord
def get_checkout_booking
booking = self.get_current_checkout_booking
if booking
lookup_checkout_time = Lookup.collection_of("checkout_alert_time")
lookup_checkout_time = Lookup.where("shop_code='#{self.shop_code}'").collection_of("checkout_alert_time")
free_time_min = 0
if !lookup_checkout_time.empty?
now = Time.now.utc
@@ -118,11 +117,11 @@ class DiningFacility < ApplicationRecord
end
def self.get_checkin_booking
checkin_time_lookup = Lookup.get_checkin_time_limit
bookings = Booking.where("booking_status ='assign' and checkin_at between '#{DateTime.now.utc - checkin_time_lookup.hours}' and '#{DateTime.now.utc}' and reserved_by is not null and checkout_by is null")
checkin_time_lookup = Lookup.get_checkin_time_limit(self.shop_code)
bookings = Booking.where("shop_code='#{self.shop_code}' and booking_status ='assign' and checkin_at between '#{DateTime.now.utc - checkin_time_lookup.hours}' and '#{DateTime.now.utc}' and reserved_by is not null and checkout_by is null")
arr_booking = Array.new
if bookings
lookup_checkout_time = Lookup.collection_of("checkout_alert_time")
lookup_checkout_time = Lookup.where("shop_code='#{self.shop_code}'").collection_of("checkout_alert_time")
free_time_min = 0
if !lookup_checkout_time.empty?
now = Time.now.utc

View File

@@ -3,7 +3,6 @@ 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
@@ -21,7 +20,7 @@ class Employee < ApplicationRecord
end
def self.login(shop,emp_id, password)
user = shop.employees.find_by_emp_id(emp_id)
user = Employee.find_by_emp_id_and_shop_code(emp_id,shop.shop_code)
expiry_time = login_expiry_time(shop)
if (user)
#user.authenticate(password)
@@ -39,7 +38,7 @@ class Employee < ApplicationRecord
def self.authenticate_by_token(session_token,shop)
if (session_token)
user = shop.employees.find_by_token_session(session_token)
user = Employee.find_by_token_session_and_shop_code(session_token,shop.shop_code)
expiry_time = login_expiry_time(shop)
if user && user.session_expiry.utc > DateTime.now.utc
@@ -57,7 +56,7 @@ class Employee < ApplicationRecord
def self.logout(shop,session_token)
if (session_token)
user = shop.employees.find_by_token_session(session_token)
user = Employee.find_by_token_session_and_shop_code(session_token,shop.shop_code)
if user
user.token_session = nil
@@ -75,7 +74,7 @@ class Employee < ApplicationRecord
def self.login_expiry_time(shop)
expiry_time = 30
login_expiry = shop.lookups.collection_of('expiry_time')
login_expiry = Lookup.where("shop_code='#{shop.shop_code}'").collection_of('expiry_time')
if !login_expiry.empty?
login_expiry.each do |exp_time|
if exp_time[0].downcase == "login"

View File

@@ -8,7 +8,7 @@ class InventoryDefinition < ApplicationRecord
logger.debug saleObj.sale_items.to_json
if !saleObj.nil?
saleObj.sale_items.each do |item|
found, inventory_definition = find_product_in_inventory(item)
found, inventory_definition = find_product_in_inventory(item,saleObj.shop_code)
if found
check_balance(item,inventory_definition)
end
@@ -23,7 +23,7 @@ class InventoryDefinition < ApplicationRecord
end
end
def self.find_product_in_inventory(item,instance_code)
def self.find_product_in_inventory(item,shop_code=nil)
# unless instance_code.empty?
# instance_code = instance_code.to_s
# instance_code[0] = ""
@@ -36,16 +36,15 @@ class InventoryDefinition < ApplicationRecord
# puts "found prodcut+++++++++++++++++++++++++++++++++++==="
# puts prod.to_json
# end
if product = InventoryDefinition.find_by_item_code(item.item_instance_code)
if stock_check_item = StockCheckItem.find_by_item_code(item.item_instance_code)
if product = InventoryDefinition.find_by_item_code_and_shop_code(item.item_instance_code,shop_code)
if stock_check_item = StockCheckItem.find_by_item_code_and_shop_code(item.item_instance_code,shop_code)
return true, product
end
end
end
def self.check_balance(item, inventory_definition) # item => saleItemOBj
stock = StockJournal.where('item_code=?', item.item_instance_code).order("id DESC").first
stock = StockJournal.where("shop_code='#{inventory_definition.shop_code}' and item_code=?", item.item_instance_code).order("id DESC").first
unless stock.nil?
modify_balance(item, stock, inventory_definition)
else
@@ -94,7 +93,7 @@ class InventoryDefinition < ApplicationRecord
.order("mi.menu_category_id desc")
end
def self.get_by_category(filter)
def self.get_by_category(shop,filter)
# THEN (SELECT min(balance) FROM stock_journals
# least_stock = "SELECT (CASE WHEN stock_journals.remark != 'out of stock'
# THEN (SELECT balance FROM stock_journals
@@ -123,9 +122,9 @@ class InventoryDefinition < ApplicationRecord
" JOIN menu_items mi ON mi.id = mii.menu_item_id" +
" JOIN menu_categories mc ON mc.id = mi.menu_category_id ")
.joins(" JOIN accounts acc ON acc.id = mi.account_id")
.where("inventory_definitions.item_code LIKE ? OR inventory_definitions.min_order_level LIKE ?
.where("inventory_definitions.shop_code='#{shop.shop_code}' and (inventory_definitions.item_code LIKE ? OR inventory_definitions.min_order_level LIKE ?
OR inventory_definitions.max_stock_level LIKE ? OR sj.balance LIKE ? OR mi.name LIKE ?
OR mii.item_instance_name LIKE ? OR mc.name LIKE ?","%#{filter}%","%#{filter}%","%#{filter}%",
OR mii.item_instance_name LIKE ? OR mc.name LIKE ?)","%#{filter}%","%#{filter}%","%#{filter}%",
"%#{filter}%","%#{filter}%","%#{filter}%","%#{filter}%")
.group("mi.menu_category_id,inventory_definitions.item_code")
.order("balance asc, mi.name asc,acc.title desc,mi.menu_category_id desc")

View File

@@ -22,10 +22,10 @@ class Lookup < ApplicationRecord
# Lookup.select("value, name").where("lookup_type = ?", lookup_type ).order("name asc").map { |r| [r.name, r.value] }
# end
def self.get_checkin_time_limit
def self.get_checkin_time_limit(shop_code)
time_limit = 5
lookup = Lookup.find_by_lookup_type('checkin_time_limit')
lookup = Lookup.find_by_lookup_type_and_shop_code('checkin_time_limit',shop_code)
if !lookup.nil?
time_limit = lookup.value.to_i
end
@@ -60,39 +60,41 @@ class Lookup < ApplicationRecord
end
def self.create_shift_sale_lookup
def self.create_shift_sale_lookup(shop_code)
@lookup = Lookup.new
@lookup.lookup_type = 'shift_sale_items'
@lookup.name = 'Shift Sale Items'
@lookup.value = 0
@lookup.shop_code = shop_code
@lookup.save
return @lookup
end
def self.create_reprint_receipt_lookup
def self.create_reprint_receipt_lookup(shop_code)
@lookup = Lookup.new
@lookup.lookup_type = 'reprint_receipt'
@lookup.name = 'Reprint Receipt in Report'
@lookup.value = 0
@lookup.shop_code = shop_code
@lookup.save
return @lookup
end
def self.save_shift_sale_items_settings(val)
@lookup = Lookup.where('lookup_type=?', 'shift_sale_items').last
def self.save_shift_sale_items_settings(val,shop_code)
@lookup = Lookup.where("shop_code='#{shop_code}' and lookup_type=?", 'shift_sale_items').last
if @lookup.nil?
@lookup = Lookup.create_shift_sale_lookup
@lookup = Lookup.create_shift_sale_lookup(shop_code)
end
@lookup.value = val
@lookup.save
end
def self.shift_sale_items_lookup_value
@lookup = Lookup.where('lookup_type=?', 'shift_sale_items').last
def self.shift_sale_items_lookup_value(shop_code)
@lookup = Lookup.where("shop_code='#{shop_code}' and lookup_type=?", 'shift_sale_items').last
if @lookup.nil?
@lookup = Lookup.create_shift_sale_lookup
@lookup = Lookup.create_shift_sale_lookup(shop_code)
end
return @lookup.value
end

View File

@@ -1,5 +1,5 @@
class MembershipSetting < ApplicationRecord
scope :active, -> { where(is_active: true) }
MembershipSetting = MembershipSetting.find_by_id(1)
# MembershipSetting = MembershipSetting.find_by_id(1)
end

View File

@@ -46,7 +46,7 @@ class Menu < ApplicationRecord
end
end
def self.import(file, created_by)
def self.import(file, created_by,shop)
status = ""
spreadsheet = open_spreadsheet(file)
if spreadsheet.sheets.count > 1
@@ -61,9 +61,9 @@ class Menu < ApplicationRecord
# 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"])
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"],shop_code: shop.shop_code)
else
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"],shop_code: shop.shop_code)
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])
@@ -92,9 +92,9 @@ class Menu < ApplicationRecord
elsif sheet_name == "Menu"
menu = Menu.find_by_id(row["id"])
if menu
Menu.create(name: row["name"], is_active: row["is_active"], is_ordering: row["is_ordering"], valid_days: row["valid_days"],valid_time_from: row["valid_time_from"], valid_time_to: row["valid_time_to"], created_by: row["created_by"])
Menu.create(name: row["name"], is_active: row["is_active"], is_ordering: row["is_ordering"], valid_days: row["valid_days"],valid_time_from: row["valid_time_from"], valid_time_to: row["valid_time_to"], created_by: row["created_by"],shop_code: shop.shop_code)
else
Menu.create(id:row["id"], name: row["name"], is_active: row["is_active"], is_ordering: row["is_ordering"], valid_days: row["valid_days"],valid_time_from: row["valid_time_from"], valid_time_to: row["valid_time_to"], created_by: row["created_by"])
Menu.create(id:row["id"], name: row["name"], is_active: row["is_active"], is_ordering: row["is_ordering"], valid_days: row["valid_days"],valid_time_from: row["valid_time_from"], valid_time_to: row["valid_time_to"], created_by: row["created_by"],shop_code: shop.shop_code)
end
elsif sheet_name == "Menu Category"
# MenuCategory.create(id:row["id"], menu_id: row["menu_id"], code: row["code"], name: row["name"], alt_name: row["alt_name"], order_by: row["order_by"], created_by: row["created_by"], menu_category_id: row["menu_category_id"], is_available: row["is_available"])
@@ -141,19 +141,13 @@ class Menu < ApplicationRecord
end
else
# Menu by Menu Import
@shop = Shop.first
shop_code = ""
if !@shop.nil?
if @shop.shop_code
shop_code = @shop.shop_code + "_"
end
end
shop_code= shop.shop_code + "_"
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,shop_code: shop.shop_code})
(4..sheet.last_row).each do |ii|
row = Hash[[sheet.row(3),sheet.row(ii)].transpose]
@@ -192,7 +186,7 @@ class Menu < ApplicationRecord
if !menu_itm
account = Account.find_by_title(row["Account"])
if account.nil?
account = Account.create({title: row["Account"], account_type: "0"})
account = Account.create({title: row["Account"], account_type: "0",shop_code: shop.shop_code})
end
image_path = ""

View File

@@ -35,12 +35,14 @@ class Order < ApplicationRecord
if self.is_extra_time && self.extra_time
booking = Booking.create({:dining_facility_id => table_id,:type => "TableBooking",
:checkin_at => Time.now.utc,:checkout_at => Time.now.utc + self.extra_time.to_i,
:checkin_by => self.employee_name,
:booking_status => "assign" })
:checkin_by => self.employee_name,
:booking_status => "assign",
:shop_code=>self.shop_code})
else
booking = Booking.create({:dining_facility_id => table_id,:type => "TableBooking",
:checkin_at => Time.now.utc, :checkin_by => self.employee_name,
:booking_status => "assign" })
:booking_status => "assign",
:shop_code=>self.shop_code })
end
#end extra time
@@ -290,11 +292,11 @@ class Order < ApplicationRecord
#Process order items and send to order queue
def self.pay_process_order_queue(id,table_id)
# if ENV["SERVER_MODE"] != 'cloud'
sidekiq = Lookup.find_by_lookup_type("sidekiq")
order = Order.find(id)
sidekiq = Lookup.find_by_lookup_type_and_shop_code("sidekiq",order.shop_code)
if !sidekiq.nil?
OrderQueueProcessorJob.perform_later(id, table_id)
else
order = Order.find(id)
if order
oqs = OrderQueueStation.new
oqs.process_order(order, table_id)

View File

@@ -136,7 +136,7 @@ class OrderItem < ApplicationRecord
print instance_code
end
if self.qty != self.qty_before_last_save
found, inventory_definition = InventoryDefinition.find_product_in_inventory(self,instance_code)
found, inventory_definition = InventoryDefinition.find_product_in_inventory(self,self.order.shop_code)
if found
InventoryDefinition.check_balance(self, inventory_definition)
end

View File

@@ -16,7 +16,7 @@ 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
oqs_stations = OrderQueueStation.active.where("shop_code='#{order.shop_code}'")
order_items = order.order_items
@@ -213,7 +213,7 @@ class OrderQueueStation < ApplicationRecord
#Print order_items in 1 slip
def print_slip(oqs, order, order_items ,pdf_status=nil,change_to=nil,current_user=nil,table_id=nil)
if pdf_status.nil?
printer = PrintSetting.all.order("id ASC")
printer = PrintSetting.where("shop_code='#{oqs.shop_code}'").order("id ASC")
unique_code="OrderSummaryPdf"
if !printer.empty?
@@ -234,7 +234,7 @@ class OrderQueueStation < ApplicationRecord
end
end
print_settings=PrintSetting.find_by_unique_code(unique_code)
print_settings=PrintSetting.find_by_unique_code_and_shop_code(unique_code,oqs.shop_code)
order_queue_printer= Printer::OrderQueuePrinter.new(print_settings)
order_queue_printer.print_order_summary(print_settings, oqs,order.order_id, order_items, print_status="")
else
@@ -249,7 +249,7 @@ class OrderQueueStation < ApplicationRecord
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")
printer = PrintSetting.where("shop_code='#{oqs.shop_code}'").order("id ASC")
unique_code="OrderItemPdf"
if !printer.empty?
@@ -274,7 +274,7 @@ class OrderQueueStation < ApplicationRecord
# order_item = OrderItem.where("order_id='#{assigned_item.order_id}' AND item_instance_code='#{assigned_item.instance_code}'").first()
# print when complete click
print_settings=PrintSetting.find_by_unique_code(unique_code)
print_settings=PrintSetting.find_by_unique_code_and_shop_code(unique_code,oqs.shop_code)
order_queue_printer= Printer::OrderQueuePrinter.new(print_settings)
if !assigned_items.nil?
assigned_items.each do |order_item|
@@ -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.find_by_shop_code(oqs.shop_code)
unique_code = "MoveTablePdf"
# pdf_no = PrintSetting.where(:unique_code => unique_code).count
print_settings = PrintSetting.find_by_unique_code(unique_code)

View File

@@ -40,8 +40,8 @@ class OrderReservation < ApplicationRecord
# unless customer.valid?
# render json: {
# status: 422,
# message: "Validation error",
# errors: customer.errors
# message: "Validation error",
# errors: customer.errors
# }.to_json
# return
# end
@@ -53,7 +53,7 @@ class OrderReservation < ApplicationRecord
Rails.logger.debug order_reserve.to_s
check_order_reservation = OrderReservation.where("transaction_ref = ?",order_reserve[:reference])
if check_order_reservation.empty?
OrderReservation.transaction do
OrderReservation.transaction do
begin
order_reservation = OrderReservation.new
order_reservation.order_reservation_type = order_reserve[:order_type]
@@ -84,7 +84,7 @@ class OrderReservation < ApplicationRecord
end
order_reservation.save!
if order_reserve[:order_info][:items]
order_reserve[:order_info][:items].each do |oritem|
order_reserve[:order_info][:items].each do |oritem|
OrderReservationItem.process_order_reservation_item(oritem[:product_code],oritem[:item_instance_code],oritem[:product_name],oritem[:product_alt_name],
oritem[:account_id],oritem[:qty],oritem[:price],oritem[:unit_price],
oritem[:options],nil,order_reservation.id)
@@ -93,13 +93,13 @@ class OrderReservation < ApplicationRecord
if order_reserve[:delivery_info]
Delivery.addDeliveryInfo(order_reserve[:delivery_info],order_reservation.id)
end
return order_reservation.id, true
rescue ActiveRecord::StatementInvalid
raise ActiveRecord::Rollback
return nil, false
end
end
end
else
return check_order_reservation[0].id, false
@@ -112,14 +112,14 @@ class OrderReservation < ApplicationRecord
items_arr = []
count = 1
order.order_reservation_items.each { |i|
order.order_reservation_items.each { |i|
i.item_instance_code = i.item_instance_code.downcase.to_s
items = {"order_item_id": count,"item_instance_code": i.item_instance_code,"quantity": i.qty,"options": i.options}
count += 1
items_arr.push(items)
}
customer_id = order.customer_id
@order = Order.new
@order.source = "doemal_order"
@order.order_type = "delivery"
@@ -138,7 +138,7 @@ class OrderReservation < ApplicationRecord
# Order.send_customer_view(@booking)
if @status && @booking
@status, @sale = Sale.request_bill(@order,current_user,current_user)
#order status send to doemal
@@ -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.find_by_shop_code(order.shop_code)
# rounding adjustment
if shop_details.is_rounding_adj
a = saleObj.grand_total % 25 # Modulus
@@ -167,11 +167,11 @@ class OrderReservation < ApplicationRecord
if (a != 0.0 && b%2 != 0.0) || (a==0.0 && b%2 !=0)
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
end
#end rounding adjustment
sale_payment = SalePayment.new
if(order.payment_type == "cash_on_delivery")
sale_payment.process_payment(saleObj, current_user, saleObj.grand_total, "cash")
@@ -183,9 +183,9 @@ class OrderReservation < ApplicationRecord
callback_response = send_status_to_ordering(order.callback_url,order.transaction_ref,DELIVERED)
#order reservation status updated
update_order_reservation(order.id, saleObj.sale_id, DELIVERED)
Rails.logger.debug "@@@@ receipt_bill :: "
Rails.logger.debug "@@@@ receipt_bill :: "
Rails.logger.debug receipt_bill
if receipt_bill == 1
#receipt bill pdf setting
# get printer info
@@ -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.first
order_reservation = OrderReservation.get_order_reservation_info(saleObj.sale_id)
if !cashier_terminal.nil?
# Calculate Food and Beverage Total
@@ -203,16 +203,16 @@ class OrderReservation < ApplicationRecord
other_amount = SaleItem.calculate_other_charges(saleObj.sale_items)
printer = Printer::ReceiptPrinter.new(print_settings)
filename, sale_receipt_no, printer_name = printer.print_receipt_bill(print_settings, false, nil,cashier_terminal,saleObj.sale_items,saleObj,saleObj.customer.name, item_price_by_accounts, discount_price_by_accounts, nil,nil,shop_detail, "Paid",nil,nil,other_amount,nil,nil, order_reservation)
filename, sale_receipt_no, printer_name = printer.print_receipt_bill(print_settings, false, nil,cashier_terminal,saleObj.sale_items,saleObj,saleObj.customer.name, item_price_by_accounts, discount_price_by_accounts, nil,nil,shop_details, "Paid",nil,nil,other_amount,nil,nil, order_reservation)
#receipt bill pdf setting
result = {:status=> true,
:filepath => filename,
:sale_id => saleObj.sale_id,
:receipt_no => sale_receipt_no,
:printer_name => printer_name,
:printer_model => print_settings.brand_name,
:printer_url => print_settings.api_settings ,
result = {:status=> true,
:filepath => filename,
:sale_id => saleObj.sale_id,
:receipt_no => sale_receipt_no,
:printer_name => printer_name,
:printer_model => print_settings.brand_name,
:printer_url => print_settings.api_settings ,
:message => DELIVERED }
else
result = {:status=> true, :message => DELIVERED }
@@ -223,7 +223,7 @@ class OrderReservation < ApplicationRecord
result = {:status=> true, :message => DELIVERED }
end
return result
end
end
end
def self.send_status_to_ordering(url,ref_no,status,waiting_time=nil,min_type=nil,reason=nil)
@@ -349,12 +349,12 @@ class OrderReservation < ApplicationRecord
shift.save
end
end
sale.payment_status = 'void'
sale.sale_status = 'void'
sale.save
# For Sale Audit
# For Sale Audit
if !current_user.nil?
action_by = current_user.name
else
@@ -363,7 +363,7 @@ class OrderReservation < ApplicationRecord
approved_name = nil
approved_by = Employee.find_by_emp_id(access_code)
if !approved_by.nil?
approved_name = approved_by.name
end
@@ -372,7 +372,7 @@ class OrderReservation < ApplicationRecord
sale_audit = SaleAudit.record_audit_for_edit(sale_id,cashier_name, approved_name,remark,"SALEVOID" )
# update complete order items in oqs
SaleOrder.where("sale_id = '#{ sale_id }'").find_each do |sodr|
SaleOrder.where("sale_id = '#{ sale_id }'").find_each do |sodr|
AssignedOrderItem.where("order_id = '#{ sodr.order_id }'").find_each do |aoi|
aoi.delivery_status = 1
aoi.save
@@ -410,7 +410,7 @@ class OrderReservation < ApplicationRecord
if order_reservation.length > 0
if ENV["SERVER_MODE"] == 'cloud'
from = request.subdomain + "." + request.domain
else
else
from = ""
end
ActionCable.server.broadcast "check_new_order_channel",data: order_reservation, shop_code: shop_code,from:from
@@ -428,7 +428,7 @@ class OrderReservation < ApplicationRecord
if order_reservation.length > 0
if ENV["SERVER_MODE"] == 'cloud'
from = request.subdomain + "." + request.domain
else
else
from = ""
end
ActionCable.server.broadcast "check_order_send_to_kitchen_channel",data: order_reservation, shop_code: shop_code,from:from
@@ -446,7 +446,7 @@ class OrderReservation < ApplicationRecord
if order_reservation.length > 0
if ENV["SERVER_MODE"] == 'cloud'
from = request.subdomain + "." + request.domain
else
else
from = ""
end
ActionCable.server.broadcast "check_order_ready_to_delivery_channel",data: order_reservation, shop_code: shop_code,from:from

View File

@@ -7,7 +7,8 @@ class PaymentJournal < ApplicationRecord
self.payment_method = payment_method
self.payment_status = 'paid'
self.payment_method_references = payment_method_reference
self.created_by = current_user
self.created_by = current_user.id
self.shop_code = current_user.shop_code
self.save
end
@@ -16,7 +17,8 @@ class PaymentJournal < ApplicationRecord
self.remark = remark
self.debit_amount = amount
self.payment_status = 'paid'
self.created_by = current_user
self.created_by = current_user.id
self.shop_code = current_user.shop_code
self.save
end

View File

@@ -38,8 +38,8 @@ class Printer::OrderQueuePrinter < Printer::PrinterWorker
end
# end
shop = Shop.first
directory_name = 'public/orders_'+shop.shop_code
# shop = Shop.first
directory_name = 'public/orders_'+oqs.shop_code
Dir.mkdir(directory_name) unless File.exists?(directory_name)
filename = directory_name + "/order_item_#{order_id}_#{order_item_id}" + ".pdf"
@@ -80,8 +80,8 @@ class Printer::OrderQueuePrinter < Printer::PrinterWorker
print_setting = PrintSetting.all.order("id ASC")
order=print_query('order_summary', order_id)
shop = Shop.first
directory_name = 'public/orders_'+shop.shop_code
# shop = Shop.first
directory_name = 'public/orders_'+oqs.shop_code
Dir.mkdir(directory_name) unless File.exists?(directory_name)
# For Print Per Item
@@ -192,8 +192,8 @@ class Printer::OrderQueuePrinter < Printer::PrinterWorker
order=print_query('booking_summary', booking_id)
shop = Shop.first
directory_name = 'public/orders_'+shop.shop_code
# shop = Shop.first
directory_name = 'public/orders_'+oqs.shop_code
Dir.mkdir(directory_name) unless File.exists?(directory_name)
# For Print Per Item

View File

@@ -195,7 +195,7 @@ class Printer::ReceiptPrinter < Printer::PrinterWorker
if !printer_settings.nil?
if !printer_settings.unique_code.strip.downcase.include? ("receiptbillorder")
pdf = ReceiptBillPdf.new(printer_settings, kbz_pay_status, qr_code, sale_items, sale_data, customer_name, item_price_by_accounts, discount_price_by_accounts, member_info,rebate_amount,shop_details,printed_status,balance,card_data,other_amount,latest_order_no,card_balance_amount)
settings = PrintSetting.all
settings = PrintSetting.where("shop_code='#{shop_details.shop_code}'")
if !settings.nil?
settings.each do |setting|
if setting.unique_code == 'ReceiptBillPdf'
@@ -206,7 +206,7 @@ class Printer::ReceiptPrinter < Printer::PrinterWorker
end
end
receipt_bill_a5_pdf = Lookup.collection_of("print_settings") #print_settings with name:ReceiptBillA5Pdf
receipt_bill_a5_pdf = Lookup.where("shop_code='#{shop_details.shop_code}'").collection_of("print_settings") #print_settings with name:ReceiptBillA5Pdf
if !receipt_bill_a5_pdf.empty?
receipt_bill_a5_pdf.each do |receipt_bilA5|
if receipt_bilA5[0] == 'ReceiptBillA5Pdf'

View File

@@ -33,9 +33,9 @@ class ProductCommission < ApplicationRecord
product_commission.save
end
def self.edit_product_commission(saleItemObj)
def self.edit_product_commission(saleItemObj,shop_code)
menu_item = MenuItem.find_by_item_code(saleItemObj.product_code)
commission = Commission.where('product_code = ? AND is_active = ?', menu_item.id, true).take
commission = Commission.where("shop_code='#{shop_code}' and product_code = ? AND is_active = ?", menu_item.id, true).take
product_commission = ProductCommission.where('sale_item_id = ?', saleItemObj.id).take
if !product_commission.nil?

View File

@@ -15,11 +15,11 @@ class Promotion < ApplicationRecord
promo_day.include? Date.today.wday.to_s
end
def self.promo_activate(saleObj)
def self.promo_activate(saleObj,shop_code)
current_day = Time.now.strftime("%Y-%m-%d")
current_time = Time.now.strftime('%H:%M:%S')
day = Date.today.wday
promoList = is_between_promo_datetime(current_day,current_time)
promoList = is_between_promo_datetime(current_day,current_time,shop_code)
promoList.each do |promo|
if promo.is_promo_day
@@ -30,8 +30,8 @@ class Promotion < ApplicationRecord
end
end
def self.is_between_promo_datetime(current_day,current_time) #database is not local time
promoList = Promotion.where("(Date_Format(promo_start_date, '%Y-%m-%d') <=? AND Date_Format(promo_end_date, '%Y-%m-%d') >=?) AND (promo_start_hour < ? AND promo_end_hour > ?)", current_day, current_day, current_time, current_time)
def self.is_between_promo_datetime(current_day,current_time,shop_code) #database is not local time
promoList = Promotion.where("shop_code='#{shop_code}' and (Date_Format(promo_start_date, '%Y-%m-%d') <=? AND Date_Format(promo_end_date, '%Y-%m-%d') >=?) AND (promo_start_hour < ? AND promo_end_hour > ?)", current_day, current_day, current_time, current_time)
return promoList
end
@@ -157,7 +157,7 @@ class Promotion < ApplicationRecord
sale_item.is_taxable = 1
sale_item.sale = saleObj
sale_item.save
saleObj.sale_items << sale_item
saleObj.compute_by_sale_items(saleObj.total_discount, nil, source)
end

View File

@@ -7,7 +7,6 @@ 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
@@ -30,7 +29,6 @@ class Sale < ApplicationRecord
scope :date_between, -> (from, to) { where("DATE(CONVERT_TZ(receipt_date, '+00:00', ?)) BETWEEN ? AND ?", Time.zone.formatted_offset, from, to) }
scope :time_between, -> (from, to) { where("TIME(CONVERT_TZ(receipt_date, '+00:00', ?)) BETWEEN ? AND ?", Time.zone.formatted_offset, from, to) }
scope :along_with_sale_payments_except_void, -> { joins("LEFT JOIN sale_payments on sales.sale_status != 'void' AND sale_payments.sale_id = sales.sale_id AND DATE(CONVERT_TZ(sale_payments.created_at,'+00:00','+06:30')) = DATE(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'))") }
def qty_of(item_instance_code)
order_items.select(:order_items_id, :item_instance_code, 'SUM(qty) as qty').where(item_instance_code: item_instance_code).group(:item_instance_code).first
end
@@ -137,8 +135,8 @@ class Sale < ApplicationRecord
order = Order.find(order_id)
# current cashier login
open_cashier = Employee.where("role = 'cashier' AND token_session <> ''")
current_shift = ShiftSale.current_shift
open_cashier = Employee.where("shop_code='#{order.shop_code}' and role = 'cashier' AND token_session <> ''")
current_shift = ShiftSale.current_shift(order.shop_code)
# shift with terminal zone
# set cashier
@@ -146,12 +144,12 @@ class Sale < ApplicationRecord
if !booking.dining_facility_id.nil?
table = DiningFacility.find(booking.dining_facility_id)
cashier_zone = CashierTerminalByZone.find_by_zone_id(table.zone_id)
shift = ShiftSale.where("shift_started_at is not null and shift_closed_at is null and cashier_terminal_id = #{cashier_zone.cashier_terminal_id}").first
shift = ShiftSale.where("shop_code='#{order.shop_code}' and shift_started_at is not null and shift_closed_at is null and cashier_terminal_id = #{cashier_zone.cashier_terminal_id}").first
#for multiple zone with terminal
if shift.nil?
multiple_zone = CashierTerminalByZone.where("zone_id = #{table.zone_id}")
multiple_zone.each do |zone|
shift = ShiftSale.where("shift_started_at is not null and shift_closed_at is null and cashier_terminal_id = #{zone.cashier_terminal_id}").first
shift = ShiftSale.where("shop_code='#{order.shop_code}' and shift_started_at is not null and shift_closed_at is null and cashier_terminal_id = #{zone.cashier_terminal_id}").first
if !shift.nil? then
break
end
@@ -159,7 +157,7 @@ class Sale < ApplicationRecord
end
end
else
shift = ShiftSale.current_open_shift(cashier.id)
shift = ShiftSale.current_open_shift(cashier)
end
# set cashier
if shift != nil #if current login employee open shift
@@ -171,7 +169,7 @@ class Sale < ApplicationRecord
# table and terminal in multiple shift
self.cashier_id = open_cashier[0].id
self.cashier_name = open_cashier[0].name
shift_id = ShiftSale.current_open_shift(open_cashier[0].id)
shift_id = ShiftSale.current_open_shift(open_cashier[0])
if shift_id
self.shift_sale_id = shift_id.id
else
@@ -189,7 +187,7 @@ class Sale < ApplicationRecord
self.requested_by = requested_by.name
self.requested_at = DateTime.now.utc.getlocal
self.shop_code = order.shop_code
Rails.logger.debug "Order -> #{order.id} | order_status -> #{order.status}"
if order
self.customer_id = order.customer_id
@@ -235,15 +233,13 @@ class Sale < ApplicationRecord
#fOR Quick Service pay and create
def self.request_bill(order,current_user,current_login_employee)
if !ShiftSale.current_shift.nil?
if !ShiftSale.current_shift(order.shop_code).nil?
order_id = order.order_id # order_id
bk_order = BookingOrder.find_by_order_id(order_id)
check_booking = Booking.find_by_booking_id(bk_order.booking_id)
if @sale_data = check_booking.sale
# Create Sale if it doesn't exist
# puts "current_login_employee"
# puts current_login_employee.name
@sale_items = SaleItem.where("sale_id=?",@sale_id)
elsif @sale_data = Sale.generate_invoice_from_booking(check_booking,current_login_employee,current_user,order.source)
@sale_items = SaleItem.where("sale_id=?",@sale_data.sale_id)
@@ -254,7 +250,7 @@ class Sale < ApplicationRecord
# @sale_data.save
# Promotion Activation
Promotion.promo_activate(@sale_data)
Promotion.promo_activate(@sale_data,order.shop_code)
@status = true
return @status, @sale_data
else
@@ -439,7 +435,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.first
#Computation Fields
subtotal_price = 0
@@ -506,7 +502,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.find_by_shop_code(self.shop_code)
#if tax is not apply create new record
# SaleTax.where("sale_id='#{sale.sale_id}'").find_each do |existing_tax|
@@ -560,7 +556,7 @@ class Sale < ApplicationRecord
# Tax Calculate
def apply_tax(total_taxable, order_source = nil, tax_type = nil)
shop = Shop.first
shop = Shop.find_by_shop_code(self.shop_code)
#if tax is not apply create new record
# SaleTax.where("sale_id='#{self.sale_id}'").find_each do |existing_tax|
@@ -640,7 +636,7 @@ class Sale < ApplicationRecord
end
def adjust_rounding
shop_details = Shop.first
shop_details = Shop.find_by_shop_code(self.shop_code)
# rounding adjustment
if shop_details.is_rounding_adj
new_total = Sale.get_rounding_adjustment(self.grand_total)
@@ -656,7 +652,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.find_by_shop_code(self.shop_code)
#Date-Shift-
if self.receipt_no.nil?
prefix = DateTime.now().utc
@@ -1526,7 +1522,7 @@ end
.order("SUM(i.qty) ASC").limit(20)
end
else
shift = ShiftSale.current_open_shift(current_user.id,shop)
shift = ShiftSale.current_open_shift(current_user)
if !shift.nil?
query = Sale.top_bottom(shop,today,shift,from,to,from_time,to_time)
if type == "top"
@@ -1557,7 +1553,7 @@ end
query = query.order("SUM(i.qty) ASC").limit(20)
end
else
shift = ShiftSale.current_open_shift(current_user.id,shop)
shift = ShiftSale.current_open_shift(current_user)
if !shift.nil?
query = Sale.top_bottom(shop,today,shift).group('i.product_name')
if type == "top"
@@ -1579,7 +1575,7 @@ end
if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor'
query = Sale.hourly_sale_data(shop,today,nil,from,to,from_time,to_time)
else
shift = ShiftSale.current_open_shift(current_user.id,shop)
shift = ShiftSale.current_open_shift(current_user)
if !shift.nil?
query = Sale.hourly_sale_data(shop,today,shift,from,to,from_time,to_time)
end
@@ -1592,7 +1588,7 @@ end
if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor'
query = Sale.hourly_sale_data(shop,today)
else
shift = ShiftSale.current_open_shift(current_user.id,shop)
shift = ShiftSale.current_open_shift(current_user)
if !shift.nil?
query = Sale.hourly_sale_data(shop,today,shift)
end
@@ -1603,7 +1599,7 @@ end
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,shop)
ShiftSale.current_open_shift(current_user)
end
payments_for_credits = SalePayment.joins(:sale_audit).to_sql
@@ -1616,8 +1612,8 @@ end
end
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"')
query = Sale.select("SUM(grand_total) as total_sale, COUNT(sales.sale_id) as total_count")
.where("sale_status = 'completed' AND shop_code='#{shop.shop_code}'")
if (!from.nil? && !to.nil?) && (from != "" && to!="")
query = query.date_between(from, to)
@@ -1628,7 +1624,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,shop)
if shift = ShiftSale.current_open_shift(current_user)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -1636,8 +1632,8 @@ end
end
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")')
query = Sale.joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id")
.where("sales.shop_code='#{shop.shop_code}' and 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)
if !from_time.nil? && !to_time.nil?
@@ -1647,7 +1643,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,shop)
if shift = ShiftSale.current_open_shift(current_user)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -1671,7 +1667,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,shop)
if shift = ShiftSale.current_open_shift(current_user)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -1680,8 +1676,8 @@ end
end
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"')
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')
.where("shop_code='#{shop.shop_code}' and sale_status = 'completed'")
if (!from.nil? && !to.nil?) && (from != "" && to!="")
query = query.date_between(from, to)
if !from_time.nil? && !to_time.nil?
@@ -1691,7 +1687,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,shop)
if shift = ShiftSale.current_open_shift(current_user)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -1699,8 +1695,8 @@ end
end
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"')
query = Sale.select("distinct sp.payment_method")
.where("sales.shop_code='#{shop.shop_code}' and sales.sale_status = 'completed'")
.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id")
if (!from.nil? && !to.nil?) && (from != "" && to!="")
query = query.date_between(from, to)
@@ -1711,7 +1707,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,shop)
if shift = ShiftSale.current_open_shift(current_user)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -1721,9 +1717,10 @@ end
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 = 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")
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")
.joins(:sale_payments)
.joins("LEFT JOIN (#{payments_for_credits}) payments_for_credits ON payments_for_credits.sale_id = sales.sale_id")
.where("sales.shop_code='#{shop.shop_code}'")
.completed
if payment_method == 'card'
@@ -1742,7 +1739,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,shop)
if shift = ShiftSale.current_open_shift(current_user)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -1773,9 +1770,9 @@ end
end
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")
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")
.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.shop_code='#{shop.shop_code}' and sales.sale_status = 'completed' and c.membership_id is null")
if (!from.nil? && !to.nil?) && (from != "" && to!="")
query = query.date_between(from, to)
if !from_time.nil? && !to_time.nil?
@@ -1785,7 +1782,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,shop)
if shift = ShiftSale.current_open_shift(current_user)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -1793,9 +1790,9 @@ end
end
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")
query = Sale.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))')
.where("sales.shop_code='#{shop.shop_code}' and 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!="")
query = query.date_between(from, to)
if !from_time.nil? && !to_time.nil?
@@ -1805,7 +1802,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,shop)
if shift = ShiftSale.current_open_shift(current_user)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -1859,8 +1856,9 @@ end
# end
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")
query = Sale.select("count(distinct sale_orders.order_id) as total_order")
.joins(:sale_orders)
.where("shop_code='#{shop.shop_code}'")
.completed
if (!from.nil? && !to.nil?) && (from != "" && to!="")
query = query.date_between(from, to)
@@ -1872,7 +1870,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,shop)
if shift = ShiftSale.current_open_shift(current_user)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -1881,10 +1879,10 @@ end
end
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")
query = Sale.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"')
.where("sales.shop_code='#{shop.shop_code}' and sales.sale_status = 'completed'")
if (!from.nil? && !to.nil?) && (from != "" && to!="")
query = query.date_between(from, to)
if !from_time.nil? && !to_time.nil?
@@ -1894,7 +1892,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,shop)
if shift = ShiftSale.current_open_shift(current_user)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -1902,9 +1900,9 @@ end
end
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")
query = Sale.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}'")
.where("sales.shop_code='#{shop.shop_code}' and sales.sale_status = 'completed' and a.account_id ='#{account_id}'")
if (!from.nil? && !to.nil?) && (from != "" && to!="")
query = query.date_between(from, to)
if !from_time.nil? && !to_time.nil?
@@ -1914,7 +1912,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,shop)
if shift = ShiftSale.current_open_shift(current_user)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -1922,9 +1920,9 @@ end
end
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")
query = Sale.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'")
.where("sales.shop_code='#{shop.shop_code}' and (a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed'")
if (!from.nil? && !to.nil?) && (from != "" && to!="")
query = query.date_between(from, to)
if !from_time.nil? && !to_time.nil?
@@ -1934,7 +1932,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,shop)
if shift = ShiftSale.current_open_shift(current_user)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -1944,8 +1942,8 @@ end
end
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%'")
query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id")
.where("sales.shop_code='#{shop.shop_code}' and 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)
if !from_time.nil? && !to_time.nil?
@@ -1955,7 +1953,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)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
@@ -2017,13 +2015,13 @@ end
query = query.where("bookings.booking_status = 'assign' AND orders.status = 'new' AND orders.source =? ","#{type}")
.group("bookings.booking_id")
end
def self.completed_sale(type)
def self.completed_sale(type,shop_code)
if type == "cashier"
type = "and orders.source = 'emenu' or orders.source = 'cashier'"
else
type = "and orders.source = '#{type}'"
end
query = Sale.all
query = Sale.where("sales.shop_code='#{shop_code}'")
query = query.joins("join sale_orders as sale_orders on sale_orders.sale_id = sales.sale_id")
.joins("join orders as orders on orders.order_id = sale_orders.order_id")
query = query.where("sales.sale_status != 'new' AND orders.status = 'billed' #{type}")
@@ -2086,8 +2084,7 @@ def self.get_sale_data_for_other_payment_credit(sale_id)
end
def unique_tax_profiles(order_source, customer_id)
tax_data = TaxProfile
.where(group_type: order_source)
tax_data = TaxProfile.where(group_type: order_source,shop_code: self.shop_code)
customer_tax_profiles = Customer.select(:tax_profiles).where(customer_id: customer_id).first
if customer_tax_profiles.present?
@@ -2099,24 +2096,24 @@ end
def self.top_bottom(shop,today,shift=nil,from=nil,to=nil,from_time=nil,to_time=nil)
if !from.nil? && !to.nil?
query = shop.sales.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," +
query = Sale.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?
query = query.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"+
" and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}' and sale_status= 'completed'")
else
query = query.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"+
query = query.where("shop_code='#{shop.shop_code}' and (i.qty > 0 and i.price > 0) and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"+
" and sale_status= 'completed'")
end
if !shift.nil?
query = query.where("shift_sale_id='#{shift.id}'")
end
else
query = shop.sales.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," +
query = Sale.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}'"+
.where("shop_code='#{shop.shop_code}' and (i.qty > 0 and i.price > 0) and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'"+
" and sale_status= 'completed'")
if !shift.nil?
query = query.where("shift_sale_id='#{shift.id}'")
@@ -2127,7 +2124,7 @@ end
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 = shop.sales.select("grand_total")
query = Sale.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
@@ -2136,11 +2133,11 @@ def self.hourly_sale_data(shop,today,shift=nil,from=nil,to=nil,from_time=nil,to_
if !shift.nil?
query = query.where("shift_sale_id='#{shift.id}'")
end
query = query.group("date_format(CONVERT_TZ(receipt_date,'+00:00', '+06:30'), '%I %p')")
query = query.where("shop_code='#{shop.shop_code}'").group("date_format(CONVERT_TZ(receipt_date,'+00:00', '+06:30'), '%I %p')")
.order('receipt_date')
else
query = shop.sales.select("grand_total")
.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today)
query = Sale.select("grand_total")
.where("shop_code='#{shop.shop_code}' and sale_status = 'completed' and DATE_FORMAT(receipt_date,'%Y-%m-%d') = ?",today)
if !shift.nil?
query = query.where("shift_sale_id='#{shift.id}'")
end
@@ -2152,9 +2149,9 @@ def self.hourly_sale_data(shop,today,shift=nil,from=nil,to=nil,from_time=nil,to_
end
def self.employee_sale(shop,today,shift=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = shop.sales.joins(:cashier)
query = Sale.joins(:cashier)
.joins(:sale_payments)
.paid.completed
.paid.completed.where("sales.shop_code='#{shop.shop_code}'")
if !from.nil? && !to.nil?
query = query.date_between(from, to)
if !from_time.nil? && !to_time.nil?
@@ -2475,7 +2472,7 @@ private
def update_stock_journal
if self.sale_status == "void" && self.sale_status_before_last_save != "void"
self.sale_items.each do |item|
found, inventory_definition = InventoryDefinition.find_product_in_inventory(item)
found, inventory_definition = InventoryDefinition.find_product_in_inventory(item,self.shop_code)
if found
stock = StockJournal.where('item_code=?', item.item_instance_code).order("id DESC").first
unless stock.nil?
@@ -2488,7 +2485,7 @@ private
end
elsif self.sale_status == "waste" || self.sale_status == "spoile" || (self.payment_status == "foc" && self.payment_status_was != "foc")
self.bookings.first.order_items.each do |item|
found, inventory_definition = InventoryDefinition.find_product_in_inventory(item)
found, inventory_definition = InventoryDefinition.find_product_in_inventory(item,self.shop_code)
if found
if stock_journal = StockJournal.find_by_trans_ref(item.order_items_id)
stock_journal.update(remark: self.sale_status)

View File

@@ -311,11 +311,11 @@ class SaleItem < ApplicationRecord
cancel_foc = self.status_before_last_save == "foc"
if is_void or cancel_void or is_edit or is_foc or cancel_foc
found, inventory_definition = InventoryDefinition.find_product_in_inventory(self)
found, inventory_definition = InventoryDefinition.find_product_in_inventory(self,self.sale.shop_code)
if found
stock = StockJournal.where('item_code=?', self.item_instance_code).order("id DESC").first
stock = StockJournal.where("shop_code='#{self.sale.shop_code}' and item_code=?", self.item_instance_code).order("id DESC").first
unless stock.nil?
check_item = StockCheckItem.where('item_code=?', self.item_instance_code).order("id DESC").first
check_item = StockCheckItem.where("shop_code='#{self.sale.shop_code}' and item_code=?", self.item_instance_code).order("id DESC").first
if is_void or cancel_void or is_edit
if is_void
qty = -self.qty

View File

@@ -34,9 +34,9 @@ class SalePayment < ApplicationRecord
end
end
def self.get_kbz_pay_amount(sale_id, current_user)
def self.get_kbz_pay_amount(sale_id, current_user,shop)
amount = 0
kbz_pay_method = PaymentMethodSetting.where(:payment_method => KbzPay::KBZ_PAY).last
kbz_pay_method = PaymentMethodSetting.where(:payment_method => KbzPay::KBZ_PAY,:shop_code => shop.shop_code).last
sale_payment = SalePayment.where('sale_id=? and payment_method=? and payment_status!=?', sale_id, KbzPay::KBZ_PAY, 'dead').last
if !sale_payment.nil? and !kbz_pay_method.nil?
if sale_payment.payment_status == 'pending'
@@ -136,11 +136,12 @@ class SalePayment < ApplicationRecord
#record an payment in sale-audit
remark = "Payment #{payment_method}- for Invoice #{invoice.receipt_no} Due [#{amount_due}]| pay amount -> #{cash_amount} | Payment Status ->#{payment_status}"
if payment_for
shift = ShiftSale.current_open_shift(self.sale.cashier_id)
current_shift_user =Employee.find_by_id(self.sale.cashier_id)
shift = ShiftSale.current_open_shift(current_shift_user)
if !shift.nil?
shift_sale_id = shift.id
else
shift = ShiftSale.current_shift
shift = ShiftSale.current_shift(self.sale.shop_code)
shift_sale_id = shift.id
end
@@ -726,11 +727,12 @@ class SalePayment < ApplicationRecord
# update for cashier shift
def update_shift
shift = ShiftSale.current_open_shift(self.action_by.id)
current_shift_user = Employee.find_by_id(self.action_by.id)
shift = ShiftSale.current_open_shift(current_shift_user)
if shift.nil?
shift = ShiftSale.current_open_shift(self.sale.cashier_id)
current_shift_user = Employee.find_by_id(self.sale.cashier_id)
shift = ShiftSale.current_open_shift(current_shift_user)
end
if !shift.nil?
@@ -740,7 +742,7 @@ class SalePayment < ApplicationRecord
self.sale.cashier_name = Employee.find(shift.employee_id).name
self.sale.save
else
shift = ShiftSale.current_shift
shift = ShiftSale.current_shift(self.sale.shop_code)
shift.update(self.sale)
self.sale.shift_sale_id = shift.id
self.sale.cashier_id = shift.employee_id
@@ -972,7 +974,7 @@ class SalePayment < ApplicationRecord
end
#credit payment query
def self.get_credit_sales(params)
def self.get_credit_sales(params,shop_code)
receipt_no = ""
customer = ""
if !params["receipt_no"].blank?
@@ -1001,7 +1003,7 @@ class SalePayment < ApplicationRecord
else
query = query.where("(CASE WHEN (s.grand_total + s.amount_changed)=(select SUM(payment_amount) FROM sale_payments WHERE sale_id=s.sale_id AND payment_method!='creditnote') THEN NULL ELSE payment_method='creditnote' AND #{order_source_query}='#{params[:type]}' END) and s.sale_status = 'completed' and s.payment_status='paid' #{receipt_no} #{customer}")
end
query = query.group("s.receipt_no")
query = query.where("s.shop_code='#{shop_code}'").group("s.receipt_no")
.order("s.receipt_date ASC, s.receipt_no ASC")
return query
end

View File

@@ -18,18 +18,18 @@ class ShiftSale < ApplicationRecord
has_many :sales
belongs_to :shop
def self.current_shift
def self.current_shift(shop_code)
# today_date = DateTime.now.strftime("%Y-%m-%d")
shift = ShiftSale.where("shift_started_at is not null and shift_closed_at is null").first
shift = ShiftSale.where("shop_code='#{shop_code}' and shift_started_at is not null and shift_closed_at is null").first
return shift
end
def self.current_open_shift(current_user,shop)
def self.current_open_shift(current_user)
#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 = shop.shift_sales.where("shift_started_at is not null and shift_closed_at is null and employee_id = #{current_user}").take
shift = ShiftSale.where("shop_code='#{current_user.shop_code}' and shift_started_at is not null and shift_closed_at is null and employee_id = #{current_user.id}").take
return shift
#end
end

View File

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

View File

@@ -1,11 +1,12 @@
class StockCheck < ApplicationRecord
has_many :stock_check_items
def create(user, reason, item_list)
def create(user, reason, item_list,shop)
self.reason = reason
self.check_by = user.id
self.check_start = Time.now
self.check_end = Time.now
self.shop_code = shop.shop_code
save
item_list.each do |item|
stockItem = StockCheckItem.new

View File

@@ -67,7 +67,7 @@ class StockCheckItem < ApplicationRecord
return query
end
def self.delete_stock_check_item(item_code)
self.where("item_code=?", item_code).delete_all
def self.delete_stock_check_item(item_code,shop)
self.where("item_code=? and shop_code='#{shop.shop_code}'", item_code).delete_all
end
end

View File

@@ -24,7 +24,8 @@ class StockJournal < ApplicationRecord
inventory_definition_id: inventory_definition.id,
remark: stock_message,
trans_ref: trans_ref,
trans_type: trans_type
trans_type: trans_type,
shop_code: inventory_definition.shop_code
)
end
@@ -32,41 +33,44 @@ class StockJournal < ApplicationRecord
return balance.to_i - qty.to_i
end
def self.from_stock_check(item)
stock_journal = StockJournal.where('item_code=?', item.item_code).order("id DESC").first
def self.from_stock_check(item,shop)
stock_journal = StockJournal.where("shop_code='#{shop.shop_code}' and item_code=?", item.item_code).order("id DESC").first
if stock_journal.nil?
old_blance = 0
inventory_definition_id = InventoryDefinition.find_by_item_code_and_shop_code(item.item_code,shop.shop_code).id
else
old_blance = stock_journal.balance
inventory_definition_id = stock_journal.inventory_definition_id
end
definition_id = InventoryDefinition.find_by_item_code(item.item_code)
# definition_id = InventoryDefinition.find_by_item_code(item.item_code)
journal = StockJournal.new
journal.item_code = item.item_code
journal.inventory_definition_id = definition_id.id
journal.inventory_definition_id = inventory_definition_id
journal.debit = 0
journal.credit = item.stock_count
journal.balance = item.stock_count + old_blance.to_i
journal.remark = StockJournal::STOCK_CHECK_TRANS
journal.trans_ref = item.id
journal.trans_type = StockJournal::STOCK_CHECK_TRANS
journal.shop_code = shop.shop_code
journal.save
end
def self.inventory_balances(today,from,to,from_time,to_time,shop)
if !from.nil? && !to.nil?
query = shop.stock_journals.select("mii.item_instance_name as item_instance_name,balance")
query = StockJournal.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}'")
else
query = query.where("DATE_FORMAT(CONVERT_TZ(stock_journals.created_at,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}' and DATE_FORMAT(CONVERT_TZ(stock_journals.created_at,'+00:00','+06:30'),'%H:%M') between '#{from_time}' and '#{to_time}'")
end
query = query.group("mii.item_instance_name")
query = query.where("shop_code='#{shop.shop_code}'").group("mii.item_instance_name")
.order("mii.item_instance_name ASC")
else
query = shop.stock_journals.select("mii.item_instance_name as item_instance_name,balance")
query = StockJournal.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}'")
.where("shop_code='#{shop.shop_code}' and DATE_FORMAT(stock_journals.created_at,'%Y-%m-%d') = '#{today}'")
.group("mii.item_instance_name")
.order("mii.item_instance_name ASC")
end
@@ -81,8 +85,8 @@ class StockJournal < ApplicationRecord
end
end
def self.delete_stock_journal(item_code)
self.where("item_code=?", item_code).delete_all
def self.delete_stock_journal(item_code,shop)
self.where("item_code=? and shop_code='#{shop.shop_code}'", item_code).delete_all
end
end