ShopClass.php 33 KB

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