InvoiceAuditForm.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. namespace common\models\forms;
  3. use common\helpers\Cache;
  4. use common\helpers\Date;
  5. use common\helpers\user\Balance;
  6. use common\helpers\user\Info;
  7. use common\helpers\user\Status;
  8. use common\components\Model;
  9. use common\helpers\Form;
  10. use common\models\InvoiceAudit;
  11. use common\models\Period;
  12. use common\models\RegInfoAudit;
  13. use common\models\User;
  14. use common\models\UserInfo;
  15. use common\models\Withdraw;
  16. use yii\base\Exception;
  17. /**
  18. * 点位绑定表单
  19. */
  20. class InvoiceAuditForm extends Model {
  21. public $id;
  22. public $withdrawId;
  23. public $invoiceCode;
  24. public $invoiceNum;
  25. public $invoiceDate;
  26. public $amount;
  27. public $taxRate;
  28. public $purchaserName;
  29. public $purchaserRegisterNum;
  30. public $purchaserAddress;
  31. public $purchaserBank;
  32. public $sellerName;
  33. public $sellerRegisterNum;
  34. public $sellerAddress;
  35. public $sellerBank;
  36. public $itemName;
  37. public $invoiceRemark;
  38. public $createRemark;
  39. public $auditStatus;
  40. private $_uid;
  41. private $_model;
  42. private $_withdrawModel;
  43. /**
  44. * @inheritdoc
  45. */
  46. public function rules() {
  47. return [
  48. [['id', 'withdrawId', 'invoiceCode', 'invoiceNum', 'invoiceDate', 'amount', 'taxRate', 'purchaserName', 'purchaserRegisterNum', 'purchaserAddress', 'purchaserBank', 'sellerName', 'sellerRegisterNum', 'sellerAddress', 'sellerBank', 'itemName', 'invoiceRemark', 'createRemark'], 'trim'],
  49. [['id', 'withdrawId', 'invoiceCode', 'invoiceNum', 'invoiceDate', 'amount', 'purchaserName', 'purchaserRegisterNum', 'purchaserAddress', 'purchaserBank', 'sellerName', 'sellerRegisterNum', 'sellerAddress', 'sellerBank', 'itemName'], 'required'],
  50. [['id'], 'initModel'],
  51. [['withdrawId'], 'initWithdrawModel'],
  52. [['itemName'], 'isItemName'],
  53. //[['invoiceNum'], 'isInvoiceNum'],
  54. [['auditStatus'], 'isAuditStatus'],
  55. ];
  56. }
  57. /**
  58. * 指定校验场景
  59. * @return array
  60. */
  61. public function scenarios() {
  62. $parentScenarios = parent::scenarios();
  63. $customScenarios = [
  64. 'addByAdmin' => ['withdrawId', 'invoiceCode', 'invoiceNum', 'invoiceDate', 'amount', 'taxRate', 'purchaserName', 'purchaserRegisterNum', 'purchaserAddress', 'purchaserBank', 'sellerName', 'sellerRegisterNum', 'sellerAddress', 'sellerBank', 'itemName', 'invoiceRemark', 'createRemark'],
  65. 'editByAdmin' => ['id', 'withdrawId', 'invoiceCode', 'invoiceNum', 'invoiceDate', 'amount', 'taxRate', 'purchaserName', 'purchaserRegisterNum', 'purchaserAddress', 'purchaserBank', 'sellerName', 'sellerRegisterNum', 'sellerAddress', 'sellerBank', 'itemName', 'invoiceRemark', 'createRemark'],
  66. 'audit' => ['id', 'withdrawId', 'invoiceCode', 'invoiceNum', 'invoiceDate', 'amount', 'taxRate', 'purchaserName', 'purchaserRegisterNum', 'purchaserAddress', 'purchaserBank', 'sellerName', 'sellerRegisterNum', 'sellerAddress', 'sellerBank', 'itemName', 'invoiceRemark', 'createRemark', 'auditStatus'],
  67. ];
  68. return array_merge($parentScenarios, $customScenarios);
  69. }
  70. public function attributeLabels() {
  71. return [
  72. 'id' => '发票id',
  73. 'withdrawId' => '提现id',
  74. 'invoiceCode' => '发票代码',
  75. 'invoiceNum' => '发票号码',
  76. 'invoiceDate' => '开票日期',
  77. 'amount' => '提现金额',
  78. 'taxRate' => '税率',
  79. 'purchaserName' => '购买方名称',
  80. 'purchaserRegisterNum' => '购买方纳税人识别号',
  81. 'purchaserAddress' => '购买方地址电话',
  82. 'purchaserBank' => '购买方开户行及账号',
  83. 'sellerName' => '销售方名称',
  84. 'sellerRegisterNum' => '销售方纳税人识别号',
  85. 'sellerAddress' => '销售方地址电话',
  86. 'sellerBank' => '销售方地址电话',
  87. 'itemName' => '货物或应税劳务服务名称',
  88. 'invoiceRemark' => '发票备注',
  89. 'createRemark' => '备注',
  90. ];
  91. }
  92. /**
  93. * 初始化model
  94. * @param $attributes
  95. */
  96. public function initModel($attributes) {
  97. $this->_model = InvoiceAudit::findOne(['ID' => $this->id]);
  98. if (!$this->_model) {
  99. $this->addError($attributes, 'The data does not exist'); // 数据不存在
  100. }
  101. if ($this->_model->AUDIT_STATUS != \Yii::$app->params['auditStatus']['un']['value']) {
  102. $this->addError($attributes, '本数据已经被处理过了');
  103. }
  104. }
  105. /**
  106. * 初始化withdrawModel
  107. * @param $attributes
  108. */
  109. public function initWithdrawModel($attributes) {
  110. $this->_withdrawModel = Withdraw::findOne(['ID' => $this->withdrawId]);
  111. if (!$this->_withdrawModel) {
  112. $this->addError($attributes, 'The data does not exist'); // 数据不存在
  113. }
  114. if ($this->_withdrawModel->AUDIT_STATUS != Withdraw::STATUS_APPLIED) {
  115. $this->addError($attributes, '已审核的提现无法修改发票信息');
  116. }
  117. if ($this->amount != $this->_withdrawModel->AMOUNT) {
  118. $this->addError($attributes, '上传发票金额与提现金额不一致');
  119. }
  120. if (InvoiceAudit::find()->where('INVOICE_NUM=:INVOICE_NUM AND WITHDRAW_ID!=:WITHDRAW_ID AND AUDIT_STATUS=:AUDIT_STATUS', [':INVOICE_NUM' => $this->invoiceNum, ':WITHDRAW_ID' => $this->withdrawId, ':AUDIT_STATUS' => \Yii::$app->params['auditStatus']['true']['value']])->exists()) {
  121. $this->addError($attributes, '该发票的发票号码【'.$this->invoiceNum.'】已被使用');
  122. }
  123. }
  124. /**
  125. * 审核状态是否正确
  126. * @param $attributes
  127. */
  128. public function isAuditStatus($attributes) {
  129. if (!array_key_exists($this->auditStatus, \Yii::$app->params['auditStatus'])) {
  130. $this->addError($attributes, '无效的审核状态');
  131. }
  132. }
  133. /**
  134. * 货物或应税劳务服务名称是否正确
  135. * @param $attributes
  136. */
  137. public function isItemName($attributes) {
  138. $configName = Cache::getSystemConfig()['invoiceItemName']['VALUE'];
  139. if ($this->itemName != $configName) {
  140. $this->addError($attributes, '上传发票中货物或应税劳务服务名称与后台预置信息【' . $configName . '】不一致');
  141. }
  142. }
  143. /**
  144. * 添加
  145. * @return RegInfoAudit|null
  146. * @throws \yii\db\Exception
  147. */
  148. public function addByAdmin() {
  149. if (!$this->validate()) {
  150. return null;
  151. }
  152. $transaction = \Yii::$app->db->beginTransaction();
  153. try {
  154. // 添加会员合作点位
  155. $invoiceModel = new InvoiceAudit();
  156. $invoiceModel->USER_ID = $this->_withdrawModel->USER_ID;
  157. $invoiceModel->WITHDRAW_ID = $this->withdrawId;
  158. $invoiceModel->INVOICE_CODE = $this->invoiceCode;
  159. $invoiceModel->INVOICE_NUM = $this->invoiceNum;
  160. $invoiceModel->INVOICE_DATE = $this->invoiceDate ? Date::convert(Date::utcToTime($this->invoiceDate), 'Y年m月d日') : 0;
  161. $invoiceModel->AMOUNT = $this->amount;
  162. $invoiceModel->TAX_RATE = $this->taxRate;
  163. $invoiceModel->PURCHASER_NAME = $this->purchaserName;
  164. $invoiceModel->PURCHASER_REGISTER_NUM = $this->purchaserRegisterNum;
  165. $invoiceModel->PURCHASER_ADDRESS = $this->purchaserAddress;
  166. $invoiceModel->PURCHASER_BANK = $this->purchaserBank;
  167. $invoiceModel->SELLER_NAME = $this->sellerName;
  168. $invoiceModel->SELLER_REGISTER_NUM = $this->sellerRegisterNum;
  169. $invoiceModel->SELLER_ADDRESS = $this->sellerAddress;
  170. $invoiceModel->SELLER_BANK = $this->sellerBank;
  171. $invoiceModel->ITEM_NAME = $this->itemName;
  172. $invoiceModel->INVOICE_REMARK = $this->invoiceRemark;
  173. $invoiceModel->CREATE_REMARK = $this->createRemark;
  174. $invoiceModel->CREATE_ADMIN = \Yii::$app->user->id;
  175. $invoiceModel->CREATED_AT = Date::nowTime();
  176. if (!$invoiceModel->save()) {
  177. throw new Exception(Form::formatErrorsForApi($invoiceModel->getErrors()));
  178. }
  179. //写入提现表
  180. $withdrawModel = $this->_withdrawModel;
  181. $withdrawModel->INVOICE_ID = $invoiceModel->ID;
  182. $withdrawModel->UPDATE_ADMIN = \Yii::$app->user->id;
  183. $withdrawModel->UPDATED_AT = Date::nowTime();
  184. if ($withdrawModel->AUDIT_STATUS == Withdraw::STATUS_APPLIED) {
  185. // $withdrawModel->AUDIT_STATUS = Withdraw::STATUS_INVOICED;
  186. $withdrawModel->AUDIT_ADMIN = \Yii::$app->user->id;
  187. $withdrawModel->AUDITED_AT = Date::nowTime();
  188. }
  189. if (!$withdrawModel->save()) {
  190. throw new Exception(Form::formatErrorsForApi($withdrawModel->getErrors()));
  191. }
  192. $transaction->commit();
  193. } catch (Exception $e) {
  194. $transaction->rollBack();
  195. $this->addError('addByAdmin', $e->getMessage());
  196. return null;
  197. }
  198. return $invoiceModel;
  199. }
  200. /**
  201. * 修改操作
  202. * @return null
  203. * @throws \yii\db\Exception
  204. */
  205. public function editByAdmin() {
  206. if (!$this->validate()) {
  207. return null;
  208. }
  209. $transaction = \Yii::$app->db->beginTransaction();
  210. try {
  211. $invoiceModel = $this->_model;
  212. $invoiceModel->USER_ID = $this->_withdrawModel->USER_ID;
  213. $invoiceModel->WITHDRAW_ID = $this->withdrawId;
  214. $invoiceModel->INVOICE_CODE = $this->invoiceCode;
  215. $invoiceModel->INVOICE_NUM = $this->invoiceNum;
  216. $invoiceModel->INVOICE_DATE = $this->invoiceDate ? Date::convert(Date::utcToTime($this->invoiceDate), 'Y年m月d日') : 0;
  217. $invoiceModel->AMOUNT = $this->amount;
  218. $invoiceModel->TAX_RATE = $this->taxRate;
  219. $invoiceModel->PURCHASER_NAME = $this->purchaserName;
  220. $invoiceModel->PURCHASER_REGISTER_NUM = $this->purchaserRegisterNum;
  221. $invoiceModel->PURCHASER_ADDRESS = $this->purchaserAddress;
  222. $invoiceModel->PURCHASER_BANK = $this->purchaserBank;
  223. $invoiceModel->SELLER_NAME = $this->sellerName;
  224. $invoiceModel->SELLER_REGISTER_NUM = $this->sellerRegisterNum;
  225. $invoiceModel->SELLER_ADDRESS = $this->sellerAddress;
  226. $invoiceModel->SELLER_BANK = $this->sellerBank;
  227. $invoiceModel->ITEM_NAME = $this->itemName;
  228. $invoiceModel->INVOICE_REMARK = $this->invoiceRemark;
  229. $invoiceModel->CREATE_REMARK = $this->createRemark;
  230. $invoiceModel->CREATE_ADMIN = \Yii::$app->user->id;
  231. $invoiceModel->CREATED_AT = Date::nowTime();
  232. if (!$invoiceModel->save()) {
  233. throw new Exception(Form::formatErrorsForApi($invoiceModel->getErrors()));
  234. }
  235. //写入提现表
  236. $withdrawModel = $this->_withdrawModel;
  237. $withdrawModel->INVOICE_ID = $invoiceModel->ID;
  238. $withdrawModel->UPDATE_ADMIN = \Yii::$app->user->id;
  239. $withdrawModel->UPDATED_AT = Date::nowTime();
  240. if ($withdrawModel->AUDIT_STATUS == Withdraw::STATUS_APPLIED) {
  241. // $withdrawModel->AUDIT_STATUS = Withdraw::STATUS_INVOICED;
  242. $withdrawModel->AUDIT_ADMIN = \Yii::$app->user->id;
  243. $withdrawModel->AUDITED_AT = Date::nowTime();
  244. }
  245. if (!$withdrawModel->save()) {
  246. throw new Exception(Form::formatErrorsForApi($withdrawModel->getErrors()));
  247. }
  248. $transaction->commit();
  249. } catch (Exception $e) {
  250. $transaction->rollBack();
  251. $this->addError('editByAdmin', $e->getMessage());
  252. return null;
  253. }
  254. return $invoiceModel;
  255. }
  256. /**
  257. * 审核操作
  258. * @return null
  259. * @throws \yii\db\Exception
  260. */
  261. public function audit() {
  262. if (!$this->validate()) {
  263. return null;
  264. }
  265. $transaction = \Yii::$app->db->beginTransaction();
  266. try {
  267. $invoiceModel = $this->_model;
  268. $withdrawModel = $this->_withdrawModel;
  269. if ($this->auditStatus == 'reject') {
  270. $invoiceModel->AUDIT_STATUS = \Yii::$app->params['auditStatus']['reject']['value'];
  271. $withdrawModel->AUDIT_STATUS = Withdraw::STATUS_APPLIED;
  272. $withdrawModel->AUDIT_ADMIN = \Yii::$app->user->id;
  273. $withdrawModel->AUDITED_AT = Date::nowTime();
  274. } elseif ($this->auditStatus == 'true') {
  275. $invoiceModel->INVOICE_CODE = $this->invoiceCode;
  276. $invoiceModel->INVOICE_NUM = $this->invoiceNum;
  277. $invoiceModel->INVOICE_DATE = $this->invoiceDate ? Date::convert(Date::utcToTime($this->invoiceDate), 'Y年m月d日') : 0;
  278. $invoiceModel->AMOUNT = $this->amount;
  279. $invoiceModel->TAX_RATE = $this->taxRate;
  280. $invoiceModel->PURCHASER_NAME = $this->purchaserName;
  281. $invoiceModel->PURCHASER_REGISTER_NUM = $this->purchaserRegisterNum;
  282. $invoiceModel->PURCHASER_ADDRESS = $this->purchaserAddress;
  283. $invoiceModel->PURCHASER_BANK = $this->purchaserBank;
  284. $invoiceModel->SELLER_NAME = $this->sellerName;
  285. $invoiceModel->SELLER_REGISTER_NUM = $this->sellerRegisterNum;
  286. $invoiceModel->SELLER_ADDRESS = $this->sellerAddress;
  287. $invoiceModel->SELLER_BANK = $this->sellerBank;
  288. $invoiceModel->ITEM_NAME = $this->itemName;
  289. $invoiceModel->INVOICE_REMARK = $this->invoiceRemark;
  290. $invoiceModel->AUDIT_STATUS = \Yii::$app->params['auditStatus']['true']['value'];
  291. //能否自动流转
  292. $userInfo = UserInfo::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => $withdrawModel->USER_ID], 'REG_TYPE,SHOULD_REG_TYPE');
  293. if ($userInfo['REG_TYPE'] == $userInfo['SHOULD_REG_TYPE']) {
  294. $withdrawModel->AUDIT_STATUS = Withdraw::STATUS_AUDITED;
  295. $withdrawModel->AUDIT_ADMIN = \Yii::$app->user->id;
  296. $withdrawModel->AUDITED_AT = Date::nowTime();
  297. }
  298. }
  299. $invoiceModel->AUDIT_ADMIN = \Yii::$app->user->id;
  300. $invoiceModel->CREATE_REMARK = $this->createRemark;
  301. $invoiceModel->AUDITED_AT = Date::nowTime();
  302. if (!$invoiceModel->save()) {
  303. throw new Exception(Form::formatErrorsForApi($invoiceModel->getErrors()));
  304. }
  305. if (!$withdrawModel->save()) {
  306. throw new Exception(Form::formatErrorsForApi($withdrawModel->getErrors()));
  307. }
  308. $transaction->commit();
  309. } catch (Exception $e) {
  310. $transaction->rollBack();
  311. $this->addError('audit', $e->getMessage());
  312. return null;
  313. }
  314. return $invoiceModel;
  315. }
  316. /**
  317. * 删除
  318. * @param $selected
  319. * @throws Exception
  320. */
  321. public static function delete($selected) {
  322. foreach ($selected as $select) {
  323. $withdraw = Withdraw::findOneAsArray('INVOICE_ID=:INVOICE_ID', [':INVOICE_ID' => $select], 'SN,AUDIT_STATUS');
  324. if ($withdraw['AUDIT_STATUS'] != Withdraw::STATUS_APPLIED) {
  325. throw new Exception('发票所属提现流水号' . $withdraw['SN'] . '非 未传发票/待审核 状态,无法删除');
  326. }
  327. }
  328. }
  329. }