1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Domains\Customers; |
4
|
|
|
|
5
|
|
|
use App\Customers; |
6
|
|
|
use Illuminate\Support\Facades\Auth; |
7
|
|
|
use Illuminate\Support\Facades\Log; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class SetCustomerDetails |
11
|
|
|
{ |
12
|
6 |
|
public function createCustomer($request) |
13
|
|
|
{ |
14
|
6 |
|
if($request->parent_id != null) |
15
|
|
|
{ |
16
|
2 |
|
$request->parent_id = $this->validateParentID($request->parent_id); |
17
|
|
|
} |
18
|
|
|
|
19
|
6 |
|
$newCust = Customers::create($request->toArray()); |
20
|
|
|
|
21
|
6 |
|
return $newCust->cust_id; |
22
|
|
|
} |
23
|
|
|
|
24
|
4 |
|
public function updateCustomer($custID, $details) |
25
|
|
|
{ |
26
|
4 |
|
Customers::find($custID)->update($details->toArray()); |
27
|
4 |
|
return true; |
28
|
|
|
} |
29
|
|
|
|
30
|
4 |
|
public function deactivateCustomer($custID) |
31
|
|
|
{ |
32
|
4 |
|
Customers::find($custID)->delete(); |
33
|
4 |
|
return true; |
34
|
|
|
} |
35
|
|
|
|
36
|
6 |
|
public function linkParent($request) |
37
|
|
|
{ |
38
|
6 |
|
if(isset($request->parent_id)) |
39
|
|
|
{ |
40
|
2 |
|
return $this->attachParent($request->cust_id, $request->parent_id); |
41
|
|
|
} |
42
|
|
|
|
43
|
4 |
|
return $this->detachParent($request->cust_id); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// Determine if the assigned parent has a partent of its own |
47
|
4 |
|
protected function validateParentID($parentID) |
48
|
|
|
{ |
49
|
4 |
|
$parent = Customers::find($parentID); |
50
|
|
|
|
51
|
4 |
|
return $parent->parent_id ? $parent->parent_id : $parentID; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Link the parent to the customer |
55
|
2 |
|
protected function attachParent($custID, $parentID) |
56
|
|
|
{ |
57
|
2 |
|
$parent = $this->validateParentID($parentID); |
58
|
2 |
|
Customers::find($custID)->update(['parent_id' => $parent]); |
59
|
|
|
|
60
|
2 |
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// Remove the parent from the customer |
64
|
4 |
|
protected function detachParent($custID) |
65
|
|
|
{ |
66
|
4 |
|
Customers::find($custID)->update(['parent_id' => null]); |
67
|
|
|
|
68
|
4 |
|
return true; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|