CashClass.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. namespace biz\sj\classes;
  3. use biz\pt\classes\PtAssetClass;
  4. use biz\pt\classes\PtYeChangeClass;
  5. use biz\shop\classes\MainClass;
  6. use biz\shop\classes\ShopClass;
  7. use biz\shop\classes\ShopYeChangeClass;
  8. use bizHd\base\classes\BaseClass;
  9. use common\components\dict;
  10. use common\components\noticeUtil;
  11. use common\components\orderSn;
  12. use common\components\util;
  13. class CashClass extends BaseClass
  14. {
  15. public static $baseFile = '\biz\sj\models\Cash';
  16. const STATUS_UN_CHECK = 1;
  17. const STATUS_CHECK_YES = 2;
  18. const STATUS_CHECK_NO = 3;
  19. //给代理发放佣金
  20. public static function sendBrokerage($shop, $cash)
  21. {
  22. $agentShopId = $shop->parentShopId ?? 0;
  23. //没有上级代理不需要分佣
  24. if (empty($agentShopId)) {
  25. return false;
  26. }
  27. $agentShop = ShopClass::getLockById($agentShopId);
  28. if (empty($agentShop)) {
  29. util::fail('没有找到代理人门店');
  30. }
  31. $capitalType = dict::getDict('capitalType', 'brokerage', 'id');
  32. //代理分佣获得余额收入
  33. ShopClass::brokerageAddBalance($agentShop, $cash, $capitalType);
  34. }
  35. //获取提现费率 ssh 20210623
  36. public static function getRate($main)
  37. {
  38. $rate = $main->rate ?? 6;
  39. $rateVal = bcdiv($rate, 1000, 4);
  40. $rateVal = floatval($rateVal);
  41. return $rateVal;
  42. }
  43. //提现记录 ssh 2021.2.21
  44. public static function getCashList($where)
  45. {
  46. $data = self::getList('*', $where, 'addTime DESC');
  47. return $data;
  48. }
  49. //申请提现
  50. public static function addApply($params)
  51. {
  52. $sjId = $params['sjId'] ?? 0;
  53. $shopId = $params['shopId'] ?? 0;
  54. $shop = ShopClass::getById($shopId, true);
  55. if (empty($shopId)) {
  56. util::fail('没有找到门店1');
  57. }
  58. $mainId = $shop->mainId ?? 0;
  59. $main = MainClass::getLockById($mainId);
  60. if (empty($main)) {
  61. util::fail('没有找到商家');
  62. }
  63. $cashList = self::getByCondition(['mainId' => $mainId, 'status' => 1], true);
  64. if (!empty($cashList)) {
  65. util::fail('已有提现审核待审核');
  66. }
  67. $txBalance = $main->txBalance ?? 0.00;
  68. if ($txBalance <= 0) {
  69. util::fail('可提现余额不足');
  70. }
  71. if (bccomp($txBalance, $main->balance) == 1) {
  72. util::fail('余额有问题,总余额比可提现余额还少');
  73. }
  74. //可提现余额减少,总余额减少,冻结金额增加
  75. $currentTxBalance = bcsub($main->txBalance, $txBalance, 2);
  76. $main->txBalance = $currentTxBalance;
  77. $main->freezeBalance = bcadd($main->freezeBalance, $txBalance, 2);
  78. $currentBalance = bcsub($main->balance, $txBalance, 2);
  79. $main->balance = $currentBalance;
  80. $main->save();
  81. //应转金额
  82. $ptStyle = $shop->ptStyle;
  83. $rate = CashClass::getRate($main);
  84. $tip = bcmul($txBalance, $rate, 2);
  85. $beAmount = bcsub($txBalance, $tip, 2);
  86. $shopName = $shop->shopName ?? '';
  87. $sjName = $shop->merchantName ?? '';
  88. $orderSn = orderSn::getCashSn();
  89. $params['orderSn'] = $orderSn;
  90. $params['event'] = '申请提现';
  91. $params['sjName'] = $sjName;
  92. $params['shopName'] = $shopName;
  93. $params['amount'] = $txBalance;
  94. $params['beAmount'] = $beAmount;
  95. $params['mainId'] = $mainId;
  96. $return = self::add($params, true);
  97. $tx = dict::getDict('yeTx', 'can');
  98. $capitalType = dict::getDict('capitalType', 'xhDrawCash', 'id');
  99. //余额减少记录
  100. $id = $return->id;
  101. $change = [
  102. 'relateId' => $id,
  103. 'capitalType' => $capitalType,
  104. 'amount' => $txBalance,
  105. 'balance' => $currentBalance,
  106. 'txBalance' => $currentTxBalance,
  107. 'io' => 0,
  108. 'payWay' => 0,
  109. 'event' => "提现",
  110. 'sjId' => $sjId,
  111. 'shopId' => $shopId,
  112. 'tx' => $tx,
  113. 'ptStyle' => $ptStyle,
  114. 'mainId' => $mainId
  115. ];
  116. ShopYeChangeClass::addChange($change, true);
  117. $txAsset = null;
  118. if ($ptStyle == dict::getDict('ptStyle', 'hd')) {
  119. $txAsset = PtAssetClass::getHdTxBalance();
  120. } elseif ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
  121. $txAsset = PtAssetClass::getGhsTxBalance();
  122. } else {
  123. util::fail('提现失败');
  124. }
  125. $currentPtTxBalance = $txAsset->amount;
  126. $currentPtTxBalance = bcsub($currentPtTxBalance, $txBalance, 2);
  127. $txAsset->amount = $currentPtTxBalance;
  128. $txAsset->save();
  129. $asset = null;
  130. if ($ptStyle == dict::getDict('ptStyle', 'hd')) {
  131. $asset = PtAssetClass::getHdBalance();
  132. } elseif ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
  133. $asset = PtAssetClass::getGhsBalance();
  134. } else {
  135. util::fail('没有找到平台');
  136. }
  137. if (bccomp($txBalance, $asset->amount, 2) == 1) {
  138. //util::fail('平台余额不足..');
  139. //noticeUtil::push('商家提现,平台余额不足。商家提现:' . $txBalance . ',平台余额:' . $asset->amount, '15280215347');
  140. }
  141. $currentPtBalance = bcsub($asset->amount, $txBalance, 2);
  142. $asset->amount = $currentPtBalance;
  143. $asset->save();
  144. $change = [
  145. 'relateId' => $id,
  146. 'capitalType' => $capitalType,
  147. 'amount' => $txBalance,
  148. 'balance' => $currentPtBalance,
  149. 'txBalance' => $currentPtTxBalance,
  150. 'io' => 0,
  151. 'payWay' => 0,
  152. 'event' => "【{$shop->merchantName} {$shop->shopName}】提现",
  153. 'sjId' => $sjId,
  154. 'shopId' => $shopId,
  155. 'tx' => $tx,
  156. 'ptStyle' => $ptStyle,
  157. 'shopName' => $shop->shopName,
  158. 'sjName' => $shop->merchantName,
  159. 'mainId' => $mainId
  160. ];
  161. PtYeChangeClass::addChange($change, true);
  162. //noticeUtil::push("客户:{$sjName} {$shopName}申请提现,金额:{$txBalance}元", '15280215347');
  163. return $return;
  164. }
  165. public static function rollback($cash, $shop, $transaction, $cashAmount, $remark)
  166. {
  167. $mainId = $shop->mainId ?? 0;
  168. $main = MainClass::getLockById($mainId);
  169. if (empty($main)) {
  170. util::fail('没有main信息1');
  171. }
  172. $cash->remark = $remark;
  173. $cash->status = self::STATUS_CHECK_NO;
  174. $cash->save();
  175. //冻结金额回滚
  176. $main->freezeBalance = bcsub($main->freezeBalance, $cashAmount, 2);
  177. $currentBalance = bcadd($main->balance, $cashAmount, 2);
  178. $main->balance = $currentBalance;
  179. $currentTxBalance = bcadd($main->txBalance, $cashAmount, 2);
  180. $main->txBalance = $currentTxBalance;
  181. $main->save();
  182. $sjId = $shop->sjId;
  183. $shopId = $shop->id;
  184. $tx = dict::getDict('yeTx', 'can');
  185. $ptStyle = $shop->ptStyle ?? dict::getDict('ptStyle', 'hd');
  186. $capitalType = dict::getDict('capitalType', 'xhDrawCash', 'id');
  187. //余额增加记录
  188. $id = $cash->id;
  189. $change = [
  190. 'relateId' => $id,
  191. 'capitalType' => $capitalType,
  192. 'amount' => $cashAmount,
  193. 'balance' => $currentBalance,
  194. 'txBalance' => $currentTxBalance,
  195. 'io' => 1,
  196. 'payWay' => 0,
  197. 'event' => "提现回滚",
  198. 'sjId' => $sjId,
  199. 'shopId' => $shopId,
  200. 'mainId' => $mainId,
  201. 'tx' => $tx,
  202. 'ptStyle' => $ptStyle,
  203. ];
  204. ShopYeChangeClass::addChange($change, true);
  205. $txAsset = null;
  206. if ($ptStyle == dict::getDict('ptStyle', 'hd')) {
  207. $txAsset = PtAssetClass::getHdTxBalance();
  208. } elseif ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
  209. $txAsset = PtAssetClass::getGhsTxBalance();
  210. } else {
  211. CashClass::rollback($cash, $shop, $transaction, $cashAmount, $remark);
  212. util::fail('没有找到平台');
  213. }
  214. $currentPtTxBalance = $txAsset->amount;
  215. $currentPtTxBalance = bcadd($currentPtTxBalance, $cashAmount, 2);
  216. $txAsset->amount = $currentPtTxBalance;
  217. $txAsset->save();
  218. $asset = null;
  219. if ($ptStyle == dict::getDict('ptStyle', 'hd')) {
  220. $asset = PtAssetClass::getHdBalance();
  221. } elseif ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
  222. $asset = PtAssetClass::getGhsBalance();
  223. } else {
  224. util::fail('没有找到平台');
  225. }
  226. $currentPtBalance = bcadd($asset->amount, $cashAmount, 2);
  227. $asset->amount = $currentPtBalance;
  228. $asset->save();
  229. $change = [
  230. 'relateId' => $id,
  231. 'capitalType' => $capitalType,
  232. 'amount' => $cashAmount,
  233. 'balance' => $currentPtBalance,
  234. 'txBalance' => $currentPtTxBalance,
  235. 'io' => 1,
  236. 'payWay' => 0,
  237. 'event' => "【{$shop->merchantName} {$shop->shopName}】提现回滚",
  238. 'sjId' => $sjId,
  239. 'shopId' => $shopId,
  240. 'tx' => $tx,
  241. 'ptStyle' => $ptStyle,
  242. 'shopName' => $shop->shopName,
  243. 'sjName' => $shop->merchantName,
  244. ];
  245. PtYeChangeClass::addChange($change, true);
  246. $transaction->commit();
  247. }
  248. }