質問編集履歴

1

company.rbを追記しました。

2021/09/22 00:23

投稿

jnpps
jnpps

スコア4

test CHANGED
File without changes
test CHANGED
@@ -59,3 +59,597 @@
59
59
  どなたかご教授いただければ幸いです。
60
60
 
61
61
  何卒宜しくお願い致します。
62
+
63
+
64
+
65
+ ### 追記
66
+
67
+ ```company.rb
68
+
69
+ class Company < ActiveRecord::Base
70
+
71
+
72
+
73
+ GIGA = 1024 * 1024 * 1024
74
+
75
+
76
+
77
+ default_scope { order("companies.id ASC") }
78
+
79
+
80
+
81
+ include SecurityEngine
82
+
83
+
84
+
85
+ validates_uniqueness_of :login_name
86
+
87
+ validates_numericality_of :storage_capacity_gb,
88
+
89
+ :greater_than_or_equal_to => :storage_size_needed_gb,
90
+
91
+ :on => :update
92
+
93
+ after_save :update_browsers
94
+
95
+
96
+
97
+ has_many :browsers, dependent: :destroy
98
+
99
+ has_many :drawers, dependent: :nullify
100
+
101
+ has_many :service_contracts, dependent: :destroy
102
+
103
+ has_many :service_units, through: :service_contracts
104
+
105
+ has_many :credits, class_name: "Charge",
106
+
107
+ foreign_key: :payee_id, dependent: :nullify
108
+
109
+ has_many :debts, class_name: "Charge",
110
+
111
+ foreign_key: :payer_id, dependent: :nullify
112
+
113
+ belongs_to :account_type
114
+
115
+ has_many :shops, :dependent => :destroy
116
+
117
+ has_many :customers, :dependent => :destroy
118
+
119
+ has_many :users, :dependent => :destroy
120
+
121
+ has_many :generations,->{ order(:position) } ,:dependent => :destroy
122
+
123
+ has_many :custom_links, ->{ order(:position) }, :dependent => :destroy
124
+
125
+ has_many :item_groups, ->{ order(:position) }, :dependent => :destroy
126
+
127
+ has_many :items, :dependent => :destroy
128
+
129
+ has_many :inventories, :dependent => :destroy
130
+
131
+ has_many :deals, :dependent => :destroy
132
+
133
+ has_many :sales, :dependent => :destroy
134
+
135
+ has_many :occupations, :dependent => :destroy
136
+
137
+ belongs_to :number_factory
138
+
139
+ belongs_to :ymd_input_style
140
+
141
+ has_many :custom_fields, :dependent => :destroy
142
+
143
+ has_many :custom_field_items, :dependent => :destroy
144
+
145
+ has_many :photos, :dependent => :destroy
146
+
147
+ has_many :payment_types, :dependent => :destroy
148
+
149
+ has_many :payments, :dependent => :destroy
150
+
151
+ has_many :trend_cubes, :dependent => :destroy
152
+
153
+ has_many :cubes, :dependent => :destroy
154
+
155
+ has_many :stocks, :dependent => :destroy
156
+
157
+ has_many :calendars, ->{ order(:position) }, :dependent => :destroy
158
+
159
+ belongs_to :company_group
160
+
161
+ belongs_to :manager, class_name: "User"
162
+
163
+ belongs_to :sales_company, class_name: "Company"
164
+
165
+ belongs_to :sales_person, class_name: "User"
166
+
167
+ has_many :user_notifications, dependent: :nullify
168
+
169
+ has_many :customer_notifications, dependent: :nullify
170
+
171
+
172
+
173
+ before_save :encrypt_privacy_information
174
+
175
+ after_save :decrypt_privacy_information
176
+
177
+ after_find :decrypt_privacy_information
178
+
179
+
180
+
181
+ scope :managed_by_dealer, lambda {
182
+
183
+ includes(:company_group).
184
+
185
+ where("company_groups.has_grant_sale = ? OR company_groups.has_grant_use = ? OR company_groups.has_grant_billing = ?", true, true, true)
186
+
187
+ }
188
+
189
+
190
+
191
+ scope :providers, lambda {
192
+
193
+ includes(:company_group).
194
+
195
+ where("company_groups.has_grant_maintenance = ?", true)
196
+
197
+ }
198
+
199
+
200
+
201
+ scope :dealers, lambda {
202
+
203
+ includes(:company_group).
204
+
205
+ where("company_groups.has_grant_service = ?", true)
206
+
207
+ }
208
+
209
+
210
+
211
+ scope :user_companies, lambda {
212
+
213
+ includes(:company_group).
214
+
215
+ where("company_groups.has_grant_use = ?", true)
216
+
217
+ }
218
+
219
+
220
+
221
+ scope :actives_on, lambda { |date = Date.today|
222
+
223
+ where("companies.end_at is null OR companies.end_at > ?", date)
224
+
225
+ }
226
+
227
+
228
+
229
+ def has_multi_shops?
230
+
231
+ self.shops.count > 1
232
+
233
+ end
234
+
235
+
236
+
237
+ def first_sale
238
+
239
+ self.sales.count > 0 ? self.sales.order("ordered_at ASC").first : nil
240
+
241
+ end
242
+
243
+
244
+
245
+ def sold(from = Date.today, to = Date.today)
246
+
247
+ self.sales.during(from, to).sum(:final_price)
248
+
249
+ end
250
+
251
+
252
+
253
+ def paid(from = Date.today, to = Date.today)
254
+
255
+ self.payments.actives.during(from, to).sum(:price)
256
+
257
+ end
258
+
259
+
260
+
261
+ def customers_at(d = Date.today)
262
+
263
+ self.customers.where("start_at <= ?", d).order("start_at ASC")
264
+
265
+ end
266
+
267
+
268
+
269
+ def accounts_receivable(from = self.first_sale.ordered_at, to = Date.today)
270
+
271
+ self.sold(from, to) - self.paid(from, to)
272
+
273
+ end
274
+
275
+
276
+
277
+ def storage_used
278
+
279
+ self.photos.sum(:image_file_size)
280
+
281
+ end
282
+
283
+
284
+
285
+ def storage_size_needed_gb
286
+
287
+ gb_needed = (1.0 * self.storage_used / GIGA).ceil
288
+
289
+ [12, gb_needed].max
290
+
291
+ end
292
+
293
+
294
+
295
+ def storage_free_space
296
+
297
+ self.storage_capacity_gb * GIGA - self.storage_used
298
+
299
+ end
300
+
301
+
302
+
303
+ def storage_used_percentage
304
+
305
+ 100 * self.storage_used / (self.storage_capacity_gb * GIGA)
306
+
307
+ end
308
+
309
+
310
+
311
+ def customers_with_large_debts(n = 10)
312
+
313
+ self.customers.except(:order).order("accounts_receivable DESC").limit(n)
314
+
315
+ end
316
+
317
+
318
+
319
+ def ready?
320
+
321
+ status = true
322
+
323
+
324
+
325
+ status = status && (self.item_groups.count > 0)
326
+
327
+ status = status && (self.items.count > 0)
328
+
329
+ status = status && (self.payment_types.count > 0)
330
+
331
+ status = status && (self.occupations.count > 0)
332
+
333
+ status = status && (self.shops.count > 0)
334
+
335
+
336
+
337
+ return status
338
+
339
+ end
340
+
341
+
342
+
343
+ def root_item_groups
344
+
345
+ self.item_groups.roots.actives
346
+
347
+ end
348
+
349
+
350
+
351
+ def leaf_item_groups
352
+
353
+ leafs = []
354
+
355
+ self.item_groups.actives.each do |ig|
356
+
357
+ leafs << ig if ig.leaf?
358
+
359
+ end
360
+
361
+
362
+
363
+ return leafs
364
+
365
+ end
366
+
367
+
368
+
369
+ def is_system_provider?
370
+
371
+ self.company_group && self.company_group.has_grant_maintenance
372
+
373
+ end
374
+
375
+
376
+
377
+ def is_dealer?
378
+
379
+ self.company_group && self.company_group.has_grant_service
380
+
381
+ end
382
+
383
+
384
+
385
+ def is_user?
386
+
387
+ self.company_group && self.company_group.has_grant_use
388
+
389
+ end
390
+
391
+
392
+
393
+ def credit_amount
394
+
395
+ self.credits.not_paid.sum(:amount)
396
+
397
+ end
398
+
399
+
400
+
401
+ def debt_amount
402
+
403
+ self.debts.not_paid.sum(:amount)
404
+
405
+ end
406
+
407
+
408
+
409
+ def has_credits?
410
+
411
+ self.credits.not_paid.count > 0
412
+
413
+ end
414
+
415
+
416
+
417
+ def has_debts?
418
+
419
+ self.debts.not_paid.count > 0
420
+
421
+ end
422
+
423
+
424
+
425
+ def get_contract(service_unit)
426
+
427
+ return nil if service_unit.nil?
428
+
429
+ return self.service_contracts.where(service_unit_id: service_unit.id).first
430
+
431
+ end
432
+
433
+
434
+
435
+ def activate_service(service_unit, user)
436
+
437
+ service_contract = get_contract(service_unit)
438
+
439
+
440
+
441
+ if service_contract.nil? && service_unit
442
+
443
+ service_contract = self.service_contracts.new(service_unit_id: service_unit.id)
444
+
445
+ end
446
+
447
+
448
+
449
+ return false if service_contract.nil?
450
+
451
+
452
+
453
+ return service_contract.activate(user)
454
+
455
+ end
456
+
457
+
458
+
459
+ def deactivate_service(service_unit, user)
460
+
461
+ service_contract = get_contract(service_unit)
462
+
463
+
464
+
465
+ return false if service_contract.nil?
466
+
467
+ return service_contract.deactivate(user)
468
+
469
+ end
470
+
471
+
472
+
473
+ def using?(service_unit)
474
+
475
+ service_contract = get_contract(service_unit)
476
+
477
+ return service_contract ? service_contract.active : false
478
+
479
+ end
480
+
481
+
482
+
483
+ def monthly_charge
484
+
485
+ self.service_contracts.actives.sum(:price)
486
+
487
+ end
488
+
489
+
490
+
491
+ def basic_contract
492
+
493
+ self.service_contracts.actives.each do |sc|
494
+
495
+ return sc if sc.service_unit.mandatory
496
+
497
+ end
498
+
499
+ return nil
500
+
501
+ end
502
+
503
+
504
+
505
+ def option_service_units
506
+
507
+ return self.basic_contract.service_unit.option_service_units
508
+
509
+ end
510
+
511
+
512
+
513
+ def has_bank_info?
514
+
515
+ return false if self.bank_name.blank?
516
+
517
+ return false if self.bank_branch.blank?
518
+
519
+ return false unless self.account_type
520
+
521
+ return false unless self.account_number
522
+
523
+ return !self.account_name.blank?
524
+
525
+ end
526
+
527
+
528
+
529
+ def num_of_books_without_sale
530
+
531
+ return self.calendars.inject(0) do |num_books, calendar|
532
+
533
+ num_books += calendar.books.without_sale.count
534
+
535
+ end
536
+
537
+ end
538
+
539
+
540
+
541
+ def num_of_books_in_today
542
+
543
+ return self.calendars.inject(0) do |num_books, calendar|
544
+
545
+ num_books += calendar.books.of_day(Date.today).count
546
+
547
+ end
548
+
549
+ end
550
+
551
+
552
+
553
+ def has_drawer?
554
+
555
+ self.drawers.count > 0
556
+
557
+ end
558
+
559
+
560
+
561
+ def is_using?(service_unit_name_tag)
562
+
563
+ if self.service_units.with_name_tag(service_unit_name_tag).count > 0
564
+
565
+ return true
566
+
567
+ end
568
+
569
+
570
+
571
+ ServiceUnit.with_name_tag(service_unit_name_tag).each do |su|
572
+
573
+ self.service_units.each do |s|
574
+
575
+ return true if s.includes?(su)
576
+
577
+ end
578
+
579
+ end
580
+
581
+
582
+
583
+ return false
584
+
585
+ end
586
+
587
+
588
+
589
+ def pusher_channel
590
+
591
+ return "Company-%d" % self.id
592
+
593
+ end
594
+
595
+
596
+
597
+ def active?
598
+
599
+ return true if self.end_at.nil?
600
+
601
+ return self.end_at > Date.today
602
+
603
+ end
604
+
605
+
606
+
607
+ private
608
+
609
+ def encrypt_privacy_information
610
+
611
+ self.bank_name = encrypt(self.bank_name)
612
+
613
+ self.bank_branch = encrypt(self.bank_branch)
614
+
615
+ self.account_number = encrypt(self.account_number)
616
+
617
+ self.account_name = encrypt(self.account_name)
618
+
619
+ end
620
+
621
+
622
+
623
+ def decrypt_privacy_information
624
+
625
+ self.bank_name = decrypt(self.bank_name)
626
+
627
+ self.bank_branch = decrypt(self.bank_branch)
628
+
629
+ self.account_number = decrypt(self.account_number)
630
+
631
+ self.account_name = decrypt(self.account_name)
632
+
633
+ end
634
+
635
+
636
+
637
+ def update_browsers
638
+
639
+ unless self.manage_browser
640
+
641
+ self.browsers.each do |b|
642
+
643
+ b.logout
644
+
645
+ end
646
+
647
+ end
648
+
649
+ end
650
+
651
+ end
652
+
653
+
654
+
655
+ ```