ProductController.php 37 KB

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