Created
February 21, 2019 10:54
-
-
Save martink-io/fca3c4849515a1875abef510b33ca011 to your computer and use it in GitHub Desktop.
Used Credit method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Current Version | |
def used_credit_amount | |
used_credit = jobs.where(paid_on_credit: true) | |
.where.not(state: 'unpaid') | |
.where.not(state: 'complete') | |
.map(&:total_charge).sum | |
completed_jobs_on_credit = jobs.completed_paid_on_credit_by_client(self) | |
completed_jobs_on_credit.each do |job| | |
if job.client_invoice.status == 'unpaid' | |
used_credit += (job.client_invoice.amount - Checkout::CreditPayment.where(job_id: job.id).map(&:amount).sum) | |
end | |
end | |
used_credit.round(2) | |
end | |
# Edition 1 - Exclude total_charge for all jobs in cancelled states | |
def used_credit_amount | |
used_credit = jobs.where(paid_on_credit: true) | |
.where.not(state: 'unpaid') | |
.where.not(state: 'complete') | |
.map(&:total_charge).sum | |
completed_jobs_on_credit = jobs.completed_paid_on_credit_by_client(self) | |
cancelled_jobs_on_credit = jobs.cancelled_paid_on_credit_by_client(self) | |
completed_jobs_on_credit.each do |job| | |
if job.client_invoice.status == 'unpaid' | |
used_credit += (job.client_invoice.amount - Checkout::CreditPayment.where(job_id: job.id).map(&:amount).sum) | |
end | |
end | |
cancelled_jobs_on_credit.each do |job| | |
used_credit -= job.total_charge | |
end | |
used_credit.round(2) | |
end | |
# Version 2 - Don't consider cancelled states to begin with | |
def used_credit_amount | |
used_credit = jobs.where(paid_on_credit: true) | |
.where.not(state: 'unpaid') | |
.where.not(state: 'complete') | |
.where.not(state: Job::CANCELLED_STATES) | |
.map(&:total_charge).sum | |
completed_jobs_on_credit = jobs.completed_paid_on_credit_by_client(self) | |
completed_jobs_on_credit.each do |job| | |
if job.client_invoice.status == 'unpaid' | |
used_credit += (job.client_invoice.amount - Checkout::CreditPayment.where(job_id: job.id).map(&:amount).sum) | |
end | |
end | |
used_credit.round(2) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment