分账更新购药订单分账
This commit is contained in:
@@ -5,6 +5,7 @@ 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;
|
||||
@@ -103,88 +104,212 @@ class LedgerForm extends BaseModel
|
||||
$time = time();
|
||||
$ledgerData = [];
|
||||
|
||||
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();
|
||||
|
||||
switch ($productOrder->prescription_type) {
|
||||
case '1':case '3': //中药/颗粒药分佣
|
||||
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' => 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();
|
||||
|
||||
switch ($productOrder->prescription_type) {
|
||||
case '1':case '3': //中药/颗粒药分佣
|
||||
foreach($productOrderItems as $v){
|
||||
$ledger = [];
|
||||
$number = $v['number'];
|
||||
@@ -246,24 +371,75 @@ class LedgerForm extends BaseModel
|
||||
}
|
||||
}
|
||||
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();
|
||||
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();
|
||||
}
|
||||
} else {
|
||||
}
|
||||
|
||||
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,
|
||||
@@ -275,7 +451,7 @@ class LedgerForm extends BaseModel
|
||||
'xd_time' => $productOrder->created_at,
|
||||
'drug_id' => $v['drug_id'],
|
||||
'number' => $v['number'],
|
||||
'money' => $totalPrice - $totalBuyPrice,
|
||||
'money' => $storeMoney,
|
||||
'status' => 0,
|
||||
'created_at' => $time,
|
||||
'updated_at' => $time
|
||||
@@ -292,7 +468,24 @@ class LedgerForm extends BaseModel
|
||||
'drug_id' => $v['drug_id'],
|
||||
'xd_time' => $productOrder->created_at,
|
||||
'number' => $v['number'],
|
||||
'money' => $totalBuyPrice,
|
||||
'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
|
||||
@@ -302,80 +495,14 @@ class LedgerForm extends BaseModel
|
||||
'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;
|
||||
break;
|
||||
default:
|
||||
throw new Exception('错误的处方类型');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$t->commit();
|
||||
|
||||
$ledgerForm = new LedgerForm();
|
||||
|
||||
Reference in New Issue
Block a user