转接方分账
This commit is contained in:
@@ -101,6 +101,16 @@ class LedgerForm extends BaseModel
|
||||
if(!empty($isLegder)){
|
||||
throw new Exception('该订单已增加待结算记录');
|
||||
}
|
||||
|
||||
// 检查处方是否来自转诊
|
||||
$isFromTransfer = false;
|
||||
if ($productOrder->prescription && $productOrder->prescription->is_from_transfer == 1) {
|
||||
$isFromTransfer = true;
|
||||
// 这里写转诊单的分账逻辑
|
||||
// 转诊单需要同时给转诊诊所和委托诊所分账
|
||||
// 具体分账比例和逻辑待后续配置
|
||||
}
|
||||
|
||||
$time = time();
|
||||
$ledgerData = [];
|
||||
|
||||
@@ -310,64 +320,208 @@ class LedgerForm extends BaseModel
|
||||
|
||||
switch ($productOrder->prescription_type) {
|
||||
case '1':case '3': //中药/颗粒药分佣
|
||||
foreach($productOrderItems as $v){
|
||||
$ledger = [];
|
||||
$number = $v['number'];
|
||||
$totalPrice = round($v['dosage'] * $number * $v['price'], 4);
|
||||
$totalBuyPrice = round($v['dosage'] * $number * $v['buy_price'], 4);
|
||||
|
||||
/*
|
||||
* 医保支付(走月付)
|
||||
*/
|
||||
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();
|
||||
// 检查是否是转诊处方(转接方只有中药处方商品)
|
||||
$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;
|
||||
}
|
||||
} else {
|
||||
$ledger[] = [
|
||||
'order_id' => $this->order_id,
|
||||
'user_id' => $store->id,
|
||||
'user_type' => 1, //门店
|
||||
'order_type' => 1,//产品订单
|
||||
'fee_type' => 1,//药品费用
|
||||
'su_id' => $productOrder->su_id,
|
||||
'drugstore_id' => $productOrder->store_id,
|
||||
'drug_id' => $v['drug_id'],
|
||||
'xd_time' => $productOrder->created_at,
|
||||
}
|
||||
}
|
||||
|
||||
// 如果是转诊处方,需要特殊处理分账逻辑
|
||||
if ($isTransferPrescription) {
|
||||
/*
|
||||
* 转诊处方分账逻辑:
|
||||
* 1. 承接方(委托诊所)分走15%发起方(转诊诊所)的利润
|
||||
* 2. 发起方获得85%的利润
|
||||
* 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,
|
||||
'money' => $totalPrice - $totalBuyPrice,
|
||||
'status' => 0,
|
||||
'created_at' => $time,
|
||||
'updated_at' => $time
|
||||
'dosage' => $dosage,
|
||||
'totalPrice' => $totalPrice,
|
||||
'totalBuyPrice' => $totalBuyPrice,
|
||||
'drug_id' => $v['drug_id'],
|
||||
];
|
||||
}
|
||||
|
||||
// 计算分账金额:从总利润中分配
|
||||
$delegateProfit = round($totalProfit * 0.15, 2); // 承接方分得15%的总利润
|
||||
$transferProfit = round($totalProfit * 0.85, 2); // 发起方分得85%的总利润
|
||||
|
||||
// 第二遍遍历:按每个药品的利润占比,分配承接方和发起方的分账金额
|
||||
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;
|
||||
$itemDelegateProfit = round($delegateProfit * $itemProfitRatio, 2); // 该药品承接方应分得的金额
|
||||
$itemTransferProfit = round($transferProfit * $itemProfitRatio, 2); // 该药品发起方应分得的金额
|
||||
|
||||
// 创建分账记录:发起方(转诊诊所)分得85%利润
|
||||
$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, // 发起方获得85%利润
|
||||
'status' => 0,
|
||||
'created_at' => $time,
|
||||
'updated_at' => $time
|
||||
];
|
||||
|
||||
// 创建分账记录:承接方(委托诊所)分得15%利润
|
||||
$ledger[] = [
|
||||
'order_id' => $this->order_id,
|
||||
'user_id' => $delegateStoreId, // 承接方诊所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' => $itemDelegateProfit, // 承接方获得15%利润
|
||||
'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 = [];
|
||||
$number = $v['number'];
|
||||
$totalPrice = round($v['dosage'] * $number * $v['price'], 4);
|
||||
$totalBuyPrice = round($v['dosage'] * $number * $v['buy_price'], 4);
|
||||
|
||||
$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' => $v['drug_id'],
|
||||
'xd_time' => $productOrder->created_at,
|
||||
'number' => $number,
|
||||
'money' => $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();
|
||||
/*
|
||||
* 医保支付(走月付)
|
||||
*/
|
||||
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 {
|
||||
$ledger[] = [
|
||||
'order_id' => $this->order_id,
|
||||
'user_id' => $store->id,
|
||||
'user_type' => 1, //门店
|
||||
'order_type' => 1,//产品订单
|
||||
'fee_type' => 1,//药品费用
|
||||
'su_id' => $productOrder->su_id,
|
||||
'drugstore_id' => $productOrder->store_id,
|
||||
'drug_id' => $v['drug_id'],
|
||||
'xd_time' => $productOrder->created_at,
|
||||
'number' => $number,
|
||||
'money' => $totalPrice - $totalBuyPrice,
|
||||
'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' => $v['drug_id'],
|
||||
'xd_time' => $productOrder->created_at,
|
||||
'number' => $number,
|
||||
'money' => $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();
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -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' => '更新时间',
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user