Withdraw.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?php
  2. namespace common\models;
  3. use common\helpers\Cache;
  4. use common\helpers\Date;
  5. use common\helpers\Tool;
  6. use common\helpers\user\Info;
  7. use Yii;
  8. /**
  9. * This is the model class for table "{{%WITHDRAW}}".
  10. *
  11. * @property string $ID
  12. * @property string $SN 单号
  13. * @property string $USER_ID 会员ID
  14. * @property string $ID_CARD 身份证号
  15. * @property int $WITHDRAW_PERIOD_NUM 提现期数
  16. * @property int $WITHDRAW_YEAR 提现年份
  17. * @property int $WITHDRAW_MONTH 提现月份
  18. * @property int $PAY_TYPE 付款类型
  19. * @property int $IS_AUTO_WITHDRAW 是否自动提现
  20. * @property string $AMOUNT 提现金额
  21. * @property string $FEES 提现手续费
  22. * @property string $REAL_AMOUNT 实际提现金额
  23. * @property string $INVOICE_ID 发票ID
  24. * @property string $REAL_NAME 会员姓名
  25. * @property string $OPEN_BANK 开户行
  26. * @property string $BANK_ADDRESS 银行地址
  27. * @property string $BANK_NO 银行账号
  28. * @property int $BANK_PROVINCE 银行省份
  29. * @property int $BANK_CITY 银行城市
  30. * @property int $BANK_COUNTY 银行地区
  31. * @property string $P_MONTH 表分区标识
  32. * @property int $AUDIT_STATUS 审核状态
  33. * @property string $REMARK 备注
  34. * @property string $UPDATE_ADMIN 修改管理员
  35. * @property string $UPDATE_REMARK 修改备注
  36. * @property string $AUDIT_ADMIN 审核管理员
  37. * @property int $CREATED_AT 创建时间
  38. * @property int $UPDATED_AT 修改时间
  39. * @property int $AUDITED_AT 审核时间
  40. * @property int $PLAN_PAID_AT 预计付款时间
  41. * @property int $PAID_AT 付款时间
  42. * @property string $PAID_FAIL_REMARK 付款失败备注
  43. * @property int $PAID_FAIL_AT 付款失败时间
  44. * @property string $BACK_REMARK 提现退回备注
  45. * @property int $BACK_FAIL_AT 提现退回时间
  46. */
  47. class Withdraw extends \common\components\ActiveRecord {
  48. const TYPE_MANUAL = 0; // 手动提现
  49. const TYPE_AUTO = 1; // 自动提现
  50. const TYPE_ADMIN = 2; // 后台管理员
  51. //付款类型
  52. const PAY_TYPE_NO_INVOICE = 0;
  53. const PAY_TYPE_NEED_INVOICE = 1;
  54. const STATUS_APPLIED = 0; // 已申请
  55. // const STATUS_INVOICED = 1; // 已传票
  56. const STATUS_AUDITED = 2; // 已审核
  57. const STATUS_WAIT_PAID = 3; // 待付款
  58. const STATUS_PAID_FALSE = 4; // 付款失败
  59. const STATUS_REFUSED = 5; // 已拒绝
  60. const STATUS_PAID = 6; // 已付款
  61. const STATUS_RETURN = 7; // 提现退回
  62. const STATUS_NAME = [
  63. self::STATUS_APPLIED => 'Applied', //Yii::t('ctx', 'commonModelsWithdrawStatusNameStatusApplied'), // 提现待审核
  64. // self::STATUS_INVOICED => '提现待审核',
  65. self::STATUS_AUDITED => 'Withdrawal has been reviewed', //Yii::t('ctx', 'commonModelsWithdrawStatusNameStatusAudited'), // 提现已审核
  66. self::STATUS_WAIT_PAID => 'Unpaid', //Yii::t('ctx', 'commonModelsWithdrawStatusNameStatusWaitPaid'), // 提现待付款
  67. self::STATUS_PAID_FALSE => 'Failed',//Yii::t('ctx', 'commonModelsWithdrawStatusNameStatusPaidFalse'), // 付款失败
  68. self::STATUS_REFUSED => 'Refused',//Yii::t('ctx', 'commonModelsWithdrawStatusNameStatusRefused'), // 已拒绝
  69. self::STATUS_PAID => 'Withdrawal has been paid',//Yii::t('ctx', 'commonModelsWithdrawStatusNameStatusPaid'), // 提现已付款
  70. self::STATUS_RETURN => 'Returned',//Yii::t('ctx', 'commonModelsWithdrawStatusNameStatusReturned'), // 已退回
  71. ];
  72. /**
  73. * @inheritdoc
  74. */
  75. public static function tableName() {
  76. return '{{%WITHDRAW}}';
  77. }
  78. /**
  79. * @inheritdoc
  80. */
  81. public function rules() {
  82. return [
  83. [['USER_ID', 'P_MONTH', 'CREATED_AT',], 'required'],
  84. [['WITHDRAW_PERIOD_NUM', 'WITHDRAW_YEAR', 'WITHDRAW_MONTH', 'IS_AUTO_WITHDRAW', 'BANK_PROVINCE', 'BANK_CITY', 'BANK_COUNTY', 'AUDIT_STATUS', 'CREATED_AT', 'UPDATED_AT', 'AUDITED_AT', 'PAID_AT', 'PAID_FAIL_AT', 'BACK_FAIL_AT', 'PAY_TYPE'], 'integer'],
  85. [['AMOUNT','FEES','REAL_AMOUNT'], 'number'],
  86. [['ID', 'SN', 'USER_ID', 'INVOICE_ID', 'OPEN_BANK', 'BANK_NO', 'UPDATE_ADMIN', 'AUDIT_ADMIN'], 'string', 'max' => 32],
  87. [['ID_CARD'], 'string', 'max' => 20],
  88. [['REAL_NAME', 'BANK_ADDRESS'], 'string', 'max' => 255],
  89. [['REMARK', 'UPDATE_REMARK', 'PAID_FAIL_REMARK', 'BACK_REMARK'], 'string', 'max' => 4000],
  90. [['ID'], 'unique'],
  91. [['SN'], 'unique'],
  92. ];
  93. }
  94. /**
  95. * @inheritdoc
  96. */
  97. public function attributeLabels() {
  98. return [
  99. 'ID' => 'ID',
  100. 'SN' => '单号',
  101. 'USER_ID' => '会员ID',
  102. 'ID_CARD' => '身份证号',
  103. 'WITHDRAW_PERIOD_NUM' => '提现期数',
  104. 'WITHDRAW_YEAR' => '提现年份',
  105. 'WITHDRAW_MONTH' => '提现月份',
  106. 'PAY_TYPE' => '付款类型',
  107. 'IS_AUTO_WITHDRAW' => '是否自动提现',
  108. 'AMOUNT' => '提现金额',
  109. 'FEES' => '提现手续费',
  110. 'REAL_AMOUNT' => '实际提现金额',
  111. 'INVOICE_ID' => '发票ID',
  112. 'REAL_NAME' => '会员姓名',
  113. 'OPEN_BANK' => '开户行',
  114. 'BANK_ADDRESS' => '银行地址',
  115. 'BANK_NO' => '银行账号',
  116. 'BANK_PROVINCE' => '银行省份',
  117. 'BANK_CITY' => '银行城市',
  118. 'BANK_COUNTY' => '银行地区',
  119. 'P_MONTH' => '表分区标识',
  120. 'AUDIT_STATUS' => '审核状态',
  121. 'REMARK' => 'Remark', // 备注,
  122. 'UPDATE_ADMIN' => '修改管理员',
  123. 'UPDATE_REMARK' => '修改备注',
  124. 'AUDIT_ADMIN' => '审核管理员',
  125. 'CREATED_AT' => '创建时间',
  126. 'UPDATED_AT' => '修改时间',
  127. 'AUDITED_AT' => '审核时间',
  128. 'PLAN_PAID_AT' => 'Estimated time of payment', // 预计付款时间
  129. 'PAID_AT' => '付款时间',
  130. 'PAID_FAIL_REMARK' => '付款失败备注',
  131. 'PAID_FAIL_AT' => '付款失败时间',
  132. 'BACK_REMARK' => '提现退回备注',
  133. 'BACK_FAIL_AT' => '提现退回时间',
  134. ];
  135. }
  136. /**
  137. * @return \yii\db\ActiveQuery
  138. */
  139. public function getOpenBank() {
  140. return $this->hasOne(OpenBank::class, ['BANK_CODE' => 'BANK_CODE']);
  141. }
  142. /**
  143. * 获取提现相关期数,目前按照直接取当月第一期来实现
  144. * @param $nowTime
  145. * @return array
  146. */
  147. public static function getPeriod($nowTime) {
  148. $period = Period::instance();
  149. $nowPeriodNum = $period->getNowPeriodNum();
  150. $year = $period->getNowYear();
  151. $month = $period->getNowMonth();
  152. $yearMonth = $period->getNowYearMonth();
  153. // $thisMonth = Period::getPeriodNumRangeFromMonth($year, $month);
  154. // $period->setPeriodNum($thisMonth['min']);
  155. $period->setPeriodNum($nowPeriodNum);
  156. $endTime = $period->getNowPeriodEnd();
  157. // return ['nowPeriodNum' => $thisMonth['min'], 'nowYear' => $year, 'nowMonth' => $month, 'yearMonth'=>$yearMonth, 'endTime' => $endTime];
  158. return ['nowPeriodNum' => $nowPeriodNum, 'nowYear' => $year, 'nowMonth' => $month, 'yearMonth'=>$yearMonth, 'endTime' => $endTime];
  159. }
  160. /**
  161. * 是否存在待审核的提现
  162. * @param $userId
  163. * @return bool
  164. */
  165. public static function existWaitAudit($userId) {
  166. $noTwiceWithdraw = [self::STATUS_APPLIED, self::STATUS_WAIT_PAID, self::STATUS_PAID_FALSE];
  167. $noTwiceWithdrawStr = implode("','", $noTwiceWithdraw);
  168. $noTwiceWithdrawStr = "'" . $noTwiceWithdrawStr . "'";
  169. if ($status = static::findOneAsArray('USER_ID=:USER_ID AND AUDIT_STATUS IN (' . $noTwiceWithdrawStr . ')', [':USER_ID' => $userId], 'AUDIT_STATUS')) {
  170. return self::STATUS_NAME[$status['AUDIT_STATUS']];
  171. } else {
  172. return '';
  173. }
  174. }
  175. /**
  176. * 会员本月是否提现
  177. * @param $uid
  178. * @return bool
  179. */
  180. public static function hasThisMonthWithdraw($uid) {
  181. $period = Period::instance();
  182. $idCard = Info::getIdCardByUserId($uid);
  183. if ($idCard) {
  184. if (static::find()->where('ID_CARD=:ID_CARD AND WITHDRAW_YEAR=:WITHDRAW_YEAR AND WITHDRAW_MONTH=:WITHDRAW_MONTH AND AUDIT_STATUS!=:AUDIT_STATUS', [':ID_CARD' => $idCard, ':WITHDRAW_YEAR' => $period->getNowYear(), ':WITHDRAW_MONTH' => $period->getNowMonth(), ':AUDIT_STATUS' => self::STATUS_RETURN])->exists()) {
  185. return true;
  186. }
  187. } else {
  188. if (static::find()->where('USER_ID=:USER_ID AND WITHDRAW_YEAR=:WITHDRAW_YEAR AND WITHDRAW_MONTH=:WITHDRAW_MONTH AND AUDIT_STATUS!=:AUDIT_STATUS', [':USER_ID' => $uid, ':WITHDRAW_YEAR' => $period->getNowYear(), ':WITHDRAW_MONTH' => $period->getNowMonth(), ':AUDIT_STATUS' => self::STATUS_RETURN])->exists()) {
  189. return true;
  190. }
  191. }
  192. return false;
  193. }
  194. /**
  195. * 是否可提现
  196. * @return bool
  197. */
  198. public static function allowWithdraw() {
  199. return true;//@todo 上线时删除
  200. $period = Period::instance();
  201. $year = $period->getNowYear();
  202. $month = $period->getNowMonth();
  203. $thisMonth = Period::getPeriodNumRangeFromMonth($year, $month);
  204. $periodNum = $period->getNowPeriodNum();
  205. if ($periodNum == $thisMonth['min']) {
  206. //上月是否已挂网
  207. $lastMonth = $period->getLastMonth();
  208. $lastPeriodPoint = Period::getPeriodPointFromMonth($lastMonth['year'], $lastMonth['month']);
  209. if ($lastPeriodPoint && $lastPeriodPoint['SENT_AT'] > 0 && Date::nowTime() > $lastPeriodPoint['SENT_AT']) {
  210. return true;
  211. }
  212. }
  213. return false;
  214. }
  215. /**
  216. * 是否到了自动期限的日期
  217. * @return bool
  218. */
  219. public static function allowAutoWithdraw() {
  220. $period = Period::instance();
  221. $year = $period->getNowYear();
  222. $month = $period->getNowMonth();
  223. $thisMonth = Period::getPeriodNumsFromMonth($year, $month);
  224. $periodNum = $period->getNowPeriodNum();
  225. //结算月第一周封期后
  226. if ($periodNum == $thisMonth[1]['PERIOD_NUM']) {
  227. //上月是否已挂网
  228. $lastMonth = $period->getLastMonth();
  229. $lastPeriodPoint = Period::getPeriodPointFromMonth($lastMonth['year'], $lastMonth['month']);
  230. if ($lastPeriodPoint && $lastPeriodPoint['SENT_AT'] > 0 && Date::nowTime() > $lastPeriodPoint['SENT_AT']) {
  231. return true;
  232. }
  233. }
  234. return false;
  235. }
  236. /**
  237. * 获取本月提现金额
  238. * @param $uid
  239. * @return string
  240. */
  241. public static function getThisMonthWithdraw($uid) {
  242. $period = Period::instance();
  243. $withdraw = static::find()->select('SUM(AMOUNT) AS sums')->where('USER_ID=:USER_ID AND WITHDRAW_YEAR=:WITHDRAW_YEAR AND WITHDRAW_MONTH=:WITHDRAW_MONTH', [':USER_ID' => $uid, ':WITHDRAW_YEAR' => $period->getNowYear(), ':WITHDRAW_MONTH' => $period->getNowMonth()])->asArray()->one();
  244. return tool::formatPrice($withdraw['sums'] ? $withdraw['sums'] : 0);
  245. }
  246. /**
  247. * 获取本年提现金额
  248. * @param $uid
  249. * @return string
  250. */
  251. public static function getThisYearWithdraw($uid) {
  252. $period = Period::instance();
  253. $withdraw = static::find()->select('SUM(AMOUNT) AS sums')->where('USER_ID=:USER_ID AND WITHDRAW_YEAR=:WITHDRAW_YEAR', [':USER_ID' => $uid, ':WITHDRAW_YEAR' => $period->getNowYear()])->asArray()->one();
  254. return tool::formatPrice($withdraw['sums'] ? $withdraw['sums'] : 0);
  255. }
  256. /**
  257. * 调整会员的自动提现状态
  258. * @param $userId
  259. * @param int $isAuto
  260. */
  261. public static function setAutoWithdraw($userId, $isAuto = 1) {
  262. UserInfo::updateAll(['IS_AUTO_WITHDRAW' => $isAuto], 'USER_ID=:USER_ID', [':USER_ID' => $userId]);
  263. }
  264. /**
  265. * 银行信息
  266. * @param $row
  267. * @param $baseInfo
  268. * @return array
  269. */
  270. public static function getBankInfo($row, $baseInfo) {
  271. $regionConfig = Cache::getRegionConfig();
  272. $data = ['REAL_NAME' => '', 'OPEN_BANK' => '', 'BANK_PROVINCE' => '', 'BANK_CITY' => '', 'BANK_COUNTY' => '', 'BANK_ADDRESS' => '', 'BANK_NO' => '', 'BANK_UPDATED_AT' => $baseInfo['BANK_UPDATED_AT']];
  273. if ($row['AUDIT_STATUS'] == self::STATUS_PAID) {
  274. $data['REAL_NAME'] = $row['REAL_NAME'];
  275. $data['OPEN_BANK'] = $row['OPEN_BANK'];
  276. $data['BANK_PROVINCE'] = $row['BANK_PROVINCE'];
  277. $data['BANK_CITY'] = $row['BANK_CITY'];
  278. $data['BANK_COUNTY'] = $row['BANK_COUNTY'];
  279. $data['BANK_ADDRESS'] = $row['BANK_ADDRESS'];
  280. $data['BANK_NO'] = $row['BANK_NO'];
  281. } else {
  282. //公司
  283. if ($row['REG_TYPE'] == '758BF69C25C3422AA7743936BC77EE64') {
  284. $companyBank = CompanyBank::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => $row['USER_ID']]);
  285. $data['REAL_NAME'] = $companyBank['OPEN_NAME'];
  286. $data['OPEN_BANK'] = $companyBank['OPEN_BANK'];
  287. $data['BANK_PROVINCE'] = $companyBank['BANK_PROVINCE'];
  288. $data['BANK_CITY'] = $companyBank['BANK_CITY'];
  289. $data['BANK_COUNTY'] = $companyBank['BANK_COUNTY'];
  290. $data['BANK_ADDRESS'] = $companyBank['BANK_ADDRESS'];
  291. $data['BANK_NO'] = $companyBank['BANK_NO'];
  292. $data['BANK_UPDATED_AT'] = $companyBank['UPDATED_AT'];
  293. } else {
  294. $data['REAL_NAME'] = $baseInfo['REAL_NAME'];
  295. $data['OPEN_BANK'] = $baseInfo['OPEN_BANK'];
  296. $data['BANK_PROVINCE'] = $baseInfo['BANK_PROVINCE'];
  297. $data['BANK_CITY'] = $baseInfo['BANK_CITY'];
  298. $data['BANK_COUNTY'] = $baseInfo['BANK_COUNTY'];
  299. $data['BANK_ADDRESS'] = $baseInfo['BANK_ADDRESS'];
  300. $data['BANK_NO'] = $baseInfo['BANK_NO'];
  301. }
  302. }
  303. $data['BANK_PROVINCE_NAME'] = $baseInfo['BANK_PROVINCE'] ? $regionConfig[$baseInfo['BANK_PROVINCE']]['REGION_NAME'] : '';
  304. $data['BANK_CITY_NAME'] = $baseInfo['BANK_CITY'] ? $regionConfig[$baseInfo['BANK_CITY']]['REGION_NAME'] : '';
  305. $data['BANK_COUNTY_NAME'] = $baseInfo['BANK_COUNTY'] ? $regionConfig[$baseInfo['BANK_COUNTY']]['REGION_NAME'] : '';
  306. $data['OPEN_BANK_NAME'] = $baseInfo['OPEN_BANK'] ? OpenBank::getCnName($baseInfo['OPEN_BANK']) : '';
  307. return $data;
  308. }
  309. /**
  310. * 判断提现状态
  311. * @param $sn
  312. * @param $nowStatus
  313. * @param $toStatus
  314. * @return string
  315. */
  316. public static function chkAuditStatus($sn, $nowStatus, $toStatus) {
  317. $statusName = self::STATUS_NAME;
  318. // $msg = '提现单' . $sn . '当前状态为【' . $statusName[$nowStatus] . '】,无法设置为【' . $statusName[$toStatus] . '】';
  319. // $msg = 'The current status of the withdrawal ' . $sn . ' is 【' . $statusName[$nowStatus] . '】, cannot be set to【'. $statusName[$toStatus] . '】';
  320. $msg = Yii::t('ctx', 'withdrawChkAuditStatus', [
  321. 'sn' => $sn,
  322. 'statusName' => $statusName[$nowStatus],
  323. 'newStatusName' => $statusName[$toStatus]
  324. ]);
  325. switch ($toStatus) {
  326. // 取消审核
  327. case Withdraw::STATUS_APPLIED; // 0 待审核 取消审核
  328. if ($nowStatus == Withdraw::STATUS_WAIT_PAID) {
  329. $msg = '';
  330. }
  331. break;
  332. //已审核
  333. // case Withdraw::STATUS_AUDITED: // 2 已审核
  334. // if ($nowStatus == Withdraw::STATUS_APPLIED) {
  335. // $msg = '';
  336. // }
  337. // break;
  338. //待付款
  339. case Withdraw::STATUS_WAIT_PAID: // 3 待复核
  340. if ($nowStatus == Withdraw::STATUS_APPLIED) {
  341. $msg = '';
  342. }
  343. break;
  344. //已付款
  345. case Withdraw::STATUS_PAID: // 6 已付款
  346. if ($nowStatus == Withdraw::STATUS_WAIT_PAID) {
  347. $msg = '';
  348. }
  349. break;
  350. //付款失败
  351. // case Withdraw::STATUS_PAID_FALSE:
  352. // if ($nowStatus == Withdraw::STATUS_PAID) {
  353. // $msg = '';
  354. // }
  355. // break;
  356. //提现退回
  357. case Withdraw::STATUS_RETURN: // 7 已退回
  358. if ($nowStatus == Withdraw::STATUS_APPLIED) {
  359. $msg = '';
  360. }
  361. break;
  362. //审核拒绝
  363. // case Withdraw::STATUS_REFUSED:
  364. // if ($nowStatus == Withdraw::STATUS_APPLIED) {
  365. // $msg = '';
  366. // }
  367. // break;
  368. default:
  369. }
  370. return $msg;
  371. }
  372. /**
  373. * 按提现状态返回总和
  374. * @param $userId
  375. * @param int $type
  376. * @return mixed
  377. */
  378. public static function getWithdrawTotal($userId, $type = self::STATUS_PAID) {
  379. $total = self::find()->where('USER_ID=:USER_ID AND AUDIT_STATUS=:AUDIT_STATUS', [':USER_ID' => $userId, ':AUDIT_STATUS' => $type])->sum('AMOUNT');
  380. return $total ? $total : '0.00';
  381. }
  382. /**
  383. * 按提现状态返回所有会员总和
  384. * @param int $type
  385. * @return mixed
  386. */
  387. public static function getAllWithdrawTotal($type = self::STATUS_PAID) {
  388. $total = self::find()->where('AUDIT_STATUS=:AUDIT_STATUS', [':AUDIT_STATUS' => $type])->sum('AMOUNT');
  389. return $total ? $total : '0.00';
  390. }
  391. // 提现状态
  392. public static function getStatusName() {
  393. return [
  394. self::STATUS_APPLIED => Yii::t('ctx', 'commonModelsWithdrawStatusNameStatusApplied'), // 提现待审核
  395. // self::STATUS_INVOICED => '提现待审核',
  396. self::STATUS_AUDITED => Yii::t('ctx', 'commonModelsWithdrawStatusNameStatusAudited'), // 提现已审核
  397. self::STATUS_WAIT_PAID => Yii::t('ctx', 'commonModelsWithdrawStatusNameStatusWaitPaid'), // 提现待付款
  398. self::STATUS_PAID_FALSE => Yii::t('ctx', 'commonModelsWithdrawStatusNameStatusPaidFalse'), // 付款失败
  399. self::STATUS_REFUSED => Yii::t('ctx', 'commonModelsWithdrawStatusNameStatusRefused'), // 已拒绝
  400. self::STATUS_PAID => Yii::t('ctx', 'commonModelsWithdrawStatusNameStatusPaid'), // 提现已付款
  401. self::STATUS_RETURN => Yii::t('ctx', 'commonModelsWithdrawStatusNameStatusReturned'), // 已退回
  402. ];
  403. }
  404. }