Make sure we update the correct month for billing:

When populating the monthly billing records on a schedule, we need
to ensure the correct month is being updated.

As an example, if the current datetime is 31 Mar 2016, 23:00. The
BST equivalent is the 1st April. Therefore we need to ensure we
update billing for April, not March. This takes care of that.
This commit is contained in:
Imdad Ahad
2017-08-10 16:37:30 +01:00
parent 782f3ea693
commit 63e1167098
3 changed files with 99 additions and 25 deletions

View File

@@ -9,7 +9,7 @@ from sqlalchemy.exc import SQLAlchemyError
from app.aws import s3
from app import notify_celery
from app import performance_platform_client
from app.dao.date_util import get_month_start_end_date
from app.dao.date_util import get_month_start_and_end_date_in_utc
from app.dao.inbound_sms_dao import delete_inbound_sms_created_more_than_a_week_ago
from app.dao.invited_user_dao import delete_invitations_created_more_than_two_days_ago
from app.dao.jobs_dao import (
@@ -17,8 +17,8 @@ from app.dao.jobs_dao import (
dao_get_jobs_older_than_limited_by
)
from app.dao.monthly_billing_dao import (
get_service_ids_that_need_sms_billing_populated,
create_or_update_monthly_billing_sms
get_service_ids_that_need_billing_populated,
create_or_update_monthly_billing
)
from app.dao.notifications_dao import (
dao_timeout_notifications,
@@ -37,6 +37,7 @@ from app.notifications.process_notifications import send_notification_to_queue
from app.statsd_decorators import statsd
from app.celery.tasks import process_job
from app.config import QueueNames
from app.utils import convert_utc_to_bst
@notify_celery.task(name="remove_csv_files")
@@ -297,6 +298,7 @@ def populate_monthly_billing():
# for every service with billable units this month update billing totals for yesterday
# this will overwrite the existing amount.
yesterday = datetime.utcnow() - timedelta(days=1)
start_date, end_date = get_month_start_end_date(yesterday)
services = get_service_ids_that_need_sms_billing_populated(start_date=start_date, end_date=end_date)
[create_or_update_monthly_billing_sms(service_id=s.service_id, billing_month=yesterday) for s in services]
yesterday_in_bst = convert_utc_to_bst(yesterday)
start_date, end_date = get_month_start_and_end_date_in_utc(yesterday_in_bst)
services = get_service_ids_that_need_billing_populated(start_date=start_date, end_date=end_date)
[create_or_update_monthly_billing(service_id=s.service_id, billing_month=end_date) for s in services]