update split bill

This commit is contained in:
Thein Lin Kyaw
2019-12-16 11:20:15 +06:30
parent a4c2b47f7a
commit 40ad724e77
5 changed files with 113 additions and 347 deletions

View File

@@ -23,11 +23,11 @@ class Origami::SalesController < BaseOrigamiController
end
def add_to_existing_invoice
Sale.transaction do
dining = params[:dining_id]
sale_id = params[:sale_id]
tax_type = params[:tax_type]
dining = params[:dining_id]
sale_id = params[:sale_id]
tax_type = params[:tax_type]
Sale.transaction do
table = DiningFacility.find(dining)
booking = table.current_checkin_booking

View File

@@ -16,335 +16,119 @@ class Origami::SplitBillController < BaseOrigamiController
@sale_data = Array.new
@current_user = current_user
table_bookings = Booking.where("dining_facility_id = #{dining_id} and sale_id IS NOT NULL")
if !table_bookings.nil?
table_bookings.each do |table_booking|
if table_booking.sale.sale_status != 'waste' && table_booking.sale.sale_status != 'spoile'
@sale_data.push(table_booking.sale)
end
end
end
@sale_data = @table.current_sales
if @booking
@booking.booking_orders.each do |booking_order|
arr_order_items = Array.new
@order = Order.find(booking_order.order_id)
if (@order.status == "new")
@orders.push(@order)
@orders = @booking.orders
@order = @orders[0]
@order_items = []
order_items = []
@order.order_items.each do |item|
if !item.set_menu_items.nil?
instance_item_sets = JSON.parse(item.set_menu_items)
arr_instance_item_sets = Array.new
instance_item_sets.each do |instance_item|
item_instance_name = MenuItemInstance.find_by_item_instance_code(instance_item["item_instance_code"]).item_instance_name
item.price = item.price.to_f + instance_item["price"].to_f
arr_instance_item_sets.push(item_instance_name)
end
item.set_menu_items = arr_instance_item_sets
end
@booking.order_items.each do |item|
if item.set_menu_items.present?
set_menu_items = JSON.parse(item.set_menu_items)
item.set_menu_items = set_menu_items.map { |x| x["item_instance_name"] }
item.price = item.price + set_menu_items.inject(0.0) { |sum, x| sum + x["item_instance_name"].to_f }
end
arr_item = Hash.new
if item.qty.to_i > 1
i = 1
while i <= item.qty.to_i do
arr_item = {'order_items_id' => item.order_items_id,
'order_id' => item.order_id,
'order_item_status' => item.order_item_status,
'item_order_by' => item.item_order_by,
'item_code' => item.item_code,
'item_instance_code' => item.item_instance_code,
'item_name' => item.item_name,
'alt_name' => item.alt_name,
'account_id' => item.account_id,
'qty' => '1.0',
'price' => item.price,
'remark' => item.remark,
'options' => item.options,
'set_menu_items' => item.set_menu_items,
'taxable' => item.taxable,
'completed_by' => item.completed_by,
'created_at' => item.created_at,
'updated_at' => item.updated_at}
i += 1
@order_items.push({@order.order_id => arr_item})
arr_order_items.push(arr_item)
end
else
arr_order_items.push(item)
@order_items.push({@order.order_id => item})
end
end
@order_items.push({'all_order' => arr_order_items})
end
end
else
@booking = nil
end
@order_items += item.qty.to_i.times.map { i = item.as_json; i["qty"] = 1; order_items << i; { item.order_id => i } }
end
@order_items << { 'all_order' => order_items }
else
@booking = nil
end
end
def create
cashier_type = params[:cashier_type]
dining_id = params[:dining_id]
order_ids = params[:order_ids]
arr_order_ids = nil
if !params[:arr_order_ids].nil?
arr_order_ids = JSON.parse(params[:arr_order_ids])
end
orders = nil
if !params[:orders].empty?
orders = JSON.parse(params[:orders])
end
order_items = nil
if !params[:order_items].empty?
order_items = JSON.parse(params[:order_items])
end
arr_order_ids = JSON.parse(params[:arr_order_ids]) if params[:arr_order_ids].present?
orders = JSON.parse(params[:orders]) if params[:orders].present?
order_items = JSON.parse(params[:order_items]) if params[:order_items].present?
status = false
if !ShiftSale.current_shift.nil?
#create Bill by Booking ID
table = 0
if !params[:booking_id].empty?
booking = Booking.find(params[:booking_id])
# for Multiple Cashier by Zone
if booking.dining_facility_id.to_i>0
table = DiningFacility.find(booking.dining_facility_id)
cashier_zone = CashierTerminalByZone.find_by_zone_id(table.zone_id)
else
table = nil
cashier_zone = nil
end
#create Bill by Booking ID
table = DiningFacility.find_by(id: params[:dining_id]) if params[:dining_id].present?
# shift_by_terminal = ShiftSale.find_by_cashier_terminal_id_and_shift_closed_at(cashier_zone.cashier_terminal_id,nil)
# get_cashier_by_terminal = Employee.find(shift_by_terminal.employee_id)
if sale_data = booking.sale
status = true
elsif sale_data = Sale.generate_invoice_from_booking(booking, current_user, current_user, cashier_type,params[:current_checkin_induties_count])
status = true
Booking.transaction do
if params[:booking_id].present?
booking = Booking.find(params[:booking_id])
else
status = false
type = "TableBooking" if params[:type] == "Table"
type ||= "RoomBooking"
split_orders = []
new_order = nil
booking = Booking.create({:dining_facility_id => table.id, :type => type, :checkin_at => Time.now.utc, :checkin_by => current_user.name, :booking_status => "assign" })
if orders.present?
split_orders += orders.map { |x| x["id"] }
end
if order_items.present?
order_items = order_items.inject([]) do |arr, v|
v["qty"] = v["qty"].to_i
if i = arr.find { |x| x["id"] == v["id"] }
i["qty"] = i["qty"] + v["qty"]
else
arr << v
end
arr
end
Order.includes(:order_items).where(order_id: order_ids).each do |order|
if order.order_items.any? { |x| order_items.none? { |y| x.order_items_id == y["id"] && x.qty == y["qty"] } }
new_order ||= Order.create({ source: "cashier", order_type: order.order_type, customer_id: order.customer_id, item_count: order_items.length, waiters: current_user.name })
order.order_items.each do |order_item|
if split_item = order_items.find { |x| x["id"] == order_item.order_items_id }
if split_item["qty"] == order_item.qty
new_order.order_items << order_item
else
order_item.qty = order_item.qty - split_item["qty"]
order_item.save
order_item_dup = order_item.dup
order_item_dup.qty = split_item["qty"]
new_order.order_items << order_item_dup
end
end
end
else
split_orders << order
end
end
end
if new_order.present?
booking.orders << new_order
end
puts split_orders
if split_orders.present?
BookingOrder.where(order_id: split_orders).update_all(booking_id: booking.booking_id)
end
end
if booking.sale.nil?
sale_data = Sale.generate_invoice_from_booking(booking, current_user, current_user, cashier_type, params[:current_checkin_induties_count])
Promotion.promo_activate(sale_data)
end
else
if params[:type] == "Table"
type = "TableBooking"
if ENV["SERVER_MODE"] == 'cloud'
from = request.subdomain + "." + request.domain
else
type = "RoomBooking"
from = ""
end
booking = Booking.create({:dining_facility_id => params[:dining_id],:type => type,
:checkin_at => Time.now.utc, :checkin_by => current_user.name,
:booking_status => "assign" })
ActionCable.server.broadcast "bill_channel",table: table,from:from
if !orders.nil?
orders.each do |order|
BookingOrder.find_by_order_id(order["id"]).delete
BookingOrder.create({:booking_id => booking.booking_id, :order_id => order["id"]})
end
elsif !order_items.nil?
order_item_count = 0
order_id_count = 0
order_id = nil
arr_order_ids.each do |order|
order.each do |odr|
data = Order.find(odr[0])
if data.order_items.count == odr[1]
order_id = odr[0]
order_id_count += 1
else
order_item_count += 1
end
end
end
# puts order_id
# puts order_ids.count
# puts order_id_count
# puts order_items.count
# puts order_item_count
if !order_id.nil?
if order_id_count > 1
puts "order_id_count > 1"
updated_order_id = Array.new
arr_order_ids.each do |order|
order.each do |odr|
data = Order.find(odr[0])
if data.order_items.count != odr[1]
updated_order_id.push(odr[0])
end
end
end
# puts "updated_order_id"
# puts updated_order_id
if !updated_order_id.empty?
order_ids.each do |odr_id|
unless updated_order_id.include?(odr_id)
BookingOrder.find_by_order_id(odr_id).delete
BookingOrder.create({:booking_id => booking.booking_id, :order_id => odr_id})
end
end
order_items.each do |order_item|
if updated_order_id.include?(order_item["order_id"])
update_order_item(order_id, order_item)
end
end
else
order_ids.each do |odr_id|
new_order_status = true
order_items.each do |order_item|
orderItem = OrderItem.find_by_order_id(odr_id)
if !orderItem.nil?
if order_item["id"] == orderItem.order_items_id
if orderItem.qty.to_f != order_item['qty'].to_f
new_order_status = false
end
end
end
end
# puts new_order_status
if new_order_status
BookingOrder.find_by_order_id(odr_id).delete
BookingOrder.create({:booking_id => booking.booking_id, :order_id => odr_id})
else
customer = Customer.find(params[:customer_id])
order_type = "dine_in"
if !customer.nil?
if customer.customer_type == "Takeaway"
order_type = "takeaway"
elsif customer.customer_type == "Delivery"
order_type = "delivery"
end
end
# begin
order = create_order(params,order_type,order_items.count,current_user)
BookingOrder.create({:booking_id => booking.booking_id, :order_id => order.order_id})
order_items.each do |order_item|
update_order_item(order.order_id, order_item)
end
end
end
end
else
# puts "order_id_count < 1"
new_order_status = true
order_items.each do |order_item|
orderItem = OrderItem.find(order_item["id"])
if !orderItem.nil?
if order_item["id"] == orderItem.order_items_id
if orderItem.qty.to_f != order_item['qty'].to_f
new_order_status = false
end
end
end
end
# puts new_order_status
if new_order_status
BookingOrder.find_by_order_id(order_id).delete
BookingOrder.create({:booking_id => booking.booking_id, :order_id => order_id})
order_items.each do |order_item|
update_order_item(order_id, order_item)
end
else
customer = Customer.find(params[:customer_id])
order_type = "dine_in"
if !customer.nil?
if customer.customer_type == "Takeaway"
order_type = "takeaway"
elsif customer.customer_type == "Delivery"
order_type = "delivery"
end
end
# begin
order = create_order(params,order_type,order_items.count,current_user)
BookingOrder.create({:booking_id => booking.booking_id, :order_id => order.order_id})
order_items.each do |order_item|
update_order_item(order.order_id, order_item)
end
end
end
else
if order_ids.count == 1 && order_item_count == 1
if order_id_count == 0
customer = Customer.find(params[:customer_id])
order_type = "dine_in"
if !customer.nil?
if customer.customer_type == "Takeaway"
order_type = "takeaway"
elsif customer.customer_type == "Delivery"
order_type = "delivery"
end
end
# begin
order = create_order(params,order_type,order_items.count,current_user)
BookingOrder.create({:booking_id => booking.booking_id, :order_id => order.order_id})
order_items.each do |order_item|
update_order_item(order.order_id, order_item)
end
else
BookingOrder.find_by_order_id(order_ids[0]).delete
BookingOrder.create({:booking_id => booking.booking_id, :order_id => order_ids[0]})
order_items.each do |order_item|
update_order_item(order_ids[0], order_item)
end
end
else
customer = Customer.find(params[:customer_id])
order_type = "dine_in"
if !customer.nil?
if customer.customer_type == "Takeaway"
order_type = "takeaway"
elsif customer.customer_type == "Delivery"
order_type = "delivery"
end
end
# begin
order = create_order(params,order_type,order_items.count,current_user)
BookingOrder.create({:booking_id => booking.booking_id, :order_id => order.order_id})
order_items.each do |order_item|
update_order_item(order.order_id, order_item)
end
end
end
end
if sale_data = Sale.generate_invoice_from_booking(booking, current_user, current_user, cashier_type ,params[:current_checkin_induties_count])
status = true
end
end
Promotion.promo_activate(sale_data)
if ENV["SERVER_MODE"] == 'cloud'
from = request.subdomain + "." + request.domain
else
from = ""
end
ActionCable.server.broadcast "bill_channel",table: table,from:from
render :json => { status: status }
else
render :json => { status: false, error_message: 'No Current Open Shift!'}
end
render :json => { status: true }
end
else
render :json => { status: false, error_message: 'No Current Open Shift!'}
end
end
def create_order(params,order_type,items_count,current_user)

