ProductController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. $level = $ghs['giveLevel'] ?? 0;
  263. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], "*");
  264. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  265. $data = ProductService::assembleData($classIds, $itemInfoData, true);
  266. util::success($data);
  267. } catch (\Exception $e) {
  268. $transaction->rollBack();
  269. util::fail();
  270. }
  271. }
  272. public function actionDetail()
  273. {
  274. $id = Yii::$app->request->get('id', 0);
  275. $respond = ProductClass::getItemInfo($id);
  276. $ptItemId = $respond['itemId'] ?? 0;
  277. $ptItemInfo = PtItemClass::getById($ptItemId);
  278. $respond['ptItemInfo'] = $ptItemInfo;
  279. util::success($respond);
  280. }
  281. public function actionUpdate()
  282. {
  283. $shop = $this->shop ?? [];
  284. $pfShopId = $shop->pfShopId ?? 0;
  285. if (!empty($pfShopId)) {
  286. util::fail('请在批发端操作');
  287. }
  288. $post = Yii::$app->request->post();
  289. $shopAdmin = $this->shopAdmin;
  290. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  291. util::fail('超管才能修改');
  292. }
  293. $post['adminId'] = $this->adminId;
  294. $post['staffName'] = $this->shopAdmin->name;
  295. ItemClass::updateProduct($post, $this->shop);
  296. util::complete();
  297. }
  298. //批量修改product 排序,成本价,加价
  299. public function actionBatchUpdate()
  300. {
  301. //data = [{id:1,inTurn:10,addPrice:2,cost:10}]
  302. $data = Yii::$app->request->post('data', '');
  303. $type = Yii::$app->request->post('type', '');
  304. if (empty($data) || empty($type)) {
  305. util::fail("参数错误");
  306. }
  307. if (in_array($type, ['inTurn', 'addPrice', 'cost', 'weight', 'ratio']))
  308. $data = json_decode($data, true);
  309. foreach ($data as $v) {
  310. $id = $v['id'];
  311. $val = $v[$type] ?? '';
  312. $where = [];
  313. $where['id'] = $id;
  314. $where['shopId'] = $this->shopId;
  315. $where['sjId'] = $this->sjId;
  316. if (empty($val)) {
  317. util::fail("参数错误1");
  318. }
  319. $data = [
  320. $type => $val
  321. ];
  322. ProductClass::updateByCondition($where, $data);
  323. ProductClass::autoPriceById($id);
  324. }
  325. util::complete();
  326. }
  327. //供应商产品的详情 ssh 20210903
  328. public function actionGhsProductDetail()
  329. {
  330. $id = Yii::$app->request->get('id', 0);
  331. $ghsId = Yii::$app->request->get('ghsId', 0);
  332. $ghs = GhsClass::getById($ghsId, true);
  333. GhsClass::valid($ghs, $this->shopId);
  334. $mainId = $ghs->mainId ?? 0;
  335. $product = ProductClass::getItemInfo($id, $ghs);
  336. if (isset($product['mainId']) == false || $product['mainId'] != $mainId) {
  337. Yii::info(json_encode($product));
  338. Yii::info('mainId: ' . $mainId);
  339. util::fail('没有权限访问');
  340. }
  341. $original = ProductClass::getById($id);
  342. if (isset($original['discountPrice']) && $original['discountPrice'] > 0) {
  343. $discountPrice = $original['discountPrice'];
  344. $original['price'] = floatval($discountPrice);
  345. $skAddPrice = $original['skAddPrice'] ?? 0;
  346. $addPrice = $original['addPrice'] ?? 0;
  347. $hjAddPrice = $original['hjAddPrice'] ?? 0;
  348. $zsAddPrice = $original['zsAddPrice'] ?? 0;
  349. $sk = bcsub($addPrice, $skAddPrice, 2);
  350. $hj = bcsub($addPrice, $hjAddPrice, 2);
  351. $zs = bcsub($addPrice, $zsAddPrice, 2);
  352. $original['skPrice'] = floatval(bcsub($discountPrice, $sk, 2));
  353. $original['hjPrice'] = floatval(bcsub($discountPrice, $hj, 2));
  354. $original['zsPrice'] = floatval(bcsub($discountPrice, $zs, 2));
  355. }
  356. $product['originalInfo'] = $original;
  357. util::success($product);
  358. }
  359. //批量修改加价 ssh 20211211
  360. public function actionBatchChangeAddPrice()
  361. {
  362. $shopAdmin = $this->shopAdmin;
  363. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  364. util::fail('超管才能操作');
  365. }
  366. $shop = $this->shop;
  367. $pfShopId = $shop->pfShopId ?? 0;
  368. if (!empty($pfShopId)) {
  369. util::fail('请在批发端操作');
  370. }
  371. $addData = Yii::$app->request->post('addData', '');
  372. if (empty($addData)) {
  373. util::fail('没有花材');
  374. }
  375. $itemData = json_decode($addData, true);
  376. if (is_array($itemData) == false) {
  377. util::fail('没有花材...');
  378. }
  379. $ids = array_column($itemData, 'id');
  380. $infoList = ProductClass::getByIds($ids);
  381. if (empty($infoList)) {
  382. util::fail('没有花材');
  383. }
  384. foreach ($infoList as $info) {
  385. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  386. util::fail('非常访问,不是你的花材不能修改');
  387. }
  388. }
  389. foreach ($itemData as $key => $data) {
  390. $id = $data['id'] ?? 0;
  391. ProductClass::updateById($id, $data);
  392. }
  393. util::complete('修改成功');
  394. }
  395. }