|
8 | 8 | * @author Tran Ngoc Duc <ductn@diepxuan.com> |
9 | 9 | * @author Tran Ngoc Duc <caothu91@gmail.com> |
10 | 10 | * |
11 | | - * @lastupdate 2026-02-12 12:36:46 |
| 11 | + * @lastupdate 2026-04-06 07:25:49 |
12 | 12 | */ |
13 | 13 |
|
14 | 14 | namespace Diepxuan\Simba\StoredProcedures; |
15 | 15 |
|
| 16 | +use Diepxuan\Simba\Helper\ParamHelper; |
16 | 17 | use Diepxuan\Simba\SModel\SModel; |
17 | 18 | use Illuminate\Support\Collection; |
18 | | -use Diepxuan\Simba\Helper\ParamHelper; |
19 | 19 |
|
| 20 | +/** |
| 21 | + * Class AsCADelPH2. |
| 22 | + * |
| 23 | + * Stored procedure: asCADelPH2 |
| 24 | + * Mục đích: Xóa phiếu thu/chi (bảng CAPH2 - Cash Phiếu 2). |
| 25 | + * Procedure thực hiện xóa bản ghi khỏi bảng CAPH2 dựa trên số tham chiếu (stt_rec). |
| 26 | + * |
| 27 | + * Tham số (theo định nghĩa thực tế trong SQL Server): |
| 28 | + * - @pMa_cty (nvarchar(3)): Mã công ty. Bắt buộc. |
| 29 | + * - @pStt_rec (nvarchar(20)): Số tham chiếu (stt_rec) của phiếu cần xóa. Bắt buộc. |
| 30 | + * - @pLUser (nvarchar(20)): Người dùng thực hiện xóa. Bắt buộc. |
| 31 | + * - @pRet (int, OUTPUT): Tham số đầu ra chứa mã lỗi (@@error). |
| 32 | + * - 0: Thành công. |
| 33 | + * - Khác 0: Mã lỗi SQL Server. |
| 34 | + * |
| 35 | + * Giá trị trả về: |
| 36 | + * - Procedure không trả về result set. |
| 37 | + * - Kết quả được trả về qua output parameter @pRet. |
| 38 | + * - Để lấy giá trị @pRet, cần đọc từ result của ProcedureCaller (có SELECT @pRet). |
| 39 | + * |
| 40 | + * Ví dụ gọi: |
| 41 | + * ```php |
| 42 | + * $result = AsCADelPH2::call([ |
| 43 | + * 'pMa_cty' => '001', |
| 44 | + * 'pStt_rec' => '001wCA40000000676668', |
| 45 | + * 'pLUser' => 'DUCTN', |
| 46 | + * ]); |
| 47 | + * |
| 48 | + * // Lấy giá trị output parameter |
| 49 | + * $ret = $result->first()->pRet ?? null; |
| 50 | + * if ($ret === 0) { |
| 51 | + * // Xóa thành công |
| 52 | + * } else { |
| 53 | + * // Có lỗi xảy ra, $ret chứa mã lỗi |
| 54 | + * } |
| 55 | + * ``` |
| 56 | + * |
| 57 | + * Lưu ý: |
| 58 | + * - Procedure chỉ thực hiện xóa dựa trên stt_rec. |
| 59 | + * - Cần đảm bảo stt_rec tồn tại trước khi xóa. |
| 60 | + * - Output parameter @pRet được xử lý tự động bởi ProcedureCaller. |
| 61 | + * |
| 62 | + * @see AsCAGetPH2::call() - Kiểm tra phiếu tồn tại trước khi xóa |
| 63 | + */ |
20 | 64 | class AsCADelPH2 |
21 | 65 | { |
| 66 | + /** |
| 67 | + * Gọi stored procedure asCADelPH2. |
| 68 | + * |
| 69 | + * @param array $params Mảng tham số với các khóa tương ứng tên tham số. |
| 70 | + * Các parameters có thể thiếu, sẽ dùng giá trị default. |
| 71 | + * |
| 72 | + * @return Collection kết quả từ procedure (chứa output parameter pRet) |
| 73 | + */ |
22 | 74 | public static function call(array $params): Collection |
23 | 75 | { |
24 | | - $paramObj = ParamHelper::fromArray($params); |
| 76 | + $paramObj = ParamHelper::fromArray($params); |
25 | 77 | $connection = (new SModel())->getConnectionName(); |
26 | 78 |
|
27 | 79 | return ProcedureCaller::call('asCADelPH2', [ |
28 | | - 'pMa_cty' => $paramObj->pMa_cty ?? null, |
| 80 | + 'pMa_cty' => $paramObj->pMa_cty ?? null, |
29 | 81 | 'pStt_rec' => $paramObj->pStt_rec ?? null, |
30 | | - 'pLUser' => $paramObj->pLUser ?? null, |
| 82 | + 'pLUser' => $paramObj->pLUser ?? null, |
| 83 | + 'pRet' => ['type' => 'INT', 'output' => true], |
31 | 84 | ], $connection); |
32 | 85 | } |
| 86 | + |
| 87 | + /** |
| 88 | + * Call stored procedure asCADelPH2 with named parameters (helper method). |
| 89 | + * |
| 90 | + * @param null|string $Ma_cty Mã công ty |
| 91 | + * @param null|string $Stt_rec Số tham chiếu |
| 92 | + * @param null|string $LUser Người dùng |
| 93 | + * |
| 94 | + * @return Collection Kết quả từ procedure |
| 95 | + */ |
| 96 | + public static function callWithParams( |
| 97 | + ?string $Ma_cty = null, |
| 98 | + ?string $Stt_rec = null, |
| 99 | + ?string $LUser = null, |
| 100 | + ): Collection { |
| 101 | + return self::call([ |
| 102 | + 'pMa_cty' => $Ma_cty, |
| 103 | + 'pStt_rec' => $Stt_rec, |
| 104 | + 'pLUser' => $LUser, |
| 105 | + ]); |
| 106 | + } |
33 | 107 | } |
0 commit comments