ProductBatchController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\controllers\product_traceability;
  8. use app\models\Goods;
  9. use app\modules\admin\controllers\BaseController;
  10. use app\modules\admin\models\GoodsForm;
  11. use app\plugins\product_traceability\models\Product;
  12. use app\plugins\product_traceability\models\ProductBatch;
  13. use app\utils\QrCode;
  14. use yii\base\BaseObject;
  15. use yii\helpers\Json;
  16. class ProductBatchController extends BaseController
  17. {
  18. public function actionProductList()
  19. {
  20. $list = Product::find()->where([
  21. 'is_delete' => 0,
  22. 'store_id' => get_store_id(),
  23. 'is_show' => 1,
  24. ])->all();
  25. return $this->asJson([
  26. 'code' => 0,
  27. 'msg' => 'success',
  28. 'data' => [
  29. 'data' => $list,
  30. ],
  31. ]);
  32. }
  33. public function actionProductBatchList()
  34. {
  35. $start_time = get_params('dateStart');
  36. $end_time = get_params('dateEnd');
  37. $search_product_name_key = get_params('search_product_name_key');
  38. $search_batch_name_key = get_params('search_batch_name_key');
  39. $search_goods_name_key = get_params('search_goods_name_key');
  40. $search_state = get_params('search_state', '-1');
  41. $query = ProductBatch::find()->alias('pb')
  42. ->leftJoin(['p' => Product::tableName()], 'p.id=pb.product_id')
  43. ->leftJoin(['g' => Goods::tableName()], 'g.id=pb.goods_id')
  44. ->where([
  45. 'pb.is_delete' => 0,
  46. 'pb.store_id' => get_store_id()
  47. ])->select(['pb.*', 'p.product_name', 'g.name as goods_name']);
  48. if (!empty($search_product_name_key)) {
  49. $query->andWhere(['like', 'p.product_name', $search_product_name_key]);
  50. }
  51. if (!empty($search_batch_name_key)) {
  52. $query->andWhere(['like', 'pb.batch_name', $search_batch_name_key]);
  53. }
  54. if (!empty($search_goods_name_key)) {
  55. $query->andWhere(['like', 'g.name', $search_goods_name_key]);
  56. }
  57. if ($search_state != -1) {
  58. $query->andWhere(['pb.state' => $search_state]);
  59. }
  60. if ($start_time) {
  61. $query->andWhere(['>=', 'pb.created_at', strtotime($start_time)]);
  62. }
  63. if ($end_time) {
  64. $query->andWhere(['<=', 'pb.created_at', strtotime($end_time)]);
  65. }
  66. $list = pagination_make($query);
  67. $list= $list['list'];
  68. foreach ($list as $k => $v) {
  69. $goods_id_list = [$v['goods_id']];
  70. $list[$k]['goods_list'] = GoodsForm::getGoodsListById($goods_id_list);
  71. }
  72. return $this->asJson([
  73. 'code' => 0,
  74. 'msg' => 'success',
  75. 'data' => [
  76. 'data' => $list,
  77. 'pageNo' => $list['pageNo'],
  78. 'totalCount' => $list['totalCount'],
  79. ],
  80. ]);
  81. }
  82. public function actionProductBatchDel()
  83. {
  84. $id = get_params('id');
  85. $product_batch = ProductBatch::findOne($id);
  86. $product_batch->is_delete = 1;
  87. if ($product_batch->save()) {
  88. return $this->asJson([
  89. 'code' => 0,
  90. 'msg' => '删除成功'
  91. ]);
  92. }else {
  93. return $this->asJson([
  94. 'code' => 1,
  95. 'msg' => $product_batch->getErrors(),
  96. ]);
  97. }
  98. }
  99. /**
  100. * 批量操作
  101. * @return \yii\web\Response
  102. */
  103. public function actionProductBatchOperate()
  104. {
  105. $id = post_params('id');
  106. $type = post_params('type');
  107. $status = post_params('status');
  108. if (empty($id) || !is_array($id)) {
  109. return $this->asJson([
  110. 'code' => 1,
  111. 'msg' => '参数有误'
  112. ]);
  113. }
  114. if (!in_array($status, [0, 1])) {
  115. return $this->asJson([
  116. 'code' => 1,
  117. 'msg' => '状态参数有误'
  118. ]);
  119. }
  120. if ($type == 'open' || $type == 'disabled') {
  121. ProductBatch::updateAll(['is_show' => $status], ['in', 'id', $id]);
  122. }
  123. if ($type == 'delete') {
  124. $res= ProductBatch::updateAll(['is_delete' => $status], ['in', 'id', $id]);
  125. }
  126. return $this->asJson([
  127. 'code' => 0,
  128. 'msg' => '更新成功'
  129. ]);
  130. }
  131. public function actionProductBatchEdit()
  132. {
  133. $store_id = get_store_id();
  134. $id = post_params('id', 0);
  135. $batch_name = post_params('batch_name');
  136. $product_id = post_params('product_id', 0);
  137. $goods_id = post_params('goods_id', 0);
  138. if ($id > 0) {
  139. $product_batch = ProductBatch::findOne($id);
  140. } else {
  141. $product_batch = new ProductBatch();
  142. while (true) {
  143. $batch_number = date('YmdHis') . mt_rand(100000, 999999);
  144. $exist_batch_number = ProductBatch::find()->where(['batch_number' => $batch_number])->exists();
  145. if (!$exist_batch_number) {
  146. break;
  147. }
  148. }
  149. $product_batch->batch_number = $batch_number;
  150. }
  151. $product_batch->store_id = $store_id;
  152. $product_batch->product_id = $product_id;
  153. $product_batch->goods_id = $goods_id;
  154. $product_batch->batch_name = $batch_name;
  155. if ($product_batch->save()) {
  156. return $this->asJson([
  157. 'code' => 0,
  158. 'msg' => $id > 0 ? '编辑成功' : '添加成功',
  159. ]);
  160. }else{
  161. return $this->asJson([
  162. 'code' => 1,
  163. 'msg' => $product_batch->getErrors(),
  164. ]);
  165. }
  166. }
  167. }