Recharge.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 $CURRENCY 货币(尼日利亚默认NGN)
  16. * @property string $AMOUNT 充值金额
  17. * @property string $RECHARGE_ORDER_ID PS订单号
  18. * @property int $RECHARGE_STATUS 充值状态
  19. * @property string $REAL_NAME 会员姓名
  20. * @property string $OPEN_BANK 开户行
  21. * @property string $BANK_ADDRESS 开户支行
  22. * @property string $BANK_NO 银行账号
  23. * @property int $BANK_PROVINCE 银行省份
  24. * @property int $BANK_CITY 银行城市
  25. * @property int $BANK_COUNTY 银行地区
  26. * @property string $BANK_PROVE 打款凭证
  27. * @property string $P_MONTH 表分区标识
  28. * @property int $AUDIT_STATUS 审核状态
  29. * @property string $REMARK 审核备注
  30. * @property string $AUDIT_ADMIN 审核管理员
  31. * @property int $CREATED_AT 创建时间
  32. * @property int $AUDITED_AT 审核时间
  33. */
  34. class Recharge extends \common\components\ActiveRecord {
  35. const TYPE_MANUAL = 0; // 手动充值
  36. const TYPE_AUTO = 1; // 自动充值
  37. const TYPE_ADMIN = 2; // 后台管理员
  38. const STATUS_APPLIED = 0; // 已申请
  39. const STATUS_PROVED = 1; // 已上传凭证
  40. const STATUS_AUDITED = 2; // 已审核
  41. const STATUS_REFUSED = 3; // 已拒绝
  42. const STATUS_NEW = 0;
  43. const STATUS_PROCESSING = 1;
  44. const STATUS_SUCCESS = 2;
  45. const STATUS_REJECT = 3;
  46. const STATUS_NAME = [
  47. self::STATUS_APPLIED => '待审核',
  48. self::STATUS_PROVED => '已上传凭证',
  49. self::STATUS_AUDITED => '已审核',
  50. self::STATUS_REFUSED => '已拒绝',
  51. ];
  52. const RECHARGE_STATUS_NAME = [
  53. self::STATUS_NEW => 'NEW',
  54. self::STATUS_PROCESSING => 'PROCESSING',
  55. self::STATUS_SUCCESS => 'SUCCESS',
  56. self::STATUS_REJECT => 'FAILED',
  57. ];
  58. /**
  59. * @inheritdoc
  60. */
  61. public static function tableName() {
  62. return '{{%RECHARGE}}';
  63. }
  64. /**
  65. * @inheritdoc
  66. */
  67. public function rules() {
  68. return [
  69. [['USER_ID', 'P_MONTH', 'CREATED_AT'], 'required'],
  70. [['RECHARGE_PERIOD_NUM', 'RECHARGE_YEAR', 'RECHARGE_MONTH', 'AUDIT_STATUS', 'CREATED_AT', 'AUDITED_AT', 'BANK_PROVINCE', 'BANK_CITY', 'BANK_COUNTY'], 'integer'],
  71. [['AMOUNT'], 'number'],
  72. [['ID', 'SN', 'USER_ID', 'OPEN_BANK', 'BANK_NO', 'AUDIT_ADMIN'], 'string', 'max' => 32],
  73. [['ID_CARD'], 'string', 'max' => 20],
  74. [['REAL_NAME', 'BANK_ADDRESS'], 'string', 'max' => 255],
  75. [['REMARK'], 'string', 'max' => 4000],
  76. [['ID'], 'unique'],
  77. [['SN'], 'unique'],
  78. ];
  79. }
  80. /**
  81. * @inheritdoc
  82. */
  83. public function attributeLabels() {
  84. return [
  85. 'ID' => 'ID',
  86. 'SN' => '单号',
  87. 'USER_ID' => '会员ID',
  88. 'ID_CARD' => '身份证号',
  89. 'RECHARGE_PERIOD_NUM' => '充值期数',
  90. 'RECHARGE_YEAR' => '充值年份',
  91. 'RECHARGE_MONTH' => '充值月份',
  92. 'CURRENCY' => '货币',
  93. 'AMOUNT' => '充值金额',
  94. 'REAL_NAME' => '会员姓名',
  95. 'OPEN_BANK' => '汇款银行',
  96. 'BANK_ADDRESS' => '开户支行',
  97. 'BANK_NO' => '汇款账号',
  98. 'BANK_PROVINCE' => '银行省份',
  99. 'BANK_CITY' => '银行城市',
  100. 'BANK_COUNTY' => '银行地区',
  101. 'BANK_PROVE' => '打款凭证',
  102. 'P_MONTH' => '表分区标识',
  103. 'AUDIT_STATUS' => '审核状态',
  104. 'REMARK' => '备注',
  105. 'AUDIT_ADMIN' => '审核管理员',
  106. 'CREATED_AT' => '创建时间',
  107. 'AUDITED_AT' => '审核时间',
  108. ];
  109. }
  110. /**
  111. * 获取充值相关期数,目前按照直接取当月第一期来实现
  112. * @param $nowTime
  113. * @return array
  114. */
  115. public static function getPeriod($nowTime) {
  116. $period = Period::instance();
  117. $year = $period->getNowYear();
  118. $month = $period->getNowMonth();
  119. $yearMonth = $period->getNowYearMonth();
  120. $thisMonth = Period::getPeriodNumRangeFromMonth($year, $month);
  121. $period->setPeriodNum($thisMonth['min']);
  122. $endTime = $period->getNowPeriodEnd();
  123. return ['nowPeriodNum' => $thisMonth['min'], 'nowYear' => $year, 'nowMonth' => $month, 'yearMonth'=>$yearMonth, 'endTime' => $endTime];
  124. }
  125. /**
  126. * 会员本月是否充值
  127. * @param $uid
  128. * @return bool
  129. */
  130. public static function hasThisMonthRecharge($uid) {
  131. $period = Period::instance();
  132. $idCard = Info::getIdCardByUserId($uid);
  133. if ($idCard) {
  134. 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()) {
  135. return true;
  136. }
  137. } else {
  138. 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()) {
  139. return true;
  140. }
  141. }
  142. return false;
  143. }
  144. /**
  145. * 判断充值状态
  146. * @param $sn
  147. * @param $nowStatus
  148. * @param $toStatus
  149. * @return string
  150. */
  151. public static function chkAuditStatus($sn, $nowStatus, $toStatus) {
  152. $statusName = self::STATUS_NAME;
  153. $msg = '充值单' . $sn . '当前状态为【' . $statusName[$nowStatus] . '】,无法设置为【' . $statusName[$toStatus] . '】';
  154. switch ($toStatus) {
  155. //已审核
  156. case Recharge::STATUS_AUDITED:
  157. if (($nowStatus == Recharge::STATUS_APPLIED) || ($nowStatus == Recharge::STATUS_PROVED)) {
  158. $msg = '';
  159. }
  160. break;
  161. //审核拒绝
  162. case Recharge::STATUS_REFUSED:
  163. if (($nowStatus == Recharge::STATUS_APPLIED) || ($nowStatus == Recharge::STATUS_PROVED)) {
  164. $msg = '';
  165. }
  166. break;
  167. default:
  168. }
  169. return $msg;
  170. }
  171. /**
  172. * 按充值状态返回总和
  173. * @param $userId
  174. * @param int $type
  175. * @return mixed
  176. */
  177. public static function getRechargeTotal($userId, $type = self::STATUS_AUDITED) {
  178. $total = self::find()->where('USER_ID=:USER_ID AND AUDIT_STATUS=:AUDIT_STATUS', [':USER_ID' => $userId, ':AUDIT_STATUS' => $type])->sum('AMOUNT');
  179. return $total ? $total : '0.00';
  180. }
  181. /**
  182. * 按充值状态返回所有会员总和
  183. * @param int $type
  184. * @return mixed
  185. */
  186. public static function getAllRechargeTotal($type = self::STATUS_AUDITED) {
  187. $total = self::find()->where('AUDIT_STATUS=:AUDIT_STATUS', [':AUDIT_STATUS' => $type])->sum('AMOUNT');
  188. return $total ? $total : '0.00';
  189. }
  190. /**
  191. *
  192. */
  193. public static function sendToPaystack(){
  194. $url = "https://api.paystack.co/transaction/initialize";
  195. $am = random_int(100, 4000);
  196. $fields = [
  197. 'email' => "fl@bd.com",
  198. 'amount' => $am
  199. ];
  200. $fields_string = http_build_query($fields);
  201. //open connection
  202. $ch = curl_init();
  203. $sk = 'sk_test_1d16378c8be9efabc71b05754190b9c3f6999402'; //taisino
  204. //$sk = 'sk_test_5ece72377432376f5cf6bb5c468395a650220309'; //elken
  205. //set the url, number of POST vars, POST data
  206. curl_setopt($ch,CURLOPT_URL, $url);
  207. curl_setopt($ch,CURLOPT_POST, true);
  208. curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
  209. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  210. "Authorization: Bearer $sk",
  211. "Cache-Control: no-cache",
  212. ));
  213. //So that curl_exec returns the contents of the cURL; rather than echoing it
  214. curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
  215. //execute post
  216. //$result = curl_exec($ch);
  217. echo $result;
  218. }
  219. }