ProductController.php 12 KB

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