def export_forecast_data_project_detail_dit(request, project_code_id, period): fields = ForecastQueryFields(period) filter = { fields.project_code_field: f"{project_code_id}", } title = f"DIT {project_code_id} {get_period_for_title(period)}" return export_forecast_data_generic(period, filter, title)
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 export_forecast_data_generic(period, data_filter, title): fields = ForecastQueryFields(period) year = fields.selected_year datamodel = fields.datamodel q = datamodel.view_data.raw_data_annotated( fields.VIEW_FORECAST_DOWNLOAD_COLUMNS, data_filter, year=year) return export_query_to_excel(q, fields.VIEW_FORECAST_DOWNLOAD_COLUMNS, title, period)
def export_forecast_data_expenditure_dit(request, expenditure_category_id, budget_type_id, period): fields = ForecastQueryFields(period) filter = { fields.budget_category_id_field: f"{expenditure_category_id}", fields.budget_type_field: f"{budget_type_id}", } title = f"DIT {get_period_for_title(period)} Expenditure" return export_forecast_data_generic(period, filter, title)
def export_forecast_data_expenditure_detail_cost_centre( request, cost_centre, expenditure_category_id, budget_type_id, period): fields = ForecastQueryFields(period) filter = { fields.cost_centre_code_field: cost_centre, fields.budget_category_id_field: f"{expenditure_category_id}", fields.budget_type_field: f"{budget_type_id}", } title = f"{cost_centre} {get_period_for_title(period)} Expenditure" return export_forecast_data_generic(period, filter, title)
def export_forecast_data_programme_detail_dit(request, programme_code_id, forecast_expenditure_type_name, period): fields = ForecastQueryFields(period) filter = { fields.programme_code_field: f"{programme_code_id}", fields.expenditure_type_name_field: f"{forecast_expenditure_type_name}", } title = f"DIT {programme_code_id} {get_period_for_title(period)}" return export_forecast_data_generic(period, filter, title)
def export_edit_forecast_data(request, cost_centre): fields = ForecastQueryFields() if can_edit_cost_centre(request.user, cost_centre): filter = {fields.cost_centre_code_field: cost_centre} q = ForecastingDataView.view_data.raw_data_annotated( { **fields.EDIT_KEYS_DOWNLOAD, **fields.EDIT_FORECAST_DOWNLOAD_COLUMNS }, filter, order_list=fields.EDIT_FORECAST_DOWNLOAD_ORDER, include_zeros=True, ) title = f"Edit forecast {cost_centre}" return export_edit_to_excel(q, fields.EDIT_KEYS_DOWNLOAD, fields.EDIT_FORECAST_DOWNLOAD_COLUMNS, title) else: return redirect(reverse( "forecast_dit", kwargs={ "period": 0, }, ))
def archive_current_year(): # Get the latest period for archiving fields = ForecastQueryFields() financial_year = fields.selected_year try: validate_year_for_archiving_actuals(financial_year, False) except ArchiveYearError as ex: raise ex datamodel = fields.datamodel data_to_archive_list = datamodel.view_data.raw_data_annotated( fields.archive_forecast_columns, {}, year=financial_year) financial_year_obj = FinancialYear.objects.get(pk=financial_year) # Clear the table used to upload the previous_years. # The previous_years are uploaded to to a temporary storage, # and copied when the upload is completed successfully. # This means that we always have a full upload. ArchivedForecastDataUpload.objects.filter( financial_year=financial_year, ).delete() rows_to_process = data_to_archive_list.count() # Create an entry in the file upload table, even if it is not a file. # It is useful for keeping the log of errors file_upload = FileUpload(document_file_name="dummy", document_type=FileUpload.PREVIOUSYEAR, file_location=FileUpload.LOCALFILE, status=FileUpload.PROCESSING) check_financial_code = CheckArchivedFinancialCode(financial_year, file_upload) row_number = 0 cost_centre_field = fields.cost_centre_code_field nac_field = fields.nac_code_field programme_field = fields.programme_code_field analysis1_field = fields.analysis1_code_field analysis2_field = fields.analysis2_code_field project_code_field = fields.project_code_field for row_to_archive in data_to_archive_list: row_number += 1 if not row_number % 100: # Display the number of rows processed every 100 rows set_file_upload_feedback( file_upload, f"Processing row {row_number} of {rows_to_process}.") logger.info(f"Processing row {row_number} of {rows_to_process}.") cost_centre = row_to_archive[cost_centre_field] nac = row_to_archive[nac_field] programme_code = row_to_archive[programme_field] analysis1 = row_to_archive[analysis1_field] analysis2 = row_to_archive[analysis2_field] project_code = row_to_archive[project_code_field] check_financial_code.validate( cost_centre, nac, programme_code, analysis1, analysis2, project_code, row_number, ) if not check_financial_code.error_found: financialcode_obj = check_financial_code.get_financial_code() try: archive_to_temp_previous_year_figures( row_to_archive, financial_year_obj, financialcode_obj, ) except (UploadFileFormatError, ArchiveYearError) as ex: set_file_upload_fatal_error( file_upload, str(ex), str(ex), ) raise ex final_status = set_final_status(check_financial_code) if final_status != FileUpload.PROCESSEDWITHERROR: # No errors, so we can copy the figures # from the temporary table to the previous_years copy_previous_year_figure_from_temp_table(financial_year) set_file_upload_feedback(file_upload, f"Processed {rows_to_process} rows.", final_status) if final_status == FileUpload.PROCESSEDWITHERROR: raise UploadFileDataError( "No data archived. Check the log in the file upload record.")
def list(self, request): # get relevant financial periods actual_periods_qs = FinancialPeriod.objects.filter( actual_loaded=True ).values_list("financial_period_code", flat=True) # add current month to period list actual_periods = [0, ] + list(actual_periods_qs) response = HttpResponse(content_type="text/csv") response["Content-Disposition"] = "attachment; filename=forecast.csv" writer = csv.writer(response, csv.excel) # Titles writer.writerow([ "Group name", "Group code", "Directorate name", "Directorate code", "Cost Centre name", "Cost Centre code", "Budget grouping", "Expenditure type" "Expenditure type description", "Budget Type", "Budget category", "Budget category", "Budget/Forecast NAC", "Budget/Forecast NAC description", "PO/Actual NAC", "Natural Account code description", "NAC Expenditure type", "Programme code", "Programme code description", "Contract code", "Contract description", "Market code", "Market description", "Project code", "Project description", "Budget", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan", "Feb", "Mar", "Adj1", "Adj2", "Adj3", "Period", "Year", ]) period_list = FinancialPeriod.financial_period_info.period_display_list() current_year = get_current_financial_year() for forecast_period in actual_periods: fields = ForecastQueryFields(forecast_period) data_model = forecast_budget_view_model[forecast_period] period_query = data_model.view_data.raw_data_annotated( fields.VIEW_FORECAST_DOWNLOAD_COLUMNS, ) for data_row in forecast_query_iterator( period_query, fields.VIEW_FORECAST_DOWNLOAD_COLUMNS, period_list, forecast_period, current_year, ): writer.writerow(data_row) return response
def create_mi_source_report(): title = f"MI Report {today_string()}" fields = ForecastQueryFields() queryset = fields.datamodel.view_data.raw_data_annotated( fields.MI_REPORT_DOWNLOAD_COLUMNS) return export_to_excel(queryset, export_mi_iterator, title, fields)
def create_mi_budget_report(): title = f"MI Budget {today_string()}" fields = ForecastQueryFields() queryset = BudgetMonthlyFigure.pivot.pivot_data( fields.MI_REPORT_DOWNLOAD_COLUMNS, {"archived_status__isnull": True}) return export_to_excel(queryset, export_mi_iterator, title, fields)
def export_forecast_data_cost_centre(request, cost_centre, period): fields = ForecastQueryFields(period) filter = {fields.cost_centre_code_field: cost_centre} title = f"{cost_centre} {get_period_for_title(period)}" return export_forecast_data_generic(period, filter, title)
def export_forecast_data_directorate(request, directorate_code, period): fields = ForecastQueryFields(period) filter = {fields.directorate_code_field: directorate_code} title = f"{directorate_code} {get_period_for_title(period)}" return export_forecast_data_generic(period, filter, title)
def export_forecast_data_group(request, group_code, period): fields = ForecastQueryFields(period) filter = {fields.group_code_field: group_code} title = f"{group_code} {get_period_for_title(period)}" return export_forecast_data_generic(period, filter, title)
def field_infos(self): if self._field_infos is None: self._field_infos = ForecastQueryFields(self.period) return self._field_infos