239 lines
8.1 KiB
TypeScript
239 lines
8.1 KiB
TypeScript
import { Controller, Get, Post, Put, Delete, Body, Param, Query, UseGuards, Request } from '@nestjs/common';
|
|
import { FinanceService } from './finance.service';
|
|
import { AuthGuard } from '@nestjs/passport';
|
|
|
|
@Controller('api/v1/finance')
|
|
export class FinanceController {
|
|
constructor(private financeService: FinanceService) {}
|
|
|
|
// 报销申请相关接口
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@Post('expense-claims')
|
|
async createExpenseClaim(@Body() data: any, @Request() req) {
|
|
data.user_id = req.user.id;
|
|
return this.financeService.createExpenseClaim(data);
|
|
}
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@Get('expense-claims')
|
|
async getExpenseClaims(@Query() filter: any, @Request() req) {
|
|
// 普通用户只能查看自己的报销申请
|
|
if (req.user.role.name !== 'admin') {
|
|
filter.user_id = req.user.id;
|
|
}
|
|
return this.financeService.getExpenseClaims(filter);
|
|
}
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@Get('expense-claims/:id')
|
|
async getExpenseClaimById(@Param('id') id: string, @Request() req) {
|
|
const expenseClaim = await this.financeService.getExpenseClaimById(id);
|
|
// 普通用户只能查看自己的报销申请
|
|
if (req.user.role.name !== 'admin' && expenseClaim.user_id !== req.user.id) {
|
|
throw new Error('Access denied');
|
|
}
|
|
return expenseClaim;
|
|
}
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@Put('expense-claims/:id/approve')
|
|
async approveExpenseClaim(@Param('id') id: string, @Body() data: any, @Request() req) {
|
|
// 只有管理员可以审批
|
|
if (req.user.role.name !== 'admin') {
|
|
throw new Error('Access denied');
|
|
}
|
|
return this.financeService.approveExpenseClaim(id, req.user.id, data.notes);
|
|
}
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@Put('expense-claims/:id/reject')
|
|
async rejectExpenseClaim(@Param('id') id: string, @Body() data: any, @Request() req) {
|
|
// 只有管理员可以审批
|
|
if (req.user.role.name !== 'admin') {
|
|
throw new Error('Access denied');
|
|
}
|
|
return this.financeService.rejectExpenseClaim(id, req.user.id, data.notes);
|
|
}
|
|
|
|
// 预支款相关接口
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@Post('advance-payments')
|
|
async createAdvancePayment(@Body() data: any, @Request() req) {
|
|
data.user_id = req.user.id;
|
|
return this.financeService.createAdvancePayment(data);
|
|
}
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@Get('advance-payments')
|
|
async getAdvancePayments(@Query() filter: any, @Request() req) {
|
|
// 普通用户只能查看自己的预支款
|
|
if (req.user.role.name !== 'admin') {
|
|
filter.user_id = req.user.id;
|
|
}
|
|
return this.financeService.getAdvancePayments(filter);
|
|
}
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@Get('advance-payments/:id')
|
|
async getAdvancePaymentById(@Param('id') id: string, @Request() req) {
|
|
const advancePayment = await this.financeService.getAdvancePaymentById(id);
|
|
// 普通用户只能查看自己的预支款
|
|
if (req.user.role.name !== 'admin' && advancePayment.user_id !== req.user.id) {
|
|
throw new Error('Access denied');
|
|
}
|
|
return advancePayment;
|
|
}
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@Put('advance-payments/:id/approve')
|
|
async approveAdvancePayment(@Param('id') id: string, @Body() data: any, @Request() req) {
|
|
// 只有管理员可以审批
|
|
if (req.user.role.name !== 'admin') {
|
|
throw new Error('Access denied');
|
|
}
|
|
return this.financeService.approveAdvancePayment(id, req.user.id, data.notes);
|
|
}
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@Put('advance-payments/:id/reject')
|
|
async rejectAdvancePayment(@Param('id') id: string, @Body() data: any, @Request() req) {
|
|
// 只有管理员可以审批
|
|
if (req.user.role.name !== 'admin') {
|
|
throw new Error('Access denied');
|
|
}
|
|
return this.financeService.rejectAdvancePayment(id, req.user.id, data.notes);
|
|
}
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@Put('advance-payments/:id/usage')
|
|
async updateAdvancePaymentUsage(@Param('id') id: string, @Body() data: any, @Request() req) {
|
|
const advancePayment = await this.financeService.getAdvancePaymentById(id);
|
|
// 普通用户只能更新自己的预支款使用情况
|
|
if (req.user.role.name !== 'admin' && advancePayment.user_id !== req.user.id) {
|
|
throw new Error('Access denied');
|
|
}
|
|
return this.financeService.updateAdvancePaymentUsage(id, data.used_amount);
|
|
}
|
|
|
|
// 凭证相关接口
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@Post('vouchers')
|
|
async uploadVoucher(@Body() data: any, @Request() req) {
|
|
data.uploaded_by = req.user.id;
|
|
return this.financeService.uploadVoucher(data);
|
|
}
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@Get('vouchers')
|
|
async getVouchers(@Query() filter: any) {
|
|
return this.financeService.getVouchers(filter);
|
|
}
|
|
|
|
// 财务审批相关接口
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@Post('approvals')
|
|
async createFinanceApproval(@Body() data: any, @Request() req) {
|
|
data.requester_id = req.user.id;
|
|
return this.financeService.createFinanceApproval(data);
|
|
}
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@Get('approvals')
|
|
async getFinanceApprovals(@Query() filter: any, @Request() req) {
|
|
// 普通用户只能查看自己的审批请求
|
|
if (req.user.role.name !== 'admin') {
|
|
filter.requester_id = req.user.id;
|
|
}
|
|
return this.financeService.getFinanceApprovals(filter);
|
|
}
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@Put('approvals/:id/approve')
|
|
async approveFinanceApproval(@Param('id') id: string, @Body() data: any, @Request() req) {
|
|
// 只有管理员可以审批
|
|
if (req.user.role.name !== 'admin') {
|
|
throw new Error('Access denied');
|
|
}
|
|
return this.financeService.approveFinanceApproval(id, req.user.id, data.notes);
|
|
}
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@Put('approvals/:id/reject')
|
|
async rejectFinanceApproval(@Param('id') id: string, @Body() data: any, @Request() req) {
|
|
// 只有管理员可以审批
|
|
if (req.user.role.name !== 'admin') {
|
|
throw new Error('Access denied');
|
|
}
|
|
return this.financeService.rejectFinanceApproval(id, req.user.id, data.notes);
|
|
}
|
|
|
|
// 公司账目管理接口
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@Get('company-records')
|
|
async getCompanyFinancialRecords(@Query() filter: any, @Request() req) {
|
|
// 只有管理员可以查看公司账目
|
|
if (req.user.role.name !== 'admin') {
|
|
throw new Error('Access denied');
|
|
}
|
|
return this.financeService.getCompanyFinancialRecords(filter);
|
|
}
|
|
|
|
// 项目财务记录接口
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@Get('project-records/:projectId')
|
|
async getProjectFinancialRecords(@Param('projectId') projectId: string) {
|
|
return this.financeService.getProjectFinancialRecords(projectId);
|
|
}
|
|
|
|
// 员工个人记账接口
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@Get('employee-records')
|
|
async getEmployeeFinancialRecords(@Request() req) {
|
|
return this.financeService.getEmployeeFinancialRecords(req.user.id);
|
|
}
|
|
|
|
// 汇率管理相关接口
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@Get('currency-rates')
|
|
async getCurrencyRates(@Request() req) {
|
|
// 只有管理员可以查看汇率设置
|
|
if (req.user.role.name !== 'admin') {
|
|
throw new Error('Access denied');
|
|
}
|
|
return this.financeService.getCurrencyRates();
|
|
}
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@Post('currency-rates')
|
|
async createOrUpdateCurrencyRate(@Body() data: any, @Request() req) {
|
|
// 只有管理员可以设置汇率
|
|
if (req.user.role.name !== 'admin') {
|
|
throw new Error('Access denied');
|
|
}
|
|
return this.financeService.createOrUpdateCurrencyRate(data);
|
|
}
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@Put('currency-rates/:id/status')
|
|
async updateCurrencyRateStatus(@Param('id') id: string, @Body() data: any, @Request() req) {
|
|
// 只有管理员可以更新汇率状态
|
|
if (req.user.role.name !== 'admin') {
|
|
throw new Error('Access denied');
|
|
}
|
|
return this.financeService.updateCurrencyRateStatus(id, data.is_active);
|
|
}
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@Get('currencies')
|
|
async getSupportedCurrencies() {
|
|
return this.financeService.getSupportedCurrencies();
|
|
}
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@Post('convert-currency')
|
|
async convertCurrency(@Body() data: any) {
|
|
const { amount, from_currency, to_currency } = data;
|
|
return this.financeService.convertCurrency(amount, from_currency, to_currency);
|
|
}
|
|
}
|