StoreCash.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use app\utils\WechatMerchant\WxV3;
  9. use Yii;
  10. use yii\db\ActiveRecord;
  11. use yii\behaviors\TimestampBehavior;
  12. /**
  13. * This is the model class for table "{{%cash}}".
  14. *
  15. * @property int $id
  16. * @property int $user_id 用户id
  17. * @property int $saas_id 商盟id
  18. * @property int $store_id 商城id
  19. * @property float $price 提现金额
  20. * @property int $status 申请状态 0--申请中 1--确认申请 2--已打款 3--驳回 5--余额通过
  21. * @property int $is_delete 是否删除
  22. * @property int|null $created_at 添加時間
  23. * @property int $pay_time 付款
  24. * @property int $type 支付方式 0--微信支付 1--支付宝 2--银行卡 3--余额
  25. * @property string|null $mobile 支付宝账号
  26. * @property string|null $name 支付宝姓名
  27. * @property string|null $bank_name 开户行名称
  28. * @property int|null $pay_type 打款方式 0--之前未统计的 1--微信自动打款 2--手动打款
  29. * @property string|null $order_no 微信自动打款订单号
  30. * @property float $service_charge 提现手续费
  31. * @property int|null $cash_type 0、正常,1、自提点提现
  32. * @property int|null $updated_at 更新时间
  33. * @property int $md_id 门店id
  34. * @property string|null $out_detail_no 微信转账明细单号
  35. * @property string|null $batch_id 微信转账明细单号
  36. * @property string|null $transfer_status 转账状态
  37. * @property string|null $transfer_time 转账成功时间
  38. * @property string|null $transfer_detail 微信返回的原始转账明细
  39. * @property string|null $partner_trade_no 微信返回的订单号 *
  40. * @property int $is_ref 是否重新打款
  41. * @property string|null $ref_order_no 重新打款订单号
  42. */
  43. class StoreCash extends \yii\db\ActiveRecord
  44. {
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public static function tableName()
  49. {
  50. return '{{%store_cash}}';
  51. }
  52. /**
  53. * 已删除
  54. */
  55. const IS_DELETE_YES = 1;
  56. /**
  57. * 未删除
  58. */
  59. const IS_DELETE_NO = 0;
  60. /**
  61. * 申请中
  62. */
  63. const STATUS_APPLY = 0;
  64. /**
  65. * 确认申请
  66. */
  67. const STATUS_CONFIRM = 1;
  68. /**
  69. * 已打款
  70. */
  71. const STATUS_GIVEN = 2;
  72. /**
  73. * 驳回
  74. */
  75. const STATUS_REFUSE = 3;
  76. /**
  77. * 手动打款
  78. */
  79. const STATUS_HAND = 4;
  80. /**
  81. * 余额通过
  82. */
  83. const STATUS_RECHARGE = 5;
  84. /**
  85. * 微信打款
  86. */
  87. const TYPE_WX = 0;
  88. /**
  89. * 支付宝打款
  90. */
  91. const TYPE_ALIPAY = 1;
  92. /**
  93. * 银行卡打款
  94. */
  95. const TYPE_BANK = 2;
  96. /**
  97. * 余额打款
  98. */
  99. const TYPE_RECHARGE = 3;
  100. /**
  101. * 之前未统计的
  102. */
  103. const PAY_TYPE_NO = 0;
  104. /**
  105. * 微信自动打款
  106. */
  107. const PAY_TYPE_WX = 1;
  108. /**
  109. * 手动拨款
  110. */
  111. const PAY_TYPE_HAND = 2;
  112. /**
  113. * 正常
  114. */
  115. const CASH_TYPE_NORMAL = 0;
  116. /**
  117. * 联盟券
  118. */
  119. const CASH_TYPE_ALLIANCE_COUPON = 1;
  120. /**
  121. * 串码红包
  122. */
  123. const CASH_TYPE_STRING_CODE = 2;
  124. public static $status = [
  125. '待审核',
  126. '待打款',
  127. '已打款',
  128. '无效'
  129. ];
  130. public static $type = [
  131. '微信线下支付',
  132. '支付宝支付',
  133. '银行卡支付',
  134. '余额支付',
  135. ];
  136. public function behaviors()
  137. {
  138. return [
  139. [
  140. 'class' => TimestampBehavior::class,
  141. 'attributes' => [
  142. ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
  143. ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at']
  144. ]
  145. ]
  146. ];
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. public function rules()
  152. {
  153. return [
  154. [['store_id'], 'required'],
  155. [['md_id', 'user_id', 'saas_id', 'store_id', 'status', 'is_delete', 'created_at', 'pay_time', 'type', 'pay_type', 'cash_type', 'updated_at'], 'integer'],
  156. [['price', 'service_charge'], 'number'],
  157. [['mobile', 'name', 'order_no'], 'string', 'max' => 255],
  158. [['bank_name'], 'string', 'max' => 30],
  159. [['user_id', 'pay_time', 'reject_reason'], 'safe'],
  160. ];
  161. }
  162. /**
  163. * {@inheritdoc}
  164. */
  165. public function attributeLabels()
  166. {
  167. return [
  168. 'id' => 'ID',
  169. 'user_id' => '用户id',
  170. 'saas_id' => '商盟用户id',
  171. 'store_id' => '商城id',
  172. 'price' => '提现金额',
  173. 'status' => '申请状态 0--申请中 1--确认申请 2--已打款 3--驳回 5--余额通过',
  174. 'is_delete' => '是否删除',
  175. 'created_at' => '添加時間',
  176. 'pay_time' => '付款',
  177. 'type' => '支付方式 0--微信支付 1--支付宝 2--银行卡 3--余额',
  178. 'mobile' => '支付宝账号',
  179. 'name' => '支付宝姓名',
  180. 'bank_name' => '开户行名称',
  181. 'pay_type' => '打款方式 0--之前未统计的 1--微信自动打款 2--手动打款',
  182. 'order_no' => '微信自动打款订单号',
  183. 'service_charge' => '提现手续费',
  184. 'cash_type' => '0、正常,1、自提点提现',
  185. 'updated_at' => '更新时间',
  186. ];
  187. }
  188. public static function getStatusName($cash)
  189. {
  190. $name = '';
  191. if($cash['status'] == 0){
  192. $name = '申请中';
  193. }
  194. if($cash['status'] == 1){
  195. $name = '确认申请';
  196. }
  197. if($cash['status'] == 2){
  198. $name = '已打款';
  199. }
  200. if($cash['status'] == 3){
  201. $name = '驳回';
  202. }
  203. if($cash['status'] == 5){
  204. $name = '余额通过';
  205. }
  206. return $name;
  207. }
  208. public static function getTypeName($cash)
  209. {
  210. $name = '';
  211. if($cash['type'] == 0){
  212. $name = '微信';
  213. }
  214. if($cash['type'] == 1){
  215. $name = '支付宝';
  216. }
  217. if($cash['type'] == 2){
  218. $name = '银行卡';
  219. }
  220. if($cash['type'] == 3){
  221. $name = '余额';
  222. }
  223. return $name;
  224. }
  225. public static function getServiceMoney($cash)
  226. {
  227. if($cash['service_charge'] == 0){
  228. $price = $cash['price'];
  229. }else{
  230. $cashPrice = floatval($cash['price']);
  231. $price = $cashPrice * (100 - $cash['service_charge']) / 100;
  232. }
  233. return round($price, 2);
  234. }
  235. //最大提现金额
  236. public static function getMaxCash($store, &$data = []) {
  237. $storeMoney = Store::getMoney($store);
  238. $storeMoneyTotal = Store::getTotalMoney($store);
  239. $moneyMd = Md::find()->where(['store_id' => $store->id, 'is_delete' => 0])
  240. ->andWhere(['>', 'cash_profit', 0])
  241. ->sum('cash_profit') ?: 0;
  242. $moneyUser = User::find()->where(['store_id' => $store->id, 'is_delete' => 0])
  243. ->andWhere(['>', 'price', 0])
  244. ->sum('price') ?: 0;
  245. $hasIncoming = Store::hasIncoming($store->id);
  246. if($hasIncoming){
  247. $max = $storeMoney;
  248. }else{
  249. $max = bcsub(bcsub($storeMoney, $moneyMd, 2), $moneyUser, 2);//$storeMoney - $moneyMd - $moneyUser;
  250. }
  251. $data['storeMoneyTotal'] = $storeMoneyTotal;
  252. $data['storeMoney'] = $storeMoney;
  253. $data['moneyUser'] = $moneyUser;
  254. $data['moneyMd'] = $moneyMd;
  255. $data['moneyFrozen'] = bcadd($moneyMd, $moneyUser, 2);
  256. return $max;
  257. }
  258. //新增判断openid
  259. // public function beforeSave($insert)
  260. // {
  261. // // 判断是否有 openid
  262. // if (empty($this->mobile)) {
  263. // // 如果没有 openid
  264. // return false;
  265. // }
  266. // // 如果有 openid,继续保存
  267. // return parent::beforeSave($insert);
  268. // }
  269. public static function getRejectReason($batch_id){
  270. $wechat = getAllianceClientWxConfig();
  271. $result = (new WxV3($wechat))->queryTransferStatus($batch_id, 0);
  272. $resfobj = json_decode($result);
  273. $transfer_detail_list = $resfobj->transfer_detail_list;
  274. $detailref = '';
  275. if ($transfer_detail_list) {
  276. $detail_response = (new WxV3($wechat))->queryTransferDetail($batch_id, $transfer_detail_list[0]->detail_id, 0);
  277. $detail_response_obj = json_decode($detail_response);
  278. $detailref = self::handleTransferFailure($detail_response_obj->fail_reason);
  279. }
  280. return $detailref;
  281. }
  282. public static function handleTransferFailure($failure_reason)
  283. {
  284. // 根据失败原因进行处理
  285. switch ($failure_reason) {
  286. case 'ACCOUNT_FROZEN':
  287. return "该用户账户被冻结,请联系客服处理。";
  288. case 'REAL_NAME_CHECK_FAIL':
  289. return "收款人未实名认证,需要用户完成微信实名认证。";
  290. case 'NAME_NOT_CORRECT':
  291. return "收款人姓名校验不通过,请核实信息。";
  292. case 'OPENID_INVALID':
  293. return "Openid格式错误或者不属于商家公众账号。";
  294. case 'TRANSFER_QUOTA_EXCEED':
  295. return "超过用户单笔收款额度,请核实产品设置是否准确。";
  296. case 'DAY_RECEIVED_QUOTA_EXCEED':
  297. return "超过用户单日收款额度,请核实产品设置是否准确。";
  298. case 'MONTH_RECEIVED_QUOTA_EXCEED':
  299. return "超过用户单月收款额度,请核实产品设置是否准确。";
  300. case 'DAY_RECEIVED_COUNT_EXCEED':
  301. return "超过用户单日收款次数,请核实产品设置是否准确。";
  302. case 'PRODUCT_AUTH_CHECK_FAIL':
  303. return "未开通该权限或权限被冻结,请核实产品权限状态。";
  304. case 'OVERDUE_CLOSE':
  305. return "超过系统重试期,系统自动关闭。";
  306. case 'ID_CARD_NOT_CORRECT':
  307. return "收款人身份证校验不通过,请核实信息。";
  308. case 'ACCOUNT_NOT_EXIST':
  309. return "该用户账户不存在,请核实用户信息。";
  310. case 'TRANSFER_RISK':
  311. return "该笔转账可能存在风险,已被微信拦截。";
  312. case 'OTHER_FAIL_REASON_TYPE':
  313. return "其它失败原因,请联系微信支付客服。";
  314. case 'REALNAME_ACCOUNT_RECEIVED_QUOTA_EXCEED':
  315. return "用户账户收款受限,请引导用户在微信支付查看详情。";
  316. case 'RECEIVE_ACCOUNT_NOT_PERMMIT':
  317. return "未配置该用户为转账收款人,请在产品设置中调整,添加该用户为收款人。";
  318. case 'PAYEE_ACCOUNT_ABNORMAL':
  319. return "用户账户收款异常,请联系用户完善其在微信支付的身份信息。";
  320. case 'PAYER_ACCOUNT_ABNORMAL':
  321. return "商户账户付款受限,请前往商户平台获取解除功能限制指引。";
  322. case 'TRANSFER_SCENE_UNAVAILABLE':
  323. return "该转账场景暂不可用,请确认转账场景ID是否正确。";
  324. case 'TRANSFER_SCENE_INVALID':
  325. return "你尚未获取该转账场景,请确认转账场景ID是否正确。";
  326. case 'TRANSFER_REMARK_SET_FAIL':
  327. return "转账备注设置失败,请调整后重新再试。";
  328. case 'RECEIVE_ACCOUNT_NOT_CONFIGURE':
  329. return "请前往商户平台-商家转账到零钱-前往功能-转账场景中添加。";
  330. case 'BLOCK_B2C_USERLIMITAMOUNT_BSRULE_MONTH':
  331. return "超出用户单月转账收款20w限额,本月不支持继续向该用户付款。";
  332. case 'BLOCK_B2C_USERLIMITAMOUNT_MONTH':
  333. return "用户账户存在风险收款受限,本月不支持继续向该用户付款。";
  334. case 'MERCHANT_REJECT':
  335. return "商户员工(转账验密人)已驳回转账,请检查操作。";
  336. case 'MERCHANT_NOT_CONFIRM':
  337. return "商户员工(转账验密人)超时未验密,请及时完成操作。";
  338. default:
  339. return "未知的失败原因,请稍后重试或联系微信支付客服。";
  340. }
  341. }
  342. }