Class: Company

Constant Summary collapse

ENCRYPTED_FIELDS =
%w[
  gst_no
  pst_no
  provincial_identification_no
  federal_identification_no
]
BUSINESS_HOURS_START =
9
BUSINESS_HOURS_END =
17
BUSINESS_DAYS =
[1, 2, 3, 4, 5].freeze
RUN_PAYROLL_HOURS_START =
10
RUN_PAYROLL_HOURS_END =
17

Constants included from RegexPatterns

RegexPatterns::PHONE

Constants included from HasCompanySetup

HasCompanySetup::EMPLOYEES_IN_UNSUPPORTED_PROVINCES, HasCompanySetup::EMPLOYEES_WITHOUT_PAYROLL_CALCULATIONS, HasCompanySetup::EMPLOYEES_WITHOUT_ROLES, HasCompanySetup::NOT_ENOUGH_PAID_HOLIDAYS_FOR_NEXT_YEAR, HasCompanySetup::NOT_ENOUGH_PAYROLL_PARAMETERS_FOR_NEXT_YEAR, HasCompanySetup::NOT_ENOUGH_PAYROLL_PERIODS_FOR_NEXT_YEAR, HasCompanySetup::NOT_ENOUGH_PAYROLL_PERIODS_FOR_THIS_YEAR, HasCompanySetup::NO_CNESST_CODES, HasCompanySetup::NO_HOLIDAYS_FOR_THIS_YEAR, HasCompanySetup::NO_PAYROLL_PARAMETERS_FOR_CURRENT_YEAR, HasCompanySetup::UNSUPPORTED_PROVINCE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CompaniesProcedures

#write_procedure

Methods included from HasPayrollFrequency

#payroll_frequency_description, #payroll_frequency_option, #payroll_frequency_type

Methods included from HasPayrollPeriods

#current_ending_payroll_period, #current_payroll_period, #current_payroll_period_id, #current_payroll_period_year, #duration_spent_in_current_payroll_period, #generate_payroll_periods, #grouped_payroll_periods, #latest_payroll_period, #latest_payroll_period_date, #open_payroll_periods, #payroll_period_for_date, #payroll_periods_data, #pervious_payroll_period, #previous_payroll_period

Methods included from CompanyEndOfYearReports

#t4_pdf

Methods included from HasCompanySetup

#employees_in_unsupported_provinces, #employees_without_payroll_calculations, #employees_without_roles, #setup_completed?, #setup_issues

Methods included from HasCompanyOnboarding

#agree_to_agreement, #agreed_to_agreement?, #company_details?, #onboarded?, #onboarding_path

Methods included from HasAddress

#address, #comma_separated_address, #line_separated_address, #space_separated_address

Methods inherited from ApplicationRecord

define_decrypted_attribute, define_decrypted_attributes, #errors_in_bullet_points, primary_connection, special_connection

Class Method Details

.create_missing_event_notification_configurationsObject



227
228
229
230
231
232
233
234
235
236
237
# File 'app/models/company.rb', line 227

def self.create_missing_event_notification_configurations
  Company.find_each do |company|
    present_configuration_event_slugs = company.event_notification_configurations.pluck(:event_slug)
    EventNotificationConfiguration.where(company_id: -1).where.not(event_slug: present_configuration_event_slugs).find_each do |record|
      record.dup.tap do |new_record|
        new_record.company_id = company.company_id
        new_record.save!
      end
    end
  end
end

.find_by_procedure(company_id) ⇒ Object



263
264
265
266
267
268
269
# File 'app/models/company.rb', line 263

def self.find_by_procedure(company_id)
  ActiveRecord::Base.connection.execute_procedure(
    'pr_get_companies',
    company_id,
    nil # company_code
  )[0]
end

Instance Method Details

#available_integrationsObject



185
186
187
# File 'app/models/company.rb', line 185

def available_integrations
  Integration.available_for_company(company_id)
end

#bank_accountsObject



174
175
176
177
178
179
# File 'app/models/company.rb', line 174

def bank_accounts
  BankAccount.where(
    source: 'Company',
    reference_id: company_id
  )
end

#can_resume_payroll?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'app/models/company.rb', line 154

def can_resume_payroll?
  payroll_transaction_headers.where(stage: PayrollTransactionHeader::STAGES[:IN_PROGRESS]).count
end

#cfo_usersObject



143
144
145
146
147
148
149
150
151
152
# File 'app/models/company.rb', line 143

def cfo_users
  ErpUser
    .joins(:contacts)
    .where(contacts: {
             position: Contact::TYPES[:CFO][:TYPE],
             company_id: company_id,
             active: true
           })
    .uniq
end

#company_nameObject



255
256
257
# File 'app/models/company.rb', line 255

def company_name
  company_name_e
end

#company_parametersObject



