ProductController.php 14 KB

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