Recharge.php 9.8 KB

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