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
1414namespace Diepxuan \Simba \Models ;
1515
16- use Diepxuan \Simba \SModel \ArDmKh as Model ;
16+ use Diepxuan \Simba \SModel \ArDmKhModel as Model ;
1717use Diepxuan \Simba \StoredProcedures \AsGetSoDuKh ;
18+ use Illuminate \Database \Eloquent \Builder ;
1819use 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+ */
2026class 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 ;
0 commit comments