ShopClass.php 31 KB

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