diff --git a/app/controller/api/InvoiceIssuance.php b/app/controller/api/InvoiceIssuance.php index bb4d94d..f4c35e7 100644 --- a/app/controller/api/InvoiceIssuance.php +++ b/app/controller/api/InvoiceIssuance.php @@ -7,7 +7,6 @@ use app\model\InvoiceIssuance as InvoiceIssuanceModel; use app\model\InvoiceIssuanceData; use app\model\WechatPucode; use app\service\invoice\InvoiceIssuanceService; -use app\service\webService\FeeService; use app\util\ReturnCode; use app\validate\InvoiceHeadValidate; use app\validate\InvoiceIssuanceValidate; @@ -130,29 +129,26 @@ class InvoiceIssuance extends Base try { $wechat_user_id = $this->request->wechat_user_id; - $pucode = (new WechatPucode())->where('wechat_user_id', $wechat_user_id)->value('pucode'); - - $FeeService = new FeeService($pucode); - $feeUserInfo = $FeeService->getUsers(); - if (!$feeUserInfo) { - throw new \Exception('用户信息获取失败'); - } - $data = [ - 'title' => $feeUserInfo['UserName'] ?? '', + $field = 'type, title, tax_number'; + $where = [ + ['wechat_user_id', '=', $wechat_user_id], + ['tax_number', '<>', ''], ]; - $data['type'] = 0; - if ($feeUserInfo['UserCategoryID'] == '普通居民') { - $data['type'] = 1; - } - $data['tax_number'] = empty($feeUserInfo['CertificateCode']) ? $feeUserInfo['CertificateCode'] : ''; + $data = (new InvoiceHead())->where($where)->field($field)->order('id', 'DESC')->find(); + if (!$data) { + $pucode = (new WechatPucode())->where('wechat_user_id', $wechat_user_id)->value('pucode'); -// if ($data['type'] === 0) { -// $data['address'] = $feeUserInfo['MaillingAddress']; -// $data['telephone'] = $feeUserInfo['Telephone']; -// $data['bank_name'] = $feeUserInfo['BankName']; -// $data['bank_account'] = $feeUserInfo['BankAccountCode']; -// } + $feeUserInfo = (new InvoiceIssuanceService())->getFeeUserData($pucode); + $data = [ + 'title' => $feeUserInfo['UserName'] ?? '', + ]; + $data['type'] = 0; + if ($feeUserInfo['UserCategoryID'] == '普通居民') { + $data['type'] = 1; + } + $data['tax_number'] = empty($feeUserInfo['CertificateCode']) ? $feeUserInfo['CertificateCode'] : ''; + } return $this->buildSuccess($data); } catch (\Exception $e) { return $this->buildFailed(ReturnCode::NOT_EXISTS, $e->getMessage()); @@ -200,7 +196,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, $params); if ($id) { validate(InvoiceIssuanceValidate::class)->scene('edit')->check($params); diff --git a/app/model/InvoiceHead.php b/app/model/InvoiceHead.php index 529b488..c77e3c0 100644 --- a/app/model/InvoiceHead.php +++ b/app/model/InvoiceHead.php @@ -2,6 +2,8 @@ namespace app\model; +use app\service\invoice\InvoiceIssuanceService; +use fast\FuncException; use think\Model; class InvoiceHead extends Model @@ -9,14 +11,22 @@ class InvoiceHead extends Model public static $typeArr = ['单位', '个人']; + /** + * + * @param $wechat_user_id + * @param $data + * @return mixed + * @throws FuncException + */ public static function createHead($wechat_user_id, $data) { - if (isset($data['tax_number']) && $data['tax_number'] == 'undefined') $data['tax_number'] = ''; + $feeUserInfo = (new InvoiceIssuanceService())->getFeeUserData($data['pucode']); + $param = [ 'type' => $data['type'], - 'title' => $data['title'], - 'tax_number' => $data['tax_number'], + 'title' => $feeUserInfo['UserName'] ?? '', + 'tax_number' => $feeUserInfo['CertificateCode'] ?? '', 'wechat_user_id' => $wechat_user_id ]; @@ -25,10 +35,10 @@ class InvoiceHead extends Model return $id; } - $param['address'] = $data['address'] ?? ''; - $param['telephone'] = $data['telephone'] ?? ''; - $param['bank_name'] = $data['bank_name'] ?? ''; - $param['bank_account'] = $data['bank_account'] ?? ''; + $param['address'] = $feeUserInfo['MaillingAddress'] ?? ''; + $param['telephone'] = $feeUserInfo['Telephone'] ?? ''; + $param['bank_name'] = $feeUserInfo['BankName'] ?? ''; + $param['bank_account'] = $feeUserInfo['BankAccountCode'] ?? ''; $param['create_time'] = time(); diff --git a/app/service/invoice/InvoiceIssuanceService.php b/app/service/invoice/InvoiceIssuanceService.php index eb76346..f9179a0 100644 --- a/app/service/invoice/InvoiceIssuanceService.php +++ b/app/service/invoice/InvoiceIssuanceService.php @@ -190,7 +190,7 @@ class InvoiceIssuanceService * @return array|mixed * @throws FuncException */ - private function getFeeUserData($pucode) + public function getFeeUserData($pucode) { $FeeService = new FeeService($pucode); $feeUsers = $FeeService->getUsers();