ProductController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * User: admin
  4. * Date Time: 2021/3/2 14:34
  5. */
  6. namespace hd\controllers;
  7. use biz\ghs\classes\GhsClass;
  8. use biz\shop\classes\ShopClass;
  9. use bizGhs\admin\classes\AdminClass;
  10. use bizGhs\custom\classes\CustomClass;
  11. use bizGhs\item\classes\ItemClassClass;
  12. use bizGhs\product\classes\ProductClass;
  13. use bizGhs\product\services\ProductService;
  14. use bizGhs\item\services\ItemService;
  15. use common\components\dict;
  16. use common\components\util;
  17. use Yii;
  18. class ProductController extends BaseController
  19. {
  20. public $guestAccess = ['ghs-item'];
  21. public function actionIndex()
  22. {
  23. $py = Yii::$app->request->get('py', '');
  24. $type = Yii::$app->request->get('type', '');
  25. //获取所有当前供应商的分类 (全部、常用)
  26. //根据分类组装分类下的花材信息
  27. $classIds = ItemClassClass::getGhsItemClassAll($this->sjId);
  28. $where['sjId'] = $this->sjId;
  29. if ($py) {
  30. $pyName = strtolower($py);
  31. $where['py'] = $pyName;
  32. }
  33. $shopId = $this->shopId;
  34. //库存预警
  35. if ($type == 'waring') {
  36. $shopId = Yii::$app->request->get('shopId', 0);
  37. if (!$shopId) {
  38. $shopId = $this->shopId;
  39. }
  40. }
  41. $where['shopId'] = $shopId;
  42. $where['delStatus'] = 0;
  43. $itemInfoData = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  44. $itemInfoData = ProductClass::groupProductInfo($itemInfoData);
  45. $data = ProductService::assembleData($classIds, $itemInfoData);
  46. util::success($data);
  47. }
  48. //当前门店所有花材分类
  49. public function actionList()
  50. {
  51. $classId = Yii::$app->request->get('classId', 0);
  52. $pyName = Yii::$app->request->get('py', '');
  53. $where['sjId'] = $this->sjId;
  54. if ($pyName) {
  55. $pyName = strtolower($pyName);
  56. $where['py'] = $pyName;
  57. }
  58. $shopId = Yii::$app->request->get('shopId', 0);
  59. if (!$shopId) {
  60. $shopId = $this->shopId;
  61. }
  62. $where['shopId'] = $shopId;
  63. if (!empty($classId)) {
  64. $where['classId'] = $classId;
  65. }
  66. $type = Yii::$app->request->get('type', '');
  67. $showPage = Yii::$app->request->get('showPage', 0);
  68. //需要显示分页
  69. if ($showPage) {
  70. $respond = ProductClass::getList("*", $where, "inTurn desc");
  71. $respond['list'] = ProductClass::groupClassName($this->sjId, $respond['list']);
  72. $respond['list'] = ProductClass::groupProductInfo($respond['list']);
  73. util::success($respond);
  74. }
  75. $data = ProductClass::getAllByCondition($where, "inTurn desc", "*");
  76. $data = ProductClass::groupProductInfo($data);
  77. if ($type == 'waring') {
  78. //库存预警
  79. $newData = [];
  80. foreach ($data as $v) {
  81. if ($v['stock'] <= $v['stockWarning']) {
  82. $newData[] = $v;
  83. }
  84. }
  85. util::success(['list' => $newData]);
  86. }
  87. util::success(['list' => $data]);
  88. }
  89. //从零售端采购入库,查看供应商的某个门店的product
  90. public function actionGhsItem()
  91. {
  92. $get = Yii::$app->request->get();
  93. $id = $get['id'] ?? 0;
  94. $ghs = GhsClass::getGhsInfo($id);
  95. if (empty($ghs)) {
  96. util::fail('没有找到供应商');
  97. }
  98. $sjId = $ghs['sjId'] ?? 0;
  99. $shopId = $ghs['shopId'] ?? 0;
  100. $black = $ghs['black'] ?? 2;
  101. if ($black == 2) {
  102. util::fail('您已被列入黑名单');
  103. }
  104. $connection = Yii::$app->db;//事务处理
  105. $transaction = $connection->beginTransaction();
  106. try {
  107. //获取所有当前供应商的分类 (全部、常用)
  108. //根据分类组装分类下的花材信息
  109. $classIds = ItemClassClass::getGhsItemClassAll($sjId);
  110. $where['sjId'] = $sjId;
  111. $where['shopId'] = $shopId;
  112. $where['delStatus'] = 0;
  113. $where['status'] = 1;
  114. $level = $ghs['giveLevel'] ?? 0;
  115. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], "*");
  116. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  117. $data = ProductService::assembleData($classIds, $itemInfoData, true);
  118. util::success($data);
  119. } catch (\Exception $e) {
  120. $transaction->rollBack();
  121. util::fail();
  122. }
  123. }
  124. public function actionDetail()
  125. {
  126. $id = Yii::$app->request->get('id', 0);
  127. $respond = ProductClass::getItemInfo($id);
  128. util::success($respond);
  129. }
  130. public function actionUpdate()
  131. {
  132. $post = Yii::$app->request->post();
  133. ProductClass::updateProduct($post);
  134. util::complete();
  135. }
  136. //批量修改product 排序,成本价,加价
  137. public function actionBatchUpdate()
  138. {
  139. //data = [{id:1,inTurn:10,addPrice:2,cost:10}]
  140. $data = Yii::$app->request->post('data', '');
  141. $type = Yii::$app->request->post('type', '');
  142. if (empty($data) || empty($type)) {
  143. util::fail("参数错误");
  144. }
  145. if (in_array($type, ['inTurn', 'addPrice', 'cost', 'weight', 'ratio']))
  146. $data = json_decode($data, true);
  147. foreach ($data as $v) {
  148. $id = $v['id'];
  149. $val = $v[$type] ?? '';
  150. $where = [];
  151. $where['id'] = $id;
  152. $where['shopId'] = $this->shopId;
  153. $where['sjId'] = $this->sjId;
  154. if (empty($val)) {
  155. util::fail("参数错误1");
  156. }
  157. $data = [
  158. $type => $val
  159. ];
  160. ProductClass::updateByCondition($where, $data);
  161. ProductClass::autoPriceById($id);
  162. }
  163. util::complete();
  164. }
  165. //供应商产品的详情 shish 20210903
  166. public function actionGhsProductDetail()
  167. {
  168. $id = Yii::$app->request->get('id', 0);
  169. $ghsId = Yii::$app->request->get('ghsId', 0);
  170. $ghs = GhsClass::getById($ghsId, true);
  171. GhsClass::valid($ghs, $this->shopId);
  172. $shopId = $ghs->shopId ?? 0;
  173. $product = ProductClass::getItemInfo($id);
  174. if (isset($product['shopId']) == false || $product['shopId'] != $shopId) {
  175. util::fail('没有权限访问');
  176. }
  177. util::success($product);
  178. }
  179. }