ShopClass.php 38 KB

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