InvoiceBalanceAuditForm.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace common\models\forms;
  3. use common\helpers\Date;
  4. use common\helpers\user\Balance;
  5. use common\helpers\user\Info;
  6. use common\helpers\user\Status;
  7. use common\components\Model;
  8. use common\helpers\Form;
  9. use common\models\InvoiceAudit;
  10. use common\models\InvoiceBalanceAudit;
  11. use common\models\Period;
  12. use common\models\RegInfoAudit;
  13. use common\models\User;
  14. use common\models\UserInfo;
  15. use yii\base\Exception;
  16. /**
  17. * 点位绑定表单
  18. */
  19. class InvoiceBalanceAuditForm extends Model {
  20. public $id;
  21. public $userName;
  22. public $realName;
  23. public $idCard;
  24. public $regType;
  25. public $amount;
  26. public $createRemark;
  27. public $auditRemark;
  28. public $auditStatus;
  29. private $_uid;
  30. private $_model;
  31. /**
  32. * @inheritdoc
  33. */
  34. public function rules() {
  35. return [
  36. [['id', 'userName', 'realName', 'idCard', 'regType', 'amount', 'createRemark', 'auditRemark', 'auditStatus'], 'trim'],
  37. [['id', 'userName', 'realName', 'idCard', 'regType', 'amount', 'auditStatus'], 'required'],
  38. [['userName'], 'isUserName'],
  39. [['id'], 'initModel'],
  40. [['auditStatus'], 'isAuditStatus'],
  41. ];
  42. }
  43. /**
  44. * 指定校验场景
  45. * @return array
  46. */
  47. public function scenarios() {
  48. $parentScenarios = parent::scenarios();
  49. $customScenarios = [
  50. 'add' => ['userName', 'realName', 'idCard', 'regType', 'amount', 'createRemark'],
  51. 'edit' => ['id', 'amount', 'createRemark'],
  52. 'audit' => ['id', 'amount', 'auditRemark', 'auditStatus'],
  53. ];
  54. return array_merge($parentScenarios, $customScenarios);
  55. }
  56. public function attributeLabels() {
  57. return [
  58. 'userName' => '会员编号',
  59. 'realName' => '会员姓名',
  60. 'idCard' => '身份证号',
  61. 'regType' => '注册类型',
  62. 'amount' => '交易金额',
  63. 'createRemark' => '创建备注',
  64. 'auditRemark' => '审核备注',
  65. 'auditStatus' => '审核状态',
  66. ];
  67. }
  68. /**
  69. * 初始化model
  70. * @param $attributes
  71. */
  72. public function initModel($attributes) {
  73. $this->_model = InvoiceBalanceAudit::findOne(['ID' => $this->id]);
  74. if (!$this->_model) {
  75. $this->addError($attributes, 'The data does not exist'); // 数据不存在
  76. }
  77. if ($this->_model->AUDIT_STATUS != \Yii::$app->params['auditStatus']['un']['value']) {
  78. $this->addError($attributes, '本数据已经被处理过了');
  79. }
  80. }
  81. /**
  82. * 用户名是否正确
  83. * @param $attributes
  84. */
  85. public function isUserName($attributes) {
  86. $userInfo = UserInfo::findOneAsArray(['USER_NAME' => $this->userName]);
  87. if ($userInfo) {
  88. $baseInfo = Info::baseInfoZh($userInfo['USER_ID']);
  89. if ($baseInfo['REAL_NAME'] != $this->realName || $baseInfo['ID_CARD'] != $this->idCard) {
  90. $this->addError($attributes, '输入的会员姓名或身份证号与系统不一致');
  91. }
  92. if ($userInfo['REG_TYPE'] != $this->regType) {
  93. $this->addError($attributes, '选择的会员注册类型与系统不一致');
  94. }
  95. if (InvoiceBalanceAudit::find()->where('USER_ID=:USER_ID AND AUDIT_STATUS=0', [':USER_ID' => $userInfo['USER_ID']])->exists()) {
  96. $this->addError($attributes, '填写的会员有待审核的发票调整');
  97. }
  98. $this->_uid = $userInfo['USER_ID'];
  99. } else {
  100. $this->addError($attributes, 'Member does not exist'); // 会员不存在
  101. }
  102. }
  103. /**
  104. * 审核状态是否正确
  105. * @param $attributes
  106. */
  107. public function isAuditStatus($attributes) {
  108. if (!array_key_exists($this->auditStatus, \Yii::$app->params['auditStatus'])) {
  109. $this->addError($attributes, '无效的审核状态');
  110. }
  111. }
  112. /**
  113. * 添加
  114. * @return RegInfoAudit|null
  115. * @throws \yii\db\Exception
  116. */
  117. public function add() {
  118. if (!$this->validate()) {
  119. return null;
  120. }
  121. $transaction = \Yii::$app->db->beginTransaction();
  122. try {
  123. // 添加会员合作点位
  124. $model = new InvoiceBalanceAudit();
  125. $model->USER_ID = $this->_uid;
  126. $model->AMOUNT = $this->amount;
  127. $model->AUDIT_STATUS = \Yii::$app->params['auditStatus']['un']['value'];
  128. $model->CREATE_ADMIN = \Yii::$app->user->id;
  129. $model->CREATE_REMARK = $this->createRemark;
  130. $model->CREATED_AT = Date::nowTime();
  131. if (!$model->save()) {
  132. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  133. }
  134. $transaction->commit();
  135. } catch (Exception $e) {
  136. $transaction->rollBack();
  137. $this->addError('add', $e->getMessage());
  138. return null;
  139. }
  140. return $model;
  141. }
  142. /**
  143. * 修改操作
  144. * @return null
  145. * @throws \yii\db\Exception
  146. */
  147. public function edit() {
  148. if (!$this->validate()) {
  149. return null;
  150. }
  151. $transaction = \Yii::$app->db->beginTransaction();
  152. try {
  153. $model = $this->_model;
  154. $model->AMOUNT = $this->amount;
  155. $model->CREATE_REMARK = $this->createRemark;
  156. if (!$model->save()) {
  157. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  158. }
  159. $transaction->commit();
  160. } catch (Exception $e) {
  161. $transaction->rollBack();
  162. $this->addError('edit', $e->getMessage());
  163. return null;
  164. }
  165. return $model;
  166. }
  167. /**
  168. * 审核操作
  169. * @return null
  170. * @throws \yii\db\Exception
  171. */
  172. public function audit() {
  173. if (!$this->validate()) {
  174. return null;
  175. }
  176. $transaction = \Yii::$app->db->beginTransaction();
  177. try {
  178. $model = $this->_model;
  179. if ($this->auditStatus == 'reject') {
  180. $model->AUDIT_STATUS = \Yii::$app->params['auditStatus']['reject']['value'];
  181. } elseif ($this->auditStatus == 'true') {
  182. $model->AUDIT_STATUS = \Yii::$app->params['auditStatus']['true']['value'];
  183. $model->AMOUNT = $this->amount;
  184. }
  185. $model->AUDIT_ADMIN = \Yii::$app->user->id;
  186. $model->AUDIT_REMARK = $this->auditRemark;
  187. $model->AUDITED_AT = Date::nowTime();
  188. if (!$model->save()) {
  189. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  190. }
  191. //更新会员税款信息
  192. if ($this->auditStatus == 'true') {
  193. Balance::changeInvoice($model->USER_ID, $model->AMOUNT, [
  194. 'REMARK' => '审核发票调整',
  195. 'CREATE_ADMIN' => $model->CREATE_ADMIN,
  196. 'CREATE_REMARK' => $model->CREATE_REMARK,
  197. 'CREATE_TIME' => $model->CREATED_AT,
  198. 'AUDIT_ADMIN' => $model->AUDIT_ADMIN,
  199. 'AUDIT_REMARK' => $model->AUDIT_REMARK,
  200. 'AUDIT_TIME' => $model->AUDITED_AT,
  201. ]);
  202. }
  203. $transaction->commit();
  204. } catch (Exception $e) {
  205. $transaction->rollBack();
  206. $this->addError('audit', $e->getMessage());
  207. return null;
  208. }
  209. return $model;
  210. }
  211. /**
  212. * 删除
  213. * @param $selected
  214. */
  215. public static function delete($selected) {
  216. }
  217. }