Completed
Push — dev5 ( 67318d...863f68 )
by Ron
13:05
created

CustomerContactsController::update()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 33
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 3
nop 2
dl 0
loc 33
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Customers;
4
5
use App\Customers;
6
use App\PhoneNumberType;
7
use App\CustomerContacts;
8
use Illuminate\Http\Request;
9
use App\CustomerContactsView;
10
use App\CustomerContactPhones;
11
use JeroenDesloovere\VCard\VCard;
12
use Illuminate\Support\Facades\Log;
13
use Illuminate\Support\Facades\Auth;
14
use App\Http\Controllers\Controller;
15
use Illuminate\Support\Facades\Route;
16
17
class CustomerContactsController extends Controller
18
{
19
    public function __construct()
20
    {
21
        $this->middleware('auth');
22
    }
23
24
    //  Store a new customer contact
25
    public function store(Request $request)
26
    {
27
        $request->validate([
28
            'custID' => 'required',
29
            'name'   => 'required'
30
        ]);
31
        
32
        $cont = CustomerContacts::create([
33
            'cust_id' => $request->custID,
34
            'name'    => $request->name,
35
            'email'   => $request->email
36
        ]);
37
        
38
        $contID = $cont->cont_id;
39
        
40
        foreach($request->numbers['type'] as $key => $num)
41
        {
42
            if(!empty($request->numbers['number'][$key]))
43
            {
44
                CustomerContactPhones::create([
45
                    'cont_id'       => $contID,
46
                    'phone_type_id' => $request->numbers['type'][$key],
47
                    'phone_number'  => PhoneNumberType::cleanPhoneNumber($request->numbers['number'][$key]),
48
                    'extension'     => isset($request->numbers['ext'][$key]) ? $request->numbers['ext'][$key] : null
49
                ]);
50
            }
51
        }
52
        
53
//        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
54
//        Log::debug('Submitted Data - ', $request->toArray());
55
        Log::info('New Customer Contact Created for Cust ID - '.$request->custID.'.  Contact ID-'.$contID);
56
        return response()->json(['success' => true]);
57
    }
58
59
    //  Get the contacts for a customer
60
    public function show($id)
61
    {
62
        $contacts = CustomerContacts::where('cust_id', $id)->with('CustomerContactsView')->get();
63
        
64
        $contArr = [];
65
        foreach($contacts as $cont)
66
        {
67
            $phoneNum = [];
68
            foreach($cont->CustomerContactsView as $num)
69
            {
70
                $phoneNum[] = [
71
                    'class'         => $num->icon_class,
72
                    'type'          => $num->description,
73
                    'type_id'       => $num->phone_type_id,
74
                    'number'        => $num->phone_number,
75
                    'num_formatted' => PhoneNumberType::readablePhoneNumber($num->phone_number),
76
                    'ext'           => $num->extension
77
                ];
78
            }
79
            
80
            $contArr[] = [
81
                'cont_id' => $cont->cont_id,
82
                'name'    => $cont->name,
83
                'numbers' => $phoneNum,
84
                'email'   => $cont->email
85
            ];
86
        }
87
        
88
//        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
89
//        Log::debug('Fetched Data - ', $contacts->toArray());
90
        return response()->json($contArr);
91
    }
92
93
    //  Edit function will actually download the contact information in V-Card format
94
    public function edit($id)
95
    {
96
        $contact  = CustomerContacts::find($id);
97
        $numbers  = CustomerContactsView::where('cont_id', $id)->get();
98
        $custData = Customers::find($contact->cust_id);
99
        
100
        $contactName = explode(' ', $contact->name);
101
        $firstName   = $contactName[0];
102
        $lastName    = isset($contactName[1]) ? $contactName[1] : '';
103
        $additional  = '';
104
        $prefix      = '';
105
        $suffix      = '';
106
        
107
        $vcard = new VCard();
108
        $vcard->addName($lastName, $firstName, $additional, $prefix, $suffix);
109
        $vcard->addCompany($custData->name);
110
        $vcard->addEmail($contact->email);
111
        $vcard->addAddress(null, null, $custData->address, $custData->city, $custData->state, $custData->zip, null);
112
        
113
        if(!empty($numbers))
114
        {
115
            foreach($numbers as $phone)
116
            {
117
                $vcard->addPhoneNumber($phone->phone_number, $phone->description);
118
            }
119
        }
120
        
121
//        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
122
        Log::info('Customer Contact Downloaded - Contact ID-'.$id);
123
        
124
        return $vcard->download();
1 ignored issue
show
Bug introduced by
Are you sure the usage of $vcard->download() targeting JeroenDesloovere\VCard\VCard::download() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
125
    }
126
127
    //  Update an existing Customer Contact
128
    public function update(Request $request, $id)
129
    {
130
        $request->validate([
131
            'custID' => 'required',
132
            'name'   => 'required'
133
        ]);
134
        
135
        CustomerContacts::find($id)->update([
136
            'name'    => $request->name,
137
            'email'   => $request->email
138
        ]);
139
        
140
        $contID = $id;
141
        
142
        //  Clear all contact phone numbers and re-enter them
143
        CustomerContactPhones::where('cont_id', $id)->delete();
144
        foreach($request->numbers['type'] as $key => $num)
145
        {
146
            if(!empty($request->numbers['number'][$key]))
147
            {
148
                CustomerContactPhones::create([
149
                    'cont_id'       => $contID,
150
                    'phone_type_id' => $request->numbers['type'][$key],
151
                    'phone_number'  => PhoneNumberType::cleanPhoneNumber($request->numbers['number'][$key]),
152
                    'extension'     => isset($request->numbers['ext'][$key]) ? $request->numbers['ext'][$key] : null
153
                ]);
154
            }
155
        }
156
        
157
//        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
158
//        Log::debug('Submitted Data - ', $request->toArray());
159
        Log::info('Customer Contact Updated for Cust ID - '.$request->custID.'.  Contact ID-'.$contID);
160
        return response()->json(['success' => true]);
161
    }
162
163
    //  Delete an existing contact
164
    public function destroy($id)
165
    {
166
        $cont = CustomerContacts::find($id);
167
        
168
//        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
169
        Log::info('Customer Contact deleted for Customer ID-'.$cont->cust_id.' by User ID-'.Auth::user()->user_id.'. Deleted Contact ID-'.$id);
170
        
171
        $cont->delete();
172
        
173
        return response()->json(['success' => true]);
174
    }
175
}
176