def fiscal_period(self): """ This is the fiscal period we want to limit our queries to when querying CPE values for self.fiscal_year. If it's prior to Q1 submission window close date, we will return quarter 1 anyhow and just show what we have (which will likely be incomplete). """ return get_final_period_of_quarter( calculate_last_completed_fiscal_quarter(self.fiscal_year)) or 3
def test_get_final_period_of_quarter(): assert fyh.get_final_period_of_quarter(1) == 3 assert fyh.get_final_period_of_quarter(2) == 6 assert fyh.get_final_period_of_quarter(3) == 9 assert fyh.get_final_period_of_quarter(4) == 12 assert fyh.get_final_period_of_quarter(None) is None assert fyh.get_final_period_of_quarter("1") is None assert fyh.get_final_period_of_quarter("a") is None assert fyh.get_final_period_of_quarter({"hello": "there"}) is None
def get_prior_year_agency_budgetary_resources(self): """ Even though we're looking at fiscal_year - 1, it's possible that fiscal year hasn't been closed out yet. For example, if today is 5 Oct 1999 then the submission window for FY 1999 is not closed yet even though we're in FY2000 Q1. """ prior_fiscal_year = self.fiscal_year - 1 prior_fiscal_year_last_completed_fiscal_period = get_final_period_of_quarter( calculate_last_completed_fiscal_quarter(prior_fiscal_year)) return AppropriationAccountBalances.objects.filter( submission__reporting_fiscal_year=prior_fiscal_year, submission__reporting_fiscal_period= prior_fiscal_year_last_completed_fiscal_period, treasury_account_identifier__funding_toptier_agency=self. toptier_agency, ).aggregate(agency_budgetary_resources=Sum( "total_budgetary_resources_amount_cpe") )["agency_budgetary_resources"]
def setup_test_data(db): """ Insert data into DB for testing """ dabs = mommy.make("submissions.DABSSubmissionWindowSchedule", submission_reveal_date="2020-10-09") sub = mommy.make( "submissions.SubmissionAttributes", submission_id=1, toptier_code="123", quarter_format_flag=False, reporting_fiscal_year=2019, reporting_fiscal_period=6, published_date="2019-07-03", submission_window_id=dabs.id, ) sub2 = mommy.make( "submissions.SubmissionAttributes", submission_id=2, toptier_code="987", quarter_format_flag=False, reporting_fiscal_year=CURRENT_FISCAL_YEAR, reporting_fiscal_period=CURRENT_LAST_PERIOD, published_date=f"{CURRENT_FISCAL_YEAR}-{CURRENT_LAST_PERIOD+1:02}-07", submission_window_id=dabs.id, ) sub3 = mommy.make( "submissions.SubmissionAttributes", submission_id=3, toptier_code="001", quarter_format_flag=True, reporting_fiscal_year=CURRENT_FISCAL_YEAR, reporting_fiscal_period=CURRENT_LAST_PERIOD, published_date=f"{CURRENT_FISCAL_YEAR}-{CURRENT_LAST_PERIOD+1:02}-07", submission_window_id=dabs.id, ) mommy.make("references.Agency", id=1, toptier_agency_id=1, toptier_flag=True) mommy.make("references.Agency", id=2, toptier_agency_id=2, toptier_flag=True) mommy.make("references.Agency", id=3, toptier_agency_id=3, toptier_flag=True) agencies = [ mommy.make("references.ToptierAgency", toptier_agency_id=1, toptier_code="123", abbreviation="ABC", name="Test Agency"), mommy.make( "references.ToptierAgency", toptier_agency_id=2, toptier_code="987", abbreviation="XYZ", name="Test Agency 2", ), mommy.make( "references.ToptierAgency", toptier_agency_id=3, toptier_code="001", abbreviation="AAA", name="Test Agency 3", ), ] treas_accounts = [ mommy.make( "accounts.TreasuryAppropriationAccount", treasury_account_identifier=1, awarding_toptier_agency_id=agencies[0].toptier_agency_id, tas_rendering_label="tas-1-overview", ), mommy.make( "accounts.TreasuryAppropriationAccount", treasury_account_identifier=2, awarding_toptier_agency_id=agencies[2].toptier_agency_id, tas_rendering_label="tas-2-overview", ), mommy.make( "accounts.TreasuryAppropriationAccount", treasury_account_identifier=3, awarding_toptier_agency_id=agencies[1].toptier_agency_id, tas_rendering_label="tas-3-overview", ), ] approps = [ { "sub_id": sub.submission_id, "treasury_account": treas_accounts[0], "total_resources": 50 }, { "sub_id": sub3.submission_id, "treasury_account": treas_accounts[1], "total_resources": 12 }, { "sub_id": sub3.submission_id, "treasury_account": treas_accounts[1], "total_resources": 29 }, { "sub_id": sub2.submission_id, "treasury_account": treas_accounts[2], "total_resources": 15.5 }, ] for approp in approps: mommy.make( "accounts.AppropriationAccountBalances", submission_id=approp["sub_id"], treasury_account_identifier=approp["treasury_account"], total_budgetary_resources_amount_cpe=approp["total_resources"], ) reporting_tases = [ { "year": sub.reporting_fiscal_year, "period": sub.reporting_fiscal_period, "label": treas_accounts[0].tas_rendering_label, "toptier_code": agencies[0].toptier_code, "diff": 29.5, }, { "year": sub2.reporting_fiscal_year, "period": sub2.reporting_fiscal_period, "label": treas_accounts[1].tas_rendering_label, "toptier_code": agencies[2].toptier_code, "diff": -1.3, }, { "year": sub2.reporting_fiscal_year, "period": sub2.reporting_fiscal_period, "label": treas_accounts[2].tas_rendering_label, "toptier_code": agencies[1].toptier_code, "diff": 20.5, }, ] for reporting_tas in reporting_tases: mommy.make( "reporting.ReportingAgencyTas", fiscal_year=reporting_tas["year"], fiscal_period=reporting_tas["period"], tas_rendering_label=reporting_tas["label"], toptier_code=reporting_tas["toptier_code"], diff_approp_ocpa_obligated_amounts=reporting_tas["diff"], appropriation_obligated_amount=100, ) mommy.make( "reporting.ReportingAgencyOverview", reporting_agency_overview_id=1, toptier_code="123", fiscal_year=2019, fiscal_period=6, total_dollars_obligated_gtas=1788370.03, total_budgetary_resources=22478810.97, total_diff_approp_ocpa_obligated_amounts=84931.95, unlinked_procurement_c_awards=1, unlinked_assistance_c_awards=2, unlinked_procurement_d_awards=3, unlinked_assistance_d_awards=4, ) mommy.make( "reporting.ReportingAgencyOverview", reporting_agency_overview_id=2, toptier_code="987", fiscal_year=CURRENT_FISCAL_YEAR, fiscal_period=CURRENT_LAST_PERIOD, total_dollars_obligated_gtas=18.6, total_budgetary_resources=100, total_diff_approp_ocpa_obligated_amounts=0, unlinked_procurement_c_awards=10, unlinked_assistance_c_awards=20, unlinked_procurement_d_awards=30, unlinked_assistance_d_awards=40, ) mommy.make( "reporting.ReportingAgencyOverview", reporting_agency_overview_id=3, toptier_code="001", fiscal_year=CURRENT_FISCAL_YEAR, fiscal_period=CURRENT_LAST_PERIOD, total_dollars_obligated_gtas=20.0, total_budgetary_resources=10.0, total_diff_approp_ocpa_obligated_amounts=10.0, unlinked_procurement_c_awards=100, unlinked_assistance_c_awards=200, unlinked_procurement_d_awards=300, unlinked_assistance_d_awards=400, ) mommy.make( "reporting.ReportingAgencyMissingTas", toptier_code="123", fiscal_year=2019, fiscal_period=6, tas_rendering_label="TAS 1", obligated_amount=10.0, ) mommy.make( "reporting.ReportingAgencyMissingTas", toptier_code="123", fiscal_year=2019, fiscal_period=6, tas_rendering_label="TAS 2", obligated_amount=1.0, ) mommy.make( "reporting.ReportingAgencyMissingTas", toptier_code="987", fiscal_year=CURRENT_FISCAL_YEAR, fiscal_period=CURRENT_LAST_PERIOD, tas_rendering_label="TAS 2", obligated_amount=12.0, ) mommy.make( "reporting.ReportingAgencyMissingTas", toptier_code="987", fiscal_year=current_fiscal_year(), fiscal_period=get_final_period_of_quarter( calculate_last_completed_fiscal_quarter(current_fiscal_year())) or 3, tas_rendering_label="TAS 3", obligated_amount=0, )
from django.conf import settings from model_mommy import mommy from rest_framework import status from usaspending_api.common.helpers.fiscal_year_helpers import ( current_fiscal_year, calculate_last_completed_fiscal_quarter, get_final_period_of_quarter, ) url = "/api/v2/reporting/agencies/overview/" CURRENT_FISCAL_YEAR = current_fiscal_year() CURRENT_LAST_QUARTER = calculate_last_completed_fiscal_quarter( CURRENT_FISCAL_YEAR) or 1 CURRENT_LAST_PERIOD = get_final_period_of_quarter(CURRENT_LAST_QUARTER) or 3 assurance_statement_1 = ( f"{settings.FILES_SERVER_BASE_URL}/agency_submissions/Raw%20DATA%20Act%20Files/" "2019/P06/123%20-%20Test%20Agency%20(ABC)/2019-P06-123_Test%20Agency%20(ABC)-Assurance_Statement.txt" ) assurance_statement_2 = ( f"{settings.FILES_SERVER_BASE_URL}/agency_submissions/Raw%20DATA%20Act%20Files/" f"{CURRENT_FISCAL_YEAR}/P{CURRENT_LAST_PERIOD:02}/987%20-%20Test%20Agency%202%20(XYZ)/" f"{CURRENT_FISCAL_YEAR}-P{CURRENT_LAST_PERIOD:02}-987_Test%20Agency%202%20(XYZ)-Assurance_Statement.txt" ) assurance_statement_3 = ( f"{settings.FILES_SERVER_BASE_URL}/agency_submissions/Raw%20DATA%20Act%20Files/" f"{CURRENT_FISCAL_YEAR}/Q{CURRENT_LAST_QUARTER}/001%20-%20Test%20Agency%203%20(AAA)/" f"{CURRENT_FISCAL_YEAR}-Q{CURRENT_LAST_QUARTER}-001_Test%20Agency%203%20(AAA)-Assurance_Statement.txt" )