Fix bugs and improvements

fix payment outstanding with amount <= 0
fix payment outstanding with other payments
This commit is contained in:
Thein Lin Kyaw
2019-12-18 15:41:18 +06:30
parent e6392c3bd4
commit 92e467d512
11 changed files with 122 additions and 217 deletions

View File

@@ -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