Skip to content

[Receipts] Switch to calculating missing receipts using Tasks #10602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions app/controllers/admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,25 @@ def merchant_memo_check
end
end

def receipts
@user = User.find_by(email: params[:user_email]) if params[:user_email].present?
if @user
deprecated = @user.transactions_missing_receipt
current = @user.transactions_missing_receipt_v2
difference = deprecated - current

@results = {
counts: {
deprecated: deprecated.count,
current: current.count,
},
difference:
}
else
@results = "Select a user"
end
end

def employees
@page = params[:page] || 1
@per = params[:per] || 20
Expand Down
12 changes: 12 additions & 0 deletions app/jobs/task/nightly_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class Task
class NightlyJob < ApplicationJob
queue_as :low
def perform
::TaskService::Nightly.new.run
end

end

end
12 changes: 9 additions & 3 deletions app/models/concerns/receiptable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,17 @@ def no_or_lost_receipt!
raise e
end

after_create_commit do
after_create_commit :create_task!

def ensure_task_exists!
create_task! unless tasks.any? || !receipt_required?
end

def create_task!
safely do
assignee = try(:author) || try(:user) || try(:event)
if missing_receipt? && assignee
Task::Receiptable::Upload.create!(taskable: self, assignee:)
if assignee
Task::Receiptable::Upload.create!(taskable: self, assignee:, complete: !missing_receipt?)
end
end
end
Expand Down
14 changes: 14 additions & 0 deletions app/models/hcb_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,10 @@ def ct2
# HCB-600: Stripe card charges (always required)
# @sampoder

scope :of_type, ->(code) {
where("hcb_code LIKE 'HCB-#{code}%'")
}

scope :receipt_required, -> {
joins("LEFT JOIN canonical_pending_transactions ON canonical_pending_transactions.hcb_code = hcb_codes.hcb_code")
.joins("LEFT JOIN canonical_pending_declined_mappings ON canonical_pending_declined_mappings.canonical_pending_transaction_id = canonical_pending_transactions.id")
Expand All @@ -470,8 +474,18 @@ def ct2
")
}

scope :without_receipt, -> {
left_outer_joins(:receipts).where(receipts: { id: nil })
}

scope :with_receipt, -> {
left_outer_joins(:receipts).where.not(receipts: { id: nil })
}


def receipt_required?
return false if pt&.declined?
return false if event.plan.type == Event::Plan::SalaryAccount.name

(type == :card_charge) ||
# starting from Feb. 2024, receipts have been required for ACHs & checks
Expand Down
4 changes: 3 additions & 1 deletion app/models/task/receiptable/upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
class Task
module Receiptable
class Upload < Task
scope :for_hcb_code, -> { where(taskable_type: "HcbCode") }

def update_complete!
update(complete: !taskable.missing_receipt?)
update(complete: taskable.nil? || !taskable.missing_receipt? || !taskable.receipt_required?)
end

def text
Expand Down
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ def transactions_missing_receipt
end
end

def transactions_missing_receipt_v2
HcbCode.where(id: tasks.where(type: "Task::Receiptable::Upload", taskable_type: "HcbCode").incomplete.select(:taskable_id)).of_type(600)
end

def transactions_missing_receipt_count
@transactions_missing_receipt_count ||= begin
transactions_missing_receipt.size
Expand Down
57 changes: 57 additions & 0 deletions app/services/task_service/nightly.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module TaskService
class Nightly
def initialize
@tasks = Task::Receiptable::Upload.incomplete
@hcb_codes_without_task = HcbCode.receipt_required.where.not(id: Task::Receiptable::Upload.select(:taskable_id))
end

def run
puts "starting nightly task service"

ensure_tasks_exist
update_task_completion
check_for_parity
end

def ensure_tasks_exist
count = @hcb_codes_without_task.count
i = 0

@hcb_codes_without_task.find_each(batch_size: 100) do |hcb_code|
hcb_code.ensure_task_exists!
i += 1
puts "Processed HCB Code #{i} of #{count}" if i % 100 == 0
end
end

def update_task_completion
count = @tasks.count
i = 0

@tasks.find_each(batch_size: 100) do |task|
task.update_complete!
i += 1
puts "Updated Task #{i} of #{count}" if i % 100 == 0
end
end

def check_for_parity
count = User.count
i = 0

User.find_each(batch_size: 100) do |user|
old = user.transactions_missing_receipt
current = user.transactions_missing_receipt_v2

if old.count != current.count
Airbrake.notify("User #{user.id} has #{old.count} old, but #{current.count} new")
end

i += 1

puts "Checked User #{i} of #{count}" if i % 100 == 0
end
end

end
end
11 changes: 11 additions & 0 deletions app/views/admin/receipts.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%= form_with url: receipts_admin_index_path, method: :get, local: true do |form| %>
<%= form.text_field :user_email, value: params[:user_email], placeholder: 'Enter user email' %>
<%= form.submit 'Search' %>
<% end %>

<% if @user %>
<h2>Missing receipts for <%= @user.name %> (<%= @user.email %>)</h2>
<% end %>


<%= (ap @results).html_safe %>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@
get "merchant_memo_check", to: "admin#merchant_memo_check"
get "unknown_merchants", to: "admin#unknown_merchants"
post "request_balance_export", to: "admin#request_balance_export"
get "receipts", to: "admin#receipts"
end

member do
Expand Down
Loading