| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use app\common\library\ProcuremenStatus;
- use app\common\library\ProcuremenTime;
- use think\Db;
- /**
- * 协助采购 — 月度报表导出列表
- *
- * @icon fa fa-file-excel-o
- */
- class Procuremenexport extends Backend
- {
- protected $searchFields = 'CCYDH,CYJMC,CGYMC';
- public function _initialize()
- {
- parent::_initialize();
- $canExport = $this->hasProcuremenSiblingPerm(['export_month_outward', 'exportMonthOutward']);
- $canDetails = $this->hasProcuremenSiblingPerm(['details']);
- $this->view->assign('canExportMonthOutward', $canExport);
- $this->assignconfig('procuremenAuth', [
- 'details' => $canDetails,
- 'export_month_outward' => $canExport,
- ]);
- }
- /**
- * 月度导出页引用的采购子权限(规则名在 procuremen 控制器下)
- *
- * @param string[] $actions
- */
- protected function hasProcuremenSiblingPerm(array $actions): bool
- {
- foreach ($actions as $action) {
- $action = strtolower(trim((string)$action));
- if ($action === '') {
- continue;
- }
- if (strpos($action, 'procuremen/') !== 0) {
- $action = 'procuremen/' . $action;
- }
- if ($this->auth->check($action)) {
- return true;
- }
- }
- $suffixes = [];
- foreach ($actions as $action) {
- $action = strtolower(trim((string)$action));
- $action = preg_replace('#^procuremen/#', '', $action);
- if ($action !== '') {
- $suffixes[$action] = true;
- }
- }
- if ($suffixes === []) {
- return false;
- }
- try {
- foreach ($this->auth->getRuleList() as $rule) {
- $rule = strtolower((string)$rule);
- if (strpos($rule, 'procuremen/') !== 0) {
- continue;
- }
- foreach (array_keys($suffixes) as $suffix) {
- if ($rule === 'procuremen/' . $suffix || strpos($rule, 'procuremen/' . $suffix . '/') === 0) {
- return true;
- }
- }
- }
- } catch (\Throwable $e) {
- }
- return false;
- }
- public function index()
- {
- $this->relationSearch = false;
- $this->request->filter(['strip_tags', 'trim']);
- if ($this->request->isAjax()) {
- $ym = trim((string)$this->request->request('ym', date('Y-m')));
- if (!preg_match('/^\d{4}-\d{2}$/', $ym)) {
- $ym = date('Y-m');
- }
- try {
- $rows = $this->buildExportableOrderRows($ym);
- return json(['total' => count($rows), 'rows' => $rows, 'ym' => $ym]);
- } catch (\Throwable $e) {
- return json(['total' => 0, 'rows' => [], 'ym' => $ym, 'msg' => $e->getMessage()]);
- }
- }
- return $this->view->fetch();
- }
- /**
- * 已完结、可按月份导出的订单(合并为一单一行)
- */
- public function exportable()
- {
- if (!$this->request->isAjax()) {
- $this->error(__('Invalid parameters'));
- }
- $ym = trim((string)$this->request->get('ym', date('Y-m')));
- if (!preg_match('/^\d{4}-\d{2}$/', $ym)) {
- $ym = date('Y-m');
- }
- try {
- $rows = $this->buildExportableOrderRows($ym);
- return json(['total' => count($rows), 'rows' => $rows, 'ym' => $ym]);
- } catch (\Throwable $e) {
- return json(['total' => 0, 'rows' => [], 'ym' => $ym, 'msg' => $e->getMessage()]);
- }
- }
- /**
- * 已完结订单(与历史存证一致),按完结月份筛选后合并为一单一行
- *
- * @return array<int, array<string, mixed>>
- */
- protected function buildExportableOrderRows(string $ym): array
- {
- try {
- $query = Db::table('purchase_order');
- $query->whereRaw(ProcuremenStatus::sqlPoCompleted('status'));
- $dbRows = $query
- ->field('id,scydgy_id,CCYDH,CYJMC,CGYMC,pick_time,createtime,dStamp')
- ->order('id', 'desc')
- ->select();
- } catch (\Throwable $e) {
- \think\Log::write('月度导出列表查询异常:' . $e->getMessage(), 'error');
- $dbRows = [];
- }
- if (!is_array($dbRows)) {
- $dbRows = [];
- }
- $sidList = [];
- foreach ($dbRows as $r) {
- if (!is_array($r)) {
- continue;
- }
- $sid = (int)($r['scydgy_id'] ?? 0);
- if ($sid > 0) {
- $sidList[$sid] = true;
- }
- }
- $completeTsMap = ProcuremenTime::loadOperLogTimestampMap(array_keys($sidList), ['purchase_confirm', 'mark_complete']);
- $amountMap = $this->loadDetailAmountSumByScydgyIds(array_keys($sidList));
- $pool = [];
- foreach ($dbRows as $r) {
- if (!is_array($r)) {
- continue;
- }
- $done = ProcuremenTime::resolveCompletedDone($r, $completeTsMap);
- $rowYm = $done['ts'] > 0 ? date('Y-m', $done['ts']) : '';
- if ($rowYm !== $ym) {
- continue;
- }
- $sid = (int)($r['scydgy_id'] ?? 0);
- $pool[] = [
- 'id' => (int)($r['id'] ?? 0),
- 'scydgy_id' => $sid,
- 'CCYDH' => trim((string)($r['CCYDH'] ?? '')),
- 'CYJMC' => trim((string)($r['CYJMC'] ?? '')),
- 'CGYMC' => trim((string)($r['CGYMC'] ?? '')),
- 'ym' => $rowYm,
- 'createtime_text' => $done['text'],
- 'detail_amount' => (float)($amountMap[$sid] ?? 0),
- ];
- }
- $merged = $this->collapseExportRowsByOrder($pool);
- foreach ($merged as &$row) {
- $amt = 0.0;
- $cnt = 0;
- if (!empty($row['_merge_rows']) && is_array($row['_merge_rows'])) {
- foreach ($row['_merge_rows'] as $mr) {
- if (!is_array($mr)) {
- continue;
- }
- $amt += (float)($mr['detail_amount'] ?? 0);
- $cnt++;
- if (trim((string)($row['ym'] ?? '')) === '') {
- $ymv = trim((string)($mr['ym'] ?? ''));
- if ($ymv !== '') {
- $row['ym'] = $ymv;
- }
- }
- }
- } else {
- $amt = (float)($row['detail_amount'] ?? 0);
- $cnt = 1;
- }
- $row['row_count'] = $cnt;
- $row['total_amount'] = round($amt, 2);
- $row['total_amount_text'] = number_format($amt, 2, '.', ',');
- unset($row['_merge_rows'], $row['detail_amount']);
- }
- unset($row);
- return $merged;
- }
- /**
- * 工序行金额合计:优先已选定明细 status=1,否则汇总有效明细上的加工金额
- *
- * @param int[] $scydgyIds
- * @return array<int, float>
- */
- protected function loadDetailAmountSumByScydgyIds(array $scydgyIds): array
- {
- $picked = $this->loadPickedAmountSumByScydgyIds($scydgyIds);
- $scydgyIds = array_values(array_unique(array_filter(array_map('intval', $scydgyIds))));
- $missing = [];
- foreach ($scydgyIds as $sid) {
- if ($sid > 0 && !isset($picked[$sid])) {
- $missing[] = $sid;
- }
- }
- if ($missing === []) {
- return $picked;
- }
- try {
- $rows = Db::table('purchase_order_detail')
- ->where('scydgy_id', 'in', $missing)
- ->whereRaw(ProcuremenStatus::sqlPodNotVoid('status'))
- ->field('scydgy_id,amount')
- ->select();
- } catch (\Throwable $e) {
- $rows = [];
- }
- if (!is_array($rows)) {
- return $picked;
- }
- foreach ($rows as $r) {
- if (!is_array($r)) {
- continue;
- }
- $sid = (int)($r['scydgy_id'] ?? 0);
- $am = trim((string)($r['amount'] ?? ''));
- if ($sid <= 0 || $am === '' || $am === '0' || $am === '0.00') {
- continue;
- }
- if (!isset($picked[$sid])) {
- $picked[$sid] = 0.0;
- }
- $picked[$sid] += (float)$am;
- }
- return $picked;
- }
- /**
- * @param int[] $scydgyIds
- * @return array<int, float>
- */
- protected function loadPickedAmountSumByScydgyIds(array $scydgyIds): array
- {
- $out = [];
- $scydgyIds = array_values(array_unique(array_filter(array_map('intval', $scydgyIds))));
- if ($scydgyIds === []) {
- return $out;
- }
- try {
- $rows = Db::table('purchase_order_detail')
- ->where('scydgy_id', 'in', $scydgyIds)
- ->whereIn('status', ProcuremenStatus::podPickedValues())
- ->field('scydgy_id,amount')
- ->select();
- } catch (\Throwable $e) {
- $rows = [];
- }
- if (!is_array($rows)) {
- return $out;
- }
- foreach ($rows as $r) {
- if (!is_array($r)) {
- continue;
- }
- $sid = (int)($r['scydgy_id'] ?? 0);
- $am = trim((string)($r['amount'] ?? ''));
- if ($sid <= 0 || $am === '' || $am === '0' || $am === '0.00') {
- continue;
- }
- if (!isset($out[$sid])) {
- $out[$sid] = 0.0;
- }
- $out[$sid] += (float)$am;
- }
- return $out;
- }
- /**
- * 归属月份:优先采购确认/完结操作时间,其次 pick_time、createtime、dStamp
- *
- * @param array<string, mixed> $row
- * @param array<int, int> $completeTsMap scydgy_id => unix 完结时间
- */
- protected function resolvePurchaseOrderRowYm(array $row, array $completeTsMap = []): string
- {
- $ts = $this->resolvePurchaseOrderCompleteTimestamp($row, $completeTsMap);
- if ($ts > 0) {
- return date('Y-m', $ts);
- }
- return '';
- }
- /**
- * @param array<string, mixed> $row
- * @param array<int, int> $completeTsMap
- */
- protected function resolvePurchaseOrderCompleteTimestamp(array $row, array $completeTsMap = []): int
- {
- $sid = (int)($row['scydgy_id'] ?? 0);
- if ($sid > 0 && isset($completeTsMap[$sid]) && (int)$completeTsMap[$sid] > 946684800) {
- return (int)$completeTsMap[$sid];
- }
- foreach (['pick_time', 'createtime', 'dStamp'] as $key) {
- $raw = $row[$key] ?? null;
- if ($raw === null || $raw === '') {
- continue;
- }
- if (is_numeric($raw) && (int)$raw > 946684800) {
- return (int)$raw;
- }
- $t = trim((string)$raw);
- if ($t !== '' && stripos($t, '0000-00-00') !== 0) {
- $parsed = (int)strtotime($t);
- return $parsed > 946684800 ? $parsed : 0;
- }
- }
- return 0;
- }
- /**
- * @param array<int, string> $actions
- * @return array<int, int> scydgy_id => 最近操作 unix 时间
- */
- protected function loadOperLogTimestampMap(array $scydgyIds, array $actions): array
- {
- $out = [];
- $scydgyIds = array_values(array_unique(array_filter(array_map('intval', $scydgyIds))));
- $actions = array_values(array_filter(array_map('strval', $actions)));
- if ($scydgyIds === [] || $actions === []) {
- return $out;
- }
- try {
- $logs = Db::table('purchase_order_oper_log')
- ->where('scydgy_id', 'in', $scydgyIds)
- ->where('action', 'in', $actions)
- ->field('scydgy_id,createtime')
- ->order('id', 'asc')
- ->select();
- } catch (\Throwable $e) {
- $logs = [];
- }
- if (!is_array($logs)) {
- return $out;
- }
- foreach ($logs as $log) {
- if (!is_array($log)) {
- continue;
- }
- $sid = (int)($log['scydgy_id'] ?? 0);
- $ct = (int)($log['createtime'] ?? 0);
- if ($sid > 0 && $ct > 946684800) {
- $out[$sid] = isset($out[$sid]) ? max($out[$sid], $ct) : $ct;
- }
- }
- return $out;
- }
- /**
- * @param array<string, mixed> $row
- * @param array<int, int> $completeTsMap
- */
- protected function formatPurchaseOrderDoneTime(array $row, array $completeTsMap = []): string
- {
- $ts = $this->resolvePurchaseOrderCompleteTimestamp($row, $completeTsMap);
- if ($ts > 0) {
- return date('Y-m-d H:i:s', $ts);
- }
- return '';
- }
- /**
- * @param array<int, array<string, mixed>> $rows
- * @return array<int, array<string, mixed>>
- */
- protected function collapseExportRowsByOrder(array $rows): array
- {
- if ($rows === []) {
- return [];
- }
- $groups = [];
- foreach ($rows as $row) {
- if (!is_array($row)) {
- continue;
- }
- $dh = trim((string)($row['CCYDH'] ?? ''));
- $key = $dh !== '' ? $dh : ('_id_' . (int)($row['id'] ?? 0));
- if (!isset($groups[$key])) {
- $groups[$key] = [];
- }
- $groups[$key][] = $row;
- }
- $out = [];
- foreach ($groups as $groupRows) {
- if ($groupRows === []) {
- continue;
- }
- usort($groupRows, function ($a, $b) {
- return ((int)($a['id'] ?? 0)) <=> ((int)($b['id'] ?? 0));
- });
- $head = $groupRows[0];
- $gymcList = [];
- foreach ($groupRows as $r) {
- $g = trim((string)($r['CGYMC'] ?? ''));
- if ($g !== '' && !in_array($g, $gymcList, true)) {
- $gymcList[] = $g;
- }
- }
- $merged = $head;
- $merged['_merge_rows'] = $groupRows;
- if (count($groupRows) > 1) {
- $merged['CGYMC'] = implode('、', $gymcList);
- }
- $latestText = '';
- $latestTs = 0;
- foreach ($groupRows as $r) {
- $t = trim((string)($r['createtime_text'] ?? ''));
- $ts = ProcuremenTime::parseToTimestamp($t);
- if ($ts > $latestTs) {
- $latestTs = $ts;
- $latestText = ProcuremenTime::formatDisplayDateTime($t);
- }
- }
- if ($latestText !== '') {
- $merged['createtime_text'] = $latestText;
- }
- $maxId = (int)($head['id'] ?? 0);
- foreach ($groupRows as $r) {
- $maxId = max($maxId, (int)($r['id'] ?? 0));
- }
- $merged['id'] = $maxId;
- $out[] = $merged;
- }
- usort($out, function ($a, $b) {
- return ((int)($b['id'] ?? 0)) <=> ((int)($a['id'] ?? 0));
- });
- return $out;
- }
- }
|