Sha256: 597f0c0a23f7346e45e3429c1039be11f97a3f7015319671559ddb07c6fa3818

Contents?: true

Size: 1.53 KB

Versions: 44

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

module MalawiHivProgramReports
  module Clinic
    # Generates a discrepancy report for a clinic
    class DiscrepancyReport
      def initialize(start_date:, end_date:, **_kwargs)
        @start_date = ActiveRecord::Base.connection.quote(start_date)
        @end_date = ActiveRecord::Base.connection.quote(end_date)
      end

      def find_report
        # TODO: Implement this
        discrepancy_report
      end

      private

      def discrepancy_report
        ActiveRecord::Base.connection.select_all <<~SQL
          SELECT
              pbi.drug_id,
              d.name,
              d.short_name,
              psv.verification_date,
              psv.reason as verification_reason,
              po_expected.quantity expected_quantity,
              po.quantity difference,
              po_expected.quantity + po.quantity as current_quantity,
              po.transaction_reason as variance_reason
          FROM pharmacy_stock_verifications psv
          INNER JOIN pharmacy_obs po ON po.stock_verification_id = psv.id AND po.voided = 0 AND po.obs_group_id IS NULL
          INNER JOIN pharmacy_batch_items pbi ON pbi.id = po.batch_item_id AND pbi.voided = 0
          INNER JOIN drug_cms d ON d.drug_inventory_id = pbi.drug_id AND d.voided = 0
          LEFT JOIN pharmacy_obs po_expected ON po_expected.obs_group_id = po.pharmacy_module_id AND po_expected.voided = 0
          WHERE psv.verification_date BETWEEN #{@start_date} AND #{@end_date}
          GROUP BY psv.id
        SQL
      end
    end
  end
end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
malawi_hiv_program_reports-1.1.18 app/services/malawi_hiv_program_reports/clinic/discrepancy_report.rb
malawi_hiv_program_reports-1.1.17 app/services/malawi_hiv_program_reports/clinic/discrepancy_report.rb
malawi_hiv_program_reports-1.1.16 app/services/malawi_hiv_program_reports/clinic/discrepancy_report.rb
malawi_hiv_program_reports-1.1.15 app/services/malawi_hiv_program_reports/clinic/discrepancy_report.rb
malawi_hiv_program_reports-1.1.14 app/services/malawi_hiv_program_reports/clinic/discrepancy_report.rb
malawi_hiv_program_reports-1.1.13 app/services/malawi_hiv_program_reports/clinic/discrepancy_report.rb
malawi_hiv_program_reports-1.1.12 app/services/malawi_hiv_program_reports/clinic/discrepancy_report.rb
malawi_hiv_program_reports-1.1.11 app/services/malawi_hiv_program_reports/clinic/discrepancy_report.rb
malawi_hiv_program_reports-1.1.10 app/services/malawi_hiv_program_reports/clinic/discrepancy_report.rb
malawi_hiv_program_reports-1.1.9 app/services/malawi_hiv_program_reports/clinic/discrepancy_report.rb
malawi_hiv_program_reports-1.1.8 app/services/malawi_hiv_program_reports/clinic/discrepancy_report.rb
malawi_hiv_program_reports-1.1.7 app/services/malawi_hiv_program_reports/clinic/discrepancy_report.rb
malawi_hiv_program_reports-1.1.6 app/services/malawi_hiv_program_reports/clinic/discrepancy_report.rb
malawi_hiv_program_reports-1.1.5 app/services/malawi_hiv_program_reports/clinic/discrepancy_report.rb
malawi_hiv_program_reports-1.1.4 app/services/malawi_hiv_program_reports/clinic/discrepancy_report.rb
malawi_hiv_program_reports-1.1.3 app/services/malawi_hiv_program_reports/clinic/discrepancy_report.rb
malawi_hiv_program_reports-1.1.2 app/services/malawi_hiv_program_reports/clinic/discrepancy_report.rb
malawi_hiv_program_reports-1.1.1 app/services/malawi_hiv_program_reports/clinic/discrepancy_report.rb
malawi_hiv_program_reports-1.1.0 app/services/malawi_hiv_program_reports/clinic/discrepancy_report.rb
malawi_hiv_program_reports-1.0.27 app/services/malawi_hiv_program_reports/clinic/discrepancy_report.rb