@@ -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 ;
}
$activePoId s = PrescriptionOrder :: whereNull ( 'delete_time' )
-> where ( 'fulfillment_status' , '<>' , 4 )
-> column ( 'id' );
if ( $activePoIds === []) {
return null ;
$link s = 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 ;
}