Completed
Push — dev5 ( db4a20...1ecb99 )
by Ron
09:23
created

CustomerController::toggleFav()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 2
dl 0
loc 17
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Customers;
4
5
use App\Customers;
6
use App\SystemTypes;
7
use App\CustomerFavs;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\Log;
10
use Illuminate\Support\Facades\Auth;
11
use App\Http\Controllers\Controller;
12
13
class CustomerController extends Controller
14
{
15
    public function __construct()
16
    {
17
        $this->middleware('auth');
18
    }
19
    
20
    //  Landing page to search for customer
21
    public function index()
22
    {
23
        $sysTypes = SystemTypes::all();
24
        
25
        $sysArr = [];
26
        foreach($sysTypes as $sys)
27
        {
28
            $sysArr[] = $sys->name;
29
        }
30
        
31
        return view('customer.index', [
32
            'sysTypes' => $sysArr
33
        ]);
34
    }
35
    
36
    //  Search for the customer based on their ID - For new file link form
37
    public function searchID($id)
38
    {
39
        $id = urldecode($id);
40
        if($id === 'NULL')
41
        {
42
            $id = '';
43
        }
44
        
45
        //  Determine if a customer number/name has already been entered
46
        if(!empty($id))
47
        {
48
            $split = explode(' ', $id);
49
            if(isset($split[1]) && $split[1] === '-')
50
            {
51
                $id = $split[0];
52
            }
53
        }
54
        
55
        $res = Customers::where('cust_id', 'like', '%'.$id.'%')
56
            ->orWhere('name', 'like', '%'.$id.'%')
57
            ->where('active', 1)
58
            ->orderBy('name')
59
            ->get();
60
        
61
        return response()->json($res);
62
    }
63
    
64
    //  Return a full JSON array of the available customers
65
    public function search()
66
    {
67
        $customers = Customers::
68
                with('CustomerSystems.SystemTypes')
69
                ->get();
70
        
71
        $custList = [];
72
        foreach($customers as $cust)
73
        {
74
            $sysArr = '';
75
            foreach($cust->CustomerSystems as $sys)
76
            {
77
                $sysArr .= $sys->SystemTypes->name.'<br />';
78
            }
79
            
80
            $custList[] = [
81
                'cust_id' => $cust->cust_id,
82
                'name' => $cust->name,
83
                'dba'  => $cust->dba_name,
84
                'city' => $cust->city.', '.$cust->state,
85
                'url'  => route('customer.details', [$cust->cust_id, urlencode($cust->name)]),
86
                'sys'  => $sysArr
87
            ];            
88
        }
89
        
90
        return response()->json($custList);
91
    }
92
    
93
    //  Check to see if a customer ID already exists
94
    public function checkID($id)
95
    {
96
        $cust = Customers::find($id);
97
        
98
        if($cust === null)
99
        {
100
            return response()->json(['dup' => false]);
101
        }
102
        
103
        return response()->json(['dup' => true, 'url' => route('customer.details', [$cust->cust_id, urlencode($cust->name)])]);
104
    }
105
    
106
    //  Toggle whether or not the customer is listed as a user favorite
107
    public function toggleFav($action, $id)
108
    {
109
        switch ($action)
110
        {
111
            case 'add':
112
                CustomerFavs::create([
113
                    'user_id' => Auth::user()->user_id,
114
                    'cust_id' => $id
115
                ]);
116
                break;
117
            case 'remove':
118
                $custFav = CustomerFavs::where('user_id', Auth::user()->user_id)->where('cust_id', $id)->first();
119
                $custFav->delete();
120
                break;
121
        }
122
        
123
        return response()->json(['success' => true]);
124
    }
125
}
126