Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
15.38% covered (danger)
15.38%
2 / 13
22.22% covered (danger)
22.22%
2 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
Customer
15.38% covered (danger)
15.38%
2 / 13
22.22% covered (danger)
22.22%
2 / 9
99.24
0.00% covered (danger)
0.00%
0 / 1
 auditName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 sales
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 orders
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 addresses
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 defaultAddress
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 hasAccount
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 group
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAll
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 getById
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace App\Models\admin;
4
5use App\Models\CustomerAddress;
6use App\Models\CustomerGroup;
7use App\Models\Order;
8use App\Support\Auditable;
9use Illuminate\Auth\Authenticatable;
10use Illuminate\Auth\Passwords\CanResetPassword;
11use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
12use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
13use Illuminate\Database\Eloquent\Factories\HasFactory;
14use Illuminate\Database\Eloquent\Model;
15use Illuminate\Notifications\Notifiable;
16
17/**
18 * A customer row is created for every checkout, logged-in or guest — that
19 * doesn't change here. What's new (Phase 7): a customer MAY also have a
20 * password and sign in (guard "customer") to see their order history,
21 * manage addresses, etc. A null password just means "guest, never
22 * registered" — nothing about existing checkout behaviour changes.
23 */
24class Customer extends Model implements AuthenticatableContract, CanResetPasswordContract
25{
26    use Auditable, Authenticatable, CanResetPassword, HasFactory, Notifiable;
27
28    protected $table = 'customers';
29
30    protected $fillable = [
31        'name',
32        'email',
33        'password',
34        'phone',
35        'country_id',
36        'city_id',
37        'address',
38        'description',
39        'pictures',
40        'customer_code',
41        'created_by',
42        'customer_group_id',
43    ];
44
45    protected $hidden = ['password', 'remember_token'];
46
47    protected $casts = ['email_verified_at' => 'datetime'];
48
49    protected function auditName(): string
50    {
51        return 'customer';
52    }
53
54    public function sales()
55    {
56        return $this->hasMany(Sale::class);
57    }
58
59    public function orders()
60    {
61        return $this->hasMany(Order::class, 'customer_id');
62    }
63
64    public function addresses()
65    {
66        return $this->hasMany(CustomerAddress::class, 'customer_id')->orderByDesc('is_default');
67    }
68
69    public function defaultAddress()
70    {
71        return $this->hasOne(CustomerAddress::class, 'customer_id')->where('is_default', true);
72    }
73
74    public function hasAccount(): bool
75    {
76        return ! empty($this->password);
77    }
78
79    /** Pricing tier (Phase 7) — null means "retail", the price everyone sees. */
80    public function group()
81    {
82        return $this->belongsTo(CustomerGroup::class, 'customer_group_id');
83    }
84
85    /**
86     * ###########################################
87     * #        Repository Methods Start         #
88     * ###########################################
89     * */
90    public static function getAll($relation = false, $pluck = false)
91    {
92        $query = self::query();
93        ($relation ? $query->with('sales') : $query)->get();
94        $data = ($pluck ? $query->pluck('name', 'id') : $query->get());
95
96        return $data;
97
98    }
99
100    public static function getById($id, $relation = false)
101    {
102        $query = self::query()->where('id', $id);
103
104        return ($relation ? $query->with('sales') : $query)->first();
105    }
106    /**
107     * ###########################################
108     * #        Repository Methods END           #
109     * ###########################################
110     * */
111}