ShopClass.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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. //门店充值增加余额 ssh 2021.2.21
  486. //$fix = true 余额只能在指定门店使用
  487. public static function rechargeAddBalance($shop, $amount, $order, $tx, $capitalType, $fix = false)
  488. {
  489. $mainId = $shop->mainId ?? 0;
  490. $main = MainClass::getLockById($mainId);
  491. if (empty($main)) {
  492. util::fail('没有找到资产信息');
  493. }
  494. //门店余额增加
  495. $currentBalance = bcadd($main->balance, $amount, 2);
  496. $main->balance = $currentBalance;
  497. $currentTotalRecharge = bcadd($shop->totalRecharge, $amount, 2);
  498. $main->totalRecharge = $currentTotalRecharge;
  499. //定向消费余额
  500. if ($fix) {
  501. $currentFix = bcadd($main->fixBalance, $amount, 2);
  502. $main->fixBalance = $currentFix;
  503. }
  504. $currentTx = $main->txBalance;
  505. //提现余额
  506. if ($tx == dict::getDict('yeTx', 'can')) {
  507. $currentTx = bcadd($main->txBalance, $amount, 2);
  508. $main->txBalance = $currentTx;
  509. }
  510. $main->save();
  511. $order->totalRecharge = $currentTotalRecharge;
  512. $order->save();
  513. //增加余额变动记录
  514. $id = $order->id;
  515. $event = "充值{$amount}元";
  516. if ($order->shopAdminId == 0) {
  517. $event = "系统充值{$amount}元";
  518. }
  519. $change = [
  520. 'relateId' => $id,
  521. 'capitalType' => $capitalType,
  522. 'amount' => $amount,
  523. 'balance' => $currentBalance,
  524. 'txBalance' => $currentTx,
  525. 'io' => 1,
  526. 'payWay' => $order->payWay,
  527. 'event' => $event,
  528. 'sjId' => $order->sjId,
  529. 'shopId' => $order->shopId,
  530. 'mainId' => $mainId,
  531. 'tx' => $tx,
  532. 'ptStyle' => $shop->ptStyle,
  533. ];
  534. ShopYeChangeClass::addChange($change, true);
  535. //平台余额增加
  536. PtAssetClass::rechargeAddBalance($shop, $amount, $order, $capitalType, $tx);
  537. }
  538. //采购余额付款,减少余额 ssh 20210519
  539. public static function purchaseReduceBalance($main, $shop, $amount, $order)
  540. {
  541. if (bccomp($amount, $main->balance, 2) == 1) {
  542. util::fail('余额不足');
  543. }
  544. $sjId = $order->sjId;
  545. $shopId = $order->shopId;
  546. $mainId = $order->mainId ?? 0;
  547. $currentBalance = bcsub($main->balance, $amount, 2);
  548. $main->balance = $currentBalance;
  549. $tx = dict::getDict('yeTx', 'canNot');
  550. //还有可提现余额也要先用掉
  551. $currentTxBalance = $main->txBalance;
  552. if ($currentTxBalance > 0) {
  553. $reduceTxBalance = $currentTxBalance > $amount ? $amount : $currentTxBalance;
  554. $main->txBalance = bcsub($currentTxBalance, $reduceTxBalance, 2);
  555. $tx = dict::getDict('yeTx', 'can');
  556. }
  557. $main->save();
  558. $capitalType = dict::getDict('capitalType', 'xhPurchase', 'id');
  559. //增加余额变动记录
  560. $id = $order->id;
  561. $change = [
  562. 'relateId' => $id,
  563. 'capitalType' => $capitalType,
  564. 'amount' => $amount,
  565. 'balance' => $currentBalance,
  566. 'txBalance' => $currentTxBalance,
  567. 'io' => 0,
  568. 'payWay' => $order->payWay,
  569. 'event' => "采购余额付款{$amount}元",
  570. 'sjId' => $sjId,
  571. 'shopId' => $shopId,
  572. 'mainId' => $mainId,
  573. 'tx' => $tx,
  574. 'ptStyle' => $shop->ptStyle,
  575. ];
  576. ShopYeChangeClass::addChange($change, true);
  577. //平台余额增加
  578. PtAssetClass::reduceBalance($shop, $amount, $order, $capitalType, $tx);
  579. }
  580. //零售采购退款
  581. public static function cgRefundAddBalance($main, $shop, $refundPrice, $cgRefund, $ghs)
  582. {
  583. //客户余额增加
  584. $currentBalance = bcadd($main->balance, $refundPrice, 2);
  585. $main->balance = $currentBalance;
  586. $main->save();
  587. $currentTxBalance = $main->txBalance;
  588. $currentType = dict::getDict('capitalType', 'cgRefund', 'id');
  589. $id = $cgRefund->id;
  590. $ptStyle = $shop->ptStyle;
  591. $sjId = $shop->sjId;
  592. $shopId = $shop->id;
  593. $mainId = $shop->mainId;
  594. $tx = dict::getDict('yeTx', 'canNot');
  595. $ghsName = $ghs->name ?? '';
  596. $change = [
  597. 'relateId' => $id,
  598. 'capitalType' => $currentType,
  599. 'amount' => $refundPrice,
  600. 'balance' => $currentBalance,
  601. 'txBalance' => $currentTxBalance,
  602. 'io' => 1,
  603. 'payWay' => 0,
  604. 'event' => "采购退款({$ghsName})",
  605. 'sjId' => $sjId,
  606. 'shopId' => $shopId,
  607. 'mainId' => $mainId,
  608. 'tx' => $tx,
  609. 'ptStyle' => $ptStyle,
  610. ];
  611. ShopYeChangeClass::addChange($change, true);
  612. PtAssetClass::cgRefundAddBalance($shop, $refundPrice, $cgRefund, $currentType, $tx);
  613. }
  614. //供货商预订被多付了退款减少余额 ssh 20220324
  615. public static function ghsBookOrderRefundReduceBalance($main, $shop, $refund, $refundPrice)
  616. {
  617. $currentBalance = bcsub($main->balance, $refundPrice, 2);
  618. if ($currentBalance < 0) {
  619. util::fail('余额不足,请先充值哦');
  620. }
  621. $main->balance = $currentBalance;
  622. $currentTx = $main->txBalance;
  623. if ($currentTx > 0) {
  624. if ($refundPrice > $currentTx) {
  625. $currentTx = 0;
  626. } else {
  627. $currentTx = bcsub($currentTx, $refundPrice, 2);
  628. }
  629. }
  630. $main->txBalance = $currentTx;
  631. $main->save();
  632. $currentType = dict::getDict('capitalType', 'ghsBookRefund');
  633. $id = $refund->id;
  634. $event = "预订单被多付退给客户(退款单 {$refund->orderSn} 订单{$refund->relateOrderSn})";
  635. $relateOrderSn = $refund->relateOrderSn ?? '';
  636. if (!empty($relateOrderSn)) {
  637. $relateOrder = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
  638. $customName = $relateOrder->customName ?? '';
  639. if (!empty($customName)) {
  640. $event = "预订单{$customName}多付退款(退款单 {$refund->orderSn} 订单{$refund->relateOrderSn})";
  641. }
  642. }
  643. $tx = dict::getDict('yeTx', 'can');
  644. $change = [
  645. 'relateId' => $id,
  646. 'capitalType' => $currentType,
  647. 'amount' => $refundPrice,
  648. 'balance' => $currentBalance,
  649. 'txBalance' => $currentTx,
  650. 'io' => 0,
  651. 'payWay' => 0,
  652. 'event' => $event,
  653. 'sjId' => $shop->sjId,
  654. 'shopId' => $shop->id,
  655. 'mainId' => $shop->mainId,
  656. 'tx' => $tx,
  657. 'ptStyle' => $shop->ptStyle,
  658. ];
  659. ShopYeChangeClass::addChange($change, true);
  660. //平台余额增加
  661. PtAssetClass::ghsBookOrderRefundReduceBalance($shop, $refund, $refundPrice);
  662. }
  663. //花店销售单退款减少余额
  664. public static function hdSaleRefundReduceBalance($main, $shop, $refund, $refundPrice)
  665. {
  666. //门店余额减少
  667. $currentBalance = bcsub($main->balance, $refundPrice, 2);
  668. if ($currentBalance < 0) {
  669. util::fail('余额不足,请先充值吧');
  670. }
  671. $main->balance = $currentBalance;
  672. $currentTx = $main->txBalance;
  673. if ($currentTx > 0) {
  674. if ($refundPrice > $currentTx) {
  675. $currentTx = 0;
  676. } else {
  677. $currentTx = bcsub($currentTx, $refundPrice, 2);
  678. }
  679. }
  680. $main->txBalance = $currentTx;
  681. $main->save();
  682. $currentType = dict::getDict('capitalType', 'hdOrderRefund');
  683. $id = $refund->id;
  684. $event = "退款给客户(退款单 {$refund->refundSn})";
  685. $relateOrderSn = $refund->orderSn ?? '';
  686. if (!empty($relateOrderSn)) {
  687. $relateOrder = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
  688. $customName = $relateOrder->customName ?? '';
  689. if (!empty($customName)) {
  690. $event = "退款给{$customName}(退款单 {$refund->refundSn})";
  691. }
  692. }
  693. $tx = dict::getDict('yeTx', 'can');
  694. $change = [
  695. 'relateId' => $id,
  696. 'capitalType' => $currentType,
  697. 'amount' => $refundPrice,
  698. 'balance' => $currentBalance,
  699. 'txBalance' => $currentTx,
  700. 'io' => 0,
  701. 'payWay' => 0,
  702. 'event' => $event,
  703. 'sjId' => $shop->sjId,
  704. 'shopId' => $shop->id,
  705. 'mainId' => $shop->mainId,
  706. 'tx' => $tx,
  707. 'ptStyle' => $shop->ptStyle,
  708. ];
  709. ShopYeChangeClass::addChange($change, true);
  710. //平台余额增加
  711. PtAssetClass::hdSaleRefundReduceBalance($shop, $refund, $refundPrice);
  712. }
  713. //销售单退款减少余额 ssh 2021.2.21
  714. public static function saleRefundReduceBalance($main, $shop, $refund, $refundPrice)
  715. {
  716. //门店余额减少
  717. $currentBalance = bcsub($main->balance, $refundPrice, 2);
  718. if ($currentBalance < 0) {
  719. util::fail('余额不足,请稍后再操作');
  720. }
  721. $main->balance = $currentBalance;
  722. $currentTx = $main->txBalance;
  723. if ($currentTx > 0) {
  724. if ($refundPrice > $currentTx) {
  725. $currentTx = 0;
  726. } else {
  727. $currentTx = bcsub($currentTx, $refundPrice, 2);
  728. }
  729. }
  730. $main->txBalance = $currentTx;
  731. $main->save();
  732. $currentType = dict::getDict('capitalType', 'saleRefund');
  733. $id = $refund->id;
  734. $event = "退款给客户(退款单 {$refund->orderSn})";
  735. $relateOrderSn = $refund->relateOrderSn ?? '';
  736. if (!empty($relateOrderSn)) {
  737. $relateOrder = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
  738. $customName = $relateOrder->customName ?? '';
  739. if (!empty($customName)) {
  740. $event = "退款给{$customName}(退款单 {$refund->orderSn})";
  741. }
  742. }
  743. $tx = dict::getDict('yeTx', 'can');
  744. $change = [
  745. 'relateId' => $id,
  746. 'capitalType' => $currentType,
  747. 'amount' => $refundPrice,
  748. 'balance' => $currentBalance,
  749. 'txBalance' => $currentTx,
  750. 'io' => 0,
  751. 'payWay' => 0,
  752. 'event' => $event,
  753. 'sjId' => $shop->sjId,
  754. 'shopId' => $shop->id,
  755. 'mainId' => $shop->mainId,
  756. 'tx' => $tx,
  757. 'ptStyle' => $shop->ptStyle,
  758. ];
  759. ShopYeChangeClass::addChange($change, true);
  760. //平台余额增加
  761. PtAssetClass::saleRefundReduceBalance($shop, $refund, $refundPrice);
  762. }
  763. //获取商家所有的门店 ssh 2021.2.26
  764. public static function getSjShopList($sjId)
  765. {
  766. return self::getAllByCondition(['sjId' => $sjId, 'delStatus' => 0], null, ['id']);
  767. }
  768. //取门店长信息 ssh 2021.1.17
  769. public static function getSuper($id)
  770. {
  771. $shop = self::getById($id);
  772. if (empty($shop)) {
  773. return [];
  774. }
  775. $adminId = $shop['adminId'] ?? 0;
  776. return \bizHd\admin\classes\AdminClass::getDetail($adminId);
  777. }
  778. //佣金引起的余额增加 lqh 2021211
  779. /**
  780. * @param $shop
  781. * @param $amount 提现金额
  782. * @param $cashId 提现主键ID
  783. * @param $capitalType
  784. */
  785. public static function brokerageAddBalance($shop, $cash, $capitalType)
  786. {
  787. $amount = $cash->amount ?? 0;
  788. $amount = floatval($amount);
  789. $cashId = $cash->id ?? 0;
  790. $customerSjName = $cash->sjName ?? '';
  791. $customerName = $cash->shopName ?? '';
  792. //获取的佣金 万分之三 暂时先写死,后续可走配置
  793. $brokerage = bcmul($amount, 0.0003, 2);
  794. //门店余额增加
  795. $currentBalance = bcadd($shop->balance, $brokerage, 2);
  796. $shop->balance = $currentBalance;
  797. $shop->totalRecharge = bcadd($shop->totalRecharge, $brokerage, 2);
  798. //可提现余额同步增加
  799. $currentTx = bcadd($shop->txBalance, $brokerage, 2);
  800. $shop->txBalance = $currentTx;
  801. $shop->save();
  802. //增加余额变动记录
  803. $tx = dict::getDict('yeTx', 'can');
  804. $change = [
  805. 'relateId' => $cashId,
  806. 'capitalType' => $capitalType,
  807. 'amount' => $brokerage,
  808. 'balance' => $currentBalance,
  809. 'txBalance' => $currentTx,
  810. 'io' => 1,
  811. 'payWay' => 0,
  812. 'event' => $customerSjName . '-' . $customerName . "提现" . $amount . "元获得佣金",
  813. 'sjId' => $shop->sjId,
  814. 'shopId' => $shop->id,
  815. 'mainId' => $shop->mainId,
  816. 'tx' => $tx,
  817. 'ptStyle' => $shop->ptStyle,
  818. ];
  819. ShopYeChangeClass::addChange($change, true);
  820. //平台余额增加
  821. $ptStyle = $shop->ptStyle;
  822. if ($ptStyle == dict::getDict('ptStyle', 'hd')) {
  823. $asset = PtAssetClass::getHdBalance();
  824. } elseif ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
  825. $asset = PtAssetClass::getGhsBalance();
  826. } else {
  827. util::fail('没有找到平台');
  828. }
  829. //增加平台可余额
  830. $currentPtBalance = bcadd($asset->amount, $brokerage, 2);
  831. $asset->amount = $currentPtBalance;
  832. $asset->save();
  833. if ($ptStyle == dict::getDict('ptStyle', 'hd')) {
  834. $txAsset = PtAssetClass::getHdTxBalance();
  835. } elseif ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
  836. $txAsset = PtAssetClass::getGhsTxBalance();
  837. } else {
  838. util::fail('没有找到平台');
  839. }
  840. //增加平台可提现余额
  841. $currentPtTxBalance = $txAsset->amount;
  842. $currentPtTxBalance = bcadd($currentPtTxBalance, $brokerage, 2);
  843. $txAsset->amount = $currentPtTxBalance;
  844. $txAsset->save();
  845. $change = [
  846. 'relateId' => $cashId,
  847. 'capitalType' => $capitalType,
  848. 'amount' => $brokerage,
  849. 'balance' => $currentPtBalance,
  850. 'txBalance' => $currentPtTxBalance,
  851. 'io' => 1,
  852. 'payWay' => 0,
  853. 'event' => $customerSjName . '-' . $customerName . "提现" . $amount . "元,代理商【{$shop->merchantName}-{$shop->shopName}】获得佣金",
  854. 'sjId' => $shop->sjId,
  855. 'shopId' => $shop->id,
  856. 'tx' => $tx,
  857. 'ptStyle' => $ptStyle,
  858. 'shopName' => $shop->shopName,
  859. 'sjName' => $shop->merchantName,
  860. ];
  861. PtYeChangeClass::addChange($change, true);
  862. }
  863. }