分账剂数还原
This commit is contained in:
@@ -101,6 +101,7 @@ class LedgerForm extends BaseModel
|
|||||||
if(!empty($isLegder)){
|
if(!empty($isLegder)){
|
||||||
throw new Exception('该订单已增加待结算记录');
|
throw new Exception('该订单已增加待结算记录');
|
||||||
}
|
}
|
||||||
|
|
||||||
$time = time();
|
$time = time();
|
||||||
$ledgerData = [];
|
$ledgerData = [];
|
||||||
|
|
||||||
@@ -310,64 +311,189 @@ class LedgerForm extends BaseModel
|
|||||||
|
|
||||||
switch ($productOrder->prescription_type) {
|
switch ($productOrder->prescription_type) {
|
||||||
case '1':case '3': //中药/颗粒药分佣
|
case '1':case '3': //中药/颗粒药分佣
|
||||||
foreach($productOrderItems as $v){
|
// 检查是否是转诊处方(转接方只有中药处方商品)
|
||||||
$ledger = [];
|
$isTransferPrescription = false;
|
||||||
$number = $v['number'];
|
$transferPrescription = null;
|
||||||
$totalPrice = round($v['dosage'] * $number * $v['price'], 4);
|
$transferStoreId = null;
|
||||||
$totalBuyPrice = round($v['dosage'] * $number * $v['buy_price'], 4);
|
$delegateStoreId = null;
|
||||||
|
|
||||||
/*
|
if ($productOrder->prescription && $productOrder->prescription->is_from_transfer == 1 && $productOrder->prescription_type == '1') {
|
||||||
* 医保支付(走月付)
|
// 通过处方关联挂号,再通过挂号关联转诊记录
|
||||||
*/
|
// 处方 -> 挂号(register_id) -> 转诊记录(transfer_prescription_id)
|
||||||
if ($productOrder['prescription']['category'] == 2) {
|
if ($productOrder->prescription->register && $productOrder->prescription->register->transferPrescription) {
|
||||||
// 查询订单是否已生成月付
|
$transferPrescription = $productOrder->prescription->register->transferPrescription;
|
||||||
$monthInfo = MonthlyPaymentModel::find()->where(['order_id' => $productOrder->id, 'store_id' => $productOrder->store_id])->one();
|
|
||||||
if (!$monthInfo) {
|
$isTransferPrescription = true;
|
||||||
$model = new MonthlyPaymentModel();
|
$transferStoreId = $transferPrescription->transfer_store_id; // 发起方(转诊诊所,西医诊所)
|
||||||
$model->store_id = $productOrder->store_id;
|
$delegateStoreId = $transferPrescription->delegate_store_id; // 承接方(委托诊所,中医诊所)
|
||||||
$model->order_id = $productOrder->id;
|
|
||||||
$model->amount = $productOrder->market_price;
|
if (!$transferStoreId || !$delegateStoreId) {
|
||||||
$model->created_at = $time;
|
// 如果转诊信息不完整,按普通处方处理
|
||||||
$model->save();
|
$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,
|
|
||||||
'number' => $number,
|
|
||||||
'money' => $totalPrice - $totalBuyPrice,
|
|
||||||
'status' => 0,
|
|
||||||
'created_at' => $time,
|
|
||||||
'updated_at' => $time
|
|
||||||
];
|
|
||||||
|
|
||||||
$ledger[] = [
|
// 如果是转诊处方,需要特殊处理分账逻辑
|
||||||
'order_id' => $this->order_id,
|
if ($isTransferPrescription) {
|
||||||
'user_id' => 0,
|
/*
|
||||||
'user_type' => 2, //平台
|
* 转诊处方分账逻辑:
|
||||||
'order_type' => 1,//产品订单
|
* 1. 发起方(转诊诊所)获得100%的利润
|
||||||
'fee_type' => 1,//药品费用
|
* 2. 承接方(委托诊所)不分账
|
||||||
'su_id' => $productOrder->su_id,
|
* 3. 平台分账保持不变(成本价)
|
||||||
'drugstore_id' => $productOrder->store_id,
|
*/
|
||||||
'drug_id' => $v['drug_id'],
|
|
||||||
'xd_time' => $productOrder->created_at,
|
// 第一遍遍历:计算所有药品的总利润
|
||||||
|
$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,
|
'number' => $number,
|
||||||
'money' => $totalBuyPrice,
|
'dosage' => $dosage,
|
||||||
'status' => 0,
|
'totalPrice' => $totalPrice,
|
||||||
'created_at' => $time,
|
'totalBuyPrice' => $totalBuyPrice,
|
||||||
'updated_at' => $time
|
'drug_id' => $v['drug_id'],
|
||||||
];
|
];
|
||||||
\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();
|
// 转诊处方分账逻辑:全部利润(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 = [];
|
||||||
|
$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();
|
||||||
|
}
|
||||||
|
} 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;
|
break;
|
||||||
|
|||||||
Reference in New Issue
Block a user