ProcuremenFormat.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?php
  2. namespace app\common\library;
  3. /**
  4. * 协助采购:纯展示/解析格式化(无请求、无写库副作用)
  5. * 从 Procuremen 控制器抽出,便于复用与减负;行为须与原 protected 方法一致。
  6. */
  7. class ProcuremenFormat
  8. {
  9. /**
  10. * 工序行主键:ERP 为正 scydgy.ID;手工新增为负 purchase_order.scydgy_id
  11. */
  12. public static function extractScydgyRowId(array $row): int
  13. {
  14. if (array_key_exists('scydgy_id', $row) && $row['scydgy_id'] !== null && $row['scydgy_id'] !== '') {
  15. return (int)$row['scydgy_id'];
  16. }
  17. if (array_key_exists('SCYDGY_ID', $row) && $row['SCYDGY_ID'] !== null && $row['SCYDGY_ID'] !== '') {
  18. return (int)$row['SCYDGY_ID'];
  19. }
  20. return (int)($row['ID'] ?? $row['id'] ?? 0);
  21. }
  22. public static function rowLabel(array $row): string
  23. {
  24. $dh = trim((string)($row['CCYDH'] ?? ''));
  25. $gymc = trim((string)($row['CGYMC'] ?? ''));
  26. if ($dh !== '' && $gymc !== '') {
  27. return '订单「' . $dh . '」、工序「' . $gymc . '」';
  28. }
  29. if ($dh !== '') {
  30. return '订单「' . $dh . '」';
  31. }
  32. if ($gymc !== '') {
  33. return '工序「' . $gymc . '」';
  34. }
  35. $id = self::extractScydgyRowId($row);
  36. return $id !== 0 ? ('工序编号 ' . $id) : '所选工序';
  37. }
  38. /** 主表/明细时间字段统一为可读字符串 */
  39. public static function detailTime($v): string
  40. {
  41. if ($v === null || $v === '') {
  42. return '';
  43. }
  44. if (is_numeric($v) && (int)$v > 946684800) {
  45. return date('Y-m-d H:i:s', (int)$v);
  46. }
  47. return ProcuremenTime::formatDisplayDateTime($v);
  48. }
  49. /** 交货日期仅展示 YYYY-MM-DD */
  50. public static function deliveryYmd($v): string
  51. {
  52. if ($v === null || $v === '') {
  53. return '';
  54. }
  55. $s = trim((string)$v);
  56. if ($s === '' || preg_match('/^0000-00-00/i', $s)) {
  57. return '';
  58. }
  59. if (preg_match('/^(\d{4}-\d{2}-\d{2})/', $s, $m)) {
  60. return $m[1];
  61. }
  62. $ts = strtotime($s);
  63. return ($ts !== false && $ts > 0) ? date('Y-m-d', $ts) : $s;
  64. }
  65. /** 解析金额/数量(去千分位等非数字字符) */
  66. public static function moneyNumber($value): ?float
  67. {
  68. if ($value === null || $value === '') {
  69. return null;
  70. }
  71. if (is_numeric($value)) {
  72. return (float)$value;
  73. }
  74. $v = preg_replace('/[^\d\.\-]/', '', (string)$value);
  75. return ($v !== '' && is_numeric($v)) ? (float)$v : null;
  76. }
  77. public static function moneyDisplay(?float $amount): string
  78. {
  79. if ($amount === null) {
  80. return '';
  81. }
  82. $s = rtrim(rtrim(sprintf('%.5F', (float)$amount), '0'), '.');
  83. return $s === '' ? '0' : $s;
  84. }
  85. /** 工作量为 0 时显示空 */
  86. public static function workloadDisplay($value): string
  87. {
  88. if ($value === null || $value === '') {
  89. return '';
  90. }
  91. $s = trim((string)$value);
  92. if ($s === '') {
  93. return '';
  94. }
  95. if (is_numeric($s) && (float)$s == 0) {
  96. return '';
  97. }
  98. return $s;
  99. }
  100. /**
  101. * @param string[] $names
  102. */
  103. public static function notifySupplierDisplay(array $names): string
  104. {
  105. $names = array_values(array_unique(array_filter(array_map(function ($n) {
  106. return trim((string)$n);
  107. }, $names))));
  108. if ($names === []) {
  109. return '';
  110. }
  111. sort($names, SORT_STRING);
  112. return implode("\n", $names);
  113. }
  114. /**
  115. * @param array{all?: array<string, bool>, quoted?: array<string, bool>} $bucket
  116. */
  117. public static function quotedSupplierLines(array $bucket): string
  118. {
  119. $all = array_keys($bucket['all'] ?? []);
  120. if ($all === []) {
  121. return '';
  122. }
  123. sort($all, SORT_STRING);
  124. $lines = [];
  125. foreach ($all as $cn) {
  126. $status = !empty($bucket['quoted'][$cn]) ? '已报价' : '未报价';
  127. $lines[] = $cn . '(' . $status . ')';
  128. }
  129. return implode("\n", $lines);
  130. }
  131. /** @param int|null|string $days */
  132. public static function leadDaysText($days): string
  133. {
  134. if ($days === null || $days === '') {
  135. return '未填写';
  136. }
  137. if (!is_numeric($days)) {
  138. return '未填写';
  139. }
  140. return ((int)$days) . '天';
  141. }
  142. public static function deliveryDeadlineForInput($value): string
  143. {
  144. $s = trim((string)$value);
  145. if ($s === '' || stripos($s, '0000-00-00') === 0) {
  146. return '';
  147. }
  148. if (preg_match('/^(\d{4}-\d{2}-\d{2})/', $s, $m)) {
  149. return $m[1];
  150. }
  151. $ts = strtotime(str_replace('/', '-', $s));
  152. return ($ts !== false && $ts > 0) ? date('Y-m-d', $ts) : '';
  153. }
  154. public static function sysRqForInput($sysRq): string
  155. {
  156. $sr = trim((string)$sysRq);
  157. if ($sr === '' || stripos($sr, '0000-00-00') === 0) {
  158. return '';
  159. }
  160. $ts = strtotime($sr);
  161. return ($ts !== false && $ts > 0) ? date('Y-m-d H:i:s', $ts) : $sr;
  162. }
  163. public static function sysRqForDisplay($sysRq): string
  164. {
  165. $sr = self::sysRqForInput($sysRq);
  166. if ($sr === '') {
  167. return '—';
  168. }
  169. $ts = strtotime($sr);
  170. return ($ts !== false && $ts > 0) ? date('Y/m/d H:i', $ts) : $sr;
  171. }
  172. /**
  173. * @param array{quality_weight?:mixed,price_weight?:mixed,lead_weight?:mixed} $rule
  174. */
  175. public static function scoreRuleWeightsText(array $rule): string
  176. {
  177. $parts = [];
  178. $qw = max(0, (float)($rule['quality_weight'] ?? 0));
  179. $pw = max(0, (float)($rule['price_weight'] ?? 0));
  180. $lw = max(0, (float)($rule['lead_weight'] ?? 0));
  181. $fmt = static function ($n) {
  182. $s = rtrim(rtrim(sprintf('%.2F', (float)$n), '0'), '.');
  183. return $s === '' ? '0' : $s;
  184. };
  185. if ($qw > 0) {
  186. $parts[] = '质量' . $fmt($qw) . '%';
  187. }
  188. if ($pw > 0) {
  189. $parts[] = '价格' . $fmt($pw) . '%';
  190. }
  191. if ($lw > 0) {
  192. $parts[] = '交货' . $fmt($lw) . '%';
  193. }
  194. return implode('、', $parts);
  195. }
  196. public static function mprocYmdForNotify($raw): string
  197. {
  198. $s = trim((string)$raw);
  199. if ($s === '' || preg_match('/^0000-00-00/i', $s)) {
  200. return '';
  201. }
  202. $s = str_replace(['/', '.'], '-', $s);
  203. if (preg_match('/^(\d{4}-\d{2}-\d{2})/', $s, $m)) {
  204. return $m[1];
  205. }
  206. $ts = strtotime(str_replace('T', ' ', $s));
  207. if ($ts === false || $ts <= 0) {
  208. return '';
  209. }
  210. return date('Y-m-d', $ts);
  211. }
  212. /** 导出用:工序列文案 */
  213. public static function exportGxText(array $r): string
  214. {
  215. $a = trim((string)($r['CDXMC'] ?? ''));
  216. $b = trim((string)($r['CGYMC'] ?? ''));
  217. if ($a !== '' && $b !== '') {
  218. return $a . ':' . $b;
  219. }
  220. return $a !== '' ? $a : $b;
  221. }
  222. /** 导出用:加工金额 */
  223. public static function exportAmount(array $r): float
  224. {
  225. foreach (['amount', 'jje', 'jgje', 'processing_amount'] as $k) {
  226. if (!array_key_exists($k, $r)) {
  227. continue;
  228. }
  229. $v = $r[$k];
  230. if ($v === null || $v === '') {
  231. continue;
  232. }
  233. if (is_numeric($v)) {
  234. return (float)$v;
  235. }
  236. $v = preg_replace('/[^\d\.\-]/', '', (string)$v);
  237. if ($v !== '' && is_numeric($v)) {
  238. return (float)$v;
  239. }
  240. }
  241. return 0.0;
  242. }
  243. /** 订单号等用于 PDF 文件名片段 */
  244. public static function sanitizePdfOrderKey(string $ccydh): string
  245. {
  246. $s = trim($ccydh);
  247. if ($s === '') {
  248. return 'ORDER';
  249. }
  250. $s = preg_replace('@[\\\\/:*?"<>|\\s]+@u', '_', $s);
  251. $s = trim($s, '._-');
  252. if ($s === '') {
  253. return 'ORDER';
  254. }
  255. if (function_exists('mb_substr')) {
  256. return mb_substr($s, 0, 80, 'UTF-8');
  257. }
  258. return strlen($s) <= 80 ? $s : substr($s, 0, 80);
  259. }
  260. /**
  261. * @return array{objectKey: string, webPath: string}
  262. */
  263. public static function buildConfirmPdfPaths(int $scydgyId, string $ccydhRaw): array
  264. {
  265. $sid = (int)$scydgyId;
  266. $safeOrder = self::sanitizePdfOrderKey($ccydhRaw);
  267. $y = date('Y');
  268. $m = date('m');
  269. $d = date('d');
  270. $basename = $safeOrder . '_' . $sid . '.pdf';
  271. $objectKey = 'xinhua/' . $y . '/' . $m . '/' . $d . '/' . $sid . '/' . $basename;
  272. return [
  273. 'objectKey' => $objectKey,
  274. 'webPath' => '/' . $objectKey,
  275. ];
  276. }
  277. /**
  278. * @param array<string, string> $vars
  279. * @return array<string, string>
  280. */
  281. public static function normalizeSmsTemplateVars(array $vars): array
  282. {
  283. $out = [];
  284. foreach ($vars as $k => $v) {
  285. $out[(string)$k] = trim((string)$v);
  286. }
  287. if (($out['contact_name'] ?? '') === '' && ($out['company_name'] ?? '') !== '') {
  288. $out['contact_name'] = $out['company_name'];
  289. }
  290. return $out;
  291. }
  292. /**
  293. * @return array<string, string>
  294. */
  295. public static function emailLogSceneLabelMap(): array
  296. {
  297. return [
  298. 'pick_issue' => '初选下发',
  299. 'audit_resend' => '确认页重发',
  300. 'audit_append' => '确认页追加供应商',
  301. 'rfq_salesman' => '询价通知业务员',
  302. 'rfq_supplier' => '供应商询价通知',
  303. 'delivery_notify' => '交货情况通知生产部',
  304. 'confirm_result' => '确认结果通知',
  305. 'confirm_ok' => '确认已通过通知',
  306. 'confirm_fail' => '确认未通过通知',
  307. ];
  308. }
  309. public static function normalizeEmailLogSceneForStore(string $scene): string
  310. {
  311. $scene = trim($scene);
  312. $map = self::emailLogSceneLabelMap();
  313. if ($scene === '') {
  314. return $map['pick_issue'];
  315. }
  316. if (isset($map[$scene])) {
  317. return $map[$scene];
  318. }
  319. if (in_array($scene, array_values($map), true)) {
  320. return $scene;
  321. }
  322. return $scene;
  323. }
  324. public static function emailLogSceneText(string $scene): string
  325. {
  326. return self::normalizeEmailLogSceneForStore($scene);
  327. }
  328. }