CashClass.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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($currentPtStyle, $shopId)
  37. {
  38. if ($currentPtStyle == dict::getDict('ptStyle', 'hd')) {
  39. return 0.006;
  40. } elseif ($currentPtStyle == dict::getDict('ptStyle', 'ghs')) {
  41. //纯彩花艺二个店0.3费率
  42. if ($shopId == 10 || $shopId == 22) {
  43. return 0.003;
  44. }
  45. return 0.0038;
  46. }
  47. return 0.006;
  48. }
  49. //提现记录 ssh 2021.2.21
  50. public static function getCashList($where)
  51. {
  52. $data = self::getList('*', $where, 'addTime DESC');
  53. return $data;
  54. }
  55. //申请提现
  56. public static function addApply($params)
  57. {
  58. $sjId = $params['sjId'] ?? 0;
  59. $shopId = $params['shopId'] ?? 0;
  60. $shop = ShopClass::getById($shopId, true);
  61. if (empty($shopId)) {
  62. util::fail('没有找到门店');
  63. }
  64. $mainId = $shop->mainId ?? 0;
  65. $main = MainClass::getLockById($mainId);
  66. if (empty($main)) {
  67. util::fail('没有找到余额');
  68. }
  69. $cashList = self::getByCondition(['mainId' => $mainId, 'status' => 1], true);
  70. if (!empty($cashList)) {
  71. util::fail('已有提现审核待审核');
  72. }
  73. $txBalance = $main->txBalance ?? 0.00;
  74. if ($txBalance <= 0) {
  75. util::fail('可提现余额不足');
  76. }
  77. if (bccomp($txBalance, $main->balance) == 1) {
  78. util::fail('余额有问题,总余额比可提现余额还少');
  79. }
  80. $miniCashAmount = dict::getDict('miniCashAmount');
  81. if (bccomp($miniCashAmount, $txBalance) == 1) {
  82. if (getenv('YII_ENV') == 'production') {
  83. util::fail("满{$miniCashAmount}元可提现");
  84. }
  85. }
  86. //可提现余额减少,总余额减少,冻结金额增加
  87. $currentTxBalance = bcsub($main->txBalance, $txBalance, 2);
  88. $main->txBalance = $currentTxBalance;
  89. $main->freezeBalance = bcadd($main->freezeBalance, $txBalance, 2);
  90. $currentBalance = bcsub($main->balance, $txBalance, 2);
  91. $main->balance = $currentBalance;
  92. $main->save();
  93. //应转金额
  94. $ptStyle = $shop->ptStyle;
  95. $rate = CashClass::getRate($ptStyle, $shop->id);
  96. $tip = bcmul($txBalance, $rate, 2);
  97. $beAmount = bcsub($txBalance, $tip, 2);
  98. $cashName = $main->cashName ?? '';
  99. $cashAccount = $main->cashAccount ?? '';
  100. if (empty($cashName) || empty($cashAccount)) {
  101. util::fail('请填写提现账号');
  102. }
  103. $shopName = $shop->shopName ?? '';
  104. $sjName = $shop->merchantName ?? '';
  105. $orderSn = orderSn::getCashSn();
  106. $params['orderSn'] = $orderSn;
  107. $params['event'] = '申请提现';
  108. $params['sjName'] = $sjName;
  109. $params['shopName'] = $shopName;
  110. $params['amount'] = $txBalance;
  111. $params['beAmount'] = $beAmount;
  112. $params['mainId'] = $mainId;
  113. $return = self::add($params, true);
  114. $tx = dict::getDict('yeTx', 'can');
  115. $capitalType = dict::getDict('capitalType', 'xhDrawCash', 'id');
  116. //余额减少记录
  117. $id = $return->id;
  118. $change = [
  119. 'relateId' => $id,
  120. 'capitalType' => $capitalType,
  121. 'amount' => $txBalance,
  122. 'balance' => $currentBalance,
  123. 'txBalance' => $currentTxBalance,
  124. 'io' => 0,
  125. 'payWay' => 0,
  126. 'event' => "提现",
  127. 'sjId' => $sjId,
  128. 'shopId' => $shopId,
  129. 'tx' => $tx,
  130. 'ptStyle' => $ptStyle,
  131. 'mainId' => $mainId
  132. ];
  133. ShopYeChangeClass::addChange($change, true);
  134. $txAsset = null;
  135. if ($ptStyle == dict::getDict('ptStyle', 'hd')) {
  136. $txAsset = PtAssetClass::getHdTxBalance();
  137. } elseif ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
  138. $txAsset = PtAssetClass::getGhsTxBalance();
  139. } else {
  140. util::fail('提现失败');
  141. }
  142. $currentPtTxBalance = $txAsset->amount;
  143. $currentPtTxBalance = bcsub($currentPtTxBalance, $txBalance, 2);
  144. $txAsset->amount = $currentPtTxBalance;
  145. $txAsset->save();
  146. $asset = null;
  147. if ($ptStyle == dict::getDict('ptStyle', 'hd')) {
  148. $asset = PtAssetClass::getHdBalance();
  149. } elseif ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
  150. $asset = PtAssetClass::getGhsBalance();
  151. } else {
  152. util::fail('没有找到平台');
  153. }
  154. if (bccomp($txBalance, $asset->amount, 2) == 1) {
  155. util::fail('平台余额不足');
  156. }
  157. $currentPtBalance = bcsub($asset->amount, $txBalance, 2);
  158. $asset->amount = $currentPtBalance;
  159. $asset->save();
  160. $change = [
  161. 'relateId' => $id,
  162. 'capitalType' => $capitalType,
  163. 'amount' => $txBalance,
  164. 'balance' => $currentPtBalance,
  165. 'txBalance' => $currentPtTxBalance,
  166. 'io' => 0,
  167. 'payWay' => 0,
  168. 'event' => "【{$shop->merchantName} {$shop->shopName}】提现",
  169. 'sjId' => $sjId,
  170. 'shopId' => $shopId,
  171. 'tx' => $tx,
  172. 'ptStyle' => $ptStyle,
  173. 'shopName' => $shop->shopName,
  174. 'sjName' => $shop->merchantName,
  175. 'mainId' => $mainId
  176. ];
  177. PtYeChangeClass::addChange($change, true);
  178. noticeUtil::push("客户:{$sjName} {$shopName}申请提现,金额:{$txBalance}元", '15280215347');
  179. return $return;
  180. }
  181. public static function rollback($cash, $shop, $transaction, $cashAmount, $remark)
  182. {
  183. $mainId = $shop->mainId ?? 0;
  184. $main = MainClass::getLockById($mainId);
  185. if (empty($main)) {
  186. util::fail('没有main信息');
  187. }
  188. $cash->remark = $remark;
  189. $cash->status = self::STATUS_CHECK_NO;
  190. $cash->save();
  191. //冻结金额回滚
  192. $main->freezeBalance = bcsub($main->freezeBalance, $cashAmount, 2);
  193. $currentBalance = bcadd($main->balance, $cashAmount, 2);
  194. $main->balance = $currentBalance;
  195. $currentTxBalance = bcadd($main->txBalance, $cashAmount, 2);
  196. $main->txBalance = $currentTxBalance;
  197. $main->save();
  198. $sjId = $shop->sjId;
  199. $shopId = $shop->id;
  200. $tx = dict::getDict('yeTx', 'can');
  201. $ptStyle = $shop->ptStyle ?? dict::getDict('ptStyle', 'hd');
  202. $capitalType = dict::getDict('capitalType', 'xhDrawCash', 'id');
  203. //余额增加记录
  204. $id = $cash->id;
  205. $change = [
  206. 'relateId' => $id,
  207. 'capitalType' => $capitalType,
  208. 'amount' => $cashAmount,
  209. 'balance' => $currentBalance,
  210. 'txBalance' => $currentTxBalance,
  211. 'io' => 1,
  212. 'payWay' => 0,
  213. 'event' => "提现回滚",
  214. 'sjId' => $sjId,
  215. 'shopId' => $shopId,
  216. 'mainId' => $mainId,
  217. 'tx' => $tx,
  218. 'ptStyle' => $ptStyle,
  219. ];
  220. ShopYeChangeClass::addChange($change, true);
  221. $txAsset = null;
  222. if ($ptStyle == dict::getDict('ptStyle', 'hd')) {
  223. $txAsset = PtAssetClass::getHdTxBalance();
  224. } elseif ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
  225. $txAsset = PtAssetClass::getGhsTxBalance();
  226. } else {
  227. CashClass::rollback($cash, $shop, $transaction, $cashAmount, $remark);
  228. util::fail('没有找到平台');
  229. }
  230. $currentPtTxBalance = $txAsset->amount;
  231. $currentPtTxBalance = bcadd($currentPtTxBalance, $cashAmount, 2);
  232. $txAsset->amount = $currentPtTxBalance;
  233. $txAsset->save();
  234. $asset = null;
  235. if ($ptStyle == dict::getDict('ptStyle', 'hd')) {
  236. $asset = PtAssetClass::getHdBalance();
  237. } elseif ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
  238. $asset = PtAssetClass::getGhsBalance();
  239. } else {
  240. util::fail('没有找到平台');
  241. }
  242. $currentPtBalance = bcadd($asset->amount, $cashAmount, 2);
  243. $asset->amount = $currentPtBalance;
  244. $asset->save();
  245. $change = [
  246. 'relateId' => $id,
  247. 'capitalType' => $capitalType,
  248. 'amount' => $cashAmount,
  249. 'balance' => $currentPtBalance,
  250. 'txBalance' => $currentPtTxBalance,
  251. 'io' => 1,
  252. 'payWay' => 0,
  253. 'event' => "【{$shop->merchantName} {$shop->shopName}】提现回滚",
  254. 'sjId' => $sjId,
  255. 'shopId' => $shopId,
  256. 'tx' => $tx,
  257. 'ptStyle' => $ptStyle,
  258. 'shopName' => $shop->shopName,
  259. 'sjName' => $shop->merchantName,
  260. ];
  261. PtYeChangeClass::addChange($change, true);
  262. $transaction->commit();
  263. }
  264. }