发票管理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.
 
 
 

267 lines
8.1 KiB

<?php
namespace app\controller\api;
use app\model\InvoiceHead;
use app\model\InvoiceIssuance as InvoiceIssuanceModel;
use app\model\InvoiceIssuanceData;
use app\model\WechatPucode;
use app\service\invoice\InvoiceIssuanceService;
use app\util\ReturnCode;
use app\validate\InvoiceHeadValidate;
use app\validate\InvoiceIssuanceValidate;
use fast\FuncException;
use think\facade\App;
use think\facade\Db;
use think\Response;
class InvoiceIssuance extends Base
{
/**
* 获取首页数据
* @return Response
*/
public function getList(): Response
{
try {
$params = $this->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 add(): Response
{
try {
$params = $this->request->post();
$id = $params['id'] ?? 0;
$wechat_user_id = $this->request->wechat_user_id;
$data = [
'wechat_user_id' => $wechat_user_id,
'project_id' => $params['project_id'],
'pucode' => $params['pucode'],
'expire_time' => strtotime($params['expire_time']),
];
$where = [['status', '<>', 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();
(new InvoiceIssuanceModel())->update($data, ['id' => $id]);
} else {
validate(InvoiceIssuanceValidate::class)->scene('add')->check($params);
$data['create_time'] = time();
(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 = InvoiceIssuanceService::getQrCode($id);
// 返回
return $this->buildSuccess(['qrCodeImage' => $qrCodeImage]);
}
public function downFile()
{
try {
$id = $this->request->get('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());
}
}
}