更新
This commit is contained in:
@@ -72,7 +72,9 @@ class PrescriptionOrderController extends BaseAdminController
|
||||
public function detail()
|
||||
{
|
||||
$params = (new PrescriptionOrderValidate())->get()->goCheck('detail');
|
||||
$detail = PrescriptionOrderLogic::detail((int) $params['id'], $this->adminId, $this->adminInfo);
|
||||
$detail = PrescriptionOrderLogic::detail(
|
||||
(int) $params['id'], $this->adminId, $this->adminInfo, (int) ($params['prescription_id'] ?? 0)
|
||||
);
|
||||
if ($detail === null) {
|
||||
return $this->fail(PrescriptionOrderLogic::getError());
|
||||
}
|
||||
@@ -80,6 +82,29 @@ class PrescriptionOrderController extends BaseAdminController
|
||||
return $this->data($detail);
|
||||
}
|
||||
|
||||
public function prescriptionOrdersForBinding()
|
||||
{
|
||||
$params = (new PrescriptionOrderValidate())->get()->goCheck('prescriptionOrdersForBinding');
|
||||
try {
|
||||
return $this->data(PrescriptionOrderLogic::prescriptionOrdersForBinding(
|
||||
(int) $params['prescription_id'], trim((string) ($params['order_no'] ?? '')), $this->adminId, $this->adminInfo
|
||||
));
|
||||
} catch (\DomainException $exception) {
|
||||
return $this->fail($exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function bindPrescription()
|
||||
{
|
||||
$params = (new PrescriptionOrderValidate())->post()->goCheck('bindPrescription');
|
||||
try {
|
||||
PrescriptionOrderLogic::bindPrescription((int) $params['id'], (int) $params['prescription_id'], $this->adminId, $this->adminInfo);
|
||||
} catch (\DomainException $exception) {
|
||||
return $this->fail($exception->getMessage());
|
||||
}
|
||||
return $this->success('业务订单已绑定');
|
||||
}
|
||||
|
||||
/**
|
||||
* 快递轨迹查询(对接快递100;未配置时仍可跳转顺丰/京东官网)
|
||||
*/
|
||||
@@ -437,6 +462,18 @@ class PrescriptionOrderController extends BaseAdminController
|
||||
return $this->success('关联支付单成功', $result);
|
||||
}
|
||||
|
||||
/** 在业务订单详情关联一笔已有收款。 */
|
||||
public function attachReceipt()
|
||||
{
|
||||
$params = (new PrescriptionOrderValidate())->post()->goCheck('attachReceipt');
|
||||
$result = PrescriptionOrderLogic::attachReceipt($params, $this->adminId, $this->adminInfo);
|
||||
if ($result === false) {
|
||||
return $this->fail(PrescriptionOrderLogic::getError());
|
||||
}
|
||||
|
||||
return $this->success('收款已关联', $result);
|
||||
}
|
||||
|
||||
/** 解除单笔收款关联,总金额不变,同步更新已付金额和需代收。 */
|
||||
public function unlinkPayOrder()
|
||||
{
|
||||
|
||||
@@ -10,7 +10,6 @@ use app\common\model\OrderDetail;
|
||||
use app\common\model\auth\Admin;
|
||||
use app\common\model\auth\AdminRole;
|
||||
use app\common\model\tcm\Diagnosis;
|
||||
use app\common\model\tcm\PrescriptionOrder;
|
||||
use app\common\model\tcm\PrescriptionOrderPayOrder;
|
||||
use app\common\service\wechat\WechatWorkExternalPayService;
|
||||
use think\facade\Db;
|
||||
@@ -1300,7 +1299,7 @@ class OrderLogic
|
||||
|
||||
/**
|
||||
* 某诊单下可关联的支付单(待支付/已支付),供关联业务订单多选(主管/诊单医助看该诊单全部;否则仅本人创建)
|
||||
* 已占用(关联到未撤回/未取消的业务订单)的支付单会排除;编辑某业务订单时传 $exceptPrescriptionOrderId 以保留当前单已选中的项
|
||||
* 已关联到未删除业务订单的支付单会排除;编辑时保留当前单已选中的项
|
||||
* 列表仅含 2026-04-20 及之后创建的支付单(已关联到当前编辑业务订单的除外)
|
||||
*
|
||||
* @return array<int, array<string, mixed>>
|
||||
@@ -1332,15 +1331,8 @@ class OrderLogic
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
$activePoIds = PrescriptionOrder::whereNull('delete_time')
|
||||
->where('fulfillment_status', '<>', 4)
|
||||
->column('id');
|
||||
$busyPayIds = [];
|
||||
if ($activePoIds !== []) {
|
||||
$busyPayIds = PrescriptionOrderPayOrder::whereIn('prescription_order_id', $activePoIds)
|
||||
->column('pay_order_id');
|
||||
$busyPayIds = array_unique(array_map('intval', $busyPayIds));
|
||||
}
|
||||
$busyPayIds = $rows === [] ? [] : array_map('intval', self::prescriptionOrderReceiptLinks(array_column($rows, 'id'))
|
||||
->column('link.pay_order_id'));
|
||||
$whiteList = [];
|
||||
if ($exceptPrescriptionOrderId !== null && $exceptPrescriptionOrderId > 0) {
|
||||
$whiteList = array_map(
|
||||
@@ -1372,8 +1364,17 @@ class OrderLogic
|
||||
return $out;
|
||||
}
|
||||
|
||||
/** @param int[] $payOrderIds */
|
||||
public static function prescriptionOrderReceiptLinks(array $payOrderIds): \think\db\Query
|
||||
{
|
||||
return Db::name('tcm_prescription_order_pay_order')->alias('link')
|
||||
->join('tcm_prescription_order po', 'po.id = link.prescription_order_id')
|
||||
->whereIn('link.pay_order_id', $payOrderIds)
|
||||
->whereNull('po.delete_time');
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验支付单均可关联到指定诊单(已支付、归属与权限)
|
||||
* 校验支付单均可关联到指定诊单(已支付、归属与权限),调用方须持有事务
|
||||
* 默认仅允许关联 2026-04-20 及之后创建的支付单;编辑业务订单时传入 $prescriptionOrderIdForDateGrace,已关联到该业务订单的旧单除外
|
||||
*
|
||||
* @param int[] $ids zyt_order.id
|
||||
@@ -1402,7 +1403,7 @@ class OrderLogic
|
||||
$assistantId = (int) Diagnosis::where('id', $diagnosisId)->value('assistant_id');
|
||||
$isDiagAssistant = $assistantId === $adminId;
|
||||
|
||||
$orders = Order::whereIn('id', $ids)->whereNull('delete_time')->select();
|
||||
$orders = Order::whereIn('id', $ids)->whereNull('delete_time')->order('id', 'asc')->lock(true)->select();
|
||||
if (count($orders) !== count($ids)) {
|
||||
return '部分支付单不存在或已删除';
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@ use app\common\service\storage\Driver as StorageDriver;
|
||||
use app\common\service\tcm\RevisitDueHook;
|
||||
use app\common\service\tcm\RevisitDueService;
|
||||
use app\common\service\tcm\RevisitVisitSlotService;
|
||||
use think\db\Query;
|
||||
use think\facade\Db;
|
||||
/**
|
||||
* 中医辨房病因诊单逻辑
|
||||
@@ -4356,7 +4357,18 @@ class DiagnosisLogic extends BaseLogic
|
||||
return false;
|
||||
}
|
||||
|
||||
$query = Diagnosis::where('id', $diagnosisId)->whereNull('delete_time');
|
||||
if (!self::readonlyDiagnosisQuery($adminId, $adminInfo)->where('id', $diagnosisId)->find()) {
|
||||
self::setError('诊单不存在或无权访问');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function readonlyDiagnosisQuery(int $adminId, array $adminInfo): Query
|
||||
{
|
||||
$query = Diagnosis::whereNull('delete_time');
|
||||
// 纯医助只看本人归属诊单;兼有团队角色时交给下方数据范围按部门过滤
|
||||
$selfRoles = DataScopeService::selfOnlyRoleIds($adminId, $adminInfo);
|
||||
if (in_array(2, $selfRoles, true)) {
|
||||
@@ -4366,22 +4378,13 @@ class DiagnosisLogic extends BaseLogic
|
||||
if (DataScopeService::isEnabled()) {
|
||||
$visibleIds = DataScopeService::getVisibleAdminIds($adminId, $adminInfo);
|
||||
if ($visibleIds === []) {
|
||||
self::setError('诊单不存在或无权访问');
|
||||
|
||||
return false;
|
||||
}
|
||||
if (is_array($visibleIds)) {
|
||||
$query->whereRaw('1 = 0');
|
||||
} elseif (is_array($visibleIds)) {
|
||||
$query->whereIn('assistant_id', $visibleIds);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$query->find()) {
|
||||
self::setError('诊单不存在或无权访问');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -989,6 +989,19 @@ class PrescriptionLogic
|
||||
}
|
||||
}
|
||||
|
||||
/** 关联处方预览使用处方原始内容和只读签署状态。 */
|
||||
public static function orderReferenceDetail(int $id, int $diagnosisId, int $adminId, array $adminInfo): ?array
|
||||
{
|
||||
$row = Prescription::where('id', $id)->where('diagnosis_id', $diagnosisId)->whereNull('delete_time')->find();
|
||||
if (!$row || !self::canViewPrescription($row, $adminId, $adminInfo)) {
|
||||
return null;
|
||||
}
|
||||
$data = self::withSigningStatus($row->toArray(), 0, false, true);
|
||||
$data['can_review_prescription'] = false;
|
||||
$data['esign']['can_review_prescription'] = false;
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处方详情(带查看权限校验,供后台管理端)
|
||||
*/
|
||||
|
||||
@@ -50,6 +50,10 @@ class PrescriptionOrderLogic
|
||||
private static string $error = '';
|
||||
|
||||
private const TRACKING_AUDIT_REQUIRED = '处方审核和支付单审核均通过后,才可填写或修改快递单号';
|
||||
private const ATTACH_RECEIPT_PERMISSION = 'tcm.prescriptionOrder/attachReceipt';
|
||||
private const VIEW_RX_LINKS_PERMISSION = 'tcm.prescriptionOrder/prescriptionOrdersForBinding';
|
||||
private const BIND_RX_PERMISSION = 'tcm.prescriptionOrder/bindPrescription';
|
||||
private const RX_LINK_TABLE = 'tcm_prescription_order_prescription';
|
||||
|
||||
public static function setError(string $msg): void
|
||||
{
|
||||
@@ -380,7 +384,8 @@ class PrescriptionOrderLogic
|
||||
}
|
||||
$perms = AuthLogic::getAuthByAdminId((int) ($adminInfo['admin_id'] ?? 0));
|
||||
foreach ($perms as $p) {
|
||||
if (is_string($p) && str_starts_with($p, 'tcm.prescriptionOrder/')) {
|
||||
if (is_string($p) && str_starts_with($p, 'tcm.prescriptionOrder/')
|
||||
&& !in_array($p, [self::VIEW_RX_LINKS_PERMISSION, self::BIND_RX_PERMISSION], true)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -897,7 +902,7 @@ class PrescriptionOrderLogic
|
||||
}
|
||||
|
||||
/**
|
||||
* 支付单不可同时关联多条「有效」业务订单(未删除且未撤回/取消);$exceptPrescriptionOrderId 为当前编辑单 ID 时跳过自检
|
||||
* 收款须由调用方在同一事务中锁定。撤回会清除原关联,取消单事后补录的关联仍占用该收款。
|
||||
*
|
||||
* @param int[] $payOrderIds
|
||||
*/
|
||||
@@ -907,27 +912,13 @@ class PrescriptionOrderLogic
|
||||
if ($ids === []) {
|
||||
return null;
|
||||
}
|
||||
$activePoIds = PrescriptionOrder::whereNull('delete_time')
|
||||
->where('fulfillment_status', '<>', 4)
|
||||
->column('id');
|
||||
if ($activePoIds === []) {
|
||||
return null;
|
||||
$links = OrderLogic::prescriptionOrderReceiptLinks($ids);
|
||||
if ($exceptPrescriptionOrderId !== null) {
|
||||
$links->where('link.prescription_order_id', '<>', $exceptPrescriptionOrderId);
|
||||
}
|
||||
$activeSet = array_fill_keys(array_map('intval', $activePoIds), true);
|
||||
$links = PrescriptionOrderPayOrder::whereIn('pay_order_id', $ids)->select();
|
||||
foreach ($links as $link) {
|
||||
$poId = (int) $link->prescription_order_id;
|
||||
if ($exceptPrescriptionOrderId !== null && $poId === $exceptPrescriptionOrderId) {
|
||||
continue;
|
||||
}
|
||||
if (!isset($activeSet[$poId])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return '支付单 #' . (int) $link->pay_order_id . ' 已关联其他有效业务订单,不可重复关联';
|
||||
}
|
||||
|
||||
return null;
|
||||
// 等待收款行锁后,用当前读检查其他事务是否已经占用收款。
|
||||
$link = $links->field('link.pay_order_id')->lock(true)->find();
|
||||
return $link ? '支付单 #' . $link['pay_order_id'] . ' 已关联业务订单,不可重复关联' : null;
|
||||
}
|
||||
|
||||
/** 收款关联增删共用订单行锁,失败时连同金额和日志一起回滚。 */
|
||||
@@ -1073,22 +1064,22 @@ class PrescriptionOrderLogic
|
||||
if (!$order) {
|
||||
return;
|
||||
}
|
||||
$amount = round((float) $order->amount, 2);
|
||||
$paid = 0.0;
|
||||
$ids = self::linkedPayOrderIdList($id);
|
||||
if ($ids !== []) {
|
||||
$payOrders = Order::whereIn('id', $ids)->whereNull('delete_time')->field(['amount', 'status'])->select();
|
||||
foreach ($payOrders as $po) {
|
||||
$st = (int) $po->status;
|
||||
if ($st === 2 || $st === 5) {
|
||||
$paid += (float) $po->amount;
|
||||
}
|
||||
}
|
||||
}
|
||||
$order->agency_collect_amount = round($amount - $paid, 2);
|
||||
$amount = (int) round((float) $order->amount * 100);
|
||||
$paid = self::linkedPayPaidCents(self::linkedPayOrderIdList($id));
|
||||
$order->agency_collect_amount = ($amount - $paid) / 100;
|
||||
$order->save();
|
||||
}
|
||||
|
||||
/** @param int[] $ids */
|
||||
private static function linkedPayPaidCents(array $ids): int
|
||||
{
|
||||
if ($ids === []) {
|
||||
return 0;
|
||||
}
|
||||
return (int) round((float) Order::whereIn('id', $ids)->whereNull('delete_time')
|
||||
->whereIn('status', [2, 5])->sum('amount') * 100);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $params
|
||||
* @return array<string,mixed>|false
|
||||
@@ -1329,7 +1320,199 @@ class PrescriptionOrderLogic
|
||||
return $edited;
|
||||
}
|
||||
|
||||
public static function detail(int $id, int $adminId, array $adminInfo): ?array
|
||||
private static function prescriptionLinkPermissions(int $adminId, array $adminInfo): array
|
||||
{
|
||||
if ($adminId <= 0) {
|
||||
return [];
|
||||
}
|
||||
if ((int) ($adminInfo['root'] ?? 0) === 1) {
|
||||
return [self::VIEW_RX_LINKS_PERMISSION, self::BIND_RX_PERMISSION];
|
||||
}
|
||||
return AuthLogic::getAuthByAdminId($adminId);
|
||||
}
|
||||
|
||||
/** @return int[] 有权访问的同患者诊单。没有患者编号的旧诊单仅匹配自身。 */
|
||||
private static function prescriptionLinkDiagnosisIds(int $diagnosisId, int $adminId, array $adminInfo): array
|
||||
{
|
||||
if ($diagnosisId <= 0) {
|
||||
return [];
|
||||
}
|
||||
$scope = DiagnosisLogic::readonlyDiagnosisQuery($adminId, $adminInfo);
|
||||
$diagnosis = (clone $scope)->where('id', $diagnosisId)->field('id,patient_id')->find();
|
||||
if (!$diagnosis) {
|
||||
return [];
|
||||
}
|
||||
$patientId = (int) $diagnosis->patient_id;
|
||||
return $patientId > 0
|
||||
? array_map('intval', $scope->where('patient_id', $patientId)->column('id'))
|
||||
: [$diagnosisId];
|
||||
}
|
||||
|
||||
private static function scopedOrdersForPrescriptionLink(array $diagnosisIds, int $adminId, array $adminInfo): Query
|
||||
{
|
||||
$query = Db::name('tcm_prescription_order')->alias('o')
|
||||
->whereIn('o.diagnosis_id', $diagnosisIds)->whereNull('o.delete_time');
|
||||
self::applyPrescriptionOrderListAccessForCommissionQuery($query, 'o', $adminId, $adminInfo);
|
||||
return $query;
|
||||
}
|
||||
|
||||
private static function ordersWithReplaceablePrescription(Query $orders): Query
|
||||
{
|
||||
return $orders->leftJoin([Db::name('tcm_prescription')->getTable() => 'original_rx'],
|
||||
'original_rx.id = o.prescription_id AND original_rx.diagnosis_id = o.diagnosis_id'
|
||||
. ' AND original_rx.delete_time IS NULL')
|
||||
->where(static function (Query $query): void {
|
||||
$query->where('original_rx.void_status', 1)->whereOr('original_rx.audit_status', 2)
|
||||
->whereOr('o.prescription_audit_status', 2);
|
||||
});
|
||||
}
|
||||
|
||||
/** @return array{orders: array, can_bind: bool} */
|
||||
public static function prescriptionOrdersForBinding(int $rxId, string $orderNo, int $adminId, array $adminInfo): array
|
||||
{
|
||||
$permissions = self::prescriptionLinkPermissions($adminId, $adminInfo);
|
||||
if (!in_array(self::VIEW_RX_LINKS_PERMISSION, $permissions, true)) {
|
||||
throw new \DomainException('无权限查看关联业务订单');
|
||||
}
|
||||
|
||||
$rx = Prescription::where('id', $rxId)->whereNull('delete_time')->find();
|
||||
if (!$rx || !PrescriptionLogic::canViewPrescription($rx, $adminId, $adminInfo)) {
|
||||
throw new \DomainException('处方不存在或无权查看');
|
||||
}
|
||||
$diagnosisId = (int) $rx->diagnosis_id;
|
||||
if ($diagnosisId <= 0) {
|
||||
return ['orders' => [], 'can_bind' => false];
|
||||
}
|
||||
$diagnosisIds = self::prescriptionLinkDiagnosisIds($diagnosisId, $adminId, $adminInfo);
|
||||
if ($diagnosisIds === []) {
|
||||
throw new \DomainException('诊单不存在或无权访问');
|
||||
}
|
||||
|
||||
$canBind = (int) $rx->void_status === 0 && (int) $rx->audit_status !== 2
|
||||
&& in_array(self::BIND_RX_PERMISSION, $permissions, true);
|
||||
$ids = Db::name(self::RX_LINK_TABLE)->where('prescription_id', $rxId)->column('prescription_order_id');
|
||||
$base = self::scopedOrdersForPrescriptionLink($diagnosisIds, $adminId, $adminInfo)
|
||||
->field('o.id,o.order_no,o.fulfillment_status,o.prescription_id')->order('o.id', 'desc');
|
||||
$linked = (clone $base)->where(static function (Query $query) use ($rxId, $ids): void {
|
||||
$query->where('o.prescription_id', $rxId);
|
||||
if ($ids !== []) {
|
||||
$query->whereOr('o.id', 'in', $ids);
|
||||
}
|
||||
})->select()->toArray();
|
||||
|
||||
foreach ($linked as &$order) {
|
||||
$order['id'] = (int) $order['id'];
|
||||
$order['fulfillment_status'] = (int) $order['fulfillment_status'];
|
||||
$order['is_primary'] = (int) $order['prescription_id'] === $rxId;
|
||||
$order['is_linked'] = true;
|
||||
unset($order['prescription_id']);
|
||||
}
|
||||
unset($order);
|
||||
|
||||
if ($canBind) {
|
||||
$candidates = self::ordersWithReplaceablePrescription($base)
|
||||
->where(static function (Query $query) use ($rxId): void {
|
||||
$query->whereNull('o.prescription_id')->whereOr('o.prescription_id', '<>', $rxId);
|
||||
});
|
||||
if ($ids !== []) {
|
||||
$candidates->whereNotIn('o.id', $ids);
|
||||
}
|
||||
if ($orderNo !== '') {
|
||||
$candidates->whereLike('o.order_no', '%' . $orderNo . '%');
|
||||
}
|
||||
foreach ($candidates->limit(30)->select() as $order) {
|
||||
$linked[] = [
|
||||
'id' => (int) $order['id'], 'order_no' => $order['order_no'],
|
||||
'fulfillment_status' => (int) $order['fulfillment_status'],
|
||||
'is_linked' => false, 'is_primary' => false,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return ['orders' => $linked, 'can_bind' => $canBind];
|
||||
}
|
||||
|
||||
/** 原处方作废或驳回后可补关联同患者的新处方,供订单详情和处方笺查阅。 */
|
||||
public static function bindPrescription(int $orderId, int $rxId, int $adminId, array $adminInfo): void
|
||||
{
|
||||
$permissions = self::prescriptionLinkPermissions($adminId, $adminInfo);
|
||||
if (array_diff([self::VIEW_RX_LINKS_PERMISSION, self::BIND_RX_PERMISSION], $permissions) !== []) {
|
||||
throw new \DomainException('无权限绑定业务订单');
|
||||
}
|
||||
|
||||
Db::transaction(static function () use ($orderId, $rxId, $adminId, $adminInfo): void {
|
||||
// 与处方作废和业务订单审核保持相同的加锁顺序。
|
||||
$order = Db::name('tcm_prescription_order')->where('id', $orderId)->whereNull('delete_time')->lock(true)->find();
|
||||
if (!$order || !self::scopedOrdersForPrescriptionLink([(int) $order['diagnosis_id']], $adminId, $adminInfo)
|
||||
->where('o.id', $orderId)->find()) {
|
||||
throw new \DomainException('业务订单不存在或无权访问');
|
||||
}
|
||||
$rx = Prescription::where('id', $rxId)->whereNull('delete_time')->lock(true)->find();
|
||||
if (!$rx || !PrescriptionLogic::canViewPrescription($rx, $adminId, $adminInfo)) {
|
||||
throw new \DomainException('处方不存在或无权查看');
|
||||
}
|
||||
$diagnosisIds = self::prescriptionLinkDiagnosisIds((int) $rx->diagnosis_id, $adminId, $adminInfo);
|
||||
if (!in_array((int) $order['diagnosis_id'], $diagnosisIds, true)) {
|
||||
throw new \DomainException('仅可绑定同一患者且有权访问的业务订单');
|
||||
}
|
||||
|
||||
$keys = ['prescription_order_id' => $orderId, 'prescription_id' => $rxId];
|
||||
if ((int) $order['prescription_id'] === $rxId || Db::name(self::RX_LINK_TABLE)->where($keys)->find()) {
|
||||
return;
|
||||
}
|
||||
if ((int) $rx->void_status !== 0 || (int) $rx->audit_status === 2) {
|
||||
throw new \DomainException('已作废或已驳回的处方不能作为新处方绑定');
|
||||
}
|
||||
$original = self::ordersWithReplaceablePrescription(
|
||||
Db::name('tcm_prescription_order')->alias('o')->where('o.id', $orderId)
|
||||
)->field('original_rx.sn,original_rx.void_status')->lock(true)->find();
|
||||
if (!$original) {
|
||||
throw new \DomainException('原处方已作废或处方审核已驳回的业务订单才可绑定新处方');
|
||||
}
|
||||
Db::name(self::RX_LINK_TABLE)->insert($keys + ['create_by' => $adminId, 'create_time' => time()]);
|
||||
$reason = (int) $original['void_status'] === 1 ? '已作废' : '审核已驳回';
|
||||
$subject = $original['sn'] !== null ? '原处方 ' . $original['sn'] . ' ' : '业务订单处方';
|
||||
self::writeLog($orderId, $adminId, $adminInfo, 'bind_prescription',
|
||||
$subject . $reason . ',绑定关联处方 ' . (string) $rx->sn, true);
|
||||
});
|
||||
}
|
||||
|
||||
/** @return ?array 已授权时返回可见范围内的处方摘要 */
|
||||
private static function linkedPrescriptionsForDetail(array $order, int $adminId, array $adminInfo): ?array
|
||||
{
|
||||
if (!in_array(self::VIEW_RX_LINKS_PERMISSION, self::prescriptionLinkPermissions($adminId, $adminInfo), true)) {
|
||||
return null;
|
||||
}
|
||||
$diagnosisId = (int) ($order['diagnosis_id'] ?? 0);
|
||||
$diagnosisIds = self::prescriptionLinkDiagnosisIds($diagnosisId, $adminId, $adminInfo);
|
||||
if ($diagnosisIds === [] || !self::scopedOrdersForPrescriptionLink([$diagnosisId], $adminId, $adminInfo)
|
||||
->where('o.id', (int) $order['id'])->find()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$primaryId = (int) $order['prescription_id'];
|
||||
$ids = Db::name(self::RX_LINK_TABLE)->where('prescription_order_id', (int) $order['id'])->column('prescription_id');
|
||||
$ids[] = $primaryId;
|
||||
$visible = [];
|
||||
$rows = Db::name('tcm_prescription')->whereIn('id', $ids)->whereIn('diagnosis_id', $diagnosisIds)
|
||||
->whereNull('delete_time')->field('id,sn,diagnosis_id,void_status,creator_id,assistant_id,is_shared,visible_role_ids')
|
||||
->order('id', 'desc')->select();
|
||||
foreach ($rows as $rx) {
|
||||
if (PrescriptionLogic::canViewPrescription($rx, $adminId, $adminInfo)) {
|
||||
$id = (int) $rx['id'];
|
||||
$visible[$id] = [
|
||||
'id' => $id, 'sn' => $rx['sn'], 'diagnosis_id' => (int) $rx['diagnosis_id'],
|
||||
'void_status' => (int) $rx['void_status'], 'is_primary' => $id === $primaryId,
|
||||
];
|
||||
}
|
||||
}
|
||||
if (isset($visible[$primaryId])) {
|
||||
$visible = [$primaryId => $visible[$primaryId]] + $visible;
|
||||
}
|
||||
return array_values($visible);
|
||||
}
|
||||
|
||||
public static function detail(int $id, int $adminId, array $adminInfo, ?int $viewedRxId = null): ?array
|
||||
{
|
||||
self::$error = '';
|
||||
$row = PrescriptionOrder::where('id', $id)->whereNull('delete_time')->find();
|
||||
@@ -1344,6 +1527,16 @@ class PrescriptionOrderLogic
|
||||
return null;
|
||||
}
|
||||
$arr = $row->toArray();
|
||||
$linked = $viewedRxId === null ? null : self::linkedPrescriptionsForDetail($arr, $adminId, $adminInfo);
|
||||
$viewReference = $viewedRxId > 0 && $viewedRxId !== (int) $row->prescription_id;
|
||||
$referenceDiagnosisId = $viewReference ? (array_column($linked ?? [], 'diagnosis_id', 'id')[$viewedRxId] ?? 0) : 0;
|
||||
if ($viewReference && !$referenceDiagnosisId) {
|
||||
self::$error = '关联处方不存在或无权查看';
|
||||
return null;
|
||||
}
|
||||
if ($linked !== null) {
|
||||
$arr['linked_prescriptions'] = $linked;
|
||||
}
|
||||
self::maskInternalCostIfNeeded($arr, $adminInfo);
|
||||
self::maskRemarkExtraIfNeeded($arr, $adminInfo);
|
||||
self::attachLinkedPayOrders($arr);
|
||||
@@ -1387,21 +1580,16 @@ class PrescriptionOrderLogic
|
||||
}
|
||||
|
||||
$arr = self::withReviewAvailability($arr, $arr['prescription'] ?? []);
|
||||
if (self::canAttachReceipt($adminId, $adminInfo)) {
|
||||
$arr['attachable_receipts'] = OrderLogic::listPaidOrdersForDiagnosis(
|
||||
(int) $arr['diagnosis_id'], $adminId, $adminInfo
|
||||
);
|
||||
}
|
||||
|
||||
$canHerbs = self::canViewPrescriptionHerbsInOrderDetail($adminInfo);
|
||||
$arr['prescription_detail_herbs_visible'] = $canHerbs;
|
||||
if (! $canHerbs && isset($arr['prescription']) && is_array($arr['prescription'])) {
|
||||
$rx = &$arr['prescription'];
|
||||
$herbs = is_array($rx['herbs'] ?? null) ? $rx['herbs'] : [];
|
||||
$hasAux = false;
|
||||
foreach ($herbs as $h) {
|
||||
if (is_array($h) && (string) ($h['formula_type'] ?? '') === '辅方') {
|
||||
$hasAux = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$rx['has_aux_formula'] = $hasAux ? 1 : 0;
|
||||
unset($rx['herbs']);
|
||||
if (!$canHerbs && is_array($arr['prescription'])) {
|
||||
self::maskPrescriptionHerbs($arr['prescription']);
|
||||
}
|
||||
|
||||
// 诊单医助 / 诊单创建人姓名(tcm_diagnosis.admin_id);部门见下方:优先业务订单 creator_id
|
||||
@@ -1487,9 +1675,28 @@ class PrescriptionOrderLogic
|
||||
// 已发货后服用天数锁定(前端据此禁用天数输入,服务端同样拦截)。
|
||||
$arr['days_locked'] = self::isOrderShipped($row->toArray());
|
||||
|
||||
if ($viewReference) {
|
||||
$rx = PrescriptionLogic::orderReferenceDetail($viewedRxId, $referenceDiagnosisId, $adminId, $adminInfo);
|
||||
if (!$rx) {
|
||||
self::$error = '关联处方不存在或无权查看';
|
||||
return null;
|
||||
}
|
||||
if (!$canHerbs) {
|
||||
self::maskPrescriptionHerbs($rx);
|
||||
$rx['esign']['can_download'] = false;
|
||||
}
|
||||
$arr['prescription'] = $rx;
|
||||
}
|
||||
return $arr;
|
||||
}
|
||||
|
||||
private static function maskPrescriptionHerbs(array &$rx): void
|
||||
{
|
||||
$herbs = is_array($rx['herbs'] ?? null) ? $rx['herbs'] : [];
|
||||
$rx['has_aux_formula'] = in_array('辅方', array_column($herbs, 'formula_type'), true) ? 1 : 0;
|
||||
unset($rx['herbs']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 业务订单详情场景:仅更新关联处方的患者姓名与手机号(不重置消费者处方审核、不改变其它处方字段)
|
||||
*/
|
||||
@@ -3165,6 +3372,11 @@ class PrescriptionOrderLogic
|
||||
*/
|
||||
public static function linkPayOrder(array $params, int $adminId, array $adminInfo)
|
||||
{
|
||||
self::$error = '';
|
||||
if (!self::canAttachReceipt($adminId, $adminInfo)) {
|
||||
self::$error = '无权限关联收款';
|
||||
return false;
|
||||
}
|
||||
return self::mutatePayOrderLinks(
|
||||
(int) ($params['id'] ?? 0),
|
||||
static fn () => self::linkPayOrderLocked($params, $adminId, $adminInfo)
|
||||
@@ -3277,6 +3489,90 @@ class PrescriptionOrderLogic
|
||||
return $out;
|
||||
}
|
||||
|
||||
private static function canAttachReceipt(int $adminId, array $adminInfo): bool
|
||||
{
|
||||
return (int) ($adminInfo['root'] ?? 0) === 1
|
||||
|| in_array(self::ATTACH_RECEIPT_PERMISSION, AuthLogic::getAuthByAdminId($adminId), true);
|
||||
}
|
||||
|
||||
/** 补录已支付收款的关联,适用于任意订单状态。 */
|
||||
public static function attachReceipt(array $params, int $adminId, array $adminInfo): array|false
|
||||
{
|
||||
self::$error = '';
|
||||
if (!self::canAttachReceipt($adminId, $adminInfo)) {
|
||||
self::$error = '无权限关联收款';
|
||||
return false;
|
||||
}
|
||||
|
||||
$id = (int) ($params['id'] ?? 0);
|
||||
$payOrderId = (int) ($params['pay_order_id'] ?? 0);
|
||||
if ($payOrderId <= 0) {
|
||||
self::$error = '请选择要关联的收款记录';
|
||||
return false;
|
||||
}
|
||||
|
||||
return self::mutatePayOrderLinks($id, static function () use ($id, $payOrderId, $adminId, $adminInfo): array|false {
|
||||
$order = PrescriptionOrder::find($id);
|
||||
if (!self::canAccessOrder($order, $adminId, $adminInfo)) {
|
||||
self::$error = '无权限操作此订单';
|
||||
return false;
|
||||
}
|
||||
$error = OrderLogic::assertPayOrdersLinkable([$payOrderId], (int) $order->diagnosis_id, $adminId, $adminInfo);
|
||||
$error ??= self::assertPayOrdersExclusive([$payOrderId], null);
|
||||
if ($error !== null) {
|
||||
self::$error = $error;
|
||||
return false;
|
||||
}
|
||||
|
||||
$payOrder = Order::where('id', $payOrderId)->whereNull('delete_time')->lock(true)->find();
|
||||
$receiptCents = (int) round((float) ($payOrder->amount ?? 0) * 100);
|
||||
if (!$payOrder || $receiptCents < 0) {
|
||||
self::$error = '收款金额异常,请先核对金额';
|
||||
return false;
|
||||
}
|
||||
$previousIds = self::linkedPayOrderIdList($id);
|
||||
$previousLinkedCents = self::linkedPayPaidCents($previousIds);
|
||||
$linkedCents = $previousLinkedCents + $receiptCents;
|
||||
$amountCents = (int) round((float) $order->amount * 100);
|
||||
$previousDueCents = max(0, $amountCents - $previousLinkedCents);
|
||||
$completed = (int) $order->fulfillment_status === 3;
|
||||
$increaseCents = 0;
|
||||
if ($completed) {
|
||||
// 补录的款项先抵销已有欠款。超出欠款的部分属于结案后的追加收款。
|
||||
$increaseCents = max(0, $receiptCents - $previousDueCents);
|
||||
$amountCents += $increaseCents;
|
||||
$order->amount = $amountCents / 100;
|
||||
|
||||
// 部分退款时 paid 可能低于关联收款原额,增量入账可保留已扣退款。
|
||||
$order->paid = (float) ($order->refund_amount ?? 0) > 0
|
||||
? min($linkedCents, (int) round((float) $order->paid * 100) + $receiptCents) / 100
|
||||
: $linkedCents / 100;
|
||||
}
|
||||
$dueCents = $completed ? max(0, $amountCents - $linkedCents) : $amountCents - $linkedCents;
|
||||
$link = new PrescriptionOrderPayOrder();
|
||||
if (!$link->save(['prescription_order_id' => $id, 'pay_order_id' => $payOrderId, 'create_time' => time()])) {
|
||||
throw new \RuntimeException('收款关联未保存');
|
||||
}
|
||||
$order->linked_pay_order_id = $previousIds[0] ?? $payOrderId;
|
||||
$order->agency_collect_amount = $dueCents / 100;
|
||||
if (!$order->save()) {
|
||||
throw new \RuntimeException('订单收款关联未保存');
|
||||
}
|
||||
$summary = sprintf('关联已支付收款 #%d(¥%.2f)', $payOrderId, $receiptCents / 100);
|
||||
if ($completed) {
|
||||
$summary .= sprintf(',订单总金额 ¥%.2f → ¥%.2f,需代收 ¥%.2f → ¥%.2f',
|
||||
($amountCents - $increaseCents) / 100, $amountCents / 100, $previousDueCents / 100, $dueCents / 100);
|
||||
}
|
||||
self::writeLog($id, $adminId, $adminInfo, 'link_pay_order', $summary, true);
|
||||
|
||||
$out = PrescriptionOrder::find($id)->toArray();
|
||||
self::maskInternalCostIfNeeded($out, $adminInfo);
|
||||
self::maskRemarkExtraIfNeeded($out, $adminInfo);
|
||||
self::attachLinkedPayOrders($out);
|
||||
return $out;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除单笔收款关联:保留原支付单、总金额与履约/审核状态,同步已付及代收金额。
|
||||
* 普通订单 paid 按剩余有效收款重算;退款订单的 paid 保留退款后的余额口径。
|
||||
@@ -3306,8 +3602,8 @@ class PrescriptionOrderLogic
|
||||
self::$error = '无权限操作此订单';
|
||||
return false;
|
||||
}
|
||||
if (in_array((int) $order->fulfillment_status, [3, 4], true)) {
|
||||
self::$error = '已完成或已取消的订单不允许移除收款关联';
|
||||
if ((int) $order->fulfillment_status === 4) {
|
||||
self::$error = '已取消的订单不允许移除收款关联';
|
||||
return false;
|
||||
}
|
||||
if (!in_array($payOrderId, self::linkedPayOrderIdList($id), true)) {
|
||||
@@ -3334,8 +3630,7 @@ class PrescriptionOrderLogic
|
||||
throw new \RuntimeException('收款关联已变化,请刷新后重试');
|
||||
}
|
||||
$remainingIds = self::linkedPayOrderIdList($id);
|
||||
$remainingPaidCents = $remainingIds === [] ? 0 : (int) round((float) Order::whereIn('id', $remainingIds)
|
||||
->whereNull('delete_time')->whereIn('status', [2, 5])->sum('amount') * 100);
|
||||
$remainingPaidCents = self::linkedPayPaidCents($remainingIds);
|
||||
$order->linked_pay_order_id = $remainingIds[0] ?? null;
|
||||
// 部分退款可能未拆分收款单金额,不能用剩余收款原额覆盖退款后的 paid 余额。
|
||||
$hasRefund = (float) ($order->refund_amount ?? 0) > 0 || (int) $order->fulfillment_status === 10;
|
||||
@@ -3344,8 +3639,11 @@ class PrescriptionOrderLogic
|
||||
: $remainingPaidCents;
|
||||
$order->paid = $newPaidCents / 100;
|
||||
// 与详情的关联已付总额口径一致;订单 amount 不变。
|
||||
$order->agency_collect_amount = round((float) $order->amount - $remainingPaidCents / 100, 2);
|
||||
$order->save();
|
||||
$dueCents = (int) round((float) $order->amount * 100) - $remainingPaidCents;
|
||||
$order->agency_collect_amount = ((int) $order->fulfillment_status === 3 ? max(0, $dueCents) : $dueCents) / 100;
|
||||
if (!$order->save()) {
|
||||
throw new \RuntimeException('订单收款关联未保存');
|
||||
}
|
||||
|
||||
self::writeLog($id, $adminId, $adminInfo, 'unlink_pay_order', sprintf(
|
||||
'移除%s收款关联 #%d(%s,¥%.2f),订单总金额 ¥%.2f 不变;已付金额(paid)¥%.2f → ¥%.2f;原收款记录保留',
|
||||
@@ -3357,6 +3655,11 @@ class PrescriptionOrderLogic
|
||||
self::maskInternalCostIfNeeded($out, $adminInfo);
|
||||
self::maskRemarkExtraIfNeeded($out, $adminInfo);
|
||||
self::attachLinkedPayOrders($out);
|
||||
if (self::canAttachReceipt($adminId, $adminInfo)) {
|
||||
$out['attachable_receipts'] = OrderLogic::listPaidOrdersForDiagnosis(
|
||||
(int) $order->diagnosis_id, $adminId, $adminInfo
|
||||
);
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
@@ -47,6 +47,7 @@ class PrescriptionOrderValidate extends BaseValidate
|
||||
'patient_name' => 'require|max:50',
|
||||
'phone' => 'require|max:20',
|
||||
'phone_tail' => 'max:20|regex:/^\\d*$/',
|
||||
'order_no' => 'alphaDash|max:50',
|
||||
];
|
||||
|
||||
protected $message = [
|
||||
@@ -73,7 +74,6 @@ class PrescriptionOrderValidate extends BaseValidate
|
||||
'is_follow_up', 'prev_staff', 'service_channel', 'service_package',
|
||||
'tracking_number', 'express_company', 'ship_mode', 'fee_type', 'amount', 'remark_extra', 'remark_assistant', 'pay_order_ids',
|
||||
],
|
||||
'detail' => ['id'],
|
||||
'editTime' => ['id', 'create_time'],
|
||||
'edit' => [
|
||||
'id', 'recipient_name', 'recipient_phone', 'shipping_address',
|
||||
@@ -106,6 +106,30 @@ class PrescriptionOrderValidate extends BaseValidate
|
||||
'confirmGancaoSubmission' => ['id', 'resolution', 'remote_order_no', 'note'],
|
||||
];
|
||||
|
||||
public function sceneAttachReceipt(): PrescriptionOrderValidate
|
||||
{
|
||||
return $this->only(['id', 'pay_order_id'])
|
||||
->append('id', 'require|integer|gt:0')
|
||||
->append('pay_order_id', 'require|integer|gt:0');
|
||||
}
|
||||
|
||||
public function sceneDetail(): PrescriptionOrderValidate
|
||||
{
|
||||
return $this->only(['id', 'prescription_id'])
|
||||
->remove('prescription_id', 'require')
|
||||
->append('prescription_id', 'gt:0');
|
||||
}
|
||||
|
||||
public function scenePrescriptionOrdersForBinding(): PrescriptionOrderValidate
|
||||
{
|
||||
return $this->only(['prescription_id', 'order_no'])->append('prescription_id', 'gt:0');
|
||||
}
|
||||
|
||||
public function sceneBindPrescription(): PrescriptionOrderValidate
|
||||
{
|
||||
return $this->only(['id', 'prescription_id'])->append('id', 'gt:0')->append('prescription_id', 'gt:0');
|
||||
}
|
||||
|
||||
public function sceneUnlinkPayOrder(): PrescriptionOrderValidate
|
||||
{
|
||||
return $this->only(['id', 'pay_order_id'])
|
||||
|
||||
Reference in New Issue
Block a user