ProductController.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. <?php
  2. /**
  3. * User: admin
  4. * Date Time: 2021/1/15 20:36
  5. */
  6. namespace ghs\controllers;
  7. use biz\admin\classes\AdminRoleClass;
  8. use biz\item\classes\PtItemClass;
  9. use bizGhs\custom\classes\CustomClass;
  10. use bizGhs\item\classes\ItemClassClass;
  11. use bizGhs\merchant\classes\ShopClass;
  12. use bizGhs\product\classes\ProductClass;
  13. use bizGhs\product\models\Product;
  14. use bizGhs\product\services\ProductService;
  15. use bizGhs\shop\classes\ShopAdminClass;
  16. use bizGhs\stock\classes\StockInDayClass;
  17. use common\components\printUtil;
  18. use common\components\stringUtil;
  19. use common\components\util;
  20. use Yii;
  21. class ProductController extends BaseController
  22. {
  23. public $guestAccess = ['index'];
  24. //修改预售 ssh 20221214
  25. public function actionChangePresell()
  26. {
  27. $post = Yii::$app->request->post();
  28. $id = $post['id'] ?? 0;
  29. $info = ProductClass::getById($id, true);
  30. ProductClass::check($info, $this->mainId);
  31. $str = $post['date'] ?? '';
  32. $presell = $post['presell'] ?? 0;
  33. $newStr = '';
  34. if ($presell == 1) {
  35. if (empty($str)) {
  36. util::fail('请填写预售日期');
  37. }
  38. $arr = json_decode($str, true);
  39. if (empty($arr)) {
  40. util::fail('请填写预售日期');
  41. }
  42. $new = [];
  43. foreach ($arr as $date) {
  44. $new[] = strtotime($date);
  45. }
  46. $new = array_unique(array_filter($new));
  47. asort($new);
  48. $newStr = implode(',', $new);
  49. }
  50. $info->presell = $presell;
  51. $info->presellDate = $newStr;
  52. $info->discountPrice = 0;
  53. $info->save();
  54. util::complete();
  55. }
  56. //取消特价 ssh 20220901
  57. public function actionCancelDiscount()
  58. {
  59. $get = Yii::$app->request->get();
  60. $id = $get['id'] ?? 0;
  61. $info = ProductClass::getById($id, true);
  62. ProductClass::check($info, $this->mainId);
  63. $info->discountPrice = 0;
  64. $info->save();
  65. util::complete();
  66. }
  67. //打印花材的标签 ssh 20220602
  68. public function actionPrint()
  69. {
  70. $get = Yii::$app->request->get();
  71. $id = $get['id'] ?? 0;
  72. $num = $get['num'] ?? 1;
  73. $info = ProductClass::getById($id, true);
  74. if (empty($info)) {
  75. util::fail('没有找到花材');
  76. }
  77. $name = $info->name ?? '';
  78. $ptItemId = $info->itemId ?? 0;
  79. $mainId = $info->mainId ?? 0;
  80. $ext = $this->shopExt;
  81. $printLabelSn = $ext->printLabelSn ?? '';
  82. if (empty($printLabelSn)) {
  83. util::fail('请设置标签打印机');
  84. }
  85. $p = new printUtil($printLabelSn);
  86. $unit = $info->ratio . $info->smallUnit . '/' . $info->bigUnit;
  87. $p->times = $num;
  88. $content = '<TEXT x="360" y="30" font="12" w="2" h="2" r="90">' . $name . '</TEXT>';
  89. $content .= '<BC128 x="260" y="30" h="80" s="1" r="90" n="5" w="12">' . $ptItemId . '</BC128>';
  90. if (in_array($mainId, [52, 1459])) {
  91. $content .= '<TEXT x="80" y="30" font="12" w="2" h="2" r="90">惠雅鲜花</TEXT>';
  92. } else {
  93. $content .= '<TEXT x="80" y="30" font="12" w="2" h="2" r="90">' . $unit . '</TEXT>';
  94. }
  95. //$content .= '<QR x="30" y="390" e="L" w="5">' . Yii::$app->params['mallHost'] . "/item/show-info?id=" . $id . '</QR>';
  96. //$content .= '<TEXT x="220" y="400" font="12" w="1" h="1" r="90">扫码看价格</TEXT>';
  97. $p->printLabelMsg($content);
  98. util::complete();
  99. }
  100. //取出今天有入库的花材 ssh 20221224
  101. public function actionGetStockIn()
  102. {
  103. $py = Yii::$app->request->get('py', '');
  104. $version = Yii::$app->request->get('version', 0);
  105. if ($version == 0) {
  106. util::fail('请先升级版本到1.0.70以上');
  107. }
  108. $classInfo = ItemClassClass::getGhsItemClassAll($this->mainId);
  109. $mainId = $this->mainId ?? 0;
  110. $where['mainId'] = $mainId;
  111. if ($py) {
  112. $pyName = strtolower($py);
  113. $where['py'] = $pyName;
  114. }
  115. $arr = StockInDayClass::getAllByCondition(['mainId' => $mainId], null, '*');
  116. if (empty($arr)) {
  117. util::fail('没有花材需要盘点');
  118. }
  119. $ids = array_column($arr, 'itemId');
  120. $ids = array_unique($ids);
  121. $where['id'] = ['in', $ids];
  122. $level = 1;
  123. $field = 'id,py,cover,name,ratio,cost,addPrice,classId,skMore,skPrice,itemId,smallUnit,smallRatio,presellDate,bigUnit,smallUnit,ratioType,variety,price,stock,stockWarning,discountPrice,presell,presellDate';
  124. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  125. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  126. $data = ProductService::assembleItemData($classInfo, $itemInfoData);
  127. util::success($data);
  128. }
  129. //取出今天有入库的花材 ssh 20221024
  130. public function actionStockIn()
  131. {
  132. $py = Yii::$app->request->get('py', '');
  133. $version = Yii::$app->request->get('version', 0);
  134. if ($version == 0) {
  135. util::fail('请先升级版本到1.0.70以上');
  136. }
  137. $classInfo = ItemClassClass::getGhsItemClassAll($this->mainId);
  138. $mainId = $this->mainId ?? 0;
  139. $where['mainId'] = $mainId;
  140. if ($py) {
  141. $pyName = strtolower($py);
  142. $where['py'] = $pyName;
  143. }
  144. $arr = StockInDayClass::getAllByCondition(['mainId' => $mainId], null, '*');
  145. if (empty($arr)) {
  146. util::fail('没有花材需要盘点');
  147. }
  148. $ids = array_column($arr, 'itemId');
  149. $ids = array_unique($ids);
  150. $where['id'] = ['in', $ids];
  151. $level = 1;
  152. $field = 'id,py,cover,name,ratio,cost,addPrice,classId,skMore,skPrice,itemId,smallUnit,smallRatio,presellDate,bigUnit,smallUnit,ratioType,variety,price,stock,stockWarning,discountPrice,presell,presellDate';
  153. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  154. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  155. $data = ProductService::assembleData($classInfo, $itemInfoData, true);
  156. util::success($data);
  157. }
  158. //按分类列出所有花材
  159. public function actionShow()
  160. {
  161. $py = Yii::$app->request->get('py', '');
  162. $requestType = Yii::$app->request->get('requestType', 'common');
  163. //默认显示上架商品
  164. $status = Yii::$app->request->get('status', 1);
  165. //默认显示未删除商品
  166. $delStatus = Yii::$app->request->get('delStatus', 0);
  167. //获取所有当前供货商的分类 (全部、常用)
  168. //根据分类组装分类下的花材信息
  169. //中央ID
  170. $classInfo = ItemClassClass::getGhsItemClassAll($this->mainId);
  171. $where['mainId'] = $this->mainId;
  172. if ($py) {
  173. $pyName = strtolower($py);
  174. $where['py'] = $pyName;
  175. }
  176. //改价和全部有库存花材列表
  177. if ($requestType == 'changePrice' || $requestType == 'hasStockList') {
  178. $where['stock>'] = 0;
  179. }
  180. if (!empty($status)) {
  181. $where['status'] = $status;
  182. }
  183. $where['delStatus'] = $delStatus;
  184. //客户等级
  185. $customId = Yii::$app->request->get('customId', 0);
  186. $level = 1;
  187. if (!empty($customId)) {
  188. $custom = CustomClass::getById($customId, true);
  189. $level = $custom->level ?? 1;
  190. }
  191. if ($requestType == 'hasStockList') {
  192. //有库存的花材列表
  193. $field = 'id,py,name,classId,itemId,price,skPrice,stock';
  194. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  195. } elseif ($requestType == 'changePrice') {
  196. //修改价格列表
  197. $field = 'id,py,cover,name,cost,itemId,classId,addPrice,skPrice,discountPrice,skMore,variety,price,stock,presell';
  198. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  199. $itemInfoData = ProductClass::changePriceGroup($itemInfoData, $level);
  200. } elseif ($requestType == 'itemList') {
  201. //花材列表
  202. $field = 'id,py,cover,name,cost,itemId,classId,skPrice,variety,price,stock,presell';
  203. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  204. $itemInfoData = ProductClass::itemListGroup($itemInfoData, $level);
  205. } elseif ($requestType == 'cg') {
  206. //采购花材
  207. $field = 'id,py,cover,name,itemId,classId,variety,price,stock,presell,ratioType,smallUnit,bigUnit,ratio';
  208. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  209. $itemInfoData = ProductClass::cgItemGroup($itemInfoData, $level);
  210. } elseif ($requestType == 'warning') {
  211. //库存预警
  212. $field = 'id,py,cover,name,cost,itemId,classId,skPrice,variety,price,stock,presell,stockWarning';
  213. $itemInfoData = Product::find()->where("mainId={$this->mainId}")
  214. ->andWhere('`stock`<`stockWarning`')
  215. ->andWhere("delStatus=0")
  216. ->select($field)->asArray()->all();
  217. $itemInfoData = ProductClass::warningGroup($itemInfoData, $level);
  218. } else {
  219. //通用
  220. $field = 'id,py,cover,name,ratio,cost,addPrice,classId,skMore,skPrice,itemId,smallUnit,smallRatio,bigUnit,smallUnit,ratioType,variety,price,stock,stockWarning,discountPrice,presell,presellDate';
  221. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  222. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  223. }
  224. $data = ProductService::assembleItemData($classInfo, $itemInfoData);
  225. util::success($data);
  226. }
  227. //按分类列出所有花材
  228. public function actionIndex()
  229. {
  230. $py = Yii::$app->request->get('py', '');
  231. $requestType = Yii::$app->request->get('requestType', 'common');
  232. //默认显示上架商品
  233. $status = Yii::$app->request->get('status', 1);
  234. //默认显示未删除商品
  235. $delStatus = Yii::$app->request->get('delStatus', 0);
  236. $warning = Yii::$app->request->get('warning', 0);
  237. //获取所有当前供货商的分类 (全部、常用)
  238. //根据分类组装分类下的花材信息
  239. //中央ID
  240. $classInfo = ItemClassClass::getGhsItemClassAll($this->mainId);
  241. $where['mainId'] = $this->mainId;
  242. if ($py) {
  243. $pyName = strtolower($py);
  244. $where['py'] = $pyName;
  245. }
  246. if ($requestType == 'hasStock') {
  247. //改价只显示库存大于0的
  248. $where['stock>'] = 0;
  249. }
  250. if (!empty($status)) {
  251. $where['status'] = $status;
  252. }
  253. $where['delStatus'] = $delStatus;
  254. //客户等级
  255. $customId = Yii::$app->request->get('customId', 0);
  256. $level = 1;
  257. if (!empty($customId)) {
  258. $custom = CustomClass::getById($customId, true);
  259. $level = $custom->level ?? 1;
  260. }
  261. if ($warning == 1) {
  262. //库存预警
  263. $field = 'id,py,cover,name,ratio,cost,addPrice,classId,skMore,skPrice,itemId,smallUnit,smallRatio,presellDate,bigUnit,smallUnit,ratioType,variety,price,stock,stockWarning,discountPrice,presell,presellDate';
  264. $itemInfoData = Product::find()->where("mainId={$this->mainId}")
  265. ->andWhere('`stock`<`stockWarning`')
  266. ->andWhere("delStatus=0")
  267. ->select($field)->asArray()->all();
  268. } else {
  269. $field = 'id,py,cover,name,ratio,cost,addPrice,classId,skMore,skPrice,itemId,smallUnit,smallRatio,presellDate,bigUnit,smallUnit,ratioType,variety,price,stock,stockWarning,discountPrice,presell,presellDate';
  270. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  271. }
  272. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  273. $data = ProductService::assembleData($classInfo, $itemInfoData, true);
  274. util::success($data);
  275. }
  276. //所有花材分页和不分页
  277. public function actionList()
  278. {
  279. $where = [];
  280. $classId = Yii::$app->request->get('classId', 0);
  281. if (!empty($classId)) {
  282. $where['classId'] = $classId;
  283. }
  284. $name = Yii::$app->request->get('name', '');
  285. if (empty($name)) {
  286. $py = Yii::$app->request->get('py', '');
  287. $name = !empty($py) ? $py : '';
  288. }
  289. if (!empty($name)) {
  290. if (stringUtil::isLetter($name)) {
  291. $where['py'] = ['like', $name];
  292. } else {
  293. $where['name'] = ['like', $name];
  294. }
  295. }
  296. $status = Yii::$app->request->get('status', 0);
  297. if (!empty($status)) {
  298. $where['status'] = $status;
  299. }
  300. $delStatus = Yii::$app->request->get('delStatus', 0);
  301. if ($delStatus != 2) {
  302. $where['delStatus'] = $delStatus;
  303. }
  304. $where['mainId'] = $this->mainId;
  305. $productIds = Yii::$app->request->get('ids', '');
  306. if (!empty($productIds)) {
  307. $productIdArr = explode(',', $productIds);
  308. if (!empty($productIdArr)) {
  309. $where['id'] = ['in', $productIdArr];
  310. }
  311. }
  312. //输出标准会员价,即花店价,默认显示的是花店价
  313. $level = 1;
  314. $type = Yii::$app->request->get('type', '');
  315. $showPage = Yii::$app->request->get('showPage', 0);
  316. if ($showPage) {
  317. if ($type == 'waring') {
  318. $pro = Product::find()->where("mainId={$this->mainId}")->andWhere('`stock`<`stockWarning`')->select('id')->asArray()->all();
  319. $lackIds = array_column($pro, 'id');
  320. if (empty($lackIds)) {
  321. util::success([]);
  322. }
  323. $lackIds = array_filter(array_unique($lackIds));
  324. $where['id'] = ['in', $lackIds];
  325. $respond = ProductClass::getList("*", $where, "actualSold desc");
  326. } else {
  327. $respond = ProductClass::getList("*", $where, "inTurn desc");
  328. }
  329. $respond['list'] = ProductClass::groupProductInfo($respond['list'], $level);
  330. $respond['list'] = ProductClass::groupClassName($this->sjId, $respond['list']);
  331. util::success($respond);
  332. } else {
  333. if ($type == 'waring') {
  334. //库存预警 按库存从小到大排序
  335. $data = ProductClass::getAllByCondition($where, ['stock' => SORT_ASC, 'inTurn' => SORT_DESC], "*");
  336. } else {
  337. if (!empty($productIds)) {
  338. //优先按 $productIds 排序 "FIELD(`id`,4,5,6)"=>true
  339. $data = ProductClass::getAllByCondition($where, ["FIELD(`id`,$productIds)" => true, 'inTurn' => SORT_DESC], "*");
  340. } else {
  341. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  342. }
  343. }
  344. $data = ProductClass::groupProductInfo($data, $level);
  345. $data = ProductClass::groupClassName($this->sjId, $data);
  346. if ($type == 'waring') {
  347. //库存预警
  348. $newData = [];
  349. foreach ($data as $v) {
  350. if ($v['stock'] <= $v['stockWarning']) {
  351. $newData[] = $v;
  352. }
  353. }
  354. util::success(['list' => $newData]);
  355. }
  356. util::success(['list' => $data]);
  357. }
  358. }
  359. //所有花材
  360. public function actionAll()
  361. {
  362. $where = [];
  363. $where['mainId'] = $this->mainId;
  364. $classId = Yii::$app->request->get('classId', 0);
  365. if (!empty($classId)) {
  366. $where['classId'] = $classId;
  367. }
  368. $status = Yii::$app->request->get('status', 0);
  369. if (!empty($status)) {
  370. $where['status'] = $status;
  371. }
  372. $delStatus = Yii::$app->request->get('delStatus', 0);
  373. if ($delStatus != 2) {
  374. $where['delStatus'] = $delStatus;
  375. }
  376. //输出标准会员价,即花店价,默认显示的是花店价
  377. $level = 1;
  378. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  379. $data = ProductClass::groupProductInfo($data, $level);
  380. util::success(['list' => $data]);
  381. }
  382. //批量改价
  383. public function actionBatchChangePrice()
  384. {
  385. $shop = $this->shop;
  386. $join = $shop->join ?? 0;
  387. $default = $shop->default ?? 0;
  388. if ($join == 0 && $default == 0) {
  389. util::fail('请在默认门店修改');
  390. }
  391. $shopAdmin = $this->shopAdmin;
  392. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  393. util::fail('超管才能修改');
  394. }
  395. $shop = $this->shop;
  396. $default = $shop->default ?? 0;
  397. $adminId = $this->adminId ?? 0;
  398. //normal常规改价,cg采购改价
  399. $changeType = Yii::$app->request->post('changeType', 'normal');
  400. $targetId = Yii::$app->request->post('targetId', 0);
  401. $changeParams = ['staffId' => $shopAdmin->id, 'staffName' => $shopAdmin->name, 'changeType' => $changeType, 'targetId' => $targetId];
  402. $data = Yii::$app->request->post('data', '');
  403. if (empty($data)) {
  404. util::fail('没有修改');
  405. }
  406. $arr = json_decode($data, true);
  407. if (empty($arr)) {
  408. util::fail('没有修改哦');
  409. }
  410. $connection = Yii::$app->db;
  411. $transaction = $connection->beginTransaction();
  412. try {
  413. $ids = array_column($arr, 'id');
  414. $ids = array_unique(array_filter($ids));
  415. if (empty($ids)) {
  416. util::fail('请选择花材');
  417. }
  418. $sjId = $shop->sjId ?? 0;
  419. $chainShopList = [];
  420. if ($default == 1) {
  421. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId, 'join' => 0], null, '*', null, true);
  422. }
  423. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  424. foreach ($arr as $product) {
  425. $productId = $product['id'];
  426. $productInfo = $productList[$productId] ?? [];
  427. if (empty($productInfo)) {
  428. util::fail('没有找到花材');
  429. }
  430. $productName = $productInfo->name ?? '';
  431. if (isset($product['id']) == false) {
  432. util::fail('没有花材');
  433. }
  434. if (isset($product['price']) == false || is_numeric($product['price']) == false || $product['price'] < 0) {
  435. util::fail('请填写' . $productName . '的花店价');
  436. }
  437. $price = $product['price'];
  438. if (isset($product['skPrice']) == false || is_numeric($product['skPrice']) == false || $product['skPrice'] < 0) {
  439. util::fail('请填写' . $productName . '的散客价');
  440. }
  441. $skPrice = $product['skPrice'];
  442. if (isset($productInfo->mainId) == false || $productInfo->mainId != $this->mainId) {
  443. util::fail('没有权限访问');
  444. }
  445. if (empty($price)) {
  446. continue;
  447. }
  448. $ptItemId = $productInfo->itemId;
  449. ProductClass::changeSinglePrice($shop, $ptItemId, $price, $skPrice, $changeParams);
  450. //在首店修改需要同步到所有直营店
  451. if ($default == 1) {
  452. foreach ($chainShopList as $chain) {
  453. if ($chain->id == $shop->id) {
  454. continue;
  455. }
  456. $chainMainId = $chain->mainId ?? 0;
  457. $staff = ShopAdminClass::getByCondition(['mainId' => $chainMainId, 'adminId' => $adminId], true);
  458. $changeParams2 = ['staffId' => $staff->id, 'staffName' => $staff->name, 'changeType' => $changeType, 'targetId' => $targetId];
  459. ProductClass::changeSinglePrice($chain, $ptItemId, $price, $skPrice, $changeParams2);
  460. }
  461. }
  462. }
  463. $mainId = $shop->mainId ?? 0;
  464. if (!empty($mainId)) {
  465. //提醒零售收银台界面要刷新
  466. $staffList = ShopAdminClass::getAllByCondition(['mainId' => $mainId], null, '*', null, true);
  467. if (!empty($staffList)) {
  468. foreach ($staffList as $staff) {
  469. $staffId = $staff->id ?? 0;
  470. Yii::$app->redis->executeCommand('SET', ['cashier_' . $mainId . '_' . $staffId . '_may_refresh', 'yes']);
  471. }
  472. }
  473. }
  474. $transaction->commit();
  475. util::complete('修改成功');
  476. } catch (\Exception $e) {
  477. $transaction->rollBack();
  478. Yii::info("修改失败原因:" . $e->getMessage());
  479. util::fail('修改失败');
  480. }
  481. }
  482. //改价
  483. public function actionChangePrice()
  484. {
  485. $shopAdmin = $this->shopAdmin;
  486. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  487. util::fail('超管才能修改');
  488. }
  489. $productId = Yii::$app->request->post('productId', 0);
  490. $productInfo = ProductClass::getById($productId, true);
  491. if (empty($productInfo)) {
  492. util::fail("花材不存在");
  493. }
  494. if ($productInfo->mainId != $this->mainId) {
  495. util::fail('没有权限');
  496. }
  497. $price = Yii::$app->request->post('price', '');
  498. if (is_numeric($price) && $price > 0) {
  499. ProductClass::changeSinglePrice($productInfo, $price, ProductClass::PRICE_LABEL_CHANGE);
  500. } else {
  501. $price = bcadd($productInfo->cost, $productInfo->addPrice, 2);
  502. ProductClass::changeSinglePrice($productInfo, $price, ProductClass::PRICE_LABEL_AUTO);
  503. }
  504. util::success(['price' => $price]);
  505. }
  506. //恢复自动调价
  507. public function actionAutoPrice()
  508. {
  509. $productId = Yii::$app->request->post('productId', 0);
  510. $productData = ProductClass::getById($productId);
  511. if (!$productData) {
  512. util::fail("花材不存在");
  513. }
  514. //2021.04.06改为用product中的成本价+加价
  515. $price = bcadd($productData['cost'], $productData['addPrice'], 2);
  516. ProductClass::changeSinglePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
  517. util::complete("操作成功");
  518. }
  519. //修改加价
  520. public function actionChangeAddPrice()
  521. {
  522. $productId = Yii::$app->request->post('productId', 0);
  523. $addPrice = Yii::$app->request->post('addPrice', '');
  524. if ($addPrice < 0) {
  525. util::fail("加价不能小于0");
  526. }
  527. $productData = ProductClass::getProductData($productId, $this->mainId);
  528. if (!$productData) {
  529. util::fail("花材不存在");
  530. }
  531. ProductClass::changeAddPrice($productId, $addPrice);
  532. util::complete();
  533. }
  534. //批量修改更多,包括排序、重量和保质期 ssh 20211211
  535. public function actionBatchChangeMore()
  536. {
  537. $shop = $this->shop;
  538. $default = $shop->default ?? 0;
  539. if ($default == 0) {
  540. util::fail('请在首店修改');
  541. }
  542. $shopAdmin = $this->shopAdmin;
  543. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  544. util::fail('超管才能操作');
  545. }
  546. $addData = Yii::$app->request->post('addData', '');
  547. if (empty($addData)) {
  548. util::fail('没有花材');
  549. }
  550. $itemData = json_decode($addData, true);
  551. if (is_array($itemData) == false) {
  552. util::fail('没有花材...');
  553. }
  554. $ids = array_column($itemData, 'id');
  555. $infoList = ProductClass::getByIds($ids, null, 'id');
  556. if (empty($infoList)) {
  557. util::fail('没有花材');
  558. }
  559. foreach ($infoList as $info) {
  560. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  561. util::fail('不是你的花材不能修改');
  562. }
  563. }
  564. //首店修改同步到所有直营店
  565. $sjId = $shop->sjId ?? 0;
  566. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  567. foreach ($itemData as $key => $data) {
  568. $id = $data['id'] ?? 0;
  569. unset($data['id']);
  570. ProductClass::updateById($id, $data);
  571. $ptItemId = $infoList[$id]['itemId'] ?? 0;
  572. if (empty($ptItemId)) {
  573. continue;
  574. }
  575. if (isset($shop->default) && $shop->default == 1) {
  576. foreach ($chainShopList as $chainShop) {
  577. $currentMainId = $chainShop->mainId ?? 0;
  578. if ($currentMainId == $shop->mainId) {
  579. //前面已经修改过了,不需要重复修改
  580. continue;
  581. }
  582. ProductClass::updateByCondition(['mainId' => $currentMainId, 'itemId' => $ptItemId], $data);
  583. }
  584. }
  585. }
  586. util::complete('修改成功');
  587. }
  588. //批量修改加价 ssh 20211211
  589. public function actionBatchChangeAddPrice()
  590. {
  591. $shop = $this->shop;
  592. $join = $shop->join ?? 0;
  593. $default = $shop->default ?? 0;
  594. if ($join == 0 && $default == 0) {
  595. util::fail('请在首店操作');
  596. }
  597. $shopAdmin = $this->shopAdmin;
  598. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  599. util::fail('超管才能操作');
  600. }
  601. $addData = Yii::$app->request->post('addData', '');
  602. if (empty($addData)) {
  603. util::fail('没有花材');
  604. }
  605. $itemData = json_decode($addData, true);
  606. if (is_array($itemData) == false) {
  607. util::fail('没有花材...');
  608. }
  609. $ids = array_column($itemData, 'id');
  610. $infoList = ProductClass::getByIds($ids, null, 'id');
  611. if (empty($infoList)) {
  612. util::fail('没有花材');
  613. }
  614. foreach ($infoList as $info) {
  615. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  616. util::fail('不能修改别人的花材');
  617. }
  618. }
  619. //首店修改同步到所有直营店
  620. $sjId = $shop->sjId ?? 0;
  621. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  622. foreach ($itemData as $key => $data) {
  623. $id = $data['id'] ?? 0;
  624. unset($data['id']);
  625. ProductClass::updateById($id, $data);
  626. $ptItemId = $infoList[$id]['itemId'] ?? 0;
  627. if (empty($ptItemId)) {
  628. continue;
  629. }
  630. if (isset($shop->default) && $shop->default == 1) {
  631. foreach ($chainShopList as $chainShop) {
  632. if (isset($chainShop->join) && $chainShop->join == 1) {
  633. continue;
  634. }
  635. $currentMainId = $chainShop->mainId ?? 0;
  636. if ($currentMainId == $shop->mainId) {
  637. //前面已经修改过了,不需要重复修改
  638. continue;
  639. }
  640. ProductClass::updateByCondition(['mainId' => $currentMainId, 'itemId' => $ptItemId], $data);
  641. }
  642. }
  643. }
  644. util::complete('修改成功');
  645. }
  646. public function actionUpdate()
  647. {
  648. $shopAdmin = $this->shopAdmin;
  649. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  650. util::fail('超管才能操作');
  651. }
  652. $shop = $this->shop;
  653. $join = $shop->join ?? 0;
  654. $default = $shop->default ?? 0;
  655. if ($join == 0 && $default == 0) {
  656. //因为盘点不能将花材盘成0,所以这边放出入口允许创始人在分店修改分店的花材库存
  657. if ($shopAdmin->super != 1) {
  658. util::fail('请先开通超管权限');
  659. }
  660. }
  661. $post = Yii::$app->request->post();
  662. $post['adminId'] = $this->adminId;
  663. $post['staffId'] = $this->shopAdmin->id;
  664. $post['staffName'] = $this->shopAdmin->name;
  665. ProductClass::updateProduct($post, $this->shop);
  666. util::complete();
  667. }
  668. //上下架状态控制 ssh 20210713
  669. public function actionStatusUpdate()
  670. {
  671. $post = Yii::$app->request->post();
  672. $shopAdmin = $this->shopAdmin;
  673. $roleId = $shopAdmin['roleId'] ?? 0;
  674. $role = AdminRoleClass::getById($roleId);
  675. $roleName = $role['roleName'] ?? '';
  676. if ($roleName == '员工') {
  677. util::fail('没有权限操作哦');
  678. }
  679. $id = $post['id'] ?? 0;
  680. $status = $post['status'] ?? 1;
  681. $product = ProductClass::getById($id, true);
  682. if ($product->mainId != $this->mainId) {
  683. util::fail('没有权限操作');
  684. }
  685. $product->status = $status;
  686. if ($status == 2) {
  687. $product->discountPrice = 0;
  688. }
  689. $product->save();
  690. util::complete();
  691. }
  692. //花材展示状态控制 ssh 20210804
  693. public function actionDelStatusUpdate()
  694. {
  695. $post = Yii::$app->request->post();
  696. $shop = $this->shop;
  697. $join = $shop->join ?? 0;
  698. $default = $shop->default ?? 0;
  699. if ($join == 0 && $default == 0) {
  700. util::fail('请在首店操作');
  701. }
  702. $shopAdmin = $this->shopAdmin;
  703. $roleId = $shopAdmin['roleId'] ?? 0;
  704. $role = AdminRoleClass::getById($roleId);
  705. $roleName = $role['roleName'] ?? '';
  706. if ($roleName == '员工') {
  707. util::fail('没有权限操作');
  708. }
  709. $id = $post['id'] ?? 0;
  710. $delStatus = $post['delStatus'] ?? 0;
  711. $product = ProductClass::getById($id, true);
  712. if ($product->mainId != $this->mainId) {
  713. util::fail('没有权限操作');
  714. }
  715. $product->delStatus = $delStatus;
  716. $product->save();
  717. $ptItemId = $product->itemId ?? 0;
  718. if (isset($shop->default) && $shop->default == 1) {
  719. //首店修改同步到所有直营店
  720. $sjId = $shop->sjId ?? 0;
  721. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  722. foreach ($chainShopList as $chainShop) {
  723. $chainShopId = $chainShop->id ?? 0;
  724. if (isset($chainShop->join) && $chainShop->join == 1) {
  725. continue;
  726. }
  727. if ($chainShopId == $shop->id) {
  728. continue;
  729. }
  730. $chainMainId = $chainShop->mainId ?? 0;
  731. $chainProduct = ProductClass::getByCondition(['mainId' => $chainMainId, 'itemId' => $ptItemId], true);
  732. if (!empty($chainProduct)) {
  733. $chainProduct->delStatus = $delStatus;
  734. $chainProduct->save();
  735. }
  736. }
  737. }
  738. util::complete();
  739. }
  740. //详情 ruidian 2021.5.3
  741. public function actionDetail()
  742. {
  743. $id = Yii::$app->request->get('id', 0);
  744. $respond = ProductClass::getItemInfo($id);
  745. $ptItemId = $respond['itemId'] ?? 0;
  746. $ptItemInfo = PtItemClass::getById($ptItemId);
  747. $respond['ptItemInfo'] = $ptItemInfo;
  748. util::success($respond);
  749. }
  750. //新增花材 2021.6.21
  751. public function actionAdd()
  752. {
  753. $post = Yii::$app->request->post();
  754. $shop = $this->shop;
  755. $default = $shop->default ?? 0;
  756. if ($default == 0) {
  757. util::fail('请在默认门店添加花材,分店只能修改花材');
  758. }
  759. $shopAdmin = $this->shopAdmin;
  760. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  761. util::fail('管理员才能添加花材');
  762. }
  763. $post['sjId'] = $this->sjId;
  764. $post['adminId'] = $this->adminId;
  765. $post['mainId'] = $this->mainId;
  766. $price = $post['price'] ?? 0;
  767. $addPrice = $post['addPrice'] ?? 0;
  768. if ($addPrice > $price) {
  769. util::fail('加价不能大于售价');
  770. }
  771. $cost = bcsub($price, $addPrice, 2);
  772. $post['cost'] = $cost;
  773. $post['staffName'] = $shopAdmin->name ?? '';
  774. ProductClass::addProduct($post, $this->shop);
  775. util::complete('添加成功');
  776. }
  777. //获取拼音缩写 ssh 20210707
  778. public function actionPy()
  779. {
  780. $name = Yii::$app->request->get('name');
  781. $py = stringUtil::py($name);
  782. util::success(['py' => $py]);
  783. }
  784. //(客户端-批发商)复制花材
  785. public function actionClone()
  786. {
  787. $post = Yii::$app->request->post();
  788. $shop = $this->shop;
  789. $join = $shop->join ?? 0;
  790. $default = $shop->default ?? 0;
  791. if ($join == 0 && $default == 0) {
  792. util::fail('请在首店操作');
  793. }
  794. $shopAdmin = $this->shopAdmin;
  795. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  796. util::fail('管理员才能添加花材');
  797. }
  798. $shop = $this->shop;
  799. $default = $shop->default ?? 0;
  800. if ($default != 1) {
  801. util::fail('请在总店添加');
  802. }
  803. $post['sjId'] = $this->sjId;
  804. $post['adminId'] = $this->adminId;
  805. $post['mainId'] = $this->mainId;
  806. $post['staffName'] = $shopAdmin->name ?? '';
  807. $price = $post['price'] ?? 0;
  808. $addPrice = $post['addPrice'] ?? 0;
  809. if ($addPrice > $price) {
  810. util::fail('加价不能大于售价');
  811. }
  812. $cost = bcsub($price, $addPrice, 2);
  813. $post['cost'] = $cost;
  814. $post['copy'] = 1; //复制标识
  815. //去除无用的字段
  816. unset($post['viewNum']);
  817. unset($post['actualSold']);
  818. unset($post['itemId']);
  819. unset($post['id']);
  820. unset($post['delStatus']);
  821. ProductClass::addProduct($post, $this->shop);
  822. util::complete('复制成功');
  823. }
  824. //下载价格表 ssh 20210922
  825. public function actionGeneratePriceTable()
  826. {
  827. $level = Yii::$app->request->get('level', 0);
  828. $respond = ProductService::generatePriceTable($this->sjId, $level, $this->mainId);
  829. $file = $respond['file'] ?? '';
  830. $shortFile = $respond['shortFile'] ?? '';
  831. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  832. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  833. }
  834. public function actionGenerateItemTable()
  835. {
  836. $level = Yii::$app->request->get('level', 0);
  837. $respond = ProductService::generateItemTable($this->sjId, $level, $this->mainId);
  838. $file = $respond['file'] ?? '';
  839. $shortFile = $respond['shortFile'] ?? '';
  840. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  841. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  842. }
  843. //下载条形码 lqh 20211227
  844. public function actionGenerateBarcodeTable()
  845. {
  846. $respond = ProductService::generateBarcodeTable($this->sjId, $this->shopId, $this->mainId);
  847. $file = $respond['file'] ?? '';
  848. $shortFile = $respond['shortFile'] ?? '';
  849. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  850. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  851. }
  852. }