| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2018/1/4/004
- * Time: 14:25
- */
- namespace backend\models\forms;
- use backend\models\RemitApi;
- use common\helpers\Utils;
- use yii\base\Model;
- use yii\web\UploadedFile;
- class ImportRemitOrderForm extends Model
- {
- /**
- * @var UploadedFile
- */
- public $file;
- private $_outRemitResult;
- /**
- * @return array
- */
- public function rules()
- {
- return [
- ['file', 'file'],
- ];
- }
- /**
- * 导入财务打款审核
- * @return bool
- */
- public function import()
- {
- set_time_limit(0);
- ini_set('memory_limit','1024M');
- if ($this->validate()) {
- $api = new RemitApi();
- $webRoot = \Yii::getAlias('@webroot');
- if ($this->file instanceof UploadedFile) {
- // 文件名格式:XXXX_付款方式_批次号.xls/xlsx
- list($file_name, $suffix) = explode('.', $this->file->name);
- if (!in_array($suffix, ['xls','xlsx'])) {
- $this->addErrors(['文件后缀错误,请核对']);
- return false;
- }
- list($remitName, $pay_type, $batch_no) = explode('_', $file_name);
- if ($pay_type != "科星") {
- $this->addErrors(['打款方式不正确,请核对']);
- return false;
- }
- if (!preg_match('/^\d{4}\d{2}\d{2}\d{2}\d{2}\d{2}$/s', $batch_no)) {
- $this->addErrors(['批次号错误,请核对批次号']);
- return false;
- }
- // 批次号重复判断
- $rs = $api->checkBatchNo($batch_no);
- if ($rs['code'] == 0) {
- $this->addErrors(['批次号已存在,请核对批次号']);
- return false;
- }
- // 创建保存excel文件的目录
- $file_path = $webRoot.'/upload/remitExcel/';
- if(!is_dir($file_path)) {
- mkdir($file_path, 0777);
- chmod($file_path, 0777);
- }
- // 保存excel文件
- $filename = date('YmdHis',time()).'_'.rand(1,9999).".". $this->file->extension;
- $this->file->saveAs($file_path . $filename);
- // 文件后缀
- $format = $this->file->extension;
- if (in_array($format, ['xls','xlsx'])) {
- $excelFile = \Yii::getAlias($file_path . $filename);//获取文件名
- // 读取数据到$data数组
- $data = \PHPExcel_IOFactory::load($excelFile)->getActiveSheet()->toArray("", true, true, false);
- // 将表头干掉
- $data = array_slice($data, 1, count($data));
- //去除空行,每一行的银行卡号不能为空, 金额为空的行直接干掉
- foreach ($data as $key=>$value) {
- if (empty(implode("", $value))) {
- unset($data[$key]);
- continue;
- }
- if (empty($data[$key][2])) {
- unset($data[$key]);
- continue;
- }
- if (empty($data[$key][5])) {
- $this->addErrors(['第'.($key+1).'行银行卡号为空,请核对']);
- return false;
- }
- }
- //插入数据库操作(批量)
- $passData['batch_no'] = $batch_no;
- $passData['list'] = json_encode($data);
- $passData['admin_id'] = \Yii::$app->getUser()->getId();
- $passData['admin_name'] = \Yii::$app->getUser()->getIdentity(false)->name;
- $passData['admin_ip'] = Utils::getClientIp();
- $result = $api->importRemitOrder($passData);
- if ($result['code'] == 1) {
- $this->_outRemitResult = $result['data'];
- } else {
- if (is_array($result['message'])) {
- $this->addErrors($result['message']);
- } else {
- $this->addError('amount', $result['message']);
- }
- }
- } else {
- $this->addErrors(['不支持上传文件类型']);
- }
- } else {
- $this->addErrors(['上传错误']);
- }
- }
- return !$this->hasErrors();
- }
- /**
- * @return mixed
- */
- public function getOutRemitResult()
- {
- return $this->_outRemitResult;
- }
- }
|