added filtering

This commit is contained in:
dinsmol 2021-08-25 22:23:58 +03:00
parent 70ca01ba4b
commit 5cb4085660
5 changed files with 121 additions and 3 deletions

View file

@ -33,10 +33,17 @@ module Admin
end
def index
@q = Invoice.includes(:account_activity).search(params[:q])
params[:q] ||= {}
invoices = filter_by_status
invoices = filter_by_receipt_date(invoices)
@q = invoices.search(params[:q])
@q.sorts = 'number desc' if @q.sorts.empty?
@invoices = @q.result.page(params[:page])
@invoices = @invoices.per(params[:results_per_page]) if paginate?
render_by_format('admin/invoices/index', 'invoices')
end
def show; end
@ -72,5 +79,29 @@ module Admin
payment_order = invoice.payment_orders.last
payment_order.update(notes: 'Cancelled')
end
def filter_by_status
case params[:status]
when 'Paid'
Invoice.includes(:account_activity, :buyer).where.not(account_activity: { id: nil })
when 'Unpaid'
Invoice.includes(:account_activity, :buyer).where(account_activity: { id: nil })
when 'Cancelled'
Invoice.includes(:account_activity, :buyer).where.not(cancelled_at: nil)
else
Invoice.includes(:account_activity, :buyer)
end
end
def filter_by_receipt_date(invoices)
if params[:q][:receipt_date_gteq].present?
invoices = invoices.where(account_activity:
{ created_at: Time.parse(params[:q][:receipt_date_gteq])..Float::INFINITY })
end
return invoices unless params[:q][:receipt_date_lteq].present?
invoices.where(account_activity: { created_at: -Float::INFINITY..Time.parse(params[:q][:receipt_date_lteq]) })
end
end
end

View file

@ -3,6 +3,7 @@ class Invoice < ApplicationRecord
include Invoice::Cancellable
include Invoice::Payable
include Invoice::BookKeeping
extend ToCsv
belongs_to :buyer, class_name: 'Registrar'
has_one :account_activity

View file

@ -0,0 +1,81 @@
<div class="row">
<div class="col-md-12">
<%= search_form_for @q, url: [:admin, :invoices], html: { style: 'margin-bottom: 0;' } do |f| %>
<div class="row">
<div class="col-md-3">
<div class="form-group">
<%= f.label t(:invoice_number) %>
<%= f.search_field :number_eq, class: 'form-control', placeholder: t(:invoice_number), autocomplete: 'off' %>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<%= f.label t(:status) %>
<%= select_tag :status, options_for_select(%w(Paid Unpaid Cancelled),params[:status]),
{ multiple: false, include_blank: true, selected: params[:status], class: 'form-control selectize'} %>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<%= f.label t(:due_date_from), for: nil %>
<%= f.search_field :due_date_gteq, value: params[:q][:due_date_gteq], class: 'form-control js-datepicker', placeholder: t(:due_date_from) %>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<%= f.label t(:due_date_until), for: nil %>
<%= f.search_field :due_date_lteq, value: params[:q][:due_date_lteq], class: 'form-control js-datepicker', placeholder: t(:due_date_until) %>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<%= f.label "Registrar" %>
<%= f.select :buyer_id_in, Registrar.all.map { |x| [x, x.id] }, {}, class: 'form-control js-combobox', placeholder: t(:choose), multiple: true %>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<%= f.label t(:receipt_date_from), for: nil %>
<%= f.search_field :receipt_date_gteq, value: params[:q][:receipt_date_gteq], class: 'form-control js-datepicker', placeholder: t(:receipt_date_from) %>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<%= f.label t(:receipt_date_until), for: nil %>
<%= f.search_field :receipt_date_lteq, value: params[:q][:receipt_date_lteq], class: 'form-control js-datepicker', placeholder: t(:receipt_date_until) %>
</div>
</div>
<div class="col-md-3"></div>
<div class="col-md-3"></div>
<div class="col-md-3">
<div class="form-group">
<%= label_tag t(:results_per_page) %>
<%= text_field_tag :results_per_page, params[:results_per_page], class: 'form-control', placeholder: t(:results_per_page) %>
</div>
</div>
<div class="col-md-3" style="padding-top: 25px; float: right; padding-right: 10px;">
<button class="btn btn-primary">
&nbsp;
<span class="glyphicon glyphicon-search"></span>
&nbsp;
</button>
<%= link_to t('.download_btn'), admin_invoices_path(format: :csv, params: params.permit!),
"data-toggle" => "tooltip", "data-placement" => "bottom", "title" => t('.download_btn'),
class: 'btn btn-default' %>
<%= link_to t('.reset_btn'), admin_invoices_path, class: 'btn btn-default' %>
</div>
</div>
<% end %>
</div>
</div>

View file

@ -1,7 +1,7 @@
- content_for :actions do
= link_to(t(:add), new_admin_invoice_path, class: 'btn btn-primary')
= render 'shared/title', name: t(:invoices)
= render 'application/pagination'
= render 'search_form'
.row
.col-md-12
@ -33,6 +33,7 @@
%td.text-grey= t(:cancelled)
- else
%td.text-danger= t(:unpaid)
.row
.col-md-6
= paginate @invoices

View file

@ -9,4 +9,8 @@ en:
deliver_btn: Send
cancel:
cancelled: Invoice has been cancelled
cancelled: Invoice has been cancelled
search_form:
download_btn: CSV
reset_btn: Reset