Skip to content

Commit

Permalink
invoice creation form
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegPhenomenon committed Nov 30, 2023
1 parent a12070f commit 1ca5444
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class CallbackHandlerController < ApplicationController
param :event_name, String, desc: event_descr

def callback

puts '============ CALLBACK'
puts params
puts '============ CALLBACK'

payment_reference = params[:payment_reference]
response = EverypayResponse.call(payment_reference)
result = Notify.call(response: response)
Expand Down
46 changes: 46 additions & 0 deletions app/controllers/invoice_creators_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class InvoiceCreatorsController < ParentController
def index
@invoices = Invoice.where(initiator: 'billing_system').order(created_at: :desc)
end

def new
invoice_number = InvoiceNumberService.call
@invoice = Invoice.new(
invoice_number:,
affiliation: :linkpay,
initiator: 'billing_system'
)
end

def create
@invoice = Invoice.new
@invoice.invoice_number = params[:invoice][:invoice_number]
@invoice.affiliation = params[:invoice][:affiliation]
@invoice.initiator = params[:invoice][:initiator]
@invoice.transaction_amount = params[:invoice][:transaction_amount]
@invoice.description = params[:invoice][:description]

ActiveRecord::Base.transaction do
@invoice.save!

linkpay_params_mutation = params[:linkpay].merge(
invoice_number: @invoice.invoice_number,
transaction_amount: @invoice.transaction_amount,
)

@linkpay = EverypayLinkGenerator.create(params: linkpay_params_mutation)
@invoice.everypay_response = {
linkpay: @linkpay,
}

@invoice.save!
end

redirect_to invoice_creators_path, notice: 'Invoice was successfully created.', status: :see_other
rescue ActiveRecord::RecordInvalid => e
flash.now[:error] = e.message
render :new and return
end

def show; end
end
54 changes: 29 additions & 25 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,35 @@ class Invoice < ApplicationRecord
REGISTRY = 'registry'.freeze
AUCTION = 'auction'.freeze
EEID = 'eeid'.freeze
BILLING_SYSTEM = 'billing_system'.freeze

pg_search_scope :search_by_number, against: [:invoice_number],
using: {
tsearch: {
prefix: true
}
}
using: {
tsearch: {
prefix: true
}
}

enum affiliation: %i[regular auction_deposit]
enum status: %i[unpaid paid cancelled failed refunded]
enum affiliation: { regular: 0, auction_deposit: 1, linkpay: 2 }
enum status: { unpaid: 0, paid: 1, cancelled: 2, failed: 3, refunded: 4 }

