Recharge.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace common\models;
  3. use common\helpers\user\Info;
  4. use Yii;
  5. /**
  6. * This is the model class for table "{{%RECHARGE}}".
  7. *
  8. * @property string $ID
  9. * @property string $SN 单号
  10. * @property string $USER_ID 会员ID
  11. * @property string $ID_CARD 身份证号
  12. * @property int $RECHARGE_PERIOD_NUM 充值期数
  13. * @property int $RECHARGE_YEAR 充值年份
  14. * @property int $RECHARGE_MONTH 充值月份
  15. * @property string $AMOUNT 充值金额
  16. * @property string $REAL_NAME 会员姓名
  17. * @property string $OPEN_BANK 开户行
  18. * @property string $BANK_ADDRESS 开户支行
  19. * @property string $BANK_NO 银行账号
  20. * @property int $BANK_PROVINCE 银行省份
  21. * @property int $BANK_CITY 银行城市
  22. * @property int $BANK_COUNTY 银行地区
  23. * @property string $BANK_PROVE 打款凭证
  24. * @property string $P_MONTH 表分区标识
  25. * @property int $AUDIT_STATUS 审核状态
  26. * @property string $REMARK 审核备注
  27. * @property string $AUDIT_ADMIN 审核管理员
  28. * @property int $CREATED_AT 创建时间
  29. * @property int $AUDITED_AT 审核时间
  30. */
  31. class Recharge extends \common\components\ActiveRecord {
  32. const TYPE_MANUAL = 0; // 手动充值
  33. const TYPE_AUTO = 1; // 自动充值
  34. const TYPE_ADMIN = 2; // 后台管理员
  35. const STATUS_APPLIED = 0; // 已申请
  36. const STATUS_PROVED = 1; // 已上传凭证
  37. const STATUS_AUDITED = 2; // 已审核
  38. const STATUS_REFUSED = 3; // 已拒绝
  39. const STATUS_NAME = [
  40. self::STATUS_APPLIED => '待审核',
  41. self::STATUS_PROVED => '已上传凭证',
  42. self::STATUS_AUDITED => '已审核',
  43. self::STATUS_REFUSED => '已拒绝',
  44. ];
  45. /**
  46. * @inheritdoc
  47. */
  48. public static function tableName() {
  49. return '{{%RECHARGE}}';
  50. }
  51. /**
  52. * @inheritdoc
  53. */
  54. public function rules() {
  55. return [
  56. [['USER_ID', 'P_MONTH', 'CREATED_AT'], 'required'],
  57. [['RECHARGE_PERIOD_NUM', 'RECHARGE_YEAR', 'RECHARGE_MONTH', 'AUDIT_STATUS', 'CREATED_AT', 'AUDITED_AT', 'BANK_PROVINCE', 'BANK_CITY', 'BANK_COUNTY'], 'integer'],
  58. [['AMOUNT'], 'number'],
  59. [['ID', 'SN', 'USER_ID', 'OPEN_BANK', 'BANK_NO', 'AUDIT_ADMIN'], 'string', 'max' => 32],
  60. [['ID_CARD'], 'string', 'max' => 20],
  61. [['REAL_NAME', 'BANK_ADDRESS'], 'string', 'max' => 255],
  62. [['REMARK'], 'string', 'max' => 4000],
  63. [['ID'], 'unique'],
  64. [['SN'], 'unique'],
  65. ];
  66. }
  67. /**
  68. * @inheritdoc
  69. */
  70. public function attributeLabels() {
  71. return [
  72. 'ID' => 'ID',
  73. 'SN' => '单号',
  74. 'USER_ID' => '会员ID',
  75. 'ID_CARD' => '身份证号',
  76. 'RECHARGE_PERIOD_NUM' => '充值期数',
  77. 'RECHARGE_YEAR' => '充值年份',
  78. 'RECHARGE_MONTH' => '充值月份',
  79. 'AMOUNT' => '充值金额',
  80. 'REAL_NAME' => '会员姓名',
  81. 'OPEN_BANK' => '汇款银行',
  82. 'BANK_ADDRESS' => '开户支行',
  83. 'BANK_NO' => '汇款账号',
  84. 'BANK_PROVINCE' => '银行省份',
  85. 'BANK_CITY' => '银行城市',
  86. 'BANK_COUNTY' => '银行地区',
  87. 'BANK_PROVE' => '打款凭证',
  88. 'P_MONTH' => '表分区标识',
  89. 'AUDIT_STATUS' => '审核状态',
  90. 'REMARK' => '备注',
  91. 'AUDIT_ADMIN' => '审核管理员',
  92. 'CREATED_AT' => '创建时间',
  93. 'AUDITED_AT' => '审核时间',
  94. ];
  95. }
  96. /**
  97. * 获取充值相关期数,目前按照直接取当月第一期来实现
  98. * @param $nowTime
  99. * @return array
  100. */
  101. public static function getPeriod($nowTime) {
  102. $period = Period::instance();
  103. $year = $period->getNowYear();
  104. $month = $period->getNowMonth();
  105. $yearMonth = $period->getNowYearMonth();
  106. $thisMonth = Period::getPeriodNumRangeFromMonth($year, $month);
  107. $period->setPeriodNum($thisMonth['min']);
  108. $endTime = $period->getNowPeriodEnd();
  109. return ['nowPeriodNum' => $thisMonth['min'], 'nowYear' => $year, 'nowMonth' => $month, 'yearMonth'=>$yearMonth, 'endTime' => $endTime];
  110. }
  111. /**
  112. * 会员本月是否充值
  113. * @param $uid
  114. * @return bool
  115. */
  116. public static function hasThisMonthRecharge($uid) {
  117. $period = Period::instance();
  118. $idCard = Info::getIdCardByUserId($uid);
  119. if ($idCard) {
  120. if (static::find()->where('ID_CARD=:ID_CARD AND RECHARGE_YEAR=:RECHARGE_YEAR AND RECHARGE_MONTH=:RECHARGE_MONTH', [':ID_CARD' => $idCard, ':RECHARGE_YEAR' => $period->getNowYear(), ':RECHARGE_MONTH' => $period->getNowMonth()])->exists()) {
  121. return true;
  122. }
  123. } else {
  124. if (static::find()->where('USER_ID=:USER_ID AND RECHARGE_YEAR=:RECHARGE_YEAR AND RECHARGE_MONTH=:RECHARGE_MONTH', [':USER_ID' => $uid, ':RECHARGE_YEAR' => $period->getNowYear(), ':RECHARGE_MONTH' => $period->getNowMonth()])->exists()) {
  125. return true;
  126. }
  127. }
  128. return false;
  129. }
  130. /**
  131. * 判断充值状态
  132. * @param $sn
  133. * @param $nowStatus
  134. * @param $toStatus
  135. * @return string
  136. */
  137. public static function chkAuditStatus($sn, $nowStatus, $toStatus) {
  138. $statusName = self::STATUS_NAME;
  139. $msg = '充值单' . $sn . '当前状态为【' . $statusName[$nowStatus] . '】,无法设置为【' . $statusName[$toStatus] . '】';
  140. switch ($toStatus) {
  141. //已审核
  142. case Recharge::STATUS_AUDITED:
  143. if (($nowStatus == Recharge::STATUS_APPLIED) || ($nowStatus == Recharge::STATUS_PROVED)) {
  144. $msg = '';
  145. }
  146. break;
  147. //审核拒绝
  148. case Recharge::STATUS_REFUSED:
  149. if (($nowStatus == Recharge::STATUS_APPLIED) || ($nowStatus == Recharge::STATUS_PROVED)) {
  150. $msg = '';
  151. }
  152. break;
  153. default:
  154. }
  155. return $msg;
  156. }
  157. /**
  158. * 按充值状态返回总和
  159. * @param $userId
  160. * @param int $type
  161. * @return mixed
  162. */
  163. public static function getRechargeTotal($userId, $type = self::STATUS_AUDITED) {
  164. $total = self::find()->where('USER_ID=:USER_ID AND AUDIT_STATUS=:AUDIT_STATUS', [':USER_ID' => $userId, ':AUDIT_STATUS' => $type])->sum('AMOUNT');
  165. return $total ? $total : '0.00';
  166. }
  167. /**
  168. * 按充值状态返回所有会员总和
  169. * @param int $type
  170. * @return mixed
  171. */
  172. public static function getAllRechargeTotal($type = self::STATUS_AUDITED) {
  173. $total = self::find()->where('AUDIT_STATUS=:AUDIT_STATUS', [':AUDIT_STATUS' => $type])->sum('AMOUNT');
  174. return $total ? $total : '0.00';
  175. }
  176. }