Files
rickyandClaude Opus 5.5 9fa199aa98 Refunds for canceled orders and returned parts (SOP-OPS-014)
A Refund page on the customer: the canceled order's estimate fills in
the parts the 30% restocking fee is worked out on. Refunding makes the
fee invoice, pays it from the customer's credit, records the rest as a
Check (company check) or Expense (card refund on the terminal) with the
category Accounts Receivable, applies the credit to it so the balance
comes back to $0.00, and optionally marks the estimate Rejected,
cancels its work orders with the reason as a note and emails the
customer. Each refund has a page with a PDF of it.

New permission: Issue refunds. New settings under Billing » Refunds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 20:43:36 -05:00

102 lines
5.2 KiB
Ruby

#The MIT License (MIT)
#
#Copyright (c) 2026 rick barrette
#
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
require File.expand_path('../test_helper', __dir__)
class QboRefundsTest < Redmine::IntegrationTest
include QboRefundTestHelper
def setup
@customer = create_customer
Customer.any_instance.stubs(:email).returns('jane@example.com')
RedmineQboBilling::RefundWriter.stubs(:fee_item).returns(fee_item)
@gateway = FakeGateway.new(payments: [deposit(50, 500)], estimate: estimate)
RedmineQboBilling::RefundGateway.stubs(:new).returns(@gateway)
RedmineQboBilling::RefundWriter.stubs(:estimate_choices).returns([['E1068 · 2026-10-06', '598']])
QboRefundMailer.stubs(:refund_summary).returns(stub(deliver_now: true))
end
def test_needs_the_permission
log_user('jsmith', 'jsmith')
get "/qbo_refunds/new?customer_id=#{CUSTOMER_ID}"
assert_response :forbidden
Role.find(1).add_permission!(:issue_refunds)
get "/qbo_refunds/new?customer_id=#{CUSTOMER_ID}"
assert_response :success
end
def test_refund_page_fills_in_the_estimates_parts
log_user('admin', 'admin')
get "/qbo_refunds/new?customer_id=#{CUSTOMER_ID}&estimate_id=598"
assert_response :success
assert_select 'input[name=?][value=?]', 'refund[parts_amount]', '307.99'
assert_select '#qbo-refund-credit', text: '$500.00'
assert_select '#qbo-refund-fee', text: '-$92.40'
assert_select '#qbo-refund-amount', text: '$407.60'
assert_select 'input[name=?][value=check][checked]', 'refund[method]'
end
def test_refund
log_user('admin', 'admin')
post '/qbo_refunds', params: { refund: { customer_id: CUSTOMER_ID, estimate_id: '598', parts_amount: '307.99',
method: 'check', check_number: '5001', txn_date: '2026-11-16',
reason: 'Canceled', close_estimate: '1', cancel_issues: '0', email_customer: '1' } }
refund = QboRefund.last
assert_redirected_to "/qbo_refunds/#{refund.id}"
assert refund.done?, refund.message
assert_equal BigDecimal('407.60'), refund.refund_amount
follow_redirect!
assert_select 'p', text: /\$407\.60 · Company check #5001/
assert_select 'a[href*="app/check?txnId="]'
assert_select 'a[href=?]', "/qbo_invoices/#{refund.fee_invoice_id}/edit"
assert_select 'a[href=?]', '/qbo_estimates/598/edit'
assert_select 'a[href*="app/invoice?txnId="]', count: 0
get "/customers/#{CUSTOMER_ID}"
assert_select 'a[href=?]', "/qbo_refunds/#{refund.id}"
end
def test_form_errors
log_user('admin', 'admin')
post '/qbo_refunds', params: { refund: { customer_id: CUSTOMER_ID, parts_amount: '10', method: 'check',
check_number: '', txn_date: '2026-11-16', reason: '' } }
assert_response :unprocessable_entity
assert_select '#errorExplanation', text: /check number/
assert_select '#errorExplanation', text: /reason/
assert_equal 0, QboRefund.count
end
def test_email_again
log_user('admin', 'admin')
refund = QboRefund.create!(customer_id: CUSTOMER_ID, method: 'check', check_number: '5001', status: 'done',
refund_amount: 100, txn_date: Date.new(2026, 11, 16))
post "/qbo_refunds/#{refund.id}/email"
assert_redirected_to "/qbo_refunds/#{refund.id}"
assert_equal 'jane@example.com', refund.reload.emailed_to
end
def test_pdf
log_user('admin', 'admin')
refund = QboRefund.create!(customer_id: CUSTOMER_ID, method: 'check', check_number: '5001', status: 'done',
estimate_number: '1068', reason: 'Canceled', parts_amount: 307.99, fee_rate: 30,
fee_amount: 92.40, fee_invoice_number: '1502', credit_amount: 500, refund_amount: 407.60,
balance_after: 0, txn_date: Date.new(2026, 11, 16), user_id: 1)
get "/qbo_refunds/#{refund.id}/pdf"
assert_response :success
assert_equal 'application/pdf', response.media_type
assert response.body.start_with?('%PDF')
assert_match(/refund-2026-11-16-jane-customer\.pdf/, response.headers['Content-Disposition'])
get "/qbo_refunds/#{refund.id}"
assert_select 'a[href=?]', "/qbo_refunds/#{refund.id}/pdf"
end
end