Passed
Push — master ( ac9f24...836696 )
by Ron
01:56
created

CustomerController::checkId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Support\Facades\Log;
8
use App\SystemTypes;
9
use App\SystemCategories;
10
use App\CustomerSystems;
11
use App\CustomerNotes;
12
use App\CustomerFavs;
13
use App\Customers;
14
use PDF;
15
16
class CustomerController extends Controller
17
{
18
    //  Only authorized users have access
19
    public function __construct()
20
    {
21
        $this->middleware('auth');
22
    }
23
    
24
    //  Landing page brings up the customer search form
25
    public function index()
26
    {
27
        $systems = SystemCategories::with('SystemTypes')
28
            ->orderBy('cat_id', 'asc')
29
            ->get();
30
        
31
        $sysArr = [];
32
        foreach($systems as $sys)
33
        {
34
            foreach($sys->SystemTypes as $s)
35
            {
36
                $sysArr[$sys->name][$s->sys_id] = $s->name;
37
            }
38
        }
39
        
40
        return view('customer.index', [
41
            'systems' => $sysArr
42
        ]);
43
    }
44
    
45
    //  Search for a customer
46
    public function search(Request $request)
47
    {
48
        //  Run different request based on if system field is filled out or not
49
        if(!empty($request->system))
50
        {
51
            $customerData = Customers::where('name', 'like', '%'.$request->customer.'%')
52
                ->where('city', 'like', '%'.$request->city.'%')
53
54
                ->with('CustomerSystems.SystemTypes')
55
                ->whereHas('CustomerSystems', function($q) use($request)
56
                {
57
                   $q->where('sys_id', $request->system);
58
                })
59
                ->get();
60
        }
61
        else
62
        {
63
            $customerData = Customers::where('name', 'like', '%'.$request->customer.'%')
64
                ->where('city', 'like', '%'.$request->city.'%')
65
                ->with('CustomerSystems.SystemTypes')
66
                ->get();
67
        }
68
69
        return view('customer.searchResults', [
70
            'results' => $customerData
71
        ]);
72
    }
73
    
74
    //  Check to see if a customer ID already exists
75
    public function checkId($id)
76
    {
77
        $cust = Customers::find($id);
78
        
79
        if($cust === null)
80
        {
81
            return 'false';
82
        }
83
        
84
        return urlencode($cust->name);
85
    }
86
    
87
    //  Toggle whether or not the customer is listed as a user favorite
88
    public function toggleFav($action, $custID)
89
    {
90
        switch ($action)
91
        {
92
            case 'add':
93
                CustomerFavs::create([
94
                    'user_id' => Auth::user()->user_id,
95
                    'cust_id' => $custID
96
                ]);
97
                break;
98
            case 'remove':
99
                $custFav = CustomerFavs::where('user_id', Auth::user()->user_id)->where('cust_id', $custID)->first();
100
                $custFav->delete();
101
                break;
102
        }        
103
    }
104
    
105
    //  Download a note as a PDF file
106
    public function generatePDF($noteID)
107
    {
108
        $note = CustomerNotes::find($noteID);
109
        $cust = Customers::find($note->cust_id);
110
        
111
        $pdf = PDF::loadView('pdf.customerNote', [
112
            'cust_name'   => $cust->name,
113
            'note_subj'   => $note->subject,
114
            'description' => $note->description,
115
        ]);
116
        
117
        return $pdf->download($cust->name.' - Note: '.$note->subject.'.pdf');
118
    }
119
}
120