261 lines
11 KiB
Ruby
261 lines
11 KiB
Ruby
class OrderReservation < ApplicationRecord
|
|
self.primary_key = "order_reservation_id"
|
|
|
|
#primary key - need to be unique generated for multiple shops
|
|
before_create :generate_custom_id
|
|
|
|
has_many :order_reservation_items
|
|
belongs_to :delivery
|
|
|
|
SEND_TO_KITCHEN = "send_to_kitchen"
|
|
READY_TO_DELIVERY = "ready_to_deliver"
|
|
DELIVERED = "delivered"
|
|
COMPLETED = "completed"
|
|
|
|
def self.addOrderReservationInfo(params)
|
|
check_order_reservation = OrderReservation.where("transaction_ref = ?",params[:reference])
|
|
if check_order_reservation.empty?
|
|
order_reservation = OrderReservation.new
|
|
order_reservation.order_reservation_type = params[:order_type]
|
|
order_reservation.customer_id = params[:cus_info]
|
|
order_reservation.requested_time = DateTime.parse(params[:requested_time]).utc.strftime("%Y-%m-%d %H:%M:%S")
|
|
order_reservation.callback_url = params[:callback_url]
|
|
order_reservation.transaction_ref = params[:reference]
|
|
if params[:order_info]
|
|
order_reservation.item_count = params[:order_info][:items].count
|
|
order_reservation.payment_type = params[:payment_info][:payment_type]
|
|
order_reservation.payment_status = params[:payment_info][:payment_status]
|
|
order_reservation.payment_ref = params[:payment_info][:payment_ref]
|
|
order_reservation.total_amount = params[:payment_info][:sub_total]
|
|
order_reservation.total_tax = params[:payment_info][:total_tax]
|
|
order_reservation.discount_amount = params[:payment_info][:discount_amount]
|
|
order_reservation.grand_total = params[:payment_info][:grand_total]
|
|
order_reservation.order_remark = params[:order_info][:order_remark]
|
|
end
|
|
if params[:reservation_info]
|
|
order_reservation.total_customer = params[:reservation_info][:total_user]
|
|
order_reservation.reservation_remark = params[:reservation_info][:reservation_note]
|
|
end
|
|
order_reservation.save!
|
|
if params[:order_info][:items]
|
|
params[: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)
|
|
end
|
|
end
|
|
if params[:delivery_info]
|
|
Delivery.addDeliveryInfo(params[:delivery_info],order_reservation.id)
|
|
end
|
|
return order_reservation.id, true
|
|
else
|
|
return check_order_reservation[0].id, false
|
|
end
|
|
end
|
|
|
|
def self.create_doemal_order(order,current_user)
|
|
is_extra_time = false
|
|
extra_time = ''
|
|
|
|
items_arr = []
|
|
count = 1
|
|
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": []}
|
|
count += 1
|
|
items_arr.push(items)
|
|
}
|
|
|
|
puts items_arr.to_json
|
|
puts "sssssssssssssssssssssss"
|
|
customer_id = order.customer_id
|
|
|
|
@order = Order.new
|
|
@order.source = "doemal_order"
|
|
@order.order_type = "delivery"
|
|
@order.customer_id = customer_id
|
|
@order.items = items_arr
|
|
@order.guest = ''
|
|
@order.table_id = nil # this is dining facilities's id
|
|
@order.new_booking = true
|
|
@order.waiters = current_user.name
|
|
@order.employee_name = current_user.name
|
|
|
|
@order.is_extra_time = is_extra_time
|
|
@order.extra_time = extra_time
|
|
|
|
@status, @booking = @order.generate
|
|
|
|
# Order.send_customer_view(@booking)
|
|
if @status && @booking
|
|
|
|
@status, @sale = Sale.request_bill(@order,current_user,current_user)
|
|
|
|
#order status send to doemal
|
|
callback_response = send_status_to_ordering(order.callback_url,order.transaction_ref,SEND_TO_KITCHEN)
|
|
#order reservation status updated
|
|
update_order_reservation(order.id, @sale.sale_id, SEND_TO_KITCHEN)
|
|
|
|
result = {:status=> @status, :data => @sale, :message => "created" }
|
|
return result
|
|
end
|
|
end
|
|
|
|
def self.update_doemal_payment(order,current_user)
|
|
if(Sale.exists?(order.sale_id))
|
|
saleObj = Sale.find(order.sale_id)
|
|
shop_details = Shop.first
|
|
# rounding adjustment
|
|
if shop_details.is_rounding_adj
|
|
a = saleObj.grand_total % 25 # Modulus
|
|
b = saleObj.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(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)
|
|
end
|
|
end
|
|
#end rounding adjustment
|
|
|
|
sale_payment = SalePayment.new
|
|
sale_payment.process_payment(saleObj, current_user.name, saleObj.grand_total, "cash")
|
|
|
|
#order status send to doemal
|
|
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)
|
|
|
|
result = {:status=> true, :message => DELIVERED }
|
|
return result
|
|
# rebate_amount = nil
|
|
|
|
# For Cashier by Zone
|
|
# bookings = Booking.where("sale_id='#{sale_id}'")
|
|
|
|
# if bookings[0].dining_facility_id.to_i > 0
|
|
# table = DiningFacility.find(bookings[0].dining_facility_id)
|
|
# cashier_zone = CashierTerminalByZone.find_by_zone_id(table.zone_id)
|
|
# cashier_terminal = CashierTerminal.find(cashier_zone.cashier_terminal_id)
|
|
# else
|
|
# shift = ShiftSale.find(saleObj.shift_sale_id)
|
|
# cashier_terminal = CashierTerminal.find(shift.cashier_terminal_id)
|
|
# end
|
|
|
|
# For Print
|
|
# if ENV["SERVER_MODE"] != "cloud" #no print in cloud server
|
|
# receipt_bill_a5_pdf = Lookup.collection_of("print_settings") #print_settings with name:ReceiptBillA5Pdf
|
|
# unique_code = "ReceiptBillPdf"
|
|
# if !receipt_bill_a5_pdf.empty?
|
|
# receipt_bill_a5_pdf.each do |receipt_bilA5|
|
|
# if receipt_bilA5[0] == 'ReceiptBillA5Pdf'
|
|
# if receipt_bilA5[1] == '1'
|
|
# unique_code = "ReceiptBillA5Pdf"
|
|
# else
|
|
# unique_code = "ReceiptBillPdf"
|
|
# end
|
|
# end
|
|
# end
|
|
# end
|
|
|
|
# customer= Customer.find(saleObj.customer_id)
|
|
|
|
# get member information
|
|
# rebate = MembershipSetting.find_by_rebate(1)
|
|
# credit_data = SalePayment.find_by_sale_id_and_payment_method(sale_id,'creditnote')
|
|
|
|
# if customer.membership_id != nil && rebate && credit_data.nil?
|
|
# member_info = Customer.get_member_account(customer)
|
|
# if member_info["status"] == true
|
|
# rebate_amount = Customer.get_membership_transactions(customer,saleObj.receipt_no)
|
|
# current_balance = SaleAudit.paymal_search(sale_id)
|
|
# end
|
|
# end
|
|
|
|
#orders print out
|
|
# if params[:type] == "quick_service"
|
|
# booking = Booking.find_by_sale_id(sale_id)
|
|
# if booking.dining_facility_id.to_i>0
|
|
# table_id = booking.dining_facility_id
|
|
# else
|
|
# table_id = 0
|
|
# end
|
|
|
|
# booking.booking_orders.each do |order|
|
|
# # Order.pay_process_order_queue(order.order_id, table_id)
|
|
# oqs = OrderQueueStation.new
|
|
# oqs.pay_process_order_queue(order.order_id, table_id)
|
|
# end
|
|
|
|
# end
|
|
|
|
#for card sale data
|
|
# card_data = Array.new
|
|
# card_sale_trans_ref_no = Sale.getCardSaleTrans(sale_id)
|
|
# if !card_sale_trans_ref_no.nil?
|
|
# card_sale_trans_ref_no.each do |cash_sale_trans|
|
|
# card_res_date = cash_sale_trans.res_date.strftime("%Y-%m-%d").to_s
|
|
# card_res_time = cash_sale_trans.res_time.strftime("%H:%M").to_s
|
|
# card_no = cash_sale_trans.pan.last(4)
|
|
# card_no = card_no.rjust(19,"**** **** **** ")
|
|
# card_data.push({'res_date' => card_res_date, 'res_time' => card_res_time, 'batch_no' => cash_sale_trans.batch_no, 'trace' => cash_sale_trans.trace, 'pan' => card_no, 'app' => cash_sale_trans.app, 'tid' => cash_sale_trans.terminal_id, 'app_code' => cash_sale_trans.app_code, 'ref_no' => cash_sale_trans.ref_no, 'mid' => cash_sale_trans.merchant_id})
|
|
# end
|
|
# end
|
|
|
|
# get printer info
|
|
# print_settings=PrintSetting.find_by_unique_code(unique_code)
|
|
# Calculate Food and Beverage Total
|
|
# item_price_by_accounts = SaleItem.calculate_price_by_accounts(saleObj.sale_items)
|
|
# discount_price_by_accounts = SaleItem.get_discount_price_by_accounts(saleObj.sale_items)
|
|
|
|
# printer = Printer::ReceiptPrinter.new(print_settings)
|
|
# filename, sale_receipt_no, printer_name = printer.print_receipt_bill(print_settings,cashier_terminal,saleObj.sale_items,saleObj,customer.name, item_price_by_accounts, discount_price_by_accounts, member_info,rebate_amount,shop_details, "Paid",current_balance,card_data)
|
|
|
|
# render json: JSON.generate({:status => saleObj.rebate_status, :message => "Can't Rebate coz of Sever Error ", :filename => filename, :receipt_no => sale_receipt_no, :printer_name => printer_name})
|
|
|
|
#end
|
|
end
|
|
end
|
|
|
|
def self.send_status_to_ordering(url,ref_no,status)
|
|
base_url = "http://192.168.1.186:3002"
|
|
post_url = base_url + url
|
|
|
|
begin
|
|
response = HTTParty.post(post_url,
|
|
:body => { id: ref_no, status: status}.to_json,
|
|
:headers => {
|
|
'Authorization' => 'Token token=3T-tnlYtFJ-5Z1vY6XQqxQ',
|
|
'Content-Type' => 'application/json',
|
|
'Accept' => 'application/json; version=3'
|
|
}, :timeout => 10
|
|
)
|
|
rescue Net::OpenTimeout
|
|
response = { status: false }
|
|
|
|
rescue OpenURI::HTTPError
|
|
response = { status: false}
|
|
rescue SocketError
|
|
response = { status: false}
|
|
end
|
|
Rails.logger.debug "Get Doemal Status "
|
|
Rails.logger.debug response.to_json
|
|
return response
|
|
end
|
|
|
|
def self.update_order_reservation(id, sale_id, status)
|
|
order_reservation = OrderReservation.find(id)
|
|
if sale_id.present?
|
|
order_reservation.sale_id = sale_id
|
|
end
|
|
order_reservation.status = status
|
|
order_reservation.save
|
|
end
|
|
|
|
private
|
|
def generate_custom_id
|
|
self.order_reservation_id = SeedGenerator.generate_id(self.class.name, "ODRS")
|
|
end
|
|
end
|