RechargeForm.php 12 KB

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