Skip to content

Commit e14a449

Browse files
caothu159portal-bot
andcommitted
refactor(simba): move catalog business methods to catalog concerns (#222)
Co-authored-by: portal-bot <bot@portal>
1 parent 2c25c0b commit e14a449

3 files changed

Lines changed: 59 additions & 276 deletions

File tree

src/Models/InDmKho.php

Lines changed: 32 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @author Tran Ngoc Duc <ductn@diepxuan.com>
99
* @author Tran Ngoc Duc <caothu91@gmail.com>
1010
*
11-
* @lastupdate 2025-06-20 13:08:16
11+
* @lastupdate 2026-06-22
1212
*/
1313

1414
namespace Diepxuan\Simba\Models;
@@ -17,12 +17,22 @@
1717
use Diepxuan\Simba\SModel\InDmKhoModel as Model;
1818
use Illuminate\Database\Eloquent\Builder;
1919
use Illuminate\Support\Collection;
20-
use Illuminate\Support\Facades\DB;
2120

2221
/**
2322
* Class InDmKho.
2423
*
2524
* This class represents the model for warehouse master data.
25+
*
26+
* Behavior nghiệp vụ (tồn kho theo vật tư / giá trị tồn kho) đã được tách
27+
* sang `Diepxuan\Catalog\Models\Concerns\HasInDmKhoInventoryOperations`
28+
* và gắn vào `Diepxuan\Catalog\Models\InDmKho`.
29+
*
30+
* Còn giữ ở đây:
31+
* - Global scope filter (ma_cty, ma_kho, ten_kho, kho_dl, ksd).
32+
* - Scope `active`, `isAgency`.
33+
* - Quan hệ Simba-Simba: glDmTk, inDmViTri, inCtNhap, inCtXuat.
34+
* - Stored procedure wrapper: getAsINGetDMKHO, getAsINRptCD01
35+
* (Catalog\InDmKho::getAsINGetDMKHO gọi lại qua parent::).
2636
*/
2737
class InDmKho extends Model
2838
{
@@ -115,121 +125,40 @@ public function inCtXuat()
115125
}
116126

117127
/**
118-
* Tính tổng tồn kho theo vật tư
119-
* Sử dụng class AsINRptCD02 thay vì gọi stored procedure trực tiếp
120-
*/
121-
public function getInventoryByProduct(string $maVt, ?string $ngay = null): array
122-
{
123-
$params = [
124-
'pMa_Cty' => $this->ma_cty,
125-
'pMa_vt' => $maVt,
126-
'pMa_kho' => $this->ma_kho,
127-
'pNgay' => $ngay ?? date('Y-m-d'),
128-
];
129-
130-
$result = \Diepxuan\Simba\StoredProcedures\AsINRptCD02::call([
131-
'pMa_Cty' => $this->ma_cty,
132-
'pMa_vt' => $maVt,
133-
'pMa_kho' => $this->ma_kho,
134-
'pNgay' => $ngay ?? date('Y-m-d'),
135-
// Các tham số khác để null
136-
'pMa_vitri' => null,
137-
'pTk_vt' => null,
138-
'pMa_nhvt' => null,
139-
'pDVT' => null,
140-
'pNgoai_te' => null,
141-
'pDk_Ck' => null,
142-
'pMa_lo' => null,
143-
'pQuaToiThieu' => null,
144-
'pQuaToiDa' => null,
145-
'pSysMsg1' => null,
146-
]);
147-
148-
return $result->isNotEmpty() ? (array) $result->first() : [];
149-
}
150-
151-
/**
152-
* Lấy danh sách vật tư tồn kho
153-
* Sử dụng class AsINRptCD02 thay vì gọi stored procedure trực tiếp
154-
*/
155-
public function getInventoryList(?string $maNhvt = null, ?string $ngay = null): Collection
156-
{
157-
return \Diepxuan\Simba\StoredProcedures\AsINRptCD02::call([
158-
'pMa_Cty' => $this->ma_cty,
159-
'pMa_kho' => $this->ma_kho,
160-
'pMa_nhvt' => $maNhvt ?? '',
161-
'pNgay' => $ngay ?? date('Y-m-d'),
162-
// Các tham số khác để null
163-
'pMa_vt' => null,
164-
'pMa_vitri' => null,
165-
'pTk_vt' => null,
166-
'pDVT' => null,
167-
'pNgoai_te' => null,
168-
'pDk_Ck' => null,
169-
'pMa_lo' => null,
170-
'pQuaToiThieu' => null,
171-
'pQuaToiDa' => null,
172-
'pSysMsg1' => null,
173-
]);
174-
}
175-
176-
/**
177-
* Tính giá trị tồn kho
178-
*/
179-
public function getInventoryValue(?string $ngay = null): float
180-
{
181-
$inventoryList = $this->getInventoryList(null, $ngay);
182-
183-
$totalValue = 0;
184-
foreach ($inventoryList as $item) {
185-
$totalValue += ($item->sl_ton ?? 0) * ($item->gia_nt2 ?? 0);
186-
}
187-
188-
return $totalValue;
189-
}
190-
191-
/**
192-
* Gọi stored procedure asINGetDMKHO để lấy danh sách kho
193-
* Sử dụng class AsINGetDMKHO thay vì gọi stored procedure trực tiếp
128+
* Gọi stored procedure asINGetDMKHO để lấy danh sách kho.
194129
*/
195130
public static function getAsINGetDMKHO(array $params): Collection
196131
{
197-
// Sử dụng AsINGetDMKHO class để gọi stored procedure
198-
// Class sẽ xử lý đúng số lượng tham số (3 tham số thực tế)
199132
return \Diepxuan\Simba\StoredProcedures\AsINGetDMKHO::call([
200133
'pMa_Cty' => $params['pMa_Cty'] ?? '',
201134
'pMa_kho' => $params['pMa_kho'] ?? null,
202135
'pStruct' => $params['pStruct'] ?? null,
203-
'pLanguage' => $params['pLanguage'] ?? null, // Tham số này sẽ bị bỏ qua trong class
136+
'pLanguage' => $params['pLanguage'] ?? null,
204137
]);
205138
}
206139

207140
/**
208-
* Gọi stored procedure asINRptCD01 để lấy báo cáo tồn kho chi tiết
209-
* Sử dụng class AsINRptCD01 thay vì gọi stored procedure trực tiếp
141+
* Gọi stored procedure asINRptCD01 để lấy báo cáo tồn kho chi tiết.
210142
*/
211143
public static function getAsINRptCD01(array $params): Collection
212144
{
213-
// Sử dụng AsINRptCD01 class để gọi stored procedure
214-
// Map pMa_Nt to pNgoai_te (foreign currency)
215145
return \Diepxuan\Simba\StoredProcedures\AsINRptCD01::call([
216-
'pMa_Cty' => $params['pMa_Cty'] ?? '',
217-
'pMa_kho' => $params['pMa_kho'] ?? '',
218-
'pMa_vt' => $params['pMa_vt'] ?? '',
219-
'pNgay1' => $params['pNgay'] ?? date('Y-m-d'), // Map pNgay to pNgay1
220-
'pNgoai_te' => $params['pMa_Nt'] ?? 'VND', // Map pMa_Nt to pNgoai_te
221-
// Các tham số khác để null
222-
'pNgay2' => null,
223-
'pLoai_bc' => null,
224-
'pTk_vt' => null,
225-
'pMa_nhvt' => null,
226-
'pMa_vitri' => null,
227-
'pma_plvt1' => null,
228-
'pma_plvt2' => null,
229-
'pma_plvt3' => null,
230-
'pDVT' => null,
231-
'pPSDC' => null,
232-
'pSysMsg1' => null,
146+
'pMa_Cty' => $params['pMa_Cty'] ?? '',
147+
'pMa_kho' => $params['pMa_kho'] ?? '',
148+
'pMa_vt' => $params['pMa_vt'] ?? '',
149+
'pNgay1' => $params['pNgay'] ?? date('Y-m-d'),
150+
'pNgoai_te' => $params['pMa_Nt'] ?? 'VND',
151+
'pNgay2' => null,
152+
'pLoai_bc' => null,
153+
'pTk_vt' => null,
154+
'pMa_nhvt' => null,
155+
'pMa_vitri' => null,
156+
'pma_plvt1' => null,
157+
'pma_plvt2' => null,
158+
'pma_plvt3' => null,
159+
'pDVT' => null,
160+
'pPSDC' => null,
161+
'pSysMsg1' => null,
233162
]);
234163
}
235-
}
164+
}

