-
Notifications
You must be signed in to change notification settings - Fork 3
/
fd_invoice_detail.go
427 lines (372 loc) · 12.5 KB
/
fd_invoice_detail.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package financial
import (
"context"
"github.com/SupenBysz/gf-admin-community/sys_service"
"github.com/SupenBysz/gf-admin-company-modules/co_interface"
"github.com/SupenBysz/gf-admin-company-modules/co_model"
"github.com/SupenBysz/gf-admin-company-modules/co_model/co_dao"
"github.com/SupenBysz/gf-admin-company-modules/co_model/co_do"
"github.com/SupenBysz/gf-admin-company-modules/co_model/co_enum"
"github.com/kysion/base-library/base_hook"
"github.com/kysion/base-library/base_model"
"github.com/kysion/base-library/utility/daoctl"
"github.com/kysion/base-library/utility/kconv"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/util/gconv"
"github.com/yitter/idgenerator-go/idgen"
)
// 发票详情
type sFdInvoiceDetail[
ITCompanyRes co_model.ICompanyRes,
ITEmployeeRes co_model.IEmployeeRes,
ITTeamRes co_model.ITeamRes,
ITFdAccountRes co_model.IFdAccountRes,
ITFdAccountBillRes co_model.IFdAccountBillRes,
ITFdBankCardRes co_model.IFdBankCardRes,
ITFdCurrencyRes co_model.IFdCurrencyRes,
ITFdInvoiceRes co_model.IFdInvoiceRes,
TR co_model.IFdInvoiceDetailRes,
] struct {
base_hook.ResponseFactoryHook[TR]
modules co_interface.IModules[
ITCompanyRes,
ITEmployeeRes,
ITTeamRes,
ITFdAccountRes,
ITFdAccountBillRes,
ITFdBankCardRes,
ITFdCurrencyRes,
ITFdInvoiceRes,
TR,
]
dao *co_dao.XDao
}
func NewFdInvoiceDetail[
ITCompanyRes co_model.ICompanyRes,
ITEmployeeRes co_model.IEmployeeRes,
ITTeamRes co_model.ITeamRes,
ITFdAccountRes co_model.IFdAccountRes,
ITFdAccountBillRes co_model.IFdAccountBillRes,
ITFdBankCardRes co_model.IFdBankCardRes,
ITFdCurrencyRes co_model.IFdCurrencyRes,
ITFdInvoiceRes co_model.IFdInvoiceRes,
TR co_model.IFdInvoiceDetailRes,
](modules co_interface.IModules[
ITCompanyRes,
ITEmployeeRes,
ITTeamRes,
ITFdAccountRes,
ITFdAccountBillRes,
ITFdBankCardRes,
ITFdCurrencyRes,
ITFdInvoiceRes,
TR,
]) co_interface.IFdInvoiceDetail[TR] {
result := &sFdInvoiceDetail[
ITCompanyRes,
ITEmployeeRes,
ITTeamRes,
ITFdAccountRes,
ITFdAccountBillRes,
ITFdBankCardRes,
ITFdCurrencyRes,
ITFdInvoiceRes,
TR,
]{
modules: modules,
dao: modules.Dao(),
}
result.ResponseFactoryHook.RegisterResponseFactory(result.FactoryMakeResponseInstance)
return result
}
// FactoryMakeResponseInstance 响应实例工厂方法
func (s *sFdInvoiceDetail[
ITCompanyRes,
ITEmployeeRes,
ITTeamRes,
ITFdAccountRes,
ITFdAccountBillRes,
ITFdBankCardRes,
ITFdCurrencyRes,
ITFdInvoiceRes,
TR,
]) FactoryMakeResponseInstance() TR {
var ret co_model.IFdInvoiceDetailRes
ret = &co_model.FdInvoiceDetailRes{}
return ret.(TR)
}
// CreateInvoiceDetail 创建发票详情,相当于创建审核列表,审核是人工审核
func (s *sFdInvoiceDetail[
ITCompanyRes,
ITEmployeeRes,
ITTeamRes,
ITFdAccountRes,
ITFdAccountBillRes,
ITFdBankCardRes,
ITFdCurrencyRes,
ITFdInvoiceRes,
TR,
]) CreateInvoiceDetail(ctx context.Context, info co_model.FdInvoiceDetailRegister) (response TR, err error) {
sessionUser := sys_service.SysSession().Get(ctx).JwtClaimsUser
// 检查指定参数是否为空
if err := g.Validator().Data(info).Run(ctx); err != nil {
return response, err
}
// 获取发票详情
// 创建发票审核详情记录
data := s.FactoryMakeResponseInstance()
gconv.Struct(info, &data)
data.Data().Id = idgen.NextId()
// 设置审核状态为待审核
data.Data().State = co_enum.Invoice.AuditType.WaitReview.Code()
data.Data().CreatedBy = sessionUser.Id
newData := kconv.Struct(data.Data(), &co_do.FdInvoiceDetail{})
// 重载Do模型
doData, err := info.OverrideDo.DoFactory(*newData)
if err != nil {
return response, err
}
result, err := s.dao.FdInvoiceDetail.Ctx(ctx).Data(doData).Insert()
if err != nil || result == nil {
return response, sys_service.SysLogs().ErrorSimple(ctx, err, s.modules.T(ctx, "{#InvoiceDetail}{#error_Create_Failed}"), s.dao.FdInvoiceDetail.Table())
}
return s.GetInvoiceDetailById(ctx, data.Data().Id)
}
// GetInvoiceDetailById 根据id获取发票详情
func (s *sFdInvoiceDetail[
ITCompanyRes,
ITEmployeeRes,
ITTeamRes,
ITFdAccountRes,
ITFdAccountBillRes,
ITFdBankCardRes,
ITFdCurrencyRes,
ITFdInvoiceRes,
TR,
]) GetInvoiceDetailById(ctx context.Context, id int64) (response TR, err error) {
if id == 0 {
return response, gerror.New(s.modules.T(ctx, "{#InvoiceDetail}{#error_Id_NotNull}"))
}
result, err := daoctl.GetByIdWithError[TR](s.dao.FdInvoiceDetail.Ctx(ctx), id)
if err != nil {
return response, sys_service.SysLogs().ErrorSimple(ctx, err, s.modules.T(ctx, "error_GetInvoiceDetailById_Failed"), s.dao.FdInvoiceDetail.Table())
}
return *result, nil
}
// MakeInvoiceDetail 开票
func (s *sFdInvoiceDetail[
ITCompanyRes,
ITEmployeeRes,
ITTeamRes,
ITFdAccountRes,
ITFdAccountBillRes,
ITFdBankCardRes,
ITFdCurrencyRes,
ITFdInvoiceRes,
TR,
]) MakeInvoiceDetail(ctx context.Context, invoiceDetailId int64, makeInvoiceDetail co_model.FdMakeInvoiceDetail) (res bool, err error) {
sessionUser := sys_service.SysSession().Get(ctx).JwtClaimsUser
invoiceDetailInfo, err := s.GetInvoiceDetailById(ctx, invoiceDetailId)
if err != nil {
return false, sys_service.SysLogs().ErrorSimple(ctx, err, s.modules.T(ctx, "error_InvoiceDetailId_Error"), s.dao.FdInvoiceDetail.Table())
}
// 校验状态是否为待开票
if invoiceDetailInfo.Data().State != co_enum.Invoice.State.WaitForInvoice.Code() {
return false, sys_service.SysLogs().ErrorSimple(ctx, err, s.modules.T(ctx, "{#error_InvoiceDetail_Create_Failed}{#error_InvoiceState_Error}"), s.dao.FdInvoiceDetail.Table())
}
// 判断是纸质发票还是电子发票 然后添加审核过后的数据
if makeInvoiceDetail.Type == 1 { // 电子发票
_, err = s.dao.FdInvoiceDetail.Ctx(ctx).OmitNilData().Data(co_do.FdInvoiceDetail{
MakeType: makeInvoiceDetail.MakeType,
MakeUserId: makeInvoiceDetail.MakeUserId,
Email: makeInvoiceDetail.Email,
State: co_enum.Invoice.State.Success.Code(),
MakeAt: gtime.Now(),
UpdatedBy: sessionUser.Id,
}).Where(co_do.FdInvoiceDetail{
Id: invoiceDetailInfo.Data().Id,
}).Update()
} else if makeInvoiceDetail.Type == 2 { // 纸质发票
_, err = s.dao.FdInvoiceDetail.Ctx(ctx).OmitNilData().Data(co_do.FdInvoiceDetail{
MakeType: makeInvoiceDetail.MakeType,
MakeUserId: makeInvoiceDetail.MakeUserId,
CourierName: makeInvoiceDetail.CourierName,
CourierNumber: makeInvoiceDetail.CourierNumber,
State: co_enum.Invoice.State.Success.Code(),
MakeAt: gtime.Now(),
UpdatedBy: sessionUser.Id,
}).Where(co_do.FdInvoiceDetail{
Id: invoiceDetailInfo.Data().Id,
}).Update()
} else {
return false, sys_service.SysLogs().ErrorSimple(ctx, err, s.modules.T(ctx, "{#error_InvoiceDetail_Create_Failed}{#error_InvoiceState_Error}"), s.dao.FdInvoiceDetail.Table())
}
if err != nil {
return false, sys_service.SysLogs().ErrorSimple(ctx, err, s.modules.T(ctx, "error_InvoiceDetail_Update_Failed"), s.dao.FdInvoiceDetail.Table())
}
return true, nil
}
// AuditInvoiceDetail 审核发票
func (s *sFdInvoiceDetail[
ITCompanyRes,
ITEmployeeRes,
ITTeamRes,
ITFdAccountRes,
ITFdAccountBillRes,
ITFdBankCardRes,
ITFdCurrencyRes,
ITFdInvoiceRes,
TR,
]) AuditInvoiceDetail(ctx context.Context, invoiceDetailId int64, auditInfo co_model.FdInvoiceAuditInfo) (bool, error) {
sessionUser := sys_service.SysSession().Get(ctx).JwtClaimsUser
// 审核行仅允许 co_enum_invoice.State.WaitForInvoice 和 co_enum_invoice.State.Failure 待开票、开票失败
if auditInfo.State != co_enum.Invoice.State.WaitForInvoice.Code() && auditInfo.State != co_enum.Invoice.State.Failure.Code() {
return false, sys_service.SysLogs().ErrorSimple(ctx, nil, s.modules.T(ctx, "error_InvoiceDetail_AuditState_Error"), s.dao.FdInvoiceDetail.Table())
}
if auditInfo.State == co_enum.Invoice.State.Failure.Code() && auditInfo.ReplyMsg == "" {
return false, sys_service.SysLogs().ErrorSimple(ctx, nil, s.modules.T(ctx, "error_AuditReply_MustHasMsg"), s.dao.FdInvoiceDetail.Table())
}
invoiceDetailInfo, err := s.GetInvoiceDetailById(ctx, invoiceDetailId)
if err != nil {
return false, sys_service.SysLogs().ErrorSimple(ctx, nil, s.modules.T(ctx, "error_InvoiceDetailId_Error"), s.dao.FdInvoiceDetail.Table())
}
// 代表已审过的
if invoiceDetailInfo.Data().State > co_enum.Invoice.State.WaitAudit.Code() {
return false, sys_service.SysLogs().ErrorSimple(ctx, nil, s.modules.T(ctx, "error_InvoiceDetail_RepeatSubmit"), s.dao.FdInvoiceDetail.Table())
}
// 添加审核过后的数据
_, err = s.dao.FdInvoiceDetail.Ctx(ctx).OmitNilData().Data(co_do.FdInvoiceDetail{
AuditUserId: auditInfo.AuditUserId,
AuditReplyMsg: auditInfo.ReplyMsg,
State: auditInfo.State,
AuditAt: gtime.Now(),
UpdatedBy: sessionUser.Id,
}).Where(co_do.FdInvoiceDetail{
Id: invoiceDetailInfo.Data().Id,
}).Update()
if err != nil {
return false, sys_service.SysLogs().ErrorSimple(ctx, err, s.modules.T(ctx, "error_InvoiceDetail_Update_Failed"), s.dao.FdInvoiceDetail.Table())
}
return true, nil
}
// QueryInvoiceDetailListByInvoiceId 根据发票抬头,获取已开票的发票详情列表
func (s *sFdInvoiceDetail[
ITCompanyRes,
ITEmployeeRes,
ITTeamRes,
ITFdAccountRes,
ITFdAccountBillRes,
ITFdBankCardRes,
ITFdCurrencyRes,
ITFdInvoiceRes,
TR,
]) QueryInvoiceDetailListByInvoiceId(ctx context.Context, invoiceId int64) (*base_model.CollectRes[TR], error) {
result, err := daoctl.Query[TR](s.dao.FdInvoiceDetail.Ctx(ctx), &base_model.SearchParams{
Filter: append(make([]base_model.FilterInfo, 0), base_model.FilterInfo{
Field: s.dao.FdInvoiceDetail.Columns().FdInvoiceId,
Where: "=",
Value: invoiceId,
}),
Pagination: base_model.Pagination{
PageNum: 1,
PageSize: -1,
},
}, false)
return result, err
}
// DeleteInvoiceDetail 标记删除发票详情
func (s *sFdInvoiceDetail[
ITCompanyRes,
ITEmployeeRes,
ITTeamRes,
ITFdAccountRes,
ITFdAccountBillRes,
ITFdBankCardRes,
ITFdCurrencyRes,
ITFdInvoiceRes,
TR,
]) DeleteInvoiceDetail(ctx context.Context, id int64) (bool, error) {
sessionUser := sys_service.SysSession().Get(ctx).JwtClaimsUser
// 判断是否存在该发票
_, err := s.GetInvoiceDetailById(ctx, id)
if err != nil {
return false, sys_service.SysLogs().ErrorSimple(ctx, err, s.modules.T(ctx, "error_InvoiceDetailId_Error"), s.dao.FdInvoiceDetail.Table())
}
err = s.dao.FdInvoiceDetail.Ctx(ctx).Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
// 状态修改为已撤消,
_, err = s.dao.FdInvoiceDetail.Ctx(ctx).
Where(co_do.FdInvoiceDetail{Id: id}).
Update(co_do.FdInvoiceDetail{
State: co_enum.Invoice.State.Cancel.Code(),
UpdatedBy: sessionUser.Id,
DeletedBy: sessionUser.Id,
DeletedAt: gtime.Now(),
})
if err != nil {
return err
}
// 删除
_, err = s.dao.FdInvoiceDetail.Ctx(ctx).Where(co_do.FdInvoiceDetail{Id: id}).Delete()
if err != nil {
return err
}
return nil
})
if err != nil {
return false, sys_service.SysLogs().ErrorSimple(ctx, err, s.modules.T(ctx, "error_InvoiceDetail_Delete_Failed"), s.dao.FdInvoiceDetail.Table())
}
return err == nil, err
}
// QueryInvoiceDetail 根据限定的条件查询发票列表
func (s *sFdInvoiceDetail[
ITCompanyRes,
ITEmployeeRes,
ITTeamRes,
ITFdAccountRes,
ITFdAccountBillRes,
ITFdBankCardRes,
ITFdCurrencyRes,
ITFdInvoiceRes,
TR,
]) QueryInvoiceDetail(ctx context.Context, info *base_model.SearchParams, userId int64, unionMainId int64) (*base_model.CollectRes[TR], error) {
newFields := make([]base_model.FilterInfo, 0)
// 筛选条件强制指定所属用户
if unionMainId != 0 {
newFields = append(newFields, base_model.FilterInfo{
Field: s.dao.FdInvoiceDetail.Columns().UnionMainId, // type
Where: "=",
Value: unionMainId,
})
}
if userId != 0 {
newFields = append(newFields, base_model.FilterInfo{
Field: s.dao.FdInvoiceDetail.Columns().UserId,
Where: "=",
Value: userId,
})
}
if info != nil {
for _, field := range info.Filter {
if field.Field != s.dao.FdInvoiceDetail.Columns().UserId {
newFields = append(newFields, field)
}
}
}
info.Filter = newFields
// 如果没有查询条件,那就会查询出来所有
if unionMainId == 0 {
info = s.modules.Company().FilterUnionMainId(ctx, info)
}
if userId == 0 {
info.Filter = append(info.Filter, base_model.FilterInfo{
Field: s.dao.FdInvoiceDetail.Columns().UserId,
Where: "=",
Value: sys_service.SysSession().Get(ctx).JwtClaimsUser.Id,
})
}
result, err := daoctl.Query[TR](s.dao.FdInvoiceDetail.Ctx(ctx), info, false)
return result, err
}