ProductController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 biz\shop\classes\ShopExtClass;
  7. use bizGhs\item\classes\ItemClassClass;
  8. use bizGhs\product\classes\ProductClass;
  9. use bizGhs\product\services\ProductService;
  10. use bizHd\item\classes\ItemClass;
  11. use common\components\printUtil;
  12. use common\components\stringUtil;
  13. use common\components\util;
  14. use Yii;
  15. class ProductController extends BaseController
  16. {
  17. public $guestAccess = ['ghs-item'];
  18. //打印花材的标签 ssh 20220602
  19. public function actionPrint()
  20. {
  21. $get = Yii::$app->request->get();
  22. $id = $get['id'] ?? 0;
  23. $num = $get['num'] ?? 1;
  24. $info = ProductClass::getById($id, true);
  25. if (empty($info)) {
  26. util::fail('没有找到花材');
  27. }
  28. $name = $info->name ?? '';
  29. $ptItemId = $info->itemId ?? 0;
  30. $shopId = $this->shopId;
  31. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  32. $printLabelSn = $ext->printLabelSn ?? '';
  33. if (empty($printLabelSn)) {
  34. util::fail('请设置标签打印机');
  35. }
  36. $p = new printUtil($printLabelSn);
  37. $unit = $info->ratio . $info->smallUnit . '/' . $info->bigUnit;
  38. $p->times = $num;
  39. $content = '<TEXT x="360" y="30" font="12" w="2" h="2" r="90">' . $name . '</TEXT>';
  40. $content .= '<BC128 x="260" y="30" h="80" s="1" r="90" n="5" w="12">' . $ptItemId . '</BC128>';
  41. $content .= '<TEXT x="60" y="30" font="12" w="2" h="2" r="90">' . $unit . '</TEXT>';
  42. $p->printLabelMsg($content);
  43. util::complete();
  44. }
  45. //新增花材 2021.6.21
  46. public function actionAdd()
  47. {
  48. $post = Yii::$app->request->post();
  49. $shop = $this->shop ?? [];
  50. $pfShopId = $shop->pfShopId ?? 0;
  51. if (!empty($pfShopId)) {
  52. util::fail('请在批发端操作');
  53. }
  54. $shopAdmin = $this->shopAdmin;
  55. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  56. util::fail('管理员才能添加花材');
  57. }
  58. $post['sjId'] = $this->sjId;
  59. $post['adminId'] = $this->adminId;
  60. $post['mainId'] = $this->mainId;
  61. $price = $post['price'] ?? 0;
  62. $addPrice = $post['addPrice'] ?? 0;
  63. if ($addPrice > $price) {
  64. util::fail('加价不能大于售价');
  65. }
  66. $cost = bcsub($price, $addPrice, 2);
  67. $post['cost'] = $cost;
  68. ItemClass::addItem($post, $this->shop);
  69. util::complete('添加成功');
  70. }
  71. //获取拼音缩写 ssh 20210707
  72. public function actionPy()
  73. {
  74. $name = Yii::$app->request->get('name');
  75. $py = stringUtil::py($name);
  76. util::success(['py' => $py]);
  77. }
  78. //所有花材
  79. public function actionAll()
  80. {
  81. $where = [];
  82. $where['mainId'] = $this->mainId;
  83. $classId = Yii::$app->request->get('classId', 0);
  84. if (!empty($classId)) {
  85. $where['classId'] = $classId;
  86. }
  87. $status = Yii::$app->request->get('status', 0);
  88. if (!empty($status)) {
  89. $where['status'] = $status;
  90. }
  91. $delStatus = Yii::$app->request->get('delStatus', 0);
  92. if ($delStatus != 2) {
  93. $where['delStatus'] = $delStatus;
  94. }
  95. //散客价
  96. $level = 0;
  97. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  98. $data = ProductClass::groupProductInfo($data, $level);
  99. util::success(['list' => $data]);
  100. }
  101. public function actionIndex()
  102. {
  103. $py = Yii::$app->request->get('py', '');
  104. //默认显示上架商品
  105. $status = Yii::$app->request->get('status', 1);
  106. //默认显示未删除商品
  107. $delStatus = Yii::$app->request->get('delStatus', 0);
  108. //获取所有当前供货商的分类 (全部、常用)
  109. //根据分类组装分类下的花材信息
  110. //中央ID
  111. $classIds = ItemClassClass::getGhsItemClassAll($this->mainId);
  112. $where['mainId'] = $this->mainId;
  113. if ($py) {
  114. $pyName = strtolower($py);
  115. $where['py'] = $pyName;
  116. }
  117. if (!empty($status)) {
  118. $where['status'] = $status;
  119. }
  120. $where['delStatus'] = $delStatus;
  121. $level = 0;
  122. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], "*");
  123. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  124. $data = ProductService::assembleData($classIds, $itemInfoData, true);
  125. util::success($data);
  126. }
  127. //当前门店所有花材分类
  128. public function actionList()
  129. {
  130. $classId = Yii::$app->request->get('classId', 0);
  131. $pyName = Yii::$app->request->get('py', '');
  132. if ($pyName) {
  133. $pyName = strtolower($pyName);
  134. $where['py'] = $pyName;
  135. }
  136. $where['mainId'] = $this->mainId;
  137. if (!empty($classId)) {
  138. $where['classId'] = $classId;
  139. }
  140. $type = Yii::$app->request->get('type', '');
  141. $showPage = Yii::$app->request->get('showPage', 0);
  142. //需要显示分页
  143. if ($showPage) {
  144. $respond = ProductClass::getList("*", $where, "inTurn desc");
  145. $respond['list'] = ProductClass::groupClassName($this->sjId, $respond['list']);
  146. $respond['list'] = ProductClass::groupProductInfo($respond['list']);
  147. util::success($respond);
  148. }
  149. $data = ProductClass::getAllByCondition($where, "inTurn desc", "*");
  150. $data = ProductClass::groupProductInfo($data);
  151. if ($type == 'waring') {
  152. //库存预警
  153. $newData = [];
  154. foreach ($data as $v) {
  155. if ($v['stock'] <= $v['stockWarning']) {
  156. $newData[] = $v;
  157. }
  158. }
  159. util::success(['list' => $newData]);
  160. }
  161. util::success(['list' => $data]);
  162. }
  163. //批量改价
  164. public function actionBatchChangePrice()
  165. {
  166. $shop = $this->shop ?? [];
  167. $pfShopId = $shop->pfShopId ?? 0;
  168. if (!empty($pfShopId)) {
  169. util::fail('请在批发端操作');
  170. }
  171. $default = $shop->default ?? 0;
  172. $shopAdmin = $this->shopAdmin;
  173. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  174. util::fail('超管才能修改');
  175. }
  176. $data = Yii::$app->request->post('data', '');
  177. if (empty($data)) {
  178. util::fail('没有修改');
  179. }
  180. $arr = json_decode($data, true);
  181. if (empty($arr)) {
  182. util::fail('没有修改!');
  183. }
  184. $connection = Yii::$app->db;
  185. $transaction = $connection->beginTransaction();
  186. try {
  187. $ids = array_column($arr, 'id');
  188. $ids = array_unique(array_filter($ids));
  189. if (empty($ids)) {
  190. util::fail('请选择花材');
  191. }
  192. $sjId = $shop->sjId ?? 0;
  193. $chainShopList = [];
  194. if ($default == 1) {
  195. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId, 'join' => 0], null, '*', null, true);
  196. }
  197. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  198. foreach ($arr as $product) {
  199. if (isset($product['price']) == false) {
  200. util::fail('没有价格');
  201. }
  202. if (isset($product['id']) == false) {
  203. util::fail('没有花材');
  204. }
  205. $price = $product['price'];
  206. $productId = $product['id'];
  207. $product = $productList[$productId] ?? [];
  208. if (empty($product)) {
  209. util::fail('没有找到花材');
  210. }
  211. if (isset($product->mainId) == false || $product->mainId != $this->mainId) {
  212. util::fail('没有权限访问');
  213. }
  214. if (empty($price)) {
  215. continue;
  216. }
  217. $ptItemId = $product->itemId;
  218. ItemClass::changeSinglePrice($shop, $ptItemId, $price);
  219. //在首店修改需要同步到所有直营店
  220. if ($default == 1) {
  221. foreach ($chainShopList as $chain) {
  222. if ($chain->id == $shop->id) {
  223. continue;
  224. }
  225. ItemClass::changeSinglePrice($chain, $ptItemId, $price);
  226. }
  227. }
  228. }
  229. $transaction->commit();
  230. util::complete('修改成功');
  231. } catch (\Exception $e) {
  232. $transaction->rollBack();
  233. Yii::info("失败原因:" . $e->getMessage());
  234. util::fail('修改失败');
  235. }
  236. }
  237. //从零售端采购入库,查看供货商的某个门店的product
  238. public function actionGhsItem()
  239. {
  240. $get = Yii::$app->request->get();
  241. $id = $get['id'] ?? 0;
  242. $ghs = GhsClass::getGhsInfo($id);
  243. if (empty($ghs)) {
  244. util::fail('没有找到供货商');
  245. }
  246. $shopId = $ghs['shopId'] ?? 0;
  247. $black = $ghs['black'] ?? 2;
  248. if ($black == 2) {
  249. util::fail('您已被列入黑名单');
  250. }
  251. $connection = Yii::$app->db;//事务处理
  252. $transaction = $connection->beginTransaction();
  253. try {
  254. //获取所有当前供货商的分类 (全部、常用)
  255. //根据分类组装分类下的花材信息
  256. $shop = ShopClass::getById($shopId, true);
  257. $mainId = $shop->mainId ?? 0;
  258. $classIds = ItemClassClass::getGhsItemClassAll($mainId);
  259. $where['mainId'] = $mainId;
  260. $where['delStatus'] = 0;
  261. $where['status'] = 1;
  262. $where['frontHide'] = 0;
  263. $level = $ghs['giveLevel'] ?? 0;
  264. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], "*");
  265. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  266. $data = ProductService::assembleData($classIds, $itemInfoData, true);
  267. util::success($data);
  268. } catch (\Exception $e) {
  269. $transaction->rollBack();
  270. util::fail();
  271. }
  272. }
  273. public function actionDetail()
  274. {
  275. $id = Yii::$app->request->get('id', 0);
  276. $respond = ProductClass::getItemInfo($id);
  277. $ptItemId = $respond['itemId'] ?? 0;
  278. $ptItemInfo = PtItemClass::getById($ptItemId);
  279. $respond['ptItemInfo'] = $ptItemInfo;
  280. util::success($respond);
  281. }
  282. public function actionUpdate()
  283. {
  284. $shop = $this->shop ?? [];
  285. $pfShopId = $shop->pfShopId ?? 0;
  286. if (!empty($pfShopId)) {
  287. util::fail('请在批发端操作');
  288. }
  289. $post = Yii::$app->request->post();
  290. $shopAdmin = $this->shopAdmin;
  291. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  292. util::fail('超管才能修改');
  293. }
  294. $post['adminId'] = $this->adminId;
  295. $post['staffName'] = $this->shopAdmin->name;
  296. ItemClass::updateProduct($post, $this->shop);
  297. util::complete();
  298. }
  299. //批量修改product 排序,成本价,加价
  300. public function actionBatchUpdate()
  301. {
  302. //data = [{id:1,inTurn:10,addPrice:2,cost:10}]
  303. $data = Yii::$app->request->post('data', '');
  304. $type = Yii::$app->request->post('type', '');
  305. if (empty($data) || empty($type)) {
  306. util::fail("参数错误");
  307. }
  308. if (in_array($type, ['inTurn', 'addPrice', 'cost', 'weight', 'ratio']))
  309. $data = json_decode($data, true);
  310. foreach ($data as $v) {
  311. $id = $v['id'];
  312. $val = $v[$type] ?? '';
  313. $where = [];
  314. $where['id'] = $id;
  315. $where['shopId'] = $this->shopId;
  316. $where['sjId'] = $this->sjId;
  317. if (empty($val)) {
  318. util::fail("参数错误1");
  319. }
  320. $data = [
  321. $type => $val
  322. ];
  323. ProductClass::updateByCondition($where, $data);
  324. ProductClass::autoPriceById($id);
  325. }
  326. util::complete();
  327. }
  328. //供货商产品的详情 ssh 20210903
  329. public function actionGhsProductDetail()
  330. {
  331. $id = Yii::$app->request->get('id', 0);
  332. $ghsId = Yii::$app->request->get('ghsId', 0);
  333. $ghs = GhsClass::getById($ghsId, true);
  334. GhsClass::valid($ghs, $this->shopId);
  335. $mainId = $ghs->mainId ?? 0;
  336. $product = ProductClass::getItemInfo($id, $ghs);
  337. if (isset($product['mainId']) == false || $product['mainId'] != $mainId) {
  338. Yii::info(json_encode($product));
  339. Yii::info('mainId: ' . $mainId);
  340. util::fail('没有权限访问');
  341. }
  342. $original = ProductClass::getById($id);
  343. if (isset($original['discountPrice']) && $original['discountPrice'] > 0) {
  344. $discountPrice = $original['discountPrice'];
  345. $original['price'] = floatval($discountPrice);
  346. $skAddPrice = $original['skAddPrice'] ?? 0;
  347. $addPrice = $original['addPrice'] ?? 0;
  348. $hjAddPrice = $original['hjAddPrice'] ?? 0;
  349. $zsAddPrice = $original['zsAddPrice'] ?? 0;
  350. $sk = bcsub($addPrice, $skAddPrice, 2);
  351. $hj = bcsub($addPrice, $hjAddPrice, 2);
  352. $zs = bcsub($addPrice, $zsAddPrice, 2);
  353. $original['skPrice'] = floatval(bcsub($discountPrice, $sk, 2));
  354. $original['hjPrice'] = floatval(bcsub($discountPrice, $hj, 2));
  355. $original['zsPrice'] = floatval(bcsub($discountPrice, $zs, 2));
  356. }
  357. $product['originalInfo'] = $original;
  358. util::success($product);
  359. }
  360. //批量修改加价 ssh 20211211
  361. public function actionBatchChangeAddPrice()
  362. {
  363. $shopAdmin = $this->shopAdmin;
  364. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  365. util::fail('超管才能操作');
  366. }
  367. $shop = $this->shop;
  368. $pfShopId = $shop->pfShopId ?? 0;
  369. if (!empty($pfShopId)) {
  370. util::fail('请在批发端操作');
  371. }
  372. $addData = Yii::$app->request->post('addData', '');
  373. if (empty($addData)) {
  374. util::fail('没有花材');
  375. }
  376. $itemData = json_decode($addData, true);
  377. if (is_array($itemData) == false) {
  378. util::fail('没有花材...');
  379. }
  380. $ids = array_column($itemData, 'id');
  381. $infoList = ProductClass::getByIds($ids);
  382. if (empty($infoList)) {
  383. util::fail('没有花材');
  384. }
  385. foreach ($infoList as $info) {
  386. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  387. util::fail('非常访问,不是你的花材不能修改');
  388. }
  389. }
  390. foreach ($itemData as $key => $data) {
  391. $id = $data['id'] ?? 0;
  392. ProductClass::updateById($id, $data);
  393. }
  394. util::complete('修改成功');
  395. }
  396. }