ProductController.php 7.2 KB

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