ProductController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 = [];
  95. if (!empty($id)) {
  96. $ghs = GhsClass::getGhsInfo($id);
  97. if (empty($ghs)) {
  98. util::fail('没有找到供应商');
  99. }
  100. $sjId = $ghs['sjId'] ?? 0;
  101. $shopId = $ghs['shopId'] ?? 0;
  102. $customId = $ghs['customId'] ?? 0;
  103. if (!empty($customId)) {
  104. $custom = CustomClass::getById($customId, true);
  105. if (isset($custom->black) && $custom->black == CustomClass::IS_BLACK) {
  106. $ghsName = $ghs['name'] ?? '';
  107. util::fail('您已被' . $ghsName . '列入黑名单');
  108. }
  109. }
  110. } else {
  111. $shopId = $get['shopId'] ?? 0;
  112. if (empty($shopId)) {
  113. util::fail('没有供应商信息');
  114. }
  115. $shop = ShopClass::getShopInfo($shopId);
  116. if (empty($shop)) {
  117. util::fail('没有供应商门店');
  118. }
  119. $sjId = $shop['sjId'] ?? 0;
  120. }
  121. $connection = Yii::$app->db;//事务处理
  122. $transaction = $connection->beginTransaction();
  123. try {
  124. //获取所有当前供应商的分类 (全部、常用)
  125. //根据分类组装分类下的花材信息
  126. $classIds = ItemClassClass::getGhsItemClassAll($sjId);
  127. $where['sjId'] = $sjId;
  128. $where['shopId'] = $shopId;
  129. $where['delStatus'] = 0;
  130. $where['status'] = 1;
  131. //客户需要给算折扣
  132. Yii::$app->params['customDiscount'] = isset($ghs['giveDiscount']) ? $ghs['giveDiscount'] : 1;
  133. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], "*");
  134. $itemInfoData = ProductClass::groupProductInfo($itemInfoData);
  135. $data = ProductService::assembleData($classIds, $itemInfoData, true);
  136. util::success($data);
  137. } catch (\Exception $e) {
  138. $transaction->rollBack();
  139. util::fail();
  140. }
  141. }
  142. public function actionDetail()
  143. {
  144. $id = Yii::$app->request->get('id', 0);
  145. $respond = ProductClass::getItemInfo($id);
  146. util::success($respond);
  147. }
  148. public function actionUpdate()
  149. {
  150. $post = Yii::$app->request->post();
  151. ProductClass::updateProduct($post);
  152. util::complete();
  153. }
  154. //批量修改product 排序,成本价,加价
  155. public function actionBatchUpdate()
  156. {
  157. //data = [{id:1,inTurn:10,addPrice:2,cost:10}]
  158. $data = Yii::$app->request->post('data', '');
  159. $type = Yii::$app->request->post('type', '');
  160. if (empty($data) || empty($type)) {
  161. util::fail("参数错误");
  162. }
  163. if (in_array($type, ['inTurn', 'addPrice', 'cost', 'weight', 'ratio']))
  164. $data = json_decode($data, true);
  165. foreach ($data as $v) {
  166. $id = $v['id'];
  167. $val = $v[$type] ?? '';
  168. $where = [];
  169. $where['id'] = $id;
  170. $where['shopId'] = $this->shopId;
  171. $where['sjId'] = $this->sjId;
  172. if (empty($val)) {
  173. util::fail("参数错误1");
  174. }
  175. $data = [
  176. $type => $val
  177. ];
  178. ProductClass::updateByCondition($where, $data);
  179. ProductClass::autoPriceById($id);
  180. }
  181. util::complete();
  182. }
  183. //供应商产品的详情 shish 20210903
  184. public function actionGhsProductDetail()
  185. {
  186. $id = Yii::$app->request->get('id', 0);
  187. $ghsId = Yii::$app->request->get('ghsId', 0);
  188. $ghs = GhsClass::getById($ghsId, true);
  189. GhsClass::valid($ghs, $this->shopId);
  190. $shopId = $ghs->shopId ?? 0;
  191. $product = ProductClass::getItemInfo($id);
  192. if (isset($product['shopId']) == false || $product['shopId'] != $shopId) {
  193. util::fail('没有权限访问');
  194. }
  195. util::success($product);
  196. }
  197. }