ProductController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. $field = 'id,cover,name,cost,itemId,classId,property,skPrice,skMore,addPrice,variety,price,discountPrice,skDiscountPrice,stock,stockWarning,presell,ratioType,smallRatio,smallUnit,bigUnit,ratio';
  130. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  131. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  132. $data = ProductService::assembleData($classIds, $itemInfoData, true);
  133. util::success($data);
  134. }
  135. //当前门店所有花材分类
  136. public function actionList()
  137. {
  138. $classId = Yii::$app->request->get('classId', 0);
  139. $pyName = Yii::$app->request->get('py', '');
  140. if ($pyName) {
  141. $pyName = strtolower($pyName);
  142. $where['py'] = $pyName;
  143. }
  144. $where['mainId'] = $this->mainId;
  145. if (!empty($classId)) {
  146. $where['classId'] = $classId;
  147. }
  148. $type = Yii::$app->request->get('type', '');
  149. $showPage = Yii::$app->request->get('showPage', 0);
  150. //需要显示分页
  151. if ($showPage) {
  152. $respond = ProductClass::getList("*", $where, "inTurn desc");
  153. $respond['list'] = ProductClass::groupClassName($this->sjId, $respond['list']);
  154. $respond['list'] = ProductClass::groupProductInfo($respond['list']);
  155. util::success($respond);
  156. }
  157. $data = ProductClass::getAllByCondition($where, "inTurn desc", "*");
  158. $data = ProductClass::groupProductInfo($data);
  159. if ($type == 'waring') {
  160. //库存预警
  161. $newData = [];
  162. foreach ($data as $v) {
  163. if ($v['stock'] <= $v['stockWarning']) {
  164. $newData[] = $v;
  165. }
  166. }
  167. util::success(['list' => $newData]);
  168. }
  169. util::success(['list' => $data]);
  170. }
  171. //批量改价
  172. public function actionBatchChangePrice()
  173. {
  174. $shop = $this->shop ?? [];
  175. $default = $shop->default ?? 0;
  176. $shopAdmin = $this->shopAdmin;
  177. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  178. util::fail('超管才能修改');
  179. }
  180. $pfShopId = $shop->pfShopId ?? 0;
  181. if (!empty($pfShopId)) {
  182. util::fail('请在批发端操作');
  183. }
  184. $data = Yii::$app->request->post('data', '');
  185. if (empty($data)) {
  186. util::fail('没有修改');
  187. }
  188. $arr = json_decode($data, true);
  189. if (empty($arr)) {
  190. util::fail('没有修改!');
  191. }
  192. $connection = Yii::$app->db;
  193. $transaction = $connection->beginTransaction();
  194. try {
  195. $ids = array_column($arr, 'id');
  196. $ids = array_unique(array_filter($ids));
  197. if (empty($ids)) {
  198. util::fail('请选择花材');
  199. }
  200. $sjId = $shop->sjId ?? 0;
  201. $chainShopList = [];
  202. if ($default == 1) {
  203. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId, 'join' => 0], null, '*', null, true);
  204. }
  205. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  206. foreach ($arr as $product) {
  207. if (isset($product['price']) == false) {
  208. util::fail('没有价格');
  209. }
  210. if (isset($product['id']) == false) {
  211. util::fail('没有花材');
  212. }
  213. $price = $product['price'];
  214. $productId = $product['id'];
  215. $product = $productList[$productId] ?? [];
  216. if (empty($product)) {
  217. util::fail('没有找到花材');
  218. }
  219. if (isset($product->mainId) == false || $product->mainId != $this->mainId) {
  220. util::fail('没有权限访问');
  221. }
  222. if (empty($price) || is_numeric($price) == false) {
  223. continue;
  224. }
  225. $ptItemId = $product->itemId;
  226. $staff = $this->shopAdmin;
  227. $staffName = $staff->name ?? '';
  228. $params = ['staffName' => $staffName];
  229. ItemClass::changeSinglePrice($shop, $ptItemId, $price, $params);
  230. //在首店修改需要同步到所有直营店
  231. if ($default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  232. foreach ($chainShopList as $chain) {
  233. if ($chain->id == $shop->id) {
  234. continue;
  235. }
  236. ItemClass::changeSinglePrice($chain, $ptItemId, $price, $params);
  237. }
  238. }
  239. }
  240. $transaction->commit();
  241. util::complete('修改成功');
  242. } catch (\Exception $e) {
  243. $transaction->rollBack();
  244. Yii::info("失败原因:" . $e->getMessage());
  245. util::fail('修改失败');
  246. }
  247. }
  248. //从零售端采购入库,查看供货商的某个门店的product
  249. public function actionGhsItem()
  250. {
  251. $get = Yii::$app->request->get();
  252. $id = $get['id'] ?? 0;
  253. $ghs = GhsClass::getGhsInfo($id);
  254. if (empty($ghs)) {
  255. util::fail('没有找到供货商');
  256. }
  257. $shopId = $ghs['shopId'] ?? 0;
  258. $black = $ghs['black'] ?? 2;
  259. if ($black == 2) {
  260. util::fail('您已被列入黑名单');
  261. }
  262. $connection = Yii::$app->db;//事务处理
  263. $transaction = $connection->beginTransaction();
  264. try {
  265. //获取所有当前供货商的分类 (全部、常用)
  266. //根据分类组装分类下的花材信息
  267. $shop = ShopClass::getById($shopId, true);
  268. $mainId = $shop->mainId ?? 0;
  269. $classIds = ItemClassClass::getGhsItemClassAll($mainId);
  270. $where['mainId'] = $mainId;
  271. $where['delStatus'] = 0;
  272. //是否显示全部花材,包括下架的
  273. $showAll = $get['showAll'] ?? 0;
  274. if ($showAll == 0) {
  275. $where['status'] = 1;
  276. }
  277. $where['frontHide'] = 0;
  278. $level = $ghs['giveLevel'] ?? 1;
  279. $field = 'id,py,cover,name,ratio,classId,itemId,weight,smallUnit,bigUnit,smallUnit,smallRatio,ratioType,variety,price,hjPrice,hjDiscountPrice,actualSold,stock,stockWarning,discountPrice,presell,presellDate';
  280. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  281. $itemInfoData = ProductClass::hdCgGroup($itemInfoData, $level);
  282. $data = ProductService::assembleData($classIds, $itemInfoData, true);
  283. util::success($data);
  284. } catch (\Exception $e) {
  285. $transaction->rollBack();
  286. Yii::info("报错内容:" . $e->getMessage());
  287. util::fail();
  288. }
  289. }
  290. public function actionDetail()
  291. {
  292. $id = Yii::$app->request->get('id', 0);
  293. $respond = ProductClass::getItemInfo($id);
  294. $ptItemId = $respond['itemId'] ?? 0;
  295. $ptItemInfo = PtItemClass::getById($ptItemId);
  296. $respond['ptItemInfo'] = $ptItemInfo;
  297. util::success($respond);
  298. }
  299. public function actionUpdate()
  300. {
  301. $shop = $this->shop ?? [];
  302. $pfShopId = $shop->pfShopId ?? 0;
  303. if (!empty($pfShopId)) {
  304. util::fail('请在批发端操作');
  305. }
  306. $post = Yii::$app->request->post();
  307. $shopAdmin = $this->shopAdmin;
  308. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  309. util::fail('超管才能修改');
  310. }
  311. $post['adminId'] = $this->adminId;
  312. $post['staffName'] = $this->shopAdmin->name;
  313. ItemClass::updateProduct($post, $this->shop);
  314. util::complete();
  315. }
  316. //批量修改product 排序,成本价,加价
  317. public function actionBatchUpdate()
  318. {
  319. //data = [{id:1,inTurn:10,addPrice:2,cost:10}]
  320. $data = Yii::$app->request->post('data', '');
  321. $type = Yii::$app->request->post('type', '');
  322. if (empty($data) || empty($type)) {
  323. util::fail("参数错误");
  324. }
  325. if (in_array($type, ['inTurn', 'addPrice', 'cost', 'weight', 'ratio']))
  326. $data = json_decode($data, true);
  327. foreach ($data as $v) {
  328. $id = $v['id'];
  329. $val = $v[$type] ?? '';
  330. $where = [];
  331. $where['id'] = $id;
  332. $where['shopId'] = $this->shopId;
  333. $where['sjId'] = $this->sjId;
  334. if (empty($val)) {
  335. util::fail("参数错误1");
  336. }
  337. $data = [
  338. $type => $val
  339. ];
  340. ProductClass::updateByCondition($where, $data);
  341. ProductClass::autoPriceById($id);
  342. }
  343. util::complete();
  344. }
  345. //供货商产品的详情 ssh 20230111
  346. public function actionGhsProductDetail()
  347. {
  348. $get = Yii::$app->request->get();
  349. $id = $get['id'] ?? 0;
  350. $ghsId = $get['ghsId'] ?? 0;
  351. $product = ProductClass::getById($id);
  352. if (empty($product)) {
  353. util::fail('没有花材信息');
  354. }
  355. $ghsInfo = GhsClass::getById($ghsId, true);
  356. $level = $ghsInfo->giveLevel ? $ghsInfo->giveLevel : 1;
  357. $list = ProductClass::groupProductInfo([$product], $level);
  358. $product = current($list);
  359. util::success($product);
  360. }
  361. //批量修改加价 ssh 20211211
  362. public function actionBatchChangeAddPrice()
  363. {
  364. $shopAdmin = $this->shopAdmin;
  365. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  366. util::fail('超管才能操作');
  367. }
  368. $shop = $this->shop;
  369. $pfShopId = $shop->pfShopId ?? 0;
  370. if (!empty($pfShopId)) {
  371. util::fail('请在批发端操作');
  372. }
  373. $addData = Yii::$app->request->post('addData', '');
  374. if (empty($addData)) {
  375. util::fail('没有花材');
  376. }
  377. $itemData = json_decode($addData, true);
  378. if (is_array($itemData) == false) {
  379. util::fail('没有花材...');
  380. }
  381. $ids = array_column($itemData, 'id');
  382. $infoList = ProductClass::getByIds($ids);
  383. if (empty($infoList)) {
  384. util::fail('没有花材');
  385. }
  386. foreach ($infoList as $info) {
  387. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  388. util::fail('非常访问,不是你的花材不能修改');
  389. }
  390. }
  391. foreach ($itemData as $key => $data) {
  392. $id = $data['id'] ?? 0;
  393. ProductClass::updateById($id, $data);
  394. }
  395. util::complete('修改成功');
  396. }
  397. }