ShopClass.php 35 KB

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