发票管理apiadmin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

162 lines
6.6 KiB

<?php
namespace app\controller\admin;
use app\model\WechatPucode;
use app\service\invoice\InvoiceIssuanceService;
use app\util\ReturnCode;
use think\Response;
use app\model\InvoiceHead as InvoiceHeadModel;
use app\model\WechatUser as WechatUserModel;
class InvoiceHead extends Base
{
/**
* 获取发票申请列表
* @return Response
* @throws \think\db\exception\DbException
*/
public function index(): Response
{
$param = $this->request->get();
$limit = $this->request->get('size', config('apiadmin.ADMIN_LIST_DEFAULT'));
$start = $this->request->get('page', 1);
$where = [];
if (isset($param['pucode']) && !empty($param['pucode'])) {
$where[] = ['pucode', 'in', explode(',',$param['pucode'])];
}
$InvoiceHead = new InvoiceHeadModel();
$listObj = $InvoiceHead->where($where)
->order('create_time', 'DESC')
->paginate(['page' => $start, 'list_rows' => $limit])
->each(function ($item, $key) use ($InvoiceHead) {
$item['type'] = InvoiceHeadModel::$typeArr[$item['type']];
$WechatUser = WechatUserModel::where('id', $item['wechat_user_id'])->find();
if ($WechatUser) {
$item['wechat_user_name'] = $WechatUser['nickname'];
$item['headimgurl'] = $WechatUser['headimgurl'];
$item['phone'] = $WechatUser['phone'];
}
unset($item['wechat_user_id']);
})->toArray();
$listInfo = $listObj['data'];
return $this->buildSuccess([
'list' => $listInfo,
'count' => $listObj['total']
]);
}
public function save(): Response
{
try {
$params = $this->request->post();
$id = $params['id'] ?? '';
$phone = $params['phone'];
$pucode = $params['pucode'];
$type = $params['type'];
$tax_number = $params['tax_number'];
$title = $params['title'];
// 数据验证
$tax_number_str = $type == 1 ? '身份证号' :'纳税识别号';
$tax_number_rule = $type == 1 ? 'idCard' :'alphaNum';
$rule = [
'phone|手机号' => 'require|mobile',
'type|抬头类型' => 'require|number|in:0,1',
'title|抬头' => 'require',
"tax_number|{$tax_number_str}" => "require|{$tax_number_rule}|length:18"
];
validate()->rule($rule)->check($params);
if ($type != 1) {
// 验证纳税识别号
$str = '0-9A-HJ-NPQRTUWXY';
$pattern = '/^[' . $str . ']{2}\d{6}[' . $str . ']{10}$/';
if (!preg_match($pattern, $tax_number)) {
throw new \Exception('纳税人编号格式不符合', ReturnCode::PARAM_INVALID);
}
}
$wechat_user_id = (new WechatUserModel())->where(['phone' => $phone, 'delete_time' => 0])->value('id');
if (!$wechat_user_id) {
throw new \Exception('当前手机号用户不存在', ReturnCode::RECORD_NOT_FOUND);
}
$feeUserInfo = (new InvoiceIssuanceService())->getFeeUserData($pucode);
$address = isset($feeUserInfo['MaillingAddress']) && !empty($feeUserInfo['MaillingAddress']) ? $feeUserInfo['MaillingAddress']: '';
$telephone = isset($feeUserInfo['Telephone']) && !empty($feeUserInfo['Telephone']) ? $feeUserInfo['Telephone']: '';
$bank_name = isset($feeUserInfo['BankName']) && !empty($feeUserInfo['BankName']) ? $feeUserInfo['BankName']: '';
$bank_account = isset($feeUserInfo['BankAccountCode']) && !empty($feeUserInfo['BankAccountCode']) ? $feeUserInfo['BankAccountCode']: '';
// 验证手机号和用户编号是否有关联
if ($telephone) {
$telephoneArr = explode(',', $telephone);
if (!in_array($phone, $telephoneArr)) {
throw new \Exception('当前手机号未关联输入的用户编号', ReturnCode::RECORD_NOT_FOUND);
}
}
// 保存操作
$query = [
'pucode' => $pucode,
'wechat_user_id' => $wechat_user_id,
];
$InvoiceHeadId = (new InvoiceHeadModel())->where($query)->value('id');
if (!$id && $InvoiceHeadId) $id = $InvoiceHeadId;
if ($id) {
if (!is_numeric($id)) {
throw new \Exception('编号数据错误', ReturnCode::RECORD_NOT_FOUND);
}
$InvoiceHeadData = (new InvoiceHeadModel())->where(['id' => $id])->find();
if (!$InvoiceHeadData) {
throw new \Exception('数据不存在', ReturnCode::RECORD_NOT_FOUND);
}
// 更新
$InvoiceHeadData->pucode = $pucode;
$InvoiceHeadData->type = $type;
$InvoiceHeadData->title = $title;
$InvoiceHeadData->tax_number = $tax_number;
if (!$InvoiceHeadData->address) {
$InvoiceHeadData->address = $address;
}
if (!$InvoiceHeadData->telephone) {
$InvoiceHeadData->telephone = $telephone;
}
if (!$InvoiceHeadData->bank_name) {
$InvoiceHeadData->bank_name = $bank_name;
}
if (!$InvoiceHeadData->bank_account) {
$InvoiceHeadData->bank_account = $bank_account;
}
$InvoiceHeadData->save();
} else {
// 新增
$data['type'] = $type;
$data['title'] = $title;
$data['tax_number'] = $tax_number;
$data['address'] = $address;
$data['telephone'] = $telephone;
$data['bank_name'] = $bank_name;
$data['bank_account'] = $bank_account;
$data['wechat_user_id'] = $wechat_user_id;
$data['create_time'] = time();
$data['pucode'] = $pucode;
InvoiceHeadModel::create($data);
}
return $this->buildSuccess();
} catch (\Exception $e) {
return $this->buildFailed($e->getCode(), $e->getMessage());
}
}
}