payment api

This commit is contained in:
Min Zeya Phyo
2017-04-18 11:01:10 +06:30
parent 6a84a34a9f
commit dbad900cd4
30 changed files with 751 additions and 32 deletions

View File

@@ -9,6 +9,8 @@ class Sale < ApplicationRecord
has_many :sale_payments
has_many :sale_orders
scope :open_invoices, -> { where("sale_status = 'new' and receipt_date BETWEEN '#{DateTime.now.utc.end_of_day}' AND '#{DateTime.now.utc.beginning_of_day}'") }
def generate_invoice_from_booking(booking_id, requested_by)
booking = Booking.find(booking_id)
status = false
@@ -48,6 +50,7 @@ class Sale < ApplicationRecord
self.requested_by = requested_by
self.requested_at = DateTime.now.utc
self.customer_id = order.customer_id
order = Order.find(order_no)
Rails.logger.debug "Order -> #{order.id} | order_status -> #{order.status}"
@@ -90,7 +93,7 @@ class Sale < ApplicationRecord
#this will result in multiple orders belonging in multiple invoices - because of spilt invoices.
link_order_sale(item.order_id, taxable)
end
#Update item status as billed
order.update_items_status_to_billed(items)
@@ -134,10 +137,6 @@ class Sale < ApplicationRecord
#save action to sale_audit
end
def accept_payment (payment_method, amount, payment_ref, payment_external_result)
end
def void_sales (void_by, reason, approval_code, request_by)
#save sale_audit
self.sale_status = "void"
@@ -161,7 +160,9 @@ class Sale < ApplicationRecord
apply_tax (total_taxable)
self.total_amount = subtotal_price
self.total_discount = total_discount
self.grand_total = (self.total_amount - self.total_discount) + self.total_tax
#compute rounding adjustment
adjust_rounding
self.save!
@@ -218,11 +219,16 @@ class Sale < ApplicationRecord
#puts Time.now.format(":short")
end
def adjust_rounding
self.grand_total
self.rounding_adjustment = 0.00
end
#Generate new Receipt No when it is not assigned
def generate_receipt_no
#Date-Shift-
if self.receipt_no.nil?
prefix = DateTime.now()
prefix = DateTime.now().utc
#self.receipt_no = prefix.to_s + "/" + self.shit_id.to_s + "/" + SeedGenerator.new_receipt_no().to_s
self.receipt_no = prefix.strftime("%Y%m%d") + "/" + SeedGenerator.new_receipt_no().to_s

View File

@@ -38,4 +38,14 @@ class SaleAudit < ApplicationRecord
sale_audit.remark = reason
sale_audit.save!
end
def record_payment(sale_id, remark, action_by)
sale_audit = SaleAudit.new()
sale_audit.sale_id = sale_id
sale_audit.action = "SALEPAYMENT"
sale_audit.action_at = DateTime.now.utc
sale_audit.action_by = action_by
sale_audit.remark = remark
sale_audit.save!
end
end

View File

@@ -1,3 +1,157 @@
class SalePayment < ApplicationRecord
belongs_to :sale
:attr_accessor :received_amount, :card_payment_reference, :vochure_no, :giftcard_no, :customer_id, :external_payment_status
def process_payment(invoice, action_by)
self.sale = invoice
amount_due = invoice.grand_total
#get all payment for this invoices
invoice.sale_payments.each do |payment|
if (payment.payment_status == "paid" )
amount_due = amount_due - payment.payment_amount
end
end
if (amount_due > 0)
payment_status = false
#route to payment type
switch (payment_method)
case "cash"
payment_status = cash_payment
case "creditnote"
payment_status = creditnote_payment
case "visa"
payment_status = external_terminal_card_payment(:visa)
case "master"
payment_status = external_terminal_card_payment(:master)
case "jcb"
payment_status = external_terminal_card_payment(:jcb)
case "mpu"
payment_status = external_terminal_card_payment(:mpu)
case "unionpay"
payment_status = external_terminal_card_payment(:unionpay)
case "vochure"
payment_status = vochure_payment
case "giftcard"
payment_status = giftcard_payment
case "paypar"
#TODO: implement paypar implementation
payment_status = paypar_payment
end
#record an payment in sale-audit
remark = "Payment #{payment_method}- for Invoice #{invoice.receipt_no} Due [#{amount_due}]| pay amount -> #{amount} | Payment Status ->#{payment_status}"
sale_audit = SaleAudit.record_payment(invoice.id, remark, action_by)
return true, self.sale
else
#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)
return false, "No outstanding Amount"
end
end
private
def cash_payment
payment_status = false
self.payment_method = "cash"
self.payment_amount = self.received_amount
self.outstanding_amount = self.sale.grand_total - received_amount
self.payment_status = "paid"
payment_method = self.save!
sale_update_payment_status(self.received_amount)
return payment_status
end
def creditnote_payment(self.customer_id)
payment_status = false
self.payment_method = "creditnote"
self.payment_amount = self.received_amount
self.customer_id = self.customer_id
self.outstanding_amount = 0 - self.received_amount
self.payment_status = "outstanding"
payment_method = self.save!
sale_update_payment_status(self.received_amount)
return payment_status
end
def external_terminal_card_payment(method)
payment_status = false
self.payment_method = method
self.payment_amount = self.received_amount
self.payment_reference = self.card_payment_reference
self.outstanding_amount = self.sale.grand_total- self.received_amount
self.payment_status = "paid"
payment_method = self.save!
sale_update_payment_status(self.received_amount)
return payment_status
end
def vochure_payment
payment_status = false
#Next time - validate if the vochure number is valid - within
self.payment_method = "vochure"
self.payment_amount = self.received_amount
self.payment_reference = self.vochure_no
self.outstanding_amount = self.sale.grand_total- self.received_amount
self.payment_status = "paid"
payment_method = self.save!
sale_update_payment_status(self.received_amount)
return payment_status
end
def giftcard_payment
payment_status = false
#Next time - validate if the vochure number is valid - within
self.payment_method = "giftcard"
self.payment_amount = self.received_amount
self.payment_reference = self.giftcard_no
self.outstanding_amount = self.sale.grand_total- self.received_amount
self.payment_status = "paid"
payment_method = self.save!
sale_update_payment_status(self.received_amount)
return payment_status
end
def paypar_payment
##TODO - Integration with Paypar (SmartPay)
end
def sale_update_payment_status(paid_amount)
#update amount_outstanding
self.sale.amount_received = self.sale.amount_received + paid_amount
self.sale.amount_changed = amount - self.sale.amount_received
if (self.sale.grand_total <= self.sale.amount_received && self.sale.amount_changed > 0)
self.sale.payment_status = "paid"
self.sale.sale_status = "completed"
self.sale.save!
end
end
end