Пример #1
0
def import_archived_analysis2(csvfile, year):
    msgerror = "Failure importing archived Analysis 2. Error:"
    try:
        validate_year_for_archiving(year)
    except ArchiveYearError as ex:
        raise ArchiveYearError(f"{msgerror} {str(ex)}")
    success, msg = import_obj(csvfile, ANALYSIS2_HISTORICAL_KEY, year=year)
    if not success:
        raise WrongChartOFAccountCodeException(f"{msgerror} {msg}")
    return success, msg
Пример #2
0
def import_archived_programme(csvfile, year):
    msgerror = "Failure importing archived Programme. Error:"
    try:
        validate_year_for_archiving(year)
    except ArchiveYearError as ex:
        raise ArchiveYearError(f"{msgerror} {str(ex)}")
    success, msg = import_obj(csvfile, PROGRAMME_HISTORICAL_KEY, year=year)
    if not success:
        raise WrongChartOFAccountCodeException(f"{msgerror} {msg}")
    return success, msg
Пример #3
0
def import_archived_cost_centre(csvfile, year):
    try:
        validate_year_for_archiving(year)
    except ArchiveYearError as ex:
        raise ArchiveYearError(
            f"Failure import Importing archived Cost Centre hierarchy  error: {str(ex)}"
        )
    success, msg = import_obj(csvfile, COST_CENTRE_HISTORICAL_KEY, year=year)
    if not success:
        raise WrongChartOFAccountCodeException(
            f"Importing archived Cost Centre hierarchy  error: "
            f"{msg}")
    return success, msg
Пример #4
0
def import_archived_nac(csvfile, year):
    msgerror = "Failure importing archived NAC. Error:"
    try:
        validate_year_for_archiving(year)
    except ArchiveYearError as ex:
        raise ArchiveYearError(f"{msgerror} {str(ex)}")
    success, msg = import_obj(csvfile, NAC_HISTORICAL_KEY, year=year)
    if not success:
        raise WrongChartOFAccountCodeException(f"{msgerror} {msg}")
    else:
        # The archived NAC is not normalised, and each expenditure NAC row
        # contains the corresponding budget NAC.
        # This information is missing from the upload file, so we need to extract it
        # from the expenditure category: each expenditure category row is linked to
        # a budget NAC.
        # The classification of budget and expenditure NAC is internal to DIT:
        # budgets should be set only using budget NAC, to reduce the number of lines
        nac_qs = ArchivedNaturalCode.objects.filter(financial_year=year)
        for nac_obj in nac_qs:
            if nac_obj.expenditure_category:
                account_L6_budget_val = nac_obj.expenditure_category.linked_budget_code
                nac_obj.account_L6_budget = account_L6_budget_val
                nac_obj.save()
        category_qs = ArchivedExpenditureCategory.objects.filter(financial_year=year)
        for category_obj in category_qs:
            account_L6_budget_val = category_obj.linked_budget_code
            try:
                nac_obj = ArchivedNaturalCode.objects.get(
                    financial_year=year, natural_account_code=account_L6_budget_val
                )
                nac_obj.used_for_budget = True
                nac_obj.save()
            except ObjectDoesNotExist:
                msg = f"NAC {account_L6_budget_val} not defined for year {year}"
                raise WrongChartOFAccountCodeException(f"{msgerror} {msg}")

        return success, msg
Пример #5
0
def import_single_archived_period(csvfile, month_to_upload, archive_period,
                                  fin_year):
    if month_to_upload <= archive_period:
        raise WrongArchivePeriodException(
            "You are trying to amend Actuals. Only forecast can be amended.")

    end_of_month_info = EndOfMonthStatus.objects.get(
        archived_period__financial_period_code=archive_period)
    if not end_of_month_info.archived:
        raise WrongArchivePeriodException(
            f"There is no archive for period {archive_period} ")

    period_obj = FinancialPeriod.objects.get(pk=month_to_upload)

    archive_period_id = end_of_month_info.id
    ActualUploadMonthlyFigure.objects.filter(
        financial_year=fin_year, financial_period=period_obj).delete()

    reader = csv.reader(csvfile)
    col_key = csv_header_to_dict(next(reader))

    row_number = 1
    fin_obj, msg = get_fk(FinancialYear, fin_year)
    period_obj = FinancialPeriod.objects.get(pk=month_to_upload)

    month_col = col_key[period_obj.period_short_name.lower()]
    check_financial_code = CheckFinancialCode(None)

    csv_reader = csv.reader(csvfile, delimiter=",", quotechar='"')
    for row in csv_reader:
        row_number += 1
        # protection against empty rows
        if len(row) == 0:
            break

        cost_centre = row[col_key["cost centre"]].strip()
        programme_code = row[col_key["programme"]].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,
            row_number,
        )

        if check_financial_code.error_found or check_financial_code.ignore_row:
            raise WrongChartOFAccountCodeException(
                f"Overwriting period, Row {row_number} error: "
                f"{check_financial_code.display_error}")

        financialcode_obj = check_financial_code.get_financial_code()
        period_amount = Decimal(row[month_col])
        if period_amount:
            month_figure_obj, created = ActualUploadMonthlyFigure.objects.get_or_create(
                financial_year=fin_obj,
                financial_period=period_obj,
                financial_code=financialcode_obj,
            )
            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 (row_number % 100) == 0:
            logger.info(row_number)

    logger.info(f"Completed processing  {row_number} rows.")
    # Now copy the newly uploaded figures to the monthly figure table
    ForecastMonthlyFigure.objects.filter(
        financial_year=fin_year,
        financial_period=period_obj,
        archived_status_id=archive_period_id,
    ).delete()
    sql_insert = sql_for_single_month_copy(month_to_upload, archive_period_id,
                                           fin_year)
    with connection.cursor() as cursor:
        cursor.execute(sql_insert)
    ForecastMonthlyFigure.objects.filter(
        financial_year=fin_year,
        financial_period=period_obj,
        amount=0,
        starting_amount=0,
        archived_status_id=archive_period_id,
    ).delete()
    ActualUploadMonthlyFigure.objects.filter(
        financial_year=fin_year, financial_period=period_obj).delete()