Supplierservicescore.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\library\ProcuremenSupplierScore;
  5. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  6. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  7. use PhpOffice\PhpSpreadsheet\Style\Border;
  8. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  9. /**
  10. * 供应商评审表(月度记录:商务/技术得分 / 价格得分 / 交货得分 / 最终得分,不参与其它业务计算)
  11. *
  12. * @icon fa fa-trophy
  13. */
  14. class Supplierservicescore extends Backend
  15. {
  16. /** @var \app\admin\model\Supplierservicescore */
  17. protected $model = null;
  18. protected $searchFields = 'company_name';
  19. /** 有列表权限即可导出/批量保存最终得分 */
  20. protected $noNeedRight = ['export', 'savefinalbatch'];
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. ProcuremenSupplierScore::ensureSchema();
  25. $this->model = new \app\admin\model\Supplierservicescore;
  26. }
  27. public function index()
  28. {
  29. $this->request->filter(['strip_tags', 'trim']);
  30. if ($this->request->isAjax()) {
  31. $ym = $this->resolveRequestYm();
  32. $keyword = $this->resolveSearchKeyword();
  33. $offset = max(0, (int)$this->request->get('offset', 0));
  34. $limit = (int)$this->request->get('limit', 50);
  35. if ($limit <= 0) {
  36. $limit = 50;
  37. }
  38. // 有搜索词:跨月查该供应商;无搜索:按查询月份展示当月
  39. $list = $keyword !== ''
  40. ? $this->loadSupplierScoreSearchList($keyword)
  41. : $this->loadMonthlySupplierScoreList($ym, '');
  42. $total = count($list);
  43. $pageRows = array_slice($list, $offset, $limit);
  44. $seqBase = $offset;
  45. $rows = [];
  46. foreach ($pageRows as $i => $item) {
  47. $rowYm = (string)($item['ym'] ?? $ym);
  48. $rows[] = [
  49. 'id' => $seqBase + $i + 1,
  50. 'seq_no' => $seqBase + $i + 1,
  51. 'company_name' => (string)($item['company_name'] ?? ''),
  52. 'quality_score' => $item['quality_score'] ?? null,
  53. 'quality_score_text' => (string)($item['quality_score_text'] ?? ''),
  54. 'price_score' => $item['price_score'] ?? null,
  55. 'price_score_text' => (string)($item['price_score_text'] ?? ''),
  56. 'delivery_score' => $item['delivery_score'] ?? null,
  57. 'delivery_score_text' => (string)($item['delivery_score_text'] ?? ''),
  58. 'final_score' => $item['final_score'] ?? null,
  59. 'final_score_text' => (string)($item['final_score_text'] ?? ''),
  60. 'final_score_saved' => !empty($item['final_score_saved']) ? 1 : 0,
  61. 'score_grade' => (string)($item['score_grade'] ?? ''),
  62. 'score_date' => (string)(($item['score_date'] ?? '') !== '' ? $item['score_date'] : $rowYm),
  63. 'ym' => $rowYm,
  64. ];
  65. }
  66. return json(['total' => $total, 'rows' => $rows]);
  67. }
  68. return $this->view->fetch();
  69. }
  70. /**
  71. * 批量保存当月最终得分与评分等级
  72. * POST: ym, rows=[{company_name, final_score, score_grade}, ...] 或 rows_json
  73. */
  74. public function savefinalbatch()
  75. {
  76. if (!$this->auth->check('supplierservicescore/index') && !$this->auth->isSuperAdmin()) {
  77. $this->error(__('You have no permission'));
  78. }
  79. ProcuremenSupplierScore::ensureFinalScoreTable();
  80. $ym = trim((string)$this->request->post('ym', ''));
  81. if ($ym === '') {
  82. $ym = trim((string)$this->request->param('ym', ''));
  83. }
  84. if (!preg_match('/^\d{4}-\d{2}$/', $ym)) {
  85. $this->error('请选择查询月份');
  86. }
  87. $rows = $this->request->post('rows/a', []);
  88. if (!is_array($rows) || $rows === []) {
  89. $rowsJson = trim((string)$this->request->post('rows_json', ''));
  90. if ($rowsJson !== '') {
  91. $decoded = json_decode($rowsJson, true);
  92. if (is_array($decoded)) {
  93. $rows = $decoded;
  94. }
  95. }
  96. }
  97. if (!is_array($rows) || $rows === []) {
  98. $this->error('没有可保存的数据');
  99. }
  100. // 按行自带 ym 分组(跨月搜索结果可同页保存)
  101. $byYm = [];
  102. foreach ($rows as $row) {
  103. if (!is_array($row)) {
  104. continue;
  105. }
  106. $rowYm = trim((string)($row['ym'] ?? ''));
  107. if (!preg_match('/^\d{4}-\d{2}$/', $rowYm)) {
  108. $rowYm = $ym;
  109. }
  110. if (!isset($byYm[$rowYm])) {
  111. $byYm[$rowYm] = [];
  112. }
  113. $byYm[$rowYm][] = $row;
  114. }
  115. $done = 0;
  116. $err = '';
  117. foreach ($byYm as $saveYm => $chunk) {
  118. $ret = ProcuremenSupplierScore::saveFinalScoresForYm($saveYm, $chunk);
  119. $done += (int)($ret['done'] ?? 0);
  120. if ($err === '') {
  121. $err = trim((string)($ret['error'] ?? ''));
  122. }
  123. }
  124. if ($done < 1) {
  125. $this->error($err !== '' ? $err : '保存失败,请检查填写的分数');
  126. }
  127. $this->success('操作成功');
  128. }
  129. /**
  130. * 导出供应商评审表
  131. * 列:序号、供应商、最终得分、评分等级
  132. */
  133. public function export()
  134. {
  135. if (!$this->auth->check('supplierservicescore/index') && !$this->auth->isSuperAdmin()) {
  136. $this->error(__('You have no permission'));
  137. }
  138. $ym = trim((string)$this->request->param('ym', ''));
  139. if (!preg_match('/^(\d{4})-(\d{2})$/', $ym, $m)) {
  140. $this->error('请选择查询月份');
  141. }
  142. $year = (int)$m[1];
  143. $month = (int)$m[2];
  144. if ($month < 1 || $month > 12) {
  145. $this->error('查询月份无效');
  146. }
  147. $list = $this->loadMonthlySupplierScoreList($ym, '');
  148. $title = $year . '年' . $month . '月外协加工供应商评审表';
  149. $spreadsheet = new Spreadsheet();
  150. $sheet = $spreadsheet->getActiveSheet();
  151. $sheet->setTitle('供应商评审表');
  152. $fontName = '宋体';
  153. $borderStyle = [
  154. 'borders' => [
  155. 'allBorders' => [
  156. 'borderStyle' => Border::BORDER_THIN,
  157. 'color' => ['rgb' => '000000'],
  158. ],
  159. ],
  160. ];
  161. $centerAlign = [
  162. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  163. 'vertical' => Alignment::VERTICAL_CENTER,
  164. 'wrapText' => true,
  165. ];
  166. $sheet->mergeCells('A1:D1');
  167. $sheet->setCellValue('A1', $title);
  168. $sheet->getRowDimension(1)->setRowHeight(36);
  169. $sheet->getStyle('A1')->getFont()->setName($fontName)->setBold(true)->setSize(20);
  170. $sheet->getStyle('A1')->getAlignment()->applyFromArray($centerAlign);
  171. $sheet->setCellValue('A2', '序号');
  172. $sheet->setCellValue('B2', '供应商');
  173. $sheet->setCellValue('C2', '最终得分');
  174. $sheet->setCellValue('D2', '评分等级');
  175. $sheet->getRowDimension(2)->setRowHeight(26);
  176. $sheet->getStyle('A2:D2')->getFont()->setName($fontName)->setBold(true)->setSize(14);
  177. $sheet->getStyle('A2:D2')->getAlignment()->applyFromArray($centerAlign);
  178. $rowNum = 3;
  179. if ($list === []) {
  180. $sheet->mergeCells('A3:D3');
  181. $sheet->setCellValue('A3', '(该月暂无供应商评审记录)');
  182. $sheet->getRowDimension(3)->setRowHeight(26);
  183. $sheet->getStyle('A3')->getFont()->setName($fontName)->setSize(12);
  184. $sheet->getStyle('A3')->getAlignment()->applyFromArray($centerAlign);
  185. $rowNum = 4;
  186. } else {
  187. $i = 1;
  188. foreach ($list as $item) {
  189. $sheet->setCellValue('A' . $rowNum, $i);
  190. $sheet->setCellValue('B' . $rowNum, (string)($item['company_name'] ?? ''));
  191. $sheet->setCellValue('C' . $rowNum, (string)($item['final_score_text'] ?? ''));
  192. $sheet->setCellValue('D' . $rowNum, (string)($item['score_grade'] ?? ''));
  193. $sheet->getRowDimension($rowNum)->setRowHeight(24);
  194. $rowNum++;
  195. $i++;
  196. }
  197. }
  198. $lastRow = max(2, $rowNum - 1);
  199. $sheet->getStyle('A1:D' . $lastRow)->applyFromArray($borderStyle);
  200. $sheet->getStyle('A3:D' . $lastRow)->getFont()->setName($fontName)->setSize(12);
  201. $sheet->getStyle('A3:D' . $lastRow)->getAlignment()->applyFromArray($centerAlign);
  202. $sheet->getColumnDimension('A')->setWidth(12);
  203. $sheet->getColumnDimension('B')->setWidth(48);
  204. $sheet->getColumnDimension('C')->setWidth(14);
  205. $sheet->getColumnDimension('D')->setWidth(12);
  206. $sheet->getPageSetup()->setFitToPage(true)->setFitToWidth(1)->setFitToHeight(0);
  207. $filename = $title . '.xlsx';
  208. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  209. header('Content-Disposition: attachment;filename="' . rawurlencode($filename) . '"');
  210. header('Cache-Control: max-age=0');
  211. $writer = new Xlsx($spreadsheet);
  212. $writer->save('php://output');
  213. $spreadsheet->disconnectWorksheets();
  214. unset($spreadsheet);
  215. exit;
  216. }
  217. /**
  218. * 月度评审记录列表(仅展示,最终得分可人工填写)
  219. *
  220. * @return array<int, array<string, mixed>>
  221. */
  222. protected function loadMonthlySupplierScoreList(string $ym, string $keyword = ''): array
  223. {
  224. $ym = trim($ym);
  225. if (!preg_match('/^\d{4}-\d{2}$/', $ym)) {
  226. return [];
  227. }
  228. ProcuremenSupplierScore::ensureSchema();
  229. $monthlyMap = ProcuremenSupplierScore::loadMonthlyMapByYm($ym);
  230. $keyword = trim($keyword);
  231. $list = [];
  232. foreach ($monthlyMap as $name => $item) {
  233. if ($keyword !== '' && mb_stripos($name, $keyword) === false) {
  234. continue;
  235. }
  236. $list[] = $this->buildSupplierScoreListItem($name, $item, $ym);
  237. }
  238. usort($list, [$this, 'sortSupplierScoreListItems']);
  239. return $list;
  240. }
  241. /**
  242. * 按供应商关键词跨月搜索(不限查询月份)
  243. *
  244. * @return array<int, array<string, mixed>>
  245. */
  246. protected function loadSupplierScoreSearchList(string $keyword): array
  247. {
  248. $keyword = trim($keyword);
  249. if ($keyword === '') {
  250. return [];
  251. }
  252. ProcuremenSupplierScore::ensureSchema();
  253. ProcuremenSupplierScore::ensureFinalScoreTable();
  254. try {
  255. $rows = \think\Db::table(ProcuremenSupplierScore::TABLE_FINAL)
  256. ->where('company_name', 'like', '%' . addcslashes($keyword, '%_\\') . '%')
  257. ->field('ym,company_name,score,quality_score,price_score,delivery_score,final_score,score_grade')
  258. ->order('ym', 'desc')
  259. ->order('company_name', 'asc')
  260. ->select();
  261. } catch (\Throwable $e) {
  262. return [];
  263. }
  264. if (!is_array($rows)) {
  265. return [];
  266. }
  267. $list = [];
  268. foreach ($rows as $r) {
  269. if (!is_array($r)) {
  270. continue;
  271. }
  272. $name = trim((string)($r['company_name'] ?? ''));
  273. $rowYm = ProcuremenSupplierScore::formatScoreYm($r['ym'] ?? '');
  274. if ($name === '' || $rowYm === '') {
  275. continue;
  276. }
  277. $quality = ProcuremenSupplierScore::readOptionalScoreValue($r['quality_score'] ?? null);
  278. $price = ProcuremenSupplierScore::readOptionalScoreValue($r['price_score'] ?? null);
  279. $delivery = ProcuremenSupplierScore::readOptionalScoreValue($r['delivery_score'] ?? null);
  280. $final = ProcuremenSupplierScore::readOptionalScoreValue($r['final_score'] ?? null);
  281. $item = [
  282. 'quality_score' => $quality,
  283. 'price_score' => $price,
  284. 'delivery_score' => $delivery,
  285. 'final_score' => $final,
  286. 'final_saved' => $final !== null ? 1 : 0,
  287. 'score_grade' => ProcuremenSupplierScore::normalizeScoreGrade($r['score_grade'] ?? ''),
  288. 'score_date' => $rowYm,
  289. ];
  290. $list[] = $this->buildSupplierScoreListItem($name, $item, $rowYm);
  291. }
  292. usort($list, [$this, 'sortSupplierScoreListItems']);
  293. return $list;
  294. }
  295. /**
  296. * @param array<string, mixed> $item
  297. * @return array<string, mixed>
  298. */
  299. protected function buildSupplierScoreListItem(string $name, array $item, string $ym): array
  300. {
  301. $quality = array_key_exists('quality_score', $item) ? $item['quality_score'] : null;
  302. $price = array_key_exists('price_score', $item) ? $item['price_score'] : null;
  303. $delivery = array_key_exists('delivery_score', $item) ? $item['delivery_score'] : null;
  304. if ($quality !== null) {
  305. $quality = (float)$quality;
  306. }
  307. if ($price !== null) {
  308. $price = (float)$price;
  309. }
  310. if ($delivery !== null) {
  311. $delivery = (float)$delivery;
  312. }
  313. $hasFinal = !empty($item['final_saved']);
  314. $final = $hasFinal ? (float)($item['final_score'] ?? 0) : null;
  315. $sortScore = $hasFinal
  316. ? (float)$final
  317. : (($quality ?? 0) + ($price ?? 0) + ($delivery ?? 0));
  318. $scoreDate = (string)($item['score_date'] ?? '');
  319. if ($scoreDate === '') {
  320. $scoreDate = ProcuremenSupplierScore::formatScoreYm($ym);
  321. }
  322. return [
  323. 'company_name' => $name,
  324. 'ym' => ProcuremenSupplierScore::formatScoreYm($ym),
  325. 'quality_score' => $quality,
  326. 'quality_score_text' => ProcuremenSupplierScore::formatOptionalScore($quality),
  327. 'price_score' => $price,
  328. 'price_score_text' => ProcuremenSupplierScore::formatOptionalScore($price),
  329. 'delivery_score' => $delivery,
  330. 'delivery_score_text' => ProcuremenSupplierScore::formatOptionalScore($delivery),
  331. 'final_score' => $final,
  332. 'final_score_text' => $hasFinal ? ProcuremenSupplierScore::formatOptionalScore($final) : '',
  333. 'final_score_saved' => $hasFinal ? 1 : 0,
  334. 'score_grade' => ProcuremenSupplierScore::normalizeScoreGrade($item['score_grade'] ?? ''),
  335. 'score_date' => $scoreDate,
  336. 'sort_score' => $sortScore,
  337. ];
  338. }
  339. /**
  340. * @param array<string, mixed> $a
  341. * @param array<string, mixed> $b
  342. */
  343. protected function sortSupplierScoreListItems(array $a, array $b): int
  344. {
  345. $ya = (string)($a['ym'] ?? '');
  346. $yb = (string)($b['ym'] ?? '');
  347. if ($ya !== $yb) {
  348. return strcmp($yb, $ya);
  349. }
  350. $sa = (float)($a['sort_score'] ?? 0);
  351. $sb = (float)($b['sort_score'] ?? 0);
  352. if ($sa !== $sb) {
  353. return $sb <=> $sa;
  354. }
  355. return strcmp((string)($a['company_name'] ?? ''), (string)($b['company_name'] ?? ''));
  356. }
  357. protected function resolveRequestYm(): string
  358. {
  359. $ym = trim((string)$this->request->get('ym', ''));
  360. if (preg_match('/^\d{4}-\d{2}$/', $ym)) {
  361. return $ym;
  362. }
  363. $filterRaw = $this->request->get('filter', '');
  364. if (is_string($filterRaw) && $filterRaw !== '') {
  365. $filter = json_decode($filterRaw, true);
  366. if (is_array($filter)) {
  367. $fy = trim((string)($filter['ym'] ?? ''));
  368. if (preg_match('/^\d{4}-\d{2}$/', $fy)) {
  369. return $fy;
  370. }
  371. }
  372. }
  373. if (preg_match('/^(\d{4})\D+(\d{1,2})/', $ym, $m)) {
  374. return sprintf('%04d-%02d', (int)$m[1], (int)$m[2]);
  375. }
  376. return date('Y-m');
  377. }
  378. protected function resolveSearchKeyword(): string
  379. {
  380. $keyword = trim((string)$this->request->get('search', ''));
  381. if ($keyword !== '') {
  382. return $keyword;
  383. }
  384. $filterRaw = $this->request->get('filter', '');
  385. if (is_string($filterRaw) && $filterRaw !== '') {
  386. $filter = json_decode($filterRaw, true);
  387. if (is_array($filter)) {
  388. return trim((string)($filter['company_name'] ?? ''));
  389. }
  390. }
  391. return '';
  392. }
  393. }