/var/www/hkosl.com/littleark/webadmin/models/Customer.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php

class Customer extends BaseSimpleModel
{
    protected 
$table "customer";

    public 
$timestamps true;
    const 
CREATED_AT   'createdate';
    const 
UPDATED_AT   'lastupdate';
    private static 
$currentCustomer;

    protected 
$guarded = ['decrypted'];

    public function 
ProProductUserComments()
    {
        return 
$this->hasMany('ProProductUserComment');
    }

    public function 
coupons()
    {
        return 
$this->hasMany('Coupon');
    }

    public function 
customerPoints()
    {
        return 
$this->hasMany('CustomerPoint');
    }

    public function 
orders()
    {
        return 
$this->hasMany('Order')->whereTemp(0);
    }

    public function 
invoices()
    {
        return 
$this->hasMany('Invoice');
    }

    public function 
cart()
    {
        return 
$this->hasMany('Order')->whereTemp(1);
    }

    public function 
wishlist()
    {
        return 
$this->hasMany('wishlist');
    }

    public function 
refunds()
    {
        return 
$this->hasMany('Refund');
    }

    public function 
currency()
    {
        return 
$this->belongsTo('Currency''currency_code''iso_code');
    }

    public function 
vipGrade()
    {
        return 
$this->belongsTo('VipGrade''vip_grade''code');
    }

    public function 
addresses()
    {
        return 
$this->hasMany('Address')->orderBy('address_type''DESC')->orderBy('default_address''DESC');
    }

    
// public function billing_address(){
    //     return $this->hasOne('Address')->where('address_type', 'BILLING_ADDRESS');
    // }

    // public function delivery_addresses(){
    //     return $this->hasMany('Address')->where('address_type', 'DELIVERY_ADDRESS');
    // }

    
public function fullname($langcode null)
    {
        switch (
$this->type) {
            case 
'BUSINESS':
                
$name $this->first_name;
                break;

            default:
                if (
$this->first_name && $this->last_name) {
                    if (
preg_match("/[\p{Latin}[A-Za-z_.,'\s]+/"$input_line$output_array)) {
                        
$name "{$this->first_name} {$this->last_name}";
                    } else {
                        
$name "{$this->last_name} {$this->first_name}";
                    }
                } elseif (
$this->first_name) {
                    
$name "{$this->first_name}";
                } elseif (
$this->last_name) {
                    
$name "{$this->last_name}";
                } else {
                    
$name "N/A";
                }
                break;
        }

        return 
trim($name);
    }

    public function 
decrypt()
    {
        if (!
$this->decrypted) {
            
$this->decrypted     1;
            
$this->birthday      aes_crypt($this->birthday2);
            
$this->tel           aes_crypt($this->tel2);
            
$this->email         aes_crypt($this->email2);
            
$this->address       aes_crypt($this->address2);
            
$this->identity_type aes_crypt($this->identity_type2);
            
$this->identity_id   aes_crypt($this->identity_id2);
        }
        return 
$this;
    }

    public function 
encrypt()
    {
        if (
$this->decrypted) {
            
$this->decrypted     0;
            
$this->birthday      aes_crypt($this->birthday1);
            
$this->tel           aes_crypt($this->tel1);
            
$this->email         aes_crypt($this->email1);
            
$this->address       aes_crypt($this->address1);
            
$this->identity_type aes_crypt($this->identity_type1);
            
$this->identity_id   aes_crypt($this->identity_id1);
        }
        return 
$this;
    }

    
// update Customer.points based on approved points in customerpoint table
    
public function calcPoints()
    {
        if (
$this->id) {
            
$total 0;
            foreach (
$this->customerPoints()->where('status''APPROVED')->get() as $key => $customerPoint) {
                
$total += $customerPoint->points;
            }
        }
        
$this->points $total;
        return 
$this;
    }

    public function 
createCustomerPointForCompletedInvoice()
    {
        
$invoices $this->invoices()
            ->
whereDeleted(0)
            ->
where('status''PAID')
        
// ->where('status', 'COMPLETED')
            
->where('give_points'0)
            ->
get();

        foreach (
$invoices as $key => $invoice) {
            
$invoice->createCustomerPoint($this);
        }
        
$this->calcPoints()->save();
        
// dq(1, 1);
    
}

    public function 
paymentGateways()
    {
        
// return ['PayVision', 'PayEase', 'PayEaseB2B', 'Trustpay', 'Espay', 'Finpay', 'Doku'];
        
switch ($this->country_code) {
            case 
'CHN':
                if (
$this->type == "BUSINESS") {
                    return [
'PayEaseB2B''Trustpay'];
                }
                return [
'PayVision''PayEase'];
                break;

            case 
'IDN':
                return [
'Espay''Finpay''Doku'];
                break;

            default:
                return [
'PayVision'];
                break;
        }
    }

    public function 
supportOrderNegotiation()
    {
        if (
$this->type == "BUSINESS") {
            return 
true;
        }
        return 
false;
    }

    public static function 
generateCodeById($id)
    {
        return 
MasterSetting::get('CUSTOMER_NUMBER_PREFIX') . str_pad($id8"0"STR_PAD_LEFT);
    }

    public static function 
current($forceRefresh false)
    {
        if (!
self::$currentCustomer || $forceRefresh) {
            
self::$currentCustomer self::find($_SESSION["customer_id"]);
        }
        return 
self::$currentCustomer ?: (new self);
    }
}