2017-11-02 17:02:00 +00:00
|
|
|
from app import db
|
2017-10-24 13:23:24 +01:00
|
|
|
from app.dao.dao_utils import (
|
|
|
|
|
transactional,
|
|
|
|
|
)
|
|
|
|
|
from app.models import AnnualBilling
|
2017-11-02 12:19:17 +00:00
|
|
|
from app.dao.date_util import get_current_financial_year_start_year
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@transactional
|
2017-12-06 11:03:42 +00:00
|
|
|
def dao_create_or_update_annual_billing_for_year(service_id, free_sms_fragment_limit, financial_year_start):
|
2017-11-02 12:19:17 +00:00
|
|
|
result = dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start)
|
|
|
|
|
|
|
|
|
|
if result:
|
|
|
|
|
result.free_sms_fragment_limit = free_sms_fragment_limit
|
|
|
|
|
else:
|
|
|
|
|
result = AnnualBilling(service_id=service_id, financial_year_start=financial_year_start,
|
|
|
|
|
free_sms_fragment_limit=free_sms_fragment_limit)
|
|
|
|
|
db.session.add(result)
|
|
|
|
|
return result
|
2017-10-24 13:23:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def dao_get_annual_billing(service_id):
|
|
|
|
|
return AnnualBilling.query.filter_by(
|
|
|
|
|
service_id=service_id,
|
2017-10-25 12:50:13 +01:00
|
|
|
).order_by(AnnualBilling.financial_year_start).all()
|
2017-10-24 13:23:24 +01:00
|
|
|
|
|
|
|
|
|
2017-11-02 12:19:17 +00:00
|
|
|
@transactional
|
2017-12-06 14:41:32 +00:00
|
|
|
def dao_update_annual_billing_for_future_years(service_id, free_sms_fragment_limit, financial_year_start):
|
2017-11-28 13:49:14 +00:00
|
|
|
AnnualBilling.query.filter(
|
2017-11-02 12:19:17 +00:00
|
|
|
AnnualBilling.service_id == service_id,
|
2017-12-06 14:41:32 +00:00
|
|
|
AnnualBilling.financial_year_start > financial_year_start
|
2017-11-02 12:19:17 +00:00
|
|
|
).update(
|
|
|
|
|
{'free_sms_fragment_limit': free_sms_fragment_limit}
|
|
|
|
|
)
|
|
|
|
|
|
2017-10-24 13:23:24 +01:00
|
|
|
|
2017-11-02 12:19:17 +00:00
|
|
|
def dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start=None):
|
2017-10-24 13:23:24 +01:00
|
|
|
|
2017-11-02 12:19:17 +00:00
|
|
|
if not financial_year_start:
|
|
|
|
|
financial_year_start = get_current_financial_year_start_year()
|
2017-10-24 13:23:24 +01:00
|
|
|
|
|
|
|
|
return AnnualBilling.query.filter_by(
|
|
|
|
|
service_id=service_id,
|
2017-11-02 12:19:17 +00:00
|
|
|
financial_year_start=financial_year_start
|
2017-10-24 13:23:24 +01:00
|
|
|
).first()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def dao_get_all_free_sms_fragment_limit(service_id):
|
|
|
|
|
|
|
|
|
|
return AnnualBilling.query.filter_by(
|
|
|
|
|
service_id=service_id,
|
2017-10-25 12:50:13 +01:00
|
|
|
).order_by(AnnualBilling.financial_year_start).all()
|
2017-10-25 11:35:13 +01:00
|
|
|
|
|
|
|
|
|
2017-12-01 16:31:21 +00:00
|
|
|
def dao_insert_annual_billing_for_this_year(service, free_sms_fragment_limit):
|
2017-10-25 11:35:13 +01:00
|
|
|
"""
|
|
|
|
|
This method is called from create_service which is wrapped in a transaction.
|
|
|
|
|
"""
|
|
|
|
|
annual_billing = AnnualBilling(
|
2017-12-01 16:31:21 +00:00
|
|
|
free_sms_fragment_limit=free_sms_fragment_limit,
|
2017-10-25 11:35:13 +01:00
|
|
|
financial_year_start=get_current_financial_year_start_year(),
|
|
|
|
|
service=service,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
db.session.add(annual_billing)
|