payment api
This commit is contained in:
19
app/controllers/api/discounts_controller.rb
Normal file
19
app/controllers/api/discounts_controller.rb
Normal file
@@ -0,0 +1,19 @@
|
||||
class Api::DiscountsController < ActionController::API
|
||||
def create
|
||||
@invoice = Sale.find(params[:invoice_id])
|
||||
end
|
||||
|
||||
#Update sale item - Price | Qty |
|
||||
def update
|
||||
end
|
||||
|
||||
#destroy - Remove items form invoice
|
||||
def destroy
|
||||
@sale.remove_item(params[:sale_item_id])
|
||||
end
|
||||
|
||||
private
|
||||
def set_invoice_params
|
||||
@sale = Sale.find(params[:invoice_id])
|
||||
end
|
||||
end
|
||||
@@ -1,33 +1,38 @@
|
||||
class Api::CustomersController < ActionController::API
|
||||
class Api::InvoicesController < ActionController::API
|
||||
before :authenticate_token
|
||||
before :set_sale_params, only:[:show, :update, :destroy]
|
||||
|
||||
#List open invoices for today.
|
||||
def index
|
||||
@sales = Sale.open_invoices
|
||||
end
|
||||
#Description
|
||||
# This API show current order details
|
||||
# Input Params - order_id
|
||||
# This API show current sale details
|
||||
# Input Params - ID
|
||||
def show
|
||||
order = Order.find(params[:order_id])
|
||||
order.order_items
|
||||
@sale = Sale.find(params[:id])
|
||||
end
|
||||
|
||||
# Description
|
||||
# This API allow new invoice creation
|
||||
# Input Params
|
||||
# order_id
|
||||
# Output Params
|
||||
# Status [Success | Error | System Error] , order_id, error_message (*)
|
||||
#Creat Sales based on Items -
|
||||
def create
|
||||
#
|
||||
end
|
||||
|
||||
#UPDATE SALES
|
||||
def update
|
||||
|
||||
end
|
||||
|
||||
def by_booking
|
||||
end
|
||||
|
||||
def by_order
|
||||
#VOID Sale
|
||||
def destroy
|
||||
#Reason | #Approval
|
||||
end
|
||||
|
||||
private
|
||||
def process_items
|
||||
def set_sale_params
|
||||
@sale = Sale.find(params[:id])
|
||||
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
19
app/controllers/api/memberships_controller.rb
Normal file
19
app/controllers/api/memberships_controller.rb
Normal file
@@ -0,0 +1,19 @@
|
||||
class Api::MembershipsController < ActionController::API
|
||||
before :authenticate_token
|
||||
|
||||
|
||||
#Add Membership to invoice
|
||||
def create
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
private
|
||||
def set_sale_params
|
||||
|
||||
end
|
||||
def process_items
|
||||
end
|
||||
|
||||
end
|
||||
@@ -31,7 +31,7 @@ class Api::OrdersController < ActionController::API
|
||||
@order.guest = params[:guest_info]
|
||||
@order.table_id = params[:table_id]
|
||||
@order.new_booking = true
|
||||
@order.employee_name = "Test User"
|
||||
@order.employee_name = current_login_employee.name
|
||||
|
||||
|
||||
#Create Table Booking or Room Booking
|
||||
@@ -41,7 +41,7 @@ class Api::OrdersController < ActionController::API
|
||||
end
|
||||
|
||||
@status = @order.generate
|
||||
|
||||
|
||||
# rescue Exception => error
|
||||
# @status = false
|
||||
# @error_messages = "Exception has occurs on System"
|
||||
|
||||
@@ -8,11 +8,82 @@ class Api::PaymentsController < ActionController::API
|
||||
# Status - [True/False] | Invoice | error_message (* when status false)
|
||||
def create
|
||||
@invoice = Sale.find(params[:invoice_id])
|
||||
|
||||
if (@invoice)
|
||||
handle_payment(@invoice)
|
||||
end
|
||||
end
|
||||
|
||||
# Update of payment status from the external party
|
||||
# Invoice No | Payment ID | External params [] (* third party references and status)
|
||||
#
|
||||
def update
|
||||
|
||||
end
|
||||
|
||||
private
|
||||
def handle_payment(sale_payment)
|
||||
|
||||
payment_method = params[:payment_method]
|
||||
sale_payment = SalePayment.new
|
||||
|
||||
#:received_amount, :card_payment_reference, :vochure_no, :giftcard_no,
|
||||
#:customer_id, :external_payment_status
|
||||
switch (payment_method)
|
||||
case "cash"
|
||||
sale_payment.payment_method = "cash"
|
||||
sale_payment.received_amount = params[:amount]
|
||||
@status, @invoice = sale_payment.process_payment(sale_payment, current_login_employee.name)
|
||||
case "creditnote"
|
||||
sale_payment.payment_method = "creditnote"
|
||||
sale_payment.received_amount = params[:amount]
|
||||
sale_payment.customer_id = params[:customer_id]
|
||||
|
||||
@status, @invoice = sale_payment.process_payment(sale_payment, current_login_employee.name)
|
||||
case "visa"
|
||||
sale_payment.payment_method = "visa"
|
||||
sale_payment.received_amount = params[:amount]
|
||||
sale_payment.payment_reference = params[:payment_reference]
|
||||
|
||||
@status, @invoice = sale_payment.process_payment(sale_payment, current_login_employee.name)
|
||||
case "master"
|
||||
sale_payment.payment_method = "master"
|
||||
sale_payment.received_amount = params[:amount]
|
||||
sale_payment.payment_reference = params[:payment_reference]
|
||||
@status, @invoice = sale_payment.process_payment(sale_payment, current_login_employee.name)
|
||||
case "jcb"
|
||||
sale_payment.payment_method = "jcb"
|
||||
sale_payment.received_amount = params[:amount]
|
||||
sale_payment.payment_reference = params[:payment_reference]
|
||||
@status, @invoice = sale_payment.process_payment(sale_payment, current_login_employee.name)
|
||||
case "mpu"
|
||||
sale_payment.payment_method = "mpu"
|
||||
sale_payment.received_amount = params[:amount]
|
||||
sale_payment.payment_reference = params[:payment_reference]
|
||||
@status, @invoice = sale_payment.process_payment(sale_payment, current_login_employee.name)
|
||||
case "unionpay"
|
||||
sale_payment.payment_method = "unionpay"
|
||||
sale_payment.received_amount = params[:amount]
|
||||
sale_payment.payment_reference = params[:payment_reference]
|
||||
@status, @invoice = sale_payment.process_payment(sale_payment, current_login_employee.name)
|
||||
case "vochure"
|
||||
sale_payment.payment_method = "vochure"
|
||||
sale_payment.received_amount = params[:amount]
|
||||
sale_payment.customer_id = params[:customer_id]
|
||||
sale_payment.payment_reference = params[:vochure_no]
|
||||
@status, @invoice = sale_payment.process_payment(sale_payment, current_login_employee.name)
|
||||
case "giftcard"
|
||||
sale_payment.payment_method = "giftcard"
|
||||
sale_payment.received_amount = params[:amount]
|
||||
sale_payment.customer_id = params[:customer_id]
|
||||
sale_payment.payment_reference = params[:giftcard_no]
|
||||
@status, @invoice = sale_payment.process_payment(sale_payment, current_login_employee.name)
|
||||
case "paypar"
|
||||
sale_payment.payment_method = "paypar"
|
||||
sale_payment.received_amount = params[:amount]
|
||||
sale_payment.payment_reference = params[:payment_reference]
|
||||
#TODO: implement paypar implementation
|
||||
@status, @invoice = sale_payment.process_payment(sale_payment, current_login_employee.name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
21
app/controllers/api/sale_items_controller.rb
Normal file
21
app/controllers/api/sale_items_controller.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
class Api::SaleItemsController < ActionController::API
|
||||
|
||||
|
||||
def create
|
||||
@invoice = Sale.find(params[:invoice_id])
|
||||
end
|
||||
|
||||
#Update sale item - Price | Qty |
|
||||
def update
|
||||
end
|
||||
|
||||
#destroy - Remove items form invoice
|
||||
def destroy
|
||||
@sale.remove_item(params[:sale_item_id])
|
||||
end
|
||||
|
||||
private
|
||||
def set_invoice_params
|
||||
@sale = Sale.find(params[:invoice_id])
|
||||
end
|
||||
end
|
||||
74
app/controllers/settings/menus_controller.rb
Normal file
74
app/controllers/settings/menus_controller.rb
Normal file
@@ -0,0 +1,74 @@
|
||||
class Settings::MenusController < ApplicationController
|
||||
before_action :set_settings_menu, only: [:show, :edit, :update, :destroy]
|
||||
|
||||
# GET /settings/menus
|
||||
# GET /settings/menus.json
|
||||
def index
|
||||
@settings_menus = Settings::Menu.all
|
||||
end
|
||||
|
||||
# GET /settings/menus/1
|
||||
# GET /settings/menus/1.json
|
||||
def show
|
||||
end
|
||||
|
||||
# GET /settings/menus/new
|
||||
def new
|
||||
@settings_menu = Settings::Menu.new
|
||||
end
|
||||
|
||||
# GET /settings/menus/1/edit
|
||||
def edit
|
||||
end
|
||||
|
||||
# POST /settings/menus
|
||||
# POST /settings/menus.json
|
||||
def create
|
||||
@settings_menu = Settings::Menu.new(settings_menu_params)
|
||||
|
||||
respond_to do |format|
|
||||
if @settings_menu.save
|
||||
format.html { redirect_to @settings_menu, notice: 'Menu was successfully created.' }
|
||||
format.json { render :show, status: :created, location: @settings_menu }
|
||||
else
|
||||
format.html { render :new }
|
||||
format.json { render json: @settings_menu.errors, status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# PATCH/PUT /settings/menus/1
|
||||
# PATCH/PUT /settings/menus/1.json
|
||||
def update
|
||||
respond_to do |format|
|
||||
if @settings_menu.update(settings_menu_params)
|
||||
format.html { redirect_to @settings_menu, notice: 'Menu was successfully updated.' }
|
||||
format.json { render :show, status: :ok, location: @settings_menu }
|
||||
else
|
||||
format.html { render :edit }
|
||||
format.json { render json: @settings_menu.errors, status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# DELETE /settings/menus/1
|
||||
# DELETE /settings/menus/1.json
|
||||
def destroy
|
||||
@settings_menu.destroy
|
||||
respond_to do |format|
|
||||
format.html { redirect_to settings_menus_url, notice: 'Menu was successfully destroyed.' }
|
||||
format.json { head :no_content }
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
# Use callbacks to share common setup or constraints between actions.
|
||||
def set_settings_menu
|
||||
@settings_menu = Settings::Menu.find(params[:id])
|
||||
end
|
||||
|
||||
# Never trust parameters from the scary internet, only allow the white list through.
|
||||
def settings_menu_params
|
||||
params.fetch(:settings_menu, {})
|
||||
end
|
||||
end
|
||||
2
app/helpers/settings/menus_helper.rb
Normal file
2
app/helpers/settings/menus_helper.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
module Settings::MenusHelper
|
||||
end
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
10
app/views/settings/menus/_form.html.erb
Normal file
10
app/views/settings/menus/_form.html.erb
Normal file
@@ -0,0 +1,10 @@
|
||||
<%= simple_form_for(@settings_menu) do |f| %>
|
||||
<%= f.error_notification %>
|
||||
|
||||
<div class="form-inputs">
|
||||
</div>
|
||||
|
||||
<div class="form-actions">
|
||||
<%= f.button :submit %>
|
||||
</div>
|
||||
<% end %>
|
||||
2
app/views/settings/menus/_settings_menu.json.jbuilder
Normal file
2
app/views/settings/menus/_settings_menu.json.jbuilder
Normal file
@@ -0,0 +1,2 @@
|
||||
json.extract! settings_menu, :id, :created_at, :updated_at
|
||||
json.url settings_menu_url(settings_menu, format: :json)
|
||||
6
app/views/settings/menus/edit.html.erb
Normal file
6
app/views/settings/menus/edit.html.erb
Normal file
@@ -0,0 +1,6 @@
|
||||
<h1>Editing Settings Menu</h1>
|
||||
|
||||
<%= render 'form', settings_menu: @settings_menu %>
|
||||
|
||||
<%= link_to 'Show', @settings_menu %> |
|
||||
<%= link_to 'Back', settings_menus_path %>
|
||||
25
app/views/settings/menus/index.html.erb
Normal file
25
app/views/settings/menus/index.html.erb
Normal file
@@ -0,0 +1,25 @@
|
||||
<p id="notice"><%= notice %></p>
|
||||
|
||||
<h1>Settings Menus</h1>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th colspan="3"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
<% @settings_menus.each do |settings_menu| %>
|
||||
<tr>
|
||||
<td><%= link_to 'Show', settings_menu %></td>
|
||||
<td><%= link_to 'Edit', edit_settings_menu_path(settings_menu) %></td>
|
||||
<td><%= link_to 'Destroy', settings_menu, method: :delete, data: { confirm: 'Are you sure?' } %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<br>
|
||||
|
||||
<%= link_to 'New Settings Menu', new_settings_menu_path %>
|
||||
1
app/views/settings/menus/index.json.jbuilder
Normal file
1
app/views/settings/menus/index.json.jbuilder
Normal file
@@ -0,0 +1 @@
|
||||
json.array! @settings_menus, partial: 'settings_menus/settings_menu', as: :settings_menu
|
||||
5
app/views/settings/menus/new.html.erb
Normal file
5
app/views/settings/menus/new.html.erb
Normal file
@@ -0,0 +1,5 @@
|
||||
<h1>New Settings Menu</h1>
|
||||
|
||||
<%= render 'form', settings_menu: @settings_menu %>
|
||||
|
||||
<%= link_to 'Back', settings_menus_path %>
|
||||
4
app/views/settings/menus/show.html.erb
Normal file
4
app/views/settings/menus/show.html.erb
Normal file
@@ -0,0 +1,4 @@
|
||||
<p id="notice"><%= notice %></p>
|
||||
|
||||
<%= link_to 'Edit', edit_settings_menu_path(@settings_menu) %> |
|
||||
<%= link_to 'Back', settings_menus_path %>
|
||||
1
app/views/settings/menus/show.json.jbuilder
Normal file
1
app/views/settings/menus/show.json.jbuilder
Normal file
@@ -0,0 +1 @@
|
||||
json.partial! "settings_menus/settings_menu", settings_menu: @settings_menu
|
||||
Reference in New Issue
Block a user