Module: EffectiveCpdAudit

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/effective_cpd_audit.rb

Overview

EffectiveCpdAudit

Mark your owner model with effective_cpd_audit to get all the includes

Defined Under Namespace

Modules: Base, ClassMethods

Instance Method Summary collapse

Instance Method Details

#anonymous?Boolean



302
303
304
# File 'app/models/concerns/effective_cpd_audit.rb', line 302

def anonymous?
  cpd_audit_level&.anonymous?
end

#assign_anonymous_name_and_numberObject

The name pattern is A23XXX where XXX is an autoincrement The name pattern is A23XXX where XXX is an autoincrement



603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# File 'app/models/concerns/effective_cpd_audit.rb', line 603

def assign_anonymous_name_and_number
  return if anonymous_name.present? || anonymous_number.present?
  return if cpd_audit_level.blank?

  prefix = cpd_audit_level.anonymous_audits_prefix
  raise('expected cpd audit level to have an anonymous prefix') unless prefix.present?

  # Where starts with prefix
  number = (self.class.all.where('anonymous_name LIKE ?', "#{prefix}%").maximum('anonymous_number') || 0) + 1 # The next number

  # Apply prefix and pad number to 3 digits
  name = prefix + number.to_s.rjust(3, '0')

  assign_attributes(anonymous_number: number, anonymous_name: name)
end

#close!Object

Admin action



556
557
558
559
# File 'app/models/concerns/effective_cpd_audit.rb', line 556

def close!
  closed!
  send_email(:cpd_audit_closed)
end

#complete!Object



508
509
510
511
512
513
514
515
516
517
518
# File 'app/models/concerns/effective_cpd_audit.rb', line 508

def complete!
  raise('audit must have been submitted to complete!') unless 

  assign_attributes(missing_info_reason: nil)
  completed!

  # Each of these sends a cpd_audit_review_ready email
  cpd_audit_reviews.each { |cpd_audit_review| cpd_audit_review.ready! }

  true
end

#completed_requirementsObject

When an audit is submitted, these must be done to go to completed. An Admin can override this and just set them to completed.



498
499
500
# File 'app/models/concerns/effective_cpd_audit.rb', line 498

def completed_requirements
  {}
end

#cpd_audit_level_section(wizard_step) ⇒ Object



371
372
373
374
# File 'app/models/concerns/effective_cpd_audit.rb', line 371

def cpd_audit_level_section(wizard_step)
  position = (wizard_step.to_s.split('section').last.to_i rescue false)
  cpd_audit_level.cpd_audit_level_sections.find { |section| (section.position + 1) == position }
end

#cpd_audit_response(cpd_audit_level_question) ⇒ Object

Find or build



377
378
379
380
# File 'app/models/concerns/effective_cpd_audit.rb', line 377

def cpd_audit_response(cpd_audit_level_question)
  cpd_audit_response = cpd_audit_responses.find { |r| r.cpd_audit_level_question_id == cpd_audit_level_question.id }
  cpd_audit_response ||= cpd_audit_responses.build(cpd_audit: self, cpd_audit_level_question: cpd_audit_level_question)
end

#deadline_dateObject



306
307
308
# File 'app/models/concerns/effective_cpd_audit.rb', line 306

def deadline_date
  (extension_date || notification_date)
end

#deadline_to_conflict_of_interestObject



570
571
572
573
574
575
576
# File 'app/models/concerns/effective_cpd_audit.rb', line 570

def deadline_to_conflict_of_interest
  return nil unless cpd_audit_level&.conflict_of_interest?
  return nil unless cpd_audit_level.days_to_declare_conflict.present?

  date = (notification_date || created_at || Time.zone.now)
  EffectiveResources.advance_date(date, business_days: cpd_audit_level.days_to_declare_conflict)
end

#deadline_to_exemptionObject



578
579
580
581
582
583
584
# File 'app/models/concerns/effective_cpd_audit.rb', line 578

def deadline_to_exemption
  return nil unless cpd_audit_level&.can_request_exemption?
  return nil unless cpd_audit_level.days_to_request_exemption.present?

  date = (notification_date || created_at || Time.zone.now)
  EffectiveResources.advance_date(date, business_days: cpd_audit_level.days_to_request_exemption)
end

#deadline_to_extensionObject



586
587
588
589
590
591
592
# File 'app/models/concerns/effective_cpd_audit.rb', line 586

def deadline_to_extension
  return nil unless cpd_audit_level&.can_request_extension?
  return nil unless cpd_audit_level.days_to_request_extension.present?

  date = (notification_date || created_at || Time.zone.now)
  EffectiveResources.advance_date(date, business_days: cpd_audit_level.days_to_request_extension)
end

#deadline_to_submitObject



594
595
596
597
598
599
# File 'app/models/concerns/effective_cpd_audit.rb', line 594

def deadline_to_submit
  return nil unless cpd_audit_level&.days_to_submit.present?

  date = (extension_date || notification_date || created_at || Time.zone.now)
  EffectiveResources.advance_date(date, business_days: cpd_audit_level.days_to_submit)
end

