diff --git a/app/serializers/v1/invoice_serializer.rb b/app/serializers/v1/invoice_serializer.rb index fbb1aff7d14..f2f26044180 100644 --- a/app/serializers/v1/invoice_serializer.rb +++ b/app/serializers/v1/invoice_serializer.rb @@ -27,6 +27,7 @@ def serialize prepaid_credit_amount_cents: model.prepaid_credit_amount_cents, file_url: model.file_url, version_number: model.version_number, + self_billed: model.self_billed, created_at: model.created_at.iso8601, updated_at: model.updated_at.iso8601 } diff --git a/app/services/invoices/create_generating_service.rb b/app/services/invoices/create_generating_service.rb index b01acb28871..94015a79b82 100644 --- a/app/services/invoices/create_generating_service.rb +++ b/app/services/invoices/create_generating_service.rb @@ -27,7 +27,8 @@ def call issuing_date:, payment_due_date:, net_payment_term: customer.applicable_net_payment_term, - skip_charges: + skip_charges:, + self_billed: customer.partner_account? ) result.invoice = invoice diff --git a/app/services/invoices/payments/create_service.rb b/app/services/invoices/payments/create_service.rb index 8b31a1e9fef..ffcdad30d2d 100644 --- a/app/services/invoices/payments/create_service.rb +++ b/app/services/invoices/payments/create_service.rb @@ -97,6 +97,7 @@ def provider end def should_process_payment? + return false if invoice.self_billed? return false if invoice.payment_succeeded? || invoice.voided? return false if current_payment_provider.blank? diff --git a/spec/factories/invoices.rb b/spec/factories/invoices.rb index 717d6b2c934..323a1913b70 100644 --- a/spec/factories/invoices.rb +++ b/spec/factories/invoices.rb @@ -56,5 +56,9 @@ end end end + + trait :self_billed do + self_billed { true } + end end end diff --git a/spec/serializers/v1/invoice_serializer_spec.rb b/spec/serializers/v1/invoice_serializer_spec.rb index 07d5c00f954..09765b3951b 100644 --- a/spec/serializers/v1/invoice_serializer_spec.rb +++ b/spec/serializers/v1/invoice_serializer_spec.rb @@ -53,6 +53,7 @@ } ], "version_number" => 4, + "self_billed" => invoice.self_billed, "created_at" => invoice.created_at.iso8601, "updated_at" => invoice.updated_at.iso8601 ) diff --git a/spec/services/invoices/create_generating_service_spec.rb b/spec/services/invoices/create_generating_service_spec.rb index 2e12b9ad0ad..830272d2f19 100644 --- a/spec/services/invoices/create_generating_service_spec.rb +++ b/spec/services/invoices/create_generating_service_spec.rb @@ -118,5 +118,15 @@ end end end + + context "when customer is a partner account" do + let(:customer) { create(:customer, account_type: "partner") } + + it "creates an invoice with self billed" do + result = create_service.call + + expect(result.invoice.self_billed).to eq(true) + end + end end end diff --git a/spec/services/invoices/payments/create_service_spec.rb b/spec/services/invoices/payments/create_service_spec.rb index 75bec167324..90f4c6ccfa1 100644 --- a/spec/services/invoices/payments/create_service_spec.rb +++ b/spec/services/invoices/payments/create_service_spec.rb @@ -97,6 +97,21 @@ end end + context "when invoice is self_billed" do + let(:invoice) do + create(:invoice, :self_billed, customer:, organization:, total_amount_cents: 100) + end + + it "does not creates a payment" do + result = create_service.call + + expect(result).to be_success + expect(result.invoice).to eq(invoice) + expect(result.payment).to be_nil + expect(provider_class).not_to have_received(:new) + end + end + context "when invoice is payment_succeeded" do before { invoice.payment_succeeded! }