Supplierservicescore.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. $list = $this->loadMonthlySupplierScoreList($ym, $keyword);
  39. $total = count($list);
  40. $pageRows = array_slice($list, $offset, $limit);
  41. $seqBase = $offset;
  42. $rows = [];
  43. foreach ($pageRows as $i => $item) {
  44. $rows[] = [
  45. 'id' => $seqBase + $i + 1,
  46. 'seq_no' => $seqBase + $i + 1,
  47. 'company_name' => (string)($item['company_name'] ?? ''),
  48. 'quality_score' => $item['quality_score'] ?? null,
  49. 'quality_score_text' => (string)($item['quality_score_text'] ?? ''),
  50. 'price_score' => $item['price_score'] ?? null,
  51. 'price_score_text' => (string)($item['price_score_text'] ?? ''),
  52. 'delivery_score' => $item['delivery_score'] ?? null,
  53. 'delivery_score_text' => (string)($item['delivery_score_text'] ?? ''),
  54. 'final_score' => $item['final_score'] ?? null,
  55. 'final_score_text' => (string)($item['final_score_text'] ?? ''),
  56. 'final_score_saved' => !empty($item['final_score_saved']) ? 1 : 0,
  57. 'score_grade' => (string)($item['score_grade'] ?? ''),
  58. 'ym' => $ym,
  59. ];
  60. }
  61. return json(['total' => $total, 'rows' => $rows]);
  62. }
  63. return $this->view->fetch();
  64. }
  65. /**
  66. * 批量保存当月最终得分与评分等级
  67. * POST: ym, rows=[{company_name, final_score, score_grade}, ...] 或 rows_json
  68. */
  69. public function savefinalbatch()
  70. {
  71. if (!$this->auth->check('supplierservicescore/index') && !$this->auth->isSuperAdmin()) {
  72. $this->error(__('You have no permission'));
  73. }
  74. ProcuremenSupplierScore::ensureFinalScoreTable();
  75. $ym = trim((string)$this->request->post('ym', ''));
  76. if ($ym === '') {
  77. $ym = trim((string)$this->request->param('ym', ''));
  78. }
  79. if (!preg_match('/^\d{4}-\d{2}$/', $ym)) {
  80. $this->error('请选择查询月份');
  81. }
  82. $rows = $this->request->post('rows/a', []);
  83. if (!is_array($rows) || $rows === []) {
  84. $rowsJson = trim((string)$this->request->post('rows_json', ''));
  85. if ($rowsJson !== '') {
  86. $decoded = json_decode($rowsJson, true);
  87. if (is_array($decoded)) {
  88. $rows = $decoded;
  89. }
  90. }
  91. }
  92. if (!is_array($rows) || $rows === []) {
  93. $this->error('没有可保存的数据');
  94. }
  95. $ret = ProcuremenSupplierScore::saveFinalScoresForYm($ym, $rows);
  96. $done = (int)($ret['done'] ?? 0);
  97. if ($done < 1) {
  98. $err = trim((string)($ret['error'] ?? ''));
  99. $this->error($err !== '' ? $err : '保存失败,请检查填写的分数');
  100. }
  101. $this->success('操作成功');
  102. }
  103. /**
  104. * 导出供应商评审表
  105. * 列:序号、供应商名称、商务/技术得分、价格得分、交货得分、最终得分、评分等级
  106. */
  107. public function export()
  108. {
  109. if (!$this->auth->check('supplierservicescore/index') && !$this->auth->isSuperAdmin()) {
  110. $this->error(__('You have no permission'));
  111. }
  112. $ym = trim((string)$this->request->param('ym', ''));
  113. if (!preg_match('/^(\d{4})-(\d{2})$/', $ym, $m)) {
  114. $this->error('请选择查询月份');
  115. }
  116. $year = (int)$m[1];
  117. $month = (int)$m[2];
  118. if ($month < 1 || $month > 12) {
  119. $this->error('查询月份无效');
  120. }
  121. $list = $this->loadMonthlySupplierScoreList($ym, '');
  122. $title = $year . '年' . $month . '月外协加工供应商评审表';
  123. $spreadsheet = new Spreadsheet();
  124. $sheet = $spreadsheet->getActiveSheet();
  125. $sheet->setTitle('供应商评审表');
  126. $fontName = '宋体';
  127. $borderStyle = [
  128. 'borders' => [
  129. 'allBorders' => [
  130. 'borderStyle' => Border::BORDER_THIN,
  131. 'color' => ['rgb' => '000000'],
  132. ],
  133. ],
  134. ];
  135. $centerAlign = [
  136. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  137. 'vertical' => Alignment::VERTICAL_CENTER,
  138. 'wrapText' => true,
  139. ];
  140. $sheet->mergeCells('A1:G1');
  141. $sheet->setCellValue('A1', $title);
  142. $sheet->getRowDimension(1)->setRowHeight(36);
  143. $sheet->getStyle('A1')->getFont()->setName($fontName)->setBold(true)->setSize(20);
  144. $sheet->getStyle('A1')->getAlignment()->applyFromArray($centerAlign);
  145. $sheet->setCellValue('A2', '序号');
  146. $sheet->setCellValue('B2', '供应商名称');
  147. $sheet->setCellValue('C2', '商务/技术得分');
  148. $sheet->setCellValue('D2', '价格得分');
  149. $sheet->setCellValue('E2', '交货得分');
  150. $sheet->setCellValue('F2', '最终得分');
  151. $sheet->setCellValue('G2', '评分等级');
  152. $sheet->getRowDimension(2)->setRowHeight(26);
  153. $sheet->getStyle('A2:G2')->getFont()->setName($fontName)->setBold(true)->setSize(14);
  154. $sheet->getStyle('A2:G2')->getAlignment()->applyFromArray($centerAlign);
  155. $rowNum = 3;
  156. if ($list === []) {
  157. $sheet->mergeCells('A3:G3');
  158. $sheet->setCellValue('A3', '(该月暂无供应商评审记录)');
  159. $sheet->getRowDimension(3)->setRowHeight(26);
  160. $sheet->getStyle('A3')->getFont()->setName($fontName)->setSize(12);
  161. $sheet->getStyle('A3')->getAlignment()->applyFromArray($centerAlign);
  162. $rowNum = 4;
  163. } else {
  164. $i = 1;
  165. foreach ($list as $item) {
  166. $finalText = (string)($item['final_score_text'] ?? '');
  167. $sheet->setCellValue('A' . $rowNum, $i);
  168. $sheet->setCellValue('B' . $rowNum, (string)($item['company_name'] ?? ''));
  169. $sheet->setCellValue('C' . $rowNum, (string)($item['quality_score_text'] ?? ''));
  170. $sheet->setCellValue('D' . $rowNum, (string)($item['price_score_text'] ?? ''));
  171. $sheet->setCellValue('E' . $rowNum, (string)($item['delivery_score_text'] ?? ''));
  172. $sheet->setCellValue('F' . $rowNum, $finalText);
  173. $sheet->setCellValue('G' . $rowNum, (string)($item['score_grade'] ?? ''));
  174. $sheet->getRowDimension($rowNum)->setRowHeight(24);
  175. $rowNum++;
  176. $i++;
  177. }
  178. }
  179. $lastRow = max(2, $rowNum - 1);
  180. $sheet->getStyle('A1:G' . $lastRow)->applyFromArray($borderStyle);
  181. $sheet->getStyle('A3:G' . $lastRow)->getFont()->setName($fontName)->setSize(12);
  182. $sheet->getStyle('A3:G' . $lastRow)->getAlignment()->applyFromArray($centerAlign);
  183. $sheet->getColumnDimension('A')->setWidth(12);
  184. $sheet->getColumnDimension('B')->setWidth(48);
  185. $sheet->getColumnDimension('C')->setWidth(14);
  186. $sheet->getColumnDimension('D')->setWidth(14);
  187. $sheet->getColumnDimension('E')->setWidth(12);
  188. $sheet->getColumnDimension('F')->setWidth(14);
  189. $sheet->getColumnDimension('G')->setWidth(12);
  190. $sheet->getPageSetup()->setFitToPage(true)->setFitToWidth(1)->setFitToHeight(0);
  191. $filename = $title . '.xlsx';
  192. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  193. header('Content-Disposition: attachment;filename="' . rawurlencode($filename) . '"');
  194. header('Cache-Control: max-age=0');
  195. $writer = new Xlsx($spreadsheet);
  196. $writer->save('php://output');
  197. $spreadsheet->disconnectWorksheets();
  198. unset($spreadsheet);
  199. exit;
  200. }
  201. /**
  202. * 月度评审记录列表(仅展示,最终得分可人工填写)
  203. *
  204. * @return array<int, array<string, mixed>>
  205. */
  206. protected function loadMonthlySupplierScoreList(string $ym, string $keyword = ''): array
  207. {
  208. $ym = trim($ym);
  209. if (!preg_match('/^\d{4}-\d{2}$/', $ym)) {
  210. return [];
  211. }
  212. ProcuremenSupplierScore::ensureSchema();
  213. $monthlyMap = ProcuremenSupplierScore::loadMonthlyMapByYm($ym);
  214. $keyword = trim($keyword);
  215. $list = [];
  216. foreach ($monthlyMap as $name => $item) {
  217. if ($keyword !== '' && mb_stripos($name, $keyword) === false) {
  218. continue;
  219. }
  220. $quality = array_key_exists('quality_score', $item) ? $item['quality_score'] : null;
  221. $price = array_key_exists('price_score', $item) ? $item['price_score'] : null;
  222. $delivery = array_key_exists('delivery_score', $item) ? $item['delivery_score'] : null;
  223. if ($quality !== null) {
  224. $quality = (float)$quality;
  225. }
  226. if ($price !== null) {
  227. $price = (float)$price;
  228. }
  229. if ($delivery !== null) {
  230. $delivery = (float)$delivery;
  231. }
  232. $hasFinal = !empty($item['final_saved']);
  233. $final = $hasFinal ? (float)($item['final_score'] ?? 0) : null;
  234. // 列表排序:有最终得分按最终得分,否则按已填分项合计
  235. $sortScore = $hasFinal
  236. ? (float)$final
  237. : (($quality ?? 0) + ($price ?? 0) + ($delivery ?? 0));
  238. $list[] = [
  239. 'company_name' => $name,
  240. 'quality_score' => $quality,
  241. 'quality_score_text' => ProcuremenSupplierScore::formatOptionalScore($quality),
  242. 'price_score' => $price,
  243. 'price_score_text' => ProcuremenSupplierScore::formatOptionalScore($price),
  244. 'delivery_score' => $delivery,
  245. 'delivery_score_text' => ProcuremenSupplierScore::formatOptionalScore($delivery),
  246. 'final_score' => $final,
  247. 'final_score_text' => $hasFinal ? ProcuremenSupplierScore::formatOptionalScore($final) : '',
  248. 'final_score_saved' => $hasFinal ? 1 : 0,
  249. 'score_grade' => ProcuremenSupplierScore::normalizeScoreGrade($item['score_grade'] ?? ''),
  250. 'sort_score' => $sortScore,
  251. ];
  252. }
  253. usort($list, function ($a, $b) {
  254. $sa = (float)($a['sort_score'] ?? 0);
  255. $sb = (float)($b['sort_score'] ?? 0);
  256. if ($sa !== $sb) {
  257. return $sb <=> $sa;
  258. }
  259. return strcmp((string)($a['company_name'] ?? ''), (string)($b['company_name'] ?? ''));
  260. });
  261. return $list;
  262. }
  263. protected function resolveRequestYm(): string
  264. {
  265. $ym = trim((string)$this->request->get('ym', ''));
  266. if (preg_match('/^\d{4}-\d{2}$/', $ym)) {
  267. return $ym;
  268. }
  269. $filterRaw = $this->request->get('filter', '');
  270. if (is_string($filterRaw) && $filterRaw !== '') {
  271. $filter = json_decode($filterRaw, true);
  272. if (is_array($filter)) {
  273. $fy = trim((string)($filter['ym'] ?? ''));
  274. if (preg_match('/^\d{4}-\d{2}$/', $fy)) {
  275. return $fy;
  276. }
  277. }
  278. }
  279. if (preg_match('/^(\d{4})\D+(\d{1,2})/', $ym, $m)) {
  280. return sprintf('%04d-%02d', (int)$m[1], (int)$m[2]);
  281. }
  282. return date('Y-m');
  283. }
  284. protected function resolveSearchKeyword(): string
  285. {
  286. $keyword = trim((string)$this->request->get('search', ''));
  287. if ($keyword !== '') {
  288. return $keyword;
  289. }
  290. $filterRaw = $this->request->get('filter', '');
  291. if (is_string($filterRaw) && $filterRaw !== '') {
  292. $filter = json_decode($filterRaw, true);
  293. if (is_array($filter)) {
  294. return trim((string)($filter['company_name'] ?? ''));
  295. }
  296. }
  297. return '';
  298. }
  299. }