Files
sx-fc/app/models/order_queue_station.rb
Myat Zin Wai Maw d1ab2c194d shop code
2019-11-22 18:24:02 +06:30

317 lines
12 KiB
Ruby
Executable File

# Order Queue station is where order are submitted to
# Order can send from tablet | table
# Source of order will determin where it will appear
class OrderQueueStation < ApplicationRecord
has_many :assigned_order_items
has_many :order_items
has_many :order_queue_process_by_zones
has_many :zones, through: :order_queue_process_by_zones
belongs_to :employee
scope :active, -> {where(is_active: true)}
# validations
validates_presence_of :station_name, :printer_name
def process_order (order, table_id, order_source = nil, pdf_status = nil ,change_to=nil,current_user=nil)
oqs_stations = OrderQueueStation.active.where("shop_code='#{order.shop_code}'")
order_items = order.order_items
if table_id.to_i > 0
# 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
oqs_order_items = []
if oqs.is_active
#Get List of items -
pq_items = JSON.parse(oqs.processing_items)
#Loop through the processing items
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 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)
# 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)
end
oqs_order_items.push(order_item)
end
# end
end
end
end
if oqs.auto_print && order_source != "quick_service"
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
print_slip(oqs, order, oqs_order_items,pdf_status,change_to,current_user,table_id)
end
is_auto_printed = true
end
end
end
end
else
oqs_stations.each do |oqs|
is_auto_printed = false
oqs_order_items = []
if oqs.is_active
#Get List of items -
pq_items = JSON.parse(oqs.processing_items)
#Loop through the processing items
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 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)
# 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)
end
oqs_order_items.push(order_item)
end
# end
end
end
end
if oqs.auto_print && order_source != "quick_service"
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
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 else
end
def pay_process_order_queue (order_id, table_id)
oqs_stations = OrderQueueStation.active
order = Order.find(order_id)
order_items = order.order_items
# Order.pay_process_order_queue(order_id,table_id)
# if order
# oqs = OrderQueueStation.new
# oqs.process_order(order, table_id)
# end
# assign_order = AssignedOrderItem.assigned_order_item_by_job(order_id)
# if ENV["SERVER_MODE"] == 'cloud'
# from = request.subdomain + "." + request.domain
# 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)
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
oqs_order_items = []
if oqs.is_active
#Get List of items -
pq_items = JSON.parse(oqs.processing_items)
#Loop through the processing items
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 (order_item.qty > 0)
oqs_order_items.push(order_item)
end
end
end
end
if oqs.auto_print
if oqs_order_items.length > 0
if oqs.cut_per_item
print_slip_item(oqs, order, oqs_order_items)
else
print_slip(oqs, order, oqs_order_items)
end
is_auto_printed = true
end
end
end
end
else
oqs_stations.each do |oqs|
is_auto_printed = false
oqs_order_items = []
if oqs.is_active
#Get List of items -
pq_items = JSON.parse(oqs.processing_items)
#Loop through the processing items
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 (order_item.qty > 0)
oqs_order_items.push(order_item)
end
end
end
end
if oqs.auto_print
if oqs_order_items.length > 0
if oqs.cut_per_item
print_slip_item(oqs, order, oqs_order_items)
else
print_slip(oqs, order, oqs_order_items)
end
is_auto_printed = true
end
end
end
end
end #end else
end
private
#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.where("shop_code='#{oqs.shop_code}'").order("id ASC")
unique_code="OrderSummaryPdf"
if !printer.empty?
printer.each do |printer_setting|
if printer_setting.unique_code == 'OrderSummaryPdf'
unique_code="OrderSummaryPdf"
elsif printer_setting.unique_code == 'OrderSummarySlimPdf'
unique_code="OrderSummarySlimPdf"
elsif printer_setting.unique_code == 'OrderSummarySetPdf'
unique_code="OrderSummarySetPdf"
elsif printer_setting.unique_code == 'OrderSummaryCustomisePdf'
unique_code="OrderSummaryCustomisePdf"
elsif printer_setting.unique_code == 'OrderSummarySetCustomisePdf'
unique_code="OrderSummarySetCustomisePdf"
elsif printer_setting.unique_code == 'OrderSummarySlimCustomisePdf'
unique_code="OrderSummarySlimCustomisePdf"
end
end
end
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
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.where("shop_code='#{oqs.shop_code}'").order("id ASC")
unique_code="OrderItemPdf"
if !printer.empty?
printer.each do |printer_setting|
if printer_setting.unique_code == 'OrderItemPdf'
unique_code="OrderItemPdf"
elsif printer_setting.unique_code == 'OrderItemStarPdf'
unique_code="OrderItemStarPdf"
elsif printer_setting.unique_code == 'OrderItemSlimPdf'
unique_code="OrderItemSlimPdf"
elsif printer_setting.unique_code == 'OrderSetItemPdf'
unique_code="OrderSetItemPdf"
elsif printer_setting.unique_code == 'OrderItemCustomisePdf'
unique_code="OrderItemCustomisePdf"
elsif printer_setting.unique_code == 'OrderSetItemCustomisePdf'
unique_code="OrderSetItemCustomisePdf"
elsif printer_setting.unique_code == 'OrderItemSlimCustomisePdf'
unique_code="OrderItemSlimCustomisePdf"
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_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|
order_queue_printer.print_order_item(print_settings, oqs,order_item.order_id, order_item.order_items_id, print_status="" )
end
end
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
def move_print_pdf(change_to,current_user,change_from,order_items,oqs)
if ENV["SERVER_MODE"] != "cloud" #no print in cloud server
# get printer info
@from = (DiningFacility.find(change_from)).name
@to = (DiningFacility.find(change_to)).name
@type = (DiningFacility.find(change_to)).type
@moved_by = current_user
@date = DateTime.now
@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)
# printer_array = []
# printer_array = PrintSetting.where(:unique_code => unique_code)
# for i in 0..pdf_no
# if i != pdf_no
# print_settings = printer_array[i]
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