Files
xk-api-yii/admin/controllers/store/WareController.php
2024-09-23 13:56:03 +08:00

629 lines
27 KiB
PHP

<?php
namespace admin\controllers\store;
use admin\components\BaseAdminController;
use admin\controllers\AuthController;
use common\enums\UserRoleEnum;
use common\jobs\ProductOrderAutoReceivedJob;
use common\jobs\ProductOrderSendJob;
use common\jobs\templateMessage\ProductOrderSend;
use common\models\DrugStoreDrug;
use common\models\DrugStoreRelations;
use common\models\ExpressCompanies;
use common\models\ExpressNos;
use common\models\Log;
use common\models\ProductOrder;
use common\models\Store;
use GuzzleHttp\Client;
use yii\db\Exception;
use yii\helpers\Json;
/**
* @doc-module 仓库
*/
class WareController extends BaseAdminController
{
/**
* @doc-name 仓库药品列表
* @doc-param int status 状态1下架2上架 / optional
* @doc-param string name 搜索别名,拼音首拼,药名 / optional
* @doc-param int type 类型1中药2西药3颗粒配方4中成药 / optional
* @doc-return mixed @List{id,drug_id,type-int-类型1中药2西药3颗粒配方,stock-int-库存,price-int-销售价格,market_price-int-采购价,status-int-状态1下架2上架,name-string-药名,pinyin_simple-string-拼音首拼,is_otc-int-是否处方} 药品信息
* @doc-return mixed @Pagination{total-int-总数量,totalPage-int-总页数,pageSize-int-每页条数} 分页数据
*/
public function actionWareGoodList()
{
$get = \Yii::$app->request->get();
$name = $get['name'];
$query = DrugStoreDrug::find()->alias('ds');
$query->joinWith(['drug' => function ($d) use ($name) {
if (!empty($name)) {
$d->andWhere([
'or',
['like', 'drug_name', $name],
['like', 'pinyin_simple', $name],
['like', 'pinyin_simple', $name],
]);
}
}]);
if (!empty($get['type'])) {
$query->andWhere(['ds.type' => $get['type']]);
}
if (!empty($get['status'])) {
$query->andWhere(['ds.status' => $get['status']]);
}
$this->field = [
DrugStoreDrug::class => [
'id', 'drug_id', 'type', 'stock', 'price', 'market_price', 'status',
'name' => 'drug.drug_name',
'image' => 'drug.image',
'pinyin_simple' => 'drug.pinyin_simple',
'is_otc' => 'drug.is_otc','drug_number'=>'drug.drug_number',
'drug_alias'=>'drug.drug_alias',
]
];
return $this->create($query, $get);
}
/**
* @doc-name 添加仓库商品
* @doc-param int drug_id 药id
* @doc-param int type 药品类型 1中药 2西药 3颗粒药 4中成药
* @doc-param float price 销售价格
* @doc-param float market_price 市场价格
* @doc-param int stock 库存
*/
public function actionSaveWareDrug()
{
$post = \Yii::$app->request->post();
$this->requestValidate($post, [
[['drug_id', 'type', 'price', 'stock'], 'required']
]);
$DrugStoreDrug = DrugStoreDrug::find()->where([
'drugstore_id' => 1,
'drug_id' => $post['drug_id'],
])->one();
if ($DrugStoreDrug) throw new Exception('已存在,请勿重复添加');
if ($post['type']==1 || $post['type']==3 ){
if (empty($post['market_price'])){
throw new Exception('市场价格不能为空');
}
}
$t = \Yii::$app->db->beginTransaction();
try {
$drug = new DrugStoreDrug();
$drug->drugstore_id = 1;//仓库默认为1
$drug->drug_id = $post['drug_id'];
$drug->type = $post['type'];
$drug->price = $post['price'];
$drug->market_price = $post['market_price']??0;
$drug->stock = $post['stock'];
$drug->saveOrFail();
$Store = Store::find()->select(['id','z_buy_percent','z_sale_percent','g_bug_percent','g_sale_percent','store_set_sale_z','store_set_sale_g'])->where(['is_delete' => 0])->all();
if ($post['type']==1){
foreach ($Store as $val) {
if (!empty($val['store_set_sale_z'])){
$DrugStoreRelations = new DrugStoreRelations();
$DrugStoreRelations->drug_id = $post['drug_id'];
$DrugStoreRelations->store_id = $val['id'];
$DrugStoreRelations->price =bcdiv(bcmul($post['price'], $val['store_set_sale_z']??100, 4), 100, 4);//销售价
$DrugStoreRelations->buy_price = bcdiv(bcmul($post['market_price'], $val['z_buy_percent']??100, 4), 100, 4);//进货(采购)价
$DrugStoreRelations->status = 2;//上架
$DrugStoreRelations->created_at = date('Y-m-d H:i:s', time());
$DrugStoreRelations->updated_at = date('Y-m-d H:i:s', time());
$DrugStoreRelations->saveOrFail();
}else{
$DrugStoreRelations = new DrugStoreRelations();
$DrugStoreRelations->drug_id = $post['drug_id'];
$DrugStoreRelations->store_id = $val['id'];
$DrugStoreRelations->price =bcdiv(bcmul($post['price'], $val['z_sale_percent']??100, 4), 100, 4);//销售价
$DrugStoreRelations->buy_price = bcdiv(bcmul($post['market_price'], $val['z_buy_percent']??100, 4), 100, 2);//进货(采购)价
$DrugStoreRelations->status = 2;//上架
$DrugStoreRelations->created_at = date('Y-m-d H:i:s', time());
$DrugStoreRelations->updated_at = date('Y-m-d H:i:s', time());
$DrugStoreRelations->saveOrFail();
}
}
}
if ($post['type']==3){
foreach ($Store as $val){
if (!empty($val['store_set_sale_g'])){
$DrugStoreRelations = new DrugStoreRelations();
$DrugStoreRelations->drug_id = $post['drug_id'];
$DrugStoreRelations->store_id = $val['id'];
$DrugStoreRelations->price =bcdiv(bcmul($post['price'], $val['store_set_sale_g']??100, 4), 100, 4);//销售价
$DrugStoreRelations->buy_price = bcdiv(bcmul($post['market_price'], $val['g_bug_percent']??100, 4), 100, 4);//进货(采购)价
$DrugStoreRelations->status = 2;//上架
$DrugStoreRelations->created_at = date('Y-m-d H:i:s', time());
$DrugStoreRelations->updated_at = date('Y-m-d H:i:s', time());
$DrugStoreRelations->saveOrFail();
}else{
$DrugStoreRelations = new DrugStoreRelations();
$DrugStoreRelations->drug_id = $post['drug_id'];
$DrugStoreRelations->store_id = $val['id'];
$DrugStoreRelations->price =bcdiv(bcmul($post['price'], $val['g_sale_percent']??100, 4), 100, 4);//销售价
$DrugStoreRelations->buy_price = bcdiv(bcmul($post['market_price'], $val['g_bug_percent']??100, 4), 100, 4);//进货(采购)价
$DrugStoreRelations->status = 2;//上架
$DrugStoreRelations->created_at = date('Y-m-d H:i:s', time());
$DrugStoreRelations->updated_at = date('Y-m-d H:i:s', time());
$DrugStoreRelations->saveOrFail();
}
}
}
if ($post['type']==2|| $post['type']==4){
foreach ($Store as $val) {
$DrugStoreRelations = new DrugStoreRelations();
$DrugStoreRelations->drug_id = $post['drug_id'];
$DrugStoreRelations->store_id = $val['id'];
$DrugStoreRelations->price =$post['price'];//销售价
$DrugStoreRelations->buy_price = $post['market_price']??$post['price'];//进货(采购)价
$DrugStoreRelations->status = 2;//上架
$DrugStoreRelations->created_at = date('Y-m-d H:i:s', time());
$DrugStoreRelations->updated_at = date('Y-m-d H:i:s', time());
$DrugStoreRelations->saveOrFail();
}
}
$t->commit();
return ['添加成功'];
} catch (\Exception $e) {
$t->rollBack();
throw new Exception($e->getMessage());
}
}
/**
* @doc-name 仓库上架下架
* @doc-param int drug_id 药品ID
* @doc-param int type 类型1中药2西药3颗粒配方4中成药
* @doc-param int status 1下架2上架
*/
public function actionIsSale()
{
$post = \Yii::$app->request->post();
$this->requestValidate($post, [
[['drug_id', 'type', 'status'], 'required']
]);
$DrugStoreDrug = DrugStoreDrug::find()->where([
'drug_id' => $post['drug_id'],
'drugstore_id' => 1,
'type' => $post['type'],
])->one();
if (!$DrugStoreDrug) {
throw new Exception('药品不存在');
}
$t = \Yii::$app->db->beginTransaction();
try {
$DrugStoreDrug->status= $post['status'];
$DrugStoreDrug->saveOrFail();
$ids = DrugStoreRelations::find()->select(['id'])->where([
'drug_id' => $post['drug_id']
])->column();
DrugStoreRelations::updateAll(['status' => $post['status']], ['id' => $ids]);//修改门店
$t->commit();
return ['success'];
} catch (\Exception $e) {
$t->rollBack();
throw new Exception($e->getMessage());
}
}
/**
* @doc-name 修改仓库价格
* @doc-param int drug_id 药品ID
* @doc-param int type 类型1中药2西药3颗粒配方4中成药
* @doc-param float price 销售价 / optional
* @doc-param int stock 库存 / optional
* @doc-param float market_price 市场价 / optional
*/
public function actionUpdateDrugstorePrice()
{
$post = \Yii::$app->request->post();
$this->requestValidate($post, [
[['drug_id', 'type'], 'required']
]);
$DrugStoreDrug = DrugStoreDrug::find()->where([
'drugstore_id' => 1,
'drug_id' => $post['drug_id'],
'type' => $post['type'],
])->one();
if (!$DrugStoreDrug) throw new Exception('药品不存在');
$t = \Yii::$app->db->beginTransaction();
try {
$DrugStoreDrug->price = $post['price'] ?? $DrugStoreDrug->price;
$DrugStoreDrug->stock = $post['stock'] ?? $DrugStoreDrug->stock;
$DrugStoreDrug->market_price = $post['market_price'] ?? $DrugStoreDrug->market_price;
$DrugStoreDrug->saveOrFail();
switch ($post['type']) {
case 1://中药
$DrugStoreRelations = DrugStoreRelations::find()->where(['drug_id' => $post['drug_id']])->asArray()->all();
if ($DrugStoreRelations) {
foreach ($DrugStoreRelations as $val) {
$store = Store::find()->where(['id' => $val['store_id']])->one();
\Yii::$app->db->createCommand()->update('yii_drug_store_relations',
['price' =>bcdiv(bcmul($store['z_sale_percent']??100,$DrugStoreDrug->price,4),100,4),
'buy_price' =>bcdiv(bcmul($store['z_buy_percent']??100,$DrugStoreDrug->market_price,4),100,4)],
['drug_id' => $val['drug_id'], 'store_id' => $val['store_id']])->execute();
}
}
break;
case 2://西药
$DrugStoreRelations = DrugStoreRelations::find()->where(['drug_id' => $post['drug_id']])->asArray()->all();
if ($DrugStoreRelations) {
foreach ($DrugStoreRelations as $val) {
\Yii::$app->db->createCommand()->update('yii_drug_store_relations',
['price' =>$DrugStoreDrug->price,
'buy_price' => $DrugStoreDrug->market_price??0],
['drug_id' => $val['drug_id'], 'store_id' => $val['store_id']])->execute();
}
}
break;
case 3://配方颗粒
$DrugStoreRelations = DrugStoreRelations::find()->where(['drug_id' => $post['drug_id']])->asArray()->all();
if ($DrugStoreRelations) {
foreach ($DrugStoreRelations as $val) {
$store = Store::find()->where(['id' => $val['store_id']])->one();
\Yii::$app->db->createCommand()->update('yii_drug_store_relations',
['price' =>bcdiv(bcmul($store['g_sale_percent']??100,$DrugStoreDrug->price,4),100,4),
'buy_price' =>bcdiv(bcmul($store['g_buy_percent']??100,$DrugStoreDrug->market_price,4),100,4)],
['drug_id' => $val['drug_id'], 'store_id' => $val['store_id']])->execute();
}
}
break;
case 4://中成药
$DrugStoreRelations = DrugStoreRelations::find()->where(['drug_id' => $post['drug_id']])->asArray()->all();
if ($DrugStoreRelations) {
foreach ($DrugStoreRelations as $val) {
\Yii::$app->db->createCommand()->update('yii_drug_store_relations',
['price' =>$DrugStoreDrug->price,
'buy_price' => $DrugStoreDrug->market_price??$DrugStoreDrug->price],
['drug_id' => $val['drug_id'], 'store_id' => $val['store_id']])->execute();
}
}
break;
default:
throw new Exception('参数错误');
}
$t->commit();
return ['修改成功'];
} catch (\Exception $e) {
$t->rollBack();
throw new Exception($e->getMessage());
}
}
/**
* @doc-name 仓库修改门店西药价格
* @doc-param int store_id 门店id
* @doc-param int drug_id 药品ID
* @doc-param float price 销售价 / optional
* @doc-param float buy_price 采购价 / optional
*/
public function actionUpdatePrice()
{
$post = \Yii::$app->request->post();
$this->requestValidate($post, [
[['store_id', 'drug_id'], 'required']
]);
if (empty($post['drug_id'])) {
throw new Exception('药品ID不能为空');
}
$DrugStoreRelations = DrugStoreRelations::find()->where([
'store_id' => $post['store_id'],
'drug_id' => $post['drug_id']
])->one();
if (!$DrugStoreRelations) throw new Exception('药品不存在');
$DrugStoreRelations->price = $post['price'] ?? $DrugStoreRelations->price;
$DrugStoreRelations->buy_price = $post['buy_price'] ?? $DrugStoreRelations->buy_price;
if (!$DrugStoreRelations->saveOrFail()) throw new Exception('修改失败');
return ['修改成功'];
}
/**
* @doc-name 仓库修改门店比例(中药/配方颗粒)
* @doc-param int store_id 门店ID
* @doc-param int type 类别1中药3配方颗粒
* @doc-return string z_buy_percent 中药采购比例 / optional
* @doc-return string z_sale_percent 中药销售比例 / optional
* @doc-return string g_bug_percent 配方颗粒采购比例 / optional
* @doc-return string g_sale_percent 配方颗粒销售比例 / optional
*/
public function actionEditPercent()
{
$post = \Yii::$app->request->post();
$this->requestValidate($post, [
[['store_id', 'type'], 'required']
]);
switch ($post['type']) {
case 1:
$beginTransaction = \Yii::$app->db->beginTransaction();
try {
$Store = Store::find()->where([
'id' => $post['store_id']
])->one();
if (!$Store) throw new Exception('门店不存在');
$Store->z_buy_percent = $post['z_buy_percent'] ?? $Store->z_buy_percent;
$Store->z_sale_percent = $post['z_sale_percent'] ?? $Store->z_sale_percent;
$Store->saveOrFail();
$chinese = DrugStoreDrug::find()->select(['drug_id'])->where(['type' => 1])->column();
$chinese_price = DrugStoreDrug::find()->select(['drug_id', 'market_price','price'])->where(['type' => 1])->all();
$DrugStoreRelations_chinese = DrugStoreRelations::find()->select(['id', 'drug_id'])->where([
'store_id' => $post['store_id'],
])->andWhere(['in', 'drug_id', $chinese])->all();
foreach ($DrugStoreRelations_chinese as $val) {
foreach ($chinese_price as $v) {
if ($val['drug_id'] == $v['drug_id']) {
\Yii::$app->db->createCommand()->update('yii_drug_store_relations',
['price' =>bcdiv(bcmul($post['z_sale_percent'] ??100,$v['price'],4),100,4) ,
'buy_price' =>bcdiv(bcmul($post['z_buy_percent'] ??100,$v['market_price'],4),100,4)],
['id' => $val['id']])->execute();
}
}
}
$beginTransaction->commit();
return ['修改成功'];
} catch (\Exception $e) {
$beginTransaction->rollBack();
throw new Exception($e->getMessage());
}
break;
case 3:
try {
$t = \Yii::$app->db->beginTransaction();
$Store = Store::find()->where([
'id' => $post['store_id']
])->one();
if (!$Store) throw new Exception('门店不存在');
$Store->g_bug_percent = $post['g_bug_percent'] ?? $Store->g_bug_percent;
$Store->g_sale_percent = $post['g_sale_percent'] ?? $Store->g_sale_percent;
$Store->saveOrFail();
$grain = DrugStoreDrug::find()->select(['drug_id'])->where(['type' => 3])->column();
$grain_price = DrugStoreDrug::find()->select(['drug_id', 'market_price','price'])->where(['type' => 3])->all();
$DrugStoreRelations_grain = DrugStoreRelations::find()->select(['id', 'drug_id'])->where([
'store_id' => $post['store_id'],
])->andWhere(['in', 'drug_id', $grain])->all();
foreach ($DrugStoreRelations_grain as $value) {
foreach ($grain_price as $vv) {
if ($value['drug_id'] == $vv['drug_id']) {
\Yii::$app->db->createCommand()->update('yii_drug_store_relations',
['price' =>bcdiv(bcmul($post['g_sale_percent'] ??100,$vv['price'],4),100,4),
'buy_price' =>bcdiv(bcmul($post['g_bug_percent']??100 ,$vv['market_price'],4),100,4)],
['id' => $value['id']])->execute();
}
}
}
$t->commit();
return ['修改成功'];
} catch (\Exception $e) {
$t->rollBack();
throw new Exception($e->getMessage());
}
break;
default:
throw new Exception('参数错误');
}
}
/**
* @doc-name 仓库查看比例
* @doc-param int store_id 门店ID
* @doc-return string z_buy_percent 中药采购比例
* @doc-return string z_sale_percent 中药销售比例
* @doc-return string g_bug_percent 配方颗粒采购比例
* @doc-return string g_sale_percent 配方颗粒销售比例
*/
public function actionPercent()
{
$get = \Yii::$app->request->get();
$this->requestValidate($get, [
['store_id', 'required']
]);
$Store = Store::find()->where([
'id' => $get['store_id']
])->one();
if (!$Store) {
throw new Exception('门店不存在');
}
return $Store;
}
/**
* @doc-name 仓库发货
* @doc-param int order_id 商品(或产品)订单id
* @doc-param string express_company_name 快递公司 / optional
* @doc-param string express_company_code 公司编码
* @doc-param string express_no 快递单号
* @doc-param string mobile 手机号码 / optional
* @doc-param string state 物流状态 / optional
*/
public function actionWareSend()
{
$post = \Yii::$app->request->post();
$this->requestValidate($post, [
[['order_id', 'express_company_code', 'express_no'], 'required']
]);
$user = \Yii::$app->user->identity;
// if ($user->role != UserRoleEnum::SUPER_ADMIN) {
// throw new Exception('您不是超管,没有发货权限');
// }
$ProductOrder = ProductOrder::find()->where([
'id' => $post['order_id']
])->one();
if (!$ProductOrder) throw new Exception('产品订单不存在');
if($ProductOrder->is_online == 1 && $ProductOrder->online_prescription_status != 1){
throw new Exception('处方暂未审核,无法发货');
}
$t = \Yii::$app->db->beginTransaction();
try {
//快递单号信息
$ExpressNos = new ExpressNos();
$ExpressNos->express_company_name = $post['express_company_name'];
$ExpressNos->express_company_code = $post['express_company_code'];
$ExpressNos->express_no = $post['express_no'];
$ExpressNos->mobile = $post['mobile'];
$ExpressNos->state = $post['state']?? 0;
$ExpressNos->sync_at = null;
$ExpressNos->created_at = date('Y-m-d H:i:s', time());
$ExpressNos->updated_at = date('Y-m-d H:i:s', time());
$ExpressNos->saveOrFail();
// $result = json_decode($response->getBody(), true);
$ProductOrder->is_send = 1;
$ProductOrder->status = 2;
$ProductOrder->send_time = date('Y-m-d H:i:s', time());
$ProductOrder->express_no_id = $ExpressNos->attributes['id'];
$ProductOrder->saveOrFail();
$log = new Log();
$log->admin_id = $user->uid;
$log->operate_time = date('Y-m-d H:i:s', time());
$log->type = '仓库发货';
$log->mold = 2;//订单操作
$log->content = $user->uid . '操作仓库发货:';
$log->save();
//订单发货自动结算
\Yii::$app->queue->push(new ProductOrderSend([
'orderId' => $ProductOrder->id,
]));
//订单自动收货- 发货7天后
$config = \Yii::$app->params;
// $orderAutoReceivedTime = isset($config['product_order']['received_time']) ? $config['product_order']['received_time'] : 7*24*3600;
$orderAutoReceivedTime = isset($config['product_order']['received_time']) ? $config['product_order']['received_time'] : 1*3600;
\Yii::$app->queue->delay($orderAutoReceivedTime)->push(new ProductOrderAutoReceivedJob([
'orderId' => $ProductOrder->id
]));
\Yii::$app->db->createCommand()->update('yii_log', ['content' => Json::encode($log->attributes)], ['id' => $log->attributes['id']])->execute();
//订单发货3天后分账结算
// if($ProductOrder->type == 2){ //易票联支付的产品订单
// $config = \Yii::$app->params;
// $orderAutoSettlementTime = isset($config['product_order']['settlement_time']) ? $config['product_order']['settlement_time'] : 72*3600;
// \Yii::$app->queue->delay($orderAutoSettlementTime)->push(new ProductOrderSendJob([
// 'orderId' => $ProductOrder->id
// ]));
// }
// //触发订单发货事件-3天后分账结算
// $event = new ProductOrderEvent();
// $event->order = $ProductOrder;
// $event->sender = $this;
// \Yii::$app->trigger(ProductOrder::EVENT_SEND, $event);
$t->commit();
(new Client(['http_errors' => false]))->get("https://shop.xiaokang88.com/t/express_update", [
'query' => [
'id' => $ExpressNos->id,
],
]);
return ['发货成功'];
} catch (\Exception $exception) {
$t->rollBack();
throw new Exception($exception->getMessage());
}
}
/**
* @doc-name 快递公司
* @doc-return mixed @List{id,name-string-公司名称,code-string-公司编码,type-string-公司类型,sort-int-排序} 快递公司
* @doc-return mixed @Pagination{total-int-总数量,totalPage-int-总页数,pageSize-int-每页条数} 分页数据
*/
public function actionExpressCompanyList()
{
$get = \Yii::$app->request->get();
$query = ExpressCompanies::find()->orderBy(['sort' => SORT_ASC]);
$this->field = [
ExpressCompanies::class => [
'id', 'name', 'code', 'type', 'sort'
]
];
return $this->create($query, $get);
}
/**
* @doc-name 修改物流
* @doc-param int express_no_id 快递单号信息ID
* @doc-param string express_company_name 公司名称 / optional
* @doc-param string express_company_code 公司编码 / optional
* @doc-param string express_no 快递单号 / optional
* @doc-param string mobile 手机号码 / optional
* @doc-param int state 物流状态0揽收1揽收2疑难4退签5派件6退回7转投8清关14拒签 / optional
*/
public function actionUpdateExpressNos()
{
$post = \Yii::$app->request->post();
$this->requestValidate($post, [
['express_no_id', 'required','message'=>'快递单号信息ID不能为空']
]);
$ExpressNos=ExpressNos::find()->where(['id'=>$post['express_no_id']])->one();
if (!$ExpressNos) throw new Exception('物流信息不存在');
$ExpressNos->express_company_name=$post['express_company_name']??$ExpressNos->express_company_name;
$ExpressNos->express_company_code=$post['express_company_code']?? $ExpressNos->express_company_code;
$ExpressNos->express_no=$post['express_no']??$ExpressNos->express_no;
$ExpressNos->mobile=$post['mobile']??$ExpressNos->mobile;
$ExpressNos->state=$post['state']??$ExpressNos->state;
$ExpressNos->saveOrFail();
(new Client(['http_errors' => false]))->get("https://shop.xiaokang88.com/t/express_update", [
'query' => [
'id' => $ExpressNos->id,
],
]);
return ['修改成功'];
}
}