mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-06 22:36:07 +00:00
update the logic to send invoice
When the invoices feature is enabled, for every order, we check if 1. a new invoice must be generated 2. or, the latest invoice needs to be updated the invoice rendrer input depends on the invoices flag. if the feature is enabled, the input is supposed to be an invoice presenter
This commit is contained in:
@@ -48,7 +48,14 @@ module Spree
|
||||
|
||||
def invoice_email(order_or_order_id)
|
||||
@order = find_order(order_or_order_id)
|
||||
pdf = InvoiceRenderer.new.render_to_string(@order)
|
||||
renderer_data = if OpenFoodNetwork::FeatureToggle.enabled?(:invoices)
|
||||
OrderInvoiceGenerator.new(@order).generate_or_update_latest_invoice
|
||||
@order.invoices.first.presenter
|
||||
else
|
||||
@order
|
||||
end
|
||||
|
||||
pdf = InvoiceRenderer.new.render_to_string(renderer_data)
|
||||
|
||||
attach_file("invoice-#{@order.number}.pdf", pdf)
|
||||
I18n.with_locale valid_locale(@order.user) do
|
||||
|
||||
@@ -216,4 +216,41 @@ describe Spree::OrderMailer do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#invoice_email" do
|
||||
subject(:email) { described_class.invoice_email(order) }
|
||||
let(:order) { create(:completed_order_with_totals) }
|
||||
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) }
|
||||
before do
|
||||
allow(OrderInvoiceGenerator).to receive(:new).with(order).and_return(generator)
|
||||
allow(InvoiceRenderer).to receive(:new).and_return(renderer)
|
||||
end
|
||||
context "When invoices feature is not enabled" do
|
||||
it "should call the invoice render with order as argument" do
|
||||
expect(generator).not_to receive(:generate_or_update_latest_invoice)
|
||||
expect(order).not_to receive(:invoices)
|
||||
expect(renderer).to receive(:render_to_string).with(order)
|
||||
email.deliver_now
|
||||
end
|
||||
end
|
||||
|
||||
context "When invoices feature is enabled" do
|
||||
before do
|
||||
Flipper.enable(:invoices)
|
||||
end
|
||||
it "should call the invoice renderer with invoice's presenter as argument" do
|
||||
expect(generator).to receive(:generate_or_update_latest_invoice)
|
||||
expect(order).to receive(:invoices).and_return([invoice])
|
||||
expect(renderer).to receive(:render_to_string).with(invoice.presenter)
|
||||
email.deliver_now
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user