def setUp(self): self.client.force_login(self.test_user) self.out = StringIO() obj = ProgrammeCodeFactory() self.programme_code = obj.programme_code self.programme_description = obj.programme_description self.budget_type = obj.budget_type.budget_type current_year = get_current_financial_year() self.archive_year = current_year - 1 current_year = get_current_financial_year() self.archive_year = current_year - 1
def write_data(self, writer): current_year = get_current_financial_year() current_queryset = ExpenditureCategory.objects.all().order_by( "-NAC_category__NAC_category_description", "grouping_description", "description", "further_description", ) historical_queryset = (ArchivedExpenditureCategory.objects.all(). select_related("financial_year").order_by( "-financial_year", "-NAC_category", "grouping_description", "description", "further_description", )) for obj in current_queryset: row = [ obj.NAC_category.NAC_category_description, obj.grouping_description, obj.description, obj.further_description, current_year, ] writer.writerow(row) for obj in historical_queryset: row = [ obj.NAC_category, obj.grouping_description, obj.description, obj.further_description, obj.financial_year.financial_year, ] writer.writerow(row)
def __init__(self): group_name = "Test Group" self.group_code = "TestGG" directorate_name = "Test Directorate" self.directorate_code = "TestDD" self.cost_centre_code = 109076 group_obj = DepartmentalGroupFactory( group_code=self.group_code, group_name=group_name, ) directorate_obj = DirectorateFactory( directorate_code=self.directorate_code, directorate_name=directorate_name, group=group_obj, ) cost_centre_obj = CostCentreFactory( directorate=directorate_obj, cost_centre_code=self.cost_centre_code, ) current_year = get_current_financial_year() programme_obj = ProgrammeCodeFactory() self.programme_code = programme_obj.programme_code nac_obj = NaturalCodeFactory(economic_budget_code="RESOURCE") self.nac = nac_obj.natural_account_code project_obj = ProjectCodeFactory() self.project_code = project_obj.project_code self.year_obj = FinancialYear.objects.get(financial_year=current_year) self.financial_code_obj = FinancialCode.objects.create( programme=programme_obj, cost_centre=cost_centre_obj, natural_account_code=nac_obj, project_code=project_obj, ) self.financial_code_obj.save
def setUp(self): self.client.force_login(self.test_user) self.out = StringIO() self.group_name = "Test Group" self.group_code = "TestGG" self.directorate_name = "Test Directorate" self.directorate_code = "TestDD" self.cost_centre_code = 109076 group = DepartmentalGroupFactory( group_code=self.group_code, group_name=self.group_name, ) directorate = DirectorateFactory( directorate_code=self.directorate_code, directorate_name=self.directorate_name, group=group, ) CostCentreFactory( directorate=directorate, cost_centre_code=self.cost_centre_code, ) current_year = get_current_financial_year() self.archive_year = current_year - 1
def month_list(self): # returns the list of month with actuals in the selected period. if self._month_list is None: if self.year: if self.year == get_current_financial_year() - 1: # We are displaying the last year before the current one. # It is possible that the actuals for march and the adjustments # have not been loaded yet, so get the list from # the FinancialPeriod self._month_list = FinancialPeriod.financial_period_info.\ actual_month_previous_year_list() else: # We are displaying historical data, so we need to include # the adjustment periods (ADJxx), and everything is actuals self._month_list = \ FinancialPeriod.financial_period_info.month_adj_display_list() else: period = self.period if period: # We are displaying historical forecast self._month_list = \ FinancialPeriod.financial_period_info.month_sublist(period) else: self._month_list = \ FinancialPeriod.financial_period_info.actual_month_list() return self._month_list
def write_data(self, writer): current_year = get_current_financial_year() current_queryset = Analysis1.objects.filter(active=True).order_by( "analysis1_code", "analysis1_description", ) historical_queryset = (ArchivedAnalysis1.objects.filter( active=True).select_related("financial_year").order_by( "-financial_year", "analysis1_code", "analysis1_description", )) for obj in current_queryset: row = [ obj.analysis1_code, obj.analysis1_description, obj.supplier, obj.pc_reference, current_year, ] writer.writerow(row) for obj in historical_queryset: row = [ obj.analysis1_code, obj.analysis1_description, obj.supplier, obj.pc_reference, obj.financial_year.financial_year, ] writer.writerow(row)
def restore_archive(period_id): if period_id > MAX_PERIOD_CODE or period_id < 1: error_msg = (f"Invalid period {period_id}: " f"Valid Period is between 1 and {MAX_PERIOD_CODE}.") logger.error(error_msg, exc_info=True) raise ArchiveMonthInvalidPeriodError(error_msg) end_of_month_info = EndOfMonthStatus.objects.get( archived_period__financial_period_code=period_id) if not end_of_month_info.archived: error_msg = f"The archive for {period_id} does not exist." logger.error(error_msg, exc_info=True) raise RestoreNonExistingArchiveError(error_msg) current_year = get_current_financial_year() restore_data(ForecastMonthlyFigure, current_year, period_id) restore_data(BudgetMonthlyFigure, current_year, period_id) MonthlyTotalBudget.objects.filter( archived_status__archived_period__financial_period_code__gte=period_id, ).delete() EndOfMonthStatus.objects.filter( archived_period__financial_period_code__gte=period_id).update( archived=False, archived_date=None) # Clear the actual loaded flag for periods after the se FinancialPeriod.objects.filter( financial_period_code__gt=period_id, ).update(actual_loaded=False, )
def write_data(self, writer): current_year = get_current_financial_year() current_queryset = (InterEntity.objects.filter( active=True).select_related("l1_value").order_by( "l1_value__l1_value", "l1_value__l1_description", "l2_value")) historical_queryset = (ArchivedInterEntity.objects.filter( active=True).select_related("financial_year").order_by( "-financial_year", "l1_value", "l1_description", "l2_value", )) for obj in current_queryset: row = [ obj.l1_value.l1_value, obj.l1_value.l1_description, obj.l2_value, obj.l2_description, obj.cpid, current_year, ] writer.writerow(row) for obj in historical_queryset: row = [ obj.l1_value, obj.l1_description, obj.l2_value, obj.l2_description, obj.cpid, obj.financial_year.financial_year, ] writer.writerow(row)
def write_data(self, writer): current_year = get_current_financial_year() current_queryset = ProgrammeCode.objects.filter(active=True).order_by( "programme_code", "programme_description", "budget_type__budget_type") historical_queryset = (ArchivedProgrammeCode.objects.filter( active=True).select_related("financial_year").order_by( "-financial_year", "programme_code", "programme_description", "budget_type__budget_type", )) for obj in current_queryset: if obj.budget_type: budget_type = obj.budget_type.budget_type else: budget_type = None row = [ obj.programme_code, obj.programme_description, budget_type, current_year, ] writer.writerow(row) for obj in historical_queryset: row = [ obj.programme_code, obj.programme_description, obj.budget_type.budget_type, obj.financial_year.financial_year, ] writer.writerow(row)
def delete_end_of_month_archive(period_id): if period_id > MAX_PERIOD_CODE or period_id < 1: error_msg = f'Invalid period {period_id}: ' \ f'Valid Period is between 1 and {MAX_PERIOD_CODE}.' logger.error(error_msg, exc_info=True) raise ArchiveMonthInvalidPeriodError(error_msg) end_of_month_info = EndOfMonthStatus.objects.get( archived_period__financial_period_code=period_id) if not end_of_month_info.archived: error_msg = f"The archive for {period_id} does not exist." logger.error(error_msg, exc_info=True) raise DeleteNonExistingArchiveError(error_msg) current_year = get_current_financial_year() forecast_queryset = ForecastMonthlyFigure.objects.filter( financial_year_id=current_year, archived_status=end_of_month_info, ) forecast_queryset.delete() BudgetMonthlyFigure.objects.filter( financial_year_id=current_year, archived_status=end_of_month_info, ).delete() MonthlyTotalBudget.objects.filter( archived_status=end_of_month_info).delete() end_of_month_info.archived = False end_of_month_info.archived_date = timezone.now() end_of_month_info.save()
def setUp(self): self.out = StringIO() init_data = MonthlyFigureSetup() init_data.setup_forecast() init_data.setup_budget() call_command("archive") self.current_year = get_current_financial_year() call_command("archive_current_year", )
def import_adi_file(csvfile): """Read the ADI file and unpivot it to enter the MonthlyFigure data""" fin_year = get_current_financial_year() # Clear the table first. The adi file has several lines with the same key, # so the figures have to be added and we don't want to add to existing data! ForecastMonthlyFigure.objects.filter( financial_year=fin_year, archived_status__isnull=True ).delete() reader = csv.reader(csvfile) col_key = csv_header_to_dict(next(reader)) line = 1 fin_obj, msg = get_fk(FinancialYear, fin_year) month_dict = get_month_dict() check_financial_code = CheckFinancialCode(None) csv_reader = csv.reader(csvfile, delimiter=",", quotechar='"') for row in csv_reader: line += 1 programme_code = row[col_key["programme"]].strip() cost_centre = row[col_key["cost centre"]].strip() nac = row[col_key["natural account"]].strip() analysis1 = row[col_key["analysis"]].strip() analysis2 = row[col_key["analysis2"]].strip() project_code = row[col_key["project"]].strip() check_financial_code.validate( cost_centre, nac, programme_code, analysis1, analysis2, project_code, line ) if check_financial_code.error_found: raise WrongChartOFAccountCodeException( f"Importing Forecast, Row {line} error: " f"{check_financial_code.display_error}" ) financialcode_obj = check_financial_code.get_financial_code() for month, per_obj in month_dict.items(): period_amount = Decimal(row[col_key[month.lower()]]) if period_amount: month_figure_obj, created = \ ForecastMonthlyFigure.objects.get_or_create( financial_year=fin_obj, financial_period=per_obj, financial_code=financialcode_obj, archived_status__isnull=True, ) if created: month_figure_obj.amount = period_amount * 100 else: month_figure_obj.amount += (period_amount * 100) month_figure_obj.current_amount = month_figure_obj.amount month_figure_obj.save() if (line % 100) == 0: logger.info(line) return True, "Import completed successfully."
def create_mi_previous_year_source_report(): previous_year = get_current_financial_year() - 1 title = \ f"MI {FinancialYear.objects.get(pk=previous_year).financial_year_display} " \ f"Report {today_string()}" fields = ForecastQueryFields(previous_year) queryset = fields.datamodel.view_data.raw_data_annotated( fields.MI_REPORT_DOWNLOAD_COLUMNS, year=previous_year) return export_to_excel(queryset, export_mi_iterator, title, fields)
def __init__(self, period=CURRENT_PERIOD): # period : between 0 and 15 it refers to the current financial year, # otherwise it contains the archived year to be used. self.current_year = period < 2000 self.period = period self._datamodel = None if self.current_year: self.selected_year = get_current_financial_year() else: self.selected_year = period
def setUp(self): self.client.force_login(self.test_user) self.cost_centre_code = 109076 cost_centre = CostCentreFactory( cost_centre_code=self.cost_centre_code, ) current_year = get_current_financial_year() self.amount_apr = -9876543 programme_obj = ProgrammeCodeFactory() self.programme_code = programme_obj.programme_code treasury_l5 = L5AccountFactory() nac_obj = NaturalCodeFactory(account_L5_code=treasury_l5) self.nac = nac_obj.natural_account_code project_obj = ProjectCodeFactory() self.project_code = project_obj.project_code year_obj = FinancialYear.objects.get(financial_year=current_year) apr_period = FinancialPeriod.objects.get(financial_period_code=1) apr_period.actual_loaded = True apr_period.save() # If you use the MonthlyFigureFactory the test fails. # I cannot work out why, it may be due to using a random year.... financial_code_obj = FinancialCode.objects.create( programme=programme_obj, cost_centre=cost_centre, natural_account_code=nac_obj, project_code=project_obj, ) financial_code_obj.save apr_figure = ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get( financial_period_code=1), financial_code=financial_code_obj, financial_year=year_obj, amount=self.amount_apr, ) apr_figure.save self.amount_may = 1234567 may_figure = ForecastMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get( financial_period_code=2, ), amount=self.amount_may, financial_code=financial_code_obj, financial_year=year_obj, ) may_figure.save # Assign download permission can_view_forecasts = Permission.objects.get( codename="can_download_mi_reports") self.test_user.user_permissions.add(can_view_forecasts) self.test_user.save() self.year_total = self.amount_apr + self.amount_may
def write_data(self, writer): current_year = get_current_financial_year() current_queryset = ( FCOMapping.objects.all().select_related( "account_L6_code_fk", "account_L6_code_fk__expenditure_category", "account_L6_code_fk__expenditure_category__NAC_category", ).order_by( "account_L6_code_fk__economic_budget_code", "account_L6_code_fk__expenditure_category__NAC_category__NAC_category_description", # noqa", "account_L6_code_fk__natural_account_code_description", "fco_code", )) historical_queryset = (ArchivedFCOMapping.objects.all().select_related( "financial_year").order_by( "economic_budget_code", "nac_category_description", "account_L6_description", "fco_code", )) for obj in current_queryset: if obj.account_L6_code_fk.expenditure_category: expenditure_category_value = ( obj.expenditure_category.grouping_description) NAC_category_value = (obj.expenditure_category.NAC_category. NAC_category_description) else: expenditure_category_value = None NAC_category_value = None row = [ obj.account_L6_code_fk.economic_budget_code, NAC_category_value, expenditure_category_value, obj.account_L6_code_fk.natural_account_code, obj.account_L6_code_fk.natural_account_code_description, obj.fco_code, obj.fco_description, current_year, ] writer.writerow(row) for obj in historical_queryset: row = [ obj.economic_budget_code, obj.nac_category_description, obj.budget_description, obj.account_L6_code, obj.account_L6_description, obj.fco_code, obj.fco_description, obj.financial_year.financial_year, ] writer.writerow(row)
def setUp(self): self.client.force_login(self.test_user) self.out = StringIO() self.commercial_category = "commercial category" self.description = "Longer description" CommercialCategoryFactory( commercial_category=self.commercial_category, description=self.description, ) current_year = get_current_financial_year() self.archive_year = current_year - 1
def setUp(self): self.client.force_login(self.test_user) self.out = StringIO() self.grouping_description = "grouping description" self.category_description = "Longer description" ExpenditureCategoryFactory( grouping_description=self.grouping_description, description=self.category_description, ) current_year = get_current_financial_year() self.archive_year = current_year - 1
def setUp(self): self.client.force_login(self.test_user) self.out = StringIO() self.analysis2_code = 123456 self.analysis2_description = "analysis2 description" Analysis2Factory( analysis2_code=self.analysis2_code, analysis2_description=self.analysis2_description, ) current_year = get_current_financial_year() self.archive_year = current_year - 1
def setUp(self): self.client.force_login(self.test_user) self.cost_centre_code = 109076 cost_centre = CostCentreFactory(cost_centre_code=self.cost_centre_code,) current_year = get_current_financial_year() self.amount_apr = -234567 self.amount_may = 345216 self.programme_obj = ProgrammeCodeFactory() nac_obj = NaturalCodeFactory() project_obj = ProjectCodeFactory() self.programme_code = project_obj.project_code year_obj = FinancialYear.objects.get(financial_year=current_year) # If you use the MonthlyFigureFactory the test fails. # I cannot work out why, it may be due to using a random year.... financial_code_obj = FinancialCode.objects.create( programme=self.programme_obj, cost_centre=cost_centre, natural_account_code=nac_obj, project_code=project_obj, ) financial_code_obj.save apr_figure = BudgetMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=1), financial_code=financial_code_obj, financial_year=year_obj, amount=self.amount_apr, ) apr_figure.save may_figure = BudgetMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=2,), amount=self.amount_may, financial_code=financial_code_obj, financial_year=year_obj, ) may_figure.save financial_code_obj1 = FinancialCode.objects.create( programme=self.programme_obj, cost_centre=cost_centre, natural_account_code=nac_obj, ) may_figure1 = BudgetMonthlyFigure.objects.create( financial_period=FinancialPeriod.objects.get(financial_period_code=2,), amount=self.amount_may, financial_code=financial_code_obj1, financial_year=year_obj, ) may_figure1.save self.year_total = self.amount_apr + self.amount_may
def write_data(self, writer): current_year = get_current_financial_year() current_queryset = (CostCentre.objects.filter( active=True).select_related("directorate").select_related( "deputy_director").select_related( "directorate__group").order_by( "directorate__group__group_code", "directorate__group__group_name", "directorate__directorate_code", "directorate__directorate_name", "cost_centre_code", )) historical_queryset = (ArchivedCostCentre.objects.filter( active=True).select_related("financial_year").order_by( "-financial_year", "group_code", "group_name", "directorate_code", "directorate_name", "cost_centre_code", )) for obj in current_queryset: row = [ obj.directorate.group.group_code, obj.directorate.group.group_name, obj.directorate.directorate_code, obj.directorate.directorate_name, obj.cost_centre_code, obj.cost_centre_name, obj.directorate.director, obj.directorate.group.director_general, obj.business_partner, obj.bsce_email, current_year, ] writer.writerow(row) for obj in historical_queryset: row = [ obj.group_code, obj.group_name, obj.directorate_code, obj.directorate_name, obj.cost_centre_code, obj.cost_centre_name, obj.director_fullname, obj.dg_fullname, obj.business_partner_fullname, obj.bsce_email, obj.financial_year.financial_year, ] writer.writerow(row)
def pivot_data(self, columns, filter_dict={}, year=0, order_list=[]): if year == 0: year = get_current_financial_year() q1 = (self.get_queryset().filter(financial_year=year, **filter_dict).order_by(*order_list)) pivot_data = pivot( q1, columns, "financial_period__period_short_name", "amount", ) return pivot_data
def validate_year_for_archiving(financial_year, exclude_current=True): current_year = get_current_financial_year() if exclude_current and financial_year == current_year: raise (ArchiveYearError(f"{financial_year} is the current year.")) if financial_year > current_year: raise (ArchiveYearError(f"{financial_year} is in the future.")) try: obj = FinancialYear.objects.get(pk=financial_year) except FinancialYear.DoesNotExist: raise (ArchiveYearError( f"Financial year {financial_year} does not exist.")) return obj
def setUp(self): self.client.force_login(self.test_user) self.out = StringIO() obj = ExpenditureCategoryFactory() self.grouping_description = obj.grouping_description current_year = get_current_financial_year() self.archive_year = current_year - 1 call_command( "archive", "Expenditure_Cat", year=self.archive_year, stdout=self.out, )
def get_budget(self, obj): budget = BudgetMonthlyFigure.objects.values( 'financial_code', 'financial_year', ).filter( financial_code=obj.id, financial_year_id=get_current_financial_year(), archived_status=None, ).annotate( yearly_amount=Sum('amount') ) if budget and "yearly_amount" in budget[0]: return budget[0]["yearly_amount"] else: return 0
def choose_year(request): current_year = get_current_financial_year() qs = FinancialYear.objects.filter(financial_year__lte=current_year).order_by( "-financial_year" ) chart_of_account_years = [] for q in qs: chart_of_account_years.append( {"year": q.financial_year, "display": q.financial_year_display} ) return render( request, "chartofaccountDIT/choose_year.html", {"chart_of_account_years": chart_of_account_years}, )
def end_of_month_archive(period_id): end_of_month_info = get_end_of_month(period_id) current_year = get_current_financial_year() # Add archive period to all the active forecast. # The actuals are not archived, because they don't change from one month to another forecast_periods = ForecastMonthlyFigure.objects.filter( financial_period__financial_period_code__gte=period_id, financial_year_id=current_year, archived_status__isnull=True, ) forecast_periods.update(archived_status=end_of_month_info) # Copy forecast just archived to current forecast # The current forecast is recognised by having archive period equal Null. # Use direct SQL for performance reason. # Simple history does not get updated, but it recovers # when changes are done to the record. # Note that initial amount is updated to be the current amount. forecast_sql = insert_query("forecast_forecastmonthlyfigure", end_of_month_info.id) with connection.cursor() as cursor: cursor.execute(forecast_sql) # Archive the budget. Use the same logic used for Forecast. budget_periods = BudgetMonthlyFigure.objects.filter( financial_period__financial_period_code__gte=period_id, archived_status__isnull=True, ) budget_periods.update(archived_status=end_of_month_info) budget_sql = insert_query("forecast_budgetmonthlyfigure", end_of_month_info.id) with connection.cursor() as cursor: cursor.execute(budget_sql) # Save the yearly total for the budgets. It makes the queries # used to display the forecast/budget much easier. budget_total_sql = insert_total_budget_query(end_of_month_info.id, period_id) with connection.cursor() as cursor: cursor.execute(budget_total_sql) end_of_month_info.archived = True end_of_month_info.archived_date = timezone.now() end_of_month_info.save()
def handle(self, *args, **options): financial_year = options.get("year") if financial_year == 0: financial_year = get_current_financial_year() archive_type = options.get("type") for arg in options[self.arg_name]: archive_type = arg if archive_type == "All": archive_all(financial_year) archive_cost_centre(financial_year) archive_treasury_l5(financial_year) self.stdout.write( self.style.SUCCESS("Archived full chart of account.")) else: row = ARCHIVE_TYPE[archive_type](financial_year) self.stdout.write( self.style.SUCCESS("Archived " + str(row) + " rows"))
def setUp(self): self.client.force_login(self.test_user) self.group_name = "Test Group" self.group_code = "TestGG" self.directorate_name = "Test Directorate" self.directorate_code = "TestDD" self.cost_centre_code = 109076 self.financial_year = FinancialYearFactory(financial_year=2019) self.name = "Test" self.surname = "FBP" self.bsce_email = "*****@*****.**" self.group = DepartmentalGroupFactory( group_code=self.group_code, group_name=self.group_name, ) self.directorate = DirectorateFactory( directorate_code=self.directorate_code, directorate_name=self.directorate_name, group=self.group, ) self.business_partner = FinanceBusinessPartnerFactory( name=self.name, surname=self.surname, ) self.bsce = BSCEFactory(bsce_email=self.bsce_email) self.cost_centre = CostCentreFactory( directorate=self.directorate, cost_centre_code=self.cost_centre_code, business_partner=self.business_partner, bsce_email=self.bsce, ) self.archive_cost_centre = ArchivedCostCentreFactory( financial_year=self.financial_year, cost_centre_code=self.cost_centre_code, ) current_year = get_current_financial_year() self.archive_year = current_year - 1
def check_after_successfull_command(self): assert ( BudgetMonthlyFigure.objects.filter(financial_year=self.current_year).count() == 0 ) assert ( ForecastMonthlyFigure.objects.filter( financial_year=self.current_year ).count() == 0 ) assert FinancialCode.objects.all().count() == 0 assert CostCentre.objects.all().count() != 0 assert ( ArchivedCostCentre.objects.filter(financial_year=self.current_year).count() != 0 ) current_year_after_command = get_current_financial_year() assert current_year_after_command != self.current_year