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

223 lines
6.2 KiB

<?php
namespace app\controller\api;
use app\model\InvoiceHead;
use app\model\InvoiceIssuance as InvoiceIssuanceModel;
use app\model\WechatPucode;
use app\service\invoice\InvoiceIssuanceService;
use app\service\wechat\WechatService;
use app\util\ReturnCode;
use app\validate\InvoiceHeadValidate;
use app\validate\InvoiceIssuanceValidate;
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'],
];
$where = [['status', '>', 0], ['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();
$InvoiceModel = (new InvoiceIssuanceModel())->save($data);
if (!$InvoiceModel) {
throw new \Exception('申请失败');
}
}
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
*/
public function getQrCode(): Response
{
// 验证
// 生成
$qrCodeImage = InvoiceIssuanceService::getQrCode();
// 返回
return $this->buildSuccess(['qrCodeImage' => $qrCodeImage]);
}
}