scope :with_status, ->(status) {
where(status: status) if status.present?
scope :with_status, lambda { |status|
where(status:) if status.present?
}

scope :with_number, ->(invoice_number) {
scope :with_number, lambda { |invoice_number|
search_by_number(invoice_number) if invoice_number.present?
}

scope :with_amount_between, ->(low, high) {
scope :with_amount_between, lambda { |low, high|
where(transaction_amount: low.to_f..high.to_f) if low.present? && high.present?
}

def self.search(params={})
sort_column = params[:sort].presence_in(%w{ invoice_number status affiliation }) || "id"
sort_direction = params[:direction].presence_in(%w{ asc desc }) || "desc"
def self.search(params = {})
sort_column = params[:sort].presence_in(%w[invoice_number status affiliation]) || 'id'
sort_direction = params[:direction].presence_in(%w[asc desc]) || 'desc'

self
.with_number(params[:invoice_number].to_s.downcase)
with_number(params[:invoice_number].to_s.downcase)
.with_status(params[:status])
.with_amount_between(params[:min_amount], params[:max_amount])
.order(sort_column => sort_direction)
Expand All @@ -57,17 +57,21 @@ def auction?
initiator == AUCTION
end

def billing_system?
initiator == BILLING_SYSTEM
end

def to_h
{
invoice_number: invoice_number,
initiator: initiator,
payment_reference: payment_reference,
transaction_amount: transaction_amount,
status: status,
in_directo: in_directo,
everypay_response: everypay_response,
transaction_time: transaction_time,
sent_at_omniva: sent_at_omniva
invoice_number:,
initiator:,
payment_reference:,
transaction_amount:,
status:,
in_directo:,
everypay_response:,
transaction_time:,
sent_at_omniva:
}
end
end
34 changes: 34 additions & 0 deletions app/views/invoice_creators/_invoice_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<h1 class='text-2xl font-bold text-center'>Invoice data</h1>

<div class="my-2">
<%= f.label :invoice_number, class: "text-sm font-bold" %><br>
<%= f.text_field :invoice_number, autofocus: true, required: true,
class: "w-full px-3 py-2 mt-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm bg-gray-200",
placeholder: "", readonly: true %>
</div>

<div class="my-2">
<%= f.label :affiliation, class: "text-sm font-bold" %><br>
<%= f.text_field :affiliation, autofocus: true, required: true,
class: "w-full px-3 py-2 mt-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm bg-gray-200",
placeholder: "", readonly: true %>
</div>

<div class="my-2">
<%= f.label :initiator, class: "text-sm font-bold" %><br>
<%= f.text_field :initiator, autofocus: true, required: true,
class: "w-full px-3 py-2 mt-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm bg-gray-200",
placeholder: "", readonly: true %>
</div>

<div class="my-2">
<%= f.label :transaction_amount, class: "text-sm font-bold" %><br>
<%= f.number_field :transaction_amount, step: '0.01', autofocus: true, required: true,
class: "w-full px-3 py-2 mt-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm",
placeholder: "" %>
</div>

<div class="my-2">
<%= f.label :description, class: "text-sm font-bold" %><br>
<%= f.text_area :description, class: "w-full px-3 py-2 mt-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm", rows: 10 %>
</div>
29 changes: 29 additions & 0 deletions app/views/invoice_creators/_linkpay_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<h1 class='text-2xl font-bold text-center'>Linkpay data</h1>

<div class="my-2">
<%= f.label :customer_name, class: "text-sm font-bold" %><br>
<%= f.text_field :customer_name, autofocus: true, required: true,
class: "w-full px-3 py-2 mt-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm",
placeholder: "" %>
</div>

<div class="my-2">
<%= f.label :customer_email, class: "text-sm font-bold" %><br>
<%= f.text_field :customer_email, autofocus: true, required: true,
class: "w-full px-3 py-2 mt-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm",
placeholder: "" %>
</div>

<div class="my-2">
<%= f.label :custom_field1, 'description', class: "text-sm font-bold" %><br>
<%= f.text_field :custom_field1, autofocus: true, required: true,
class: "w-full px-3 py-2 mt-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm",
placeholder: "" %>
</div>

<div class="my-2">
<%= f.label :custom_field2, class: "text-sm font-bold" %><br>
<%= f.text_field :custom_field2, autofocus: true, required: true,
class: "w-full px-3 py-2 mt-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm bg-gray-200",
placeholder: "" , value: 'billing_system', readonly: true %>
</div>
29 changes: 29 additions & 0 deletions app/views/invoice_creators/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<div class="container mx-auto">
<div class='flex flex-row'>
<%= link_to 'New Invoice', new_invoice_creator_path,
class: 'h-10 py-2 my-1 px-4 border border-transparent text-sm font-semibold rounded-md text-white bg-violet-700 hover:bg-violet-800 transition duration-150 ease-in-out shadow-md cursor-pointer' %>
</div>

<div>
<table class="min-w-full mt-6">
<thead>
<tr>
<th class="px-6 py-1 border-b border-gray-200 bg-gray-50 text-left text-xs text-gray-500 uppercase">Invoice number</th>
<th class="px-6 py-1 border-b border-gray-200 bg-gray-50 text-left text-xs text-gray-500 uppercase">Status</th>
<th class="px-6 py-1 border-b border-gray-200 bg-gray-50 text-left text-xs text-gray-500 uppercase">transaction_amount</th>
<th class="px-6 py-1 border-b border-gray-200 bg-gray-50 text-left text-xs text-gray-500 uppercase">Linkpay URL</th>
</tr>
</thead>
<tbody id="invoices">
<% @invoices.each do |invoice| %>
<tr>
<td class="px-6 py-1 border-b border-gray-200"><%= invoice.invoice_number %></td>
<td class="px-6 py-1 border-b border-gray-200"><%= invoice.status %></td>
<td class="px-6 py-1 border-b border-gray-200"><%= invoice.transaction_amount %></td>
<td class="px-6 py-1 border-b border-gray-200 break-all"><%= invoice.everypay_response["linkpay"] if invoice.everypay_response.present? %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
18 changes: 18 additions & 0 deletions app/views/invoice_creators/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<div class="container mx-auto">
<div class='ml-4 p-6 bg-white'>
<%= form_with url: invoice_creators_path do |form| %>

<%= form.fields_for @invoice do |f|%>
<%= render 'invoice_creators/invoice_form', f: f %>
<% end %>

<%= form.fields_for :linkpay do |f| %>
<%= render 'invoice_creators/linkpay_form', f: f %>
<% end %>

<div class="my-2 text-center">
<%= form.submit 'Create', class: "h-10 py-2 mt-4 px-4 border border-transparent text-sm font-semibold rounded-md text-white bg-violet-700 hover:bg-violet-800 transition duration-150 ease-in-out shadow-md cursor-pointer" %>
</div>
<% end %>
</div>
</div>
5 changes: 4 additions & 1 deletion app/views/shared/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@
<i class="fa-solid fa-money-bill"></i> <span class="ml-2">Get data from Everypay</span>
<% end %>

<%= link_to invoice_creators_path, class: 'px-6 py-1 flex flex-col md:flex-row md:items-center' do %>
<i class="fa-solid fa-money-bill-transfer"></i> <span class="ml-2">Invoice creator</span>
<% end %>

<%= link_to white_codes_path, class: 'px-6 py-1 flex flex-col md:flex-row md:items-center' do %>
<i class="fa-solid fa-users"></i> <span class="ml-2">White list</span>
<% end %>

<%= link_to logout_path, method: :destroy, data: { "turbo-method": 'delete' }, class: 'px-6 py-1 flex flex-col md:flex-row md:items-center' do %>
<i class="fa-solid fa-arrow-right-from-bracket"></i> <span class="ml-2">Logout</span>
<% end %>

<% end %>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/white_codes/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
<% end %>
</div>
</div>
</div>
</div>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
get 'sign_in', to: 'sessions#new'
delete 'logout', to: 'sessions#destroy'
resources :white_codes
resources :invoice_creators, only: %i[new create index show]

resources :dashboard do
collection do
Expand Down

0 comments on commit 1ca5444

Please sign in to comment.