Browse Source

CLDC-4326: initial alignment

CLDC-4326-align-duplicate-logs-checks
Nat Dean-Lewis 1 week ago
parent
commit
e15fe3b772
  1. 39
      app/models/sales_log.rb

39
app/models/sales_log.rb

@ -85,15 +85,34 @@ class SalesLog < Log
} }
scope :after_date, ->(date) { where("saledate >= ?", date) } scope :after_date, ->(date) { where("saledate >= ?", date) }
scope :duplicate_sets, lambda { |assigned_to_id = nil| scope :duplicate_sets_2025_and_earlier, lambda { |assigned_to_id = nil|
scope = visible scope = visible
.group(*DUPLICATE_LOG_ATTRIBUTES) .filter_by_year_or_earlier(2025)
.where.not(saledate: nil) .group(*DUPLICATE_LOG_ATTRIBUTES)
.sex1_answered .where.not(saledate: nil)
.address_answered .where.not(sex1: nil)
.age1_answered .address_answered
.ecstat1_answered .age1_answered
.having("COUNT(*) > 1") .ecstat1_answered
.having("COUNT(*) > 1")
if assigned_to_id
scope = scope.having("MAX(CASE WHEN assigned_to_id = ? THEN 1 ELSE 0 END) >= 1", assigned_to_id)
end
scope.pluck("ARRAY_AGG(id)")
}
scope :duplicate_sets_2026_and_later, lambda { |assigned_to_id = nil|
scope = visible
.filter_by_year_or_later(2026)
.group(*DUPLICATE_LOG_ATTRIBUTES)
.where.not(saledate: nil)
.where.not(sexrab1: nil)
.address_answered
.age1_answered
.ecstat1_answered
.having("COUNT(*) > 1")
if assigned_to_id if assigned_to_id
scope = scope.having("MAX(CASE WHEN assigned_to_id = ? THEN 1 ELSE 0 END) >= 1", assigned_to_id) scope = scope.having("MAX(CASE WHEN assigned_to_id = ? THEN 1 ELSE 0 END) >= 1", assigned_to_id)
@ -102,6 +121,10 @@ class SalesLog < Log
scope.pluck("ARRAY_AGG(id)") scope.pluck("ARRAY_AGG(id)")
} }
scope :duplicate_sets, lambda { |assigned_to_id = nil|
duplicate_sets_2025_and_earlier(assigned_to_id) + duplicate_sets_2026_and_later(assigned_to_id)
}
OPTIONAL_FIELDS = %w[purchid othtype buyers_organisations].freeze OPTIONAL_FIELDS = %w[purchid othtype buyers_organisations].freeze
DUPLICATE_LOG_ATTRIBUTES = %w[owning_organisation_id purchid saledate age1_known age1 sex1 sexrab1 ecstat1 postcode_full uprn address_line1].freeze DUPLICATE_LOG_ATTRIBUTES = %w[owning_organisation_id purchid saledate age1_known age1 sex1 sexrab1 ecstat1 postcode_full uprn address_line1].freeze

Loading…
Cancel
Save