ProductController.php 7.5 KB

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