502 lines
22 KiB
PHP
502 lines
22 KiB
PHP
<?php
|
||
|
||
namespace admin\controllers\drug;
|
||
|
||
use admin\components\BaseAdminController;
|
||
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 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;
|
||
|
||
/**
|
||
* @doc-module 产品服务包库
|
||
*/
|
||
class ServerController extends BaseAdminController
|
||
{
|
||
|
||
/**
|
||
* @doc-name 药品基本信息列表
|
||
* @doc-param int status 状态类型1中药2西药3颗粒配方5全部列表
|
||
* @doc-param string name 搜素药名、编号、条形码、厂家、中西药标签、拼音首拼、别名
|
||
* @doc-return mixed @List{id,drug_name-string-药名,drug_number-string-编号,drug_alias-string-别名,bar_code-string-条形码,guozi_no-string-国字准号,category_first-int-一级分类,category_second-int-二级分类,source-string-货源(厂家),function-string-功能主治,decotion-string-煎法,is_otc-int-是否处方药,usage-string-用法,specification-string-规格,instruction-string-说明书图片,image-string-药品图片,time-string-使用时间,use_type-string-使用方法,frequency-string-频率,unit-string-单位,small_info-string-简介,info-string-基础信息,content-string-详情信息,status-int-状态1草稿2下架3上架,is_shalving-int-状态1下架2上架,stock-int-库存,store-int-门店,type-int-类别1中药2西药3颗粒配方4中成药} 药品列表
|
||
* @doc-return mixed @Pagination{total-int-总数量,totalPage-int-总页数,pageSize-int-每页条数} 分页数据
|
||
*/
|
||
public function actionList()
|
||
{
|
||
$get = \Yii::$app->request->get();
|
||
|
||
if (!empty($get['start_time']) && !empty($get['end_time'])) {
|
||
$start_time = strtotime($get['start_time'] . ' ' . '00:00:00');
|
||
$end_time = strtotime($get['end_time'] . ' ' . '23:59:59');
|
||
}
|
||
|
||
$query = Drug::find()->with(['supplier'])->alias('d')->orderBy(['id' => SORT_DESC]);
|
||
|
||
$user = \Yii::$app->user->identity;
|
||
|
||
if ($user->role == UserRoleEnum::STORE_ADMIN) { //门店
|
||
$query->joinWith(['drugStoreRelationes' => function ($drug) use ($user) {
|
||
$drug->alias('ds');
|
||
$drug->andWhere(['ds.store_id' => $user->store_id,'ds.is_forbid'=>0,'ds.status'=>2]);
|
||
}]);
|
||
}
|
||
|
||
|
||
$query->andWhere(['d.type' => 5]);
|
||
if (!empty($get['name'])) {
|
||
$query->andWhere([
|
||
'or',
|
||
['like', 'drug_name', $get['name']],
|
||
['like', 'drug_number', $get['name']],
|
||
['like', 'bar_code', $get['name']],
|
||
['like', 'source', $get['name'] ],
|
||
['like', 'pinyin_simple', $get['name'] ],
|
||
['like', 'drug_alias', $get['name'] ],
|
||
]);
|
||
}
|
||
|
||
if (!empty($start_time) && !empty($end_time)) {
|
||
if ($start_time==$end_time){//筛选当天
|
||
$time=FuncHelper::getDayBE($get['start_time']);
|
||
|
||
$end_time=$time;
|
||
}
|
||
|
||
$query->andWhere(['between', 'd.created_at', $start_time,$end_time]);
|
||
}
|
||
if (!empty($get['type'])) {
|
||
$query->andWhere(['type' => $get['type']]);
|
||
}
|
||
|
||
$this->field = [
|
||
Drug::class => [
|
||
'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',
|
||
'stock' => 'drugStoreRelationes.drugStoreDrug.stock',
|
||
'is_shalving' => 'drugStoreRelationes.status',
|
||
'type_id', 'frequency_id', 'unit_id', 'time_id',
|
||
'use_frequency' => 'drugUseFrequency.name', 'use_type' => 'drugUseType.name',
|
||
'use_time' => 'drugUseTime.name',
|
||
'is_forbid'=>'drugStoreRelationes.is_forbid',
|
||
'supplier',
|
||
'source_name' => 'supplier.name',
|
||
'specification',
|
||
'platform_ledger',
|
||
'store_ledger',
|
||
'supplier_ledger',
|
||
],
|
||
];
|
||
return $this->create($query, $get);
|
||
}
|
||
/**
|
||
* @doc-name 新增产品服务包库
|
||
* @doc-param string drug_name 药名
|
||
* @doc-param string pinyin_simple 拼音首拼
|
||
* @doc-param string drug_number 编号
|
||
* @doc-param string drug_alias 别名
|
||
* @doc-param int unit_id 单位
|
||
* @doc-param int status 状态1草稿2下架3上架
|
||
* @doc-param string place 产地 / optional
|
||
* @doc-param string instruction 说明书图片 / optional
|
||
* @doc-param json drugstore[{"drugstore_id":1,"price":100,"type":1,"stock":100},{"drugstore_id":2,"price":100,"type":1,"stock":200}] 仓库
|
||
*/
|
||
public function actionSave()
|
||
{
|
||
$post=\Yii::$app->request->post();
|
||
$post['type']=5;
|
||
$GranularForm=new ServerForm();
|
||
$GranularForm->attributes=$post;
|
||
return $GranularForm->save();
|
||
}
|
||
|
||
/**
|
||
* 获取服务商的下拉框
|
||
* @return array|\yii\db\ActiveRecord[]
|
||
*/
|
||
public function actionSupplierOption()
|
||
{
|
||
return SupplierModel::find()->select(['id', 'name'])->all();
|
||
}
|
||
/**
|
||
* @doc-name 删除产品服务包库
|
||
* @doc-param int id 药品ID
|
||
*/
|
||
public function actionDelete()
|
||
{
|
||
$post=\Yii::$app->request->post();
|
||
$this->requestValidate($post,[
|
||
['id','required']
|
||
]);
|
||
$GranularForm = new ServerForm();
|
||
$GranularForm->attributes=$post;
|
||
return $GranularForm->del();
|
||
}
|
||
|
||
/**
|
||
* @doc-name 修改产品服务包库
|
||
* @doc-param int id 药品ID
|
||
* @doc-param string drug_name 药名
|
||
* @doc-param string pinyin_simple 拼音首拼
|
||
* @doc-param string drug_number 编号
|
||
* @doc-param string drug_alias 别名
|
||
* @doc-param int unit_id 单位
|
||
* @doc-param string place 产地 / optional
|
||
* @doc-param string instruction 说明书图片 / optional
|
||
*/
|
||
public function actionUpdate()
|
||
{
|
||
$post=\Yii::$app->request->post();
|
||
$this->requestValidate($post,[
|
||
['id','required']
|
||
]);
|
||
$GranularForm = new ServerForm();
|
||
$GranularForm->attributes=$post;
|
||
return $GranularForm->update();
|
||
|
||
}
|
||
/**
|
||
* @doc-name 查看产品服务包库
|
||
* @doc-param int id 药品ID
|
||
*/
|
||
public function actionInfo()
|
||
{
|
||
$drug= Drug::find()->with(['supplier'])->where([
|
||
'id'=>\Yii::$app->request->get('id'),
|
||
'type'=>5,
|
||
])->asArray()->one();
|
||
if (!$drug) throw new Exception('该药品不存在');
|
||
$drug['source_name']=$drug['supplier']['name'];
|
||
return $drug;
|
||
}
|
||
|
||
# 后面可能回做一个结算重试
|
||
// 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 ['重试成功'];
|
||
// }
|
||
} |