ReconsumeForm.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. <?php
  2. namespace common\models\forms;
  3. use backendApi\modules\v1\models\Admin;
  4. use common\helpers\Cache;
  5. use common\helpers\Date;
  6. use common\components\Model;
  7. use common\helpers\Form;
  8. use common\helpers\user\Reconsume;
  9. use common\helpers\user\Info;
  10. use common\helpers\Validator;
  11. use common\libs\logging\operate\AdminOperate;
  12. use common\models\PerfMonth;
  13. use common\models\Period;
  14. use common\models\ReconsumeAudit;
  15. use common\models\ReconsumePool;
  16. use common\models\ReconsumePoolFlow;
  17. use common\models\UserInfo;
  18. use yii\base\Exception;
  19. use yii\validators\NumberValidator;
  20. /**
  21. * Login form
  22. */
  23. class ReconsumeForm extends Model {
  24. public $id;
  25. public $selected;
  26. public $userName;
  27. public $type;
  28. public $changeAmount;
  29. public $remark;
  30. public $auditStatus;
  31. public $calcYearMonth;
  32. public $isPass;
  33. public $isShow;
  34. private $_model;
  35. private $_userId;
  36. const TYPE_BALANCE = 0; // 变动余额
  37. const TYPE_MONTH = 1; // 变动月份
  38. public function init() {
  39. parent::init();
  40. $this->adminOperateLogger = new AdminOperate([
  41. 'fetchClass' => ReconsumePool::class,
  42. ]);
  43. }
  44. /**
  45. * @inheritdoc
  46. */
  47. public function rules() {
  48. return [
  49. [['id', 'selected', 'userName', 'type', 'changeAmount', 'remark', 'auditStatus', 'calcYearMonth', 'isShow'], 'trim'],
  50. [['id', 'userName', 'type', 'changeAmount', 'remark', 'auditStatus', 'calcYearMonth'], 'required'],
  51. [['id'], 'exist', 'targetClass' => ReconsumeAudit::class, 'targetAttribute' => 'ID'],
  52. [['id'], 'initModel'],
  53. [['userName'], 'exist', 'targetClass' => UserInfo::class, 'targetAttribute' => 'USER_NAME'],
  54. [['userName'], 'isUserName'],
  55. [['calcYearMonth'], 'yearMonth'],
  56. [['type'], 'isType'],
  57. [['auditStatus'], 'isAuditStatus'],
  58. [['selected'], 'isSelected'],
  59. [['changeAmount'], 'isChangeAmount'],
  60. [['calcYearMonth'], 'isCanDeduct'],
  61. ];
  62. }
  63. public function attributeLabels() {
  64. return [
  65. 'userName' => '会员编号',
  66. 'type' => '变动类型',
  67. 'changeAmount' => '变动数',
  68. 'remark' => '备注',
  69. 'auditStatus' => '审核状态',
  70. 'isShow' => '前台显示备注',
  71. 'calcYearMonth' => '结算年月',
  72. ];
  73. }
  74. /**
  75. * 指定校验场景
  76. * @return array
  77. */
  78. public function scenarios() {
  79. $parentScenarios = parent::scenarios();
  80. $customScenarios = [
  81. 'changeAdd' => ['userName', 'type', 'changeAmount', 'remark', 'isShow'],
  82. 'changeAudit' => ['selected', 'remark', 'auditStatus'],
  83. 'edit' => ['id', 'type', 'changeAmount', 'remark', 'isShow'],
  84. 'pass' => ['id', 'type', 'changeAmount', 'remark', 'isShow'],
  85. 'deductReConsume' => ['userName', 'remark', 'isPass'],
  86. 'deductReConsumeAdd' => ['userName', 'calcYearMonth'],
  87. 'deductReConsumeAudit' => ['id', 'remark', 'isShow', 'auditStatus'],
  88. ];
  89. return array_merge($parentScenarios, $customScenarios);
  90. }
  91. /**
  92. * 初始化广告model类
  93. * @param $attribute
  94. */
  95. public function initModel($attribute) {
  96. $model = $this->_model = ReconsumeAudit::findOne(['ID' => $this->id]);
  97. $this->_userId = $model->USER_ID;
  98. if ($model->AUDIT_STATUS > \Yii::$app->params['auditStatus']['un']['value']) {
  99. $this->addError($attribute, 'The application has been reviewed and cannot be reviewed again');// 该申请已经被审核,不能重复审核
  100. }
  101. }
  102. /**
  103. * 校验用户名
  104. * @param $attribute
  105. */
  106. public function isUserName($attribute) {
  107. // 如果该用户存在待审核状态的数据,如果有,则提示管理员先处理待审核的数据
  108. $this->_userId = Info::getUserIdByUserName($this->userName);
  109. /*$userId = $this->_userInfo['USER_ID'];
  110. $type = '';
  111. if ($this->scenario == 'changeAdd') {
  112. $type = ReconsumeAudit::TYPE_CHANGE;
  113. } elseif ($this->scenario == 'deductReConsumeAdd') {
  114. $type = ReconsumeAudit::TYPE_RECONSUME;
  115. }
  116. if (ReconsumeAudit::find()->where('USER_ID=:USER_ID AND AUDIT_STATUS=:AUDIT_STATUS AND TYPE=:TYPE', [':USER_ID' => $userId, ':AUDIT_STATUS' => \Yii::$app->params['auditStatus']['un']['value'], ':TYPE' => $type])->exists()) {
  117. $this->addError($attribute, '该会员存在一条待审核的记录,请等待待审核数据处理完毕');
  118. }*/
  119. }
  120. /**
  121. * 校验类型
  122. * @param $attribute
  123. */
  124. public function isType($attribute) {
  125. if (!in_array($this->type, [self::TYPE_BALANCE, self::TYPE_MONTH])) {
  126. $this->addError($attribute, '调整类型错误');
  127. }
  128. }
  129. /**
  130. * 审核状态是否正确
  131. * @param $attributes
  132. */
  133. public function isAuditStatus($attributes) {
  134. if (!array_key_exists($this->auditStatus, \Yii::$app->params['auditStatus'])) {
  135. $this->addError($attributes, '无效的审核状态');
  136. }
  137. }
  138. /**
  139. * 批量数据
  140. * @param $attributes
  141. */
  142. public function isSelected($attributes) {
  143. if (!$this->selected) {
  144. $this->addError($attributes, 'A piece of data must be selected'); // 必须选择一条数据
  145. }
  146. if (!is_array($this->selected)) {
  147. $this->selected = [$this->selected];
  148. }
  149. }
  150. /**
  151. * 变动内容
  152. * @param $attribute
  153. * @throws \yii\db\Exception
  154. */
  155. public function isChangeAmount($attribute) {
  156. if ($this->changeAmount == 0) {
  157. $this->addError($attribute, '调整值不能等于0');
  158. }
  159. $reConsumePool = Reconsume::getUserReconsumePool($this->_userId);
  160. if ($this->type == self::TYPE_BALANCE) {
  161. if (!Validator::validateQuickLy('fullPrice', $this->changeAmount)) {
  162. $this->addError($this->changeAmount, '调整金额不正确');
  163. }
  164. if(($reConsumePool['unusedPV']+$this->changeAmount)<0){
  165. $this->addError($this->changeAmount, '可用余额不足');
  166. }
  167. } elseif ($this->type == self::TYPE_MONTH) {
  168. $validator = Validator::createValidator(NumberValidator::class, $this, [['changeAmount'], 'integer']);
  169. $this->validators->append($validator);
  170. if(($reConsumePool['unusedMonth']+$this->changeAmount)<0){
  171. $this->addError($this->changeAmount, '可用月数不足');
  172. }
  173. }
  174. }
  175. /**
  176. * 是否能扣除复销
  177. * @param $attribute
  178. * @throws \yii\db\Exception
  179. */
  180. public function isCanDeduct($attribute) {
  181. $userId = Info::getUserIdByUserName($this->userName);
  182. if (!Reconsume::isCanDeduct($userId, $this->calcYearMonth)) {
  183. $this->addError($attribute, '会员已合格不能再次扣除');
  184. }
  185. //未来的结算年和月不能手动扣除,即未结算的结算月不能扣除
  186. if($periodPoint = Period::getPeriodPointFromMonth(substr($this->calcYearMonth, 0, 4), intval(substr($this->calcYearMonth, 4, 2)))){
  187. if(boolval($periodPoint['IS_CALCULATED'] != Period::CALCULATE_FINISH)){
  188. $this->addError($attribute, '所选结算月未结算不能扣除');
  189. }
  190. }
  191. }
  192. /**
  193. * 添加申请
  194. * @return ReconsumeAudit|null
  195. * @throws \yii\db\Exception
  196. */
  197. public function changeAdd() {
  198. if (!$this->validate()) {
  199. return null;
  200. }
  201. $db = \Yii::$app->db;
  202. $transaction = $db->beginTransaction();
  203. try {
  204. $model = new ReconsumeAudit();
  205. $model->CREATED_AT = Date::nowTime();
  206. $model->USER_ID = Info::getUserIdByUserName($this->userName);
  207. if ($this->type == self::TYPE_BALANCE) {
  208. $model->CHANGE_PV = $this->changeAmount;
  209. } elseif ($this->type == self::TYPE_MONTH) {
  210. $model->CHANGE_MONTH = $this->changeAmount;
  211. }
  212. $model->TYPE = $this->type;//190715调整余额的类型
  213. $model->CREATE_ADMIN = \Yii::$app->user->id;
  214. $model->CREATE_REMARK = $this->remark;
  215. $model->REMARK_IS_SHOW = $this->isShow;
  216. if (!$model->save()) {
  217. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  218. }
  219. $transaction->commit();
  220. } catch (Exception $e) {
  221. $transaction->rollBack();
  222. $this->addError('changeAdd', $e->getMessage());
  223. return null;
  224. }
  225. $this->adminOperateLogger->fetchClass= ReconsumeAudit::class;
  226. $this->adminOperateLogger->afterInsert($model)->clean()->save([
  227. 'optType' => '申请调整复销池余额',
  228. 'userId' => $model->USER_ID,
  229. 'userName' => Info::getUserNameByUserId($model->USER_ID),
  230. 'remark' => $this->remark
  231. ]);
  232. return $model;
  233. }
  234. /**
  235. * 修改信息
  236. * @return null
  237. * @throws \yii\db\Exception
  238. */
  239. public function edit() {
  240. //取消编辑功能
  241. return null;
  242. if (!$this->validate()) {
  243. return null;
  244. }
  245. $db = \Yii::$app->db;
  246. $transaction = $db->beginTransaction();
  247. try {
  248. $model = $this->_model;
  249. if ($this->type == self::TYPE_BALANCE) {
  250. $model->CHANGE_PV = $this->changeAmount;
  251. $model->CHANGE_MONTH = 0;
  252. } elseif ($this->type == self::TYPE_MONTH) {
  253. $model->CHANGE_MONTH = $this->changeAmount;
  254. $model->CHANGE_PV = 0;
  255. }
  256. $model->REMARK_IS_SHOW = $this->isShow;
  257. $model->CREATE_REMARK = $this->remark;
  258. if (!$model->save()) {
  259. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  260. }
  261. $transaction->commit();
  262. } catch (Exception $e) {
  263. $transaction->rollBack();
  264. $this->addError('edit', $e->getMessage());
  265. return null;
  266. }
  267. return $model;
  268. }
  269. /**
  270. * 审核通过
  271. * @return null
  272. * @throws \yii\db\Exception
  273. */
  274. public function pass() {
  275. if (!$this->validate()) {
  276. return null;
  277. }
  278. $this->adminOperateLogger->beforeUpdate(ReconsumePool::findOne(['USER_ID'=>$this->_userId]));
  279. $db = \Yii::$app->db;
  280. $transaction = $db->beginTransaction();
  281. try {
  282. $model = $this->_model;
  283. if ($this->type == self::TYPE_BALANCE) {
  284. $model->CHANGE_PV = $this->changeAmount;
  285. $model->CHANGE_MONTH = 0;
  286. } elseif ($this->type == self::TYPE_MONTH) {
  287. $model->CHANGE_MONTH = $this->changeAmount;
  288. $model->CHANGE_PV = 0;
  289. }
  290. $model->AUDIT_STATUS = \Yii::$app->params['auditStatus']['true']['value'];
  291. $model->AUDIT_ADMIN = \Yii::$app->user->id;
  292. $model->CREATE_REMARK = $this->remark;
  293. $model->REMARK_IS_SHOW = $this->isShow;
  294. $model->AUDITED_AT = Date::nowTime();
  295. if (!$model->save()) {
  296. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  297. }
  298. // 查看该条记录中变化的是PV还是月数
  299. if ($model->CHANGE_PV != 0) {
  300. Reconsume::changePoolPV($model->USER_ID, $model->CHANGE_PV, [
  301. 'DEAL_TYPE' => Reconsume::TYPE_AUDIT_PV,
  302. 'REMARK' => $model->CREATE_REMARK,
  303. 'REMARK_IS_SHOW' => $this->isShow,
  304. 'ADMIN_NAME'=>Admin::getAdminNameById(\Yii::$app->user->id),
  305. ]);
  306. }
  307. if ($model->CHANGE_MONTH != 0) {
  308. Reconsume::changePoolMonthNum($model->USER_ID, $model->CHANGE_MONTH, ['DEAL_TYPE' => Reconsume::TYPE_AUDIT_MONTH, 'REMARK' => $model->CREATE_REMARK,'REMARK_IS_SHOW' => $this->isShow, 'ADMIN_NAME'=>Admin::getAdminNameById(\Yii::$app->user->id)]);
  309. }
  310. $transaction->commit();
  311. } catch (Exception $e) {
  312. $transaction->rollBack();
  313. $this->addError('pass', $e->getMessage());
  314. return null;
  315. }
  316. $this->adminOperateLogger->afterUpdate(ReconsumePool::findOne(['USER_ID'=>$this->_userId]))->clean()->save([
  317. 'optType' => '审核通过调整复销池余额',
  318. 'userId' => $model->USER_ID,
  319. 'userName' => Info::getUserNameByUserId($model->USER_ID),
  320. 'remark' => $this->remark
  321. ]);
  322. return $model;
  323. }
  324. /**
  325. * 审核余额变动
  326. * @return null
  327. * @throws \yii\db\Exception
  328. */
  329. public function changeAudit() {
  330. if (!$this->validate()) {
  331. return null;
  332. }
  333. $uids=[];
  334. if($this->auditStatus=='true'){
  335. foreach ($this->selected as $select) {
  336. $oneBalanceAudit = ReconsumeAudit::findOneAsArray('ID=:ID',[':ID'=>$select],'USER_ID');
  337. $uids[]=$oneBalanceAudit['USER_ID'];
  338. }
  339. }
  340. if($this->auditStatus=='true'){
  341. $this->adminOperateLogger->setIsBatch(true)->beforeUpdate($uids, 'USER_ID');
  342. }else{
  343. $this->adminOperateLogger->fetchClass = ReconsumeAudit::class;
  344. $this->adminOperateLogger->setIsBatch(true)->beforeUpdate($this->selected, 'ID');
  345. }
  346. $model = new ReconsumeAudit();
  347. $db = \Yii::$app->db;
  348. $transaction = $db->beginTransaction();
  349. try {
  350. foreach ($this->selected as $select) {
  351. $model = ReconsumeAudit::findOne(['ID' => $select]);
  352. if ($model->AUDIT_STATUS > \Yii::$app->params['auditStatus']['un']['value']) continue;
  353. $model->AUDIT_STATUS = \Yii::$app->params['auditStatus'][$this->auditStatus]['value'];
  354. $model->AUDIT_ADMIN = \Yii::$app->user->id;
  355. $model->CREATE_REMARK = $this->remark;
  356. $model->AUDITED_AT = Date::nowTime();
  357. if (!$model->save()) {
  358. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  359. }
  360. if ($model->AUDIT_STATUS == \Yii::$app->params['auditStatus']['true']['value']) {
  361. // 查看该条记录中变化的是PV还是月数
  362. if ($model->CHANGE_PV != 0) {
  363. Reconsume::changePoolPV($model->USER_ID, $model->CHANGE_PV, [
  364. 'DEAL_TYPE' => Reconsume::TYPE_AUDIT_PV,
  365. 'REMARK' => $model->CREATE_REMARK,
  366. 'ADMIN_NAME'=>Admin::getAdminNameById(\Yii::$app->user->id)
  367. ]);
  368. }
  369. if ($model->CHANGE_MONTH != 0) {
  370. Reconsume::changePoolMonthNum($model->USER_ID, $model->CHANGE_MONTH, ['DEAL_TYPE' => Reconsume::TYPE_AUDIT_MONTH, 'REMARK' => $model->CREATE_REMARK, 'ADMIN_NAME'=>Admin::getAdminNameById(\Yii::$app->user->id)]);
  371. }
  372. }
  373. }
  374. $transaction->commit();
  375. } catch (Exception $e) {
  376. $transaction->rollBack();
  377. $this->addError('changeAudit', $e->getMessage());
  378. return null;
  379. }
  380. if($this->auditStatus=='true'){
  381. $this->adminOperateLogger->fetchClass = ReconsumePool::class;
  382. $this->adminOperateLogger->setIsBatch(true)->afterUpdate($uids, 'USER_ID')->clean()->save([
  383. 'optType' => '批量审核调整复销池余额',
  384. 'remark' => $this->remark,
  385. ]);
  386. }else{
  387. $this->adminOperateLogger->fetchClass = ReconsumeAudit::class;
  388. $this->adminOperateLogger->setIsBatch(true)->afterUpdate($this->selected, 'ID')->clean()->save([
  389. 'optType' => '批量拒绝调整复销池余额',
  390. 'remark' => $this->remark,
  391. ]);
  392. }
  393. return count($this->selected) == 1 ? $model : true;
  394. }
  395. /**
  396. * 扣除复销(当月是否合格)
  397. * @return null
  398. * @throws \yii\db\Exception
  399. */
  400. public function deductReConsume() {
  401. if (!$this->validate()) {
  402. return null;
  403. }
  404. $db = \Yii::$app->db;
  405. $transaction = $db->beginTransaction();
  406. try {
  407. $userId = $this->_userId;
  408. $period = Period::instance();
  409. $yearMonth = $period->getNowYearMonth();
  410. $periodNum = $period->getNowPeriodNum();
  411. $lastYearMonth = $period->getLastMonth($periodNum);
  412. $lastYearMonth = $lastYearMonth['yearMonth'];
  413. if (!Reconsume::isCanDeduct($userId, $yearMonth)) {
  414. throw new Exception('该月不能扣除复销');
  415. }
  416. if (Reconsume::isCanGetBonus($userId, $yearMonth)) {
  417. throw new Exception('该月已有获奖资格,不能扣除');
  418. }
  419. $isPassText = $this->isPass ? '合格' : '不合格';
  420. $remark = $this->remark ? $this->remark : '手动扣除本月复销并设置为' . $isPassText;
  421. // 扣除复销
  422. $sysConfig = Cache::getSystemConfig();
  423. if (!Reconsume::deductFxByCalcMonth($userId, $yearMonth, ['DEAL_TYPE' => Reconsume::TYPE_MANUAL, 'REMARK' => $remark])) {
  424. throw new Exception('复销扣除失败,请检查会员复销余额是否充足');
  425. }
  426. if ($this->isPass) {
  427. // 把月业绩表中对应的该会员上个月的复销状态设置
  428. PerfMonth::updateAll(['FX_STATUS' => 1], 'USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', [':USER_ID' => $userId, ':CALC_MONTH' => $lastYearMonth]);
  429. }
  430. $transaction->commit();
  431. } catch (Exception $e) {
  432. $transaction->rollBack();
  433. $this->addError('deductReConsume', $e->getMessage());
  434. return null;
  435. }
  436. return $this->_userId;
  437. }
  438. /**
  439. * 审核扣除复销
  440. * @return null
  441. * @throws \yii\db\Exception
  442. */
  443. public function deductReConsumeAudit() {
  444. if (!$this->validate()) {
  445. return null;
  446. }
  447. $db = \Yii::$app->db;
  448. $transaction = $db->beginTransaction();
  449. try {
  450. $model = $this->_model;
  451. $model->AUDIT_STATUS = \Yii::$app->params['auditStatus'][$this->auditStatus]['value'];
  452. $model->AUDIT_ADMIN = \Yii::$app->user->id;
  453. $model->CREATE_REMARK = $this->remark;
  454. $model->AUDITED_AT = Date::nowTime();
  455. if (!$model->save()) {
  456. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  457. }
  458. if ($model->AUDIT_STATUS == \Yii::$app->params['auditStatus']['true']['value']) {
  459. if (!Reconsume::isCanDeduct($model->USER_ID, $model->CALC_MONTH)) {
  460. throw new Exception('该月不能扣除复销');
  461. }
  462. // 查看该条记录中变化的是PV还是月数
  463. $sysConfig = Cache::getSystemConfig();
  464. if (!Reconsume::deductFxByCalcMonth($model->USER_ID, $model->CALC_MONTH, ['DEAL_TYPE' => Reconsume::TYPE_MANUAL, 'REMARK' => $model->CREATE_REMARK, 'ADMIN_NAME'=>Admin::getAdminNameById(\Yii::$app->user->id)])) {
  465. throw new Exception('复销扣除失败,请检查会员复销余额是否充足');
  466. }
  467. // 把月业绩表中对应的该会员您的复销状态设置
  468. PerfMonth::updateAll(['FX_STATUS' => 1], 'USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', [':USER_ID' => $model->USER_ID, ':CALC_MONTH' => $model->CALC_MONTH]);
  469. }
  470. $transaction->commit();
  471. } catch (Exception $e) {
  472. $transaction->rollBack();
  473. $this->addError('changeAudit', $e->getMessage());
  474. return null;
  475. }
  476. return $model;
  477. }
  478. /**
  479. * 删除前
  480. * @param $selected
  481. */
  482. public function beforeDelete($selected) {
  483. $this->adminOperateLogger->fetchClass = ReconsumeAudit::class;
  484. $this->adminOperateLogger->setIsBatch(true)->beforeDelete($selected, 'ID');
  485. }
  486. /**
  487. * 删除
  488. * @param $selected
  489. * @throws Exception
  490. */
  491. public function delete($selected) {
  492. $this->adminOperateLogger->clean()->save([
  493. 'optType' => '删除调整复销池余额待审核数据',
  494. ]);
  495. }
  496. }