Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
walkthrough.rb
class WalkThroughReport < Report
attr_accessor :facility, :walk_through_table
WalkThroughLine = Struct.new :unit, :unit_status, :tenant, :balance, :paid_through_date, :delinquency_stage, :overlock_event, :maintenance
def pdf_options
{orientation: :landscape}
end
def run
create_walk_thru_table
end
private
def create_walk_thru_table
self.walk_through_table = facility.units.without_deleted.includes(:current_tenant, :current_ledger, :events).in_walk_thru_order.collect do |unit|
create_rent_roll_line unit
end
end
def create_rent_roll_line unit
line = WalkThroughLine.new unit
line.tenant = unit.current_tenant
if unit.current_ledger
current_ledger = unit.current_ledger
line.balance = current_ledger.account_balance_on start_date.end_of_day
line.paid_through_date = current_ledger.paid_through
end
if unit.current_tenant
delinquency_stage_tenant_events = unit.ordered_active_non_recurring_delinquency_stage_tenant_events
line.delinquency_stage = delinquency_stage_tenant_events[-1].delinquency_stage if delinquency_stage_tenant_events.length > 0
end
line.unit_status = occupancy_status unit
line.overlock_event = overlock_event unit
line.maintenance = maintenance_status unit
line
end
def overlock_event unit
table_name = joined_table_name = Event.quoted_table_name
joined_table_alias = "PARENT_EVENTS"
unit.events.joins("INNER JOIN #{joined_table_name} AS #{joined_table_alias} ON #{table_name}.PARENT_EVENT_ID = #{joined_table_alias}.ID AND #{joined_table_alias}.RESOLVED IS FALSE").where(:type => OverlockDelinquencyTenantEvent).order(:created_at).last
end
def maintenance_status unit
status = []
status << 'Damaged' if unit.damaged
status << 'Dirty' if unit.dirty
status.join('/')
end
def occupancy_status unit
unless unit.rentable
'Unrentable'
else
unit.status
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment