ProcuremenGuard.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace app\common\library;
  3. use think\Db;
  4. /**
  5. * 协助采购全流程:订单存在性、软删、流程阶段校验
  6. */
  7. class ProcuremenGuard
  8. {
  9. public static function isValidScydgyId(int $id): bool
  10. {
  11. return $id !== 0;
  12. }
  13. public static function assertValidScydgyId(int $id, string $message = '工序数据无效'): void
  14. {
  15. if (!self::isValidScydgyId($id)) {
  16. throw new \InvalidArgumentException($message);
  17. }
  18. }
  19. public static function isSoftDeletedRow(array $row): bool
  20. {
  21. $mod = trim((string)($row['mod_rq'] ?? ''));
  22. return $mod !== '' && stripos($mod, '0000-00-00') !== 0;
  23. }
  24. public static function assertNotSoftDeleted(array $row, string $message = '该工序已删除或不可用'): void
  25. {
  26. if (self::isSoftDeletedRow($row)) {
  27. throw new \InvalidArgumentException($message);
  28. }
  29. }
  30. /**
  31. * @return array<string, mixed>
  32. */
  33. public static function loadPurchaseOrderOrFail(int $scydgyId, string $message = '未找到订单'): array
  34. {
  35. self::assertValidScydgyId($scydgyId, $message);
  36. try {
  37. $row = Db::table('purchase_order')->where('scydgy_id', $scydgyId)->find();
  38. } catch (\Throwable $e) {
  39. $row = null;
  40. }
  41. if (!is_array($row)) {
  42. throw new \InvalidArgumentException($message);
  43. }
  44. self::assertNotSoftDeleted($row, $message);
  45. return $row;
  46. }
  47. public static function assertWflowPendingConfirm(array $po, string $message = '该订单不在待确认供应商状态'): void
  48. {
  49. if (!ProcuremenStatus::isWflowPendingConfirm($po['wflow_status'] ?? '')) {
  50. throw new \InvalidArgumentException($message);
  51. }
  52. }
  53. public static function assertWflowPendingApproval(array $po, string $message = '该订单不在待审批状态'): void
  54. {
  55. if (!ProcuremenStatus::isWflowPendingApproval($po['wflow_status'] ?? '')) {
  56. throw new \InvalidArgumentException($message);
  57. }
  58. }
  59. public static function assertNotCompleted(array $po, string $message = '订单已完结,无法重复操作'): void
  60. {
  61. if (ProcuremenStatus::isPoCompleted($po['status'] ?? '')) {
  62. throw new \InvalidArgumentException($message);
  63. }
  64. }
  65. /**
  66. * @param int[] $detailIds
  67. */
  68. public static function assertDetailIdsBelongToOrder(int $scydgyId, array $detailIds, string $message = '下发明细与订单不匹配'): void
  69. {
  70. $detailIds = array_values(array_unique(array_filter(array_map('intval', $detailIds))));
  71. if ($detailIds === []) {
  72. throw new \InvalidArgumentException('请选择供应商明细');
  73. }
  74. try {
  75. $cnt = (int)Db::table('purchase_order_detail')
  76. ->where('scydgy_id', $scydgyId)
  77. ->where('id', 'in', $detailIds)
  78. ->count();
  79. } catch (\Throwable $e) {
  80. $cnt = 0;
  81. }
  82. if ($cnt !== count($detailIds)) {
  83. throw new \InvalidArgumentException($message);
  84. }
  85. }
  86. }