Passed
Push — dev5 ( 4e7807...23c1c8 )
by Ron
08:30
created

DatabaseCheck::system_data_fields()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 39
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 20
c 1
b 0
f 0
nc 16
nop 0
dl 0
loc 39
ccs 0
cts 21
cp 0
crap 56
rs 8.6666
1
<?php
2
3
namespace App\Domains\Maintenance;
4
5
use Illuminate\Support\Facades\Log;
6
7
use App\User;
8
use App\Files;
9
use App\TechTips;
10
use App\FileLinks;
11
use App\Customers;
12
use App\UserLogins;
13
use App\SystemTypes;
14
use App\TechTipFavs;
15
use App\CustomerFavs;
16
use App\UserRoleType;
17
use App\TechTipFiles;
18
use App\TechTipTypes;
19
use App\FileLinkFiles;
20
use App\CustomerFiles;
21
use App\CustomerNotes;
22
use App\TechTipSystems;
23
use App\TechTipComments;
24
use App\CustomerSystems;
25
use App\CustomerContacts;
26
use App\PhoneNumberTypes;
27
use App\SystemCategories;
28
use App\SystemDataFields;
29
use App\CustomerFileTypes;
30
use App\CustomerSystemData;
31
use App\UserRolePermissions;
32
use App\SystemDataFieldTypes;
33
use App\CustomerContactPhones;
34
use App\UserRolePermissionTypes;
35
36
class DatabaseCheck
37
{
38
    protected $fix;
39
40
    public function __construct($fix = false)
41
    {
42
        $this->fix = $fix;
43
    }
44
45
    public function execute($table)
46
    {
47
        return $this->$table();
48
    }
49
50
    //  users table contains the user_role foreign key
51
    protected function users()
52
    {
53
        $valid = true;
54
        $list = User::select('role_id')->groupBy('role_id')->get();
55
        foreach($list as $l)
56
        {
57
            $v = UserRoleType::find($l->role_id);
58
            if(!$v)
59
            {
60
                $valid = false;
61
                if($this->fix)
62
                {
63
                    $data = User::where('role_id', $l->role_id);
64
                    Log::notice('DBCheck - user Foreign key failed.  Deleting data - ', array($data));
65
                    $data->delete();
66
                }
67
            }
68
        }
69
70
        return $valid;
71
    }
72
73
    //  user_role_permissions table has the role_id and perm_type_id foreign keys
74
    protected function user_role_permissions()
75
    {
76
        $valid = true;
77
78
        //  Check role_id foreign key
79
        $list  = UserRolePermissions::select('role_id')->groupBy('role_id')->get();
80
        foreach($list as $l)
81
        {
82
            $v = UserRoleType::find($l->role_id);
83
            if(!$v)
84
            {
85
                $valid = false;
86
                if($this->fix)
87
                {
88
                    $data = UserRolePermissions::where('role_id', $l->role_id);
89
                    Log::notice('DBCheck - user_role_permissions Foreign key failed.  Deleting data - ', array($data));
90
                    $data->delete();
91
                }
92
            }
93
        }
94
95
        //  Check perm_type_id foreign key
96
        $list  = UserRolePermissions::select('perm_type_id')->groupBy('perm_type_id')->get();
97
        foreach($list as $l)
98
        {
99
            $v = UserRolePermissionTypes::find($l->perm_type_id);
100
            if(!$v)
101
            {
102
                $valid = false;
103
                if($this->fix)
104
                {
105
                    $data = UserRolePermissions::where('perm_type_id', $l->perm_type_id);
106
                    Log::notice('DBCheck - user_role_permissions Foreign key failed.  Deleting data - ', array($data));
107
                    $data->delete();
108
                }
109
            }
110
        }
111
112
        return $valid;
113
    }
114
115
    //  user_logins table has the user_id foreign key
116
    protected function user_logins()
117
    {
118
        $valid = true;
119
120
        //  Check role_id foreign key
121
        $list  = UserLogins::select('user_id')->groupBy('user_id')->get();
122
        foreach($list as $l)
123
        {
124
            $v = User::where('user_id', $l->user_id)->withTrashed()->first();
125
            if(!$v)
126
            {
127
                $valid = false;
128
                if($this->fix)
129
                {
130
                    $data = UserLogins::where('user_id', $l->user_id);
131
                    Log::notice('DBCheck - user_logins Foreign key failed.  Deleting data - ', array($data));
132
                    $data->delete();
133
                }
134
            }
135
        }
136
137
        return $valid;
138
    }
139
140
    //  tech_tips table has user_id, updated_id and tip_type_id foreign keys
141
    protected function tech_tips()
142
    {
143
        $valid = true;
144
145
        //  Check user_id foreign key
146
        $list  = TechTips::select('user_id')->groupBy('user_id')->get();
147
        foreach($list as $l)
148
        {
149
            $v = User::where('user_id', $l->user_id)->withTrashed()->first();
150
            if(!$v)
151
            {
152
                $valid = false;
153
                if($this->fix)
154
                {
155
                    $adminUser = User::orderBy('user_id')->first();
156
                    $tipList   = TechTips::where('user_id', $l->user_id)->get();
157
                    foreach($tipList as $tip)
158
                    {
159
                        Log::notice('DBCheck - tech_tip Foreign key failed.  Updating user_id column - ', array($tip));
160
                        $tip->update([
161
                            'user_id' => $adminUser->user_id,
162
                        ]);
163
                    }
164
                }
165
            }
166
        }
167
168
        //  Check updated_id foreign key
169
        $list  = TechTips::select('updated_id')->groupBy('updated_id')->get();
170
        foreach($list as $l)
171
        {
172
            $v = User::where('user_id', $l->user_id)->withTrashed()->get();
173
            if(!$v)
174
            {
175
                $valid = false;
176
                if($this->fix)
177
                {
178
                    $adminUser = User::orderBy('user_id')->first();
179
                    $tipList   = TechTips::where('updated_id', $l->user_id)->get();
180
                    foreach($tipList as $tip)
181
                    {
182
                        Log::notice('DBCheck - tech_tip Foreign key failed.  Updating updated_id column - ', array($tip));
183
                        $tip->update([
184
                            'updated_id' => $adminUser->user_id,
185
                        ]);
186
                    }
187
                }
188
            }
189
        }
190
191
        //  Check tip_type_id foreign key
192
        $list  = TechTips::select('tip_type_id')->groupBy('tip_type_id')->get();
193
        foreach($list as $l)
194
        {
195
            $v = TechTipTypes::find($l->tip_type_id);
196
            if(!$v)
197
            {
198
                $valid = false;
199
                if($this->fix)
200
                {
201
                    $data = TechTips::where('tip_type_id', $l->tip_type_id);
202
                    Log::notice('DBCheck - tech_tip Foreign key failed.  Deleting data - ', array($data));
203
                    $data->delete();
204
                }
205
            }
206
        }
207
208
        return $valid;
209
    }
210
211
    //  tech_tip_systems table has tip_id and sys_id foreign keys
212
    protected function tech_tip_systems()
213
    {
214
        $valid = true;
215
216
        //  Check tech_tip foreign key
217
        $list  = TechTipSystems::select('tip_id')->groupBy('tip_id')->get();
218
        foreach($list as $l)
219
        {
220
            $v = TechTips::find($l->tip_id);
221
            if(!$v)
222
            {
223
                $valid = false;
224
                if($this->fix)
225
                {
226
                    $data = TechTipSystems::where('tip_id', $l->tip_id);
227
                    Log::notice('DBCheck - tech_tip_systems Foreign key failed.  Deleting data - ', array($data));
228
                    $data->delete();
229
                }
230
            }
231
        }
232
233
        //  Check sys_id foreign key
234
        $list  = TechTipSystems::select('sys_id')->groupBy('sys_id')->get();
235
        foreach($list as $l)
236
        {
237
            $v = SystemTypes::find($l->sys_id);
238
            if(!$v)
239
            {
240
                $valid = false;
241
                if($this->fix)
242
                {
243
                    $data = TechTipSystems::where('sys_id', $l->sys_id);
244
                    Log::notice('DBCheck - tech_tip_systems Foreign key failed.  Deleting data - ', array($data));
245
                    $data->delete();
246
                }
247
            }
248
        }
249
250
        return $valid;
251
    }
252
253
    //  tech_tip_files table has tip_id and file_id foreign keys
254
    protected function tech_tip_files()
255
    {
256
        $valid = true;
257
258
        //  Check tip_id foreign key
259
        $list  = TechTipFiles::select('tip_id')->groupBy('tip_id')->get();
260
        foreach($list as $l)
261
        {
262
            $v = TechTips::find($l->tip_id);
263
            if(!$v)
264
            {
265
                $valid = false;
266
                if($this->fix)
267
                {
268
                    $data = TechTipFiles::where('tip_id', $l->tip_id);
269
                    Log::notice('DBCheck - tech_tip_files Foreign key failed.  Deleting data - ', array($data));
270
                    $data->delete();
271
                }
272
            }
273
        }
274
275
        //  Check file_id foreign key
276
        $list  = TechTipFiles::select('file_id')->groupBy('file_id')->get();
277
        foreach($list as $l)
278
        {
279
            $v = Files::find($l->file_id);
280
            if(!$v)
281
            {
282
                $valid = false;
283
                if($this->fix)
284
                {
285
                    $data = TechTipFiles::where('file_id', $l->file_id);
286
                    Log::notice('DBCheck - tech_tip_files Foreign key failed.  Deleting data - ', array($data));
287
                    $data->delete();
288
                }
289
            }
290
        }
291
292
        return $valid;
293
    }
294
295
    //  tech_tip_favs table has the user_id and tip_id foreign keys
296
    protected function tech_tip_favs()
297
    {
298
        $valid = true;
299
300
        //  Check tip_id foreign key
301
        $list  = TechTipFavs::select('tip_id')->groupBy('tip_id')->get();
302
        foreach($list as $l)
303
        {
304
            $v = TechTips::find($l->tip_id);
305
            if(!$v)
306
            {
307
                $valid = false;
308
                if($this->fix)
309
                {
310
                    $data = TechTipFavs::where('tip_id', $l->tip_id);
311
                    Log::notice('DBCheck - tech_tip_favs Foreign key failed.  Deleting data - ', array($data));
312
                    $data->delete();
313
                }
314
            }
315
        }
316
317
        //  Check user_id foreign key
318
        $list  = TechTipFavs::select('user_id')->groupBy('user_id')->get();
319
        foreach($list as $l)
320
        {
321
            $v = User::find($l->user_id);
322
            if(!$v)
323
            {
324
                $valid = false;
325
                if($this->fix)
326
                {
327
                    $data = TechTipFavs::where('user_id', $l->user_id);
328
                    Log::notice('DBCheck - tech_tip_favs Foreign key failed.  Deleting data - ', array($data));
329
                    $data->delete();
330
                }
331
            }
332
        }
333
334
        return $valid;
335
    }
336
337
    //  tech_tip_comments table has the tip_id and user_id foreign keys
338
    protected function tech_tip_comments()
339
    {
340
        $valid = true;
341
342
        //  Check tip_id foreign key
343
        $list  = TechTipComments::select('tip_id')->groupBy('tip_id')->get();
344
        foreach($list as $l)
345
        {
346
            $v = TechTips::find($l->tip_id);
347
            if(!$v)
348
            {
349
                $valid = false;
350
                if($this->fix)
351
                {
352
                    $data = TechTipComments::where('tip_id', $l->tip_id);
353
                    Log::notice('DBCheck - tech_tip_comments Foreign key failed.  Deleting data - ', array($data));
354
                    $data->delete();
355
                }
356
            }
357
        }
358
359
        //  Check user_id foreign key
360
        $list  = TechTipComments::select('user_id')->groupBy('user_id')->get();
361
        foreach($list as $l)
362
        {
363
            $v = User::find($l->user_id);
364
            if(!$v)
365
            {
366
                $valid = false;
367
                if($this->fix)
368
                {
369
                    $data = TechTipComments::where('user_id', $l->user_id);
370
                    Log::notice('DBCheck - tech_tip_comments Foreign key failed.  Deleting data - ', array($data));
371
                    $data->delete();
372
                }
373
            }
374
        }
375
376
        return $valid;
377
    }
378
379
    //  system_types table has cat_id foreign key
380
    protected function system_types()
381
    {
382
        $valid = true;
383
384
        //  Check cat_id foreign key
385
        $list  = SystemTypes::select('cat_id')->groupBy('cat_id')->get();
386
        foreach($list as $l)
387
        {
388
            $v = SystemCategories::where('cat_id', $l->cat_id)->first();
389
            if(!$v)
390
            {
391
                $valid = false;
392
                if($this->fix)
393
                {
394
                    $data = SystemTypes::where('cat_id', $l->cat_id);
395
                    Log::notice('DBCheck - system_types Foreign key failed.  Deleting data - ', array($data));
396
                    $data->delete();
397
                }
398
            }
399
        }
400
401
        return $valid;
402
    }
403
404
    //  system_data_fields table has sys_id and data_type_id foreign keys
405
    protected function system_data_fields()
406
    {
407
        $valid = true;
408
409
        //  Check sys_id foreign key
410
        $list  = SystemDataFields::select('sys_id')->groupBy('sys_id')->get();
411
        foreach($list as $l)
412
        {
413
            $v = SystemTypes::where('sys_id', $l->sys_id)->first();
414
            if(!$v)
415
            {
416
                $valid = false;
417
                if($this->fix)
418
                {
419
                    $data = SystemDataFields::where('sys_id', $l->sys_id);
420
                    Log::notice('DBCheck - system_data_fields Foreign key failed.  Deleting data - ', array($data));
421
                    $data->delete();
422
                }
423
            }
424
        }
425
426
        //  Check data_type_id foreign key
427
        $list  = SystemDataFields::select('data_type_id')->groupBy('data_type_id')->get();
428
        foreach($list as $l)
429
        {
430
            $v = SystemDataFieldTypes::where('data_type_id', $l->data_type_id)->first();
431
            if(!$v)
432
            {
433
                $valid = false;
434
                if($this->fix)
435
                {
436
                    $data = SystemDataFields::where('data_type_id', $l->data_type_id);
437
                    Log::notice('DBCheck - system_data_fields Foreign key failed.  Deleting data - ', array($data));
438
                    $data->delete();
439
                }
440
            }
441
        }
442
443
        return $valid;
444
    }
445
446
447
    //  file_links table has user_id and cust_id foreign keys
448
    protected function file_links()
449
    {
450
        $valid = true;
451
452
        //  Check user_id foreign key
453
        $list  = FileLinks::select('user_id')->groupBy('user_id')->get();
454
        foreach($list as $l)
455
        {
456
            $v = User::where('user_id', $l->user_id)->first();
457
            if(!$v)
458
            {
459
                $valid = false;
460
                if($this->fix)
461
                {
462
                    $data = FileLinks::where('user_id', $l->user_id);
463
                    Log::notice('DBCheck - file_links Foreign key failed.  Deleting data - ', array($data));
464
                    $data->delete();
465
                }
466
            }
467
        }
468
469
        //  Check cust_id foreign key
470
        $list  = FileLinks::select('cust_id')->where('cust_id', '!=', null)->groupBy('cust_id')->get();
471
        foreach($list as $l)
472
        {
473
            $v = Customers::where('cust_id', $l->cust_id)->first();
474
            if(!$v)
475
            {
476
                $valid = false;
477
                if($this->fix)
478
                {
479
                    $data = FileLinks::where('cust_id', $l->cust_id);
480
                    Log::notice('DBCheck - file_links Foreign key failed.  Updating data - ', array($data));
481
                    foreach($data as $d)
482
                    {
483
                        $d->update([
484
                            'cust_id' => null,
485
                        ]);
486
                    }
487
                }
488
            }
489
        }
490
491
        return $valid;
492
    }
493
494
    //  file_link_files table has link_id, file_id, and user_id foreign keys
495
    protected function file_link_files()
496
    {
497
        $valid = true;
498
499
        //  Check link_id foreign key
500
        $list  = FileLinkFiles::select('link_id')->groupBy('link_id')->get();
501
        foreach($list as $l)
502
        {
503
            $v = FileLinks::where('link_id', $l->link_id)->first();
504
            if(!$v)
505
            {
506
                $valid = false;
507
                if($this->fix)
508
                {
509
                    $data = FileLinkFiles::where('link_id', $l->link_id);
510
                    Log::notice('DBCheck - file_link_files Foreign key failed.  Deleting data - ', array($data));
511
                    $data->delete();
512
                }
513
            }
514
        }
515
516
        //  Check file_id foreign key
517
        $list  = FileLinkFiles::select('file_id')->groupBy('file_id')->get();
518
        foreach($list as $l)
519
        {
520
            $v = Files::where('file_id', $l->file_id)->first();
521
            if(!$v)
522
            {
523
                $valid = false;
524
                if($this->fix)
525
                {
526
                    $data = FileLinkFiles::where('file_id', $l->file_id);
527
                    Log::notice('DBCheck - file_link_files Foreign key failed.  Deleting data - ', array($data));
528
                    $data->delete();
529
                }
530
            }
531
        }
532
533
        //  Check user_id foreign key
534
        $list  = FileLinkFiles::select('user_id')->groupBy('user_id')->get();
535
        foreach($list as $l)
536
        {
537
            if($l->user_id)
538
            {
539
                $v = User::where('user_id', $l->user_id)->withTrashed()->first();
540
                if(!$v)
541
                {
542
                    $valid = false;
543
                    if($this->fix)
544
                    {
545
                        $data = FileLinkFiles::where('user_id', $l->user_id);
546
                        Log::notice('DBCheck - file_link_files Foreign key failed.  Deleting data - ', array($data));
547
                        $data->delete();
548
                    }
549
                }
550
            }
551
        }
552
553
        return $valid;
554
    }
555
556
    //  customer_systems table has cust_id and sys_id foreign keys
557
    protected function customer_systems()
558
    {
559
        $valid = true;
560
561
        //  Check cust_id foreign key
562
        $list  = CustomerSystems::select('cust_id')->groupBy('cust_id')->get();
563
        foreach($list as $l)
564
        {
565
            $v = Customers::where('cust_id', $l->cust_id)->first();
566
            if(!$v)
567
            {
568
                $valid = false;
569
                if($this->fix)
570
                {
571
                    $data = CustomerSystems::where('cust_id', $l->cust_id);
572
                    Log::notice('DBCheck - customer_systems Foreign key failed.  Deleting data - ', array($data));
573
                    $data->delete();
574
                }
575
            }
576
        }
577
578
        //  Check sys_id foreign key
579
        $list  = CustomerSystems::select('sys_id')->groupBy('sys_id')->get();
580
        foreach($list as $l)
581
        {
582
            $v = SystemTypes::where('sys_id', $l->sys_id)->first();
583
            if(!$v)
584
            {
585
                $valid = false;
586
                if($this->fix)
587
                {
588
                    $data = CustomerSystems::where('sys_id', $l->sys_id);
589
                    Log::notice('DBCheck - customer_systems Foreign key failed.  Deleting data - ', array($data));
590
                    $data->delete();
591
                }
592
            }
593
        }
594
595
        return $valid;
596
    }
597
598
    //  The customer_system_data table has the cust_sys_id and field_id foreign keys
599
    protected function customer_system_data()
600
    {
601
        $valid = true;
602
603
        //  Check cust_sys_id foreign key
604
        $list  = CustomerSystemData::select('cust_sys_id')->groupBy('cust_sys_id')->get();
605
        foreach($list as $l)
606
        {
607
            $v = CustomerSystems::where('cust_sys_id', $l->cust_sys_id)->first();
608
            if(!$v)
609
            {
610
                $valid = false;
611
                if($this->fix)
612
                {
613
                    $data = CustomerSystemData::where('cust_sys_id', $l->cust_sys_id);
614
                    Log::notice('DBCheck - customer_system_data Foreign key failed.  Deleting data - ', array($data));
615
                    $data->delete();
616
                }
617
            }
618
        }
619
620
        //  Check field_id foreign key
621
        $list  = CustomerSystemData::select('field_id')->groupBy('field_id')->get();
622
        foreach($list as $l)
623
        {
624
            $v = SystemDataFields::where('field_id', $l->field_id)->first();
625
            if(!$v)
626
            {
627
                $valid = false;
628
                if($this->fix)
629
                {
630
                    $data = CustomerSystemData::where('field_id', $l->field_id);
631
                    Log::notice('DBCheck - customer_system_data Foreign key failed.  Deleting data - ', array($data));
632
                    $data->delete();
633
                }
634
            }
635
        }
636
637
        return $valid;
638
    }
639
640
    //  customer_notes table has cust_id and user_id foreign keys
641
    protected function customer_notes()
642
    {
643
        $valid = true;
644
645
        //  Check cust_id foreign key
646
        $list  = CustomerNotes::select('cust_id')->groupBy('cust_id')->get();
647
        foreach($list as $l)
648
        {
649
            $v = Customers::where('cust_id', $l->cust_id)->first();
650
            if(!$v)
651
            {
652
                $valid = false;
653
                if($this->fix)
654
                {
655
                    $data = CustomerNotes::where('cust_id', $l->cust_id);
656
                    Log::notice('DBCheck - customer_notes Foreign key failed.  Deleting data - ', array($data));
657
                    $data->delete();
658
                }
659
            }
660
        }
661
662
        //  Check user_id foreign key
663
        $list  = CustomerNotes::select('user_id')->groupBy('user_id')->get();
664
        foreach($list as $l)
665
        {
666
            if($l->user_id)
667
            {
668
                $v = User::where('user_id', $l->user_id)->withTrashed()->first();
669
                if(!$v)
670
                {
671
                    $valid = false;
672
                    if($this->fix)
673
                    {
674
                        $data = CustomerNotes::where('user_id', $l->user_id);
675
                        Log::notice('DBCheck - customer_notes Foreign key failed.  Deleting data - ', array($data));
676
                        $data->delete();
677
                    }
678
                }
679
            }
680
        }
681
682
        return $valid;
683
    }
684
685
    //  customer_files table has file_id, file_type_id, cust_id, and user_id foreign keys
686
    protected function customer_files()
687
    {
688
        $valid = true;
689
690
        //  Check file_id foreign key
691
        $list  = CustomerFiles::select('file_id')->groupBy('file_id')->get();
692
        foreach($list as $l)
693
        {
694
            $v = Files::where('file_id', $l->file_id)->first();
695
            if(!$v)
696
            {
697
                $valid = false;
698
                if($this->fix)
699
                {
700
                    $data = CustomerFiles::where('file_id', $l->file_id);
701
                    Log::notice('DBCheck - customer_files Foreign key failed.  Deleting data - ', array($data));
702
                    $data->delete();
703
                }
704
            }
705
        }
706
707
        //  Check file_type_id foreign key
708
        $list  = CustomerFiles::select('file_type_id')->groupBy('file_type_id')->get();
709
        foreach($list as $l)
710
        {
711
            $v = CustomerFileTypes::where('file_type_id', $l->file_type_id)->first();
712
            if(!$v)
713
            {
714
                $valid = false;
715
                if($this->fix)
716
                {
717
                    $data = CustomerFiles::where('file_type_id', $l->file_type_id);
718
                    Log::notice('DBCheck - customer_files Foreign key failed.  Deleting data - ', array($data));
719
                    $data->delete();
720
                }
721
            }
722
        }
723
724
        //  Check cust_id foreign key
725
        $list  = CustomerFiles::select('cust_id')->groupBy('cust_id')->get();
726
        foreach($list as $l)
727
        {
728
            $v = Customers::where('cust_id', $l->cust_id)->first();
729
            if(!$v)
730
            {
731
                $valid = false;
732
                if($this->fix)
733
                {
734
                    $data = CustomerFiles::where('cust_id', $l->cust_id);
735
                    Log::notice('DBCheck - customer_files Foreign key failed.  Deleting data - ', array($data));
736
                    $data->delete();
737
                }
738
            }
739
        }
740
741
        //  Check user_id foreign key
742
        $list  = CustomerFiles::select('user_id')->groupBy('user_id')->get();
743
        foreach($list as $l)
744
        {
745
            if($l->user_id)
746
            {
747
                $v = User::where('user_id', $l->user_id)->withTrashed()->first();
748
                if(!$v)
749
                {
750
                    $valid = false;
751
                    if($this->fix)
752
                    {
753
                        $data = CustomerFiles::where('user_id', $l->user_id);
754
                        Log::notice('DBCheck - customer_files Foreign key failed.  Deleting data - ', array($data));
755
                        $data->delete();
756
                    }
757
                }
758
            }
759
        }
760
761
        return $valid;
762
    }
763
764
    //  customer_favs table has user_id and cust_id foreign keys
765
    protected function customer_favs()
766
    {
767
        $valid = true;
768
769
        //  Check cust_id foreign key
770
        $list  = CustomerFavs::select('cust_id')->groupBy('cust_id')->get();
771
        foreach($list as $l)
772
        {
773
            $v = Customers::where('cust_id', $l->cust_id)->first();
774
            if(!$v)
775
            {
776
                $valid = false;
777
                if($this->fix)
778
                {
779
                    $data = CustomerFavs::where('cust_id', $l->cust_id);
780
                    Log::notice('DBCheck - customer_favs Foreign key failed.  Deleting data - ', array($data));
781
                    $data->delete();
782
                }
783
            }
784
        }
785
786
        //  Check user_id foreign key
787
        $list  = CustomerFavs::select('user_id')->groupBy('user_id')->get();
788
        foreach($list as $l)
789
        {
790
            if($l->user_id)
791
            {
792
                $v = User::where('user_id', $l->user_id)->first();
793
                if(!$v)
794
                {
795
                    $valid = false;
796
                    if($this->fix)
797
                    {
798
                        $data = CustomerFavs::where('user_id', $l->user_id);
799
                        Log::notice('DBCheck - customer_favs Foreign key failed.  Deleting data - ', array($data));
800
                        $data->delete();
801
                    }
802
                }
803
            }
804
        }
805
806
        return $valid;
807
    }
808
809
    //  customer_contacts table has cust_id foreign key
810
    protected function customer_contacts()
811
    {
812
        $valid = true;
813
814
        //  Check cust_id foreign key
815
        $list  = CustomerContacts::select('cust_id')->groupBy('cust_id')->get();
816
        foreach($list as $l)
817
        {
818
            $v = Customers::where('cust_id', $l->cust_id)->first();
819
            if(!$v)
820
            {
821
                $valid = false;
822
                if($this->fix)
823
                {
824
                    $data = CustomerContacts::where('cust_id', $l->cust_id);
825
                    Log::notice('DBCheck - customer_contacts Foreign key failed.  Deleting data - ', array($data));
826
                    $data->delete();
827
                }
828
            }
829
        }
830
831
        return $valid;
832
    }
833
834
    //  customer_contact_phones table has cont_id and phone_type_id table
835
    protected function customer_contact_phones()
836
    {
837
        $valid = true;
838
839
        //  Check cont_id foreign key
840
        $list  = CustomerContactPhones::select('cont_id')->groupBy('cont_id')->get();
841
        foreach($list as $l)
842
        {
843
            $v = CustomerContacts::where('cont_id', $l->cont_id)->first();
844
            if(!$v)
845
            {
846
                $valid = false;
847
                if($this->fix)
848
                {
849
                    $data = CustomerContactPhones::where('cont_id', $l->cont_id);
850
                    Log::notice('DBCheck - customer_contact_phones Foreign key failed.  Deleting data - ', array($data));
851
                    $data->delete();
852
                }
853
            }
854
        }
855
856
        //  Check phone_type_id foreign key
857
        $list  = CustomerContactPhones::select('phone_type_id')->groupBy('phone_type_id')->get();
858
        foreach($list as $l)
859
        {
860
            if($l->user_id)
861
            {
862
                $v = PhoneNumberTypes::where('phone_type_id', $l->phone_type_id)->first();
863
                if(!$v)
864
                {
865
                    $valid = false;
866
                    if($this->fix)
867
                    {
868
                        $data = CustomerContactPhones::where('phone_type_id', $l->phone_type_id);
869
                        Log::notice('DBCheck - customer_contact_phones Foreign key failed.  Deleting data - ', array($data));
870
                        $data->delete();
871
                    }
872
                }
873
            }
874
        }
875
876
        return $valid;
877
    }
878
}
879