diff --git a/app/controllers/admin/invoices_controller.rb b/app/controllers/admin/invoices_controller.rb index 35b650c84..0e154dff0 100644 --- a/app/controllers/admin/invoices_controller.rb +++ b/app/controllers/admin/invoices_controller.rb @@ -21,8 +21,7 @@ module Admin end def cancel_paid - invoice_id = params[:invoice_id] - invoice = Invoice.find(invoice_id) + invoice = Invoice.find(params[:invoice_id]) if account_activity_with_negative_sum(invoice) flash[:notice] = t(:payment_was_cancelled) @@ -33,10 +32,16 @@ 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 +77,28 @@ 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) + date_from_param = params[:q][:receipt_date_gteq] if params[:q][:receipt_date_gteq].present? + date_from = date_from_param ? Time.zone.parse(date_from_param) : -Float::INFINITY + + date_until_param = params[:q][:receipt_date_lteq] if params[:q][:receipt_date_lteq].present? + date_until = date_until_param ? Time.zone.parse(date_from_param) : Float::INFINITY + + invoices.where(account_activity: { created_at: date_from..date_until }) + end end end diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 3170687c9..bb82d0fcb 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -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 diff --git a/app/views/admin/invoices/_search_form.html.erb b/app/views/admin/invoices/_search_form.html.erb new file mode 100644 index 000000000..7739387b2 --- /dev/null +++ b/app/views/admin/invoices/_search_form.html.erb @@ -0,0 +1,80 @@ +
+
+ <%= search_form_for @q, url: [:admin, :invoices], html: { style: 'margin-bottom: 0;' } do |f| %> +
+
+
+ <%= f.label t(:invoice_number) %> + <%= f.search_field :number_eq, class: 'form-control', placeholder: t(:invoice_number), autocomplete: 'off' %> +
+
+ +
+
+ <%= 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'} %> +
+
+ +
+
+ <%= 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) %> +
+
+ +
+
+ <%= 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) %> +
+
+
+ +
+
+
+ <%= 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 %> +
+
+ +
+
+ <%= 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) %> +
+
+ +
+
+ <%= 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) %> +
+
+ +
+
+
+
+ <%= label_tag t(:results_per_page) %> + <%= text_field_tag :results_per_page, params[:results_per_page], class: 'form-control', placeholder: t(:results_per_page) %> +
+
+ +
+ + <%= 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' %> +
+
+ <% end %> +
+
diff --git a/app/views/admin/invoices/index.haml b/app/views/admin/invoices/index.haml index ede0b5c3c..046772d68 100644 --- a/app/views/admin/invoices/index.haml +++ b/app/views/admin/invoices/index.haml @@ -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 diff --git a/config/locales/admin/invoices.en.yml b/config/locales/admin/invoices.en.yml index 6ec73ca9c..65333635f 100644 --- a/config/locales/admin/invoices.en.yml +++ b/config/locales/admin/invoices.en.yml @@ -9,4 +9,8 @@ en: deliver_btn: Send cancel: - cancelled: Invoice has been cancelled \ No newline at end of file + cancelled: Invoice has been cancelled + + search_form: + download_btn: CSV + reset_btn: Reset