ShopClass.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. namespace biz\shop\classes;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\pt\classes\PtAssetClass;
  5. use biz\sj\classes\SjClass;
  6. use bizGhs\product\classes\ProductClass;
  7. use common\components\dict;
  8. use common\components\httpUtil;
  9. use common\components\imgUtil;
  10. use common\components\qrCodeUtil;
  11. use common\components\stringUtil;
  12. use common\components\util;
  13. use Yii;
  14. use biz\base\classes\BaseClass;
  15. class ShopClass extends BaseClass
  16. {
  17. public static $baseFile = '\biz\shop\models\Shop';
  18. //获取图片信息 ssh 2021.3.14
  19. public static function getShopInfo($id)
  20. {
  21. $info = self::getById($id);
  22. if (empty($info)) {
  23. return $info;
  24. }
  25. $list = self::groupShopInfo([$info]);
  26. return current($list);
  27. }
  28. public static function getShopList($where)
  29. {
  30. $list = self::getList('*', $where, 'addTime DESC');
  31. return $list;
  32. }
  33. //查看店铺 ssh 2021.3.1
  34. public static function getShopByIds($ids)
  35. {
  36. $list = self::getByIds($ids);
  37. return self::groupShopInfo($list);
  38. }
  39. //门店 ssh 2021.3.1
  40. public static function groupShopInfo($list)
  41. {
  42. if (empty($list)) {
  43. return $list;
  44. }
  45. foreach ($list as $key => $val) {
  46. $list[$key]['avatar'] = imgUtil::groupImg($val['avatar']);
  47. }
  48. return $list;
  49. }
  50. //获取商家默认的门店ID ssh 2020.1.19
  51. public static function getDefaultShopId($merchant)
  52. {
  53. return isset($merchant['defaultShopId']) ? $merchant['defaultShopId'] : 0;
  54. }
  55. //创建门店 ssh 2020.2.8
  56. public static function addShop($data)
  57. {
  58. $data['createTime'] = date("Y-m-d H:i:s");
  59. $shopName = isset($data['shopName']) ? $data['shopName'] : '';
  60. if (empty($shopName)) {
  61. util::fail('请填写门店名称');
  62. }
  63. if (stringUtil::getWordNum($shopName) > 8) {
  64. util::fail('门店名称不能超过8个汉字,2个字母顶一个汉字');
  65. }
  66. $exists = self::getByCondition(['sjId' => $data['sjId'], 'shopName' => $shopName]);
  67. if (!empty($exists)) {
  68. util::fail('门店名称已经存在');
  69. }
  70. //补充fullAddress linqh 2021.4.18
  71. $data['fullAddress'] = $data['province'] . $data['city'] . $data['address'] . $data['floor'];
  72. $img = json_encode([]);
  73. if (isset($data['img']) && !empty($data['img'])) {
  74. $img = json_encode($data['img']);
  75. }
  76. $data['img'] = $img;
  77. if (isset($data['ptStyle']) == false) {
  78. util::fail('请先择平台类型');
  79. }
  80. $shop = self::add($data);
  81. $newShopId = $shop['id'];
  82. //初始化花材 linqh 2021.5.11
  83. $adminId = $data['adminId'] ?? 0;
  84. $shopId = $data['shopId'] ?? 0;
  85. ProductClass::newShopInitProduct($data['sjId'], $shopId, $newShopId, $adminId);
  86. return $shop;
  87. }
  88. //更新门店 ssh 2020.2.29
  89. public static function updateShop($id, $data)
  90. {
  91. $shopName = isset($data['shopName']) ? $data['shopName'] : '';
  92. $sjId = $data['sjId'];
  93. $findShop = self::getByCondition(['sjId' => $sjId, 'shopName' => $shopName]);
  94. if (!empty($findShop) && $findShop['id'] != $id) {
  95. util::fail('门店名称已经存在');
  96. }
  97. return self::updateById($id, $data);
  98. }
  99. //取商家所有门店 ssh 2020.2.29
  100. public static function getAllShop($sjId)
  101. {
  102. return self::getAllByCondition(['sjId' => $sjId], null, "*");
  103. }
  104. //验证所属权限 ssh 2020.2.29
  105. public static function valid($shop, $sjId)
  106. {
  107. if (empty($shop)) {
  108. util::fail('门店无效');
  109. }
  110. if (isset($shop['sjId']) == false || $shop['sjId'] != $sjId) {
  111. util::fail('您没有权限访问');
  112. }
  113. }
  114. //删除门店 ssh 2020.3.1
  115. public static function deleteShop($shop)
  116. {
  117. if (isset($shop['default']) && $shop['default'] == 1) {
  118. util::fail('默认门店不允许删除');
  119. }
  120. $id = $shop['id'];
  121. self::updateById($id, ['delStatus' => 1]);
  122. }
  123. //生成收款码 ssh 2020.3.9
  124. public static function generateGatheringCode($sjId, $shopId)
  125. {
  126. $url = Yii::$app->params['mallDomain'] . "/#/pages/pay/index?account={$sjId}&shopId=" . $shopId;
  127. //强制转成https
  128. $url = httpUtil::becomeHttps($url);
  129. $gatheringCode = qrCodeUtil::generateGatheringQrCode($url, $sjId, $shopId, '', 10);
  130. return $gatheringCode;
  131. }
  132. //客户下单引起的余额增加 shish 20210520
  133. public static function customKdAddBalance($shop, $amount, $order, $capitalType)
  134. {
  135. //门店余额增加
  136. $currentBalance = bcadd($shop->balance, $amount, 2);
  137. $shop->balance = $currentBalance;
  138. $shop->totalRecharge = bcadd($shop->totalRecharge, $amount, 2);
  139. //可提现余额同步增加
  140. $currentTx = bcadd($shop->txBalance, $amount, 2);
  141. $shop->txBalance = $currentTx;
  142. $shop->save();
  143. //增加余额变动记录
  144. $id = $order->id;
  145. $event = "客户下单 {$amount}元";
  146. $tx = dict::getDict('yeTx', 'can');
  147. $change = [
  148. 'relateId' => $id,
  149. 'capitalType' => $capitalType,
  150. 'amount' => $amount,
  151. 'balance' => $currentBalance,
  152. 'txBalance' => $currentTx,
  153. 'io' => 1,
  154. 'payWay' => $order->payWay,
  155. 'event' => $event,
  156. 'sjId' => $order->sjId,
  157. 'shopId' => $order->shopId,
  158. 'tx' => $tx,
  159. 'ptStyle' => $shop->ptStyle,
  160. ];
  161. ShopYeChangeClass::addChange($change, true);
  162. //平台余额增加
  163. PtAssetClass::customKdAddBalance($shop, $amount, $order, $capitalType, $tx);
  164. }
  165. //门店充值增加余额 ssh 2021.2.21
  166. //$fix = true 余额只能在指定门店使用
  167. public static function rechargeAddBalance($shop, $amount, $order, $tx, $capitalType, $fix = false)
  168. {
  169. //门店余额增加
  170. $currentBalance = bcadd($shop->balance, $amount, 2);
  171. $shop->balance = $currentBalance;
  172. $currentTotalRecharge = bcadd($shop->totalRecharge, $amount, 2);
  173. $shop->totalRecharge = $currentTotalRecharge;
  174. //定向消费余额
  175. if ($fix) {
  176. $currentFix = bcadd($shop->fixBalance, $amount, 2);
  177. $shop->fixBalance = $currentFix;
  178. }
  179. $currentTx = $shop->txBalance;
  180. //提现余额
  181. if ($tx == dict::getDict('yeTx', 'can')) {
  182. $currentTx = bcadd($shop->txBalance, $amount, 2);
  183. $shop->txBalance = $currentTx;
  184. }
  185. $shop->save();
  186. $order->totalRecharge = $currentTotalRecharge;
  187. $order->save();
  188. //增加余额变动记录
  189. $id = $order->id;
  190. $event = "充值{$amount}元";
  191. if ($order->shopAdminId == 0) {
  192. $event = "系统充值{$amount}元";
  193. }
  194. $change = [
  195. 'relateId' => $id,
  196. 'capitalType' => $capitalType,
  197. 'amount' => $amount,
  198. 'balance' => $currentBalance,
  199. 'txBalance' => $currentTx,
  200. 'io' => 1,
  201. 'payWay' => $order->payWay,
  202. 'event' => $event,
  203. 'sjId' => $order->sjId,
  204. 'shopId' => $order->shopId,
  205. 'tx' => $tx,
  206. 'ptStyle' => $shop->ptStyle,
  207. ];
  208. ShopYeChangeClass::addChange($change, true);
  209. //平台余额增加
  210. PtAssetClass::rechargeAddBalance($shop, $amount, $order, $capitalType, $tx);
  211. }
  212. //采购余额付款,减少余额 shish 20210519
  213. public static function purchaseReduceBalance($shop, $amount, $order)
  214. {
  215. if (bccomp($amount, $shop->balance, 2) == 1) {
  216. util::fail('余额不足');
  217. }
  218. $sjId = $order->sjId;
  219. $shopId = $order->shopId;
  220. //定向消费余额
  221. $currentFixBalance = $shop->fixBalance;
  222. $ghsId = $order->ghsId;
  223. $ghs = GhsClass::getById($ghsId, true);
  224. if (empty($ghs)) {
  225. util::fail('没有找到供货商');
  226. }
  227. //找出推荐人
  228. $ghsShopId = $ghs->shopId;
  229. $parentShopId = SjClass::getParentShopId($sjId);
  230. //指定消费判断
  231. if ($ghsShopId != $parentShopId) {
  232. //如果不是在自己推荐人那边采购,需确认扣掉定向消费余额后还够不够扣
  233. $notFixBalance = bcsub($shop->balance, $currentFixBalance, 2);
  234. if (bccomp($amount, $notFixBalance, 2) == 1) {
  235. util::fail('充值余额只能在介绍您的门店消费');
  236. }
  237. } else {
  238. //需要定向消费余额不够本次扣时全部扣掉
  239. if ($currentFixBalance > 0) {
  240. $reduceFix = $currentFixBalance > $amount ? $amount : $currentFixBalance;
  241. $shop->fixBalance = bcsub($currentFixBalance, $reduceFix, 2);
  242. }
  243. }
  244. $currentBalance = bcsub($shop->balance, $amount, 2);
  245. $shop->balance = $currentBalance;
  246. $tx = dict::getDict('yeTx', 'canNot');
  247. //还有可提现余额也要先用掉
  248. $currentTxBalance = $shop->txBalance;
  249. if ($currentTxBalance > 0) {
  250. $reduceTxBalance = $currentTxBalance > $amount ? $amount : $currentTxBalance;
  251. $shop->txBalance = bcsub($currentTxBalance, $reduceTxBalance, 2);
  252. $tx = dict::getDict('yeTx', 'can');
  253. }
  254. $shop->save();
  255. $capitalType = dict::getDict('capitalType', 'xhPurchase', 'id');
  256. //增加余额变动记录
  257. $id = $order->id;
  258. $change = [
  259. 'relateId' => $id,
  260. 'capitalType' => $capitalType,
  261. 'amount' => $amount,
  262. 'balance' => $currentBalance,
  263. 'txBalance' => $currentTxBalance,
  264. 'io' => 0,
  265. 'payWay' => $order->payWay,
  266. 'event' => "余额支付{$amount}元",
  267. 'sjId' => $sjId,
  268. 'shopId' => $shopId,
  269. 'tx' => $tx,
  270. 'ptStyle' => $shop->ptStyle,
  271. ];
  272. ShopYeChangeClass::addChange($change, true);
  273. //平台余额增加
  274. PtAssetClass::reduceBalance($shop, $amount, $order, $capitalType, $tx);
  275. }
  276. //获取商家所有的门店 ssh 2021.2.26
  277. public static function getSjShopList($sjId)
  278. {
  279. return self::getAllByCondition(['sjId' => $sjId, 'delStatus' => 0], null, ['id']);
  280. }
  281. //取门店长信息 ssh 2021.1.17
  282. public static function getSuper($id)
  283. {
  284. $shop = self::getById($id);
  285. if (empty($shop)) {
  286. return [];
  287. }
  288. $adminId = $shop['adminId'] ?? 0;
  289. return \bizHd\admin\classes\AdminClass::getDetail($adminId);
  290. }
  291. }