mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2026-11-18 02:20:37 -05:00
Security - Connecting to QuickBooks (authenticate/oauth_callback) and all syncs are admin-only; the OAuth state is stored and verified on callback - New "Bill issue time to QuickBooks" project permission; billing is now a POST and checks the permission and issue visibility - Estimate/invoice PDFs require a logged-in user, or a customer share link limited to the estimate and invoices attached to that issue (the old require_user check never ran) - Attachments patch keeps the share token per request instead of in Thread.current, so it can't leak into other requests Permissions and share links - customers#autocomplete and the filter_* actions are part of View customers, so non-admins can pick customers on forms - customers#view no longer goes through authorize_global; share needs the new "Share issues with customers" permission and a visible issue - Removed a check in customers#view that compared a value to itself Bugs - Subjects and customer names capitalize each word without lowercasing the rest (LED, JL, 4x4 stay as typed); subjects only when changed - Settings page shows Invoice/Estimate sync times (Item/Account models no longer exist) - customers#update shows validation errors instead of reporting success - Billing marks each activity billed once sent to QBO, so retries and token refreshes don't create duplicate TimeActivities Search - New CustomerSearchService for autocomplete and the customer search page: nicknames (Bill/William), typos, word order, partial words, accents, and phone digits in any format; ranked, with the candidate list cached until a customer changes Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM
135 lines
5.8 KiB
Ruby
135 lines
5.8 KiB
Ruby
#The MIT License (MIT)
|
|
#
|
|
#Copyright (c) 2016 - 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.
|
|
|
|
class BillIssueTimeJob < ActiveJob::Base
|
|
queue_as :default
|
|
retry_on StandardError, wait: 5.minutes, attempts: 5
|
|
|
|
# Perform billing of unbilled time entries for a given issue by creating corresponding TimeActivity records in QuickBooks Online, and then marking those entries as billed in Redmine. This job is typically triggered after an invoice is created or updated to ensure all relevant time is captured for billing.
|
|
def perform(issue_id)
|
|
issue = Issue.find(issue_id)
|
|
|
|
log "Starting billing for issue ##{issue.id}"
|
|
failure = nil
|
|
|
|
issue.with_lock do
|
|
unbilled_entries = issue.time_entries.where(billed: [false, nil]).lock.includes(:activity).to_a
|
|
return if unbilled_entries.blank?
|
|
|
|
totals = aggregate_hours(unbilled_entries)
|
|
return if totals.blank?
|
|
log "Aggregated hours for billing: #{totals.inspect}"
|
|
|
|
# Activities already sent to QBO (or deliberately skipped) in this run. If QBO fails part way, or the
|
|
# token refresh re-runs the block, finished activities are not sent again.
|
|
done = []
|
|
begin
|
|
qbo = QboConnectionService.current!
|
|
qbo.perform_authenticated_request do |access_token|
|
|
create_time_activities(issue, totals, access_token, qbo, done)
|
|
end
|
|
rescue => e
|
|
failure = e
|
|
end
|
|
|
|
# Mark billed only the entries whose activity was finished, and commit that even if a later
|
|
# activity failed, so a retry only sends what's left
|
|
finished = unbilled_entries.select { |entry| done.include?(entry.activity&.name) }
|
|
TimeEntry.where(id: finished.map(&:id)).update_all(billed: true) if finished.any?
|
|
end
|
|
|
|
raise failure if failure
|
|
|
|
log "Completed billing for issue ##{issue.id}"
|
|
Qbo.update_time_stamp
|
|
rescue => e
|
|
log "Billing failed for issue ##{issue_id} - #{e.message}"
|
|
raise e
|
|
end
|
|
|
|
private
|
|
|
|
# Aggregate time entries by activity name and sum their hours
|
|
def aggregate_hours(entries)
|
|
entries.group_by { |e| e.activity&.name }
|
|
.transform_values { |rows| rows.sum(&:hours) }
|
|
.compact
|
|
end
|
|
|
|
# Create TimeActivity records in QBO for each activity type with the appropriate hours and link them to the issue's assigned employee and customer
|
|
# Adds each activity name to done once it has been created in QBO (or skipped because it can't be billed).
|
|
def create_time_activities(issue, totals, access_token, qbo, done)
|
|
log "Creating TimeActivity records in QBO for issue ##{issue.id}"
|
|
|
|
time_service = Quickbooks::Service::TimeActivity.new( company_id: qbo.realm_id, access_token: access_token)
|
|
item_service = Quickbooks::Service::Item.new( company_id: qbo.realm_id, access_token: access_token )
|
|
|
|
totals.each do |activity_name, hours_float|
|
|
next if done.include?(activity_name)
|
|
|
|
# Not billable: skipped, and marked billed as before
|
|
if activity_name.blank? || hours_float.to_f <= 0
|
|
done << activity_name
|
|
next
|
|
end
|
|
|
|
item = find_item(item_service, activity_name)
|
|
unless item
|
|
log "No QBO item named #{activity_name.inspect}; skipping"
|
|
done << activity_name
|
|
next
|
|
end
|
|
|
|
hours, minutes = convert_hours(hours_float)
|
|
|
|
time_entry = Quickbooks::Model::TimeActivity.new
|
|
time_entry.description = build_description(issue)
|
|
time_entry.employee_id = issue.assigned_to.employee_id
|
|
time_entry.customer_id = issue.customer_id
|
|
time_entry.billable_status = "Billable"
|
|
time_entry.hours = hours
|
|
time_entry.minutes = minutes
|
|
time_entry.name_of = "Employee"
|
|
time_entry.txn_date = Date.today
|
|
time_entry.hourly_rate = item.unit_price
|
|
time_entry.item_id = item.id
|
|
|
|
log "Creating TimeActivity for #{activity_name} (#{hours}h #{minutes}m)"
|
|
|
|
time_service.create(time_entry)
|
|
done << activity_name
|
|
end
|
|
end
|
|
|
|
# Convert a decimal hours float into separate hours and minutes components for QBO TimeActivity
|
|
def convert_hours(hours_float)
|
|
total_minutes = (hours_float.to_f * 60).round
|
|
hours = total_minutes / 60
|
|
minutes = total_minutes % 60
|
|
[hours, minutes]
|
|
end
|
|
|
|
# Build a descriptive string for the TimeActivity based on the issue's tracker, ID, subject, and completion status
|
|
def build_description(issue)
|
|
base = "#{issue.tracker} ##{issue.id}: #{issue.subject}"
|
|
return base if issue.closed?
|
|
"#{base} (Partial @ #{issue.done_ratio}%)"
|
|
end
|
|
|
|
# Find an item in QBO by name, escaping single quotes to prevent query issues. Returns nil if not found.
|
|
def find_item(item_service, name)
|
|
safe = name.gsub("'", "\\\\'")
|
|
item_service.query("SELECT * FROM Item WHERE Name = '#{safe}'").first
|
|
end
|
|
|
|
def log(msg)
|
|
Rails.logger.info "[BillIssueTimeJob] #{msg}"
|
|
end
|
|
end |