| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\controllers\product_traceability;
- use app\models\Goods;
- use app\modules\admin\controllers\BaseController;
- use app\modules\admin\models\GoodsForm;
- use app\plugins\product_traceability\models\Product;
- use app\plugins\product_traceability\models\ProductBatch;
- use app\utils\QrCode;
- use yii\base\BaseObject;
- use yii\helpers\Json;
- class ProductBatchController extends BaseController
- {
- public function actionProductList()
- {
- $list = Product::find()->where([
- 'is_delete' => 0,
- 'store_id' => get_store_id(),
- 'is_show' => 1,
- ])->all();
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list,
- ],
- ]);
- }
- public function actionProductBatchList()
- {
- $start_time = get_params('dateStart');
- $end_time = get_params('dateEnd');
- $search_product_name_key = get_params('search_product_name_key');
- $search_batch_name_key = get_params('search_batch_name_key');
- $search_goods_name_key = get_params('search_goods_name_key');
- $search_state = get_params('search_state', '-1');
- $query = ProductBatch::find()->alias('pb')
- ->leftJoin(['p' => Product::tableName()], 'p.id=pb.product_id')
- ->leftJoin(['g' => Goods::tableName()], 'g.id=pb.goods_id')
- ->where([
- 'pb.is_delete' => 0,
- 'pb.store_id' => get_store_id()
- ])->select(['pb.*', 'p.product_name', 'g.name as goods_name']);
- if (!empty($search_product_name_key)) {
- $query->andWhere(['like', 'p.product_name', $search_product_name_key]);
- }
- if (!empty($search_batch_name_key)) {
- $query->andWhere(['like', 'pb.batch_name', $search_batch_name_key]);
- }
- if (!empty($search_goods_name_key)) {
- $query->andWhere(['like', 'g.name', $search_goods_name_key]);
- }
- if ($search_state != -1) {
- $query->andWhere(['pb.state' => $search_state]);
- }
- if ($start_time) {
- $query->andWhere(['>=', 'pb.created_at', strtotime($start_time)]);
- }
- if ($end_time) {
- $query->andWhere(['<=', 'pb.created_at', strtotime($end_time)]);
- }
- $list = pagination_make($query);
- $list= $list['list'];
- foreach ($list as $k => $v) {
- $goods_id_list = [$v['goods_id']];
- $list[$k]['goods_list'] = GoodsForm::getGoodsListById($goods_id_list);
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list,
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount'],
- ],
- ]);
- }
- public function actionProductBatchDel()
- {
- $id = get_params('id');
- $product_batch = ProductBatch::findOne($id);
- $product_batch->is_delete = 1;
- if ($product_batch->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '删除成功'
- ]);
- }else {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $product_batch->getErrors(),
- ]);
- }
- }
- /**
- * 批量操作
- * @return \yii\web\Response
- */
- public function actionProductBatchOperate()
- {
- $id = post_params('id');
- $type = post_params('type');
- $status = post_params('status');
- if (empty($id) || !is_array($id)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '参数有误'
- ]);
- }
- if (!in_array($status, [0, 1])) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '状态参数有误'
- ]);
- }
- if ($type == 'open' || $type == 'disabled') {
- ProductBatch::updateAll(['is_show' => $status], ['in', 'id', $id]);
- }
- if ($type == 'delete') {
- $res= ProductBatch::updateAll(['is_delete' => $status], ['in', 'id', $id]);
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => '更新成功'
- ]);
- }
- public function actionProductBatchEdit()
- {
- $store_id = get_store_id();
- $id = post_params('id', 0);
- $batch_name = post_params('batch_name');
- $product_id = post_params('product_id', 0);
- $goods_id = post_params('goods_id', 0);
- if ($id > 0) {
- $product_batch = ProductBatch::findOne($id);
- } else {
- $product_batch = new ProductBatch();
- while (true) {
- $batch_number = date('YmdHis') . mt_rand(100000, 999999);
- $exist_batch_number = ProductBatch::find()->where(['batch_number' => $batch_number])->exists();
- if (!$exist_batch_number) {
- break;
- }
- }
- $product_batch->batch_number = $batch_number;
- }
- $product_batch->store_id = $store_id;
- $product_batch->product_id = $product_id;
- $product_batch->goods_id = $goods_id;
- $product_batch->batch_name = $batch_name;
- if ($product_batch->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => $id > 0 ? '编辑成功' : '添加成功',
- ]);
- }else{
- return $this->asJson([
- 'code' => 1,
- 'msg' => $product_batch->getErrors(),
- ]);
- }
- }
- }
|