#deny_exemption!Object



423
424
425
426
427
# File 'app/models/concerns/effective_cpd_audit.rb', line 423

def deny_exemption!
  assign_attributes(exemption_request: false)
  exemption_denied!
  send_email(:cpd_audit_exemption_denied)
end

#deny_extension!Object



456
457
458
459
460
# File 'app/models/concerns/effective_cpd_audit.rb', line 456

def deny_extension!
  assign_attributes(extension_request: false)
  extension_denied!
  send_email(:cpd_audit_extension_denied)
end

#done?Boolean



318
319
320
# File 'app/models/concerns/effective_cpd_audit.rb', line 318

def done?
  self.class.done_states.include?(status.to_sym)
end

#draft?Boolean



310
311
312
# File 'app/models/concerns/effective_cpd_audit.rb', line 310

def draft?
  ! && !closed?
end

#email_form_defaults(action) ⇒ Object



561
562
563
# File 'app/models/concerns/effective_cpd_audit.rb', line 561

def email_form_defaults(action)
  { from: EffectiveCpd.mailer_sender }
end

#exemption!Object

Auditee wizard action



400
401
402
403
404
405
# File 'app/models/concerns/effective_cpd_audit.rb', line 400

def exemption!
  return started! unless exemption_request?

  update!(status: :exemption_requested)
  send_email(:cpd_audit_exemption_request)
end

#extension!Object

Auditee wizard action



430
431
432
433
434
435
# File 'app/models/concerns/effective_cpd_audit.rb', line 430

def extension!
  return started! unless extension_request?

  update!(status: :extension_requested)
  send_email(:cpd_audit_extension_request)
end

#grant_exemption!Object



417
418
419
420
421
# File 'app/models/concerns/effective_cpd_audit.rb', line 417

def grant_exemption!
  wizard_steps[:submit] ||= Time.zone.now
   && exemption_granted!
  send_email(:cpd_audit_exemption_granted)
end

#grant_extension!Object



447
448
449
450
451
452
453
454
# File 'app/models/concerns/effective_cpd_audit.rb', line 447

def grant_extension!
  self.extension_date = extension_request_date
  self.due_date = deadline_to_submit()

  cpd_audit_reviews.each { |cpd_audit_review| cpd_audit_review.extension_granted! }
  extension_granted!
  send_email(:cpd_audit_extension_granted)
end

#in_progress?Boolean



314
315
316
# File 'app/models/concerns/effective_cpd_audit.rb', line 314

def in_progress?
  self.class.done_states.include?(status.to_sym) == false
end

#missing!Object



520
521
522
523
524
525
# File 'app/models/concerns/effective_cpd_audit.rb', line 520

def missing!
  raise('audit must have been submitted to missing!') unless 

  missing_info!
  send_email(:cpd_audit_missing_info)
end

#nameObject



298
299
300
# File 'app/models/concerns/effective_cpd_audit.rb', line 298

def name
  anonymous_name.presence || user.to_s
end

#process_exemption!Object

Admin action



408
409
410
411
412
413
414
415
# File 'app/models/concerns/effective_cpd_audit.rb', line 408

def process_exemption!
  case admin_process_request
  when 'Granted' then grant_exemption!
  when 'Denied' then deny_exemption!
  else
    self.errors.add(:admin_process_request, "can't be blank"); save!
  end
end

#process_extension!Object

Admin action



438
439
440
441
442
443
444
445
# File 'app/models/concerns/effective_cpd_audit.rb', line 438

def process_extension!
  case admin_process_request
  when 'Granted' then grant_extension!
  when 'Denied' then deny_extension!
  else
    self.errors.add(:admin_process_request, "can't be blank"); save!
  end
end

#ready_to_review?Boolean



322
323
324
# File 'app/models/concerns/effective_cpd_audit.rb', line 322

def ready_to_review?
  was_completed?
end

#required_cpd_cycleObject



473
474
475
476
477
478
# File 'app/models/concerns/effective_cpd_audit.rb', line 473

def required_cpd_cycle
  @required_cpd_cycle ||= begin
    last_year = ((notification_date || created_at || Time.zone.now) - 1.year).all_year
    Effective::CpdCycle.available.where(start_at: last_year).first
  end
end

#resolve_conflict!Object

Admin action



388
389
390
391
392
393
394
395
396
397
# File 'app/models/concerns/effective_cpd_audit.rb', line 388

def resolve_conflict!
  wizard_steps[:conflict] = nil   # Have them complete the conflict step again.

  assign_attributes(conflict_of_interest: false, conflict_of_interest_reason: nil)
  conflicted_resolved!
  

  send_email(:cpd_audit_conflict_resolved)
  true
end

#resubmit!Object



527
528
529
530
531
532
533
534
535
# File 'app/models/concerns/effective_cpd_audit.rb', line 527

def resubmit!
  raise('audit must have been submitted and missing info to resubmit!') unless  && was_missing_info?
  raise('already submitted') if 

  assign_attributes(skip_to_step: :submitted, submitted_at: Time.zone.now)

  
  send_email(:cpd_audit_submitted)
end

