Customer.php
3.02 KB
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
<?php
/**
* @Author: irul
* @Date: 2019-09-23 20:33:14
* @Last Modified by: irul
* @Last Modified time: 2019-10-01 23:43:20
*/
namespace Integrasi\Reklame\Base\Models;
use \Illuminate\Database\Capsule\Manager as DB;
class Customer extends AbstractModel
{
protected $table = 'pad_customer';
protected $visible = array(
'id',
'npwpd',
'rp',
'pb',
'reg_date',
'customernm',
// 'kecamatan_id',
'kecamatan',
// 'kelurahan_id',
'kelurahan',
'kabupaten',
'alamat',
'kodepos',
'telphone',
'wpnama',
'wpalamat',
'wpkelurahan',
'wpkecamatan',
'wpkabupaten',
'wptelp',
'wpkodepos',
);
protected $appends = array(
'kecamatan',
'kelurahan',
);
protected $guarded = array();
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
return $model->_onCreating($model);
});
}
public function _onCreating($attr)
{
/**
* Increment formno.
*/
// $maxFormno = Customer::select(DB::raw('max(formno) as nomor'))
$maxFormno = call_user_func($this->getParentNamespace() . '\Customer::select',
DB::raw('max(formno) as nomor'))
->where(DB::raw('rp'), trim($this->attributes['rp']))
->first();
$formno = ($maxFormno->nomor ?: 0) + 1;
$this->attributes['formno'] = $formno;
$this->attributes['create_uid'] = \Api\AuthBasic::getUid();
/**
* Get kecamatannm
*/
// $attr->wpkecamatan = trim(Kecamatan::find($attr->wpkecamatan_id)->kecamatannm);
$attr->wpkecamatan = trim(call_user_func($this->getParentNamespace() . '\Kecamatan::find',
$attr->wpkecamatan_id)->kecamatannm);
/**
* Get kelurahannm
*/
// $attr->wpkelurahan = trim(Kelurahan::find($attr->wpkelurahan_id)->kelurahannm);
$attr->wpkelurahan = trim(call_user_func($this->getParentNamespace() . '\Kelurahan::find',
$attr->wpkelurahan_id)->kelurahannm);
/**
* Unset wpkecamatan_id dan wpkelurahan_id
* karena tidak ada fieldnya pada table pad_customer
*/
unset($attr->wpkecamatan_id);
unset($attr->wpkelurahan_id);
return true;
}
// == ACCESSORS == //
public function getNpwpdAttribute($value)
{
// return strtolower($value);
$qry = "select get_npwpd(?, false) as npwpd";
$row = DB::select(DB::raw($qry), array($this->attributes['id']));
return $row[0]->npwpd;
}
public function getKecamatanAttribute($value)
{
// $row = Kecamatan::find($this->attributes['kecamatan_id']);
$row = call_user_func($this->getParentNamespace() . '\Kecamatan::find',
$this->attributes['kecamatan_id']);
return $row->kecamatannm;
}
public function getKelurahanAttribute($value)
{
// $row = Kelurahan::find($this->attributes['kelurahan_id']);
$row = call_user_func($this->getParentNamespace() . '\Kelurahan::find',
$this->attributes['kelurahan_id']);
return $row->kelurahannm;
}
// == QUERY SCOPES == //
public function scopeFindByNpwpd($query, $npwpd)
{
// $data = Customer::where(DB::raw('get_npwpd(id, false)'), array($npwpd))->toSql();
// print_r($data); die();
return $query->where(DB::raw('get_npwpd(id, false)'), array($npwpd));
}
}