259
260
261
# File 'app/models/company.rb', line 259

def company_parameters
  CompanyParameter.where(company_id: [-1, company_id])
end

#create_event_notification_configurationsObject



217
218
219
220
221
222
223
224
# File 'app/models/company.rb', line 217

def create_event_notification_configurations
  EventNotificationConfiguration.where(company_id: -1).find_each do |record|
    record.dup.tap do |new_record|
      new_record.company_id = company_id
      new_record.save!
    end
  end
end

#default_language_optionObject



133
134
135
# File 'app/models/company.rb', line 133

def default_language_option
  Language.new(default_language).to_h
end

#gvt_remittance_frequency_optionObject



137
138
139
140
141
# File 'app/models/company.rb', line 137

def gvt_remittance_frequency_option
  TypesMaster.gov_remittance_frequency.find_by(type: gvt_remittance_frequency).as_json(
    methods: %i[type_description]
  )
end

#has_default_bank_account?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'app/models/company.rb', line 158

def has_default_bank_account?
  bank_accounts.exists?(is_default: true)
end

#industry_sector_optionObject



127
128
129
130
131
# File 'app/models/company.rb', line 127

def industry_sector_option
  TypesMaster.industry_sectors.find_by(type: industry_sector).as_json(
    methods: %i[type_description]
  )
end

#invoices_countObject



121
122
123
124
125
# File 'app/models/company.rb', line 121

def invoices_count
  payroll_transaction_headers_views.where.not(
    invoice_no: nil
  ).count
end

#localeObject



162
163
164
# File 'app/models/company.rb', line 162

def locale
  default_language == 'F' ? 'fr' : 'en'
end

#nameObject



251
252
253
# File 'app/models/company.rb', line 251

def name
  company_name
end

#provinceObject



239
240
241
# File 'app/models/company.rb', line 239

def province
  Province.find_by(province_id: province_id)
end

#province_codeObject



117
118
119
# File 'app/models/company.rb', line 117

def province_code
  province.province_code
end

#reportsObject



181
182
183
# File 'app/models/company.rb', line 181

def reports
  Report.available_for_company(company_id)
end

#ship_provinceObject



243
244
245
# File 'app/models/company.rb', line 243

def ship_province
  Province.find_by(province_id: ship_province_id)
end

#supplier_by_companiesObject



247
248
249
# File 'app/models/company.rb', line 247

def supplier_by_companies
  Supplier.where(company_code: company_code)
end

#timezoneObject



170
171
172
# File 'app/models/company.rb', line 170

def timezone
  'America/Montreal'
end

#urlObject



166
167
168
# File 'app/models/company.rb', line 166

def url
  "#{AppMode.payroll.web_uri}/app/settings/company"
end

#validate_business_daysObject



189
190
191
192
193
194
195
196
197
198
# File 'app/models/company.rb', line 189

def validate_business_days
  return if business_days.blank?

  business_days.split(',').each do |day|
    unless (1..7).cover?(day.to_i)
      errors.add(:business_days, 'Invalid business days')
      break
    end
  end
end

#validate_gvt_remittance_frequencyObject



209
210
211
212
213
214
215
# File 'app/models/company.rb', line 209

def validate_gvt_remittance_frequency
  return if gvt_remittance_frequency.blank?

  return if TypesMaster.gov_remittance_frequency.pluck(:type).include?(gvt_remittance_frequency)

  errors.add(:gvt_remittance_frequency, 'Invalid government remittance frequency')
end

#validate_industry_sectorObject



200
201
202
203
204
205
206
207
# File 'app/models/company.rb', line 200

def validate_industry_sector
  return if industry_sector.blank?

  valid_sector_types = TypesMaster.industry_sectors.pluck(:type)
  return if valid_sector_types.include?(industry_sector)

  errors.add(:payroll_frequency, 'Cannot change the payroll frequency when there are existing timesheets.')
end

#within_business_hours?(time = Time.zone.now) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
# File 'app/models/company.rb', line 99

def within_business_hours?(time = Time.zone.now)
  current_weekday_number = time.strftime('%u').to_i
  current_hour = time.strftime('%H').to_i

  return false unless BUSINESS_DAYS.include?(current_weekday_number)

  current_hour >= BUSINESS_HOURS_START && current_hour < BUSINESS_HOURS_END
end

#within_run_payroll_hours?(time = Time.zone.now) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
111
112
113
114
115
# File 'app/models/company.rb', line 108

def within_run_payroll_hours?(time = Time.zone.now)
  current_weekday_number = time.strftime('%u').to_i
  current_hour = time.strftime('%H').to_i

  return false unless BUSINESS_DAYS.include?(current_weekday_number)

  current_hour >= RUN_PAYROLL_HOURS_START && current_hour < RUN_PAYROLL_HOURS_END
end