RechargeForm.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. namespace common\models\forms;
  3. use common\components\Model;
  4. use common\helpers\Cache;
  5. use common\helpers\Date;
  6. use common\helpers\Form;
  7. use common\helpers\Tool;
  8. use common\helpers\user\Cash;
  9. use common\helpers\user\Info;
  10. use common\models\Recharge;
  11. use common\models\UserInfo;
  12. use yii\base\Exception;
  13. /**
  14. * Login form
  15. */
  16. class RechargeForm extends Model {
  17. public $selectedIds;
  18. public $auditStatus;
  19. public $createRemark;
  20. public $userName;
  21. public $applyAmount;
  22. public $openBank;
  23. public $bankAddress;
  24. public $bankNo;
  25. // public $bankProvince;
  26. // public $bankCity;
  27. // public $bankCounty;
  28. public $sn;
  29. public $amount;
  30. public $bankRealName;
  31. private $_userId;
  32. /**
  33. * @inheritdoc
  34. */
  35. public function rules() {
  36. return [
  37. [['userName', 'idCard', 'applyAmount', 'auditStatus', 'remark', 'selectedIds', 'sn','amount', 'bankRealName', 'bankNo'], 'trim'],
  38. [['selectedIds', 'auditStatus', 'userName','applyAmount','openBank','bankNo'/*,'bankAddress'*/], 'required'],
  39. [['selectedIds'], 'exist', 'targetClass' => Recharge::class, 'targetAttribute' => 'ID', 'message' => 'Recharge does not exist'], // 充值申请不存在
  40. [['userName'], 'exist', 'targetClass' => UserInfo::class, 'targetAttribute' => 'USER_NAME', 'message' => 'Member does not exist'],// 会员不存在
  41. [['applyAmount'], 'price'],
  42. [['applyAmount'], 'isApplyAmount'],
  43. [['applyAmount'], 'number', 'max' => 10000000000, 'min' => 1],
  44. [['selectedIds'], 'isSelected'],
  45. //[['sn'], 'isSn'],
  46. ];
  47. }
  48. /**
  49. * 指定场景
  50. * @return array
  51. */
  52. public function scenarios() {
  53. $parentScenarios = parent::scenarios();
  54. $customScenarios = [
  55. 'addByAdmin' => ['userName', 'applyAmount'],
  56. 'addByUser' => ['applyAmount','openBank','bankNo','bankAddress'/*,'currency'*/],
  57. 'statusByAdmin' => ['selectedIds', 'auditStatus', 'createRemark'],
  58. ];
  59. return array_merge($parentScenarios, $customScenarios);
  60. }
  61. /**
  62. * @return array
  63. */
  64. public function attributeLabels() {
  65. return [
  66. 'selectedIds' => 'RechargeID',// 充值申请ID
  67. 'userName' => 'Member code',// 会员编号
  68. 'applyAmount' => 'Apply Amount', // 申请充值的金额
  69. 'openBank' => 'Bank name',// 汇款银行
  70. 'bankNo' => 'Bank account', // 汇款账号
  71. // 'bankAddress' => '银行支行',
  72. // 'bankProvince' => '银行省',
  73. // 'bankCity' => '银行市',
  74. // 'bankCounty' => '银行县',
  75. ];
  76. }
  77. /**
  78. * 校验申请金额是否小于当前余额并符合配置中的设置
  79. * @param $attribute
  80. * @return bool
  81. */
  82. public function isApplyAmount($attribute) {
  83. if ($this->applyAmount <= 0) {
  84. $this->addError('scenario', 'The recharge amount must be greater than 0'); // 充值金额必须大于0
  85. }
  86. if ((int)($this->applyAmount)!=$this->applyAmount) {
  87. $this->addError('scenario', 'The recharge amount must be an integer');// 充值金额必须是整数
  88. }
  89. if ($this->scenario == 'addByUser') {
  90. $this->_userId = \Yii::$app->user->id;
  91. } elseif ($this->scenario == 'addByAdmin') {
  92. $userInfo = UserInfo::findOneAsArray(['USER_NAME' => $this->userName]);
  93. if (!$userInfo) {
  94. $this->addError('scenario', $this->userName . 'Member does not exist');// 会员不存在
  95. }else{
  96. $this->_userId = $userInfo['USER_ID'];
  97. }
  98. } else {
  99. $this->addError($attribute, 'Scenes does not exist'); // 场景不存在
  100. return false;
  101. }
  102. }
  103. /**
  104. * 批量数据
  105. * @param $attributes
  106. */
  107. public function isSelected($attributes) {
  108. if (!$this->selectedIds) {
  109. $this->addError($attributes, 'A piece of data must be selected'); // 必须选择一条数据
  110. }
  111. // if (!is_array($this->selectedIds)) {
  112. // $this->selectedIds = [$this->selectedIds];
  113. // }
  114. }
  115. /**
  116. * 判断信息是否一致
  117. * @param $attribute
  118. * @throws Exception
  119. */
  120. public function isSn($attribute) {
  121. $sn = $this->sn;
  122. if (!$oneWithdraw = Recharge::findOneAsArray('SN=:SN AND AUDIT_STATUS=:AUDIT_STATUS', [':SN' => $sn, ':AUDIT_STATUS' => Recharge::STATUS_AUDITED])) {
  123. $this->addError($attribute, '不存在充值流水号为' . $sn . '的已审核记录');
  124. }
  125. if ($oneWithdraw) {
  126. $info = Info::baseInfo($oneWithdraw['USER_ID']);
  127. if ($this->userName != $info['USER_NAME']) {
  128. $this->addError($attribute, 'Excel中充值流水号' . $sn . '的用户名' . $this->userName . '与系统中的信息【' . $info['USER_NAME'] . '】不一致');
  129. }
  130. if ($this->amount != $oneWithdraw['AMOUNT']) {
  131. $this->addError($attribute, 'Excel中充值流水号' . $sn . '的充值金额' . $this->amount . '与系统中的信息【' . $oneWithdraw['AMOUNT'] . '】不一致');
  132. }
  133. if ($this->bankRealName != $oneWithdraw['REAL_NAME']) {
  134. $this->addError($attribute, 'Excel中充值流水号' . $sn . '的实时开户名' . $this->bankRealName . '与系统中的信息【' . $oneWithdraw['REAL_NAME'] . '】不一致');
  135. }
  136. if ($this->bankNo != $oneWithdraw['BANK_NO']) {
  137. $this->addError($attribute, 'Excel中充值流水号' . $sn . '的实时银行账户' . $this->bankNo . '与系统中的信息【' . $oneWithdraw['BANK_NO'] . '】不一致');
  138. }
  139. }
  140. }
  141. /**
  142. * 管理员充值
  143. * @return null|string
  144. * @throws \yii\db\Exception
  145. */
  146. public function recharge() {
  147. if (!$this->validate()) {
  148. return false;
  149. }
  150. $db = \Yii::$app->db;
  151. $transaction = $db->beginTransaction();
  152. try {
  153. Cash::changeUserCash($this->_userId, 'CASH', $this->applyAmount, ['REMARK' => 'Background administrator recharge']);
  154. $transaction->commit();
  155. } catch (Exception $e) {
  156. $transaction->rollBack();
  157. $this->addError('add', $e->getMessage());
  158. return false;
  159. }
  160. return true;
  161. }
  162. /**
  163. * 添加充值申请
  164. * @return null|string
  165. * @throws \yii\db\Exception
  166. */
  167. public function add() {
  168. if (!$this->validate()) {
  169. return false;
  170. }
  171. $db = \Yii::$app->db;
  172. $transaction = $db->beginTransaction();
  173. try {
  174. $nowTime = Date::nowTime();
  175. $period = Recharge::getPeriod($nowTime);
  176. //增加记录
  177. $userInfo = Info::baseInfo($this->_userId);
  178. $rechargeModel = new Recharge();
  179. $rechargeModel->SN = $this->_generateSn();
  180. $rechargeModel->USER_ID = $this->_userId;
  181. $rechargeModel->REAL_NAME = Info::getUserRealNameByUserId($this->_userId);
  182. $rechargeModel->ID_CARD = $userInfo['ID_CARD'];
  183. $rechargeModel->RECHARGE_PERIOD_NUM = $period['nowPeriodNum'];
  184. $rechargeModel->RECHARGE_YEAR = $period['nowYear'];
  185. $rechargeModel->RECHARGE_MONTH = $period['nowMonth'];
  186. $rechargeModel->AMOUNT = $this->applyAmount;
  187. $rechargeModel->OPEN_BANK = $this->openBank;
  188. $rechargeModel->BANK_ADDRESS = $this->bankAddress;
  189. $rechargeModel->BANK_NO = $this->bankNo;
  190. // $rechargeModel->BANK_PROVINCE = $this->bankProvince ?? 0;
  191. // $rechargeModel->BANK_CITY = $this->bankCity ?? 0;
  192. // $rechargeModel->BANK_COUNTY = $this->bankCounty ?? 0;
  193. $rechargeModel->P_MONTH = Date::ociToDate($period['yearMonth'], Date::OCI_TIME_FORMAT_SHORT_MONTH);
  194. $rechargeModel->AUDIT_STATUS = Recharge::STATUS_APPLIED;
  195. $rechargeModel->CREATED_AT = $nowTime;
  196. if (!$rechargeModel->save()) {
  197. throw new Exception(Form::formatErrorsForApi($rechargeModel->getErrors()));
  198. }
  199. $transaction->commit();
  200. } catch (Exception $e) {
  201. $transaction->rollBack();
  202. $this->addError('add', $e->getMessage());
  203. return false;
  204. }
  205. return $rechargeModel;
  206. }
  207. /**
  208. * 生成流水号
  209. * @return string
  210. */
  211. private function _generateSn() {
  212. return 'CZ' . Date::today('Ymd') . $this->_random(10, 1);
  213. }
  214. /**
  215. * 生成随机数
  216. *
  217. * @param $length
  218. * @param int $numeric
  219. * @return string
  220. */
  221. private function _random($length, $numeric = 0) {
  222. $seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
  223. $seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
  224. $hash = '';
  225. $max = strlen($seed) - 1;
  226. for ($i = 0; $i < $length; $i++) {
  227. $hash .= $seed[mt_rand(0, $max)];
  228. }
  229. return $hash;
  230. }
  231. /**
  232. * 设置充值订单的状态
  233. * @return null|static
  234. * @throws \yii\db\Exception
  235. */
  236. public function changeStatus() {
  237. if (!$this->validate()) {
  238. return null;
  239. }
  240. $logs = [];
  241. $db = \Yii::$app->db;
  242. $transaction = $db->beginTransaction();
  243. try {
  244. $oneRecharge = Recharge::findOne(['ID' => $this->selectedIds]);
  245. //判断状态
  246. if (($msg = Recharge::chkAuditStatus($oneRecharge->SN, $oneRecharge->AUDIT_STATUS, $this->auditStatus)) != '') {
  247. throw new Exception($msg);
  248. }
  249. //待审核->已审核 修改会员现金钱包
  250. if ($this->auditStatus == Recharge::STATUS_AUDITED) {
  251. Cash::changeUserCash($oneRecharge->USER_ID, 'CASH', abs($oneRecharge->AMOUNT), ['REMARK' => 'Review member recharge application']);
  252. }
  253. $oneRecharge->REMARK = $this->createRemark ?? '';
  254. $oneRecharge->AUDIT_ADMIN = \Yii::$app->user->id;
  255. $oneRecharge->AUDIT_STATUS = $this->auditStatus;
  256. $oneRecharge->AUDITED_AT = Date::nowTime();
  257. if (!$oneRecharge->save()) {
  258. throw new Exception(Form::formatErrorsForApi($oneRecharge->getErrors()));
  259. }
  260. $logs[$this->selectedIds] = $oneRecharge->SN;
  261. $transaction->commit();
  262. } catch (Exception $e) {
  263. $transaction->rollBack();
  264. $this->addError('auditStatus', $e->getMessage());
  265. return null;
  266. }
  267. return ['logs' => $logs, 'status' => $this->auditStatus];
  268. }
  269. }