Sha256: 11b6dbd14369e10817c6f96c3592eaa2e5af78a2bd6c500ec3affeddf9aafe36

Contents?: true

Size: 1.81 KB

Versions: 3

Compression:

Stored size: 1.81 KB

Contents

module Spree
  class SalesTaxReport < Spree::Report
    HEADERS             = { zone_name: :string, sales_tax: :integer }
    SEARCH_ATTRIBUTES   = { start_date: :taxation_from, end_date: :taxation_till }
    SORTABLE_ATTRIBUTES = []

    class Result < Spree::Report::TimedResult
      charts MonthlySalesTaxComparisonChart

      def build_empty_observations
        super
        @_zones = @results.collect { |r| r['zone_name'] }.uniq
        @observations = @_zones.collect do |zone|
          @observations.collect do |observation|
            _d_observation = observation.dup
            _d_observation.zone_name = zone
            _d_observation.sales_tax = 0
            _d_observation
          end
        end.flatten
      end

      class Observation < Spree::Report::TimedObservation
        observation_fields [:zone_name, :sales_tax]

        def describes?(result, time_scale)
          (zone_name == result['zone_name']) && super
        end

        def sales_tax
          @sales_tax.to_f
        end
      end
    end


    def report_query
      Spree::Report::QueryFragments
        .from_subquery(tax_adjustments)
        .group(*time_scale_columns_to_s, 'zone_name')
        .order(*time_scale_columns)
        .project(
          'zone_name',
          *time_scale_columns,
          'SUM(sales_tax) as sales_tax'
        )
    end

    private def tax_adjustments
      Spree::TaxRate
        .joins(:adjustments)
        .joins(:zone)
        .where(spree_adjustments: { adjustable_type: 'Spree::LineItem' } )
        .where(spree_adjustments: { created_at: reporting_period })
        .select(
          'spree_adjustments.amount  as sales_tax',
          'spree_zones.id            as zone_id',
          'spree_zones.name          as zone_name',
          *time_scale_selects('spree_adjustments')
        )
    end

  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
spree_admin_insights-3.2.0 app/reports/spree/sales_tax_report.rb
solidus_admin_insights-2.1.1 app/reports/spree/sales_tax_report.rb
solidus_admin_insights-2.1.0 app/reports/spree/sales_tax_report.rb