Commit Graph

26 Commits

Author SHA1 Message Date
Andrew Shumway
afd77cf9fe Returned months changed to calendar year 2024-02-07 14:53:26 -07:00
Carlo Costino
490893397e Fixed isinstance() calls, oops
Signed-off-by: Carlo Costino <carlo.costino@gsa.gov>
2023-10-30 16:30:06 -04:00
Carlo Costino
8c825a43bd Updated a couple more dependencies and fix flake8 warnings
Signed-off-by: Carlo Costino <carlo.costino@gsa.gov>
2023-10-30 16:16:49 -04:00
Kenneth Kehl
1ecb747c6d reformat 2023-08-29 14:54:30 -07:00
Kenneth Kehl
9f9e0a6ad8 notify-136 change financial year starting in april to calendar year (#278)
Co-authored-by: Kenneth Kehl <@kkehl@flexion.us>
2023-06-14 16:19:11 -04:00
Kenneth Kehl
08c1ad75c8 notify-260 remove server-side timezone handling 2023-05-10 08:39:50 -07:00
stvnrlly
99de747a36 fix formatting 2022-11-21 11:29:38 -05:00
stvnrlly
c8533ae524 pull timezone from utils for other pytz instances 2022-11-16 16:53:55 -05:00
stvnrlly
213f699c99 time adjustments in tests 2022-11-14 14:23:54 -05:00
stvnrlly
b50cb4712f tz utility swap and many test updates 2022-11-10 12:33:25 -05:00
Ben Thorner
ee4da698fe Standardise timezones for service usage APIs
We want to query for service usage in the BST financial year:

    2022-04-01T00:00:00+01:00 to 2023-03-31T23:59:59+01:00 =>
    2022-04-01 to 2023-03-31  # bst_date

Previously we were only doing this explicitly for the monthly API
and it seemed like the yearly usage API was incorrectly querying:

    2022-03-31T23:00:00+00:00 to 2023-03-30T23:00:00+00:00 =>
    2022-03-31 to 2023-03-30  # "bst_date"

However, it turns out this isn't a problem for two reasons:

1. We've been lucky that none of our rates have changed since 2017,
which is long ago enough that no one would care.

2. There's a quirk somewhere in Sqlalchemy / Postgres that has been
compensating for the lack of explicit BST conversion.

To help ensure we do this consistently in future I've DRYed-up the
BST conversion into a new utility. I could have just hard-coded the
dates but it seemed strange to have the knowledge twice.

I've also adjusted the tests so they detect if we accidentally use
data from a different financial year. (2) is why none of the test
assertions actually need changing and users won't be affected.

Sqlalchemy / Postgres quirk
===========================

The following queries were run on the same data but results differ:

    FactBilling.query.filter(FactBilling.bst_date >= datetime(2021,3,31,23,0), FactBilling.bst_date <= '2021-04-05').order_by(FactBilling.bst_date).first().bst_date
    datetime.date(2021, 4, 1)

    FactBilling.query.filter(FactBilling.bst_date >= '2021-03-31 23:00:00', FactBilling.bst_date <= '2021-04-05').order_by(FactBilling.bst_date).first().bst_date
    datetime.date(2021, 3, 31)

Looking at the actual query for the first item above still suggests
the results should be the same, but for the use of "timestamp".

    SELECT ...
    FROM ft_billing
    WHERE ft_billing.service_id = '16b60315-9dab-45d3-a609-e871fbbf5345'::uuid AND ft_billing.bst_date >= '2016-03-31T23:00:00'::timestamp AND ft_billing.bst_date <= '2017-03-31T22:59:59.999999'::timestamp AND ft_billing.notification_type IN ('email', 'letter') GROUP BY ft_billing.rate, ft_billing.notification_type UNION ALL SELECT sum(ft_billing.notifications_sent) AS notifications_sent, sum(ft_billing.billable_units * ft_billing.rate_multiplier) AS billable_units, ft_billing.rate AS ft_billing_rate, ft_billing.notification_type AS ft_billing_notification_type
    FROM ft_billing
    WHERE ft_billing.service_id = '16b60315-9dab-45d3-a609-e871fbbf5345'::uuid AND ft_billing.bst_date >= '2016-03-31T23:00:00'::timestamp AND ft_billing.bst_date <= '2017-03-31T22:59:59.999999'::timestamp AND ft_billing.notification_type = 'sms' GROUP BY ft_billing.rate, ft_billing.notification_type) AS anon_1 ORDER BY anon_1.notification_type, anon_1.rate

