Skip to content

Commit 351a325

Browse files
committed
Merge pull request #107 from diepxuan/fix/input-khachhang-component
fix: InputKhachhang component và ArDmKh model
1 parent bf33004 commit 351a325

2 files changed

Lines changed: 125 additions & 23 deletions

File tree

src/Models/ArDmKh.php

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,93 @@
88
* @author Tran Ngoc Duc <ductn@diepxuan.com>
99
* @author Tran Ngoc Duc <caothu91@gmail.com>
1010
*
11-
* @lastupdate 2026-02-27 15:03:13
11+
* @lastupdate 2026-03-11 16:15:00
1212
*/
1313

1414
namespace Diepxuan\Simba\Models;
1515

16-
use Diepxuan\Simba\SModel\ArDmKh as Model;
16+
use Diepxuan\Simba\SModel\ArDmKhModel as Model;
1717
use Diepxuan\Simba\StoredProcedures\AsGetSoDuKh;
18+
use Illuminate\Database\Eloquent\Builder;
1819
use Illuminate\Database\Eloquent\Relations\HasMany;
1920

21+
/**
22+
* Model ArDmKh - Danh mục khách hàng.
23+
*
24+
* Mở rộng từ ArDmKhModel với các scope và helper methods.
25+
*/
2026
class ArDmKh extends Model
2127
{
22-
protected $casts = [
23-
'isKh' => 'boolean',
24-
'isNcc' => 'boolean',
25-
'isNv' => 'boolean',
26-
];
28+
/**
29+
* Boot model và thêm global scopes.
30+
*/
31+
protected static function booted(): void
32+
{
33+
parent::booted();
34+
35+
// Global scope: mặc định chỉ lấy các bản ghi đang sử dụng (ksd = true)
36+
static::addGlobalScope('ksd', static function (Builder $query): void {
37+
$query->where('ksd', true);
38+
});
39+
}
40+
41+
/**
42+
* Scope: Chỉ lấy khách hàng (isKh = true).
43+
*/
44+
public function scopeLaKhachHang(Builder $query): Builder
45+
{
46+
return $query->where('isKh', true);
47+
}
48+
49+
/**
50+
* Scope: Chỉ lấy nhà cung cấp (isNcc = true).
51+
*/
52+
public function scopeLaNhaCungCap(Builder $query): Builder
53+
{
54+
return $query->where('isNcc', true);
55+
}
56+
57+
/**
58+
* Scope: Chỉ lấy nhân viên (isNv = true).
59+
*/
60+
public function scopeLaNhanVien(Builder $query): Builder
61+
{
62+
return $query->where('isNv', true);
63+
}
64+
65+
/**
66+
* Scope: Tìm kiếm theo mã khách hàng hoặc tên.
67+
*/
68+
public function scopeSearch(Builder $query, string $search): Builder
69+
{
70+
return $query->where(static function (Builder $q) use ($search): void {
71+
$q->where('ma_kh', 'like', "%{$search}%")
72+
->orWhere('ten_kh', 'like', "%{$search}%")
73+
->orWhere('dia_chi', 'like', "%{$search}%")
74+
->orWhere('tel', 'like', "%{$search}%");
75+
});
76+
}
77+
78+
/**
79+
* Scope: Sắp xếp theo mã khách hàng.
80+
*/
81+
public function scopeOrderByMaKh(Builder $query): Builder
82+
{
83+
return $query->orderBy('ma_kh');
84+
}
2785

86+
/**
87+
* Quan hệ với GlCt (chi tiết sổ cái).
88+
*/
2889
public function glCts(): HasMany
2990
{
3091
return $this->hasMany(GlCt::class, 'ma_kh', 'ma_kh');
3192
}
3293

3394
/**
3495
* Gọi stored procedure AsGetSoDuKh để lấy số dư khách hàng.
96+
*
97+
* @param array $params Tham số: pMa_Cty, pMa_kh/pMa_Kh, pNgay, pTk
3598
*/
3699
public static function AsGetSoDuKh(array $params): float
37100
{
@@ -43,16 +106,25 @@ public static function AsGetSoDuKh(array $params): float
43106
]);
44107
}
45108

