ProductController.php 15 KB

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