Fix bugs and improvements
fix payment outstanding with amount <= 0 fix payment outstanding with other payments
This commit is contained in:
@@ -6,6 +6,8 @@ class Booking < ApplicationRecord
|
||||
|
||||
belongs_to :dining_facility, :optional => true
|
||||
belongs_to :sale, :optional => true
|
||||
has_one :cashier_terminal_by_dining_facility, through: :dining_facility, source: :cashier_terminal
|
||||
has_one :current_shift_by_dining_facility, through: :dining_facility, source: :current_shift
|
||||
has_many :booking_orders
|
||||
has_many :orders, :through => :booking_orders
|
||||
has_many :order_items, :through => :orders do
|
||||
@@ -13,7 +15,7 @@ class Booking < ApplicationRecord
|
||||
sale_items = []
|
||||
proxy_association.load_target.select(&:order_items_id).each do |order_item|
|
||||
menu_category = order_item.menu_category || OpenStruct.new(name: 'Product', code: '') #get menu category for menu items
|
||||
|
||||
|
||||
sale_items << SaleItem.new({
|
||||
menu_category_name: menu_category.name,
|
||||
menu_category_code: menu_category.code,
|
||||
|
||||
@@ -4,11 +4,11 @@ class DiningFacility < ApplicationRecord
|
||||
has_one :dining_charge
|
||||
has_one :cashier_terminal_by_zone, foreign_key: "zone_id", primary_key: "zone_id"
|
||||
has_one :cashier_terminal, through: :cashier_terminal_by_zone
|
||||
has_one :current_shift, through: :cashier_terminal
|
||||
has_many :order_queue_process_by_zones, foreign_key: "zone_id", primary_key: "zone_id"
|
||||
has_many :order_queue_stations, -> { where(is_active: true) }, through: :order_queue_process_by_zones
|
||||
|
||||
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"
|
||||
|
||||
@@ -3,6 +3,7 @@ class Employee < ApplicationRecord
|
||||
has_many :commissioners
|
||||
has_many :shit_sales
|
||||
has_one :current_shift, -> { where.not(shift_started_at: nil).where(shift_closed_at: nil) },class_name: "ShiftSale"
|
||||
has_one :cashier_terminal, through: :current_shift
|
||||
belongs_to :order_queue_station
|
||||
|
||||
validates_presence_of :name, :role
|
||||
|
||||
@@ -9,6 +9,8 @@ class Sale < ApplicationRecord
|
||||
belongs_to :cashier, foreign_key: "cashier_id", class_name: "Employee"
|
||||
belongs_to :customer, :optional => true
|
||||
belongs_to :shift_sale
|
||||
has_one :current_shift_by_cashier, through: :cashier, source: :current_shift
|
||||
has_one :cashier_terminal_by_shift_sale, through: :shift_sale, source: :cashier_terminal
|
||||
has_one :survey, foreign_key: "receipt_no"
|
||||
has_many :sale_audits
|
||||
has_many :sale_items
|
||||
@@ -16,8 +18,8 @@ class Sale < ApplicationRecord
|
||||
has_many :sale_discounts
|
||||
has_many :sale_taxes
|
||||
has_many :sale_payments
|
||||
has_many :payments_for_credits, through: :sale_audits
|
||||
has_many :sale_orders
|
||||
has_many :sale_payments_for_credits, through: :sale_audits
|
||||
has_many :orders, through: :sale_orders
|
||||
has_many :order_items, through: :sale_orders
|
||||
has_many :bookings
|
||||
@@ -32,7 +34,7 @@ class Sale < ApplicationRecord
|
||||
scope :paid, -> { where(payment_status: 'paid')}
|
||||
scope :completed, -> { where(sale_status: 'completed') }
|
||||
scope :receipt_date_between, -> (from, to) { where(receipt_date: from..to) }
|
||||
scope :along_with_sale_payments_except_void_between, -> (from, to) { joins(sanitize_sql_array(["LEFT JOIN sale_payments on sales.sale_status != 'void' AND sale_payments.sale_id = sales.sale_id AND sale_payments.created_at BETWEEN ? and ?", from, to])) }
|
||||
scope :along_with_sale_payments_except_void_between, -> (from, to) { joins(sanitize_sql_array(["LEFT JOIN sale_payments ON sales.sale_status != 'void' AND sale_payments.sale_id = sales.sale_id AND sale_payments.created_at BETWEEN ? and ?", from, to])) }
|
||||
|
||||
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
|
||||
@@ -96,12 +98,19 @@ class Sale < ApplicationRecord
|
||||
if cashier.role == 'cashier'
|
||||
sale.cashier = cashier
|
||||
sale.shift_sale = cashier.current_shift
|
||||
elsif booking.dining_facility_id
|
||||
sale.shift_sale = booking.dining_facility.cashier_terminal.current_shift
|
||||
sale.cashier = booking.dining_facility.cashier_terminal.current_shift.employee
|
||||
else
|
||||
sale.cashier = Employee.where(role: 'cashier').where.not(token_session: [nil, '']).first
|
||||
sale.shift_sale = sale.cashier.current_shift
|
||||
elsif booking.dining_facility
|
||||
if sale.shift_sale = booking.current_shift_by_dining_facility
|
||||
sale.cashier = sale.shift_sale.employee
|
||||
end
|
||||
end
|
||||
|
||||
if sale.shift_sale.nil?
|
||||
if sale.cashier = Employee.where(role: 'cashier').where.not(token_session: [nil, '']).first
|
||||
sale.shift_sale = sale.current_shift_by_cashier
|
||||
else
|
||||
sale.shift_sale = ShiftSale.current_shift
|
||||
sale.cashier = sale.shift_sale.employee
|
||||
end
|
||||
end
|
||||
|
||||
sale.cashier_name = sale.cashier.name
|
||||
@@ -110,7 +119,7 @@ class Sale < ApplicationRecord
|
||||
|
||||
sale.sale_items << booking.order_items.to_sale_items
|
||||
|
||||
if dining_charge = booking.dining_facility.dining_charge
|
||||
if booking.dining_facility.present? && dining_charge = booking.dining_facility.dining_charge
|
||||
block_count, dining_price = DiningCharge.amount_calculate(dining_charge, booking.checkin_at, booking.checkout_at)
|
||||
format = "%I:%M %p" if dining_charge.charge_type == "hr"
|
||||
format ||= "%B %d, %I:%M %p"
|
||||
@@ -173,14 +182,12 @@ class Sale < ApplicationRecord
|
||||
order_id = order.order_id # order_id
|
||||
booking = order.booking
|
||||
|
||||
sale_data = Sale.generate_invoice_from_booking(check_booking, current_login_employee, current_user, order.source)
|
||||
if booking.sale.nil?
|
||||
sale_data = Sale.generate_invoice_from_booking(booking, current_login_employee, current_user, order.source)
|
||||
# Promotion Activation
|
||||
Promotion.promo_activate(sale_data)
|
||||
end
|
||||
|
||||
# Bind shift sale id to sale
|
||||
# @sale_data.shift_sale_id = shift.id
|
||||
# @sale_data.save
|
||||
|
||||
# Promotion Activation
|
||||
Promotion.promo_activate(sale_data)
|
||||
return true, sale_data
|
||||
else
|
||||
return false, "No Current Open Shift for This Employee"
|
||||
@@ -1219,7 +1226,7 @@ def self.get_shift_sales_by_receipt_no_detail(shift_sale_range,shift,from,to,pay
|
||||
.includes(:bookings => :dining_facility)
|
||||
.select("sales.*, SUM(sale_payments.payment_amount) AS payments_for_credits_amount")
|
||||
.joins(:bookings)
|
||||
.left_joins(:sale_payments_for_credits)
|
||||
.left_joins(:payments_for_credits)
|
||||
.completed
|
||||
.where.not(total_amount: 0)
|
||||
.group(:sale_id)
|
||||
|
||||
@@ -66,7 +66,7 @@ class SalePayment < ApplicationRecord
|
||||
return self.save
|
||||
end
|
||||
|
||||
def process_payment(invoice, action_by, cash_amount, payment_method,remark=nil,payment_for=false)
|
||||
def process_payment(invoice, action_by, cash_amount, payment_method, remark=nil, payment_for=false)
|
||||
self.sale = invoice
|
||||
self.received_amount = cash_amount
|
||||
self.payment_reference = remark
|
||||
@@ -87,13 +87,14 @@ class SalePayment < ApplicationRecord
|
||||
amount_due = amount_due - payment.payment_amount
|
||||
end
|
||||
end
|
||||
|
||||
if (amount_due > 0)
|
||||
payment_status = false
|
||||
membership_data = nil
|
||||
#route to payment type
|
||||
case payment_method
|
||||
when "cash"
|
||||
payment_status ,outstanding_amount ,balance_amount = cash_payment(payment_for)
|
||||
payment_status, outstanding_amount, balance_amount = cash_payment(payment_for)
|
||||
when "creditnote"
|
||||
if !self.sale.customer_id.nil?
|
||||
payment_status = creditnote_payment(self.customer_id)
|
||||
@@ -157,7 +158,7 @@ class SalePayment < ApplicationRecord
|
||||
# update complete order items in oqs
|
||||
booking = Booking.find_by_sale_id(sale_id)
|
||||
booking.booking_orders.each do |sodr|
|
||||
assigned =AssignedOrderItem.where("order_id = '#{ sodr.order_id }'").pluck(:assigned_order_item_id)
|
||||
assigned = AssignedOrderItem.where("order_id = '#{ sodr.order_id }'").pluck(:assigned_order_item_id)
|
||||
AssignedOrderItem.where('assigned_order_item_id IN (?)', assigned).update_all(delivery_status: true)
|
||||
# AssignedOrderItem.where("order_id = '#{ sodr.order_id }'").find_each do |aoi|
|
||||
# aoi.delivery_status = 1
|
||||
@@ -165,7 +166,7 @@ class SalePayment < ApplicationRecord
|
||||
# end
|
||||
end
|
||||
|
||||
return true, self.save,membership_data, outstanding_amount ,balance_amount
|
||||
return true, self.save, membership_data, outstanding_amount, balance_amount
|
||||
else
|
||||
#record an payment in sale-audit
|
||||
remark = "Payment failed - Grand Total [#{invoice.grand_total}] | Due [#{amount_due}] | Paid [#{invoice.amount_received}]"
|
||||
@@ -173,6 +174,7 @@ class SalePayment < ApplicationRecord
|
||||
return false, "Payment failed"
|
||||
end
|
||||
else
|
||||
sale_update_payment_status(0)
|
||||
#record an payment in sale-audit
|
||||
remark = "No outstanding Amount - Grand Total [#{invoice.grand_total}] | Due [#{amount_due}] | Paid [#{invoice.amount_received}]"
|
||||
sale_audit = SaleAudit.record_payment(invoice.id, remark,action_by.name)
|
||||
@@ -640,85 +642,62 @@ class SalePayment < ApplicationRecord
|
||||
return payment_status
|
||||
end
|
||||
|
||||
def sale_update_payment_status(paid_amount,check_foc = false)
|
||||
def sale_update_payment_status(paid_amount, check_foc = false)
|
||||
#update amount_outstanding
|
||||
self.sale.amount_received = self.sale.amount_received.to_f + paid_amount.to_f
|
||||
self.sale.save!
|
||||
self.sale.amount_changed = self.sale.amount_received.to_f - self.sale.grand_total.to_f
|
||||
all_received_amount = 0.0
|
||||
sObj = Sale.find(self.sale_id)
|
||||
is_credit = 0
|
||||
is_foc = 0
|
||||
is_kbz_pay = 0
|
||||
method_status = false
|
||||
sObj.sale_payments.each do |spay|
|
||||
all_received_amount += spay.payment_amount.to_f
|
||||
if spay.payment_method == "creditnote"
|
||||
is_credit = 1
|
||||
end
|
||||
if spay.payment_method == "foc"
|
||||
is_foc = 1
|
||||
end
|
||||
if spay.payment_method == KbzPay::KBZ_PAY
|
||||
is_kbz_pay = 1
|
||||
end
|
||||
if spay.payment_method == "cash" || spay.payment_method == "foc" || spay.payment_method == "creditnote" || spay.payment_method == KbzPay::KBZ_PAY
|
||||
method_status = true
|
||||
end
|
||||
end
|
||||
if (self.sale.grand_total <= all_received_amount) && method_status
|
||||
if is_credit == 0
|
||||
self.sale.payment_status = "paid"
|
||||
sale = self.sale
|
||||
sale.amount_received = sale.amount_received.to_f + paid_amount.to_f
|
||||
sale.amount_changed = sale.amount_received.to_f - sale.grand_total.to_f
|
||||
|
||||
all_received_amount = sale.sale_payments.reload.sum(&:payment_amount)
|
||||
|
||||
is_credit = sale.sale_payments.any? { |x| x.payment_method == "creditnote" }
|
||||
is_foc = sale.sale_payments.any? { |x| x.payment_method == "foc" }
|
||||
|
||||
if (sale.grand_total <= all_received_amount)
|
||||
if is_credit
|
||||
sale.payment_status = "outstanding"
|
||||
elsif is_foc
|
||||
sale.payment_status = "foc"
|
||||
else
|
||||
self.sale.payment_status = "outstanding"
|
||||
sale.payment_status = "paid"
|
||||
end
|
||||
|
||||
if is_foc == 0
|
||||
self.sale.payment_status = "paid"
|
||||
else
|
||||
self.sale.payment_status = "foc"
|
||||
end
|
||||
|
||||
if is_kbz_pay == 1
|
||||
self.sale.payment_status = 'paid'
|
||||
end
|
||||
|
||||
self.sale.sale_status = "completed"
|
||||
sale.sale_status = "completed"
|
||||
|
||||
if MembershipSetting.find_by_rebate(1) && is_foc == 0 && is_credit == 0
|
||||
response = rebat(sObj)
|
||||
response = rebat(sale)
|
||||
|
||||
#record an payment in sale-audit
|
||||
remark = "#{response} Rebate- for Customer #{self.sale.customer_id} | Sale Id [#{self.sale.sale_id}]| pay amount -> #{self.received_amount} "
|
||||
sale_audit = SaleAudit.record_paymal(sObj.sale_id, remark, 1)
|
||||
remark = "#{response} Rebate- for Customer #{sale.customer_id} | Sale Id [#{sale.sale_id}]| pay amount -> #{self.received_amount} "
|
||||
sale_audit = SaleAudit.record_paymal(sale.sale_id, remark, 1)
|
||||
|
||||
if !response.nil?
|
||||
if response["status"] == true
|
||||
self.sale.rebate_status = 'true'
|
||||
sale.rebate_status = 'true'
|
||||
end
|
||||
if response["status"] == false
|
||||
self.sale.rebate_status = 'false'
|
||||
sale.rebate_status = 'false'
|
||||
end
|
||||
|
||||
if response[:status] == false
|
||||
self.sale.rebate_status = 'false'
|
||||
sale.rebate_status = 'false'
|
||||
end
|
||||
if response[:status] == "no_member"
|
||||
self.sale.rebate_status = nil
|
||||
sale.rebate_status = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
self.sale.save!
|
||||
sale.save!
|
||||
|
||||
if check_foc
|
||||
table_update_status(sObj)
|
||||
table_update_status(sale)
|
||||
update_shift
|
||||
elsif paid_amount.to_f > 0 #|| paid_amount != "0.0"
|
||||
table_update_status(sObj)
|
||||
table_update_status(sale)
|
||||
update_shift
|
||||
elsif method_status && paid_amount.to_f == 0 && is_credit == 0
|
||||
table_update_status(sObj)
|
||||
elsif paid_amount.to_f == 0 && !is_credit
|
||||
table_update_status(sale)
|
||||
update_shift
|
||||
end
|
||||
end
|
||||
@@ -778,32 +757,16 @@ class SalePayment < ApplicationRecord
|
||||
end
|
||||
|
||||
def table_update_status(sale_obj)
|
||||
status = true
|
||||
sale_count = 0
|
||||
if booking = sale_obj.bookings[0]
|
||||
if booking.dining_facility_id.to_i > 0
|
||||
puts "Update staus 1"
|
||||
if booking = sale_obj.booking
|
||||
puts "Update staus 2"
|
||||
if booking.dining_facility
|
||||
puts "Update staus 3"
|
||||
table = booking.dining_facility
|
||||
if Booking.left_joins(:sale).where(dining_facility_id: booking.dining_facility_id).where.not(booking_status: 'moved').where("sales.sale_status NOT IN ('completed', 'void', 'spoile', 'waste') OR sales.sale_status IS NULL").exists?
|
||||
status = false
|
||||
if !table.current_bookings.exists?
|
||||
puts "Update staus 3"
|
||||
table.update_attributes(status: "available")
|
||||
end
|
||||
|
||||
if status
|
||||
table.update_attributes(status: "available")
|
||||
# table.status = "available"
|
||||
# table.save
|
||||
end
|
||||
|
||||
# type = 'payment'
|
||||
#Send to background job for processing
|
||||
# OrderBroadcastJob.perform_later(table,type)
|
||||
#if ENV["SERVER_MODE"] != 'cloud'
|
||||
# if ENV["SERVER_MODE"] == 'cloud'
|
||||
# from = request.subdomain + "." + request.domain
|
||||
# else
|
||||
# from = ""
|
||||
# end
|
||||
# ActionCable.server.broadcast "order_channel",table: table,type:type,from:from
|
||||
#end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -19,7 +19,7 @@ class ShiftSale < ApplicationRecord
|
||||
|
||||
def self.current_shift
|
||||
# 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.not(shift_started_at: nil).where(shift_closed_at: nil).first
|
||||
return shift
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user