ProductController.php 15 KB

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