109+
/**
110+
* Accessor: Kiểm tra có phải khách hàng không.
111+
*/
46112
public function getIsKhachHangAttribute(): bool
47113
{
48114
return (bool) $this->isKh;
49115
}
50116

117+
/**
118+
* Accessor: Kiểm tra có phải nhà cung cấp không.
119+
*/
51120
public function getIsNhaCungCapAttribute(): bool
52121
{
53122
return (bool) $this->isNcc;
54123
}
55124

125+
/**
126+
* Accessor: Kiểm tra có phải nhân viên không.
127+
*/
56128
public function getIsNhanVienAttribute(): bool
57129
{
58130
return (bool) $this->isNv;

src/SModel/ArDmKhModel.php

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
11
<?php
22

3+
declare(strict_types=1);
4+
5+
/*
6+
* @copyright © 2019 Dxvn, Inc.
7+
*
8+
* @author Tran Ngoc Duc <ductn@diepxuan.com>
9+
* @author Tran Ngoc Duc <caothu91@gmail.com>
10+
*
11+
* @lastupdate 2026-03-11 16:10:00
12+
*/
13+
314
namespace Diepxuan\Simba\SModel;
415

5-
use Illuminate\Database\Eloquent\Model;
616
use Diepxuan\Simba\SModel\SModel;
717

18+
/**
19+
* Model ArDmKh - Danh mục khách hàng.
20+
*
21+
* Gộp từ ArDmKh.php và ArDmKhModel.php
22+
*/
823
class ArDmKhModel extends SModel
924
{
25+
/**
26+
* Indicates if the IDs are auto-incrementing.
27+
*
28+
* @var bool
29+
*/
30+
public $incrementing = false;
31+
1032
/**
1133
* The table associated with the model.
1234
*
@@ -15,18 +37,26 @@ class ArDmKhModel extends SModel
1537
protected $table = 'ArDmKh';
1638

1739
/**
18-
* The primary key for the model.
40+
* The primary key associated with the table.
41+
*
42+
* @var string
43+
*/
44+
protected $primaryKey = 'ma_kh';
45+
46+
/**
47+
* The "type" of the primary key ID.
1948
*
2049
* @var string
2150
*/
22-
protected $primaryKey = 'ma_cty';
51+
protected $keyType = 'string';
2352

2453
/**
2554
* Indicates if the model should be timestamped.
2655
*
2756
* @var bool
2857
*/
29-
public $timestamps = false;
58+
public const CREATED_AT = 'cdate';
59+
public const UPDATED_AT = 'ldate';
3060

3161
/**
3262
* The attributes that are mass assignable.
@@ -72,7 +102,7 @@ class ArDmKhModel extends SModel
72102
'cdate',
73103
'cuser',
74104
'ldate',
75-
'luser'
105+
'luser',
76106
];
77107

78108
/**
@@ -81,17 +111,17 @@ class ArDmKhModel extends SModel
81111
* @var array
82112
*/
83113
protected $casts = [
84-
'gh_no' => 'float',
85-
'han_ck' => 'float',
86-
'tl_ck' => 'float',
87-
'han_tt' => 'float',
88-
'ls_qh' => 'float',
114+
'gh_no' => 'float',
115+
'han_ck' => 'float',
116+
'tl_ck' => 'float',
117+
'han_tt' => 'float',
118+
'ls_qh' => 'float',
89119
'tinh_dt_nb' => 'boolean',
90-
'isKh' => 'boolean',
91-
'isNcc' => 'boolean',
92-
'isNv' => 'boolean',
93-
'ksd' => 'boolean',
94-
'cdate' => 'datetime',
95-
'ldate' => 'datetime',
120+
'isKh' => 'boolean',
121+
'isNcc' => 'boolean',
122+
'isNv' => 'boolean',
123+
'ksd' => 'boolean',
124+
'cdate' => 'datetime',
125+
'ldate' => 'datetime',
96126
];
97127
}

0 commit comments

Comments
 (0)