diff --git a/app/controller/api/Test.php b/app/controller/api/Test.php index 5960eb9..7d6e51f 100644 --- a/app/controller/api/Test.php +++ b/app/controller/api/Test.php @@ -40,20 +40,14 @@ class Test extends Base return $FeeService->getUsers(); } - /* - * - * ^ array:3 [▼ - "code" => "200" - "message" => "申请成功" - "pzsqkjYcxxList" => array:1 [▼ - "pzsqkjYcxxVO" => array:4 [▼ - "bdznsrsbh" => "91440300772709730N" - "yhbm" => "41107394" - "assetID" => "BC664422394E9D115C730C58AEB7BD7B" - "typeID" => "A0A35204362E8C26B40E8609C66090C2" - ] - ] -] - * - */ + public function apiInvoiceIssuance() + { + try { + $param = $this->request->param(); + $data = (new \app\service\invoice\TempInvoiceService())->IssueAnInvoice($param); + return $this->buildSuccess($data); + } catch (\Exception $e) { + return $this->buildFailed(0, $e->getMessage()); + } + } } diff --git a/app/service/invoice/InvoiceIssuanceService.php b/app/service/invoice/InvoiceIssuanceService.php index 36fb070..40d8c89 100644 --- a/app/service/invoice/InvoiceIssuanceService.php +++ b/app/service/invoice/InvoiceIssuanceService.php @@ -130,7 +130,7 @@ class InvoiceIssuanceService * @param $expire_time * @return array */ - private function getInvoiceDate($expire_time): array + public function getInvoiceDate($expire_time): array { $expire_time = strtotime(date("Y-m",$expire_time)); return [ diff --git a/app/service/invoice/TempInvoiceService.php b/app/service/invoice/TempInvoiceService.php new file mode 100644 index 0000000..41d1711 --- /dev/null +++ b/app/service/invoice/TempInvoiceService.php @@ -0,0 +1,138 @@ + '', + 'ycms' => '' + ]; + try { + $InvoiceIssuanceService = new InvoiceIssuanceService(); + // 用户编码 + $pucode = $invoiceIssuance['pucode']; + + if (empty($invoiceIssuance['expire_time'])) { + throw new FuncException('申请日期不能为空'); + } + + $invoiceDate = $InvoiceIssuanceService->getInvoiceDate($invoiceIssuance['expire_time']); + + $feeUsers = $InvoiceIssuanceService->getFeeUserData($pucode); + + $bdznsrsbh = $InvoiceIssuanceService->getTaxNumber(0); // 被代征纳税人识别号 + + $feeUsers['bdznsrsbh'] = $bdznsrsbh; + + // 获取用户应收费信息 + $FeeComputeDetail = InvoiceIssuanceService::getFeeComputeDetail($pucode, date("Ym", $invoiceIssuance['expire_time'])); + if ($FeeComputeDetail['MsgID'] != 1) { + throw new FuncException($FeeComputeDetail); + } + + $feeUsers['jsyj'] = $FeeComputeDetail['WaterAmount']; + $feeUsers['zsfsmc'] = $FeeComputeDetail['chargetype']; + $feeUsers['jfrq'] = $FeeComputeDetail['WaterPayDate']; + + // 保存 + $ChinaTaxes = new ChinaTaxes($feeUsers, $invoiceDate); + $savingDetailedData = $ChinaTaxes->savingDetailedData(); + + // 批次号 + $sbpch = $savingDetailedData['sbpch']; + $returnData['sbpch'] = $sbpch; + + // 委托 - 加工数据 + $realTimeProcessing = $ChinaTaxes->realTimeProcessing($sbpch); + + if ($realTimeProcessing['code'] == 200) { + // 处理加工成功返回数据 + if (!isset($realTimeProcessing['data']['mxGrid'])) { + throw new FuncException('加工数据有误'); + } + $mxGrid = $realTimeProcessing['data']['mxGrid']; + + $jsyj = 0; // 总金额 + if (isset($mxGrid['sbMxsjVOList'])) { + foreach ($mxGrid['sbMxsjVOList'] as $sbMxsjVOValue) { + if (isset($sbMxsjVOValue['jsyj'])) $jsyj += $sbMxsjVOValue['jsyj']; + } + } + + // 加工成功 电子缴款申请 + $issueElectronic = $ChinaTaxes->issueElectronic($sbpch, $bdznsrsbh); + + if (!isset($issueElectronic['pzsqkjYcxxList']['pzsqkjYcxxVO']['assetID'])) { + throw new FuncException('文件唯一标识不存在'); + } + $assetID = $issueElectronic['pzsqkjYcxxList']['pzsqkjYcxxVO']['assetID']; // 文件唯一标识 + + // 查询电子缴款凭证上链信息 + $queryPaymentVoucher = $ChinaTaxes->queryPaymentVoucher($assetID); + + //$mmq = $queryPaymentVoucher['mmq']; + $meta = $queryPaymentVoucher['meta']; + $data = $queryPaymentVoucher['data']; + + // 拼接pdf + // 盖章 + $requestData = ['data' => $data, 'meta' => $meta]; + $deCompress = $InvoiceIssuanceService->deCompress($requestData); + if ($deCompress['status'] != 200) { + throw new FuncException('pdf数据解密失败'); + } + if (!isset($deCompress['data']['pdfdata'])) { + throw new FuncException('pdf文件合并失败'); + } + $pdfFilepath = $deCompress['data']['pdfdata'];//$this->savePdfFile($deCompress['data']['pdfdata']); + + $returnData['i_i_d_data'] = [ + 'i_i_id' => $invoiceIssuance['id'], + 'bdznsrsbh' => $bdznsrsbh, + 'jsyj' => $jsyj, + 'assetID' => $assetID, + 'pdfFilepath' => $pdfFilepath, + ]; + + $returnData['i_i_data'] = [ + 'status' => 3, + 'issuance_time' => time() + ]; + } else if ($realTimeProcessing['code'] == 302) { + // 加工失败 + + // 查询上传异常数据接口 + $queryUploadErrorData = $ChinaTaxes->queryUploadErrorData($sbpch); + if (!isset($queryUploadErrorData['mxGrid']['sbMxsjVOList']['ycms'])) { + throw new FuncException('异常信息返回有误'); + } + // 查询待开具电子缴款 + $ChinaTaxes->queryIssuedPaymentVoucher($bdznsrsbh); + + // 保存用户异常状态 + $ycms = $queryUploadErrorData['mxGrid']['sbMxsjVOList']['ycms']; // 数据异常描述 + $returnData['ycms'] = $ycms; + + // 开票失败 + $returnData['i_i_data'] = ['status' => 2]; + //throw new FuncException('加工失败流程待定!'); + } + } catch (\Exception $e) { + $returnData['ycms'] = $e->getMessage(); + } + return $returnData; + } +} diff --git a/route/apiRoute.php b/route/apiRoute.php index c75b039..7d6a2bd 100644 --- a/route/apiRoute.php +++ b/route/apiRoute.php @@ -18,6 +18,7 @@ Route::group('api', function() { // 测试FeeService 接口 Route::rule('Test/index', 'api.Test/index', 'get'); Route::rule('Test1/index', 'api.Test1/index', 'get'); + Route::rule('Test/index', 'api.Test/apiInvoiceIssuance', 'post'); Route::rule('InvoiceIssuance/downFile', 'api.InvoiceIssuance/downFile', 'get'); //MISS路由定义 //Route::miss('api.Miss/index');