Removed month and year and replaced it with start_date and end_date.

This will allow us to sort the data properly.
This commit is contained in:
Rebecca Law
2017-07-26 13:19:17 +01:00
parent 3878ece9ea
commit c1f2634c90
6 changed files with 95 additions and 29 deletions

View File

@@ -158,6 +158,7 @@ class PopulateMonthlyBilling(Command):
)
def run(self, service_id, month, year):
print('Starting populating monthly billing')
create_or_update_monthly_billing_sms(service_id, datetime(int(year), int(month), 1))
results = get_monthly_billing_sms(service_id, datetime(int(year), int(month), 1))
print("Finished populating data for {} for service id {}".format(month, service_id))

View File

@@ -25,25 +25,25 @@ def create_or_update_monthly_billing_sms(service_id, billing_month):
monthly = get_billing_data_for_month(service_id=service_id, start_date=start_date, end_date=end_date)
# update monthly
monthly_totals = _monthly_billing_data_to_json(monthly)
row = MonthlyBilling.query.filter_by(year=billing_month.year,
month=datetime.strftime(end_date, "%B"),
row = MonthlyBilling.query.filter_by(start_date=start_date,
notification_type='sms').first()
if row:
row.monthly_totals = monthly_totals
row.updated_at = datetime.utcnow()
else:
row = MonthlyBilling(service_id=service_id,
notification_type=SMS_TYPE,
year=billing_month.year,
month=datetime.strftime(end_date, "%B"),
monthly_totals=monthly_totals)
monthly_totals=monthly_totals,
start_date=start_date,
end_date=end_date)
db.session.add(row)
@statsd(namespace="dao")
def get_monthly_billing_sms(service_id, billing_month):
start_date, end_date = get_month_start_end_date(billing_month)
monthly = MonthlyBilling.query.filter_by(service_id=service_id,
year=billing_month.year,
month=datetime.strftime(billing_month, "%B"),
start_date=start_date,
notification_type=SMS_TYPE).first()
return monthly

View File

@@ -1253,20 +1253,20 @@ class MonthlyBilling(db.Model):
id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, nullable=False)
service = db.relationship('Service', backref='monthly_billing')
month = db.Column(db.String, nullable=False)
year = db.Column(db.Float(asdecimal=False), nullable=False)
start_date = db.Column(db.DateTime, nullable=False)
end_date = db.Column(db.DateTime, nullable=False)
notification_type = db.Column(notification_types, nullable=False)
monthly_totals = db.Column(JSON, nullable=False)
updated_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
__table_args__ = (
UniqueConstraint('service_id', 'month', 'year', 'notification_type', name='uix_monthly_billing'),
UniqueConstraint('service_id', 'start_date', 'notification_type', name='uix_monthly_billing'),
)
def serialized(self):
return {
"month": self.month,
"year": self.year,
"start_date": self.start_date,
"end_date": self.end_date,
"service_id": str(self.service_id),
"notification_type": self.notification_type,
"monthly_totals": self.monthly_totals