ImportRemitOrderForm.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018/1/4/004
  6. * Time: 14:25
  7. */
  8. namespace backend\models\forms;
  9. use backend\models\RemitApi;
  10. use common\helpers\Utils;
  11. use yii\base\Model;
  12. use yii\web\UploadedFile;
  13. class ImportRemitOrderForm extends Model
  14. {
  15. /**
  16. * @var UploadedFile
  17. */
  18. public $file;
  19. private $_outRemitResult;
  20. /**
  21. * @return array
  22. */
  23. public function rules()
  24. {
  25. return [
  26. ['file', 'file'],
  27. ];
  28. }
  29. /**
  30. * 导入财务打款审核
  31. * @return bool
  32. */
  33. public function import()
  34. {
  35. set_time_limit(0);
  36. ini_set('memory_limit','1024M');
  37. if ($this->validate()) {
  38. $api = new RemitApi();
  39. $webRoot = \Yii::getAlias('@webroot');
  40. if ($this->file instanceof UploadedFile) {
  41. // 文件名格式:XXXX_付款方式_批次号.xls/xlsx
  42. list($file_name, $suffix) = explode('.', $this->file->name);
  43. if (!in_array($suffix, ['xls','xlsx'])) {
  44. $this->addErrors(['文件后缀错误,请核对']);
  45. return false;
  46. }
  47. list($remitName, $pay_type, $batch_no) = explode('_', $file_name);
  48. if ($pay_type != "科星") {
  49. $this->addErrors(['打款方式不正确,请核对']);
  50. return false;
  51. }
  52. if (!preg_match('/^\d{4}\d{2}\d{2}\d{2}\d{2}\d{2}$/s', $batch_no)) {
  53. $this->addErrors(['批次号错误,请核对批次号']);
  54. return false;
  55. }
  56. // 批次号重复判断
  57. $rs = $api->checkBatchNo($batch_no);
  58. if ($rs['code'] == 0) {
  59. $this->addErrors(['批次号已存在,请核对批次号']);
  60. return false;
  61. }
  62. // 创建保存excel文件的目录
  63. $file_path = $webRoot.'/upload/remitExcel/';
  64. if(!is_dir($file_path)) {
  65. mkdir($file_path, 0777);
  66. chmod($file_path, 0777);
  67. }
  68. // 保存excel文件
  69. $filename = date('YmdHis',time()).'_'.rand(1,9999).".". $this->file->extension;
  70. $this->file->saveAs($file_path . $filename);
  71. // 文件后缀
  72. $format = $this->file->extension;
  73. if (in_array($format, ['xls','xlsx'])) {
  74. $excelFile = \Yii::getAlias($file_path . $filename);//获取文件名
  75. // 读取数据到$data数组
  76. $data = \PHPExcel_IOFactory::load($excelFile)->getActiveSheet()->toArray("", true, true, false);
  77. // 将表头干掉
  78. $data = array_slice($data, 1, count($data));
  79. //去除空行,每一行的银行卡号不能为空, 金额为空的行直接干掉
  80. foreach ($data as $key=>$value) {
  81. if (empty(implode("", $value))) {
  82. unset($data[$key]);
  83. continue;
  84. }
  85. if (empty($data[$key][2])) {
  86. unset($data[$key]);
  87. continue;
  88. }
  89. if (empty($data[$key][5])) {
  90. $this->addErrors(['第'.($key+1).'行银行卡号为空,请核对']);
  91. return false;
  92. }
  93. }
  94. //插入数据库操作(批量)
  95. $passData['batch_no'] = $batch_no;
  96. $passData['list'] = json_encode($data);
  97. $passData['admin_id'] = \Yii::$app->getUser()->getId();
  98. $passData['admin_name'] = \Yii::$app->getUser()->getIdentity(false)->name;
  99. $passData['admin_ip'] = Utils::getClientIp();
  100. $result = $api->importRemitOrder($passData);
  101. if ($result['code'] == 1) {
  102. $this->_outRemitResult = $result['data'];
  103. } else {
  104. if (is_array($result['message'])) {
  105. $this->addErrors($result['message']);
  106. } else {
  107. $this->addError('amount', $result['message']);
  108. }
  109. }
  110. } else {
  111. $this->addErrors(['不支持上传文件类型']);
  112. }
  113. } else {
  114. $this->addErrors(['上传错误']);
  115. }
  116. }
  117. return !$this->hasErrors();
  118. }
  119. /**
  120. * @return mixed
  121. */
  122. public function getOutRemitResult()
  123. {
  124. return $this->_outRemitResult;
  125. }
  126. }