ProductController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 biz\shop\classes\ShopClass;
  10. use bizGhs\item\classes\ItemClassClass;
  11. use bizGhs\product\classes\ProductClass;
  12. use bizGhs\product\services\ProductService;
  13. use common\components\util;
  14. use Yii;
  15. class ProductController extends BaseController
  16. {
  17. public $guestAccess = ['ghs-item'];
  18. public function actionIndex()
  19. {
  20. $py = Yii::$app->request->get('py', '');
  21. //默认显示上架商品
  22. $status = Yii::$app->request->get('status', 1);
  23. //默认显示未删除商品
  24. $delStatus = Yii::$app->request->get('delStatus', 0);
  25. //获取所有当前供应商的分类 (全部、常用)
  26. //根据分类组装分类下的花材信息
  27. //中央ID
  28. $classIds = ItemClassClass::getGhsItemClassAll($this->mainId);
  29. $where['mainId'] = $this->mainId;
  30. if ($py) {
  31. $pyName = strtolower($py);
  32. $where['py'] = $pyName;
  33. }
  34. if (!empty($status)) {
  35. $where['status'] = $status;
  36. }
  37. $where['delStatus'] = $delStatus;
  38. $level = 0;
  39. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], "*");
  40. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  41. $data = ProductService::assembleData($classIds, $itemInfoData, true);
  42. util::success($data);
  43. }
  44. //当前门店所有花材分类
  45. public function actionList()
  46. {
  47. $classId = Yii::$app->request->get('classId', 0);
  48. $pyName = Yii::$app->request->get('py', '');
  49. $where['sjId'] = $this->sjId;
  50. if ($pyName) {
  51. $pyName = strtolower($pyName);
  52. $where['py'] = $pyName;
  53. }
  54. $shopId = Yii::$app->request->get('shopId', 0);
  55. if (!$shopId) {
  56. $shopId = $this->shopId;
  57. }
  58. $where['shopId'] = $shopId;
  59. if (!empty($classId)) {
  60. $where['classId'] = $classId;
  61. }
  62. $type = Yii::$app->request->get('type', '');
  63. $showPage = Yii::$app->request->get('showPage', 0);
  64. //需要显示分页
  65. if ($showPage) {
  66. $respond = ProductClass::getList("*", $where, "inTurn desc");
  67. $respond['list'] = ProductClass::groupClassName($this->sjId, $respond['list']);
  68. $respond['list'] = ProductClass::groupProductInfo($respond['list']);
  69. util::success($respond);
  70. }
  71. $data = ProductClass::getAllByCondition($where, "inTurn desc", "*");
  72. $data = ProductClass::groupProductInfo($data);
  73. if ($type == 'waring') {
  74. //库存预警
  75. $newData = [];
  76. foreach ($data as $v) {
  77. if ($v['stock'] <= $v['stockWarning']) {
  78. $newData[] = $v;
  79. }
  80. }
  81. util::success(['list' => $newData]);
  82. }
  83. util::success(['list' => $data]);
  84. }
  85. //批量改价
  86. public function actionBatchChangePrice()
  87. {
  88. util::fail('暂时无法修改');
  89. $shopAdmin = $this->shopAdmin;
  90. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  91. util::fail('超管才能修改');
  92. }
  93. //花材价格只能在首店修改,并同步到分店
  94. $shop = $this->shop;
  95. $default = $shop->default ?? 0;
  96. if ($default != 1) {
  97. util::fail('请在首店修改');
  98. }
  99. $data = Yii::$app->request->post('data', '');
  100. if (empty($data)) {
  101. util::fail('没有修改');
  102. }
  103. $arr = json_decode($data, true);
  104. if (empty($arr)) {
  105. util::fail('没有修改!');
  106. }
  107. $connection = Yii::$app->db;
  108. $transaction = $connection->beginTransaction();
  109. try {
  110. $ids = array_column($arr, 'id');
  111. $ids = array_unique(array_filter($ids));
  112. if (empty($ids)) {
  113. util::fail('请选择花材');
  114. }
  115. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  116. foreach ($arr as $product) {
  117. if (isset($product['price']) == false) {
  118. util::fail('没有价格');
  119. }
  120. if (isset($product['id']) == false) {
  121. util::fail('没有花材');
  122. }
  123. $price = $product['price'];
  124. $productId = $product['id'];
  125. $product = $productList[$productId] ?? [];
  126. if (empty($product)) {
  127. util::fail('没有找到花材');
  128. }
  129. if (isset($product->shopId) == false || $product->shopId != $this->shopId) {
  130. util::fail('没有权限访问');
  131. }
  132. if (empty($price)) {
  133. continue;
  134. }
  135. ProductClass::changePrice($product, $price, ProductClass::PRICE_LABEL_CHANGE);
  136. }
  137. $transaction->commit();
  138. util::complete('修改成功');
  139. } catch (\Exception $e) {
  140. $transaction->rollBack();
  141. Yii::info("修改失败原因:" . $e->getMessage());
  142. util::fail('修改失败');
  143. }
  144. }
  145. //从零售端采购入库,查看供应商的某个门店的product
  146. public function actionGhsItem()
  147. {
  148. $get = Yii::$app->request->get();
  149. $id = $get['id'] ?? 0;
  150. $ghs = GhsClass::getGhsInfo($id);
  151. if (empty($ghs)) {
  152. util::fail('没有找到供应商');
  153. }
  154. $shopId = $ghs['shopId'] ?? 0;
  155. $black = $ghs['black'] ?? 2;
  156. if ($black == 2) {
  157. util::fail('您已被列入黑名单');
  158. }
  159. $connection = Yii::$app->db;//事务处理
  160. $transaction = $connection->beginTransaction();
  161. try {
  162. //获取所有当前供应商的分类 (全部、常用)
  163. //根据分类组装分类下的花材信息
  164. $shop = ShopClass::getById($shopId, true);
  165. $mainId = $shop->mainId ?? 0;
  166. $classIds = ItemClassClass::getGhsItemClassAll($mainId);
  167. $where['mainId'] = $mainId;
  168. $where['delStatus'] = 0;
  169. $where['status'] = 1;
  170. $level = $ghs['giveLevel'] ?? 0;
  171. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], "*");
  172. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  173. $data = ProductService::assembleData($classIds, $itemInfoData, true);
  174. util::success($data);
  175. } catch (\Exception $e) {
  176. $transaction->rollBack();
  177. util::fail();
  178. }
  179. }
  180. public function actionDetail()
  181. {
  182. $id = Yii::$app->request->get('id', 0);
  183. $respond = ProductClass::getItemInfo($id);
  184. $ptItemId = $respond['itemId'] ?? 0;
  185. $ptItemInfo = PtItemClass::getById($ptItemId);
  186. $respond['ptItemInfo'] = $ptItemInfo;
  187. util::success($respond);
  188. }
  189. public function actionUpdate()
  190. {
  191. $post = Yii::$app->request->post();
  192. ProductClass::updateProduct($post);
  193. util::complete();
  194. }
  195. //批量修改product 排序,成本价,加价
  196. public function actionBatchUpdate()
  197. {
  198. //data = [{id:1,inTurn:10,addPrice:2,cost:10}]
  199. $data = Yii::$app->request->post('data', '');
  200. $type = Yii::$app->request->post('type', '');
  201. if (empty($data) || empty($type)) {
  202. util::fail("参数错误");
  203. }
  204. if (in_array($type, ['inTurn', 'addPrice', 'cost', 'weight', 'ratio']))
  205. $data = json_decode($data, true);
  206. foreach ($data as $v) {
  207. $id = $v['id'];
  208. $val = $v[$type] ?? '';
  209. $where = [];
  210. $where['id'] = $id;
  211. $where['shopId'] = $this->shopId;
  212. $where['sjId'] = $this->sjId;
  213. if (empty($val)) {
  214. util::fail("参数错误1");
  215. }
  216. $data = [
  217. $type => $val
  218. ];
  219. ProductClass::updateByCondition($where, $data);
  220. ProductClass::autoPriceById($id);
  221. }
  222. util::complete();
  223. }
  224. //供应商产品的详情 shish 20210903
  225. public function actionGhsProductDetail()
  226. {
  227. $id = Yii::$app->request->get('id', 0);
  228. $ghsId = Yii::$app->request->get('ghsId', 0);
  229. $ghs = GhsClass::getById($ghsId, true);
  230. GhsClass::valid($ghs, $this->shopId);
  231. $shopId = $ghs->shopId ?? 0;
  232. $product = ProductClass::getItemInfo($id, $ghs);
  233. if (isset($product['shopId']) == false || $product['shopId'] != $shopId) {
  234. util::fail('没有权限访问');
  235. }
  236. $original = ProductClass::getById($id);
  237. if (isset($original['discountPrice']) && $original['discountPrice'] > 0) {
  238. $discountPrice = $original['discountPrice'];
  239. $original['price'] = floatval($discountPrice);
  240. $skAddPrice = $original['skAddPrice'] ?? 0;
  241. $addPrice = $original['addPrice'] ?? 0;
  242. $hjAddPrice = $original['hjAddPrice'] ?? 0;
  243. $zsAddPrice = $original['zsAddPrice'] ?? 0;
  244. $sk = bcsub($addPrice, $skAddPrice, 2);
  245. $hj = bcsub($addPrice, $hjAddPrice, 2);
  246. $zs = bcsub($addPrice, $zsAddPrice, 2);
  247. $original['skPrice'] = floatval(bcsub($discountPrice, $sk, 2));
  248. $original['hjPrice'] = floatval(bcsub($discountPrice, $hj, 2));
  249. $original['zsPrice'] = floatval(bcsub($discountPrice, $zs, 2));
  250. }
  251. $product['originalInfo'] = $original;
  252. util::success($product);
  253. }
  254. }