Last active
June 7, 2021 20:49
-
-
Save mdaftab88/cf333cf2e62d031a45830ef62ac762f9 to your computer and use it in GitHub Desktop.
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
Billing Issue | |
# select distinct account_id , payment_subscription_id from billing_units bu where | |
# ( | |
# ( | |
# location_associated_at between '2021-05-20 01:00:00' and '2021-05-27 01:00:00' | |
# ) or | |
# ( | |
# archival_scheduled_at between '2021-05-20 01:00:00' and '2021-05-27 01:00:00' | |
# ) | |
# ); | |
pses = [] | |
pses += [{"account_id" => 637, "id" => "95cf8de3-96b4-448e-bb75-3ae81e0c977e"},{"account_id" => 2962, "id" => "db964bab-2e54-4b70-8c14-8b5b2b710bff"},{"account_id" => 5759, "id" => "ed695e4a-169a-4ffa-9ecb-a05f292a6710"},{"account_id" => 6522, "id" => "46162836-5380-4061-ac70-4ade9dfd2f35"},{"account_id" => 9234, "id" => "8ead6d3d-1a46-4a1d-9a05-19100cd81f85"},{"account_id" => 14550, "id" => "acb2be4a-3ba4-4f60-8bd4-6d9723c135b2"},{"account_id" => 14822, "id" => "b8f36536-6f7c-4fb0-8797-0215c402e77d"},{"account_id" => 16154, "id" => "b8b5075f-35ff-4aaf-9580-44271e0dde34"},{"account_id" => 16642, "id" => "e6aa54cf-d3b2-4af2-8493-ea342c5f18a5"},{"account_id" => 16687, "id" => "179e0336-99f7-426d-80a7-99beed18f56e"},{"account_id" => 17203, "id" => "07555057-f308-484c-8bf9-d3233249be94"},{"account_id" => 17382, "id" => "a9e26768-0472-40b4-b04f-6fe20d9f1352"},{"account_id" => 17843, "id" => "6aa60235-a211-4397-8d91-e6ca6ab789fd"},{"account_id" => 17978, "id" => "3bcdb57c-9a9a-4a72-9dbe-56f1aa2ccaf0"},{"account_id" => 18153, "id" => "52b93d92-cc0c-4de7-83e6-0d26d26ab479"},{"account_id" => 18243, "id" => "632a2192-4fd6-4e58-b429-980fae0a64c7"},{"account_id" => 18410, "id" => "c63fccae-7fb5-4260-b1b8-e7bd7d10d5e5"},{"account_id" => 18682, "id" => "2b51a4a0-ac14-4cd0-bfdd-613dea3a350b"},{"account_id" => 19266, "id" => "5fdb0592-f539-4bdc-bb2e-ee7252c3b9cc"},{"account_id" => 19271, "id" => "90098cc0-129f-4654-ae37-3490781df06f"}] | |
pses += [{"account_id" => 19315, "id" => "17b8ec5e-f4c8-4e5f-a204-c0623856cd0b"},{"account_id" => 19369, "id" => "cc49357a-0fee-47e2-87eb-60643609a2ba"},{"account_id" => 19370, "id" => "e1bcced0-7f65-4262-9cee-365b55d09086"},{"account_id" => 19454, "id" => "5f8b3d2b-eb36-40df-8632-36408f9c58f8"},{"account_id" => 19554, "id" => "90cf15aa-5439-43ab-a25d-028ec9687fff"},{"account_id" => 19694, "id" => "f0f541da-29f0-4132-b11d-cee507eebcf3"},{"account_id" => 19724, "id" => "574e6ea4-7655-411f-8631-312357186867"},{"account_id" => 20379, "id" => "11641b05-12f3-4f8a-9437-d577c53d3502"},{"account_id" => 20461, "id" => "4a08d1ee-b152-4863-9311-8715f3330939"},{"account_id" => 20478, "id" => "c11bffd8-4b73-439e-b87e-6668dfb3d100"},{"account_id" => 20534, "id" => "8b95ab7f-d6ee-484d-808f-41f1b72d11f4"},{"account_id" => 20549, "id" => "405e6a65-b034-406a-b1a1-e9e83118f72b"},{"account_id" => 20577, "id" => "195d7ca8-4960-49e4-9830-20411b53b4cb"},{"account_id" => 20670, "id" => "616a35db-7517-441d-a57e-bd0d7b3b10b5"},{"account_id" => 20675, "id" => "fa43af61-2d33-4aec-a49a-5174d4355f11"},{"account_id" => 20740, "id" => "bd1ce31c-7f3f-43a3-9778-e097bd56ee62"},{"account_id" => 20828, "id" => "16c7b21a-0e5f-42b3-8df6-2252b749b511"},{"account_id" => 20889, "id" => "a2e16630-6d44-45dc-a7e2-bc8f9c36a47f"},{"account_id" => 20904, "id" => "cc14d8a7-b6b5-427a-82d1-073472470b28"},{"account_id" => 20959, "id" => "c28862c8-227b-4245-ab50-8835d1b02fb9"}] | |
pses += [{"account_id" => 21011, "id" => "bcab5e19-71e5-4f4e-a003-20af42d95e65"},{"account_id" => 21041, "id" => "85f005d8-ea52-4a4f-adee-15b85c7f3b79"},{"account_id" => 21092, "id" => "0f3e57b6-da8d-4038-b9ce-947d5cb1335f"},{"account_id" => 21126, "id" => "4202ae2b-a990-4c1e-a36d-715b66a9fd27"},{"account_id" => 21341, "id" => "6e6914e6-672b-4575-ab6d-c1be315b944a"},{"account_id" => 21368, "id" => "43d93b14-0891-4341-8241-0ab12b13b77a"},{"account_id" => 21472, "id" => "a53dea5e-ccdc-4990-8d8f-8a4bdd6ae5e5"},{"account_id" => 21638, "id" => "a3c5a627-2629-4248-8917-d1a87ef0d118"},{"account_id" => 21660, "id" => "39a6c1d2-8b51-4f49-b49c-10b840ca9749"},{"account_id" => 21665, "id" => "ae5256df-39b8-474f-aee3-4e6d003440ff"},{"account_id" => 21698, "id" => "06e85e9b-98fe-4c30-97be-6cab3ff01f71"},{"account_id" => 21871, "id" => "174c893d-6d7c-44a1-a5db-0cf9b952b37c"},{"account_id" => 21899, "id" => "678d3306-7e61-4d36-96d4-b1cf21469a84"},{"account_id" => 21984, "id" => "5cf43e7b-90f2-4e0b-965e-fcca964e78e9"},{"account_id" => 22011, "id" => "ee2607bf-afd6-4406-bf93-e9b99c7da040"},{"account_id" => 22016, "id" => "77bcca3b-7477-43e2-9a42-a0e6cbd42b46"},{"account_id" => 22018, "id" => "57e979a7-1864-4272-94c2-bf82fb606a90"},{"account_id" => 22080, "id" => "7838f5b6-1912-4bc6-9924-959331a5f4a6"},{"account_id" => 22119, "id" => "6e51bcae-a150-4c03-97be-4ea5bf0baed7"},{"account_id" => 22207, "id" => "19d8e6d1-dd23-4cc7-b75c-c2beaa0bd3b0"}] | |
pses += [{"account_id" => 22292, "id" => "f5e33fdd-8075-41ec-8414-1b022f0efac8"},{"account_id" => 22342, "id" => "b1966a0c-bccf-45d7-bf0b-087e43007097"},{"account_id" => 22364, "id" => "8334a7c3-312f-422e-a26d-dc7781298962"},{"account_id" => 22374, "id" => "f17bce48-65a5-46bd-abe2-0fa29630893f"},{"account_id" => 22379, "id" => "8ca2fd19-7677-4c22-aefa-5b6e5b95552f"},{"account_id" => 22382, "id" => "2a169a36-743c-4051-841c-932b88dcec5f"},{"account_id" => 22386, "id" => "90a1da72-1961-4e4e-93f0-128b3f0e044b"},{"account_id" => 22389, "id" => "3691f24d-a744-483b-81a8-5a84c4752123"},{"account_id" => 22429, "id" => "8d98e982-6276-4489-b584-6655f163a5e6"},{"account_id" => 22430, "id" => "516e1ed0-8231-4bb0-801e-0fa94a019a32"},{"account_id" => 22435, "id" => "a23db063-5827-4574-9ea8-fddb5aa61eb9"},{"account_id" => 22459, "id" => "9b1f41db-3f90-4f00-a636-e11def6dc7b5"},{"account_id" => 22481, "id" => "77397875-cc32-4744-acfe-73616ab5532c"},{"account_id" => 22500, "id" => "35680672-6149-4422-acfc-2f81357643ed"},{"account_id" => 22501, "id" => "1530a7bd-35b3-4ad8-a388-7e18581be5f9"},{"account_id" => 22524, "id" => "0d47dcb6-bd19-4208-b7be-e0dc65af1a65"},{"account_id" => 22527, "id" => "8e636087-9599-4fb2-990b-07818989cca6"},{"account_id" => 22529, "id" => "4a26cdea-4a11-4c57-9295-e680fb7d9417"},{"account_id" => 22541, "id" => "1159b126-de2b-4483-bce7-49ae970ebba6"},{"account_id" => 22543, "id" => "2f156b60-98e6-46f7-be07-476c042fc0cc"}] | |
pses += [{"account_id" => 22546, "id" => "5ba25998-478a-4a11-a452-b9677844a703"},{"account_id" => 22564, "id" => "44228d59-175e-4c8b-b4ba-adc50a555d5b"},{"account_id" => 22565, "id" => "602319b5-1f0b-4107-a0be-4640a6a87592"},{"account_id" => 22572, "id" => "4a38c814-080c-40b8-958f-8db623928989"},{"account_id" => 22589, "id" => "a7fb3786-d36d-4083-ac04-fd669e10821d"},{"account_id" => 22590, "id" => "d9f8afa3-142c-4f27-b60e-3d18e0a4afa8"},{"account_id" => 22595, "id" => "85c75b4e-2ad7-4c3b-ab9c-2a4c324fca15"},{"account_id" => 22596, "id" => "00f14db8-fdba-489b-be13-30d3f085e522"},{"account_id" => 22599, "id" => "2845236b-fc04-48e3-bf0a-6b34f9d2a46e"}] | |
FROM_DATE_TO_CHECK = Time.parse("2021-05-21 01:00:00 UTC") | |
def find_daily_unit_count(ps) | |
# invoice generated condition | |
if ps.billing_period == 1 && ps.current_term_starts_at > FROM_DATE_TO_CHECK && ps.current_term_starts_at < Time.now | |
start_date = ps.current_term_starts_at - 1.month | |
end_date = ps.current_term_ends_at - 1.month - 1.day | |
data = start_date.to_date.upto(end_date.to_date).each_with_object({}) do |datee, _hash| | |
start_time = datee.beginning_of_day + 1.hour | |
end_time = datee.end_of_day + 1.hour | |
bu_added = ps.billing_units.where("location_associated_at between '#{start_time}' and '#{end_time}'") | |
bu_archived = ps.billing_units.where("archival_scheduled_at between '#{start_time}' and '#{end_time}'") | |
_hash[datee.to_s] = { | |
location_added: bu_added.count, | |
location_archived: bu_archived.count, | |
} | |
end | |
data | |
end | |
end | |
def gateway_invoices(gateway_subscription_id) | |
result = ChargeBee::Invoice.invoices_for_subscription(gateway_subscription_id, { | |
:limit => 2, | |
"sort_by[desc]" => "date" | |
}) | |
last_invoice, second_last_invoice = result.map { |e| e.invoice } | |
[last_invoice, second_last_invoice] | |
end | |
def day_wise_unbilled_charges(invoice) | |
invoice.line_items.each_with_object({}) do |li, _hash| | |
date_from = Time.at(li.date_from) | |
date_to = Time.at(li.date_to) | |
_hash[date_from.strftime("%Y-%m-%d")] = li if li.description == "Premium (monthly) - Prorated Charges" | |
end | |
end | |
def calculate_prorata_price(ps, invoice, date) | |
unit_price = ps.unit_price * 100 | |
date_obj = Date.parse(date.to_s) | |
if day_wise_unbilled_charges(invoice)[date.to_s].present? | |
day_wise_unbilled_charges(invoice)[date_obj.to_s].unit_amount | |
else | |
last_term_ended_at = ps.current_term_ends_at - 1.month - 1.day | |
left_days = last_term_ended_at.to_date.mjd - date_obj.mjd | |
total_no_of_days = date_obj.all_month.count | |
price_for_one_day = unit_price.to_f/total_no_of_days | |
(left_days + 1) * price_for_one_day | |
end | |
end | |
def addon_price_in_cents(gateway_subscription_id) | |
subscription = ChargeBee::Subscription.retrieve(gateway_subscription_id).subscription | |
addons = subscription.addons | |
price = 0.00 | |
if addons.present? | |
addons.each do |addon| | |
price += addon.amount | |
end | |
end | |
price | |
end | |
report = {} | |
pses.each do |_ps| | |
begin | |
ps = PaymentSubscription.find_by(_ps) | |
if ps.present? | |
case ps.billing_period | |
when 1 # MONTHLY | |
if ps.current_term_starts_at > FROM_DATE_TO_CHECK && ps.current_term_starts_at < Time.now | |
# invoice generated | |
db_unit_count = find_daily_unit_count(ps) | |
current_base_units = ps.base_units | |
last_invoice, second_last_invoice = gateway_invoices(ps.gateway_subscription_id) | |
if second_last_invoice.blank? | |
previous_unit_count = 0 # new account first month | |
else | |
if Time.at(second_last_invoice.date).to_date == ps.created_at.to_date | |
previous_unit_count = 0 # new account with second month | |
else | |
previous_unit_count = second_last_invoice&.line_items&.select {|e| e.as_json["description"] == "Premium (monthly)"}.first.as_json["quantity"] rescue nil | |
end | |
end | |
current_unit_price = ps.unit_price | |
total_invoice_amount = 0.00 | |
final_unit_count = previous_unit_count | |
db_unit_count.each do |date, billing_unit_data| | |
final_unit_count += billing_unit_data[:location_added] - billing_unit_data[:location_archived] | |
prorated_price_of_one_unit = calculate_prorata_price(ps, last_invoice, date) | |
total_invoice_amount += prorated_price_of_one_unit * billing_unit_data[:location_added] if final_unit_count > current_base_units | |
p "#{ps.gateway_subscription_id}---#{date}---add: #{billing_unit_data[:location_added]} --archived: ##{billing_unit_data[:location_archived]}---#{prorated_price_of_one_unit}---#{total_invoice_amount}---#{final_unit_count}" | |
end | |
if final_unit_count < current_base_units | |
total_invoice_amount += (current_base_units * current_unit_price * 100) | |
else | |
total_invoice_amount += (final_unit_count * current_unit_price * 100) | |
end | |
total_invoice_amount += addon_price_in_cents(ps.gateway_subscription_id) | |
actual_amount = (total_invoice_amount.to_f/100).round(2) | |
charged_amount = ((last_invoice.total - last_invoice.tax).to_f/100).round(2) | |
report["#{ps.account_id}___#{ps.id}___#{ps.gateway_subscription_id}"] = { | |
"actual_amount_without_tax" => actual_amount, | |
"charged_amount_without_tax" => charged_amount, | |
"difference" => (actual_amount - charged_amount).round(2) | |
} | |
else | |
# invoice not generated. Update unbilled charges on Chargebee | |
report["#{ps.account_id}___#{ps.id}___#{ps.gateway_subscription_id}"] = "INVOICE NOT GENERATED YET" | |
end | |
when 3 # QUARTERLY | |
report["#{ps.account_id}___#{ps.id}___#{ps.gateway_subscription_id}"] = "QUARTERLY SUBSCRIPTION" | |
when 12 # YEARLY | |
report["#{ps.account_id}___#{ps.id}___#{ps.gateway_subscription_id}"] = "YEARLY SUBSCRIPTION" | |
end | |
end | |
rescue Exception => e | |
report["#{ps.account_id}___#{ps.id}___#{ps.gateway_subscription_id}"] = "EXCEPTION #{e.message}" | |
end | |
end | |
# JUST A COMMENT | |
# result = ChargeBee::Subscription.retrieve("2skdb1h9RaNq3aD23eU").subscription.addons | |
# result = ChargeBee::Invoice.invoices_for_subscription("AzqNOkSWYRqqN1O7D", {:limit => 2,"sort_by[desc]" => "date"}) | |
# last_invoice, second_last_invoice = result.map { |e| e.invoice } | |
# second_last_invoice.line_items |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment