RechargeForm.php 12 KB

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