View File

@@ -1,7 +1,7 @@
class Origami::TableInvoicesController < BaseOrigamiController
def index
@table = DiningFacility.find(params[:table_id])
shop = Shop.current_shop
shop = Shop.current_shop
puts "table bookig lenght"
@sale_array = Array.new
@table.bookings.each do |booking|
@@ -34,30 +34,7 @@ class Origami::TableInvoicesController < BaseOrigamiController
@table = DiningFacility.find(params[:table_id])
@membership = MembershipSetting::MembershipSetting
@payment_methods = PaymentMethodSetting.all
shop = Shop.current_shop
@sale_array = Array.new
@table.bookings.each do |booking|
if booking.sale_id.nil?
else
sale = Sale.find(booking.sale_id)
# rounding adjustment
if shop.is_rounding_adj
a = sale.grand_total % 25 # Modulus
b = sale.grand_total / 25 # Division
#not calculate rounding if modulus is 0 and division is even
#calculate rounding if modulus is zero or not zero and division are not even
if (a != 0.0 && b%2 != 0.0) || (a==0.0 && b%2 !=0)
new_total = Sale.get_rounding_adjustment(sale.grand_total)
sale.rounding_adjustment = new_total-sale.grand_total
sale.update_attributes(grand_total: new_total,old_grand_total: sale.grand_total,rounding_adjustment:sale.rounding_adjustment)
end
end
#end rounding adjustment
if sale.sale_status != "completed" && sale.sale_status != 'void' && sale.sale_status != "waste" && sale.sale_status != "spoile"
@sale_array.push(sale)
end
end
end
@sale_array = @table.current_sales
@sale = Sale.find(params[:invoice_id])
@date = @sale.created_at

