diff --git a/common/forms/LedgerForm.php b/common/forms/LedgerForm.php index e9e3817..26b1f34 100644 --- a/common/forms/LedgerForm.php +++ b/common/forms/LedgerForm.php @@ -53,7 +53,7 @@ class LedgerForm extends BaseModel throw new Exception('门店不存在'); } - + $time = time(); $ledger[] = [ @@ -84,7 +84,7 @@ class LedgerForm extends BaseModel throw $e; } break; - + default: //产品订单 ProductOrderLog::saveLog($this->order_id, '产品订单增加待结算记录开始'); $t = \Yii::$app->db->beginTransaction(); @@ -101,7 +101,6 @@ class LedgerForm extends BaseModel if(!empty($isLegder)){ throw new Exception('该订单已增加待结算记录'); } - $time = time(); $ledgerData = []; @@ -311,189 +310,67 @@ 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; - } + foreach($productOrderItems as $v){ + $ledger = []; + if ($v['dosage'] == 0) { + $v['dosage'] = 1; } - } - - // 如果是转诊处方,需要特殊处理分账逻辑 - if ($isTransferPrescription) { + $number = $v['number']; + $totalPrice = round($v['dosage'] * $number * $v['price'], 4); + $totalBuyPrice = round($v['dosage'] * $number * $v['buy_price'], 4); + /* - * 转诊处方分账逻辑: - * 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, + 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 ]; - } - - // 计算分账金额:从总利润中分配 - // 转诊处方分账逻辑:全部利润(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(); - } + $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; @@ -659,7 +536,7 @@ class LedgerForm extends BaseModel $store = Store::findOne(['id' => $Register->store_id]); if(!$store){ throw new Exception('门店不存在'); - } + } $time = time(); $t = \Yii::$app->db->beginTransaction(); $model = new LedgerLog(); @@ -671,14 +548,14 @@ class LedgerForm extends BaseModel $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; @@ -695,7 +572,7 @@ class LedgerForm extends BaseModel //门店可提现及累计金额更新 CashAccount::updateAllCounters(['able_cash' => $storeTotal, 'total_cash' => $storeTotal],['user_type' => 1, 'user_id' => $store->id]); //门店 - + break; case 2://已取消 if($Register->is_settled == 1){ //已结算回退 @@ -703,7 +580,7 @@ class LedgerForm extends BaseModel 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; @@ -718,7 +595,7 @@ class LedgerForm extends BaseModel $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']]); } @@ -738,7 +615,7 @@ class LedgerForm extends BaseModel throw $e; } break; - + default: //产品订单 ProductOrderLog::saveLog($this->order_id, '产品订单结算更新开始'); $productOrder = ProductOrder::find()->where(['id' => $this->order_id])->one(); @@ -748,7 +625,7 @@ class LedgerForm extends BaseModel $store = Store::findOne(['id' => $productOrder->store_id]); if(!$store){ throw new Exception('门店不存在'); - } + } $time = time(); $t = \Yii::$app->db->beginTransaction(); $model = new LedgerLog(); @@ -758,7 +635,7 @@ class LedgerForm extends BaseModel //更新产品订单为已结算 $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){ @@ -815,10 +692,10 @@ class LedgerForm extends BaseModel ]); } } - + //分账表批量更新状态为已结算 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'); // 该订单平台分佣总金额 //增加药品分佣 @@ -930,9 +807,9 @@ class LedgerForm extends BaseModel $logTreatement->saveOrFail(); } - //门店可提现及累计金额更新 - CashAccount::updateAllCounters(['able_cash' => $storeTotal, 'total_cash' => $storeTotal],['user_type' => 1, 'user_id' => $store->id]); //门店 - + //门店可提现及累计金额更新 + CashAccount::updateAllCounters(['able_cash' => $storeTotal, 'total_cash' => $storeTotal],['user_type' => 1, 'user_id' => $store->id]); //门店 + break; case 2://已取消 if($productOrder->is_settled == 1){ //已结算回退 @@ -940,7 +817,7 @@ class LedgerForm extends BaseModel 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) { @@ -1001,7 +878,7 @@ class LedgerForm extends BaseModel throw $e; } break; - } + } } } \ No newline at end of file