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

113 lines
3.4 KiB

<?php
namespace app\controller\api;
use app\model\InvoiceHead;
use app\model\InvoiceIssuance as InvoiceIssuanceModel;
use app\service\invoice\InvoiceIssuanceService;
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());
}
}
public function add()
{
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_id' => $params['pucode_id'],
];
$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());
}
}
public function delete()
{
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());
}
}
}