产品服务包,分账
This commit is contained in:
@@ -18,99 +18,95 @@ class SyncController extends BaseAdminController
|
||||
*/
|
||||
public function actionSyncStore()
|
||||
{
|
||||
$admin = \Yii::$app->user->identity;
|
||||
if ($admin->role != UserRoleEnum::SUPER_ADMIN) {
|
||||
throw new Exception('非超管,您不能同步药品');
|
||||
// 定义药物类型映射
|
||||
$drugTypes = [
|
||||
'chinese' => 1, // 假设中药类型为1
|
||||
'west' => 2,
|
||||
'grain' => 3,
|
||||
'zongcheng' => 4,
|
||||
'service_package' => 5
|
||||
];
|
||||
|
||||
// 获取所有类型的药物数据
|
||||
$drugData = [];
|
||||
foreach ($drugTypes as $typeKey => $typeValue) {
|
||||
$drugData[$typeKey] = DrugStoreDrug::find()
|
||||
->select(['drug_id', 'price', 'market_price', 'status'])
|
||||
->where(['type' => $typeValue])
|
||||
->asArray()
|
||||
->all();
|
||||
}
|
||||
$DrugStoreDrug_chinese = DrugStoreDrug::find()->select(['drug_id', 'price', 'market_price', 'status'])->where(['type' => 1])->asArray()->all();
|
||||
|
||||
$DrugStoreDrug_west = DrugStoreDrug::find()->select(['drug_id', 'price', 'market_price', 'status'])->where(['type' => 2])->asArray()->all();
|
||||
// 获取有效的店铺数据
|
||||
$store = Store::find()
|
||||
->select(['id', 'z_buy_percent', 'z_sale_percent', 'g_bug_percent', 'g_sale_percent'])
|
||||
->where(['is_delete' => 0])
|
||||
->all();
|
||||
|
||||
$DrugStoreDrug_grain = DrugStoreDrug::find()->select(['drug_id', 'price', 'market_price', 'status'])->where(['type' => 3])->asArray()->all();
|
||||
$DrugStoreDrug_zongcheng = DrugStoreDrug::find()->select(['drug_id', 'price', 'market_price', 'status'])->where(['type' => 4])->asArray()->all();
|
||||
$store = Store::find()->select(['id', 'z_buy_percent', 'z_sale_percent', 'g_bug_percent', 'g_sale_percent'])->where(['is_delete' => 0])->all();
|
||||
if (empty($store) || empty(array_filter($drugData))) {
|
||||
return ['同步成功']; // 如果没有任何数据需要处理,直接返回
|
||||
}
|
||||
|
||||
$t = \Yii::$app->db->beginTransaction();
|
||||
try {
|
||||
foreach ($store as $value) {//中药
|
||||
foreach ($DrugStoreDrug_chinese as $val) {
|
||||
|
||||
$Drug = DrugStoreRelations::find()->where([
|
||||
'store_id' => $value['id'],
|
||||
'drug_id' => $val['drug_id']
|
||||
])->one();
|
||||
if (!$Drug) {
|
||||
$DrugStoreRelations = new DrugStoreRelations();
|
||||
$DrugStoreRelations->store_id = $value['id'];
|
||||
$DrugStoreRelations->drug_id = $val['drug_id'];
|
||||
$DrugStoreRelations->price = bcdiv(bcmul($val['price'], $value['z_sale_percent']??100, 4), 100, 4);
|
||||
$DrugStoreRelations->buy_price = bcdiv(bcmul($val['market_price'], $value['z_buy_percent']??100, 4), 100, 4);
|
||||
$DrugStoreRelations->status = $val['status'];
|
||||
$DrugStoreRelations->saveOrFail();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($store as $value) {//西药
|
||||
foreach ($DrugStoreDrug_west as $v) {
|
||||
$DrugStore = DrugStoreRelations::find()->where([
|
||||
'store_id' => $value['id'],
|
||||
'drug_id' => $v['drug_id']
|
||||
])->one();
|
||||
if (!$DrugStore) {
|
||||
$DrugStoreRelations = new DrugStoreRelations();
|
||||
$DrugStoreRelations->store_id = $value['id'];
|
||||
$DrugStoreRelations->drug_id = $v['drug_id'];
|
||||
$DrugStoreRelations->price = $v['price'];
|
||||
$DrugStoreRelations->buy_price = $v['market_price'] ?? $v['price'];
|
||||
$DrugStoreRelations->status = $v['status'];
|
||||
$DrugStoreRelations->saveOrFail();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($store as $value) {//配方
|
||||
foreach ($DrugStoreDrug_grain as $vv) {
|
||||
$DrugStoreRelations = DrugStoreRelations::find()->where([
|
||||
'store_id' => $value['id'],
|
||||
'drug_id' => $vv['drug_id']
|
||||
])->one();
|
||||
if (!$DrugStoreRelations) {
|
||||
$DrugStoreRelations = new DrugStoreRelations();
|
||||
$DrugStoreRelations->store_id = $value['id'];
|
||||
$DrugStoreRelations->drug_id = $vv['drug_id'];
|
||||
$DrugStoreRelations->price = bcdiv(bcmul($vv['price'], $value['g_sale_percent']??100, 4), 100, 4);
|
||||
$DrugStoreRelations->buy_price = bcdiv(bcmul($vv['market_price'], $value['g_bug_percent']??100, 4), 100, 4);
|
||||
$DrugStoreRelations->status = $vv['status'];
|
||||
$DrugStoreRelations->saveOrFail();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($store as $value) {//中成药
|
||||
foreach ($DrugStoreDrug_zongcheng as $vv) {
|
||||
$DrugStoreRelations_z = DrugStoreRelations::find()->where([
|
||||
'store_id' => $value['id'],
|
||||
'drug_id' => $vv['drug_id']
|
||||
])->one();
|
||||
if (!$DrugStoreRelations_z) {
|
||||
$DrugStoreRelations = new DrugStoreRelations();
|
||||
$DrugStoreRelations->store_id = $value['id'];
|
||||
$DrugStoreRelations->drug_id = $vv['drug_id'];
|
||||
$DrugStoreRelations->price = $vv['price'];
|
||||
$DrugStoreRelations->buy_price = $vv['market_price'] ?? $vv['price'];
|
||||
$DrugStoreRelations->status = $vv['status'];
|
||||
$DrugStoreRelations->saveOrFail();
|
||||
}
|
||||
}
|
||||
foreach ($drugTypes as $typeKey => $typeValue) {
|
||||
$this->processDrugType($typeKey, $drugData[$typeKey], $store, $drugTypes);
|
||||
}
|
||||
|
||||
$t->commit();
|
||||
return ['同步成功'];
|
||||
} catch (\Exception $e) {
|
||||
$t->rollBack();
|
||||
throw new Exception($e->getMessage());
|
||||
throw new Exception("事务回滚: " . $e->getMessage(), 0, $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理特定类型的药物
|
||||
*
|
||||
* @param string $typeKey 药物类型键
|
||||
* @param array $drugList 药物列表
|
||||
* @param array $store 店铺列表
|
||||
* @throws \yii\base\Exception
|
||||
*/
|
||||
protected function processDrugType($typeKey, $drugList, $store, $drugTypes) {
|
||||
|
||||
// 批量获取所有需要插入的记录
|
||||
$existingRelations = DrugStoreRelations::find()
|
||||
->select(['store_id', 'drug_id'])
|
||||
->indexBy(function ($row) {
|
||||
return "{$row['store_id']}_{$row['drug_id']}";
|
||||
})
|
||||
->column();
|
||||
|
||||
foreach ($store as $storeItem) {
|
||||
foreach ($drugList as $drugItem) {
|
||||
$key = "{$storeItem['id']}_{$drugItem['drug_id']}";
|
||||
if (!isset($existingRelations[$key])) {
|
||||
$relation = new DrugStoreRelations();
|
||||
$relation->store_id = $storeItem['id'];
|
||||
$relation->drug_id = $drugItem['drug_id'];
|
||||
$relation->status = $drugItem['status'];
|
||||
|
||||
switch ($typeKey) {
|
||||
case 'chinese':
|
||||
$relation->price = bcdiv(bcmul($drugItem['price'], $storeItem['z_sale_percent'] ?? 100, 4), 100, 4);
|
||||
$relation->buy_price = bcdiv(bcmul($drugItem['market_price'], $storeItem['z_buy_percent'] ?? 100, 4), 100, 4);
|
||||
break;
|
||||
case 'grain':
|
||||
$relation->price = bcdiv(bcmul($drugItem['price'], $storeItem['g_sale_percent'] ?? 100, 4), 100, 4);
|
||||
$relation->buy_price = bcdiv(bcmul($drugItem['market_price'], $storeItem['g_bug_percent'] ?? 100, 4), 100, 4);
|
||||
break;
|
||||
default:
|
||||
$relation->price = $drugItem['price'];
|
||||
$relation->buy_price = $drugItem['market_price'] ?? $drugItem['price'];
|
||||
break;
|
||||
}
|
||||
|
||||
$relation->saveOrFail();
|
||||
}
|
||||
}
|
||||
}
|
||||
$t->commit();
|
||||
return ['同步成功'];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,16 +3,20 @@
|
||||
namespace admin\controllers\drug;
|
||||
|
||||
use admin\components\BaseAdminController;
|
||||
use admin\models\forms\drug\GranularForm;
|
||||
use admin\models\forms\drug\ImportForm;
|
||||
use admin\models\forms\drug\ServerForm;
|
||||
use common\enums\UserRoleEnum;
|
||||
use common\forms\LedgerForm;
|
||||
use common\helpers\FuncHelper;
|
||||
use common\models\newDb\AccountBalanceModel;
|
||||
use common\models\newDb\SupplierModel;
|
||||
use common\models\oldDb\CashAccount;
|
||||
use common\models\oldDb\Drug;
|
||||
use moonland\phpexcel\Excel;
|
||||
use common\models\oldDb\Ledger;
|
||||
use common\models\oldDb\LedgerLog;
|
||||
use common\models\oldDb\ProductOrder;
|
||||
use common\models\oldDb\ProductOrderItems;
|
||||
use common\models\oldDb\Store;
|
||||
use yii\db\Exception;
|
||||
use yii\web\UploadedFile;
|
||||
|
||||
/**
|
||||
* @doc-module 产品服务包库
|
||||
@@ -76,7 +80,7 @@ class ServerController extends BaseAdminController
|
||||
|
||||
$this->field = [
|
||||
Drug::class => [
|
||||
'id', 'pinyin_simple', 'drug_name', 'source', 'type', 'is_otc', 'category_first', 'category_second', 'small_info', 'info', 'content', 'usage', 'specification', 'image', 'status', 'drug_number', 'drug_alias', 'bar_code', 'guozi_no', 'place', 'function', 'instruction', 'decotion_id' => 'decotion', 'decotion' => 'drugUseWay.name', 'unit' => 'unit.name',
|
||||
'id', 'pinyin_simple', 'drug_name', 'source', 'type', 'is_otc', 'category_first', 'category_second', 'small_info', 'info', 'content', 'usage', 'image', 'status', 'drug_number', 'drug_alias', 'bar_code', 'guozi_no', 'place', 'function', 'instruction', 'decotion_id' => 'decotion', 'decotion' => 'drugUseWay.name', 'unit' => 'unit.name',
|
||||
'store' => 'drugStoreRelationes.store_id',
|
||||
'price' => 'drugStoreRelationes.price',
|
||||
'buy_price' => 'drugStoreRelationes.buy_price',
|
||||
@@ -88,6 +92,10 @@ class ServerController extends BaseAdminController
|
||||
'is_forbid'=>'drugStoreRelationes.is_forbid',
|
||||
'supplier',
|
||||
'source_name' => 'supplier.name',
|
||||
'specification',
|
||||
'platform_ledger',
|
||||
'store_ledger',
|
||||
'supplier_ledger',
|
||||
],
|
||||
];
|
||||
return $this->create($query, $get);
|
||||
@@ -131,7 +139,7 @@ class ServerController extends BaseAdminController
|
||||
$this->requestValidate($post,[
|
||||
['id','required']
|
||||
]);
|
||||
$GranularForm=new GranularForm();
|
||||
$GranularForm = new ServerForm();
|
||||
$GranularForm->attributes=$post;
|
||||
return $GranularForm->del();
|
||||
}
|
||||
@@ -153,7 +161,7 @@ class ServerController extends BaseAdminController
|
||||
$this->requestValidate($post,[
|
||||
['id','required']
|
||||
]);
|
||||
$GranularForm=new GranularForm();
|
||||
$GranularForm = new ServerForm();
|
||||
$GranularForm->attributes=$post;
|
||||
return $GranularForm->update();
|
||||
|
||||
@@ -173,35 +181,322 @@ class ServerController extends BaseAdminController
|
||||
return $drug;
|
||||
}
|
||||
|
||||
/**
|
||||
* @doc-name 导入产品服务包库
|
||||
* @doc-param file file 文件
|
||||
*/
|
||||
public function actionGranularImport()
|
||||
{
|
||||
$request = \Yii::$app->request;
|
||||
if ($request->isPost) {
|
||||
//设置最大执行时间
|
||||
ini_set("max_execution_time", "360");
|
||||
$file = UploadedFile::getInstanceByName('file');
|
||||
if ($file->size > 5 * 1024 * 1024) {
|
||||
return ['excel文件不能超过5M!'];
|
||||
}
|
||||
|
||||
//文件名
|
||||
$filename = date('His') . md5($file->getBaseName()) . mt_rand(1000, 9999) . '.' . $file->getExtension();
|
||||
//保存文件
|
||||
$file->saveAs($filename);
|
||||
|
||||
$data = Excel::import($filename, [
|
||||
'setFirstRecordAsKeys' => true,
|
||||
'getOnlySheet' => 'Sheet1',
|
||||
]);
|
||||
|
||||
$ImportForm=new ImportForm();
|
||||
return $ImportForm->GranularImport($data);
|
||||
} else {
|
||||
return ['请求方式错误'];
|
||||
}
|
||||
}
|
||||
# 后面可能回做一个结算重试
|
||||
// public function actionServiceLedger()
|
||||
// {
|
||||
// //门店可提现及累计金额更新
|
||||
// $supplierId = 1;
|
||||
//
|
||||
// $time = time();
|
||||
// /******************************************** 供应商 ********************************************** */
|
||||
// $storeTotal = Ledger::find()->where(['order_id' => -1, 'user_id' => $supplierId, 'user_type' => 3, 'order_type' => 1, 'fee_type' => 1])->sum('money'); // 该订单供应商分佣总金额
|
||||
// $storeTotal = round($storeTotal, 6)?? '20.01';//4舍5入
|
||||
// //增加供应商分佣
|
||||
// $log = new LedgerLog();
|
||||
// $log->order_id = -1;
|
||||
// $log->user_id = $supplierId;
|
||||
// $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' => $supplierId,
|
||||
// 'type' => 3
|
||||
// ]);
|
||||
// return ['分账成功'];
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// $t = \Yii::$app->db->beginTransaction();
|
||||
// $orderId = 314;
|
||||
// try {
|
||||
// $productOrder = ProductOrder::find()->with(['orderItems'])->where(['id' => $orderId])->one();
|
||||
// if (!$productOrder) {
|
||||
// throw new Exception('产品订单不存在');
|
||||
// }
|
||||
// $store = Store::findOne(['id' => $productOrder->store_id]);
|
||||
// if (!$store) {
|
||||
// throw new Exception('仓库不存在');
|
||||
// }
|
||||
// $isLegder = Ledger::find()->where(['order_id' => $orderId])->andWhere(['order_type' => 1])->one();
|
||||
// if (!empty($isLegder)) {
|
||||
// throw new Exception('该订单已增加待结算记录');
|
||||
// }
|
||||
// $time = time();
|
||||
// $ledgerData = [];
|
||||
// if ($productOrder->decoct_price > 0) {
|
||||
// $ledgerData[] = [
|
||||
// 'order_id' => $orderId,
|
||||
// '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' => $orderId,
|
||||
// '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' => $orderId,
|
||||
// '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' => $orderId,
|
||||
// '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()->with(['drug.supplier'])->where(['product_order_id' => $productOrder->id])->asArray()->all();
|
||||
//
|
||||
// switch ($productOrder->prescription_type) {
|
||||
// case '1':
|
||||
// case '3': //中药/颗粒药分佣
|
||||
// foreach ($productOrderItems as $v) {
|
||||
// $ledger = [];
|
||||
// $number = $v['number'];
|
||||
// $totalPrice = round($number * $v['price'], 4);
|
||||
// $totalBuyPrice = round($number * $v['buy_price'], 4);
|
||||
//
|
||||
// $ledger[] = [
|
||||
// 'order_id' => $orderId,
|
||||
// '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' => $orderId,
|
||||
// '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);
|
||||
// $ledger[] = [
|
||||
// 'order_id' => $orderId,
|
||||
// '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' => $orderId,
|
||||
// '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':
|
||||
// //产品服务包分成
|
||||
// $ledger = [];
|
||||
// foreach ($productOrderItems as $v) {
|
||||
// $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' => $orderId,
|
||||
// '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' => $orderId,
|
||||
// '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' => $orderId,
|
||||
// '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
|
||||
// ];
|
||||
// ds($ledger);
|
||||
// \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 = $orderId;
|
||||
//// $ledgerForm->status = 1; // 已结算
|
||||
//// $ledgerForm->update();
|
||||
// } catch (\Exception $e) {
|
||||
// $t->rollBack();
|
||||
// throw $e;
|
||||
// }
|
||||
// return ['重试成功'];
|
||||
// }
|
||||
}
|
||||
@@ -66,6 +66,11 @@ class WareController extends BaseAdminController
|
||||
$this->field = [
|
||||
DrugStoreDrug::class => [
|
||||
'id', 'drug_id', 'type', 'stock', 'price', 'market_price', 'status',
|
||||
'specification' => 'drug.specification',
|
||||
'guozi_no' => 'drug.guozi_no',
|
||||
'bar_code' => 'drug.bar_code',
|
||||
'source' => 'drug.source',
|
||||
'supplier' => 'drug.supplier',
|
||||
'name' => 'drug.drug_name',
|
||||
'image' => 'drug.image',
|
||||
'pinyin_simple' => 'drug.pinyin_simple',
|
||||
@@ -776,6 +781,11 @@ class WareController extends BaseAdminController
|
||||
'reference_price' => 'drugStoreDrug.price',
|
||||
'market_price' => 'drugStoreDrug.market_price',
|
||||
'status',
|
||||
'specification' => 'drug.specification',
|
||||
'guozi_no' => 'drug.guozi_no',
|
||||
'bar_code' => 'drug.bar_code',
|
||||
'source' => 'drug.source',
|
||||
'supplier' => 'drug.supplier',
|
||||
'name' => 'drug.drug_name',
|
||||
'image' => 'drug.image',
|
||||
'pinyin_simple' => 'drug.pinyin_simple',
|
||||
|
||||
@@ -24,6 +24,9 @@ class ServerForm extends BaseModel
|
||||
public $usage;
|
||||
public $function;
|
||||
public $drugstore;
|
||||
public $platform_ledger;
|
||||
public $store_ledger;
|
||||
public $supplier_ledger;
|
||||
|
||||
public $pinyin_simple;
|
||||
|
||||
@@ -34,7 +37,7 @@ class ServerForm extends BaseModel
|
||||
{
|
||||
return [
|
||||
[['id','is_otc'],'integer'],
|
||||
[['drug_name','drug_number','image','specification','pinyin_simple','function','status'],'required'],
|
||||
[['drug_name','drug_number','image','specification','pinyin_simple','platform_ledger','store_ledger','supplier_ledger','function','status'],'required'],
|
||||
[['place','source','instruction','drugstore','pinyin_simple','usage'],'string'],
|
||||
];
|
||||
}
|
||||
@@ -55,13 +58,22 @@ class ServerForm extends BaseModel
|
||||
throw new Exception($this->getErrorMsg());
|
||||
}
|
||||
|
||||
$ledgerTotal = $this->platform_ledger + $this->store_ledger + $this->supplier_ledger;
|
||||
|
||||
if ($ledgerTotal>100 || $ledgerTotal<0) {
|
||||
throw new Exception('分账比例之和不能大于100或小于0');
|
||||
}
|
||||
$t = \Yii::$app->db->beginTransaction();
|
||||
try {
|
||||
$drug=new Drug();
|
||||
$drug->attributes=$this->attributes;
|
||||
$drug->specification = $this->specification;
|
||||
$drug->usage = $this->usage;
|
||||
$drug->platform_ledger = $this->platform_ledger;
|
||||
$drug->store_ledger = $this->store_ledger;
|
||||
$drug->supplier_ledger = $this->supplier_ledger;
|
||||
$drug->type = '5';
|
||||
|
||||
$drug->created_at=time();
|
||||
$drug->saveOrFail();
|
||||
|
||||
@@ -75,9 +87,14 @@ class ServerForm extends BaseModel
|
||||
|
||||
public function update()
|
||||
{
|
||||
$ledgerTotal = $this->platform_ledger + $this->store_ledger + $this->supplier_ledger;
|
||||
|
||||
if ($ledgerTotal>100 || $ledgerTotal<0) {
|
||||
throw new Exception('分账比例之和不能大于100或小于0');
|
||||
}
|
||||
$drug=Drug::find()->where([
|
||||
'id'=>$this->id,
|
||||
'type'=>3,
|
||||
'type'=>5,
|
||||
])->one();
|
||||
if (!$drug) throw new Exception('该服务包不存在');
|
||||
$drug->pinyin_simple=$this->pinyin_simple??$drug->pinyin_simple;
|
||||
@@ -89,6 +106,10 @@ class ServerForm extends BaseModel
|
||||
$drug->place=$this->place??$drug->place;
|
||||
$drug->source=$this->source??$drug->source;
|
||||
$drug->instruction=$this->instruction??$drug->instruction;
|
||||
$drug->platform_ledger = $this->platform_ledger;
|
||||
$drug->store_ledger = $this->store_ledger;
|
||||
$drug->supplier_ledger = $this->supplier_ledger;
|
||||
|
||||
|
||||
$drug->saveOrFail();
|
||||
return ['编辑成功'];
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace common\forms;
|
||||
|
||||
use common\core\BaseModel;
|
||||
use common\models\newDb\AccountBalanceModel;
|
||||
use common\models\oldDb\CashAccount;
|
||||
use common\models\oldDb\Ledger;
|
||||
use common\models\oldDb\LedgerLog;
|
||||
@@ -116,7 +117,7 @@ class LedgerForm extends BaseModel
|
||||
$ledgerData[] = [
|
||||
'order_id' => $this->order_id,
|
||||
'user_id' => 0,
|
||||
'user_type' => 2, //平台
|
||||
'user_type' => 3, //供应商
|
||||
'order_type' => 1,//产品订单
|
||||
'fee_type' => 5,//加工费用
|
||||
'su_id' => $productOrder->su_id,
|
||||
@@ -265,6 +266,72 @@ class LedgerForm extends BaseModel
|
||||
], $ledger)->execute();
|
||||
}
|
||||
|
||||
break;
|
||||
case '5':
|
||||
|
||||
//产品服务包分成
|
||||
$ledger = [];
|
||||
foreach ($productOrderItems as $v) {
|
||||
$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('错误的处方类型');
|
||||
@@ -416,6 +483,34 @@ class LedgerForm extends BaseModel
|
||||
$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入
|
||||
//增加供应商分佣
|
||||
$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
|
||||
]);
|
||||
}
|
||||
|
||||
//分账表批量更新状态为已结算
|
||||
@@ -541,21 +636,47 @@ class LedgerForm extends BaseModel
|
||||
|
||||
$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']]);
|
||||
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),
|
||||
'frozen' => bcmul($v['amount'], -1, 6),
|
||||
], [
|
||||
'user_id' => $v->drug->source,
|
||||
'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挂号订单
|
||||
|
||||
19
common/models/newDb/AccountBalanceModel.php
Normal file
19
common/models/newDb/AccountBalanceModel.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace common\models\newDb;
|
||||
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
|
||||
class AccountBalanceModel extends \common\modelsgii\newDb\AccountBalanceModelGii
|
||||
{
|
||||
public function behaviors()
|
||||
{
|
||||
return [
|
||||
[
|
||||
'class' => TimestampBehavior::class,
|
||||
'attributes' => [
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
49
common/modelsgii/newDb/AccountBalanceModelGii.php
Normal file
49
common/modelsgii/newDb/AccountBalanceModelGii.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace common\modelsgii\newDb;
|
||||
|
||||
/**
|
||||
* This is the model class for table "yii_address".
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $user_id
|
||||
* @property string $name
|
||||
* @property string $mobile
|
||||
* @property string $province 省
|
||||
* @property string $city 市
|
||||
* @property string $area 区
|
||||
* @property string $region 省市区
|
||||
* @property string $detail_address 详细地址
|
||||
* @property int $is_default 是否默认地址0否1是
|
||||
* @property int $created_at
|
||||
* @property int $updated_at
|
||||
* @property int $deleted_at
|
||||
*/
|
||||
class AccountBalanceModelGii extends \common\core\BaseNewActiveRecord
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function tableName()
|
||||
{
|
||||
return '{{%account_balance}}';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user