forked from Ravf95/ddjj
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathasset.go
More file actions
156 lines (140 loc) · 4.22 KB
/
Copy pathasset.go
File metadata and controls
156 lines (140 loc) · 4.22 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
148
149
150
151
152
153
154
155
156
package extract
import (
"fmt"
"strings"
"github.com/InstIDEA/ddjj/parser/declaration"
)
func Assets(e *Extractor, parser *ParserData) ([]*declaration.OtherAsset, error) {
var assets []*declaration.OtherAsset //lsit of extracted assets
asset := &declaration.OtherAsset{} //aux for the actual extraction
e.BindFlag(EXTRACTOR_FLAG_1) //remueve las lineas en blanco
e.BindFlag(EXTRACTOR_FLAG_2) //remueve los espacios en los extremos
//EXTRACTOR_FLAG_3 crea nuevos tokens siempre que dentro de la linea haya mas o igual a 3 espacios
var bandera bool
bandera = false
counter := 0
successful := 0
if e.MoveUntilStartWith(CurrToken, "1.9 OTROS ACTIVOS") {
for e.Scan() {
// other assets table header and OBS are omitted
if isAssetFormField(e.CurrToken) {
bandera = true //we are in the table records because we have the header
continue
}
if strings.Contains(e.CurrToken, "OBS:") && bandera {
counter++
continue
}
// final of others assets of current page
if strings.Contains(e.CurrToken, "TOTAL OTROS ACTIVOS") {
bandera = false
}
//if the ban it's true, we can proceed with the extraction
if bandera {
values := tokenize(e.CurrToken, 3)
//case 1: Description is in two lines
//in this case the lines are
//descPart1
//number of the register
//descPart2
//rest of row
if len(values) == 1 && isNumber(e.CurrToken) {
description := e.PrevToken + " " + e.NextToken
// moving the current token to the next part
e.Scan()
e.Scan()
//building the struct of other assets
fixed := []string{"#", description}
values = append(fixed, tokenize(e.CurrToken, 3)...)
} else
//case 2: Enterprise name is in two lines
//in this case the lines are
//enterprisePart1
//number of the register + description
//enterprisePart2
//rest of row
if len(values) == 2 {
enterpriseNamePart1 := e.PrevToken
//extracting the description of the currentToken thats saved on values array
description := values[1]
e.Scan() // we need to save the description in this part
allName := enterpriseNamePart1 + " " + e.CurrToken
//moving to the rest of the row
e.Scan()
//building the struct of other assets
fixed := []string{"#", description, allName}
values = append(fixed, tokenize(e.CurrToken, 3)...)
} else
//case 3: country in two lines
//namePart1
//num + description + enterprise + ruc
//namePart2
//cant + price + total
if len(values) == 4 {
country := e.PrevToken + " " + e.NextToken
description := values[1]
enterprise := values[2]
ruc := values[3]
// moving the current token to the next part
e.Scan()
e.Scan()
//building the struct of other assets
fixed := []string{"#", description, enterprise, ruc, country}
values = append(fixed, tokenize(e.CurrToken, 4)...)
}
if len(values) == 8 {
asset = getAsset(values)
assets = append(assets, asset)
}
}
}
successful = len(assets)
}
if successful != counter {
parser.addMessage(fmt.Sprintf("ignored assets: %d/%d", counter-successful, counter))
}
if assets == nil {
parser.addError(fmt.Errorf("failed when extracting assets"))
return nil, nil
}
return assets, nil
}
/*
Function to check if a given string is or not the header of the section.
Parameter: string s
Return: True or false
*/
func isAssetFormField(s string) bool {
formField := []string{
"DESCRIPCION",
"EMPRESA",
"RUC",
"PAIS",
"CANT.",
"PRECIO UNI.",
"IMPORTE",
}
s = removeAccents(s)
for _, value := range formField {
if !strings.Contains(s, value) {
return false
}
}
return true
}
/*
Function to load the extracted values into the OtherAsset structure.
Parameters: values in an array of strings. The first element is not inserted because it is the index and not relevant.
Return: an instance of OtherAsset with the values from the array
*/
func getAsset(values []string) *declaration.OtherAsset {
return &declaration.OtherAsset{
Descripcion: values[1],
Empresa: values[2],
RUC: values[3],
Pais: values[4],
Cantidad: stringToInt64(values[5]),
Precio: stringToInt64(values[6]),
Importe: stringToInt64(values[7]),
}
}