| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
- namespace app\common\library;
- use think\Db;
- /**
- * 协助采购全流程:订单存在性、软删、流程阶段校验
- */
- class ProcuremenGuard
- {
- public static function isValidScydgyId(int $id): bool
- {
- return $id !== 0;
- }
- public static function assertValidScydgyId(int $id, string $message = '工序数据无效'): void
- {
- if (!self::isValidScydgyId($id)) {
- throw new \InvalidArgumentException($message);
- }
- }
- public static function isSoftDeletedRow(array $row): bool
- {
- $mod = trim((string)($row['mod_rq'] ?? ''));
- return $mod !== '' && stripos($mod, '0000-00-00') !== 0;
- }
- public static function assertNotSoftDeleted(array $row, string $message = '该工序已删除或不可用'): void
- {
- if (self::isSoftDeletedRow($row)) {
- throw new \InvalidArgumentException($message);
- }
- }
- /**
- * @return array<string, mixed>
- */
- public static function loadPurchaseOrderOrFail(int $scydgyId, string $message = '未找到订单'): array
- {
- self::assertValidScydgyId($scydgyId, $message);
- try {
- $row = Db::table('purchase_order')->where('scydgy_id', $scydgyId)->find();
- } catch (\Throwable $e) {
- $row = null;
- }
- if (!is_array($row)) {
- throw new \InvalidArgumentException($message);
- }
- self::assertNotSoftDeleted($row, $message);
- return $row;
- }
- public static function assertWflowPendingConfirm(array $po, string $message = '该订单不在待确认供应商状态'): void
- {
- if (!ProcuremenStatus::isWflowPendingConfirm($po['wflow_status'] ?? '')) {
- throw new \InvalidArgumentException($message);
- }
- }
- public static function assertWflowPendingApproval(array $po, string $message = '该订单不在待审批状态'): void
- {
- if (!ProcuremenStatus::isWflowPendingApproval($po['wflow_status'] ?? '')) {
- throw new \InvalidArgumentException($message);
- }
- }
- public static function assertNotCompleted(array $po, string $message = '订单已完结,无法重复操作'): void
- {
- if (ProcuremenStatus::isPoCompleted($po['status'] ?? '')) {
- throw new \InvalidArgumentException($message);
- }
- }
- /**
- * @param int[] $detailIds
- */
- public static function assertDetailIdsBelongToOrder(int $scydgyId, array $detailIds, string $message = '下发明细与订单不匹配'): void
- {
- $detailIds = array_values(array_unique(array_filter(array_map('intval', $detailIds))));
- if ($detailIds === []) {
- throw new \InvalidArgumentException('请选择供应商明细');
- }
- try {
- $cnt = (int)Db::table('purchase_order_detail')
- ->where('scydgy_id', $scydgyId)
- ->where('id', 'in', $detailIds)
- ->count();
- } catch (\Throwable $e) {
- $cnt = 0;
- }
- if ($cnt !== count($detailIds)) {
- throw new \InvalidArgumentException($message);
- }
- }
- }
|