RechargeClass.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <?php
  2. namespace bizHd\recharge\classes;
  3. use biz\shop\classes\ShopClass;
  4. use bizGhs\shop\classes\ShopMoneyClass;
  5. use bizHd\balance\classes\BalanceChangeClass;
  6. use bizHd\balance\classes\BalanceGiveChangeClass;
  7. use bizHd\balance\classes\BalancePayChangeClass;
  8. use bizHd\custom\classes\CustomClass;
  9. use bizHd\custom\classes\HdClass;
  10. use bizHd\member\classes\MemberWealClass;
  11. use bizHd\order\classes\OrderClass;
  12. use bizHd\order\classes\SettleClass;
  13. use bizHd\shop\classes\MainClass;
  14. use bizHd\wx\classes\WxOpenClass;
  15. use common\components\arrayUtil;
  16. use common\components\dict;
  17. use common\components\lakala\Lakala;
  18. use common\components\noticeUtil;
  19. use common\components\util;
  20. use Yii;
  21. use bizHd\base\classes\BaseClass;
  22. class RechargeClass extends BaseClass
  23. {
  24. public static $baseFile = '\bizHd\recharge\models\Recharge';
  25. public static function addRecharge($data)
  26. {
  27. //创建充值单时,要把过去的充值单和结账单取消掉
  28. $customId = $data['customId'] ?? 0;
  29. if (empty($customId)) {
  30. util::fail('没有客户信息');
  31. }
  32. $shopId = $data['shopId'] ?? 0;
  33. $hasList = self::getAllByCondition(['customId' => $customId, 'status' => 0], null, '*', null, true);
  34. if (!empty($hasList)) {
  35. $shop = \bizHd\merchant\classes\ShopClass::getById($shopId, true);
  36. if (empty($shop)) {
  37. util::fail('没有找到门店');
  38. }
  39. foreach ($hasList as $k => $recharge) {
  40. $payRequest = $recharge->payRequest ?? 0;
  41. if ($payRequest == 1 || $payRequest == 2) {
  42. $closeOrderSn = $recharge->orderSn;
  43. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  44. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  45. $params = [
  46. 'appid' => 'OP00002119',
  47. 'serial_no' => '018b08cfddbd',
  48. 'merchant_no' => $shop->lklSjNo,
  49. 'term_no' => $shop->lklScanTermNo,
  50. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  51. 'lklCertificatePath' => $lklCertificatePath,
  52. ];
  53. $laResource = new Lakala($params);
  54. $closeParams = ['orderSn' => $closeOrderSn];
  55. if ($payRequest == 1) {
  56. //聚合支付
  57. $response = $laResource->close($closeParams);
  58. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  59. $msg = $response['msg'] ?? '';
  60. noticeUtil::push('充值换单,聚合支付,没有成功,单号:' . $closeOrderSn . ',关单没有成功,编号966558,' . $msg, '15280215347');
  61. //util::fail('老订单取消失败,请联系管理员');
  62. }
  63. } else {
  64. //收银台支付
  65. $response = $laResource->cashClose($closeParams);
  66. if (!isset($response['code']) || $response['code'] != '000000') {
  67. $msg = $response['msg'] ?? '';
  68. noticeUtil::push('充值换单,收银台的单号:' . $closeOrderSn . ',关单没有成功,编号66996,' . $msg, '15280215347');
  69. //util::fail('旧订单取消失败,请联系管理员');
  70. }
  71. }
  72. }
  73. $recharge->status = 2;
  74. $recharge->save();
  75. $settleId = $recharge->settleId ?? 0;
  76. $settle = SettleClass::getById($settleId, true);
  77. if (!empty($settle)) {
  78. $settle->status = 3;
  79. $settle->save();
  80. }
  81. }
  82. }
  83. return self::add($data, true);
  84. }
  85. //第三支付后流程 ssh 2021.4.27
  86. public static function thirdPay($payWay, $orderSn, $totalFee, $attach)
  87. {
  88. $recharge = RechargeClass::getByCondition(['orderSn' => $orderSn], true);
  89. if (empty($recharge)) {
  90. noticeUtil::push('充值付款成功,收到回调,但没有找到充值记录,orderSn:' . $orderSn, '15280215347');
  91. return false;
  92. }
  93. if ($totalFee != $recharge->amount) {
  94. noticeUtil::push("充值付款成功,收到回调,但金额不一致 {$totalFee} {$recharge->amount},orderSn:" . $orderSn, '15280215347');
  95. return false;
  96. }
  97. if ($recharge->payStatus == 1) {
  98. noticeUtil::push("充值付款成功,收到回调,但订单是已经支付过了,orderSn:" . $orderSn, '15280215347');
  99. return false;
  100. }
  101. $id = $recharge->id;
  102. $recharge = RechargeClass::getLockById($id);
  103. $params = [
  104. 'onlinePay' => 2,
  105. 'side' => 1,
  106. ];
  107. self::complete($recharge, $payWay, $params);
  108. return $recharge;
  109. }
  110. //处理充值流程 ssh 20250321
  111. public static function complete($recharge, $payWay, $params)
  112. {
  113. $onlinePay = $params['onlinePay'];
  114. //0 花店端操作,1 客户端操作
  115. $side = $params['side'] ?? 0;
  116. $recharge->onlinePay = $onlinePay;
  117. $recharge->payWay = $payWay;
  118. $recharge->payStatus = 1;
  119. $recharge->status = 1;
  120. $recharge->save();
  121. $shopId = $recharge->shopId;
  122. $mainId = $recharge->mainId;
  123. $hdId = $recharge->hdId;
  124. $hdInfo = HdClass::getById($hdId, true);
  125. $hdName = $hdInfo->name ?? "";
  126. $amount = $recharge->amount;
  127. $staffId = $recharge->staffId;
  128. $payWay = $recharge->payWay;
  129. $staffName = $recharge->staffName;
  130. $customId = $recharge->customId;
  131. $customName = $recharge->customName;
  132. $capitalType = dict::getDict('capitalType', 'xhRecharge', 'id');
  133. $relateId = $recharge->id;
  134. $shOrderId = $recharge->shOrderId ?? 0;
  135. $shOrderSn = $recharge->shOrderSn ?? '';
  136. $custom = CustomClass::getLockById($customId);
  137. if (empty($custom)) {
  138. $msg = '充值回调失败,没有找到客户';
  139. util::fail($msg);
  140. }
  141. $oldLevel = $custom->member ?? 0;
  142. $hd = HdClass::getLockById($hdId);
  143. if (empty($hd)) {
  144. $msg = '充值回调失败,没有找到花店';
  145. util::fail($msg);
  146. }
  147. $shop = ShopClass::getById($shopId, true);
  148. $rechargeWeal = $shop->rechargeWeal ?? 0;
  149. $balanceGive = 0;
  150. //如果客户有欠款、结账充值、售后充值、售后付款充值,不参与优惠
  151. if ($custom->balance >= 0 && $recharge->settleId == 0 && $recharge->shOrderId == 0 && $recharge->refundOrderId == 0) {
  152. if ($rechargeWeal == 1) {
  153. $weal = MemberWealClass::getAllByCondition(['shopId' => $shopId], 'recharge ASC', '*');
  154. if (!empty($weal)) {
  155. //优惠方式2:充多少送多少
  156. foreach ($weal as $k => $v) {
  157. $thisRecharge = $v['recharge'] ?? 0;
  158. $thisGive = $v['give'] ?? 0;
  159. if ($amount >= $thisRecharge) {
  160. $recharge->giveAmount = $thisGive;
  161. $recharge->save();
  162. //增加赠送的金额
  163. $balanceGive = $thisGive;
  164. }
  165. }
  166. }
  167. }
  168. }
  169. $balancePay = $amount;
  170. if ($onlinePay == 2) {
  171. $event = $payWay == 0 ? '线上微信 充值' : '线上支付宝 充值';
  172. } else {
  173. $payWayName = dict::getDict('payWayName');
  174. $channel = $payWayName[$payWay] ?? '微信';
  175. $event = "线下{$channel} 充值";
  176. }
  177. $event .= floatval($balancePay);
  178. //赠送的余额
  179. if ($balanceGive > 0) {
  180. $event .= "(送" . floatval($balanceGive) . ")";
  181. $custom->balanceGive = bcadd($custom->balanceGive, $balanceGive, 2);
  182. $hd->balanceGive = bcadd($hd->balanceGive, $balanceGive, 2);
  183. }
  184. //充值的余额
  185. $custom->balancePay = bcadd($custom->balancePay, $balancePay, 2);
  186. $hd->balancePay = bcadd($hd->balancePay, $balancePay, 2);
  187. //总余额
  188. $totalBalance = bcadd($balancePay, $balanceGive, 2);
  189. $custom->balance = bcadd($custom->balance, $totalBalance, 2);
  190. $hd->balance = bcadd($hd->balance, $totalBalance, 2);
  191. $custom->save();
  192. $hd->save();
  193. if ($hd->balance != $custom->balance) {
  194. $msg = '充值金额不一致。编号:' . floatval($hd->balance) . ' / ' . floatval($custom->balance);
  195. Yii::info($msg);
  196. util::fail($msg);
  197. }
  198. if ($hd->balancePay != $custom->balancePay) {
  199. $msg = '充值金额有异常。编号:' . floatval($hd->balancePay) . ' / ' . floatval($custom->balancePay);
  200. Yii::info($msg);
  201. util::fail($msg);
  202. }
  203. if ($hd->balanceGive != $custom->balanceGive) {
  204. $msg = '充值金额异常。编号:' . floatval($hd->balanceGive) . ' / ' . floatval($custom->balanceGive);
  205. Yii::info($msg);
  206. util::fail($msg);
  207. }
  208. $addBalance = bcadd($hd->balanceGive, $hd->balancePay, 2);
  209. if (floatval($hd->balance) != floatval($addBalance)) {
  210. util::fail('充值金额有问题哦,编号:' . $customId);
  211. }
  212. $settleId = $recharge->settleId;
  213. $settleAmount = 0;
  214. $settleNo = '';
  215. $realClearAmount = 0;
  216. if (!empty($settleId)) {
  217. //如果有结账单,按结账单销账
  218. $set = SettleClass::getById($settleId, true);
  219. $res = SettleClass::clearSettle($set, $recharge);
  220. $realClearAmount = $res['totalClearAmount'] ?? 0;
  221. $settleAmount = $set->actPrice ?? 0;
  222. $settleNo = $set->orderSn ?? '';
  223. } else {
  224. //没有结账单,按顺序去销订单欠款
  225. $orderList = OrderClass::getAllByCondition(['customId' => $customId, 'debt' => 1], 'id asc', '*', null, true);
  226. if (!empty($orderList)) {
  227. $lastRemainDebtAmount = 0;
  228. $needClearIdMap = [];
  229. $hasOver = false;
  230. foreach ($orderList as $order) {
  231. $orderId = $order->id ?? 0;
  232. $orderSn = $order->orderSn ?? '';
  233. //销订单全部的欠款!!!
  234. $remainDebtPrice = $order->remainDebtPrice ?? 0;
  235. $currentTotalDebtAmount = bcadd($lastRemainDebtAmount, $remainDebtPrice, 2);
  236. if ($currentTotalDebtAmount > $totalBalance) {
  237. if (!$hasOver) {
  238. $lastClearOrderAmount = bcsub($totalBalance, $lastRemainDebtAmount, 2);
  239. if ($lastClearOrderAmount > 0) {
  240. //销订单部分的欠款!!!
  241. $lastClearOrderId = $orderId;
  242. $needClearIdMap[] = ['orderId' => $lastClearOrderId, 'clearAmount' => $lastClearOrderAmount, 'orderSn' => $orderSn];
  243. }
  244. }
  245. $hasOver = true;
  246. continue;
  247. }
  248. $lastRemainDebtAmount = bcadd($lastRemainDebtAmount, $remainDebtPrice, 2);
  249. $needClearIdMap[] = ['orderId' => $orderId, 'clearAmount' => $remainDebtPrice, 'orderSn' => $orderSn];
  250. }
  251. if (!empty($needClearIdMap)) {
  252. //创建结账单,并且销结账单
  253. $staffId = $recharge->staffId ?? 0;
  254. $staffName = $recharge->staffName ?? '';
  255. $settleParams = ['staffId' => $staffId, 'staffName' => $staffName];
  256. $set = SettleClass::addSettle($needClearIdMap, $shop, $custom, $hd, $settleParams);
  257. $settleId = $set->id;
  258. $settleNo = $set->orderSn ?? '';
  259. $settleAmount = $set->actPrice ?? 0;
  260. $res = SettleClass::clearSettle($set);
  261. $realClearAmount = $res['totalClearAmount'] ?? 0;
  262. }
  263. }
  264. }
  265. if ($settleId > 0) {
  266. if (floatval($totalBalance) != floatval($realClearAmount) && $hd->balance < 0) {
  267. $rechargeId = $recharge->id;
  268. noticeUtil::push("注意,充值单,结账金额不一致:getBalance:{$totalBalance} totalAmount:{$realClearAmount},recharge:{$rechargeId}", '15280215347');
  269. util::fail('充值和结账金额异常哈');
  270. }
  271. if (floatval($totalBalance) != floatval($settleAmount) && $hd->balance < 0) {
  272. $rechargeId = $recharge->id;
  273. noticeUtil::push("注意,充值单,结账单不一致:getBalance:{$totalBalance} settleAmount:{$settleAmount},settleId:{$settleId} recharge:{$rechargeId}", '15280215347');
  274. util::fail('充值和结账金额异常呢');
  275. }
  276. if (floatval($realClearAmount) != floatval($settleAmount)) {
  277. $rechargeId = $recharge->id;
  278. noticeUtil::push("注意,结账金额有问题,编号 settleId:{$settleId} recharge:{$rechargeId}", '15280215347');
  279. util::fail('充值结账金额异常呢');
  280. }
  281. }
  282. $remark = $recharge->remark;
  283. $change = [
  284. 'customId' => $customId,
  285. 'customName' => $customName,
  286. 'relateId' => $relateId,
  287. 'onlinePay' => $onlinePay,
  288. 'capitalType' => $capitalType,
  289. 'amount' => $totalBalance,
  290. 'balance' => $hd->balance,
  291. 'io' => 1,
  292. 'side' => $side,
  293. 'payWay' => $payWay,
  294. 'event' => $event,
  295. 'staffId' => $staffId,
  296. 'staffName' => $staffName,
  297. 'shopId' => $shopId,
  298. 'mainId' => $mainId,
  299. 'settleId' => $settleId,
  300. 'settleNo' => $settleNo,
  301. 'settleAmount' => $settleAmount,
  302. 'remark' => $remark,
  303. 'shOrderId' => $shOrderId,
  304. 'shOrderSn' => $shOrderSn,
  305. 'hdId' => $hdId,
  306. 'hdName' => $hdName,
  307. ];
  308. $returnChange = BalanceChangeClass::add($change, true);
  309. $payChange = [
  310. 'customId' => $customId,
  311. 'customName' => $customName,
  312. 'relateId' => $relateId,
  313. 'onlinePay' => $onlinePay,
  314. 'capitalType' => $capitalType,
  315. 'amount' => $balancePay,
  316. 'balance' => $hd->balancePay,
  317. 'io' => 1,
  318. 'side' => $side,
  319. 'payWay' => $payWay,
  320. 'event' => $event,
  321. 'staffId' => $staffId,
  322. 'staffName' => $staffName,
  323. 'shopId' => $shopId,
  324. 'mainId' => $mainId,
  325. 'settleId' => $settleId,
  326. 'settleNo' => $settleNo,
  327. 'settleAmount' => $settleAmount,
  328. 'remark' => $remark,
  329. 'shOrderId' => $shOrderId,
  330. 'shOrderSn' => $shOrderSn,
  331. 'hdId' => $hdId,
  332. 'hdName' => $hdName,
  333. ];
  334. BalancePayChangeClass::add($payChange, true);
  335. if ($balanceGive > 0) {
  336. $givChange = [
  337. 'customId' => $customId,
  338. 'customName' => $customName,
  339. 'relateId' => $relateId,
  340. 'onlinePay' => $onlinePay,
  341. 'capitalType' => $capitalType,
  342. 'amount' => $balanceGive,
  343. 'balance' => $hd->balanceGive,
  344. 'io' => 1,
  345. 'side' => $side,
  346. 'payWay' => $payWay,
  347. 'event' => $event,
  348. 'staffId' => $staffId,
  349. 'staffName' => $staffName,
  350. 'shopId' => $shopId,
  351. 'mainId' => $mainId,
  352. 'remark' => $remark,
  353. 'hdId' => $hdId,
  354. 'hdName' => $hdName,
  355. ];
  356. BalanceGiveChangeClass::add($givChange, true);
  357. }
  358. if ($recharge->onlinePay == 2) {
  359. //如果线上充值,门店余额增加
  360. $shop = ShopClass::getLockById($shopId);
  361. $main = MainClass::getLockById($mainId);
  362. ShopClass::skRechargeAddBalance($main, $shop, $recharge);
  363. } else {
  364. if ($recharge->payWay == 4) {
  365. //现在现金充值要增加现金
  366. $main = MainClass::getLockById($mainId);
  367. $cashData = [];
  368. $cashData['mainId'] = $mainId;
  369. $cashData['sjId'] = 0;
  370. $cashData['amount'] = $amount;
  371. $cashData['staffId'] = $staffId;
  372. $cashData['staffName'] = $staffName;
  373. $cashData['customName'] = $customName;
  374. $cashData['ptStyle'] = dict::getDict('ptStyle', 'hd');
  375. ShopMoneyClass::skRecharge($cashData, $main);
  376. }
  377. }
  378. return $returnChange;
  379. }
  380. /**
  381. * 售后付款单是否已经充值过了 ssh 20250801
  382. */
  383. public static function checkShOrderRecharge($order)
  384. {
  385. $orderId = $order->id;
  386. $has = self::getByCondition(['shOrderId' => $orderId, 'payStatus' => 1]);
  387. if (!empty($has)) {
  388. util::fail('已经充值过了,无需再次操作');
  389. }
  390. }
  391. }