If we try some manual queries with and without '::timestamp' we get:

    select distinct(bst_date) from ft_billing where bst_date >= '2022-04-20T23:00:00' order by bst_date desc;
      bst_date
    ------------
     2022-04-21
     2022-04-20

    select distinct(bst_date) from ft_billing where bst_date >= '2022-04-20T23:00:00'::timestamp order by bst_date desc;
      bst_date
    ------------
     2022-04-21
     2022-04-20

It looks like this is happening because all client connections are
aware of the local timezone, and naive datetimes are interpreted as
being in UTC - not necessarily true, but saves us here!

The monthly API datetimes were pre-converted to dates, so none of
this was relevant for deciding exactly which date to use.
2022-04-26 13:11:34 +01:00
Ben Thorner
a91fde2fda Run auto-correct on app/ and tests/ 2021-03-12 11:45:45 +00:00
Leo Hemsted
93e631221a use dates rather than datetimes when comparing with bst_date
bst_date is a date field. Comparing dates with datetimes in postgres
gets confusing and dangerous. See this example, where a date evaluates
as older than midnight that same day.

```
notification_api=# select '2019-04-01' >= '2019-04-01 00:00';
 ?column?
----------
 f
(1 row)
```

By only using dates everywhere, we reduce the chance of these bugs
happening
2019-09-02 11:56:56 +01:00
Leo Hemsted
b7e8f1baa2 rename which_financial_year to get_financial_year_for_datetime
also fix bug on the very second of rollover, march 31st 23:00:00, and
add tests
2019-08-30 12:18:00 +01:00
Rebecca Law
236d111c22 Update queries for data model change (removal of organisation_to_service)
New query to get letter breakdown.
2019-08-30 12:17:59 +01:00
Pea Tyczynska
c15d7878fc Only include ft_billing data for current financial year
Test get_live_services_data endpoint

Expand dao_fetch_live_services_data test with more ft_billing records
2019-05-01 15:57:45 +01:00
Katie Smith
ff06d120e8 Bump notifications-utils to 3.7.0
Bumped notifications-utils to 3.7.0. Version 3.7.0 includes the
`convert_utc_to_bst` and `convert_bst_to_utc` functions and the
`LETTER_PROCESSING_DEADLINE` constant, so these have been removed from
this repo and anywhere using these has now been updated to get these
from `notifications-utils`.

Also bumped pytest by a patch version to bring in a bug fix.
2018-11-26 12:53:39 +00:00
Leo Hemsted
089056ef72 add tests for new statistics and fact notification status functions 2018-07-03 14:58:32 +01:00
venusbb
6f7793d761 - Add update dao_update_annual_billing_for_current_and_future_years
- moved get_current_financial_year_start_year from service.utils to dao.date_utils
- Moved logic for data persistence from rest to dao when updating records in db
2017-11-02 12:38:43 +00:00
venusbb
b2fa7cdd83 create new table annual_billing 2017-10-18 15:35:51 +01:00
Imdad Ahad
2371c2c9a7 Add helper to get months for a financial year:
* No tests have been added as this was taken from the admin
  where it is already tested thoroughly.
* TODO: Move to utils
2017-08-18 12:45:05 +01:00
Imdad Ahad
782f3ea693 Rename method to get start and end date of a month for clarity 2017-08-11 16:56:47 +01:00
Rebecca Law
b62ee8380c Use BST to calculate monthly billing 2017-07-25 17:38:53 +01:00
Rebecca Law
9400988d72 Monthly billing - part 1
This is still a work in progress but it would be good to get some eyes on it.
This commit includes creating and updating a row in the monthly billing table and a method to fetch the results.
There is a command to populate the monthly billing for a service and month so we can try it out.
The total cost at the moment are wrong, they do not take into account the free allowance - see notes below about adding that to the table.
Left to do:
create a nightly task to run to update the monthly totals.
create an endpoint to return the yearly billing, the current day will need to be calculated on the fly and added to the totals.
Add the free allowance into the total costs.
2017-07-18 18:21:35 +01:00
Rebecca Law
3e0221adec Change get_financial_year to return ending date as 1 microsecond earlier.
That way we can write the queries as between start and end dates, making it easier to read.
This makes more sense.
2017-05-02 10:00:47 +01:00
Rebecca Law
6dc336ad6c Created new queries to return the rate with the sum of billable units for the year totals.
Once we have the new columns in notifications table, the query will need to include the rate multiplier and if the number is international.
The monthly billing query will be built next.
2017-04-28 10:10:48 +01:00