From 5cb4085660ba6aabe021d0648af215463c7e2205 Mon Sep 17 00:00:00 2001 From: dinsmol Date: Wed, 25 Aug 2021 22:23:58 +0300 Subject: [PATCH 1/9] added filtering --- app/controllers/admin/invoices_controller.rb | 33 +++++++- app/models/invoice.rb | 1 + .../admin/invoices/_search_form.html.erb | 81 +++++++++++++++++++ app/views/admin/invoices/index.haml | 3 +- config/locales/admin/invoices.en.yml | 6 +- 5 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 app/views/admin/invoices/_search_form.html.erb diff --git a/app/controllers/admin/invoices_controller.rb b/app/controllers/admin/invoices_controller.rb index 35b650c84..9e0c83efe 100644 --- a/app/controllers/admin/invoices_controller.rb +++ b/app/controllers/admin/invoices_controller.rb @@ -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 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..619ae9316 --- /dev/null +++ b/app/views/admin/invoices/_search_form.html.erb @@ -0,0 +1,81 @@ +
+
+ <%= 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 From bfa2fcc98ce6fcdc49f10712400caa41ed317d79 Mon Sep 17 00:00:00 2001 From: dinsmol Date: Thu, 26 Aug 2021 22:24:48 +0300 Subject: [PATCH 2/9] minor fixes --- app/views/admin/invoices/_search_form.html.erb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/views/admin/invoices/_search_form.html.erb b/app/views/admin/invoices/_search_form.html.erb index 619ae9316..7739387b2 100644 --- a/app/views/admin/invoices/_search_form.html.erb +++ b/app/views/admin/invoices/_search_form.html.erb @@ -78,4 +78,3 @@ <% end %> - From 24d247f4da560615a8f7f523d054130d7573f1c8 Mon Sep 17 00:00:00 2001 From: dinsmol Date: Thu, 26 Aug 2021 22:33:30 +0300 Subject: [PATCH 3/9] fixed codeclimate errors --- app/controllers/admin/invoices_controller.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/app/controllers/admin/invoices_controller.rb b/app/controllers/admin/invoices_controller.rb index 9e0c83efe..8f05521d8 100644 --- a/app/controllers/admin/invoices_controller.rb +++ b/app/controllers/admin/invoices_controller.rb @@ -33,8 +33,6 @@ module Admin end def index - params[:q] ||= {} - invoices = filter_by_status invoices = filter_by_receipt_date(invoices) @@ -95,13 +93,14 @@ module Admin 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 }) + date_from = Time.zone.parse(params[:q][:receipt_date_gteq]) + invoices = invoices.where(account_activity: { created_at: date_from..Float::INFINITY }) end - return invoices unless params[:q][:receipt_date_lteq].present? + return invoices if params[:q][:receipt_date_lteq].blank? - invoices.where(account_activity: { created_at: -Float::INFINITY..Time.parse(params[:q][:receipt_date_lteq]) }) + date_until = Time.zone.parse(params[:q][:receipt_date_lteq]) + invoices.where(account_activity: { created_at: -Float::INFINITY..date_until }) end end end From 867fbb02a876924163b7693b311babe303154d3b Mon Sep 17 00:00:00 2001 From: dinsmol Date: Thu, 26 Aug 2021 23:29:48 +0300 Subject: [PATCH 4/9] fixed codeclimate errors --- app/controllers/admin/invoices_controller.rb | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/app/controllers/admin/invoices_controller.rb b/app/controllers/admin/invoices_controller.rb index 8f05521d8..131f6ffdc 100644 --- a/app/controllers/admin/invoices_controller.rb +++ b/app/controllers/admin/invoices_controller.rb @@ -40,7 +40,6 @@ module Admin @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 @@ -84,10 +83,8 @@ module Admin 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) + when 'Cancelled' then Invoice.includes(:account_activity, :buyer).where.not(cancelled_at: nil) + else Invoice.includes(:account_activity, :buyer) end end From 9dffb1fd3124dc4cd6fc27e0c8b94a49d0ce78ab Mon Sep 17 00:00:00 2001 From: dinsmol Date: Fri, 27 Aug 2021 00:30:56 +0300 Subject: [PATCH 5/9] fixed codeclimate errors --- app/controllers/admin/invoices_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/admin/invoices_controller.rb b/app/controllers/admin/invoices_controller.rb index 131f6ffdc..4fa5d26d2 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) @@ -40,6 +39,7 @@ module Admin @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 From dce074e326e38ff37599a0d20d99f6f59f8cd12a Mon Sep 17 00:00:00 2001 From: dinsmol Date: Sat, 28 Aug 2021 00:29:01 +0300 Subject: [PATCH 6/9] fixed undefined method error --- app/controllers/admin/invoices_controller.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/controllers/admin/invoices_controller.rb b/app/controllers/admin/invoices_controller.rb index 4fa5d26d2..82dd5e88c 100644 --- a/app/controllers/admin/invoices_controller.rb +++ b/app/controllers/admin/invoices_controller.rb @@ -32,6 +32,7 @@ module Admin end def index + params[:q] ||= {} invoices = filter_by_status invoices = filter_by_receipt_date(invoices) From 4770411cdad751dccc1e52869bb475f3e2981c80 Mon Sep 17 00:00:00 2001 From: dinsmol Date: Sat, 28 Aug 2021 00:41:11 +0300 Subject: [PATCH 7/9] refactored --- app/controllers/admin/invoices_controller.rb | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/app/controllers/admin/invoices_controller.rb b/app/controllers/admin/invoices_controller.rb index 82dd5e88c..a6072e17c 100644 --- a/app/controllers/admin/invoices_controller.rb +++ b/app/controllers/admin/invoices_controller.rb @@ -84,21 +84,17 @@ module Admin 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' then Invoice.includes(:account_activity, :buyer).where.not(cancelled_at: nil) - else Invoice.includes(:account_activity, :buyer) + 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? - date_from = Time.zone.parse(params[:q][:receipt_date_gteq]) - invoices = invoices.where(account_activity: { created_at: date_from..Float::INFINITY }) - end - - return invoices if params[:q][:receipt_date_lteq].blank? - - date_until = Time.zone.parse(params[:q][:receipt_date_lteq]) - invoices.where(account_activity: { created_at: -Float::INFINITY..date_until }) + date_from = Time.zone.parse(params[:q][:receipt_date_gteq]) || -Float::INFINITY + date_until = Time.zone.parse(params[:q][:receipt_date_lteq]) || Float::INFINITY + invoices.where(account_activity: { created_at: date_from..date_until }) end end end From e9750aee313fabf4b7d50b443ba8620ec7b0ced4 Mon Sep 17 00:00:00 2001 From: dinsmol Date: Sun, 29 Aug 2021 23:15:07 +0300 Subject: [PATCH 8/9] fixed test error --- app/controllers/admin/invoices_controller.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/controllers/admin/invoices_controller.rb b/app/controllers/admin/invoices_controller.rb index a6072e17c..283f60ce3 100644 --- a/app/controllers/admin/invoices_controller.rb +++ b/app/controllers/admin/invoices_controller.rb @@ -92,8 +92,16 @@ module Admin end def filter_by_receipt_date(invoices) - date_from = Time.zone.parse(params[:q][:receipt_date_gteq]) || -Float::INFINITY - date_until = Time.zone.parse(params[:q][:receipt_date_lteq]) || Float::INFINITY + date_from = if params[:q][:receipt_date_gteq].present? + Time.zone.parse(params[:q][:receipt_date_gteq]) + else + -Float::INFINITY + end + date_until = if params[:q][:receipt_date_lteq].present? + Time.zone.parse(params[:q][:receipt_date_lteq]) + else + Float::INFINITY + end invoices.where(account_activity: { created_at: date_from..date_until }) end end From 5ca81115edff1feea186fffd61b02eea78b7c2ff Mon Sep 17 00:00:00 2001 From: dinsmol Date: Mon, 30 Aug 2021 08:40:08 +0300 Subject: [PATCH 9/9] fixed codeclimate error --- app/controllers/admin/invoices_controller.rb | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/app/controllers/admin/invoices_controller.rb b/app/controllers/admin/invoices_controller.rb index 283f60ce3..0e154dff0 100644 --- a/app/controllers/admin/invoices_controller.rb +++ b/app/controllers/admin/invoices_controller.rb @@ -92,16 +92,12 @@ module Admin end def filter_by_receipt_date(invoices) - date_from = if params[:q][:receipt_date_gteq].present? - Time.zone.parse(params[:q][:receipt_date_gteq]) - else - -Float::INFINITY - end - date_until = if params[:q][:receipt_date_lteq].present? - Time.zone.parse(params[:q][:receipt_date_lteq]) - else - Float::INFINITY - end + 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