Files
xk-api-yii/common/forms/LedgerForm.php
2026-04-30 13:08:20 +08:00

1007 lines
60 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace common\forms;
use common\core\BaseModel;
use common\models\newDb\AccountBalanceModel;
use common\models\newDb\MonthlyPaymentModel;
use common\models\newDb\RegisterInfoModel;
use common\models\oldDb\CashAccount;
use common\models\oldDb\Ledger;
use common\models\oldDb\LedgerLog;
use common\models\oldDb\ProductOrder;
use common\models\oldDb\ProductOrderItems;
use common\models\oldDb\ProductOrderLog;
use common\models\oldDb\Reconciliation;
use common\models\oldDb\Register;
use common\models\oldDb\RegisterLog;
use common\models\oldDb\Store;
use yii\db\Exception;
class LedgerForm extends BaseModel
{
public $order_id;
public $status;
public function rules()
{
return [
[['order_id'],'required'],
[['status'],'in','range' => [1,2]]
];
}
/**
* 订单分账
* @param $type
* @return void
* @throws Exception
*/
public function create($type = 'productorder'){
switch ($type) {
case 'register':
RegisterLog::saveLog($this->order_id, '挂号订单增加待结算记录开始');
$t = \Yii::$app->db->beginTransaction();
try {
$register = Register::find()->where(['id' => $this->order_id])->one();
if(!$register){
throw new Exception('挂号订单不存在');
}
$store = Store::findOne(['id' => $register->store_id]);
if(!$store){
throw new Exception('门店不存在');
}
$time = time();
$ledger[] = [
'order_id' => $this->order_id,
'user_id' => $store->id,
'user_type' => 1, //诊所
'order_type' => 2,//挂号订单
'fee_type' => 2,//挂号费用
'su_id' => $register->service_user_id,
'drugstore_id' => 0,
'drug_id' =>0,
'xd_time' => strtotime($register->created_at),
'number' => 1,
'money' => $register->price,
'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();
$t->commit();
} catch (\Exception $e) {
$t->rollBack();
RegisterLog::saveLog($this->order_id, '挂号订单增加待结算记录异常:'.$e->getMessage());
throw $e;
}
break;
default: //产品订单
ProductOrderLog::saveLog($this->order_id, '产品订单增加待结算记录开始');
$t = \Yii::$app->db->beginTransaction();
try {
$productOrder = ProductOrder::find()->with(['prescription'])->where(['id' => $this->order_id])->one();
if(!$productOrder){
throw new Exception('产品订单不存在');
}
$store = Store::findOne(['id' => $productOrder->store_id]);
if(!$store){
throw new Exception('仓库不存在');
}
$isLegder = Ledger::find()->where(['order_id' => $this->order_id])->andWhere(['order_type' => 1])->one();
if(!empty($isLegder)){
throw new Exception('该订单已增加待结算记录');
}
$time = time();
$ledgerData = [];
if ($productOrder->order_type === 2) {
if($productOrder->decoct_price > 0){
$ledgerData[] = [
'order_id' => $this->order_id,
'user_id' => 0,
'user_type' => 2, //平台
'order_type' => 1,//产品订单
'fee_type' => 4,//代煎费用
'su_id' => $productOrder->su_id,
'drugstore_id' => $productOrder->store_id,
'drug_id' => 0,
'xd_time' => $productOrder->created_at,
'number' => 1,
'money' => $productOrder->decoct_price,
'status' => 0,
'created_at' => $time,
'updated_at' => $time
];
}
if($productOrder->process_price > 0){
$ledgerData[] = [
'order_id' => $this->order_id,
'user_id' => 0,
'user_type' => 3, //供应商
'order_type' => 1,//产品订单
'fee_type' => 5,//加工费用
'su_id' => $productOrder->su_id,
'drugstore_id' => $productOrder->store_id,
'drug_id' => 0,
'xd_time' => $productOrder->created_at,
'number' => 1,
'money' => $productOrder->process_price,
'status' => 0,
'created_at' => $time,
'updated_at' => $time
];
}
if($productOrder->treatement_price > 0){
$ledgerData[] = [
'order_id' => $this->order_id,
'user_id' => $store->id,
'user_type' => 1, //门店
'order_type' => 1,//产品订单
'fee_type' => 6,//诊疗费用
'su_id' => $productOrder->su_id,
'drugstore_id' => $productOrder->store_id,
'drug_id' => 0,
'xd_time' => $productOrder->created_at,
'number' => 1,
'money' => $productOrder->treatement_price,
'status' => 0,
'created_at' => $time,
'updated_at' => $time
];
}
if($productOrder->trans_expenses > 0){
$ledgerData[] = [
'order_id' => $this->order_id,
'user_id' => 0,
'user_type' => 2, //平台
'order_type' => 1,//产品订单
'fee_type' => 3,//快递费用
'su_id' => $productOrder->su_id,
'drugstore_id' => $productOrder->store_id,
'drug_id' => 0,
'xd_time' => $productOrder->created_at,
'number' => 1,
'money' => $productOrder->trans_expenses,
'status' => 0,
'created_at' => $time,
'updated_at' => $time
];
}
if(!empty($ledgerData)){
\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'
], $ledgerData)->execute();
}
$productOrderItems = ProductOrderItems::find()->where(['product_order_id' => $productOrder->id])->all();
$ledger = [];
foreach($productOrderItems as $v){
$totalPrice = round($v['number'] * $v['price'], 4);
$totalBuyPrice = round($v['number'] * $v['buy_price'], 4);
$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,
'xd_time' => $productOrder->created_at,
'drug_id' => $v['drug_id'],
'number' => $v['number'],
'money' => bcsub($totalPrice, $totalBuyPrice, 2),
'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' => $v['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();
}
} else {
if($productOrder->decoct_price > 0){
$ledgerData[] = [
'order_id' => $this->order_id,
'user_id' => 0,
'user_type' => 2, //平台
'order_type' => 1,//产品订单
'fee_type' => 4,//代煎费用
'su_id' => $productOrder->su_id,
'drugstore_id' => $productOrder->store_id,
'drug_id' => 0,
'xd_time' => $productOrder->created_at,
'number' => 1,
'money' => $productOrder->decoct_price,
'status' => 0,
'created_at' => $time,
'updated_at' => $time
];
}
if($productOrder->process_price > 0){
$ledgerData[] = [
'order_id' => $this->order_id,
'user_id' => 0,
'user_type' => 2, //供应商
'order_type' => 1,//产品订单
'fee_type' => 5,//加工费用
'su_id' => $productOrder->su_id,
'drugstore_id' => $productOrder->store_id,
'drug_id' => 0,
'xd_time' => $productOrder->created_at,
'number' => 1,
'money' => $productOrder->process_price,
'status' => 0,
'created_at' => $time,
'updated_at' => $time
];
}
if($productOrder->treatement_price > 0){
$ledgerData[] = [
'order_id' => $this->order_id,
'user_id' => $store->id,
'user_type' => 1, //门店
'order_type' => 1,//产品订单
'fee_type' => 6,//诊疗费用
'su_id' => $productOrder->su_id,
'drugstore_id' => $productOrder->store_id,
'drug_id' => 0,
'xd_time' => $productOrder->created_at,
'number' => 1,
'money' => $productOrder->treatement_price,
'status' => 0,
'created_at' => $time,
'updated_at' => $time
];
}
if($productOrder->trans_expenses > 0){
$ledgerData[] = [
'order_id' => $this->order_id,
'user_id' => 0,
'user_type' => 2, //平台
'order_type' => 1,//产品订单
'fee_type' => 3,//快递费用
'su_id' => $productOrder->su_id,
'drugstore_id' => $productOrder->store_id,
'drug_id' => 0,
'xd_time' => $productOrder->created_at,
'number' => 1,
'money' => $productOrder->trans_expenses,
'status' => 0,
'created_at' => $time,
'updated_at' => $time
];
}
if(!empty($ledgerData)){
\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'
], $ledgerData)->execute();
}
$productOrderItems = ProductOrderItems::find()->where(['product_order_id' => $productOrder->id])->all();
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 = [];
$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;
case '2': //西药分佣
$ledger = [];
foreach($productOrderItems as $v){
$totalPrice = round($v['number'] * $v['price'], 4);
$totalBuyPrice = round($v['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,
'xd_time' => $productOrder->created_at,
'drug_id' => $v['drug_id'],
'number' => $v['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' => $v['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;
case '5':
//产品服务包分成
foreach ($productOrderItems as $v) {
$ledger = [];
$totalPrice = bcmul($v['number'], $v['price'], 4);
$platformMoney = bcmul($totalPrice, $v['drug']['platform_ledger'] / 100, 4);
$storeMoney = bcmul($totalPrice, $v['drug']['store_ledger'] / 100, 4);
$supplierMoney = bcmul($totalPrice, $v['drug']['supplier_ledger'] / 100, 4);
$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,
'xd_time' => $productOrder->created_at,
'drug_id' => $v['drug_id'],
'number' => $v['number'],
'money' => $storeMoney,
'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' => $v['number'],
'money' => $platformMoney,
'status' => 0,
'created_at' => $time,
'updated_at' => $time
];
$ledger[] = [
'order_id' => $this->order_id,
'user_id' => $v['drug']['supplier']['id'],
'user_type' => 3, //平台
'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' => $v['number'],
'money' => $supplierMoney,
'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;
default:
throw new Exception('错误的处方类型');
break;
}
}
$t->commit();
$ledgerForm = new LedgerForm();
$ledgerForm->order_id = $this->order_id;
$ledgerForm->status = 1; // 已结算
$ledgerForm->update();
ProductOrderLog::saveLog($this->order_id, '产品订单增加待结算记录结束');
} catch (\Exception $e) {
$t->rollBack();
ProductOrderLog::saveLog($this->order_id, '产品订单增加待结算记录异常:'.$e->getMessage().'——'.$e->getLine());
throw $e;
}
break;
}
}
//更新记录状态 1已结算 2已取消
public function update($type = 'productorder'){
switch ($type) {
case 'register': //挂号订单
RegisterLog::saveLog($this->order_id, '挂号订单结算更新开始');
$Register = Register::find()->where(['id' => $this->order_id])->one();
if(!$Register){
throw new Exception('产品订单不存在');
}
$store = Store::findOne(['id' => $Register->store_id]);
if(!$store){
throw new Exception('门店不存在');
}
$time = time();
$t = \Yii::$app->db->beginTransaction();
$model = new LedgerLog();
try {
switch ($this->status) {
case 1: // 已结算
//更新挂号订单为已结算
$Register->is_settled = 1;
$Register->created_at = strtotime($Register->created_at);
$Register->updated_at = $time;
$Register->saveOrFail();
//分账表批量更新状态为已结算
Ledger::updateAll(['status' => 1, 'updated_at' => $time], ['order_id' => $this->order_id, 'order_type' => 2]); //1产品订单 2挂号订单
/******************************************** 门店 ********************************************** */
$storeTotal = Ledger::find()->where(['order_id' => $this->order_id, 'user_id' => $store->id, 'user_type' => 1, 'order_type' => 2, 'fee_type' => 2])->sum('money'); // 该订单门店分佣总金额
$storeTotal = round($storeTotal, 2);//4舍5入
//增加门店分佣
$log = clone $model;
$log->order_id = $this->order_id;
$log->user_id = $store->id;
$log->user_type = 1;// 门店
$log->order_type = 2; //挂号订单
$log->fee_type = 2; //挂号费用
$log->type = 1;// 增加
$log->amount = $storeTotal;
$log->content = '挂号订单结算后增加挂号分佣金额';
$log->created_at = $time;
$log->updated_at = $time;
$log->saveOrFail();
//门店可提现及累计金额更新
CashAccount::updateAllCounters(['able_cash' => $storeTotal, 'total_cash' => $storeTotal],['user_type' => 1, 'user_id' => $store->id]); //门店
break;
case 2://已取消
if($Register->is_settled == 1){ //已结算回退
//已结算对账单状态更新
Reconciliation::updateAll(['status' => -1], ['order_id' => $this->order_id]);
//分账表批量更新状态为已取消
Ledger::updateAll(['status' => 2, 'updated_at' => $time], ['order_id' => $this->order_id, 'order_type' => 1]);//1产品订单 2挂号订单
$ledgerLog = LedgerLog::find()->where(['order_id' => $this->order_id])->asArray()->all();
foreach($ledgerLog as $v){
$log = clone $model;
$log->order_id = $v['order_id'];
$log->user_id = $v['user_id'];
$log->user_type = $v['user_type'];
$log->order_type = $v['order_type'];
$log->fee_type = $v['fee_type'];
$log->type = 2;// 扣减
$log->amount = $v['amount'];
$log->content = '订单结算后用户退货退款,扣减金额';
$log->created_at = $time;
$log->updated_at = $time;
$log->saveOrFail();
//可提现及累计金额更新
CashAccount::updateAllCounters(['able_cash' => $v['amount'] * -1, 'total_cash' => $v['amount'] * -1],['user_type' => $v['user_type'], 'user_id' => $v['user_id'], 'order_type' => $v['order_type'], 'fee_type' => $v['fee_type']]);
}
} else { // 未结算
Ledger::updateAll(['status' => 2, 'updated_at' => $time], ['order_id' => $this->order_id, 'order_type' => 2]);
}
break;
default:
# code...
break;
}
$t->commit();
ProductOrderLog::saveLog($this->order_id, '挂号订单结算更新结束');
} catch (\Exception $e){
$t->rollBack();
ProductOrderLog::saveLog($this->order_id, '挂号订单结算更新异常:'.$e->getMessage().'——'.$e->getLine());
throw $e;
}
break;
default: //产品订单
ProductOrderLog::saveLog($this->order_id, '产品订单结算更新开始');
$productOrder = ProductOrder::find()->where(['id' => $this->order_id])->one();
if(!$productOrder){
throw new Exception('产品订单不存在');
}
$store = Store::findOne(['id' => $productOrder->store_id]);
if(!$store){
throw new Exception('门店不存在');
}
$time = time();
$t = \Yii::$app->db->beginTransaction();
$model = new LedgerLog();
try {
switch ($this->status) {
case 1: // 已结算
//更新产品订单为已结算
$productOrder->is_settled = 1;
$productOrder->saveOrFail();
$productOrderItems = ProductOrderItems::find()->with(['drug.supplier'])->where(['product_order_id' => $productOrder->id])->asArray()->all();
$Reconciliation = new Reconciliation();
foreach($productOrderItems as $v){
$number = $v['dosage'] === 0 ? $v['number'] : $v['number'] * $v['dosage'];
$item = clone $Reconciliation;
$item->store_id = $productOrder->store_id;
$item->order_id = $productOrder->id;
$item->pay_time = $productOrder->pay_time;
$item->xd_time = $productOrder->created_at;
$item->drug_id = $v['drug_id'];
$item->drug_type = $v['type'];
$item->number = $number;
// $item->total_buy_price = $v['number'] * $v['buy_price'];
$item->total_buy_price = bcmul($number, $v['buy_price'], 6);
// $item->total_price = $v['number'] * $v['price'];
$item->total_price = bcmul($number, $v['price'], 6);
$item->created_at = $time;
$item->updated_at = $time;
$item->saveOrFail();
/******************************************** 供应商 ********************************************** */
$storeTotal = Ledger::find()->where([
'order_id' => $this->order_id,
'user_id' => $v['drug']['source'],
'user_type' => 3,
'order_type' => 1,
'fee_type' => 1
])->sum('money'); // 该订单供应商分佣总金额
$storeTotal = round($storeTotal, 6);//4舍5入
if ($storeTotal > 0) {
//增加供应商分佣
$log = clone $model;
$log->order_id = $this->order_id;
$log->user_id = $v['drug']['source'];
$log->user_type = 3;// 供应商
$log->order_type = 1; //产品订单
$log->fee_type = 1; //药品费用
$log->type = 1;// 增加
$log->amount = $storeTotal;
$log->content = '订单结算后增加供应商佣金额';
$log->created_at = $time;
$log->updated_at = $time;
$log->saveOrFail();
//供应商冻结金额及累计金额更新
AccountBalanceModel::updateAllCounters([
'total' => $storeTotal,
'frozen' => $storeTotal,
], [
'user_id' => $v['drug']['source'],
'type' => 3
]);
}
}
//分账表批量更新状态为已结算
Ledger::updateAll(['status' => 1, 'updated_at' => $time], ['order_id' => $this->order_id, 'order_type' => 1]); //1产品订单 2挂号订单
/******************************************** 平台 ********************************************** */
$platformDrugTotal = Ledger::find()->where(['order_id' => $this->order_id, 'user_type' => 2, 'order_type' => 1, 'fee_type' => 1])->sum('money'); // 该订单平台分佣总金额
//增加药品分佣
$log = clone $model;
$log->order_id = $this->order_id;
$log->user_id = 0;
$log->user_type = 2;// 平台
$log->order_type = 1; //产品订单
$log->fee_type = 1; //药品费用
$log->type = 1;// 增加
$log->amount = round($platformDrugTotal, 2);
$log->content = '订单结算后增加药品分佣金额';
$log->created_at = $time;
$log->updated_at = $time;
$log->saveOrFail();
$platformDecoctTotal = 0;
//增加代煎费
if($productOrder->decoct_price > 0){
$platformDecoctTotal = Ledger::find()->where(['order_id' => $this->order_id, 'user_type' => 2, 'order_type' => 1, 'fee_type' => 4])->sum('money'); // 该订单代煎费用
$logDecoct = clone $model;
$logDecoct->order_id = $this->order_id;
$logDecoct->user_id = 0;
$logDecoct->user_type = 2;// 平台
$logDecoct->order_type = 1; //产品订单
$logDecoct->fee_type = 4; //代煎费用
$logDecoct->type = 1;// 增加
$logDecoct->amount = round($platformDecoctTotal, 2);
$logDecoct->content = '订单结算后增加代煎费用';
$logDecoct->created_at = $time;
$logDecoct->updated_at = $time;
$logDecoct->saveOrFail();
}
$platformProcessTotal = 0;
//增加加工费
if($productOrder->process_price > 0){
$platformProcessTotal = Ledger::find()->where(['order_id' => $this->order_id, 'user_type' => 2, 'order_type' => 1, 'fee_type' => 5])->sum('money'); // 该订单加工费用
$logProcess = clone $model;
$logProcess->order_id = $this->order_id;
$logProcess->user_id = 0;
$logProcess->user_type = 2;// 平台
$logProcess->order_type = 1; //产品订单
$logProcess->fee_type = 5; //代煎费用
$logProcess->type = 1;// 增加
$logProcess->amount = round($platformProcessTotal, 2);
$logProcess->content = '订单结算后增加加工费用';
$logProcess->created_at = $time;
$logProcess->updated_at = $time;
$logProcess->saveOrFail();
}
$platformDeliveryTotal = 0;
//增加快递记录
if($productOrder->trans_expenses > 0){
$platformDeliveryTotal = Ledger::find()->where(['order_id' => $this->order_id, 'user_type' => 2, 'order_type' => 1, 'fee_type' => 3])->sum('money'); // 该订单快递费用
$logDelivery = clone $model;
$logDelivery->order_id = $this->order_id;
$logDelivery->user_id = 0;
$logDelivery->user_type = 2;// 平台
$logDelivery->order_type = 1; //产品订单
$logDelivery->fee_type = 3; //快递费用
$logDelivery->type = 1;// 增加
$logDelivery->amount = round($platformDeliveryTotal, 2);;
$logDelivery->content = '订单结算后增加快递费用';
$logDelivery->created_at = $time;
$logDelivery->updated_at = $time;
$logDelivery->saveOrFail();
}
$money1 = bcadd($platformDrugTotal, $platformProcessTotal, 2);
$money2 = bcadd($platformDeliveryTotal, $platformDecoctTotal, 2);
$platformTotal = round(bcadd($money1, $money2, 2), 2); //该订单平台分佣总金额
//可提现金额表更新
CashAccount::updateAllCounters(['able_cash' => $platformTotal, 'total_cash' => $platformTotal],['user_type' => 2]); //平台
/******************************************** 门店 ********************************************** */
$storeTotal = Ledger::find()->where(['order_id' => $this->order_id, 'user_id' => $store->id, 'user_type' => 1, 'order_type' => 1, 'fee_type' => 1])->sum('money'); // 该订单门店分佣总金额
$storeTotal = round($storeTotal, 2);//4舍5入
//增加门店分佣
$log = clone $model;
$log->order_id = $this->order_id;
$log->user_id = $store->id;
$log->user_type = 1;// 门店
$log->order_type = 1; //产品订单
$log->fee_type = 1; //药品费用
$log->type = 1;// 增加
$log->amount = $storeTotal;
$log->content = '订单结算后增加药品分佣金额';
$log->created_at = $time;
$log->updated_at = $time;
$log->saveOrFail();
//增加诊疗费
if($productOrder->treatement_price > 0){
$platformTreatementTotal = Ledger::find()->where(['order_id' => $this->order_id, 'user_type' => 2, 'order_type' => 1, 'fee_type' => 6])->sum('money'); // 该订单诊疗费用
$logTreatement = clone $model;
$logTreatement->order_id = $this->order_id;
$logTreatement->user_id = $store->id;
$logTreatement->user_type = 1;// 门店
$logTreatement->order_type = 1; //产品订单
$logTreatement->fee_type = 6; //诊疗费用
$logTreatement->type = 1;// 增加
$logTreatement->amount = round($platformTreatementTotal, 2);
$logTreatement->content = '订单结算后增加诊疗费用';
$logTreatement->created_at = $time;
$logTreatement->updated_at = $time;
$logTreatement->saveOrFail();
}
//门店可提现及累计金额更新
CashAccount::updateAllCounters(['able_cash' => $storeTotal, 'total_cash' => $storeTotal],['user_type' => 1, 'user_id' => $store->id]); //门店
break;
case 2://已取消
if($productOrder->is_settled == 1){ //已结算回退
//已结算对账单状态更新
Reconciliation::updateAll(['status' => -1], ['order_id' => $this->order_id]);
//分账表批量更新状态为已取消
Ledger::updateAll(['status' => 2, 'updated_at' => $time], ['order_id' => $this->order_id, 'order_type' => 1]);//1产品订单 2挂号订单
$ledgerLog = LedgerLog::find()->where(['order_id' => $this->order_id])->asArray()->all();
foreach($ledgerLog as $v){
if ($v['user_type'] === 3) {
/******************************************** 供应商 ********************************************** */
//增加供应商分佣
$log = clone $model;
$log->order_id = $v['order_id'];
$log->user_id = $v['user_id'];
$log->user_type = $v['user_type'];// 供应商
$log->order_type = $v['order_type']; //产品订单
$log->fee_type = $v['fee_type']; //药品费用
$log->type = 2;// 增加
$log->amount = $v['amount'];
$log->content = '订单结算后用户退货退款,扣减供应商金额';
$log->created_at = $time;
$log->updated_at = $time;
$log->saveOrFail();
//供应商冻结金额及累计金额更新
AccountBalanceModel::updateAllCounters([
// 'total' => bcmul($v['amount'], -1, 6),
'balance' => bcmul($v['amount'], -1, 6),
], [
'user_id' => $v['user_id'],
'type' => 3
]);
} else {
$log = clone $model;
$log->order_id = $v['order_id'];
$log->user_id = $v['user_id'];
$log->user_type = $v['user_type'];
$log->order_type = $v['order_type'];
$log->fee_type = $v['fee_type'];
$log->type = 2;// 扣减
$log->amount = $v['amount'];
$log->content = '订单结算后用户退货退款,扣减金额';
$log->created_at = $time;
$log->updated_at = $time;
$log->saveOrFail();
//可提现及累计金额更新
CashAccount::updateAllCounters(['able_cash' => $v['amount'] * -1, 'total_cash' => $v['amount'] * -1],['user_type' => $v['user_type'], 'user_id' => $v['user_id']]);
}
}
} else { // 未结算
Ledger::updateAll(['status' => 2, 'updated_at' => $time], ['order_id' => $this->order_id, 'order_type' => 1]);//order_type 1产品订单 2挂号订单
}
break;
default:
# code...
break;
}
$t->commit();
ProductOrderLog::saveLog($this->order_id, '产品订单结算更新结束');
} catch (\Exception $e){
$t->rollBack();
ProductOrderLog::saveLog($this->order_id, '产品订单结算更新异常'.$e->getMessage().'——'.$e->getLine());
throw $e;
}
break;
}
}
}