request->post(); $list = InvoiceIssuanceService::getListPage($params, $this->request->wechat_user_id); return $this->buildSuccess($list); } catch (\Exception $e) { return $this->buildFailed(ReturnCode::UPDATE_FAILED, $e->getMessage()); } } /** * 获取申请前数据 * @return Response */ public function getApplyData(): Response { $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'); $projectArr = InvoiceIssuanceModel::$projectArr; unset($projectArr[0]); $keyArr = []; foreach ($projectArr as $key => $value) { $keyArr[] = [ 'value' => $key, 'text' => $value ]; } return $this->buildSuccess([ 'pucode' => $pucode ?: '', 'project_arr' => $keyArr ]); } /** * 验证是否缴费 并返回金额 * @return Response */ public function validateFeePay(): Response { try { $param = $this->request->post(); validate(InvoiceIssuanceValidate::class)->scene('feePay')->check($param); $res = InvoiceIssuanceService::validateFeePay($param['pucode'],$param['expire_time'],$param['project_id']); return $this->buildSuccess($res); } catch (\Exception $e) { return $this->buildFailed(ReturnCode::NOT_EXISTS, $e->getMessage()); } } /** * 获取发票抬头 * @return Response */ public function getInvoiceHead(): Response { try { $wechat_user_id = $this->request->wechat_user_id; $param = $this->request->get(); validate(InvoiceHeadValidate::class)->scene('type')->check($param); $where = [ 'wechat_user_id' => $wechat_user_id, 'type' => $param['type'] ]; $field = 'title,tax_number'; if (!$param['type']) { $field .= ',address,telephone,bank_name,bank_account'; } $InvoiceHead = new InvoiceHead; $data = $InvoiceHead->where($where)->field($field)->find(); if (!$data) { throw new \Exception('数据不存在'); } return $this->buildSuccess($data->toArray()); } catch (\Exception $e) { return $this->buildFailed(ReturnCode::NOT_EXISTS, $e->getMessage()); } } /** * 获取抬头信息 * @return Response */ public function getFeeInvoiceHead(): Response { try { $wechat_user_id = $this->request->wechat_user_id; $field = 'type, title, tax_number'; $where = [ ['wechat_user_id', '=', $wechat_user_id], ['tax_number', '<>', ''], ]; $data = (new InvoiceHead())->where($where)->field($field)->order('id', 'DESC')->find(); $userRes = (new WechatUser())->where('id', $wechat_user_id)->find(); $data['email'] = $userRes['email']; $pucode = (new WechatPucode())->where('wechat_user_id', $wechat_user_id)->value('pucode'); $feeUserInfo = (new InvoiceIssuanceService())->getFeeUserData($pucode); if ($data['title'] != $feeUserInfo['UserName'] && isset($data['tax_number'])) { unset($data['tax_number']); } if (empty($data['email'])) { $data['email'] = $feeUserInfo['EMail'] ?? ''; if (!empty($data['email'])) { $userRes->email = $data['email']; $userRes->save(); } else { $data['email'] = ''; } } if (!isset($data['tax_number'])) { $data['title'] = $feeUserInfo['UserName'] ?? ''; $data['type'] = 0; if ($feeUserInfo['UserCategoryID'] == '普通居民') { $data['type'] = 1; } $data['tax_number'] = !empty($feeUserInfo['CertificateCode']) ? $feeUserInfo['CertificateCode'] : ''; } else { $data = $data->toArray(); } return $this->buildSuccess($data); } catch (\Exception $e) { return $this->buildFailed(ReturnCode::NOT_EXISTS, $e->getMessage()); } } /** * 新增、编辑 * @return Response */ public function add(): Response { try { $params = $this->request->post(); $id = $params['id'] ?? 0; $wechat_user_id = $this->request->wechat_user_id; $pucode = $param['pucode'] ?? ''; if (!$pucode) { $pucode = WechatPucode::where('wechat_user_id', $wechat_user_id)->order('create_time desc')->value('pucode'); if (!$pucode) throw new \Exception('请先绑定用户编号'); if (isset($params['pucode']) && $pucode != $params['pucode']) { throw new \Exception('用户编号填写错误'); } } $data = [ 'wechat_user_id' => $wechat_user_id, 'project_id' => $params['project_id'], 'pucode' => $pucode, 'expire_time' => strtotime($params['expire_time']), ]; $where = [['status', 'in', [0,1,3]], ['delete_time', '=', 0]]; if ($id) { $where[] = ['id', '!=', $id]; } Db::startTrans(); $query_id = InvoiceIssuanceModel::where($data)->where($where)->value('id'); if ($query_id) { throw new \Exception('当前开票项目已申请开票'); } $data['expire_time'] = strtotime($params['expire_time']); $data['mobile'] = $params['mobile']; $data['email'] = $params['email']; $data['amount'] = $params['amount']; //验证 、 新建抬头、获取抬头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); if ($id) { validate(InvoiceIssuanceValidate::class)->scene('edit')->check($params); $data['update_time'] = time(); $status = (new InvoiceIssuanceModel())->where('id', $id)->value('status'); if ($status == 2) { $data['status'] = 1; } (new InvoiceIssuanceModel())->update($data, ['id' => $id]); } else { validate(InvoiceIssuanceValidate::class)->scene('add')->check($params); $data['create_time'] = time(); $data['serial_number'] = (new InvoiceIssuanceService())->getSerialNumber(); (new InvoiceIssuanceModel())->save($data); } Db::commit(); return $this->buildSuccess(); } catch (\Exception $e) { Db::rollback(); return $this->buildFailed(ReturnCode::UPDATE_FAILED, $e->getMessage()); } } /** * 删除 * @return Response */ public function delete(): Response { try { $params = $this->request->post(); validate(InvoiceIssuanceValidate::class)->scene('delete')->check($params); $id = $params['id']; (new InvoiceIssuanceModel())->update(['delete_time' => time()], ['id' => $id]); return $this->buildSuccess(); } catch (\Exception $e) { return $this->buildFailed(ReturnCode::DELETE_FAILED, $e->getMessage()); } } /** * 获取二维码 * @return Response * @throws \Exception */ public function getQrCode(): Response { // 验证 $id = $this->request->get('id', ''); if (empty($id)) { throw new \Exception('缺少必传参数'); } // 生成 $qrCodeImage = InvoiceQrCode::getQrCode($id); // 返回 return $this->buildSuccess(['qrCodeImage' => $qrCodeImage]); } public function getErrorInfo() { try { // 验证 $id = $this->request->get('id', ''); if (empty($id)) { throw new \Exception('缺少必传参数'); } $status = InvoiceIssuanceModel::where('id', $id)->value('status'); if ($status != 2) { throw new \Exception('失败原因获取失败'); } $ycms = InvoiceIssuanceData::where(['invoice_issuance_id' => $id, 'status' => 1])->value('ycms'); if (!$ycms) { throw new \Exception('暂无失败原因'); } return $this->buildSuccess(['content' => $ycms]); } catch (\Exception $e) { return $this->buildFailed(ReturnCode::NOT_EXISTS,$e->getMessage()); } } public function downFile() { try { // 验证令牌 $token = $this->request->get('token', ''); if (empty($token)) { throw new \Exception('缺少必传参数'); } $checkRes = InvoiceQrCode::checkToken($token); if ($checkRes['code'] != 1) { throw new FuncException($checkRes['msg']); } // 获取文件id $id = $checkRes['data']['invoice_issuance_id']; if (empty($id)) { throw new \Exception('缺少必传参数'); } $InvoiceIssuanceData = (new InvoiceIssuanceData)->where(['invoice_issuance_id' => $id, 'status' => 1])->find(); if (!$InvoiceIssuanceData) { throw new FuncException('PDF文件下载失败'); } $filePath = $InvoiceIssuanceData['pdf_filepath']; $file_dir = App::getRootPath() . 'public/' . $filePath; if (file_exists($file_dir)) { // 打开文件 $file1 = fopen($file_dir, "r"); //文件名,自定义名称+文件后缀 $file_houzhui = strrchr($file_dir, '.'); $file_name = time().rand(0000,9999).$file_houzhui; // 输入文件标签 Header("Content-type: application/octet-stream"); Header("Accept-Ranges: bytes"); Header("Accept-Length:".filesize($file_dir)); Header("Content-Disposition: attachment;filename=" . $file_name);//自定义文件名 ob_clean(); // 重点!!! flush(); // 重点!!!!可以清除文件中多余的路径名以及解决乱码的问题: //输出文件内容 //读取文件内容并直接输出到浏览器 echo fread($file1, filesize($file_dir)); fclose($file1); exit(); } else { throw new \Exception('文件不存在'); } } catch (\Exception $e) { return $this->buildFailed(ReturnCode::NOT_EXISTS, $e->getMessage()); } } }