#review!Object

Called in a before_save. Intended for applicant_review to call in its submit! method



546
547
548
549
550
551
552
553
# File 'app/models/concerns/effective_cpd_audit.rb', line 546

def review!
  raise('already reviewed') if was_reviewed?
  raise('audit must have been submitted to review!') unless 
  raise('audit must have been completed to review!') unless was_completed?

  reviewed!
  send_email(:cpd_audit_reviewed)
end

#send_email(email) ⇒ Object



565
566
567
568
# File 'app/models/concerns/effective_cpd_audit.rb', line 565

def send_email(email)
  EffectiveCpd.send_email(email, self, email_form_params) unless email_form_skip?
  true
end

#start!Object

Auditee wizard action



383
384
385
# File 'app/models/concerns/effective_cpd_audit.rb', line 383

def start!
  started!
end

#status_labelObject



326
327
328
# File 'app/models/concerns/effective_cpd_audit.rb', line 326

def status_label
  (status_was || status).to_s.gsub('_', ' ')
end

#submit!Object

Auditee wizard action



481
482
483
484
485
486
487
488
489
490
491
492
493
494
# File 'app/models/concerns/effective_cpd_audit.rb', line 481

def submit!
  # Complete the very last step too
  wizard_steps[:submitted] = Time.zone.now

  if conflict_of_interest?
    conflicted!
    send_email(:cpd_audit_conflicted)
  else
    
    send_email(:cpd_audit_submitted)
  end

  true
end

#summaryObject



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'app/models/concerns/effective_cpd_audit.rb', line 330

def summary
  case status_was
  when 'opened'
    "The audit has been opened. The auditee and audit reviewers have been notified. Waiting for the auditee to submit their audit questionnaire."
  when 'started'
    "The auditee has begun their audit questionnaire. Waiting for the auditee to submit their audit questionnaire."
  when 'conflicted'
    "The auditee has declared a conflict of interest. A new reviewer will be assigned."
  when 'conflicted_resolved'
    "The auditee had declared a conflict of interest. This has been resolved. Waiting for the auditee to submit their audit questionnaire."
  when 'exemption_requested'
    "The auditee has requested an exemption. Waiting for request to be granted or denied."
  when 'exemption_granted'
    "The exemption request has been granted. This audit may now be closed."
  when 'exemption_denied'
    "The exemption request has been denied. The audit will continue. Waiting for the auditee to submit their audit questionnaire."
  when 'extension_requested'
    "The auditee has requested an extension. Waiting for request to be granted or denied."
  when 'extension_granted'
    "The extension request has been granted. There is a new deadline. Waiting for the auditee to submit their audit questionnaire."
  when 'extension_denied'
    "The extension request has been denied. The deadline remains. Waiting for the auditee to submit their audit questionnaire."
  when 'submitted'
    summary = "Auditee has submitted their audit wizard."
    tasks = "The following tasks remain before it can be completed:"
    approval = "Waiting on complete, review and determination."
    items = completed_requirements.map { |item, done| "<li>#{item}: #{done ? 'Complete' : 'Incomplete'}</li>" }.join
    completed_requirements.present? ? "<p>#{summary} #{tasks}</p><ul>#{items}</ul>" : "#{summary} #{approval}"
  when 'completed'
    "All required materials have been provided. This audit will transition to 'reviewed' after all reviewers have finished."
  when 'missing_info'
    "Missing the following information: <ul><li>#{missing_info_reason}</li></ul>"
  when 'reviewed'
    "The audit has been reviewed and is ready for the final determination to be made."
  when 'closed'
    "This audit has been closed with a final determination #{determination}. All done."
  else
    raise("unexpected status #{status}")
  end.html_safe
end

#to_sObject



294
295
296
# File 'app/models/concerns/effective_cpd_audit.rb', line 294

def to_s
  (cpd_audit_level.present? && name.present?) ? "#{cpd_audit_level} Audit of #{name}" : 'audit'
end

#try_complete!Object

called by a before_save when submitted



503
504
505
506
# File 'app/models/concerns/effective_cpd_audit.rb', line 503

def try_complete!
  # complete! if submitted? # Automatically go from submitted->complete
  false # Nothing to do. Admin completes audits.
end

#try_review!Object



537
538
539
540
541
542
543
# File 'app/models/concerns/effective_cpd_audit.rb', line 537

def try_review!
  return false unless 
  return false unless completed?
  return false unless cpd_audit_reviews.present? && cpd_audit_reviews.all?(&:completed?)

  review!
end

#user_cpd_completed?Boolean



468
469
470
471
# File 'app/models/concerns/effective_cpd_audit.rb', line 468

def user_cpd_completed?
  return true if required_cpd_cycle.blank?
  user.cpd_statements.any? { |s| s.completed? && s.cpd_cycle_id == required_cpd_cycle.id }
end

#user_cpd_required?Boolean

Require CPD step



463
464
465
466
# File 'app/models/concerns/effective_cpd_audit.rb', line 463

def user_cpd_required?
  return false unless user.cpd_audit_cpd_required?
  required_cpd_cycle.present?
end