Recharge.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 => 'To be reviewed', // 待审核
  48. self::STATUS_PROVED => 'Voucher uploaded', // 已上传凭证
  49. self::STATUS_AUDITED => 'Approved', // 已审核
  50. self::STATUS_REFUSED => 'Rejected', // 已拒绝
  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. const STATUS_NAME_LANGUAGE_PREFIX = 'rechargeStatusTitle:';
  59. const RECHARGE_STATUS_NAME_LANGUAGE_PREFIX = 'rechargeStatus:';
  60. /**
  61. * @inheritdoc
  62. */
  63. public static function tableName() {
  64. return '{{%RECHARGE}}';
  65. }
  66. /**
  67. * @inheritdoc
  68. */
  69. public function rules() {
  70. return [
  71. [['USER_ID', 'P_MONTH', 'CREATED_AT'], 'required'],
  72. [['RECHARGE_PERIOD_NUM', 'RECHARGE_YEAR', 'RECHARGE_MONTH', 'AUDIT_STATUS', 'CREATED_AT', 'AUDITED_AT', 'BANK_PROVINCE', 'BANK_CITY', 'BANK_COUNTY'], 'integer'],
  73. [['AMOUNT'], 'number'],
  74. [['ID', 'SN', 'USER_ID', 'OPEN_BANK', 'BANK_NO', 'AUDIT_ADMIN'], 'string', 'max' => 32],
  75. [['ID_CARD'], 'string', 'max' => 20],
  76. [['REAL_NAME', 'BANK_ADDRESS'], 'string', 'max' => 255],
  77. [['REMARK'], 'string', 'max' => 4000],
  78. [['ID'], 'unique'],
  79. [['SN'], 'unique'],
  80. ];
  81. }
  82. /**
  83. * @inheritdoc
  84. */
  85. public function attributeLabels() {
  86. return [
  87. 'ID' => 'ID',
  88. 'SN' => '单号',
  89. 'USER_ID' => '会员ID',
  90. 'ID_CARD' => '身份证号',
  91. 'RECHARGE_PERIOD_NUM' => '充值期数',
  92. 'RECHARGE_YEAR' => '充值年份',
  93. 'RECHARGE_MONTH' => '充值月份',
  94. 'CURRENCY' => '货币',
  95. 'AMOUNT' => '充值金额',
  96. 'REAL_NAME' => '会员姓名',
  97. 'OPEN_BANK' => '汇款银行',
  98. 'BANK_ADDRESS' => '开户支行',
  99. 'BANK_NO' => '汇款账号',
  100. 'BANK_PROVINCE' => '银行省份',
  101. 'BANK_CITY' => '银行城市',
  102. 'BANK_COUNTY' => '银行地区',
  103. 'BANK_PROVE' => '打款凭证',
  104. 'P_MONTH' => '表分区标识',
  105. 'AUDIT_STATUS' => '审核状态',
  106. 'REMARK' => '备注',
  107. 'AUDIT_ADMIN' => '审核管理员',
  108. 'CREATED_AT' => '创建时间',
  109. 'AUDITED_AT' => '审核时间',
  110. ];
  111. }
  112. public static function getStatusName()
  113. {
  114. return array_map(fn($index): string => \Yii::t('ctx', self::STATUS_NAME_LANGUAGE_PREFIX . $index), array_keys(self::STATUS_NAME));
  115. }
  116. public static function getRechargeStatus()
  117. {
  118. return array_map(fn($index): string => \Yii::t('ctx', self::RECHARGE_STATUS_NAME_LANGUAGE_PREFIX . $index), array_keys(self::RECHARGE_STATUS_NAME));
  119. }
  120. /**
  121. * 获取充值相关期数,目前按照直接取当月第一期来实现
  122. * @param $nowTime
  123. * @return array
  124. */
  125. public static function getPeriod($nowTime) {
  126. $period = Period::instance();
  127. $year = $period->getNowYear();
  128. $month = $period->getNowMonth();
  129. $yearMonth = $period->getNowYearMonth();
  130. $thisMonth = Period::getPeriodNumRangeFromMonth($year, $month);
  131. $period->setPeriodNum($thisMonth['min']);
  132. $endTime = $period->getNowPeriodEnd();
  133. return ['nowPeriodNum' => $thisMonth['min'], 'nowYear' => $year, 'nowMonth' => $month, 'yearMonth'=>$yearMonth, 'endTime' => $endTime];
  134. }
  135. /**
  136. * 会员本月是否充值
  137. * @param $uid
  138. * @return bool
  139. */
  140. public static function hasThisMonthRecharge($uid) {
  141. $period = Period::instance();
  142. $idCard = Info::getIdCardByUserId($uid);
  143. if ($idCard) {
  144. 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()) {
  145. return true;
  146. }
  147. } else {
  148. 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()) {
  149. return true;
  150. }
  151. }
  152. return false;
  153. }
  154. /**
  155. * 判断充值状态
  156. * @param $sn
  157. * @param $nowStatus
  158. * @param $toStatus
  159. * @return string
  160. */
  161. public static function chkAuditStatus($sn, $nowStatus, $toStatus) {
  162. $statusName = self::getStatusName();
  163. // $msg = '充值单' . $sn . '当前状态为【' . $statusName[$nowStatus] . '】,无法设置为【' . $statusName[$toStatus] . '】';
  164. $msg = 'The current status of the recharge ' . $sn . ' is 【' . $statusName[$nowStatus] . '】, cannot be set to 【' . $statusName[$toStatus] . '】';
  165. switch ($toStatus) {
  166. //已审核
  167. case Recharge::STATUS_AUDITED:
  168. if (($nowStatus == Recharge::STATUS_APPLIED) || ($nowStatus == Recharge::STATUS_PROVED)) {
  169. $msg = '';
  170. }
  171. break;
  172. //审核拒绝
  173. case Recharge::STATUS_REFUSED:
  174. if (($nowStatus == Recharge::STATUS_APPLIED) || ($nowStatus == Recharge::STATUS_PROVED)) {
  175. $msg = '';
  176. }
  177. break;
  178. default:
  179. }
  180. return $msg;
  181. }
  182. /**
  183. * 按充值状态返回总和
  184. * @param $userId
  185. * @param int $type
  186. * @return mixed
  187. */
  188. public static function getRechargeTotal($userId, $type = self::STATUS_AUDITED) {
  189. $total = self::find()->where('USER_ID=:USER_ID AND AUDIT_STATUS=:AUDIT_STATUS', [':USER_ID' => $userId, ':AUDIT_STATUS' => $type])->sum('AMOUNT');
  190. return $total ? $total : '0.00';
  191. }
  192. /**
  193. * 按充值状态返回所有会员总和
  194. * @param int $type
  195. * @return mixed
  196. */
  197. public static function getAllRechargeTotal($type = self::STATUS_AUDITED) {
  198. $total = self::find()->where('AUDIT_STATUS=:AUDIT_STATUS', [':AUDIT_STATUS' => $type])->sum('AMOUNT');
  199. return $total ? $total : '0.00';
  200. }
  201. /**
  202. *
  203. */
  204. public static function sendToPaystack(){
  205. $url = "https://api.paystack.co/transaction/initialize";
  206. $am = random_int(100, 4000);
  207. $fields = [
  208. 'email' => "fl@bd.com",
  209. 'amount' => $am
  210. ];
  211. $fields_string = http_build_query($fields);
  212. //open connection
  213. $ch = curl_init();
  214. $sk = 'sk_test_1d16378c8be9efabc71b05754190b9c3f6999402'; //taisino
  215. //$sk = 'sk_test_5ece72377432376f5cf6bb5c468395a650220309'; //elken
  216. //set the url, number of POST vars, POST data
  217. curl_setopt($ch,CURLOPT_URL, $url);
  218. curl_setopt($ch,CURLOPT_POST, true);
  219. curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
  220. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  221. "Authorization: Bearer $sk",
  222. "Cache-Control: no-cache",
  223. ));
  224. //So that curl_exec returns the contents of the cURL; rather than echoing it
  225. curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
  226. //execute post
  227. $result = curl_exec($ch);
  228. echo $result;
  229. }
  230. // 多语言状态名
  231. // public static function getStatusName() {
  232. // return [
  233. // self::STATUS_APPLIED => \Yii::t('ctx', 'financeRechargeListStatusTobeReviewed'), // 待审核
  234. // self::STATUS_PROVED => \Yii::t('ctx', 'financeRechargeListStatusVoucherUploaded'), // 已上传凭证
  235. // self::STATUS_AUDITED => \Yii::t('ctx', 'financeRechargeListStatusApproved'), // 已审核
  236. // self::STATUS_REFUSED => \Yii::t('ctx', 'financeRechargeListStatusRejected'), // 已拒绝
  237. // ];
  238. // }
  239. }