View File

@@ -6,10 +6,13 @@ class DiningFacility < ApplicationRecord
has_one :cashier_terminal, through: :cashier_terminal_by_zone
has_many :bookings
has_many :current_bookings, -> { left_joins(:sale).assign.within_time_limit.merge(Booking.where(checkout_at: nil).or(Booking.merge(Sale.where(sale_status: ['new', nil])))) }, class_name: "Booking"
has_one :current_checkin_booking, -> { left_joins(:sale).assign.within_time_limit.merge(Sale.where(sale_status: nil)) }, class_name: "Booking"
has_one :current_checkout_booking, -> { left_joins(:sale).assign.within_time_limit.where.not(checkout_at: nil).merge(Sale.where(sale_status: 'new')) }, class_name: "Booking"
has_one :current_reserved_booking, -> { left_joins(:sale).assign.within_time_limit.where.not(reserved_at: nil).merge(Sale.where(sale_status: nil)) }, class_name: "Booking"
has_one :current_checkin_booking, -> { left_joins(:sale).assign.within_time_limit.merge(Sale.where(sale_status: nil)) }, class_name: "Booking"
has_one :current_checkout_booking, -> { left_joins(:sale).assign.within_time_limit.where.not(checkout_at: nil).merge(Sale.where(sale_status: 'new')) }, class_name: "Booking"
has_one :current_reserved_booking, -> { left_joins(:sale).assign.within_time_limit.where.not(reserved_at: nil).merge(Sale.where(sale_status: nil)) }, class_name: "Booking"
has_many :current_sales, -> { where(sale_status: 'new').merge(Booking.assign.within_time_limit) }, through: :bookings, class_name: "Sale", source: "sale"
TABLE_TYPE = "Table"
ROOM_TYPE = "Room"

