ShopClass.php 38 KB

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