Class: Ecom::Core::OvertimeSheet
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Ecom::Core::OvertimeSheet
- Defined in:
- app/models/ecom/core/overtime_sheet.rb
Class Method Summary collapse
-
.create_new(date, site_id) ⇒ Object
Overtime sheet should be created using the method below only.
- .exists_for_date?(date, site_id) ⇒ Boolean
- .open_exists?(site_id) ⇒ Boolean
- .open_for_date_exists?(date, site_id) ⇒ Boolean
Instance Method Summary collapse
Class Method Details
.create_new(date, site_id) ⇒ Object
Overtime sheet should be created using the method below only. This is because we need to check if there is an open overtime already, and also that we have only one open overtime sheet at a time.
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'app/models/ecom/core/overtime_sheet.rb', line 44 def self.create_new(date, site_id) if OvertimeSheet.exists_for_date?(date, site_id) raise 'There is already an overtime sheet for the selected date.' end if OvertimeSheet.open_exists?(site_id) open_overtime_sheet = OvertimeSheet.find_by(status: 'Open') raise "There is already an open overtime sheet for #{open_overtime_sheet.date}. It has to be submitted before creating a new one." end OvertimeSheet.create(date: date, opened_at: Time.now, status: StatusConstants::OPEN, site_id: site_id) end |
.exists_for_date?(date, site_id) ⇒ Boolean
32 33 34 35 36 37 |
# File 'app/models/ecom/core/overtime_sheet.rb', line 32 def self.exists_for_date?(date, site_id) OvertimeSheet .by_site(site_id) .by_date(date) .exists? end |
.open_exists?(site_id) ⇒ Boolean
17 18 19 20 21 22 |
# File 'app/models/ecom/core/overtime_sheet.rb', line 17 def self.open_exists?(site_id) OvertimeSheet .by_site(site_id) .by_status(StatusConstants::OPEN) .exists? end |
.open_for_date_exists?(date, site_id) ⇒ Boolean
24 25 26 27 28 29 30 |
# File 'app/models/ecom/core/overtime_sheet.rb', line 24 def self.open_for_date_exists?(date, site_id) OvertimeSheet .by_site(site_id) .by_status(StatusConstants::OPEN) .by_date(date) .exists? end |
Instance Method Details
#approve ⇒ Object
66 67 68 69 70 71 72 |
# File 'app/models/ecom/core/overtime_sheet.rb', line 66 def approve raise 'Overtime sheet is not submitted and cannot be approved' unless status == StatusConstants::SUBMITTED self.status = StatusConstants::APPROVED self.approved_at = DateTime.now save end |
#submit ⇒ Object
58 59 60 61 62 63 64 |
# File 'app/models/ecom/core/overtime_sheet.rb', line 58 def submit raise 'Overtime sheet is not open and cannot be submitted' if status != StatusConstants::OPEN self.submitted_at = DateTime.now self.status = StatusConstants::SUBMITTED save end |