This commit is contained in:
Your Name
2026-06-16 15:47:44 +08:00
parent c9c2b4608f
commit 37b2945c44
3 changed files with 325 additions and 1 deletions
@@ -3161,6 +3161,193 @@ class PrescriptionOrderLogic
return $out;
}
/**
* 导出用:统一解析时间(支持 Unix 秒/毫秒与 Y-m-d H:i:s 字符串,与前端 formatOrderTime 同口径)
*
* @param mixed $value
*/
private static function formatDateTimeForExport($value, string $format = 'Y-m-d H:i'): string
{
if ($value === null || $value === '' || $value === false) {
return '';
}
if (\is_int($value) || \is_float($value)) {
$ts = (int) $value;
if ($ts > 9999999999) {
$ts = (int) floor($ts / 1000);
}
return $ts > 0 ? date($format, $ts) : '';
}
$str = trim((string) $value);
if ($str === '') {
return '';
}
if (preg_match('/^\d+$/', $str)) {
$ts = (int) $str;
if ($ts > 9999999999) {
$ts = (int) floor($ts / 1000);
}
return $ts > 0 ? date($format, $ts) : '';
}
if (str_contains($str, '-') || str_contains($str, '/')) {
$ts = strtotime($str);
return $ts !== false ? date($format, $ts) : $str;
}
return $str;
}
/**
* 导出用:支付单来源/方式(与前端 formatPayOrderSource 同口径)
*
* @param array<string, mixed> $row
*/
private static function formatPayOrderSourceForExport(array $row): string
{
$createType = trim((string) ($row['create_type'] ?? ''));
if ($createType === 'wechat_work') {
return '企业微信对外收款';
}
if ($createType === 'fubei') {
return '付呗';
}
$paymentMethod = trim((string) ($row['payment_method'] ?? ''));
$methodMap = [
'alipay' => '支付宝',
'wechat' => '微信',
'wechat_work' => '企业微信',
'fubei' => '付呗',
'manual' => '手动确认到账',
];
if ($paymentMethod !== '' && isset($methodMap[$paymentMethod])) {
return $methodMap[$paymentMethod];
}
return '普通订单';
}
/**
* 导出用:单条关联收款可读文本(两行结构,便于 Excel 自动换行阅读)
*
* @param array<string, mixed> $pay
* @param array<int|string, string> $creatorNames
*/
private static function formatLinkedPayRecordLineForExport(array $pay, array $creatorNames, int $index = 1): string
{
$typeMap = [
1 => '挂号费',
2 => '问诊费',
3 => '药品费用',
4 => '首付费用',
5 => '尾款费用',
6 => '其他费用',
7 => '全部费用',
8 => '驼奶费用',
];
$statusMap = [
1 => '待支付',
2 => '已支付',
3 => '已取消',
4 => '已退款',
5 => '待审核',
];
$orderNo = trim((string) ($pay['order_no'] ?? ''));
$amount = number_format(round((float) ($pay['amount'] ?? 0), 2), 2, '.', '');
$typeDesc = $typeMap[(int) ($pay['order_type'] ?? 0)] ?? '—';
$statusDesc = $statusMap[(int) ($pay['status'] ?? 0)] ?? '—';
$source = self::formatPayOrderSourceForExport($pay);
$cid = (int) ($pay['creator_id'] ?? 0);
$creator = $cid > 0 ? (string) ($creatorNames[$cid] ?? '') : '—';
$timeStr = self::formatDateTimeForExport($pay['create_time'] ?? '') ?: '—';
$line1 = sprintf('[%d] %s ¥%s %s %s', $index, $orderNo, $amount, $typeDesc, $statusDesc);
$line2 = sprintf(' %s %s %s', $source, $creator, $timeStr);
return $line1 . "\n" . $line2;
}
/**
* 导出用:批量解析业务订单关联收款记录(与详情 linked_pay_orders 同口径:status ∈ {2,4,5}
*
* @param int[] $poIds
*
* @return array<int, string> prescription_order_id => 多笔以空行分隔的可读文本
*/
private static function batchLinkedPayRecordsExportTextByPoIds(array $poIds): array
{
$poIds = array_values(array_filter(array_unique(array_map('intval', $poIds)), static fn (int $id): bool => $id > 0));
if ($poIds === []) {
return [];
}
$linksByPo = [];
$allPayIds = [];
$linkRows = PrescriptionOrderPayOrder::whereIn('prescription_order_id', $poIds)
->field(['prescription_order_id', 'pay_order_id'])
->order('id', 'asc')
->select()
->toArray();
foreach ($linkRows as $l) {
$poid = (int) ($l['prescription_order_id'] ?? 0);
$payId = (int) ($l['pay_order_id'] ?? 0);
if ($poid <= 0 || $payId <= 0) {
continue;
}
$linksByPo[$poid][] = $payId;
$allPayIds[$payId] = true;
}
$out = [];
foreach ($poIds as $poid) {
$out[$poid] = '';
}
if ($allPayIds === []) {
return $out;
}
$payRows = Order::whereIn('id', array_keys($allPayIds))
->whereNull('delete_time')
->whereIn('status', [2, 4, 5])
->field(['id', 'order_no', 'order_type', 'amount', 'status', 'create_time', 'creator_id', 'payment_method', 'create_type'])
->select()
->toArray();
$payById = [];
$creatorIds = [];
foreach ($payRows as $r) {
$id = (int) ($r['id'] ?? 0);
if ($id <= 0) {
continue;
}
$payById[$id] = $r;
$cid = (int) ($r['creator_id'] ?? 0);
if ($cid > 0) {
$creatorIds[$cid] = true;
}
}
$creatorNames = $creatorIds !== []
? Admin::whereIn('id', array_keys($creatorIds))->column('name', 'id')
: [];
foreach ($poIds as $poid) {
$lines = [];
$seq = 0;
foreach ($linksByPo[$poid] ?? [] as $payId) {
$pay = $payById[$payId] ?? null;
if (!\is_array($pay)) {
continue;
}
++$seq;
$lines[] = self::formatLinkedPayRecordLineForExport($pay, $creatorNames, $seq);
}
$out[$poid] = implode("\n\n", $lines);
}
return $out;
}
/**
* 业务订单列表导出:写入与 PrescriptionOrderLists::setExcelFields 对应的字段(export=2 时由列表类调用)
* 「挂号渠道来源」取值与详情/列表解析一致:处方 appointment_id → 否则诊单下 MAX(挂号.id);业绩抽屉带渠道时与列表同源高亮。
@@ -3392,6 +3579,7 @@ class PrescriptionOrderLogic
$firstVisitAssistantByDiag = self::batchFirstVisitAssistantNameByDiagnosis($diagIdList, $diagById);
$assistantDeptCache = [];
$linkedPayExportByPo = self::batchLinkedPayRecordsExportTextByPoIds($poIds);
foreach ($lists as &$item) {
$poId = (int) ($item['id'] ?? 0);
@@ -3464,6 +3652,7 @@ class PrescriptionOrderLogic
$paid = round((float) ($item['linked_pay_paid_total'] ?? 0), 2);
$item['export_amount'] = number_format($amt, 2, '.', '');
$item['export_paid_amount'] = number_format($paid, 2, '.', '');
$item['export_linked_pay_records'] = $linkedPayExportByPo[$poId] ?? '';
$refundStored = round((float) ($item['refund_amount'] ?? 0), 2);
$item['export_refund_amount'] = $refundStored > 0
? number_format($refundStored, 2, '.', '')