ProductController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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\item\classes\PtItemClass;
  9. use bizGhs\item\classes\ItemClassClass;
  10. use bizGhs\product\classes\ProductClass;
  11. use bizGhs\product\services\ProductService;
  12. use common\components\util;
  13. use Yii;
  14. class ProductController extends BaseController
  15. {
  16. public $guestAccess = ['ghs-item'];
  17. public function actionIndex()
  18. {
  19. $py = Yii::$app->request->get('py', '');
  20. //默认显示上架商品
  21. $status = Yii::$app->request->get('status', 1);
  22. //默认显示未删除商品
  23. $delStatus = Yii::$app->request->get('delStatus', 0);
  24. //获取所有当前供应商的分类 (全部、常用)
  25. //根据分类组装分类下的花材信息
  26. //中央ID
  27. $classIds = ItemClassClass::getGhsItemClassAll($this->mainId);
  28. $where['mainId'] = $this->mainId;
  29. if ($py) {
  30. $pyName = strtolower($py);
  31. $where['py'] = $pyName;
  32. }
  33. if (!empty($status)) {
  34. $where['status'] = $status;
  35. }
  36. $where['delStatus'] = $delStatus;
  37. $level = 0;
  38. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], "*");
  39. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  40. $data = ProductService::assembleData($classIds, $itemInfoData, true);
  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. //中央ID
  105. $classIds = ItemClassClass::getGhsItemClassAll($this->mainId);
  106. // $where['sjId'] = $sjId;
  107. // $where['shopId'] = $shopId;
  108. $where['mainId'] = $this->mainId;
  109. $where['delStatus'] = 0;
  110. $where['status'] = 1;
  111. $level = $ghs['giveLevel'] ?? 0;
  112. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], "*");
  113. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  114. $data = ProductService::assembleData($classIds, $itemInfoData, true);
  115. util::success($data);
  116. } catch (\Exception $e) {
  117. $transaction->rollBack();
  118. util::fail();
  119. }
  120. }
  121. public function actionDetail()
  122. {
  123. $id = Yii::$app->request->get('id', 0);
  124. $respond = ProductClass::getItemInfo($id);
  125. $ptItemId = $respond['itemId'] ?? 0;
  126. $ptItemInfo = PtItemClass::getById($ptItemId);
  127. $respond['ptItemInfo'] = $ptItemInfo;
  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, $ghs);
  174. if (isset($product['shopId']) == false || $product['shopId'] != $shopId) {
  175. util::fail('没有权限访问');
  176. }
  177. $original = ProductClass::getById($id);
  178. if (isset($original['discountPrice']) && $original['discountPrice'] > 0) {
  179. $discountPrice = $original['discountPrice'];
  180. $original['price'] = floatval($discountPrice);
  181. $skAddPrice = $original['skAddPrice'] ?? 0;
  182. $addPrice = $original['addPrice'] ?? 0;
  183. $hjAddPrice = $original['hjAddPrice'] ?? 0;
  184. $zsAddPrice = $original['zsAddPrice'] ?? 0;
  185. $sk = bcsub($addPrice, $skAddPrice, 2);
  186. $hj = bcsub($addPrice, $hjAddPrice, 2);
  187. $zs = bcsub($addPrice, $zsAddPrice, 2);
  188. $original['skPrice'] = floatval(bcsub($discountPrice, $sk, 2));
  189. $original['hjPrice'] = floatval(bcsub($discountPrice, $hj, 2));
  190. $original['zsPrice'] = floatval(bcsub($discountPrice, $zs, 2));
  191. }
  192. $product['originalInfo'] = $original;
  193. util::success($product);
  194. }
  195. }