<commit_before>module Report
  class EconomyPerRefugeeStatus < Economy
    def create!
      @axlsx    = Axlsx::Package.new
      @workbook = @axlsx.workbook
      @style    = Style.new(@axlsx)

      add_sheet
      @axlsx.serialize File.join(Rails.root, 'reports', @filename)
    end

    def add_sheet(name = @sheet_name)
      @sheet = @workbook.add_worksheet(name: name)
      fill_sheet
    end

    def records
      RateCategory.includes(:rates).all
    end

    # Add a data row and a sub sheet for each rate category
    def data_rows
      records.each_with_index.map do |category, i|
        @sheet.add_hyperlink(
          location: "'#{i18n_name(category[:human_name])}'!A1", ref: "A#{i + 2}",
          target: :sheet
        )

        cols = columns(category, i).map do |cell|
          cell
        end

        add_sub_sheet(category)
        cols
      end
    end

    def add_sub_sheet(category)
      Report::EconomyPerRefugeeStatusSubSheets.new(
        sheet_name: i18n_name(category[:human_name]),
        rates: @rates,
        costs: @costs,
        payments: @payments,
        axlsx: @axlsx
      ).create!
    end

    def refugees(category)
      rates(category).uniq.map { |hash| hash[:refugee] }
    end

    def rates(category)
      @rates = Statistics::Rates.refugees_rates_for_category(category, @range)
    end

    def placements_within_range
      @_placements_within_range ||= begin
        Placement.includes(
          :refugee,
          home: :costs
        ).within_range(@from, @to)
      end
    end

    def costs(category)
      costs = refugees(category).map do |refugee|
        placements = refugee_placements_within_range(refugee)
        Statistics::Cost.placements_costs_and_days(placements, @range)
      end

      @costs = costs.flatten.map { |rate| rate[:amount] * rate[:days] }.sum
    end

    def payments(category)
      @payments = refugees(category).map do |refugee|
        Statistics::Payment.amount_and_days(refugee.payments, @range)
      end.flatten
    end

    def columns(category = RateCategory.new, i = 0)
      row = i + 2
      [
        {
          heading: 'Barnens status',
          query: category.human_name
        },
        {
          heading: 'Budgeterad kostnad',
          query: costs(category)
        },
        {
          heading: 'Förväntad schablon',
          query: rates(category).map { |rate| rate[:amount] * rate[:days] }.sum
        },
        {
          heading: 'Avvikelse',
          query: "=C#{row}-B#{row}"
        },
        {
          heading: 'Utbetald schablon',
          query: payments(category).map { |payment| payment[:amount] * payment[:days] }.sum
        },
        {
          heading: 'Avvikelse mellan förväntad och utbetald schablon',
          query: "=E#{row}-C#{row}"
        }
      ]
    end

    def last_row(row_number)
      [
        'SUMMA:',
        "=SUM(B2:B#{row_number})",
        "=SUM(C2:C#{row_number})",
        "=SUM(D2:D#{row_number})",
        "=SUM(E2:E#{row_number})",<commit_msg>Fixed bug in costs query<commit_after>module Report
  class EconomyPerRefugeeStatus < Economy
    def create!
      @axlsx    = Axlsx::Package.new
      @workbook = @axlsx.workbook
      @style    = Style.new(@axlsx)

      add_sheet
      @axlsx.serialize File.join(Rails.root, 'reports', @filename)
    end

    def add_sheet(name = @sheet_name)
      @sheet = @workbook.add_worksheet(name: name)
      fill_sheet
    end

    def records
      RateCategory.includes(:rates).all
    end

    # Add a data row and a sub sheet for each rate category
    def data_rows
      records.each_with_index.map do |category, i|
        @sheet.add_hyperlink(
          location: "'#{i18n_name(category[:human_name])}'!A1", ref: "A#{i + 2}",
          target: :sheet
        )

        cols = columns(category, i).map do |cell|
          cell
        end

        add_sub_sheet(category)
        cols
      end
    end

    def add_sub_sheet(category)
      Report::EconomyPerRefugeeStatusSubSheets.new(
        sheet_name: i18n_name(category[:human_name]),
        rates: @rates,
        costs: @costs,
        payments: @payments,
        axlsx: @axlsx
      ).create!
    end

    def refugees(category)
      rates(category).uniq.map { |hash| hash[:refugee] }
    end

    def rates(category)
      @rates = Statistics::Rates.refugees_rates_for_category(category, @range)
    end

    def placements_within_range
      @_placements_within_range ||= begin
        Placement.includes(
          :refugee,
          home: :costs
        ).within_range(@from, @to)
      end
    end

    def costs(category)
      costs = refugees(category).map do |refugee|
        placements = refugee_placements_within_range(refugee)
        Statistics::Cost.placements_costs_and_days(placements, @range)
      end

      @costs = costs.flatten
    end

    def payments(category)
      @payments = refugees(category).map do |refugee|
        Statistics::Payment.amount_and_days(refugee.payments, @range)
      end.flatten
    end

    def columns(category = RateCategory.new, i = 0)
      row = i + 2
      [
        {
          heading: 'Barnens status',
          query: category.human_name
        },
        {
          heading: 'Budgeterad kostnad',
          query: costs(category).map { |cost| cost[:amount] * cost[:days] }.sum
        },
        {
          heading: 'Förväntad schablon',
          query: rates(category).map { |rate| rate[:amount] * rate[:days] }.sum
        },
        {
          heading: 'Avvikelse',
          query: "=C#{row}-B#{row}"
        },
        {
          heading: 'Utbetald schablon',
          query: payments(category).map { |payment| payment[:amount] * payment[:days] }.sum
        },
        {
          heading: 'Avvikelse mellan förväntad och utbetald schablon',
          query: "=E#{row}-C#{row}"
        }
      ]
    end

    def last_row(row_number)
      [
        'SUMMA:',
        "=SUM(B2:B#{row_number})",
        "=SUM(C2:C#{row_number})",
        "=SUM(D2:D#{row_number})",
        "=SUM(E2:E#{row_number})",
{'fraction_non_alphanumeric': 0.12111239098239263, 'fraction_numerical': 0.003949317097251934, 'mean_word_length': 2.112135176651306, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2756497', 'n_tokens_mistral': 2044, 'n_tokens_neox': 1985, 'n_words': 387}