View File

@@ -31,16 +31,18 @@ class MenuItem < ApplicationRecord
# Work with item_code = item_instance_code
def self.search_by_item_code(item_code)
MenuItem.joins(:menu_item_instances)
MenuItem.left_joins(:menu_item_instances => :menu_instance_item_sets)
.where(menu_item_instances: {item_instance_code: item_code})
.pluck(:type, :account_id, :item_code, :item_instance_code, :name, :alt_name, "menu_item_instances.item_instance_name AS item_instance_name", :price, :promotion_price, :is_on_promotion, "menu_item_instances.is_available", :taxable)
.map { |type, account_id, item_code, item_instance_code, item_name, item_alt_name, item_instance_name, price, promotion_price, is_on_promotion, is_available, taxable|
.pluck(:type, :account_id, :item_code, :item_instance_code, :name, :alt_name, :item_instance_name, :price, :promotion_price, :is_on_promotion, "menu_item_instances.is_available", :taxable, :item_set_id)
.map { |type, account_id, item_code, item_instance_code, item_name, item_alt_name, item_instance_name, price, promotion_price, is_on_promotion, is_available, taxable, item_set_id|
name = item_instance_name if item_set_id
name ||= "#{item_name}#{' - ' + item_instance_name if item_instance_name.present?}"
{
type: type,
account_id: account_id,
item_code: item_code,
item_instance_code: item_instance_code,
name: "#{item_name}#{' - ' + item_instance_name if item_instance_name.present?}",
name: "#{name}",
alt_name: "#{item_alt_name}",
price: price,
promotion_price: promotion_price,