Merge pull request #5353 from pacodelaluna/sort-invoices-in-bulk-invoice-with-forced-order

Sort invoices in bulk invoice with completed_at desc order
This commit is contained in:
Pau Pérez Fabregat
2020-06-11 15:34:39 +02:00
committed by GitHub
2 changed files with 26 additions and 2 deletions

View File

@@ -7,9 +7,8 @@ class BulkInvoiceService
def start_pdf_job(order_ids)
pdf = CombinePDF.new
orders = Spree::Order.where(id: order_ids)
orders.each do |order|
orders_from(order_ids).each do |order|
invoice = renderer.render_to_string(order)
pdf << CombinePDF.parse(invoice)
@@ -29,6 +28,10 @@ class BulkInvoiceService
private
def orders_from(order_ids)
Spree::Order.where(id: order_ids).order("completed_at DESC")
end
def new_invoice_id
Time.zone.now.to_i.to_s
end

View File

@@ -47,4 +47,25 @@ describe BulkInvoiceService do
expect(filepath).to eq 'tmp/invoices/1234567.pdf'
end
end
describe "#orders_from" do
let(:renderer) { InvoiceRenderer.new }
before do
allow(InvoiceRenderer).to receive(:new).and_return(renderer)
end
it "orders with completed desc" do
order_old = create(:order_with_distributor, :completed, completed_at: 2.minutes.ago)
order_oldest = create(:order_with_distributor, :completed, completed_at: 4.minutes.ago)
order_older = create(:order_with_distributor, :completed, completed_at: 3.minutes.ago)
expect(renderer).to receive(:render_to_string).with(order_old).ordered.and_return("")
expect(renderer).to receive(:render_to_string).with(order_older).ordered.and_return("")
expect(renderer).to receive(:render_to_string).with(order_oldest).ordered.and_return("")
order_ids = [order_oldest, order_old, order_older].map(&:id)
service.start_pdf_job_without_delay(order_ids)
end
end
end