Files
xk-api-yii/admin/controllers/store/WareController.php
2024-12-25 14:44:34 +08:00

963 lines
42 KiB
PHP

<?php
namespace admin\controllers\store;
use admin\components\BaseAdminController;
use admin\models\forms\drug\ImportForm;
use common\jobs\ProductOrderAutoReceivedJob;
use common\jobs\templateMessage\ProductOrderSend;
use common\models\oldDb\DrugStoreDrug;
use common\models\oldDb\DrugStoreRelations;
use common\models\oldDb\ExpressCompanies;
use common\models\oldDb\ExpressNos;
use common\models\oldDb\Log;
use common\models\oldDb\ProductOrder;
use common\models\oldDb\Store;
use common\services\ExportService;
use GuzzleHttp\Client;
use moonland\phpexcel\Excel;
use yii\db\Exception;
use yii\helpers\Json;
use yii\web\Response;
use yii\web\UploadedFile;
//use common\enums\UserRoleEnum;
//use common\jobs\ProductOrderSendJob;
/**
* @doc-module 仓库
*/
class WareController extends BaseAdminController
{
public $optional = ['api-ware-send'];
/**
* @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',
'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',
'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();
$isStore = $post['isStore']?? false;
$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 {
if (!$isStore) {
// 有争议
$DrugStoreDrug->status= $post['status'];
$DrugStoreDrug->saveOrFail();
$ids = DrugStoreRelations::find()->select(['id'])->where([
'drug_id' => $post['drug_id']
])->column();
} else {
$storeId = \Yii::$app->user->identity->store_id;
if (empty($storeId)) throw new Exception('门店获取失败');
$ids = DrugStoreRelations::find()->select(['id'])->where([
'drug_id' => $post['drug_id'],
'store_id' => $storeId
])->column();
}
if (!empty($ids)) {
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()
{
// TODO 开始修改这部分
$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();
// TODO 2024-10-31 删除了修改门店售价的操作,门店售价由门店自己设置
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',
[ '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',
[ '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',
['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',
[ '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
* @throws Exception
*/
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;
\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());
}
}
public function actionApiWareSend()
{
return ['发货成功'];
$order = \Yii::$app->request->post('order_id');
$orderAutoReceivedTime = isset($config['product_order']['received_time']) ? $config['product_order']['received_time'] : 7*24*3600;
\Yii::$app->queue->delay($orderAutoReceivedTime)->push(new ProductOrderAutoReceivedJob([
'orderId' => $order
]));
return ['发货成功'];
}
/**
* @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 ['修改成功'];
}
/**
* Excel导入批量修改商品价格
* @return array|array[]|string[]|void
*/
public function actionExcelUpdatePrice()
{
try {
$request = \Yii::$app->request;
if ($request->isPost) {
$post = \Yii::$app->request->post();
//设置最大执行时间
ini_set("max_execution_time", "360");
$file = UploadedFile::getInstanceByName('file');
if ($file->size > 5 * 1024 * 1024) {
return ['excel文件不能超过5M!'];
}
//文件名
$filename = date('YmdHi') . md5($file->getBaseName()) . mt_rand(1000, 9999) . '.' . $file->getExtension();
//保存文件
$file->saveAs($filename);
// $fullFileName = $absolute_path . $filename;
$data = Excel::import($filename, [
'setFirstRecordAsKeys' => true,
'getOnlySheet' => 'Sheet1',
]);
$ImportForm = new ImportForm();
$ImportForm->attributes = $post;
if ($request->post('is_store') == true) {
$admin = \Yii::$app->user->identity;
$storeId = $admin->store_id;
if (empty($storeId)) {
throw new Exception('店铺账号获取失败-'. $storeId);
}
return $ImportForm->ExportUpdateDrugPriceByStore($data, $storeId);
}
return $ImportForm->ExportUpdateDrugPrice($data);
}
} catch (\Exception $e) {
// 删除文件
@unlink($filename);
return [
'error' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTraceAsString()
];
} finally {
if (file_exists($filename)) {
unlink($filename);
}
}
}
/**
* @doc-name 导出药品价格
*/
public function actionExportDrugByWareStore()
{
\Yii::$app->response->format = Response::FORMAT_RAW;
$params = \Yii::$app->request->post();
$parameter['header'] = ['药品ID', '药品名称', '供货价', '建议零售价', '销售价格'];
$parameter['data'] = DrugStoreRelations::inventory($params, \Yii::$app->user->identity->store_id);
ExportService::ExportByCors($parameter);
}
/**
* @doc-name 导出药品价格
*/
public function actionExportDrugByWare()
{
\Yii::$app->response->format = Response::FORMAT_RAW;
$params = \Yii::$app->request->post();
$parameter['header'] = ['药品ID', '药品名称', '供货价', '建议零售价'];
$parameter['data'] = DrugStoreDrug::inventory($params, \Yii::$app->user->identity->store_id);
ExportService::ExportByCors($parameter);
}
/**
* @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 actionWareGoodListByStore()
{
$get = \Yii::$app->request->get();
$admin = \Yii::$app->user->identity;
$storeId = $admin->store_id;
$name = $get['name'];
// 获取门店信息
$store = Store::find()->where(['id' => $storeId])->one();
if (!$store) throw new Exception('门店信息不存在');
$query = DrugStoreRelations::find()->alias('dsr')->with(['drugStoreDrug', 'drug']);
$query->andWhere(['dsr.store_id' => $storeId]);
$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],
]);
}
}]);
$query->joinWith(['drugStoreDrug' => function ($drugStoreDrug) use ($get) {
if (!empty($get['type'])) {
$drugStoreDrug->alias('dsd')->where(['dsd.type' => $get['type']]);
}
}]);
if (!empty($get['status'])) {
$query->andWhere(['dsr.status' => $get['status']]);
}
$this->field = [
DrugStoreRelations::class => [
'id',
'drug_id',
'type' => 'drugStoreDrug.type',
'stock',
'price',
'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',
'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 actionSaveWareDrugByStore()
{
// TODO 未做,等确认需求
$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 float price 销售价 / optional
* @doc-param int stock 库存 / optional
* @doc-param float market_price 市场价 / optional
*/
public function actionUpdateDrugstorePriceByStore()
{
// TODO 开始修改这部分
$post = \Yii::$app->request->post();
$admin = \Yii::$app->user->identity;
$storeId = $admin->store_id;
if (empty($storeId)) throw new Exception('门店信息获取失败');
$this->requestValidate($post, [
[['drug_id', 'type'], 'required']
]);
$DrugStoreRelations = DrugStoreRelations::find()->where([
'store_id' => $storeId,
'drug_id' => $post['drug_id'],
])->one();
if (!$DrugStoreRelations) throw new Exception('药品不存在');
try {
$DrugStoreRelations->price = $post['price'] ?? $DrugStoreRelations->price;
$DrugStoreRelations->stock = $post['stock'] ?? $DrugStoreRelations->stock;
$DrugStoreRelations->saveOrFail();
return ['修改成功'];
} catch (\Exception $e) {
throw new Exception($e->getMessage());
}
}
}