src/Models/PoCt1.php

Lines changed: 14 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,28 @@
88
* @author Tran Ngoc Duc <ductn@diepxuan.com>
99
* @author Tran Ngoc Duc <caothu91@gmail.com>
1010
*
11-
* @lastupdate 2025-06-20 13:08:16
11+
* @lastupdate 2026-06-22
1212
*/
1313

1414
namespace Diepxuan\Simba\Models;
1515

1616
use Diepxuan\Simba\Models\Concerns\HasSimbaCompositeKey;
1717
use Diepxuan\Simba\SModel\PoCt1Model as Model;
1818
use Illuminate\Database\Eloquent\Builder;
19-
use Illuminate\Support\Collection;
20-
use Illuminate\Support\Facades\DB;
2119

2220
/**
2321
* Class PoCt1.
2422
*
2523
* This class represents the model for purchase order details.
24+
*
25+
* Behavior nghiệp vụ (tổng giá trị / số lượng theo vật tư / nhà cung cấp,
26+
* tỷ lệ nhập, các báo cáo mua hàng) đã được tách sang
27+
* `Diepxuan\Catalog\Models\Concerns\HasPoCt1PurchaseMetrics`
28+
* và gắn vào `Diepxuan\Catalog\Models\PoCt1` (nếu có).
29+
*
30+
* Còn giữ ở đây:
31+
* - Scope filter theo field Simba.
32+
* - Quan hệ Simba-Simba: poPh3, inDmVt, inDmKho, inDmViTri, inDmLo, sysDepartment, glDmTkVt, glDmTkThue.
2633
*/
2734
class PoCt1 extends Model
2835
{
@@ -62,7 +69,7 @@ public function scopeFilterByMaKho(Builder $query, ?string $maKho): Builder
6269
public function scopeFilterByMaNcc(Builder $query, ?string $maNcc): Builder
6370
{
6471
if (!empty($maNcc)) {
65-
return $query->whereHas('poPh3', function ($q) use ($maNcc) {
72+
$query->whereHas('poPh3', function ($q) use ($maNcc): void {
6673
$q->where('ma_ncc', $maNcc);
6774
});
6875
}
@@ -84,12 +91,12 @@ public function scopeFilterByMaBp(Builder $query, ?string $maBp): Builder
8491
public function scopeFilterByNgayCt(Builder $query, ?string $fromDate, ?string $toDate): Builder
8592
{
8693
if (!empty($fromDate)) {
87-
$query->whereHas('poPh3', function ($q) use ($fromDate) {
94+
$query->whereHas('poPh3', function ($q) use ($fromDate): void {
8895
$q->whereDate('ngay_ct', '>=', $fromDate);
8996
});
9097
}
9198
if (!empty($toDate)) {
92-
$query->whereHas('poPh3', function ($q) use ($toDate) {
99+
$query->whereHas('poPh3', function ($q) use ($toDate): void {
93100
$q->whereDate('ngay_ct', '<=', $toDate);
94101
});
95102
}
@@ -144,158 +151,4 @@ public function glDmTkThue()
144151
{
145152
return $this->belongsTo(GlDmTk::class, 'tk_thue', 'tk');
146153
}
147-
148-
/**
149-
* Tính tổng giá trị mua hàng theo vật tư
150-
*/
151-
public static function getTotalPurchaseByProduct(string $maCty, ?string $fromDate = null, ?string $toDate = null): Collection
152-
{
153-
$query = self::where('ma_cty', $maCty);
154-
155-
if ($fromDate || $toDate) {
156-
$query->whereHas('poPh3', function ($q) use ($fromDate, $toDate) {
157-
if ($fromDate) {
158-
$q->whereDate('ngay_ct', '>=', $fromDate);
159-
}
160-
if ($toDate) {
161-
$q->whereDate('ngay_ct', '<=', $toDate);
162-
}
163-
});
164-
}
165-
166-
return $query->select('ma_vt', DB::raw('SUM(tien0) as tong_gia_tri'), DB::raw('SUM(tien_nt0) as tong_gia_tri_nt'))
167-
->groupBy('ma_vt')
168-
->get();
169-
}
170-
171-
/**
172-
* Tính tổng số lượng mua theo vật tư
173-
*/
174-
public static function getTotalQuantityByProduct(string $maCty, ?string $fromDate = null, ?string $toDate = null): Collection
175-
{
176-
$query = self::where('ma_cty', $maCty);
177-
178-
if ($fromDate || $toDate) {
179-
$query->whereHas('poPh3', function ($q) use ($fromDate, $toDate) {
180-
if ($fromDate) {
181-
$q->whereDate('ngay_ct', '>=', $fromDate);
182-
}
183-
if ($toDate) {
184-
$q->whereDate('ngay_ct', '<=', $toDate);
185-
}
186-
});
187-
}
188-
189-
return $query->select('ma_vt', DB::raw('SUM(so_luong) as tong_so_luong'), DB::raw('SUM(so_luong_qd) as tong_so_luong_qd'))
190-
->groupBy('ma_vt')
191-
->get();
192-
}
193-
194-
/**
195-
* Tính tổng giá trị mua hàng theo nhà cung cấp
196-
*/
197-
public static function getTotalPurchaseBySupplier(string $maCty, ?string $fromDate = null, ?string $toDate = null): Collection
198-
{
199-
$query = self::where('ma_cty', $maCty);
200-
201-
if ($fromDate || $toDate) {
202-
$query->whereHas('poPh3', function ($q) use ($fromDate, $toDate) {
203-
if ($fromDate) {
204-
$q->whereDate('ngay_ct', '>=', $fromDate);
205-
}
206-
if ($toDate) {
207-
$q->whereDate('ngay_ct', '<=', $toDate);
208-
}
209-
});
210-
}
211-
212-
return $query->select('poPh3.ma_ncc', DB::raw('SUM(po_ct1.tien0) as tong_gia_tri'), DB::raw('SUM(po_ct1.tien_nt0) as tong_gia_tri_nt'))
213-
->join('po_ph3', 'po_ct1.stt_rec', '=', 'po_ph3.stt_rec')
214-
->groupBy('po_ph3.ma_ncc')
215-
->get();
216-
}
217-
218-
/**
219-
* Tính tỷ lệ nhập hàng (số lượng đã nhập / số lượng đặt hàng)
220-
*/
221-
public function getReceiptRate(): float
222-
{
223-
if ($this->so_luong == 0) {
224-
return 0;
225-
}
226-
227-
return ($this->sl_nhap / $this->so_luong) * 100;
228-
}
229-
230-
/**
231-
* Gọi stored procedure asPORptMH01 để lấy báo cáo mua hàng
232-
*/
233-
public static function getPORptMH01(array $params): Collection
234-
{
235-
return collect(DB::connection((new static())->getConnectionName())->select('EXEC asPORptMH01
236-
@pMa_Cty = :pMa_Cty,
237-
@pNgay_Ct1 = :pNgay_Ct1,
238-
@pNgay_Ct2 = :pNgay_Ct2,
239-
@pMa_Ncc = :pMa_Ncc,
240-
@pMa_Vt = :pMa_Vt,
241-
@pMa_Bp = :pMa_Bp,
242-
@pMa_Nt = :pMa_Nt
243-
', [
244-
'pMa_Cty' => $params['pMa_Cty'] ?? '',
245-
'pNgay_Ct1' => $params['pNgay_Ct1'] ?? '2025-01-01',
246-
'pNgay_Ct2' => $params['pNgay_Ct2'] ?? now(),
247-
'pMa_Ncc' => $params['pMa_Ncc'] ?? '',
248-
'pMa_Vt' => $params['pMa_Vt'] ?? '',
249-
'pMa_Bp' => $params['pMa_Bp'] ?? '',
250-
'pMa_Nt' => $params['pMa_Nt'] ?? 'VND',
251-
]));
252-
}
253-
254-
/**
255-
* Gọi stored procedure asPORptDH01 để lấy báo cáo đặt hàng
256-
*/
257-
public static function getPORptDH01(array $params): Collection
258-
{
259-
return collect(DB::connection((new static())->getConnectionName())->select('EXEC asPORptDH01
260-
@pMa_Cty = :pMa_Cty,
261-
@pNgay_Ct1 = :pNgay_Ct1,
262-
@pNgay_Ct2 = :pNgay_Ct2,
263-
@pMa_Ncc = :pMa_Ncc,
264-
@pMa_Vt = :pMa_Vt,
265-
@pMa_Bp = :pMa_Bp,
266-
@pMa_Nt = :pMa_Nt
267-
', [
268-
'pMa_Cty' => $params['pMa_Cty'] ?? '',
269-
'pNgay_Ct1' => $params['pNgay_Ct1'] ?? '2025-01-01',
270-
'pNgay_Ct2' => $params['pNgay_Ct2'] ?? now(),
271-
'pMa_Ncc' => $params['pMa_Ncc'] ?? '',
272-
'pMa_Vt' => $params['pMa_Vt'] ?? '',
273-
'pMa_Bp' => $params['pMa_Bp'] ?? '',
274-
'pMa_Nt' => $params['pMa_Nt'] ?? 'VND',
275-
]));
276-
}
277-
278-
/**
279-
* Gọi stored procedure asPORptNH01 để lấy báo cáo nhập hàng
280-
*/
281-
public static function getPORptNH01(array $params): Collection
282-
{
283-
return collect(DB::connection((new static())->getConnectionName())->select('EXEC asPORptNH01
284-
@pMa_Cty = :pMa_Cty,
285-
@pNgay_Ct1 = :pNgay_Ct1,
286-
@pNgay_Ct2 = :pNgay_Ct2,
287-
@pMa_Ncc = :pMa_Ncc,
288-
@pMa_Vt = :pMa_Vt,
289-
@pMa_Bp = :pMa_Bp,
290-
@pMa_Nt = :pMa_Nt
291-
', [
292-
'pMa_Cty' => $params['pMa_Cty'] ?? '',
293-
'pNgay_Ct1' => $params['pNgay_Ct1'] ?? '2025-01-01',
294-
'pNgay_Ct2' => $params['pNgay_Ct2'] ?? now(),
295-
'pMa_Ncc' => $params['pMa_Ncc'] ?? '',
296-
'pMa_Vt' => $params['pMa_Vt'] ?? '',
297-
'pMa_Bp' => $params['pMa_Bp'] ?? '',
298-
'pMa_Nt' => $params['pMa_Nt'] ?? 'VND',
299-
]));
300-
}
301-
}
154+
}

0 commit comments

Comments
 (0)