-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbank.go
More file actions
147 lines (130 loc) · 5.24 KB
/
bank.go
File metadata and controls
147 lines (130 loc) · 5.24 KB
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
// Copyright 2017 NDP Systèmes. All Rights Reserved.
// See LICENSE file for full licensing details.
package base
import (
"fmt"
"regexp"
"strings"
"github.com/hexya-erp/hexya/src/models"
"github.com/hexya-erp/hexya/src/models/fields"
"github.com/hexya-erp/hexya/src/models/operator"
"github.com/hexya-erp/pool/h"
"github.com/hexya-erp/pool/m"
"github.com/hexya-erp/pool/q"
)
func sanitizeAccountNumber(accNumber string) string {
if accNumber == "" {
return ""
}
rg, _ := regexp.Compile("\\W+")
san := rg.ReplaceAllString(accNumber, "")
san = strings.ToUpper(san)
return san
}
var fields_Bank = map[string]models.FieldDefinition{
"Name": fields.Char{Required: true},
"Street": fields.Char{},
"Street2": fields.Char{},
"Zip": fields.Char{},
"City": fields.Char{},
"State": fields.Many2One{RelationModel: h.CountryState(), String: "Fed. State",
Filter: q.CountryState().Country().EqualsFunc(func(rs models.RecordSet) models.RecordSet {
bank := rs.(m.BankSet)
return bank.Country()
}),
OnChange: h.Bank().Methods().OnchangeState()},
"Country": fields.Many2One{RelationModel: h.Country(), OnChange: h.Bank().Methods().OnchangeCountry()},
"Email": fields.Char{},
"Phone": fields.Char{},
"Active": fields.Boolean{Default: models.DefaultValue(true)},
"BIC": fields.Char{String: "Bank Identifier Code", Index: true, Help: "Sometimes called BIC or Swift."},
}
func bank_NameGet(rs m.BankSet) string {
res := rs.Name()
if rs.BIC() != "" {
res = fmt.Sprintf("%s - %s", res, rs.BIC())
}
return res
}
func bank_SearchByName(rs m.BankSet, name string, op operator.Operator, additionalCond q.BankCondition, limit int) m.BankSet {
if name == "" {
return rs.Super().SearchByName(name, op, additionalCond, limit)
}
cond := q.Bank().BIC().ILike(name+"%").Or().Name().AddOperator(op, name)
if !additionalCond.Underlying().IsEmpty() {
cond = cond.AndCond(additionalCond)
}
return h.Bank().Search(rs.Env(), cond).Limit(limit)
}
// OnchangeCountry updates the state field when country is changed
func bank_OnchangeCountry(rs m.BankSet) m.BankData {
res := h.Bank().NewData()
if rs.Country().IsNotEmpty() && !rs.Country().Equals(rs.State().Country()) {
res.SetState(nil)
}
return res
}
// OnchangeState updates the country field when the state is changed
func bank_OnchangeState(rs m.BankSet) m.BankData {
res := h.Bank().NewData()
if rs.State().Country().IsNotEmpty() {
res.SetCountry(rs.State().Country())
}
return res
}
var fields_BankAccount = map[string]models.FieldDefinition{
"AccountType": fields.Char{Compute: h.BankAccount().Methods().ComputeAccountType(), Depends: []string{""}},
"Name": fields.Char{String: "Account Number", Required: true},
"SanitizedAccountNumber": fields.Char{Compute: h.BankAccount().Methods().ComputeSanitizedAccountNumber(),
Stored: true, Depends: []string{"Name"}},
"Partner": fields.Many2One{RelationModel: h.Partner(),
String: "Account Holder", OnDelete: models.Cascade, Index: true,
Filter: q.Partner().IsCompany().Equals(true).Or().Parent().IsNull()},
"Bank": fields.Many2One{RelationModel: h.Bank()},
"BankName": fields.Char{Related: "Bank.Name"},
"BankBIC": fields.Char{Related: "Bank.BIC"},
"Sequence": fields.Integer{},
"Currency": fields.Many2One{RelationModel: h.Currency()},
"Company": fields.Many2One{RelationModel: h.Company(), Required: true, Default: func(env models.Environment) interface{} {
return h.User().NewSet(env).CurrentUser().Company()
}},
}
// ComputeAccountType computes the type of account from the account number
func bankAccount_ComputeAccountType(rs m.BankAccountSet) m.BankAccountData {
return h.BankAccount().NewData().SetAccountType("bank")
}
// ComputeSanitizedAccountNumber removes all spaces and invalid characters from account number
func bankAccount_ComputeSanitizedAccountNumber(rs m.BankAccountSet) m.BankAccountData {
return h.BankAccount().NewData().SetSanitizedAccountNumber(sanitizeAccountNumber(rs.Name()))
}
func bankAccount_Search(rs m.BankAccountSet, cond q.BankAccountCondition) m.BankAccountSet {
predicates := cond.PredicatesWithField(h.BankAccount().Fields().Name())
for i, pred := range predicates {
switch arg := pred.Argument().(type) {
case []string:
newArg := make([]string, len(arg))
for j, a := range arg {
newArg[j] = sanitizeAccountNumber(a)
}
predicates[i].AlterArgument(newArg)
case string:
predicates[i].AlterArgument(sanitizeAccountNumber(arg))
}
predicates[i].AlterField(h.BankAccount().Fields().SanitizedAccountNumber())
}
return rs.Super().Search(cond)
}
func init() {
models.NewModel("Bank")
h.Bank().AddFields(fields_Bank)
h.Bank().Methods().NameGet().Extend(bank_NameGet)
h.Bank().Methods().SearchByName().Extend(bank_SearchByName)
h.Bank().NewMethod("OnchangeCountry", bank_OnchangeCountry)
h.Bank().NewMethod("OnchangeState", bank_OnchangeState)
models.NewModel("BankAccount")
h.BankAccount().AddFields(fields_BankAccount)
h.BankAccount().AddSQLConstraint("unique_number", "unique(sanitized_account_number, company_id)", "Account Number must be unique")
h.BankAccount().NewMethod("ComputeAccountType", bankAccount_ComputeAccountType)
h.BankAccount().NewMethod("ComputeSanitizedAccountNumber", bankAccount_ComputeSanitizedAccountNumber)
h.BankAccount().Methods().Search().Extend(bankAccount_Search)
}