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
1414namespace Diepxuan \Simba \Models ;
1515
1616use Diepxuan \Simba \Models \Concerns \HasSimbaCompositeKey ;
1717use Diepxuan \Simba \SModel \PoCt1Model as Model ;
1818use 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 */
2734class 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