update the bulk invoice job to print invoiceable orders

the invoices feature is enabled:
1. we filter non-invoiceable order
2. for each invoiceable order, we check if we need to generate a new invoice or update the latest invoice
This commit is contained in:
Mohamed ABDELLANI
2023-07-12 09:48:26 +01:00
parent c921250d68
commit 687760046b
2 changed files with 71 additions and 9 deletions

View File

@@ -5,7 +5,9 @@ class BulkInvoiceJob < ApplicationJob
delegate :render, to: ActionController::Base
def perform(order_ids, filepath, options = {})
sorted_orders(order_ids).each(&method(:generate_invoice))
orders = sorted_orders(order_ids)
orders = orders.filter(&:invoiceable?) if OpenFoodNetwork::FeatureToggle.enabled?(:invoices)
orders.each(&method(:generate_invoice))
ensure_directory_exists filepath
@@ -26,8 +28,14 @@ class BulkInvoiceJob < ApplicationJob
@renderer ||= InvoiceRenderer.new
end
def generate_invoice order
invoice = renderer.render_to_string(order)
def generate_invoice(order)
renderer_data = if OpenFoodNetwork::FeatureToggle.enabled?(:invoices)
OrderInvoiceGenerator.new(order).generate_or_update_latest_invoice
order.invoices.first.presenter
else
order
end
invoice = renderer.render_to_string(renderer_data)
pdf << CombinePDF.parse(invoice)
end

View File

@@ -3,19 +3,73 @@
require 'spec_helper'
describe BulkInvoiceJob do
let(:order1) { build(:order, id: 1) }
let(:order2) { build(:order, id: 2) }
let(:order3) { build(:order, id: 3) }
let(:order_ids) { [3, 1, 2] }
subject { BulkInvoiceJob.new(order_ids, "/tmp/file/path") }
describe "#sorted_orders" do
let(:order1) { build(:order, id: 1) }
let(:order2) { build(:order, id: 2) }
let(:order3) { build(:order, id: 3) }
let(:order_ids) { [3, 1, 2] }
it "returns results in their original order" do
expect(Spree::Order).to receive(:where).and_return([order1, order2, order3])
expect(subject.__send__(:sorted_orders, order_ids)).to eq [order3, order1, order2]
end
end
context "when invoices are enabled" do
before do
Flipper.enable(:invoices)
end
describe "#perform" do
let(:order1) { create(:order) }
let(:order2) { create(:order) }
let(:order3) { create(:order) }
let(:order_ids) { [order1.id, order2.id, order3.id] }
it "should generate invoices for invoiceable orders only" do
expect(subject).to receive(:sorted_orders).with(order_ids).and_return([order1, order2,
order3])
expect(order1).to receive(:invoiceable?).and_return(true)
expect(order2).to receive(:invoiceable?).and_return(false)
expect(order3).to receive(:invoiceable?).and_return(true)
[order1, order3].each do |order|
expect(subject).to receive(:generate_invoice).with(order)
end
expect(subject).not_to receive(:generate_invoice).with(order2)
subject.perform(order_ids, "/tmp/file/path")
end
end
describe "#generate_invoice" do
let(:order) { create(:completed_order_with_totals) }
let(:order_ids){ [order.id] }
let!(:invoice_data_generator){ InvoiceDataGenerator.new(order) }
let!(:invoice){
create(:invoice, order:,
data: invoice_data_generator.serialize_for_invoice)
}
let(:generator){ double("generator") }
let(:renderer){ double("renderer") }
let(:printed_invoice_string){ "printed invoice string" }
before do
expect(OrderInvoiceGenerator).to receive(:new).with(order).and_return(generator)
expect(subject).to receive(:renderer).and_return(renderer)
end
it "should call the renderer with the invoice presenter" do
expect(generator).to receive(:generate_or_update_latest_invoice)
expect(renderer).to receive(:render_to_string).with(invoice.presenter)
.and_return(printed_invoice_string)
expect(order).to receive(:invoices).and_return([invoice])
expect(CombinePDF).to receive(:parse).with(printed_invoice_string)
subject.__send__(:generate_invoice, order)
end
end
end
end