Merge remote-tracking branch 'origin/master'
# Conflicts: # common/forms/LedgerForm.php
This commit is contained in:
@@ -101,6 +101,7 @@ class LedgerForm extends BaseModel
|
||||
if(!empty($isLegder)){
|
||||
throw new Exception('该订单已增加待结算记录');
|
||||
}
|
||||
|
||||
$time = time();
|
||||
$ledgerData = [];
|
||||
|
||||
@@ -310,11 +311,132 @@ class LedgerForm extends BaseModel
|
||||
|
||||
switch ($productOrder->prescription_type) {
|
||||
case '1':case '3': //中药/颗粒药分佣
|
||||
// 检查是否是转诊处方(转接方只有中药处方商品)
|
||||
$isTransferPrescription = false;
|
||||
$transferPrescription = null;
|
||||
$transferStoreId = null;
|
||||
$delegateStoreId = null;
|
||||
|
||||
if ($productOrder->prescription && $productOrder->prescription->is_from_transfer == 1 && $productOrder->prescription_type == '1') {
|
||||
// 通过处方关联挂号,再通过挂号关联转诊记录
|
||||
// 处方 -> 挂号(register_id) -> 转诊记录(transfer_prescription_id)
|
||||
if ($productOrder->prescription->register && $productOrder->prescription->register->transferPrescription) {
|
||||
$transferPrescription = $productOrder->prescription->register->transferPrescription;
|
||||
|
||||
$isTransferPrescription = true;
|
||||
$transferStoreId = $transferPrescription->transfer_store_id; // 发起方(转诊诊所,西医诊所)
|
||||
$delegateStoreId = $transferPrescription->delegate_store_id; // 承接方(委托诊所,中医诊所)
|
||||
|
||||
if (!$transferStoreId || !$delegateStoreId) {
|
||||
// 如果转诊信息不完整,按普通处方处理
|
||||
$isTransferPrescription = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果是转诊处方,需要特殊处理分账逻辑
|
||||
if ($isTransferPrescription) {
|
||||
/*
|
||||
* 转诊处方分账逻辑:
|
||||
* 1. 发起方(转诊诊所)获得100%的利润
|
||||
* 2. 承接方(委托诊所)不分账
|
||||
* 3. 平台分账保持不变(成本价)
|
||||
*/
|
||||
|
||||
// 第一遍遍历:计算所有药品的总利润
|
||||
$totalProfit = 0;
|
||||
$itemProfits = [];
|
||||
$itemData = [];
|
||||
|
||||
foreach($productOrderItems as $v) {
|
||||
$number = $v['number'];
|
||||
$dosage = $v['dosage'] ?? 1;
|
||||
$totalPrice = round($dosage * $number * $v['price'], 4);
|
||||
$totalBuyPrice = round($dosage * $number * $v['buy_price'], 4);
|
||||
$profit = $totalPrice - $totalBuyPrice;
|
||||
|
||||
$totalProfit += $profit;
|
||||
$itemProfits[] = $profit;
|
||||
$itemData[] = [
|
||||
'number' => $number,
|
||||
'dosage' => $dosage,
|
||||
'totalPrice' => $totalPrice,
|
||||
'totalBuyPrice' => $totalBuyPrice,
|
||||
'drug_id' => $v['drug_id'],
|
||||
];
|
||||
}
|
||||
|
||||
// 计算分账金额:从总利润中分配
|
||||
// 转诊处方分账逻辑:全部利润(100%)分给发起方(转诊诊所),承接方不分账
|
||||
$transferProfit = round($totalProfit, 2); // 发起方分得100%的总利润(全部利润)
|
||||
|
||||
// 第二遍遍历:按每个药品的利润占比,分配发起方的分账金额
|
||||
foreach($itemData as $index => $item) {
|
||||
$ledger = [];
|
||||
|
||||
// 医保支付(走月付)
|
||||
if ($productOrder['prescription']['category'] == 2) {
|
||||
// 查询订单是否已生成月付
|
||||
$monthInfo = MonthlyPaymentModel::find()->where(['order_id' => $productOrder->id, 'store_id' => $productOrder->store_id])->one();
|
||||
if (!$monthInfo) {
|
||||
$model = new MonthlyPaymentModel();
|
||||
$model->store_id = $productOrder->store_id;
|
||||
$model->order_id = $productOrder->id;
|
||||
$model->amount = $productOrder->market_price;
|
||||
$model->created_at = $time;
|
||||
$model->save();
|
||||
}
|
||||
} else {
|
||||
// 按该药品利润占比分配总利润
|
||||
$itemProfitRatio = $totalProfit > 0 ? ($itemProfits[$index] / $totalProfit) : 0;
|
||||
$itemTransferProfit = round($transferProfit * $itemProfitRatio, 2); // 该药品发起方应分得的金额(100%利润)
|
||||
|
||||
// 创建分账记录:发起方(转诊诊所)分得100%利润
|
||||
$ledger[] = [
|
||||
'order_id' => $this->order_id,
|
||||
'user_id' => $transferStoreId, // 发起方诊所ID
|
||||
'user_type' => 1, //门店
|
||||
'order_type' => 1,//产品订单
|
||||
'fee_type' => 1,//药品费用
|
||||
'su_id' => $productOrder->su_id,
|
||||
'drugstore_id' => $productOrder->store_id,
|
||||
'drug_id' => $item['drug_id'],
|
||||
'xd_time' => $productOrder->created_at,
|
||||
'number' => $item['number'],
|
||||
'money' => $itemTransferProfit, // 发起方获得100%利润
|
||||
'status' => 0,
|
||||
'created_at' => $time,
|
||||
'updated_at' => $time
|
||||
];
|
||||
|
||||
// 创建分账记录:平台分账保持不变(成本价)
|
||||
$ledger[] = [
|
||||
'order_id' => $this->order_id,
|
||||
'user_id' => 0,
|
||||
'user_type' => 2, //平台
|
||||
'order_type' => 1,//产品订单
|
||||
'fee_type' => 1,//药品费用
|
||||
'su_id' => $productOrder->su_id,
|
||||
'drugstore_id' => $productOrder->store_id,
|
||||
'drug_id' => $item['drug_id'],
|
||||
'xd_time' => $productOrder->created_at,
|
||||
'number' => $item['number'],
|
||||
'money' => $item['totalBuyPrice'], // 平台分账保持成本价不变
|
||||
'status' => 0,
|
||||
'created_at' => $time,
|
||||
'updated_at' => $time
|
||||
];
|
||||
|
||||
\Yii::$app->db->createCommand()->batchInsert(Ledger::tableName(), [
|
||||
'order_id', 'user_id', 'user_type', 'order_type', 'fee_type', 'su_id', 'drugstore_id',
|
||||
'drug_id','xd_time', 'number', 'money', 'status', 'created_at', 'updated_at'
|
||||
], $ledger)->execute();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 非转诊处方,按原有逻辑处理
|
||||
foreach($productOrderItems as $v){
|
||||
$ledger = [];
|
||||
if ($v['dosage'] == 0) {
|
||||
$v['dosage'] = 1;
|
||||
}
|
||||
$number = $v['number'];
|
||||
$totalPrice = round($v['dosage'] * $number * $v['price'], 4);
|
||||
$totalBuyPrice = round($v['dosage'] * $number * $v['buy_price'], 4);
|
||||
@@ -373,6 +495,7 @@ class LedgerForm extends BaseModel
|
||||
], $ledger)->execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case '2': //西药分佣
|
||||
$ledger = [];
|
||||
|
||||
@@ -70,6 +70,12 @@ class Prescription extends \common\modelsgii\oldDb\Prescription
|
||||
return $this->hasOne(Register::class, ['id' => 'register_id']);
|
||||
}
|
||||
|
||||
//转诊记录
|
||||
public function getTransferPrescription()
|
||||
{
|
||||
return $this->hasOne(TransferPrescription::class, ['id' => 'transfer_prescription_id']);
|
||||
}
|
||||
|
||||
//导出处方
|
||||
public static function inventory($params,$store_id)
|
||||
{
|
||||
|
||||
@@ -57,6 +57,11 @@ class Register extends \common\modelsgii\oldDb\Register
|
||||
public function getPrescription(){
|
||||
return $this->hasMany(Prescription::class, ['register_id' => 'id']);
|
||||
}
|
||||
//转诊记录
|
||||
public function getTransferPrescription()
|
||||
{
|
||||
return $this->hasOne(TransferPrescription::class, ['id' => 'transfer_prescription_id']);
|
||||
}
|
||||
//健康信息
|
||||
public function getHealthInquery()
|
||||
{
|
||||
|
||||
34
common/models/oldDb/TransferPrescription.php
Normal file
34
common/models/oldDb/TransferPrescription.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace common\models\oldDb;
|
||||
|
||||
/**
|
||||
* 转诊记录模型
|
||||
* 存储西医诊所开具的中药处方转诊信息
|
||||
*/
|
||||
class TransferPrescription extends \common\modelsgii\oldDb\TransferPrescription
|
||||
{
|
||||
/**
|
||||
* 关联原处方
|
||||
*/
|
||||
public function getOriginalPrescription()
|
||||
{
|
||||
return $this->hasOne(Prescription::class, ['id' => 'original_prescription_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 关联转诊诊所(西医诊所)
|
||||
*/
|
||||
public function getTransferStore()
|
||||
{
|
||||
return $this->hasOne(Store::class, ['id' => 'transfer_store_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 关联委托诊所(中医诊所)
|
||||
*/
|
||||
public function getDelegateStore()
|
||||
{
|
||||
return $this->hasOne(Store::class, ['id' => 'delegate_store_id']);
|
||||
}
|
||||
}
|
||||
65
common/modelsgii/oldDb/TransferPrescription.php
Normal file
65
common/modelsgii/oldDb/TransferPrescription.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace common\modelsgii\oldDb;
|
||||
|
||||
/**
|
||||
* This is the model class for table "yii_transfer_prescription".
|
||||
*
|
||||
* @property int $id 主键
|
||||
* @property int $original_prescription_id 原处方ID(关联yii_prescription)
|
||||
* @property int $transfer_store_id 转诊诊所ID(西医诊所)
|
||||
* @property int $delegate_store_id 委托诊所ID(中医诊所)
|
||||
* @property int $online_consultation_doctor_id 在线问诊医生ID
|
||||
* @property int $user_id 用户ID(关联用户表)
|
||||
* @property string $prescription_no 原处方编号
|
||||
* @property int $prescription_type 处方类型
|
||||
* @property string $content 处方内容(JSON,完整复制原处方)
|
||||
* @property int $status 转诊状态 0=待确认, 1=已确认, 2=已导入, 3=已取消
|
||||
* @property int $transfer_time 转诊时间
|
||||
* @property int $created_at 创建时间
|
||||
* @property int $updated_at 更新时间
|
||||
*/
|
||||
class TransferPrescription extends \common\core\BaseActiveRecord
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function tableName()
|
||||
{
|
||||
return 'yii_transfer_prescription';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['original_prescription_id', 'transfer_store_id', 'delegate_store_id', 'online_consultation_doctor_id', 'user_id', 'prescription_type', 'status', 'transfer_time', 'created_at', 'updated_at'], 'integer'],
|
||||
[['content'], 'string'],
|
||||
[['prescription_no'], 'string', 'max' => 255],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
'id' => 'ID',
|
||||
'original_prescription_id' => '原处方ID',
|
||||
'transfer_store_id' => '转诊诊所ID',
|
||||
'delegate_store_id' => '委托诊所ID',
|
||||
'online_consultation_doctor_id' => '在线问诊医生ID',
|
||||
'user_id' => '用户ID',
|
||||
'prescription_no' => '处方编号',
|
||||
'prescription_type' => '处方类型',
|
||||
'content' => '处方内容',
|
||||
'status' => '转诊状态',
|
||||
'transfer_time' => '转诊时间',
|
||||
'created_at' => '创建时间',
|
||||
'updated_at' => '更新时间',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -177,10 +177,17 @@ class ProductOrderController extends BaseAppController
|
||||
$h->select('name');
|
||||
}])->asArray()->one();
|
||||
|
||||
$prescription = Prescription::find()->select('cr_ids,wr_ids,gr_ids,category')->where(['id' => $ProductOrder['p_id']])->one();
|
||||
$prescription = Prescription::find()->select('cr_ids,wr_ids,gr_ids,category,store_id')->where(['id' => $ProductOrder['p_id']])->one();
|
||||
|
||||
if($prescription['wr_ids']){
|
||||
$wr_ids = explode(',', $prescription['wr_ids']);
|
||||
// 获取处方来源诊所信息
|
||||
$prescriptionStore = null;
|
||||
if ($prescription && $prescription->store_id) {
|
||||
$prescriptionStore = Store::find()->select('id,name,mobile,position')->where(['id' => $prescription->store_id])->asArray()->one();
|
||||
}
|
||||
|
||||
$count = 0;
|
||||
if($prescription && $prescription->wr_ids){
|
||||
$wr_ids = explode(',', $prescription->wr_ids);
|
||||
if ($ProductOrder['prescription_type'] == 5) {
|
||||
$west = ServicePackageRepice::find()->select('content,number,total_price')->where([
|
||||
'in', 'id', $wr_ids
|
||||
@@ -194,8 +201,8 @@ class ProductOrderController extends BaseAppController
|
||||
$value['content']=Json::decode($value['content']);
|
||||
}
|
||||
$count = count($west);
|
||||
}else if($prescription['cr_ids']){
|
||||
$cr_ids = explode(',', $prescription['cr_ids']);
|
||||
}else if($prescription && $prescription->cr_ids){
|
||||
$cr_ids = explode(',', $prescription->cr_ids);
|
||||
$chinese = ChineseRepice::find()->select('content,total_price')->where([
|
||||
'in', 'id', $cr_ids
|
||||
])->all();
|
||||
@@ -206,8 +213,8 @@ class ProductOrderController extends BaseAppController
|
||||
}
|
||||
}
|
||||
$count = count($chinese['list']);
|
||||
}else{
|
||||
$gr_ids = explode(',', $prescription['gr_ids']);
|
||||
}else if($prescription && $prescription->gr_ids){
|
||||
$gr_ids = explode(',', $prescription->gr_ids);
|
||||
$granular = GranularRepice::find()->select('content,total_price')->where([
|
||||
'in', 'id', $gr_ids
|
||||
])->all();
|
||||
@@ -220,7 +227,7 @@ class ProductOrderController extends BaseAppController
|
||||
$count = count($granular['list']);
|
||||
}
|
||||
if ($ProductOrder['p_id'] !== 0 && $ProductOrder['order_type'] !== 2) {
|
||||
if ($prescription['category'] === 2) {
|
||||
if ($prescription && $prescription->category === 2) {
|
||||
$ProductOrder['total_pay_price'] = $ProductOrder['trans_expenses'];
|
||||
$ProductOrder['items_price'] = 0;
|
||||
}
|
||||
@@ -234,6 +241,7 @@ class ProductOrderController extends BaseAppController
|
||||
'west' => $west ?? null,
|
||||
'chinese' => $chinese ?? null,
|
||||
'granular' => $granular ?? null,
|
||||
'store' => $prescriptionStore ? ['store' => $prescriptionStore] : null,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -426,11 +434,22 @@ class ProductOrderController extends BaseAppController
|
||||
$q->select('id,drug_name,function,specification,usage,instruction');
|
||||
}])->asArray()->all();
|
||||
$ProductOrder['count'] = count($ProductOrderItems);
|
||||
|
||||
// 获取处方来源诊所信息
|
||||
$prescriptionStore = null;
|
||||
if ($ProductOrder['p_id']) {
|
||||
$prescription = Prescription::find()->select('store_id')->where(['id' => $ProductOrder['p_id']])->asArray()->one();
|
||||
if ($prescription && isset($prescription['store_id']) && $prescription['store_id']) {
|
||||
$prescriptionStore = Store::find()->select('id,name,mobile,position')->where(['id' => $prescription['store_id']])->asArray()->one();
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'ProductOrder' => $ProductOrder,
|
||||
'userInfo' => $userInfo,
|
||||
'hospital' => $hospital,
|
||||
'ProductOrderItems' => $ProductOrderItems
|
||||
'ProductOrderItems' => $ProductOrderItems,
|
||||
'store' => $prescriptionStore ? ['store' => $prescriptionStore] : null,
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
@@ -56,6 +56,8 @@ class ErpController extends Controller
|
||||
//解析成erp所需参数(订单详情是否需要改动 - 线上订单的展示)
|
||||
$erpData = [];
|
||||
$ids = [];
|
||||
|
||||
$defaultUserId = '41677';
|
||||
foreach($productOrders as $k=>$v){
|
||||
// 如果是商城订单
|
||||
if ($v['order_type'] == 2) {
|
||||
@@ -64,7 +66,7 @@ class ErpController extends Controller
|
||||
$erpData[$k]['createTime'] = date('Y-m-d H:i:s', $v['created_at']);
|
||||
// $erpData[$k]['userId'] = $v['store']['erp_id'];
|
||||
// 商城订单暂时都用11007诊所来接收订单
|
||||
$erpData[$k]['userId'] = '41677';
|
||||
$erpData[$k]['userId'] = $defaultUserId;
|
||||
$erpData[$k]['orderStatus'] = $v['status'];
|
||||
$erpData[$k]['orderType'] = $v['type'];
|
||||
$erpData[$k]['totalAmount'] = $v['total_pay_price'];
|
||||
@@ -115,7 +117,12 @@ class ErpController extends Controller
|
||||
if ($v['prescription']['status'] == 1) {
|
||||
$erpData[$k]['orderId'] = $v['order_no'];
|
||||
$erpData[$k]['createTime'] = date('Y-m-d H:i:s', $v['created_at']);
|
||||
// 如果是商城处方订单
|
||||
if ($v['is_online'] == 3) {
|
||||
$erpData[$k]['userId'] = $defaultUserId;
|
||||
} else {
|
||||
$erpData[$k]['userId'] = $v['store']['erp_id'];
|
||||
}
|
||||
$erpData[$k]['orderStatus'] = $v['status'];
|
||||
$erpData[$k]['totalAmount'] = $v['total_pay_price'];
|
||||
// $erpData[$k]['deliveryAmount'] = $v['trans_expenses'];
|
||||
|
||||
@@ -206,6 +206,21 @@ class LoginForm extends BaseModel
|
||||
'su_id' => $ServiceUser->id,
|
||||
])->with(['serviceUser'])
|
||||
->asArray()->one();
|
||||
|
||||
// 把ServiceUser信息存在Redis里面(使用DB 1,key前缀加上doctor_wx区分)
|
||||
$ServiceUserData = [
|
||||
'user' => [
|
||||
'id' => $ServiceUser->id,
|
||||
'mobile' => $ServiceUser->mobile ?? '',
|
||||
'nickname' => $ServiceUser->nickname ?? '微信用户',
|
||||
'role' => $ServiceUser->role,
|
||||
'status' => $ServiceUser->status,
|
||||
'plate_type' => $ServiceUser->plate_type,
|
||||
]
|
||||
];
|
||||
\Yii::$app->redis->select(1);
|
||||
\Yii::$app->redis->set('xiaokang_database_xk_user_doctor_wx_' . $token, json_encode($ServiceUserData));
|
||||
|
||||
return [
|
||||
'token'=>$token,
|
||||
'data'=>$StoreDoctor
|
||||
@@ -256,6 +271,21 @@ class LoginForm extends BaseModel
|
||||
$StoreDoctor = StoreDoctor::find()->where([
|
||||
'su_id' => $ServiceUser->id,
|
||||
])->with(['serviceUser'])->asArray()->one();
|
||||
|
||||
// 把ServiceUser信息存在Redis里面(使用DB 1,key前缀加上doctor_wx区分)
|
||||
$ServiceUserData = [
|
||||
'user' => [
|
||||
'id' => $ServiceUser->id,
|
||||
'mobile' => $ServiceUser->mobile ?? '',
|
||||
'nickname' => $ServiceUser->nickname ?? '微信用户',
|
||||
'role' => $ServiceUser->role,
|
||||
'status' => $ServiceUser->status,
|
||||
'plate_type' => $ServiceUser->plate_type,
|
||||
]
|
||||
];
|
||||
\Yii::$app->redis->select(1);
|
||||
\Yii::$app->redis->set('xiaokang_database_xk_user_doctor_wx_' . $token, json_encode($ServiceUserData));
|
||||
|
||||
return [
|
||||
'token'=>$token,
|
||||
'data'=>$StoreDoctor,
|
||||
@@ -321,6 +351,21 @@ class LoginForm extends BaseModel
|
||||
}
|
||||
|
||||
$t->commit();
|
||||
|
||||
// 把ServiceUser信息存在Redis里面(使用DB 1,key前缀加上doctor_wx区分)
|
||||
$ServiceUserData = [
|
||||
'user' => [
|
||||
'id' => $ServiceUser->id,
|
||||
'mobile' => $ServiceUser->mobile ?? '',
|
||||
'nickname' => $ServiceUser->nickname ?? '微信用户',
|
||||
'role' => $ServiceUser->role,
|
||||
'status' => $ServiceUser->status,
|
||||
'plate_type' => $ServiceUser->plate_type,
|
||||
]
|
||||
];
|
||||
\Yii::$app->redis->select(1);
|
||||
\Yii::$app->redis->set('xiaokang_database_xk_user_doctor_wx_' . $token, json_encode($ServiceUserData));
|
||||
|
||||
return [
|
||||
'token'=>$token,
|
||||
'StoreDoctor'=>$StoreDoctor,
|
||||
|
||||
Reference in New Issue
Block a user