diff --git a/app/command/ExportExcel.php b/app/command/ExportExcel.php new file mode 100644 index 0000000..985d49c --- /dev/null +++ b/app/command/ExportExcel.php @@ -0,0 +1,104 @@ +setName('exportexcel') + ->setDescription('the exportexcel command'); + } + + protected function execute(Input $input, Output $output) + { + + set_time_limit(0); // 无时间限制 + ini_set('memory_limit', '512M'); // 增加内存限制 + $this->runData(); + // 指令输出 + $output->writeln('exportexcel'); + } + + protected function runData() + { + $filename = 'export_data.xls'; + $path = public_path() . '/' . $filename; + $file = new File($path); + + $fileInfo = []; + //$fileInfo['name'] = $file->getOriginalName(); //上传文件名 + //$fileInfo['type'] = $file->getOriginalMime(); //上传文件类型信息 + $fileInfo['tmp_name'] = $file->getPathname(); + //$fileInfo['size'] = $file->getSize(); + $obj = \PHPExcel_IOFactory::createReader('Excel5'); + $obj_PHPExcel = $obj->load($fileInfo['tmp_name']); + $excel_array = $obj_PHPExcel->getSheet(0)->toArray(); + + $error_array = []; + foreach ($excel_array as $key => $data) { + $key = $key + 1; + $phone = $data[0]; + $email = $data[1] ?? ''; + $pucode = $data[2]; + $taxId = $data[3] ?? ''; + $type = $data[4]; + // 验证不能为空 + if (empty($phone) || empty($pucode) || empty($type)) { + continue; + } + if (!preg_match('/^1[3-9]\d{9}$/', $phone)) { + $error_array[] = "第{$key}行:手机号格式不符合"; + continue; + } + + $FeeService = new FeeService($pucode); + $feeUserInfo = $FeeService->getUsers(); + + if ($feeUserInfo['UserCode'] != $pucode) { + $error_array[] = "第{$key}行:用户编号不存在"; + continue; + } + if ($feeUserInfo['CellPhone'] != $phone) { + $error_array[] = "第{$key}行:手机号或用户编号错误"; + continue; + } + if ($email) { + if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) { + $error_array[] = "第{$key}行:邮箱格式不符合"; + continue; + } + } + if ($taxId) { +// $str = '0-9A-HJ-NPQRTUWXY'; +// $pattern = '/^[' . $str . ']{2}\d{6}[' . $str . ']{10}$/'; +// if (!preg_match($pattern, $taxId)) { +// $error_array[] = "第{$key}行:纳税人编号格式不符合"; +// continue; +// } + } + $typeArr = ['单位', '个人']; + if (!in_array($type, $typeArr)) { + $error_array[] = "第{$key}行:抬头类型不符合"; + continue; + } + $type = array_search($type, $typeArr); + UserService::addUser((int)$phone, $email, $pucode, $taxId, (int)$type); + } + + if (!empty($error_array)) { + \think\facade\Log::info(json_encode($error_array)); + } + } +} diff --git a/app/controller/admin/WechatUser.php b/app/controller/admin/WechatUser.php index 453890f..c479c84 100644 --- a/app/controller/admin/WechatUser.php +++ b/app/controller/admin/WechatUser.php @@ -126,6 +126,9 @@ class WechatUser extends Base $excel_array = $obj_PHPExcel->getSheet(0)->toArray(); array_shift($excel_array); + set_time_limit(0); // 无时间限制 + ini_set('memory_limit', '512M'); // 增加内存限制 + $error_array = []; foreach ($excel_array as $key => $data) { $key = $key + 1; diff --git a/app/controller/api/InvoiceIssuance.php b/app/controller/api/InvoiceIssuance.php index a627a20..000695d 100644 --- a/app/controller/api/InvoiceIssuance.php +++ b/app/controller/api/InvoiceIssuance.php @@ -47,7 +47,7 @@ class InvoiceIssuance extends Base $wechat_user_id = $this->request->wechat_user_id; $WechatPucode = new WechatPucode(); - $pucode = $WechatPucode->where('wechat_user_id',$wechat_user_id)->order('create_time desc')->value('pucode'); + $pucodeArr = $WechatPucode->where('wechat_user_id',$wechat_user_id)->order('create_time desc')->column('pucode'); $projectArr = InvoiceIssuanceModel::$projectArr; @@ -62,7 +62,7 @@ class InvoiceIssuance extends Base } return $this->buildSuccess([ - 'pucode' => $pucode ?: '', + 'pucode' => $pucodeArr ?: [], 'project_arr' => $keyArr ]); } @@ -130,23 +130,38 @@ class InvoiceIssuance extends Base { try { $wechat_user_id = $this->request->wechat_user_id; + $pucode = $this->request->get('pucode'); + $data = [ + 'email' => '', + 'tax_number' => '', + 'title' => '', + 'type' => '' + ]; $field = 'type, title, tax_number'; $where = [ ['wechat_user_id', '=', $wechat_user_id], + ['pucode', '=', $pucode], ['tax_number', '<>', ''], ]; - $data = (new InvoiceHead())->where($where)->field($field)->order('id', 'DESC')->find(); + // 获取抬头 + $InvoiceHead = (new InvoiceHead())->where($where)->field($field)->order('id', 'DESC')->find()->toArray(); + if ($InvoiceHead) { + $data['type'] = $InvoiceHead['type']; + $data['title'] = $InvoiceHead['title']; + $data['tax_number'] = $InvoiceHead['tax_number']; + } + // 获取邮箱 $userRes = (new WechatUser())->where('id', $wechat_user_id)->find(); $data['email'] = $userRes['email']; - $feeUserInfo = []; - if (!isset($data['tax_number']) || empty($userRes['email'])) { - $pucode = (new WechatPucode())->where('wechat_user_id', $wechat_user_id)->value('pucode'); - - $feeUserInfo = (new InvoiceIssuanceService())->getFeeUserData($pucode); + // 直接获取发票 + $pucode = (new WechatPucode())->where('wechat_user_id', $wechat_user_id)->where('pucode', $pucode)->value('pucode'); + if (!$pucode) { + throw new \Exception('用户编号错误'); } + $feeUserInfo = (new InvoiceIssuanceService())->getFeeUserData($pucode); if (empty($data['email'])) { $data['email'] = $feeUserInfo['EMail'] ?? ''; @@ -158,17 +173,17 @@ class InvoiceIssuance extends Base } } - if (!isset($data['tax_number'])) { - - $data['title'] = $feeUserInfo['UserName'] ?? ''; + if ($feeUserInfo) { + $data['title'] = $feeUserInfo['UserName'] ?? $data['title']; $data['type'] = 0; if ($feeUserInfo['UserCategoryID'] == '普通居民') { $data['type'] = 1; } - $data['tax_number'] = !empty($feeUserInfo['CertificateCode']) ? $feeUserInfo['CertificateCode'] : ''; - } else { - $data = $data->toArray(); + if (!empty($feeUserInfo['CertificateCode']) ) { + $data['tax_number'] = $feeUserInfo['CertificateCode']; + } } + return $this->buildSuccess($data); } catch (\Exception $e) { return $this->buildFailed(ReturnCode::NOT_EXISTS, $e->getMessage()); @@ -225,7 +240,7 @@ class InvoiceIssuance extends Base //验证 、 新建抬头、获取抬头id validate(InvoiceHeadValidate::class)->scene('type')->check($params); validate(InvoiceHeadValidate::class)->scene('type'.$params['type'])->check($params); - $data['invoice_head_id'] = InvoiceHead::createHead($wechat_user_id, $params); + $data['invoice_head_id'] = InvoiceHead::createHead($wechat_user_id, $pucode, $params); if ($id) { validate(InvoiceIssuanceValidate::class)->scene('edit')->check($params); diff --git a/app/model/InvoiceHead.php b/app/model/InvoiceHead.php index 7fb15b3..a279171 100644 --- a/app/model/InvoiceHead.php +++ b/app/model/InvoiceHead.php @@ -14,11 +14,12 @@ class InvoiceHead extends Model /** * * @param $wechat_user_id + * @param $pucode * @param $data * @return mixed * @throws FuncException */ - public static function createHead($wechat_user_id, $data) + public static function createHead($wechat_user_id, $pucode, $data) { $feeUserInfo = (new InvoiceIssuanceService())->getFeeUserData($data['pucode']); @@ -28,6 +29,7 @@ class InvoiceHead extends Model $param = [ 'tax_number' => $tax_number, + 'pucode' => $pucode, 'wechat_user_id' => $wechat_user_id ]; @@ -44,6 +46,7 @@ class InvoiceHead extends Model $param['bank_name'] = isset($feeUserInfo['BankName']) && !empty($feeUserInfo['BankName']) ? $feeUserInfo['BankName']: ''; $param['bank_account'] = isset($feeUserInfo['BankAccountCode']) && !empty($feeUserInfo['BankAccountCode']) ? $feeUserInfo['BankAccountCode']: ''; $param['create_time'] = time(); + $param['pucode'] = $pucode; return self::insertGetId($param); diff --git a/app/service/user/UserService.php b/app/service/user/UserService.php index 5779c6f..967b7bb 100644 --- a/app/service/user/UserService.php +++ b/app/service/user/UserService.php @@ -100,10 +100,11 @@ class UserService extends BaseService if ($tax_number) { $headQuery = (new InvoiceHead())->where($headSave)->find(); - if ($headQuery) { + if ($headQuery && ($headQuery['pucode'] == $pucode || empty($headQuery['pucode']))) { $headQuery->type = $type; $headQuery->tax_number = $tax_number; $headQuery->phone = $phone; + $headQuery->pucode = $pucode; $headQuery->create_time = time(); $headQuery->save(); } else { @@ -111,6 +112,7 @@ class UserService extends BaseService $headSave['type'] = $type; $headSave['tax_number'] = $tax_number; $headSave['phone'] = $phone; + $headSave['pucode'] = $pucode; $headSave['create_time'] = time(); (new InvoiceHead())->save($headSave); }