ProductController.php 12 KB

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