ShopController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\shop\classes\MainClass;
  4. use biz\shop\classes\ShopAdminClass;
  5. use biz\sj\services\MerchantExtendService;
  6. use bizGhs\admin\classes\AdminClass;
  7. use bizGhs\custom\classes\CustomClass;
  8. use bizGhs\ghs\classes\GhsClass;
  9. use bizGhs\merchant\classes\ShopClass;
  10. use bizGhs\merchant\services\ShopService;
  11. use bizHd\saas\services\RegionService;
  12. use common\components\arrayUtil;
  13. use common\components\dict;
  14. use common\components\dirUtil;
  15. use common\components\httpUtil;
  16. use common\components\imgUtil;
  17. use common\components\noticeUtil;
  18. use common\components\stringUtil;
  19. use common\components\util;
  20. use common\components\wxUtil;
  21. use Yii;
  22. use bizGhs\stat\classes\StatSaleClass;
  23. class ShopController extends BaseController
  24. {
  25. public $guestAccess = ['all'];
  26. //修改客户微信二维码 ssh 20240714
  27. public function actionChangeWx()
  28. {
  29. $get = Yii::$app->request->get();
  30. $url = $get['url'] ?? '';
  31. $shop = $this->shop;
  32. $shop->superWx = $url;
  33. $shop->save();
  34. util::complete('修改成功');
  35. }
  36. //我的可以进入的门店 ssh 20230424
  37. public function actionMyShop()
  38. {
  39. $staff = $this->shopAdmin;
  40. $shopList = [];
  41. if (isset($staff->super) && $staff->super == 1) {
  42. $sjId = $this->sjId ?? 0;
  43. $shopList = ShopClass::getAllByCondition(['sjId' => $sjId, 'ptStyle' => 2], null, 'id,shopName,merchantName,sjId,mainId,ptStyle', 'id');
  44. }
  45. $adminId = $this->adminId;
  46. $staffList = ShopAdminClass::getAllByCondition(['adminId' => $adminId], null, '*', null, true);
  47. $mainIdList = [];
  48. if (!empty($staffList)) {
  49. foreach ($staffList as $staff) {
  50. $isPt = $staff->isPt ?? 0;
  51. $delStatus = $staff->delStatus ?? 0;
  52. //$super = $staff->super ?? 0;
  53. $mainId = $staff->mainId ?? 0;
  54. if ($isPt == 0 && $delStatus == 0) {
  55. $mainIdList[] = $mainId;
  56. }
  57. }
  58. }
  59. if (!empty($mainIdList)) {
  60. $addShopList = ShopClass::getAllByCondition(['mainId' => ['in', $mainIdList], 'ptStyle' => 2], null, 'id,shopName,merchantName,sjId,mainId,ptStyle', 'id');
  61. if (!empty($addShopList)) {
  62. foreach ($addShopList as $addShop) {
  63. $addShopId = $addShop['id'] ?? 0;
  64. if (!isset($shopList[$addShopId])) {
  65. $shopList[$addShopId] = $addShop;
  66. }
  67. }
  68. }
  69. }
  70. $list = array_values($shopList);
  71. util::success(['list' => $list]);
  72. }
  73. //绑定零售店 ssh 20220430
  74. public function actionBindLsShop()
  75. {
  76. $id = Yii::$app->request->get('id');
  77. $connection = Yii::$app->db;
  78. $transaction = $connection->beginTransaction();
  79. util::fail('此方法已停用,如需启用请确认有没问题');
  80. try {
  81. $shop = $this->shop ?? [];
  82. $lsShopId = $shop->lsShopId ?? 0;
  83. if (!empty($lsShopId)) {
  84. util::fail('您已经绑定过了');
  85. }
  86. ShopClass::bindLsShopByCustomId($shop, $id, $this->adminId);
  87. $transaction->commit();
  88. util::complete();
  89. } catch (\Exception $exception) {
  90. $transaction->rollBack();
  91. util::fail('绑定失败');
  92. }
  93. }
  94. //预订设置 ssh 20220324
  95. public function actionBookSet()
  96. {
  97. $get = Yii::$app->request->get();
  98. $presell = $get['presell'] ?? 0;
  99. $this->shop->presell = $presell;
  100. $this->shop->save();
  101. util::complete();
  102. }
  103. //当前门店信息
  104. public function actionInfo()
  105. {
  106. $shop = $this->shop ?? [];
  107. util::success($shop);
  108. }
  109. //获取当前门店信息 ssh 2021.3.27
  110. public function actionCurrentShop()
  111. {
  112. $main = $this->shop;
  113. util::success($main);
  114. }
  115. //门店列表 ssh 2021.1.14
  116. public function actionList()
  117. {
  118. $where = [];
  119. $where['sjId'] = $this->sjId;
  120. $where['delStatus'] = 0;
  121. $list = ShopClass::getShopList($where);
  122. util::success($list);
  123. }
  124. //更新门店 ssh 2020.2.29
  125. public function actionUpdate()
  126. {
  127. $post = Yii::$app->request->post();
  128. $post['sjId'] = $this->sjId;
  129. $id = $post['id'] ?? 0;
  130. $shop = ShopService::getById($id);
  131. if (empty($shop)) {
  132. util::fail('没有找到门店,编号657701');
  133. }
  134. $staff = $this->shopAdmin;
  135. if (isset($staff->finance) == false || $staff->finance == 0) {
  136. //花掌柜的小石,源花汇的小吕和小吴,没有财务权限也要能修改门店
  137. if (!in_array($this->adminId, [4, 11961, 11976])) {
  138. util::fail('没有修改门店权限');
  139. }
  140. }
  141. if (isset($staff->super) == false || $staff->super == 0) {
  142. //花掌柜的小石,源花汇的小吕和小吴,没有改库存改价格权限也要能修改门店
  143. if (!in_array($this->adminId, [4, 11961, 11976])) {
  144. util::fail('没有修改门店权限哦');
  145. }
  146. }
  147. if ($this->mainId == 59421) {
  148. if (!in_array($this->adminId, [4])) {
  149. util::fail('已停用,请联系管理员');
  150. }
  151. }
  152. $post['meetNum'] = isset($post['meetNum']) && !empty($post['meetNum']) ? $post['meetNum'] : 0;
  153. $post['meetAmount'] = isset($post['meetAmount']) && !empty($post['meetAmount']) ? $post['meetAmount'] : 0;
  154. $post['cutAmount'] = isset($post['cutAmount']) && !empty($post['cutAmount']) ? $post['cutAmount'] : 0;
  155. ShopClass::valid($shop, $this->sjId);
  156. //手机号具有唯一性,暂时不允许修改
  157. unset($post['mobile']);
  158. $post['img'] = isset($post['img']) && is_array($post['img']) ? json_encode($post['img']) : '';
  159. \biz\shop\classes\ShopClass::updateShop($shop, $post);
  160. util::complete();
  161. }
  162. // 更新门店头像
  163. public function actionUpdateAvatar()
  164. {
  165. $post = Yii::$app->request->post();
  166. $shopId = $post['shopId'];
  167. if ($post['shopId'] != $this->shopId or $this->mainId == 0) {
  168. util::fail('不是您的门店');
  169. }
  170. $avatar = $post['avatar'];
  171. if ($avatar == '') {
  172. util::fail('头像地址不能为空');
  173. }
  174. $shop = ShopClass::getById($shopId, true);
  175. $shop->avatar = $avatar;
  176. $shop->save();
  177. $ptStyle = $shop['ptStyle'] ?? dict::getDict('ptStyle', 'hd');
  178. $update = ['avatar' => $avatar];
  179. $where = ['mainId' => $this->mainId];
  180. if ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
  181. GhsClass::updateByCondition($where, $update);
  182. } else {
  183. util::fail('门店头像更新失败,平台类型错误');
  184. }
  185. $shopImg = imgUtil::groupImg($shop->avatar);
  186. $smallShopImg = imgUtil::groupImg($shop->avatar) . "?x-oss-process=image/resize,m_fill,h_80,w_80";
  187. util::success(['shopImg' => $shopImg, 'smallShopImg' => $smallShopImg], '门店头像更新成功');
  188. }
  189. //添加门店 ssh 2021.1.14
  190. public function actionAdd()
  191. {
  192. $shop = $this->shop;
  193. $default = $shop->default ?? 0;
  194. if ($default == 0) {
  195. util::fail('当前直营分店,请切回总店添加');
  196. }
  197. $adminId = $this->adminId;
  198. $ptSuperAdminId = \biz\admin\classes\AdminClass::getPtSuperAdminId();
  199. if ($adminId != $shop->adminId && $adminId != $ptSuperAdminId) {
  200. util::fail('本店超管才能添加门店');
  201. }
  202. $mainId = $this->mainId;
  203. if (getenv('YII_ENV') == 'production') {
  204. if ($mainId == 42490) {
  205. util::fail('无法建分店,请联系石头');
  206. }
  207. } else {
  208. if ($mainId == 644) {
  209. util::fail('无法建分店,请联系石头');
  210. }
  211. }
  212. $data = Yii::$app->request->post();
  213. $data['sjId'] = $this->sjId;
  214. $data['shopId'] = $this->shopId;
  215. $data['adminId'] = $this->adminId;
  216. if (isset($data['shopName']) == false || empty($data['shopName'])) {
  217. util::fail('请填写门店名称');
  218. }
  219. $sj = $this->sj;
  220. $sjName = $sj->name ?? '';
  221. $ptStyle = $sj->style ?? dict::getDict('ptStyle', 'hd');
  222. $data['ptStyle'] = $ptStyle;
  223. $data['merchantName'] = $sjName;
  224. $data['default'] = 0;
  225. $connection = Yii::$app->db;
  226. $transaction = $connection->beginTransaction();
  227. try {
  228. $mobile = $data['mobile'] ?? 0;
  229. if (stringUtil::isMobile($mobile) == false) {
  230. util::fail('请填写正确手机号');
  231. }
  232. $adminInfo = ['name' => $mobile, 'mobile' => $mobile];
  233. $fromApp = dict::getDict('userSourceGetId', 'app', 'id');
  234. $admin = AdminClass::replaceAdmin($adminInfo, $fromApp);
  235. $adminId = $admin['id'] ?? 0;
  236. $data['adminId'] = $adminId;
  237. ShopClass::addMainShop($data, $this->shop, $this->sj);
  238. $transaction->commit();
  239. util::complete();
  240. } catch (\Exception $exception) {
  241. $transaction->rollBack();
  242. util::fail();
  243. }
  244. }
  245. //PC下载微信和支付宝收款码 ssh 2020.2.29
  246. public function actionGatheringCode()
  247. {
  248. $id = Yii::$app->request->get('id', 0);
  249. $shop = ShopService::getById($id);
  250. ShopClass::valid($shop, $this->sjId);
  251. $gatheringCode = ShopClass::generateGatheringCode($this->sjId, $id);
  252. $file = dirUtil::getImgDir() . $gatheringCode;
  253. if (file_exists($file) == false) {
  254. util::fail('没有找到收款码');
  255. }
  256. if (httpUtil::isMiniProgram()) {
  257. $imgUrl = imgUtil::getPrefix() . $gatheringCode;
  258. util::success(['imgUrl' => $imgUrl]);
  259. }
  260. $fileName = "门店({$id})收款码.jpg";
  261. $contentType = 'image/jpeg';
  262. header("Cache-control: private");
  263. header("Content-type: $contentType"); //设置要下载的文件类型
  264. header("Content-Length:" . filesize($file)); //设置要下载文件的文件大小
  265. header("Content-Disposition: attachment; filename=" . urldecode($fileName)); //设置要下载文件的文件名
  266. readfile($file);
  267. }
  268. //PC获取小程序的收款码 ssh
  269. public function actionGatheringMiniCode()
  270. {
  271. $id = Yii::$app->request->get('id', 0);
  272. $extend = MerchantExtendService::getBySjId($this->sjId);
  273. if ($extend['miniAuth'] == 0) {
  274. util::fail('您的小程序还没有授权');
  275. }
  276. $shop = ShopService::getById($id);
  277. ShopClass::valid($shop, $this->sjId);
  278. $applyMemberCode = wxUtil::generateGatheringMiniCode($this->sj->attributes, $id);
  279. $file = dirUtil::getImgDir() . $applyMemberCode;
  280. if (file_exists($file) == false) {
  281. util::fail('没有找到注册会员码');
  282. }
  283. if (httpUtil::isMiniProgram()) {
  284. $imgUrl = imgUtil::getPrefix() . $applyMemberCode;
  285. util::success(['imgUrl' => $imgUrl]);
  286. }
  287. $fileName = "门店({$id})小程序收款码.jpg";
  288. $contentType = 'image/jpeg';
  289. header("Cache-control: private");
  290. header("Content-type: $contentType"); //设置要下载的文件类型
  291. header("Content-Length:" . filesize($file)); //设置要下载文件的文件大小
  292. header("Content-Disposition: attachment; filename=" . urldecode($fileName)); //设置要下载文件的文件名
  293. readfile($file);
  294. }
  295. //PC下载会员注册码 ssh 2020.2.29
  296. public function actionApplyMemberCode()
  297. {
  298. $id = Yii::$app->request->get('id', 0);
  299. $extend = MerchantExtendService::getBySjId($this->sjId);
  300. if ($extend['miniAuth'] == 0) {
  301. util::fail('您的小程序还没有授权');
  302. }
  303. $shop = ShopService::getById($id);
  304. ShopClass::valid($shop, $this->sjId);
  305. $applyMemberCode = wxUtil::generateMemberCode($this->sj->attributes, $id);
  306. $file = dirUtil::getImgDir() . $applyMemberCode;
  307. if (file_exists($file) == false) {
  308. util::fail('没有找到注册会员码');
  309. }
  310. if (httpUtil::isMiniProgram()) {
  311. $imgUrl = imgUtil::getPrefix() . $applyMemberCode;
  312. util::success(['imgUrl' => $imgUrl]);
  313. }
  314. $fileName = "注册会员码{$id}.jpg";
  315. $contentType = 'image/jpeg';
  316. header("Cache-control: private");
  317. header("Content-type: $contentType"); //设置要下载的文件类型
  318. header("Content-Length:" . filesize($file)); //设置要下载文件的文件大小
  319. header("Content-Disposition: attachment; filename=" . urldecode($fileName)); //设置要下载文件的文件名
  320. readfile($file);
  321. }
  322. //删除门店 ssh 2020.3.1
  323. public function actionDelete()
  324. {
  325. util::fail('禁止操作');
  326. $id = Yii::$app->request->get('id', 0);
  327. $shop = ShopService::getById($id);
  328. ShopClass::valid($shop, $this->sjId);
  329. ShopClass::deleteShop($shop);
  330. util::complete();
  331. }
  332. //获取所有门店lqh 2021.1.31
  333. public function actionAll()
  334. {
  335. $res = ShopClass::getAllShop($this->sjId);
  336. util::success($res);
  337. }
  338. //切换门店 lqh 2021.1.31
  339. public function actionToggleShop()
  340. {
  341. $mainId = $this->mainId;
  342. $adminId = $this->adminId;
  343. if (getenv('YII_ENV') == 'production') {
  344. //安海恋善指定人不能切换门店
  345. if (in_array($mainId, [44759, 56314, 56315, 56452])) {
  346. if (in_array($adminId, [54630, 54723])) {
  347. util::fail('不能切换门店,原因是容易误操作或收银机登录使用');
  348. }
  349. }
  350. } else {
  351. if (in_array($mainId, [644])) {
  352. if (in_array($adminId, [919])) {
  353. //util::fail('不能切换门店,原因是容易误操作或收银机登录使用');
  354. }
  355. }
  356. }
  357. $shopAdmin = $this->shopAdmin;
  358. $switchShop = ShopAdminClass::hasSwitchShopRight($shopAdmin);
  359. if ($switchShop == 0) {
  360. util::fail('不能切换门店');
  361. }
  362. $newShopId = Yii::$app->request->post('shopId', 0);
  363. $newShop = ShopClass::getById($newShopId, true);
  364. if (empty($newShop)) {
  365. util::fail('没有找到门店66');
  366. }
  367. $res = AdminClass::toggleShop($this->admin, $newShop, $this->sjId, $this->shopAdmin);
  368. util::success($res);
  369. }
  370. //提现信息更新 ssh 2021.3.23
  371. public function actionCashUpdate()
  372. {
  373. $get = Yii::$app->request->get();
  374. $cashAccount = $get['cashAccount'] ?? '';
  375. $cashName = $get['cashName'] ?? '';
  376. $cashBank = $get['cashBank'] ?? '';
  377. if (empty($cashName)) {
  378. util::fail('请填写姓名');
  379. }
  380. if (empty($cashAccount)) {
  381. util::fail('请填写银行卡号');
  382. }
  383. if (empty($cashBank)) {
  384. util::fail('请输入开户行');
  385. }
  386. $shopAdmin = $this->shopAdmin;
  387. $mainId = $shopAdmin->mainId ?? 0;
  388. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  389. util::fail('超管才能操作');
  390. }
  391. MainClass::updateById($mainId, ['cashAccount' => $cashAccount, 'cashName' => $cashName, 'cashBank' => $cashBank]);
  392. $shop = $this->shop;
  393. $sjName = $shop->merchantName ?? '';
  394. $shopName = $shop->shopName ?? '';
  395. //noticeUtil::push("{$sjName}-{$shopName} 修改提现账号,{$cashName} {$cashAccount} {$cashBank}", '15280215347');
  396. util::complete();
  397. }
  398. //获取门店详情 ssh 2021.4.23
  399. public function actionDetail()
  400. {
  401. $shopId = Yii::$app->request->get('shopId');
  402. if (empty($shopId)) {
  403. $shopId = $this->shopId;
  404. }
  405. $shop = ShopClass::getById($shopId, true);
  406. if ($shop->mainId != $this->mainId) {
  407. //这个注释不能打开,因为首店可能修改其它分店信息
  408. //util::fail('无法查看');
  409. }
  410. util::success(['info' => $shop]);
  411. }
  412. //获取平台所有的供货商门店 ssh 20210908
  413. public function actionGetPtGhsShop()
  414. {
  415. $hasSale = Yii::$app->request->get('hasSale', 0);
  416. $name = Yii::$app->request->get('name', '');
  417. $arr = ['ptStyle' => 2, 'live' => 1];
  418. if (!empty($name)) {
  419. $arr['merchantName'] = ['like', $name];
  420. $hasSale = 0;
  421. }
  422. $shopList = ShopClass::getAllByCondition($arr, 'id asc', '*');
  423. $totalSale = 0;
  424. $totalShopNum = 0;
  425. $totalBalance = 0;
  426. if (!empty($shopList)) {
  427. foreach ($shopList as $key => $shop) {
  428. $mainId = $shop['mainId'] ?? 0;
  429. $main = MainClass::getById($mainId, true);
  430. $balance = $main->balance ?? 0;
  431. if (!empty($name) || $hasSale == 0) {
  432. $incomeList = [];
  433. } else {
  434. $respond = StatSaleClass::profile($mainId);
  435. $incomeList = $respond['income'] ?? [];
  436. }
  437. $todaySale = 0;
  438. if (!empty($incomeList)) {
  439. foreach ($incomeList as $item) {
  440. //调拨出库暂时不算到收入里去
  441. if (isset($item['id']) && $item['id'] == 'allot') {
  442. continue;
  443. }
  444. $todaySale = bcadd($todaySale, $item['amount'], 2);
  445. }
  446. }
  447. $shopList[$key]['balance'] = $balance;
  448. $shopList[$key]['todaySale'] = $todaySale;
  449. $count = CustomClass::getCount(['ownMainId' => $mainId]);
  450. $count = $count > 0 ? $count : 0;
  451. $shopList[$key]['shopNum'] = $count;
  452. if ($hasSale == 1 && $todaySale <= 0) {
  453. unset($shopList[$key]);
  454. }
  455. $totalSale = bcadd($totalSale, $todaySale, 2);
  456. $totalSale = floatval($totalSale);
  457. $totalBalance = bcadd($totalBalance, $balance, 2);
  458. $totalBalance = floatval($totalBalance);
  459. $totalShopNum = bcadd($totalShopNum, $count);
  460. }
  461. $shopList = arrayUtil::arraySort($shopList, 'todaySale');
  462. }
  463. util::success(['list' => $shopList, 'totalSale' => $totalSale, 'totalBalance' => $totalBalance, 'totalShopNum' => $totalShopNum]);
  464. }
  465. //切换门店 ssh 20220624
  466. public function actionSwitchGhsShop()
  467. {
  468. $shopId = Yii::$app->request->get('shopId', 0);
  469. $shop = ShopClass::getById($shopId, true);
  470. if (empty($shop)) {
  471. util::fail('没有找到门店67');
  472. }
  473. $shopAdmin = $this->shopAdmin;
  474. $admin = $this->admin;
  475. $adminId = $this->adminId ?? 0;
  476. if (getenv('YII_ENV') == 'production') {
  477. if (in_array($adminId, [4]) == false) {
  478. util::fail('开发中');
  479. }
  480. } else {
  481. if (in_array($adminId, [919]) == false) {
  482. util::fail('开发中');
  483. }
  484. }
  485. ShopAdminClass::changeShopAddRelate($shopAdmin, $shop, 1);
  486. $admin->currentGhsShopId = $shopId;
  487. $lsShopId = $shop->lsShopId ?? 0;
  488. $admin->currentShopId = $lsShopId;
  489. $admin->save();
  490. util::complete();
  491. }
  492. //散客门店详情 ssh 20211007
  493. public function actionSkCustom()
  494. {
  495. $skCustomId = $this->shop->skCustomId ?? 0;
  496. $custom = [];
  497. if (!empty($skCustomId)) {
  498. $custom = CustomClass::getById($skCustomId);
  499. $avatar = $custom['avatar'] ?? '';
  500. $custom['avatar'] = imgUtil::groupImg($avatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  501. }
  502. util::success(['custom' => $custom]);
  503. }
  504. //散客门店设置
  505. public function actionSkCustomSet()
  506. {
  507. $id = Yii::$app->request->get('id', 0);
  508. $custom = CustomClass::getById($id, true);
  509. CustomClass::valid($custom, $this->shopId);
  510. $this->shop->skCustomId = $id;
  511. $this->shop->save();
  512. util::complete('修改成功');
  513. }
  514. //运费模式和运费成本设置 ssh 20211014
  515. public function actionUpdateCgModel()
  516. {
  517. $get = Yii::$app->request->get();
  518. $cgModel = $get['cgModel'] ?? 0;
  519. $cgPerCost = $get['cgPerCost'] ?? 0;
  520. if ($cgModel == 1) {
  521. if (is_numeric($cgPerCost) == false || $cgPerCost < 0) {
  522. util::fail('请输入运费成本');
  523. }
  524. $this->shop->cgPerCost = $cgPerCost;
  525. }
  526. $this->shop->cgModel = $cgModel;
  527. $this->shop->save();
  528. util::complete('修改成功');
  529. }
  530. //地区 ssh 20220606
  531. public function actionRegion()
  532. {
  533. $regionTree = RegionService::tree();
  534. util::success(['region' => $regionTree]);
  535. }
  536. }