ShopClass.php 40 KB

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