Files
zyt/server/app/common/service/esign/PrescriptionPdf.php
T
Your NameandClaude Opus 5 (1M context) 7c916d8053 签署原文中金额为 0、辅方不代煎时不再写入
- 金额(元)为 0 / 0.00 时省略,非 0 照常写入
- 辅方代煎为 0 时省略;代煎时显示「是」(与主方代煎一致),不再显示 0/1

Co-Authored-By: Claude Opus 5 (1M context) <[EMAIL]>
2026-11-27 18:27:13 +08:00

147 lines
7.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
namespace app\common\service\esign;
/** Deterministic, text based A4 prescription; fixed first-page signature area. */
final class PrescriptionPdf
{
public static function render(array $row, string $environment, int $issuedAt = 0, bool $review = false): string
{
$row = PrescriptionEsignPolicy::snapshot($row);
$lines = [];
$add = static function (string $label, mixed $value) use (&$lines): void {
if ($value === null || $value === '' || $value === []) {
return;
}
if (is_array($value)) {
$value = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR);
}
$text = $label . ':' . (string) $value;
foreach (preg_split('/\R/u', $text) as $paragraph) {
$current = '';
foreach (mb_str_split($paragraph) as $char) {
if (mb_strwidth($current . $char, 'UTF-8') > 84) {
$lines[] = $current;
$current = ' ';
}
$current .= $char;
}
$lines[] = $current;
}
};
$add('处方编号', $row['sn']);
$add('日期', $row['prescription_date']);
if ($issuedAt > 0) {
$time = (new \DateTimeImmutable('@' . $issuedAt))->setTimezone(new \DateTimeZone('Asia/Shanghai'));
$add('开具确认时间(北京时间)', $time->format('Y-m-d H:i:s'));
}
$add('患者', $row['patient_name'] . ' 性别:' . ($row['gender'] === '1' ? '男' : '女') . ' 年龄:' . $row['age']);
$add('联系电话', $row['phone']);
$add('门诊号', $row['visit_no']);
$add('临床诊断', $row['clinical_diagnosis']);
$add('舌象', $row['tongue']);
$add('脉象', $row['pulse']);
$add('脉诊补充', $row['pulse_condition']);
$add('剂型', $row['prescription_type']);
$herbLabels = ['name' => '药名', 'dosage' => '用量', 'unit' => '单位', 'remark' => '备注',
'usage' => '煎法', 'usage_method' => '煎法', 'method' => '煎法', 'group' => '组别',
'group_type' => '组别', 'formula_type' => '方别', 'is_aux' => '辅方', 'price' => '价格'];
foreach ((array) $row['herbs'] as $index => $herb) {
$parts = [];
foreach ((array) $herb as $key => $value) {
if (in_array($key, ['id', 'medicine_id', 'herb_id', 'name_pinyin', 'pinyin'], true) || $value === '' || $value === null) {
continue;
}
$parts[] = ($herbLabels[$key] ?? $key) . ' ' . (is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) : $value);
}
$add('药品 ' . ($index + 1), implode(' / ', $parts));
}
foreach ([
'dosage_amount' => '单次用量', 'dosage_unit' => '用量单位', 'dosage_bag_count' => '单次袋数',
'usage_days' => '服用天数', 'times_per_day' => '每日次数', 'usage_instruction' => '服用说明',
'usage_time' => '服药时间', 'usage_way' => '服用方式', 'dietary_taboo' => '忌口', 'usage_notes' => '医嘱',
] as $key => $label) {
$add($label, $row[$key]);
}
$add('代煎', $row['need_decoction'] === '1' ? '是' : '否');
// 金额为 0(含 0.00)时不写入签署原文。
if (is_numeric($row['amount']) && (float) $row['amount'] != 0.0) {
$add('金额(元)', $row['amount']);
}
// 开方表单总会带一份默认辅方用法;只有药品里确有辅方(与笺面 formula_type 判定一致)才写入。
$hasAux = array_filter((array) $row['herbs'], static fn ($herb): bool =>
is_array($herb) && ($herb['formula_type'] ?? '') === '辅方') !== [];
foreach ($hasAux ? (array) $row['aux_usage'] : [] as $key => $value) {
if ($key === 'bags_per_dose') {
continue;
}
$label = ['dosage_amount' => '单次用量', 'dosage_bag_count' => '单次袋数', 'need_decoction' => '代煎',
'times_per_day' => '每日次数', 'usage_days' => '服用天数',
'prescription_name' => '方名'][$key] ?? $key;
if ($key === 'need_decoction') {
// 辅方不代煎(0)时不写;代煎时与主方一样显示「是」。
if ((int) $value === 0) {
continue;
}
$value = '是';
}
$add('辅方' . $label, $value);
}
$pages = array_chunk($lines, 33);
$objects = [1 => '', 2 => '',
3 => '<< /Type /Font /Subtype /Type0 /BaseFont /STSong-Light /Encoding /UniGB-UCS2-H /DescendantFonts [4 0 R] >>',
4 => '<< /Type /Font /Subtype /CIDFontType0 /BaseFont /STSong-Light /CIDSystemInfo << /Registry (Adobe) /Ordering (GB1) /Supplement 4 >> /FontDescriptor 5 0 R /DW 1000 /W [1 95 500] >>',
5 => '<< /Type /FontDescriptor /FontName /STSong-Light /Flags 6 /FontBBox [-25 -254 1000 880] /ItalicAngle 0 /Ascent 880 /Descent -254 /CapHeight 880 /StemV 80 >>',
];
$kids = [];
foreach ($pages as $index => $pageLines) {
$pageId = 6 + $index * 2;
$streamId = $pageId + 1;
$kids[] = $pageId . ' 0 R';
$stream = self::text('中医处方笺', 235, 795, 20);
if ($environment === 'sandbox') {
$stream .= self::text('沙箱测试文件 · 不用于正式发药', 170, 770, 12);
}
$y = 738;
foreach ($pageLines as $line) {
$stream .= self::text($line, 45, $y, 11);
$y -= 18;
}
$stream .= "0.7 w 45 133 m 550 133 l S\n";
$stream .= self::text('开方医师:' . $row['doctor_name'], 45, 110, 12);
if ($index === 0) {
$stream .= self::text('医师电子签章', 420, 104, 11);
if ($review) {
$stream .= self::text('药师审核', 420, 59, 11);
}
}
$stream .= self::text('处方快照 SHA-256', 45, 58, 8);
$stream .= self::text(PrescriptionEsignPolicy::hash($row), 45, 44, 7);
$stream .= self::text(($index + 1) . ' / ' . count($pages), 515, 30, 9);
$objects[$pageId] = '<< /Type /Page /Parent 2 0 R /MediaBox [0 0 595 842] /Resources << /Font << /F1 3 0 R >> >> /Contents ' . $streamId . ' 0 R >>';
$objects[$streamId] = '<< /Length ' . strlen($stream) . ">>\nstream\n" . $stream . 'endstream';
}
$objects[1] = '<< /Type /Catalog /Pages 2 0 R >>';
$objects[2] = '<< /Type /Pages /Kids [' . implode(' ', $kids) . '] /Count ' . count($pages) . ' >>';
ksort($objects);
$pdf = "%PDF-1.4\n%\xE2\xE3\xCF\xD3\n";
$offsets = [0];
foreach ($objects as $id => $object) {
$offsets[$id] = strlen($pdf);
$pdf .= "$id 0 obj\n$object\nendobj\n";
}
$xref = strlen($pdf);
$pdf .= 'xref' . "\n0 " . (count($objects) + 1) . "\n0000000000 65535 f \n";
foreach ($objects as $id => $_) {
$pdf .= sprintf("%010d 00000 n \n", $offsets[$id]);
}
return $pdf . 'trailer << /Size ' . (count($objects) + 1) . " /Root 1 0 R >>\nstartxref\n$xref\n%%EOF\n";
}
private static function text(string $text, int $x, int $y, int $size): string
{
return "BT /F1 $size Tf 1 0 0 1 $x $y Tm <" . bin2hex(mb_convert_encoding($text, 'UTF-16BE', 'UTF-8')) . "> Tj ET\n";
}
}