Files
zyt/server/app/common/service/esign/PrescriptionPdf.php
T
Your NameandClaude Opus 5 b55f30753c 处方作废不再受签署/审核/订单状态限制,作废后订单随重新开方自动关联;签署原文去掉辅方方名;出丸显示辅方克数;查看处方可下载 CA 原件或药房联/处方联
- 作废:药师签署、审核通过、已有业务订单后均可作废(仍需「作废」权限;已提交药房的订单须先远端取消)。
  作废时关联的待审核/履约中订单处方审核重置为待审核(履约中退回待审核,支付审核保留),
  同诊单重新开方后自动改关联到新处方并写订单日志;已发货/已完成订单按原处方留档。
  迁移 2026_11_30_prescription_order_reissue.sql 新增 reissue_from_prescription_id
- 签署 PDF 不再写入「辅方方名」
- 出丸:有辅方药材时显示「主方 X克,辅方 Y克」(管理后台与桌面端)
- 已开处方「查看处方」下载改为下拉:CA 签署处方 PDF、药房联 PDF、处方联 PDF(A4,按当前笺面样式)
- 重新构建管理后台

Co-Authored-By: Claude Opus 5 <[EMAIL]>
2026-11-30 10:45:08 +08:00

150 lines
7.8 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 === 'prescription_name') {
continue; // 辅方方名仅内部使用,不提交签署原文。
}
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";
}
}