ProductController.php 14 KB

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