ShopClass.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. <?php
  2. namespace biz\shop\classes;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\pt\classes\PtAssetClass;
  5. use biz\pt\classes\PtYeChangeClass;
  6. use bizGhs\custom\classes\CustomClass;
  7. use bizGhs\order\classes\OrderClass;
  8. use bizHd\custom\classes\HdClass;
  9. use common\components\dict;
  10. use common\components\httpUtil;
  11. use common\components\imgUtil;
  12. use common\components\qrCodeUtil;
  13. use common\components\stringUtil;
  14. use common\components\util;
  15. use Yii;
  16. use biz\base\classes\BaseClass;
  17. class ShopClass extends BaseClass
  18. {
  19. public static $baseFile = '\biz\shop\models\Shop';
  20. //商家是否有同城配送能力
  21. public static function hasIntraCity($shop)
  22. {
  23. //万丽花行 跑腿不算运费
  24. if ($shop->id == 12003) {
  25. return ['openIntraCity' => 0, 'wxStoreId' => ''];
  26. }
  27. $mainId = $shop->mainId ?? 0;
  28. $main = MainClass::getById($mainId, true);
  29. $wxStoreId = $main->wxStoreId ?? '';
  30. $lat = $shop->lat ?? '';
  31. $long = $shop->long ?? '';
  32. $address = $shop->address ?? '';
  33. $hasMap = dict::getDict('hasMap');
  34. if (getenv('YII_ENV') == 'production') {
  35. $openIntraCityMainList = [50, 58668, 24162];
  36. } else {
  37. $openIntraCityMainList = [644];
  38. }
  39. $openIntraCity = 0;
  40. //有地图功能
  41. if ($hasMap == 1) {
  42. //有地址功能,并且叫跑腿功能开启
  43. if (in_array($mainId, $openIntraCityMainList)) {
  44. if (!empty($lat) && !empty($long) && !empty($address)) {
  45. if (!empty($wxStoreId)) {
  46. $openIntraCity = 1;
  47. }
  48. }
  49. }
  50. }
  51. return ['openIntraCity' => $openIntraCity, 'wxStoreId' => $wxStoreId];
  52. }
  53. //营业状态
  54. public static function isOpen($shop)
  55. {
  56. if (isset($shop['open']) == false || $shop['open'] == 0) {
  57. return 0;
  58. }
  59. $openStartTime = $shop['openStartTime'] ?? '';
  60. if (empty($openStartTime)) {
  61. return 1;
  62. }
  63. $openEndTime = $shop['openEndTime'] ?? '';
  64. $start = strtotime(date("Y-m-d") . ' ' . $openStartTime);
  65. $end = strtotime(date("Y-m-d") . ' ' . $openEndTime);
  66. $time = time();
  67. if ($time > $start && $time < $end) {
  68. return 1;
  69. }
  70. return 0;
  71. }
  72. //获取图片信息 ssh 2021.3.14
  73. public static function getShopInfo($id)
  74. {
  75. $info = self::getById($id);
  76. if (empty($info)) {
  77. return $info;
  78. }
  79. $list = self::groupShopInfo([$info]);
  80. return current($list);
  81. }
  82. public static function getShopList($where)
  83. {
  84. $list = self::getList('*', $where, 'addTime DESC');
  85. return $list;
  86. }
  87. //查看店铺 ssh 2021.3.1
  88. public static function getShopByIds($ids)
  89. {
  90. $list = self::getByIds($ids);
  91. return self::groupShopInfo($list);
  92. }
  93. //门店 ssh 2021.3.1
  94. public static function groupShopInfo($list)
  95. {
  96. if (empty($list)) {
  97. return $list;
  98. }
  99. foreach ($list as $key => $val) {
  100. $shortAvatar = $val['avatar'] ?? '';
  101. $avatar = imgUtil::groupImg($shortAvatar);
  102. $list[$key]['avatar'] = $avatar;
  103. $list[$key]['shortAvatar'] = $shortAvatar;
  104. $smallAvatar = imgUtil::groupImg($shortAvatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  105. $list[$key]['smallAvatar'] = $smallAvatar;
  106. //店长微信
  107. $superWx = $val['superWx'] ?? '';
  108. $list[$key]['superWx'] = imgUtil::groupImg($superWx) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  109. }
  110. return $list;
  111. }
  112. //获取商家默认的门店ID ssh 2020.1.19
  113. public static function getDefaultShopId($merchant)
  114. {
  115. return isset($merchant['defaultShopId']) ? $merchant['defaultShopId'] : 0;
  116. }
  117. //更新门店 ssh 2020.2.29
  118. public static function updateShop($shop, $data)
  119. {
  120. $id = $shop['id'] ?? 0;
  121. $shopName = isset($data['shopName']) ? $data['shopName'] : '';
  122. if (stringUtil::getWordNum($shopName) > 8) {
  123. util::fail('门店名称不能超过8个汉字,2个字母顶一个汉字');
  124. }
  125. $sjId = $data['sjId'];
  126. $findShop = self::getByCondition(['sjId' => $sjId, 'shopName' => $shopName]);
  127. if (!empty($findShop) && $findShop['id'] != $id) {
  128. util::fail('门店名称已经存在');
  129. }
  130. $dist = $data['dist'] ?? '';
  131. $floor = $data['floor'] ?? '';
  132. $address = $data['address'] ?? '';
  133. $data['fullAddress'] = $data['city'] . $dist . $address . $floor;
  134. // 营业时间设置逻辑
  135. if ($data['openType'] == 0) { // openType: 0. 表示 24小时营业 1. 表示 xx ~ xx
  136. $data['openStartTime'] = ''; // openStartTime 营业开始时间,空表示24小时
  137. $data['openEndTime'] = '';
  138. }
  139. $cutStyle = $data['cutStyle'] ?? 0;
  140. $cutAmount = $data['cutAmount'] ?? 0;
  141. $meetAmount = $data['meetAmount'] ?? 0;
  142. if ($cutStyle == 1) {
  143. if ($cutAmount >= 1) {
  144. util::fail('折扣要小于1');
  145. }
  146. } else {
  147. if ($meetAmount > 0 && $cutAmount > 0) {
  148. if ($cutAmount >= $meetAmount) {
  149. util::fail('优惠金额过大');
  150. }
  151. }
  152. }
  153. //默认门店的开启和关闭
  154. $preDefault = $shop['default'] ?? 0;
  155. $default = $data['default'] ?? 0;
  156. if ($preDefault != $default) {
  157. if ($default == 0) {
  158. util::fail('总店地位无法修改,至少要有一个总店');
  159. }
  160. $sjId = $shop['sjId'] ?? 0;
  161. self::updateByCondition(['sjId' => $sjId], ['default' => 0]);
  162. }
  163. $data['packCost'] = isset($data['packCost']) && $data['packCost'] > 0 ? $data['packCost'] : 0;
  164. $data['cgUnitType'] = isset($data['cgUnitType']) ? $data['cgUnitType'] : 0;
  165. $respond = self::updateById($id, $data);
  166. $ptStyle = $shop['ptStyle'] ?? dict::getDict('ptStyle', 'hd');
  167. $default = $shop['default'] ?? 0;
  168. $sjName = $shop['merchantName'] ?? '';
  169. //默认门店只显示商家名称即可
  170. $name = $default == 1 && $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  171. $shopUpdate = [
  172. 'name' => $name,
  173. //'mobile' => $data['mobile'] ?? '', // 由于前面已经 unset 掉 mobile,所以会为"",而把原有值置空
  174. 'province' => $data['province'] ?? '',
  175. 'city' => $data['city'] ?? '',
  176. 'dist' => $data['dist'] ?? '',
  177. 'address' => $data['address'] ?? '',
  178. 'floor' => $data['floor'] ?? '',
  179. 'fullAddress' => $data['fullAddress'] ?? '',
  180. 'showAddress' => $data['showAddress'] ?? '',
  181. 'lat' => $data['lat'] ?? '',
  182. 'long' => $data['long'] ?? ''
  183. ];
  184. // 如果是批发端,则变更零售端的地址
  185. if ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
  186. //$lsShop = self::getById($id, false, 'lsShopId');
  187. $lsShopId = intval($shop['lsShopId']);
  188. if ($lsShopId > 0) {
  189. $re = self::updateById($lsShopId, $shopUpdate);
  190. }
  191. }
  192. // 如果是零售端,则变更批发端的地址
  193. if ($ptStyle == dict::getDict('ptStyle', 'hd')) {
  194. //$pfShop = self::getById($id, false, 'pfShopId');
  195. $pfShopId = intval($shop['pfShopId']);
  196. if ($pfShopId > 0) {
  197. $re = self::updateById($pfShopId, $shopUpdate);
  198. }
  199. }
  200. $where = ['sjId' => $sjId, 'shopId' => $id];
  201. if ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
  202. //如果是批发店,更新供货商信息
  203. GhsClass::updateByCondition($where, $shopUpdate);
  204. }
  205. if ($ptStyle == dict::getDict('ptStyle', 'hd')) {
  206. //如果是花店,更新客户端信息
  207. $city = $data['city'] ?? '';
  208. $dist = $data['dist'] ?? '';
  209. $address = $data['address'] ?? '';
  210. $floor = $data['floor'] ?? '';
  211. $fullAddress = $city . $dist . $address . $floor;
  212. $showAddress = $data['showAddress'] ?? '';
  213. $lat = $data['lat'] ?? '';
  214. $long = $data['long'] ?? '';
  215. $upData = [
  216. 'city' => $city,
  217. 'dist' => $dist,
  218. 'address' => $address,
  219. 'floor' => $floor,
  220. 'fullAddress' => $fullAddress,
  221. 'showAddress' => $showAddress,
  222. 'lat' => $lat,
  223. 'long' => $long
  224. ];
  225. CustomClass::updateByCondition(['shopId' => $id], $upData);
  226. //更新散客的xhHd信息
  227. $upData['name'] = $name;
  228. HdClass::updateByCondition(['shopId' => $id], $upData);
  229. }
  230. return $respond;
  231. }
  232. //取商家所有门店 ssh 2020.2.30
  233. public static function getAllShop($sjId)
  234. {
  235. return self::getAllByCondition(['sjId' => $sjId], null, "*");
  236. }
  237. //验证所属权限 ssh 2020.2.29
  238. public static function valid($shop, $sjId)
  239. {
  240. if (empty($shop)) {
  241. util::fail('门店无效');
  242. }
  243. if (isset($shop['sjId']) == false || $shop['sjId'] != $sjId) {
  244. util::fail('您无法访问');
  245. }
  246. }
  247. //删除门店 ssh 2020.3.1
  248. public static function deleteShop($shop)
  249. {
  250. if (isset($shop['default']) && $shop['default'] == 1) {
  251. util::fail('默认门店不允许删除');
  252. }
  253. $id = $shop['id'];
  254. self::updateById($id, ['delStatus' => 1]);
  255. }
  256. //生成收款码 ssh 2020.3.9
  257. public static function generateGatheringCode($sjId, $shopId)
  258. {
  259. $url = Yii::$app->params['mallDomain'] . "/#/pages/pay/index?account=" . $shopId;
  260. //强制转成https
  261. $url = httpUtil::becomeHttps($url);
  262. Yii::info('收款码url:' . $url);
  263. $gatheringCode = qrCodeUtil::generateGatheringQrCode($url, $sjId, $shopId, '', 16);
  264. Yii::info('gatheringCode:' . $gatheringCode);
  265. //$imgUrl = Yii::$app->params['hdImgHost'] . 'gathering/1.jpg?x-oss-process=image/watermark,image_' . base64_encode($gatheringCode) . ',g_nw,x_276,y_463/watermark,image_' . base64_encode('gathering/logo_big.png?x-oss-process=image/resize,P_12') . ',g_nw,x_555,y_728';
  266. $imgUrl = Yii::$app->params['hdImgHost'] . 'gathering/1.jpg?x-oss-process=image/watermark,image_' . base64_encode($gatheringCode) . ',g_nw,x_276,y_463';
  267. return $imgUrl;
  268. }
  269. //客户充值增加余额 ssh 20240508
  270. public static function customRechargeAddBalance($main, $shop, $order)
  271. {
  272. $mainId = $shop->mainId ?? 0;
  273. $balance = bcadd($main->balance, $order->amount, 2);
  274. $txBalance = bcadd($main->txBalance, $order->amount, 2);
  275. $main->balance = $balance;
  276. $main->txBalance = $txBalance;
  277. $main->save();
  278. $rechargeId = $order->id ?? 0;
  279. $tx = dict::getDict('yeTx', 'can');
  280. $capitalType = dict::getDict('capitalType', 'customRechargeToGhs', 'id');
  281. $custom = '客户';
  282. if (isset($order->customName) && !empty($order->customName)) {
  283. $custom = $order->customName;
  284. }
  285. $change = [
  286. 'relateId' => $rechargeId,
  287. 'capitalType' => $capitalType,
  288. 'amount' => $order->amount,
  289. 'balance' => $balance,
  290. 'txBalance' => $txBalance,
  291. 'io' => 1,
  292. 'payWay' => $order->payWay,
  293. 'event' => $custom . "充值,单号:{$order->orderSn}",
  294. 'sjId' => $shop->sjId,
  295. 'shopId' => $shop->id,
  296. 'mainId' => $shop->mainId,
  297. 'tx' => $tx,
  298. 'ptStyle' => $shop->ptStyle,
  299. 'mainId' => $mainId,
  300. ];
  301. ShopYeChangeClass::addChange($change, true);
  302. //平台余额增加
  303. PtAssetClass::customRechargeAddBalance($shop, $order, $capitalType, $tx);
  304. }
  305. public static function skRechargeAddBalance($main, $shop, $order)
  306. {
  307. $mainId = $main->id;
  308. $balance = bcadd($main->balance, $order->amount, 2);
  309. $txBalance = bcadd($main->txBalance, $order->amount, 2);
  310. $main->balance = $balance;
  311. $main->txBalance = $txBalance;
  312. $main->save();
  313. $rechargeId = $order->id ?? 0;
  314. $tx = dict::getDict('yeTx', 'can');
  315. $capitalType = dict::getDict('capitalType', 'xhRecharge', 'id');
  316. $custom = !empty($order->customName) ? $order->customName : '客户';
  317. $change = [
  318. 'relateId' => $rechargeId,
  319. 'capitalType' => $capitalType,
  320. 'amount' => $order->amount,
  321. 'balance' => $balance,
  322. 'txBalance' => $txBalance,
  323. 'io' => 1,
  324. 'payWay' => $order->payWay,
  325. 'event' => $custom . "充值,单号:{$order->orderSn}",
  326. 'sjId' => $shop->sjId,
  327. 'shopId' => $shop->id,
  328. 'mainId' => $mainId,
  329. 'tx' => $tx,
  330. 'ptStyle' => $shop->ptStyle,
  331. ];
  332. ShopYeChangeClass::addChange($change, true);
  333. }
  334. //客户结帐增加余额 ssh 20210729
  335. public static function customClearAddBalance($main, $shop, $order)
  336. {
  337. $mainId = $shop->mainId ?? 0;
  338. $balance = bcadd($main->balance, $order->actPrice, 2);
  339. $txBalance = bcadd($main->txBalance, $order->actPrice, 2);
  340. $main->balance = $balance;
  341. $main->txBalance = $txBalance;
  342. $main->save();
  343. $clearId = $order->id ?? 0;
  344. $tx = dict::getDict('yeTx', 'can');
  345. $capitalType = dict::getDict('capitalType', 'hdPurchaseClear', 'id');
  346. $custom = '客户';
  347. if (isset($order->customName) && !empty($order->customName)) {
  348. $custom = $order->customName;
  349. }
  350. $change = [
  351. 'relateId' => $clearId,
  352. 'capitalType' => $capitalType,
  353. 'amount' => $order->actPrice,
  354. 'balance' => $balance,
  355. 'txBalance' => $txBalance,
  356. 'io' => 1,
  357. 'payWay' => $order->payWay,
  358. 'event' => $custom . "结帐(单号:{$order->orderSn})",
  359. 'sjId' => $shop->sjId,
  360. 'shopId' => $shop->id,
  361. 'mainId' => $shop->mainId,
  362. 'tx' => $tx,
  363. 'ptStyle' => $shop->ptStyle,
  364. 'mainId' => $mainId,
  365. ];
  366. ShopYeChangeClass::addChange($change, true);
  367. //平台余额增加
  368. PtAssetClass::customClearAddBalance($shop, $order, $capitalType, $tx);
  369. }
  370. //客户使用优惠券增加余额 ssh 20210601
  371. public static function customKdUseCouponAddBalance($shop, $order, $capitalType)
  372. {
  373. //门店余额增加
  374. $amount = $order->discountAmount;
  375. $currentBalance = bcadd($shop->balance, $amount, 2);
  376. $shop->balance = $currentBalance;
  377. $shop->totalRecharge = bcadd($shop->totalRecharge, $amount, 2);
  378. //可提现余额同步增加
  379. $currentTx = bcadd($shop->txBalance, $amount, 2);
  380. $shop->txBalance = $currentTx;
  381. $shop->save();
  382. //增加余额变动记录
  383. $id = $order->id;
  384. $event = "客户下单(使用平台优惠券{$amount}元)";
  385. $tx = dict::getDict('yeTx', 'can');
  386. $mainId = $shop->mainId ?? 0;
  387. $change = [
  388. 'relateId' => $id,
  389. 'capitalType' => $capitalType,
  390. 'amount' => $amount,
  391. 'balance' => $currentBalance,
  392. 'txBalance' => $currentTx,
  393. 'io' => 1,
  394. 'payWay' => $order->payWay,
  395. 'event' => $event,
  396. 'mainId' => $mainId,
  397. 'tx' => $tx,
  398. 'ptStyle' => $shop->ptStyle,
  399. ];
  400. ShopYeChangeClass::addChange($change, true);
  401. //平台余额增加
  402. PtAssetClass::customKdUseCouponAddBalance($shop, $order, $capitalType, $tx);
  403. }
  404. //客户下单引起的余额增加 ssh 20210520
  405. public static function customKdAddBalance($main, $shop, $amount, $order, $capitalType)
  406. {
  407. //门店余额增加
  408. $mainId = $shop->mainId ?? 0;
  409. $currentBalance = bcadd($main->balance, $amount, 2);
  410. $main->balance = $currentBalance;
  411. //可提现余额同步增加
  412. $currentTx = bcadd($main->txBalance, $amount, 2);
  413. $main->txBalance = $currentTx;
  414. $main->save();
  415. $ptStyle = $shop->ptStyle ?? dict::getDict('ptStyle', 'hd');
  416. $customName = '客户';
  417. if ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
  418. $customId = $order->customId ?? 0;
  419. $custom = CustomClass::getById($customId, true);
  420. if (isset($custom->name) && !empty($custom->name)) {
  421. $customName = $custom->name;
  422. }
  423. }
  424. //增加余额变动记录
  425. $id = $order->id;
  426. $event = $customName . "下单" . floatval($amount) . "元";
  427. $tx = dict::getDict('yeTx', 'can');
  428. $fromType = $order->fromType ?? 1;
  429. $change = [
  430. 'relateId' => $id,
  431. 'capitalType' => $capitalType,
  432. 'amount' => $amount,
  433. 'balance' => $currentBalance,
  434. 'txBalance' => $currentTx,
  435. 'io' => 1,
  436. 'payWay' => $order->payWay,
  437. 'event' => $event,
  438. 'mainId' => $mainId,
  439. 'tx' => $tx,
  440. 'ptStyle' => $shop->ptStyle,
  441. 'fromType' => $fromType,
  442. ];
  443. ShopYeChangeClass::addChange($change, true);
  444. //平台余额增加
  445. PtAssetClass::customKdAddBalance($shop, $amount, $order, $capitalType, $tx);
  446. }
  447. public static function customScanPayAddBalance($main, $shop, $amount, $order, $capitalType)
  448. {
  449. //门店余额增加
  450. $mainId = $shop->mainId ?? 0;
  451. $currentBalance = bcadd($main->balance, $amount, 2);
  452. $main->balance = $currentBalance;
  453. //可提现余额同步增加
  454. $currentTx = bcadd($main->txBalance, $amount, 2);
  455. $main->txBalance = $currentTx;
  456. $main->save();
  457. $customName = '客户';
  458. $customId = $order->customId ?? 0;
  459. $custom = \bizHd\custom\classes\CustomClass::getById($customId, true);
  460. if (!empty($custom->name)) {
  461. $customName = $custom->name;
  462. }
  463. //增加余额变动记录
  464. $id = $order->id;
  465. $event = $customName . "扫码付款" . floatval($amount) . "元";
  466. $change = [
  467. 'relateId' => $id,
  468. 'capitalType' => $capitalType,
  469. 'amount' => $amount,
  470. 'balance' => $currentBalance,
  471. 'txBalance' => $currentTx,
  472. 'io' => 1,
  473. 'payWay' => $order->payWay,
  474. 'event' => $event,
  475. 'mainId' => $mainId,
  476. 'tx' => 2,
  477. 'ptStyle' => $shop->ptStyle,
  478. 'fromType' => 1,
  479. ];
  480. ShopYeChangeClass::addChange($change, true);
  481. }
  482. //门店充值增加余额 ssh 2021.2.21
  483. //$fix = true 余额只能在指定门店使用
  484. public static function rechargeAddBalance($shop, $amount, $order, $tx, $capitalType, $fix = false)
  485. {
  486. $mainId = $shop->mainId ?? 0;
  487. $main = MainClass::getLockById($mainId);
  488. if (empty($main)) {
  489. util::fail('没有找到资产信息');
  490. }
  491. //门店余额增加
  492. $currentBalance = bcadd($main->balance, $amount, 2);
  493. $main->balance = $currentBalance;
  494. $currentTotalRecharge = bcadd($shop->totalRecharge, $amount, 2);
  495. $main->totalRecharge = $currentTotalRecharge;
  496. //定向消费余额
  497. if ($fix) {
  498. $currentFix = bcadd($main->fixBalance, $amount, 2);
  499. $main->fixBalance = $currentFix;
  500. }
  501. $currentTx = $main->txBalance;
  502. //提现余额
  503. if ($tx == dict::getDict('yeTx', 'can')) {
  504. $currentTx = bcadd($main->txBalance, $amount, 2);
  505. $main->txBalance = $currentTx;
  506. }
  507. $main->save();
  508. $order->totalRecharge = $currentTotalRecharge;
  509. $order->save();
  510. //增加余额变动记录
  511. $id = $order->id;
  512. $event = "充值{$amount}元";
  513. if ($order->shopAdminId == 0) {
  514. $event = "系统充值{$amount}元";
  515. }
  516. $change = [
  517. 'relateId' => $id,
  518. 'capitalType' => $capitalType,
  519. 'amount' => $amount,
  520. 'balance' => $currentBalance,
  521. 'txBalance' => $currentTx,
  522. 'io' => 1,
  523. 'payWay' => $order->payWay,
  524. 'event' => $event,
  525. 'sjId' => $order->sjId,
  526. 'shopId' => $order->shopId,
  527. 'mainId' => $mainId,
  528. 'tx' => $tx,
  529. 'ptStyle' => $shop->ptStyle,
  530. ];
  531. ShopYeChangeClass::addChange($change, true);
  532. //平台余额增加
  533. PtAssetClass::rechargeAddBalance($shop, $amount, $order, $capitalType, $tx);
  534. }
  535. //采购余额付款,减少余额 ssh 20210519
  536. public static function purchaseReduceBalance($main, $shop, $amount, $order)
  537. {
  538. if (bccomp($amount, $main->balance, 2) == 1) {
  539. util::fail('余额不足');
  540. }
  541. $sjId = $order->sjId;
  542. $shopId = $order->shopId;
  543. $mainId = $order->mainId ?? 0;
  544. $currentBalance = bcsub($main->balance, $amount, 2);
  545. $main->balance = $currentBalance;
  546. $tx = dict::getDict('yeTx', 'canNot');
  547. //还有可提现余额也要先用掉
  548. $currentTxBalance = $main->txBalance;
  549. if ($currentTxBalance > 0) {
  550. $reduceTxBalance = $currentTxBalance > $amount ? $amount : $currentTxBalance;
  551. $main->txBalance = bcsub($currentTxBalance, $reduceTxBalance, 2);
  552. $tx = dict::getDict('yeTx', 'can');
  553. }
  554. $main->save();
  555. $capitalType = dict::getDict('capitalType', 'xhPurchase', 'id');
  556. //增加余额变动记录
  557. $id = $order->id;
  558. $change = [
  559. 'relateId' => $id,
  560. 'capitalType' => $capitalType,
  561. 'amount' => $amount,
  562. 'balance' => $currentBalance,
  563. 'txBalance' => $currentTxBalance,
  564. 'io' => 0,
  565. 'payWay' => $order->payWay,
  566. 'event' => "采购余额付款{$amount}元",
  567. 'sjId' => $sjId,
  568. 'shopId' => $shopId,
  569. 'mainId' => $mainId,
  570. 'tx' => $tx,
  571. 'ptStyle' => $shop->ptStyle,
  572. ];
  573. ShopYeChangeClass::addChange($change, true);
  574. //平台余额增加
  575. PtAssetClass::reduceBalance($shop, $amount, $order, $capitalType, $tx);
  576. }
  577. //零售采购退款
  578. public static function cgRefundAddBalance($main, $shop, $refundPrice, $cgRefund, $ghs)
  579. {
  580. //客户余额增加
  581. $currentBalance = bcadd($main->balance, $refundPrice, 2);
  582. $main->balance = $currentBalance;
  583. $main->save();
  584. $currentTxBalance = $main->txBalance;
  585. $currentType = dict::getDict('capitalType', 'cgRefund', 'id');
  586. $id = $cgRefund->id;
  587. $ptStyle = $shop->ptStyle;
  588. $sjId = $shop->sjId;
  589. $shopId = $shop->id;
  590. $mainId = $shop->mainId;
  591. $tx = dict::getDict('yeTx', 'canNot');
  592. $ghsName = $ghs->name ?? '';
  593. $change = [
  594. 'relateId' => $id,
  595. 'capitalType' => $currentType,
  596. 'amount' => $refundPrice,
  597. 'balance' => $currentBalance,
  598. 'txBalance' => $currentTxBalance,
  599. 'io' => 1,
  600. 'payWay' => 0,
  601. 'event' => "采购退款({$ghsName})",
  602. 'sjId' => $sjId,
  603. 'shopId' => $shopId,
  604. 'mainId' => $mainId,
  605. 'tx' => $tx,
  606. 'ptStyle' => $ptStyle,
  607. ];
  608. ShopYeChangeClass::addChange($change, true);
  609. PtAssetClass::cgRefundAddBalance($shop, $refundPrice, $cgRefund, $currentType, $tx);
  610. }
  611. //供货商预订被多付了退款减少余额 ssh 20220324
  612. public static function ghsBookOrderRefundReduceBalance($main, $shop, $refund, $refundPrice)
  613. {
  614. $currentBalance = bcsub($main->balance, $refundPrice, 2);
  615. if ($currentBalance < 0) {
  616. util::fail('余额不足,请先充值哦');
  617. }
  618. $main->balance = $currentBalance;
  619. $currentTx = $main->txBalance;
  620. if ($currentTx > 0) {
  621. if ($refundPrice > $currentTx) {
  622. $currentTx = 0;
  623. } else {
  624. $currentTx = bcsub($currentTx, $refundPrice, 2);
  625. }
  626. }
  627. $main->txBalance = $currentTx;
  628. $main->save();
  629. $currentType = dict::getDict('capitalType', 'ghsBookRefund');
  630. $id = $refund->id;
  631. $event = "预订单被多付退给客户(退款单 {$refund->orderSn} 订单{$refund->relateOrderSn})";
  632. $relateOrderSn = $refund->relateOrderSn ?? '';
  633. if (!empty($relateOrderSn)) {
  634. $relateOrder = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
  635. $customName = $relateOrder->customName ?? '';
  636. if (!empty($customName)) {
  637. $event = "预订单{$customName}多付退款(退款单 {$refund->orderSn} 订单{$refund->relateOrderSn})";
  638. }
  639. }
  640. $tx = dict::getDict('yeTx', 'can');
  641. $change = [
  642. 'relateId' => $id,
  643. 'capitalType' => $currentType,
  644. 'amount' => $refundPrice,
  645. 'balance' => $currentBalance,
  646. 'txBalance' => $currentTx,
  647. 'io' => 0,
  648. 'payWay' => 0,
  649. 'event' => $event,
  650. 'sjId' => $shop->sjId,
  651. 'shopId' => $shop->id,
  652. 'mainId' => $shop->mainId,
  653. 'tx' => $tx,
  654. 'ptStyle' => $shop->ptStyle,
  655. ];
  656. ShopYeChangeClass::addChange($change, true);
  657. //平台余额增加
  658. PtAssetClass::ghsBookOrderRefundReduceBalance($shop, $refund, $refundPrice);
  659. }
  660. //花店销售单退款减少余额
  661. public static function hdSaleRefundReduceBalance($main, $shop, $refund, $refundPrice)
  662. {
  663. //门店余额减少
  664. $currentBalance = bcsub($main->balance, $refundPrice, 2);
  665. if ($currentBalance < 0) {
  666. util::fail('余额不足,请先充值吧');
  667. }
  668. $main->balance = $currentBalance;
  669. $currentTx = $main->txBalance;
  670. if ($currentTx > 0) {
  671. if ($refundPrice > $currentTx) {
  672. $currentTx = 0;
  673. } else {
  674. $currentTx = bcsub($currentTx, $refundPrice, 2);
  675. }
  676. }
  677. $main->txBalance = $currentTx;
  678. $main->save();
  679. $currentType = dict::getDict('capitalType', 'hdOrderRefund');
  680. $id = $refund->id;
  681. $event = "退款给客户(退款单 {$refund->refundSn})";
  682. $relateOrderSn = $refund->orderSn ?? '';
  683. if (!empty($relateOrderSn)) {
  684. $relateOrder = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
  685. $customName = $relateOrder->customName ?? '';
  686. if (!empty($customName)) {
  687. $event = "退款给{$customName}(退款单 {$refund->refundSn})";
  688. }
  689. }
  690. $tx = dict::getDict('yeTx', 'can');
  691. $change = [
  692. 'relateId' => $id,
  693. 'capitalType' => $currentType,
  694. 'amount' => $refundPrice,
  695. 'balance' => $currentBalance,
  696. 'txBalance' => $currentTx,
  697. 'io' => 0,
  698. 'payWay' => 0,
  699. 'event' => $event,
  700. 'sjId' => $shop->sjId,
  701. 'shopId' => $shop->id,
  702. 'mainId' => $shop->mainId,
  703. 'tx' => $tx,
  704. 'ptStyle' => $shop->ptStyle,
  705. ];
  706. ShopYeChangeClass::addChange($change, true);
  707. //平台余额增加
  708. PtAssetClass::hdSaleRefundReduceBalance($shop, $refund, $refundPrice);
  709. }
  710. //销售单退款减少余额 ssh 2021.2.21
  711. public static function saleRefundReduceBalance($main, $shop, $refund, $refundPrice)
  712. {
  713. //门店余额减少
  714. $currentBalance = bcsub($main->balance, $refundPrice, 2);
  715. if ($currentBalance < 0) {
  716. util::fail('余额不足,请稍后再操作');
  717. }
  718. $main->balance = $currentBalance;
  719. $currentTx = $main->txBalance;
  720. if ($currentTx > 0) {
  721. if ($refundPrice > $currentTx) {
  722. $currentTx = 0;
  723. } else {
  724. $currentTx = bcsub($currentTx, $refundPrice, 2);
  725. }
  726. }
  727. $main->txBalance = $currentTx;
  728. $main->save();
  729. $currentType = dict::getDict('capitalType', 'saleRefund');
  730. $id = $refund->id;
  731. $event = "退款给客户(退款单 {$refund->orderSn})";
  732. $relateOrderSn = $refund->relateOrderSn ?? '';
  733. if (!empty($relateOrderSn)) {
  734. $relateOrder = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
  735. $customName = $relateOrder->customName ?? '';
  736. if (!empty($customName)) {
  737. $event = "退款给{$customName}(退款单 {$refund->orderSn})";
  738. }
  739. }
  740. $tx = dict::getDict('yeTx', 'can');
  741. $change = [
  742. 'relateId' => $id,
  743. 'capitalType' => $currentType,
  744. 'amount' => $refundPrice,
  745. 'balance' => $currentBalance,
  746. 'txBalance' => $currentTx,
  747. 'io' => 0,
  748. 'payWay' => 0,
  749. 'event' => $event,
  750. 'sjId' => $shop->sjId,
  751. 'shopId' => $shop->id,
  752. 'mainId' => $shop->mainId,
  753. 'tx' => $tx,
  754. 'ptStyle' => $shop->ptStyle,
  755. ];
  756. ShopYeChangeClass::addChange($change, true);
  757. //平台余额增加
  758. PtAssetClass::saleRefundReduceBalance($shop, $refund, $refundPrice);
  759. }
  760. //获取商家所有的门店 ssh 2021.2.26
  761. public static function getSjShopList($sjId)
  762. {
  763. return self::getAllByCondition(['sjId' => $sjId, 'delStatus' => 0], null, ['id']);
  764. }
  765. //取门店长信息 ssh 2021.1.17
  766. public static function getSuper($id)
  767. {
  768. $shop = self::getById($id);
  769. if (empty($shop)) {
  770. return [];
  771. }
  772. $adminId = $shop['adminId'] ?? 0;
  773. return \bizHd\admin\classes\AdminClass::getDetail($adminId);
  774. }
  775. //佣金引起的余额增加 lqh 2021211
  776. /**
  777. * @param $shop
  778. * @param $amount 提现金额
  779. * @param $cashId 提现主键ID
  780. * @param $capitalType
  781. */
  782. public static function brokerageAddBalance($shop, $cash, $capitalType)
  783. {
  784. $amount = $cash->amount ?? 0;
  785. $amount = floatval($amount);
  786. $cashId = $cash->id ?? 0;
  787. $customerSjName = $cash->sjName ?? '';
  788. $customerName = $cash->shopName ?? '';
  789. //获取的佣金 万分之三 暂时先写死,后续可走配置
  790. $brokerage = bcmul($amount, 0.0003, 2);
  791. //门店余额增加
  792. $currentBalance = bcadd($shop->balance, $brokerage, 2);
  793. $shop->balance = $currentBalance;
  794. $shop->totalRecharge = bcadd($shop->totalRecharge, $brokerage, 2);
  795. //可提现余额同步增加
  796. $currentTx = bcadd($shop->txBalance, $brokerage, 2);
  797. $shop->txBalance = $currentTx;
  798. $shop->save();
  799. //增加余额变动记录
  800. $tx = dict::getDict('yeTx', 'can');
  801. $change = [
  802. 'relateId' => $cashId,
  803. 'capitalType' => $capitalType,
  804. 'amount' => $brokerage,
  805. 'balance' => $currentBalance,
  806. 'txBalance' => $currentTx,
  807. 'io' => 1,
  808. 'payWay' => 0,
  809. 'event' => $customerSjName . '-' . $customerName . "提现" . $amount . "元获得佣金",
  810. 'sjId' => $shop->sjId,
  811. 'shopId' => $shop->id,
  812. 'mainId' => $shop->mainId,
  813. 'tx' => $tx,
  814. 'ptStyle' => $shop->ptStyle,
  815. ];
  816. ShopYeChangeClass::addChange($change, true);
  817. //平台余额增加
  818. $ptStyle = $shop->ptStyle;
  819. if ($ptStyle == dict::getDict('ptStyle', 'hd')) {
  820. $asset = PtAssetClass::getHdBalance();
  821. } elseif ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
  822. $asset = PtAssetClass::getGhsBalance();
  823. } else {
  824. util::fail('没有找到平台');
  825. }
  826. //增加平台可余额
  827. $currentPtBalance = bcadd($asset->amount, $brokerage, 2);
  828. $asset->amount = $currentPtBalance;
  829. $asset->save();
  830. if ($ptStyle == dict::getDict('ptStyle', 'hd')) {
  831. $txAsset = PtAssetClass::getHdTxBalance();
  832. } elseif ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
  833. $txAsset = PtAssetClass::getGhsTxBalance();
  834. } else {
  835. util::fail('没有找到平台');
  836. }
  837. //增加平台可提现余额
  838. $currentPtTxBalance = $txAsset->amount;
  839. $currentPtTxBalance = bcadd($currentPtTxBalance, $brokerage, 2);
  840. $txAsset->amount = $currentPtTxBalance;
  841. $txAsset->save();
  842. $change = [
  843. 'relateId' => $cashId,
  844. 'capitalType' => $capitalType,
  845. 'amount' => $brokerage,
  846. 'balance' => $currentPtBalance,
  847. 'txBalance' => $currentPtTxBalance,
  848. 'io' => 1,
  849. 'payWay' => 0,
  850. 'event' => $customerSjName . '-' . $customerName . "提现" . $amount . "元,代理商【{$shop->merchantName}-{$shop->shopName}】获得佣金",
  851. 'sjId' => $shop->sjId,
  852. 'shopId' => $shop->id,
  853. 'tx' => $tx,
  854. 'ptStyle' => $ptStyle,
  855. 'shopName' => $shop->shopName,
  856. 'sjName' => $shop->merchantName,
  857. ];
  858. PtYeChangeClass::addChange($change, true);
  859. }
  860. }