ProductController.php 15 KB

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