CashClass.php 10 KB

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