ProductController.php 14 KB

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