moved issue hooks into issue_patch

This commit is contained in:
2026-02-03 22:42:08 -05:00
parent 83fb20044d
commit c36f4c905b
2 changed files with 57 additions and 85 deletions

View File

@@ -1,36 +0,0 @@
#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.
module Hooks
class IssuesSaveHookListener < Redmine::Hook::ViewListener
# Called Before Issue Saved
def controller_issues_edit_before_save(context={})
return context[:issue].subject = context[:issue].subject.titleize
end
def controller_issues_new_before_save(context={})
return context[:issue].subject = context[:issue].subject.titleize
end
# Called After Issue Saved
def controller_issues_edit_after_save(context={})
issue = context[:issue]
begin
issue.bill_time if issue.status.is_closed?
rescue
# TODO flash[:error] = "Unable to bill, check QBO Auth"
end
end
end
end

View File

@@ -12,9 +12,9 @@ require_dependency 'issue'
module Patches module Patches
# Patches Redmine's Issues dynamically. # Patches Redmine's Issues dynamically.
# Adds a relationships # Adds relationships for customers, estimates, invoices, customer_tokens
# Adds before and after save hooks
module IssuePatch module IssuePatch
def self.included(base) # :nodoc: def self.included(base) # :nodoc:
@@ -28,6 +28,8 @@ module Patches
belongs_to :customer_token, primary_key: :id belongs_to :customer_token, primary_key: :id
belongs_to :estimate, primary_key: :id belongs_to :estimate, primary_key: :id
has_and_belongs_to_many :invoices has_and_belongs_to_many :invoices
before_save :titlize_subject
after_save :bill_time
end end
end end
@@ -40,61 +42,62 @@ module Patches
# Create billable time entries # Create billable time entries
def bill_time def bill_time
logger.debug "QBO: Billing time for issue ##{id} - #{subject}"
# Check to see if we have everything we need to bill the customer return unless status.is_closed?
return if assigned_to.nil? return if assigned_to.nil?
return unless Qbo.first return unless Qbo.first
return unless customer return unless customer
# Get unbilled time entries Thread.new do
spent_time = time_entries.where(billed: [false, nil]) spent_time = time_entries.where(billed: [false, nil])
spent_hours ||= spent_time.sum(:hours) || 0 spent_hours ||= spent_time.sum(:hours) || 0
if spent_hours > 0 then if spent_hours > 0 then
# Prepare to create a new Time Activity # Prepare to create a new Time Activity
qbo = Qbo.first qbo = Qbo.first
qbo.perform_authenticated_request do |access_token| qbo.perform_authenticated_request do |access_token|
time_service = Quickbooks::Service::TimeActivity.new(company_id: qbo.realm_id, access_token: access_token) 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) item_service = Quickbooks::Service::Item.new(company_id: qbo.realm_id, access_token: access_token)
time_entry = Quickbooks::Model::TimeActivity.new time_entry = Quickbooks::Model::TimeActivity.new
# Lets total up each activity before billing. # Lets total up each activity before billing.
# This will simpify the invoicing with a single billable time entry per time activity # This will simpify the invoicing with a single billable time entry per time activity
h = Hash.new(0) h = Hash.new(0)
spent_time.each do |entry| spent_time.each do |entry|
h[entry.activity.name] += entry.hours h[entry.activity.name] += entry.hours
# update time entries billed status # update time entries billed status
entry.billed = true entry.billed = true
entry.save entry.save
end end
# Now letes upload our totals for each activity as their own billable time entry # Now letes upload our totals for each activity as their own billable time entry
h.each do |key, val| h.each do |key, val|
# Convert float spent time to hours and minutes # Convert float spent time to hours and minutes
hours = val.to_i hours = val.to_i
minutesDecimal = (( val - hours) * 60) minutesDecimal = (( val - hours) * 60)
minutes = minutesDecimal.to_i minutes = minutesDecimal.to_i
# Lets match the activity to an qbo item # Lets match the activity to an qbo item
item = item_service.query("SELECT * FROM Item WHERE Name = '#{key}' ").first item = item_service.query("SELECT * FROM Item WHERE Name = '#{key}' ").first
next if item.nil? next if item.nil?
# Create the new billable time entry and upload it # Create the new billable time entry and upload it
time_entry.description = "#{tracker} ##{id}: #{subject} #{"(Partial @ #{done_ratio}%)" if not closed?}" time_entry.description = "#{tracker} ##{id}: #{subject} #{"(Partial @ #{done_ratio}%)" if not closed?}"
time_entry.employee_id = assigned_to.employee_id time_entry.employee_id = assigned_to.employee_id
time_entry.customer_id = customer_id time_entry.customer_id = customer_id
time_entry.billable_status = "Billable" time_entry.billable_status = "Billable"
time_entry.hours = hours time_entry.hours = hours
time_entry.minutes = minutes time_entry.minutes = minutes
time_entry.name_of = "Employee" time_entry.name_of = "Employee"
time_entry.txn_date = Date.today time_entry.txn_date = Date.today
time_entry.hourly_rate = item.unit_price time_entry.hourly_rate = item.unit_price
time_entry.item_id = item.id time_entry.item_id = item.id
time_entry.start_time = start_date time_entry.start_time = start_date
time_entry.end_time = Time.now time_entry.end_time = Time.now
time_service.create(time_entry) time_service.create(time_entry)
end
end end
end end
end end
@@ -106,6 +109,11 @@ module Patches
CustomerToken.get_token self CustomerToken.get_token self
end end
# Titleize the subject before save
def titlize_subject
self.subject = self.subject.titleize
end
end end
# Add module to Issue # Add module to Issue