forked from hexya-erp/hexya-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bank.go
93 lines (83 loc) · 3.73 KB
/
bank.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
// Copyright 2017 NDP Systèmes. All Rights Reserved.
// See LICENSE file for full licensing details.
package base
import (
"fmt"
"regexp"
"github.com/hexya-erp/hexya/hexya/models"
"github.com/hexya-erp/hexya/hexya/models/operator"
"github.com/hexya-erp/hexya/pool"
)
func init() {
pool.Bank().DeclareModel()
pool.Bank().AddFields(map[string]models.FieldDefinition{
"Name": models.CharField{Required: true},
"Street": models.CharField{},
"Street2": models.CharField{},
"Zip": models.CharField{},
"City": models.CharField{},
"State": models.Many2OneField{RelationModel: pool.CountryState(), String: "Fed. State",
Filter: pool.CountryState().Country().EqualsFunc(func(rs models.RecordSet) pool.CountrySet {
bank := rs.(pool.BankSet)
return bank.Country()
})},
"Country": models.Many2OneField{RelationModel: pool.Country()},
"Email": models.CharField{},
"Phone": models.CharField{},
"Fax": models.CharField{},
"Active": models.BooleanField{Default: models.DefaultValue(true)},
"BIC": models.CharField{String: "Bank Identifier Cord", Index: true, Help: "Sometimes called BIC or Swift."},
})
pool.Bank().Methods().NameGet().Extend("",
func(rs pool.BankSet) string {
res := rs.Name()
if rs.BIC() != "" {
res = fmt.Sprintf("%s - %s", res, rs.BIC())
}
return res
})
pool.Bank().Methods().SearchByName().Extend("",
func(rs pool.BankSet, name string, op operator.Operator, additionalCond pool.BankCondition, limit int) pool.BankSet {
if name == "" {
return rs.Super().SearchByName(name, op, additionalCond, limit)
}
cond := pool.Bank().BIC().ILike(name+"%").Or().Name().AddOperator(op, name)
if !additionalCond.Underlying().IsEmpty() {
cond = cond.AndCond(additionalCond)
}
return pool.Bank().Search(rs.Env(), cond).Limit(limit)
})
pool.BankAccount().DeclareModel()
pool.BankAccount().AddFields(map[string]models.FieldDefinition{
"AccountType": models.CharField{Compute: pool.BankAccount().Methods().ComputeAccountType(), Depends: []string{""}},
"Name": models.CharField{String: "Account Number", Required: true},
"SanitizedAccountNumber": models.CharField{Compute: pool.BankAccount().Methods().ComputeSanitizedAccountNumber(),
Stored: true, Depends: []string{"Name"}},
"Partner": models.Many2OneField{RelationModel: pool.Partner(),
String: "Account Holder", OnDelete: models.Cascade, Index: true,
Filter: pool.Partner().IsCompany().Equals(true).Or().Parent().IsNull()},
"Bank": models.Many2OneField{RelationModel: pool.Bank()},
"BankName": models.CharField{Related: "Bank.Name"},
"BankBIC": models.CharField{Related: "Bank.BIC"},
"Sequence": models.IntegerField{},
"Currency": models.Many2OneField{RelationModel: pool.Currency()},
"Company": models.Many2OneField{RelationModel: pool.Company()},
})
pool.BankAccount().AddSQLConstraint("unique_number", "unique(sanitized_account_number, company_id)", "Account Number must be unique")
pool.BankAccount().Methods().ComputeAccountType().DeclareMethod(
`ComputeAccountType computes the type of account from the account number`,
func(rs pool.BankAccountSet) (*pool.BankAccountData, []models.FieldNamer) {
return &pool.BankAccountData{
AccountType: "bank",
}, []models.FieldNamer{pool.BankAccount().AccountType()}
})
pool.BankAccount().Methods().ComputeSanitizedAccountNumber().DeclareMethod(
`ComputeSanitizedAccountNumber removes all spaces and invalid characters from account number`,
func(rs pool.BankAccountSet) (*pool.BankAccountData, []models.FieldNamer) {
rg, _ := regexp.Compile("\\W+")
san := rg.ReplaceAllString(rs.Name(), "")
return &pool.BankAccountData{
SanitizedAccountNumber: san,
}, []models.FieldNamer{pool.BankAccount().SanitizedAccountNumber()}
})
}