-
Notifications
You must be signed in to change notification settings - Fork 0
/
brm.php
executable file
·249 lines (183 loc) · 6.48 KB
/
brm.php
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
<?php
class RecMov {
private $codrec;
private $dtdue;
private $colstd = '';
private $colinstr = '';
private $list;
public function set($input) {
$this->codrec = $input['codrec'];
$this->dtdue = $input['dtdue'];
$this->colstd = $input['colstd'];
$this->colinstr = $input['colinstr'];
$this->list = array();
foreach ($input['list'] as $value) {
if (!in_array($value['std'], ['RecAdd', 'RecSub', 'RecRei'])) continue;
$this->list[] = array(
'doc' => $value['docto'],
'reccod' => $input['codrec'],
'seq' => 1,
'bandt' => null,
'bancc' => 0,
'banseq' => 0,
'std' => $value['std'],
'recdt' => $value['dtrec'],
'val' => $value['val'],
);
}
}
public function get() {
$data = array(
'codrec' => $this->codrec,
'dtdue' => $this->dtdue,
'colstd' => $this->colstd,
'colinstr' => $this->colinstr,
'list' => $this->list
);
return $data;
}
}
class RecMovService {
public function getList($dbid, $dc, $dt0, $dtn, $dtParam, $status) {
/*
Input: token, date range (string format 'yyyy-MM-dd'), param (string 'dt' or 'dtdue')
Output: Array of mov data arrays:
array(
array(
'cod' => 0,
'coCod' => '',
'cpName' => '',
'dt' => 'yyyy-MM-dd',
'val' => 0,
'bal' => 0,
);
);
*/
try {
// Dependencies
$dao = new RecMovDao();
// Test
if (!$dbid or !is_numeric($dbid)) throw new Exception('db');
$this->testDt($dt0);
$this->testDt($dtn);
if ($dtParam != 'dt' and $dtParam != 'dtdue') throw new Exception("dtParam invalid");
// Get
$list = $dao->getList($dbid, $dc, $dt0, $dtn, $dtParam, $status);
return $list;
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
public function get($dbid, $dc, $cod) {
/*
Input token, icodd (0 if new)
Output: Mov data array
array(
...
);
*/
try {
$data = array();
// Dependencies
$dao = new RecMovDao();
// Test (includes no new record)
if (!$dbid or !is_numeric($dbid)) throw new Exception('db');
$this->testCodRec($cod);
// Get
$data = $dao->get($dbid, $dc, $cod);
$data['colstdlist'] = $this->getColStdList();
return $data;
} catch (Exception $e) {
throw new exception($e->getMessage());
}
}
public function put($dbid, $dc, $input) {
try {
// Dependencies
$mov = new RecMov();
$dao = new RecMovDao();
// Test
if (!$dbid or !is_numeric($dbid)) throw new Exception('db');
if (!in_array($dc, array('1', '2'))) throw new Exception('srv');
$this->test($input);
// Put
$mov->set($input);
$data = $mov->get();
$dao->put($dbid, $dc, $data);
return true;
} catch (Exception $e) {
throw new exception($e->getMessage());
}
}
// Gets
public function getColStdList() {
$data = array(
['cod'=>'', 'name'=>''],
['cod'=>'TR', 'name'=>'Transf.'],
['cod'=>'BL', 'name'=>'Boleto'],
['cod'=>'DB', 'name'=>'Db.auto.'],
['cod'=>'CH', 'name'=>'Cheque'],
['cod'=>'DN', 'name'=>'Dinheiro']
);
return $data;
}
// Tests
public function test($input) {
$this->testCodRec($input['codrec']);
$this->testDtDue($input['dtdue']);
$this->testList($input['list']);
$this->testColStd($input['colstd']);
$this->testColInstr($input['colinstr']);
}
public function testCodRec($codrec) {
try {
if (!preg_match("/^[A-Za-z0-9-\/.]{1,20}$/", $codrec)) throw new exception('invalid cod');
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
public function testDtDue($dtdue) {
try {
$this->testDt($dtdue);
} catch (Exception $e) {
throw new exception("invalid dtdue");
}
}
public function testList($list) {
try {
foreach ($list as $key => $value) {
//Std 'RecIns' and 'RecBan' not handled here
if (!in_array($value['std'], ['RecAdd', 'RecSub', 'RecRei'])) continue;
if (!preg_match("/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/", $value['dtrec'])) throw new exception("invalid dt[{$key}]");
if (!preg_match("/^[a-zA-Z0-9.\/-]{1,30}$/", $value['docto'])) throw new exception("invalid docto[{$key}]");
if (!preg_match("/^[-]{0,1}[0-9]{1,8}[.]{0,1}[0-9]{0,2}$/", $value['val'])) throw new exception("invalid val[{$key}]");
}
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
public function testDt($dt) {
try {
if (!preg_match("/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/", $dt)) throw new exception();
$d = new DateTime($dt);
} catch (Exception $e) {
throw new exception("invalid dt");
}
}
public function testColStd($colstd) {
try {
$list = array_keys($this->getColStdList()) ;
if (!in_array($colstd, $list)) throw new exception();
} catch (Exception $e) {
throw new exception("invalid colstd");
}
}
public function testColInstr($colinstr) {
try {
if (!preg_match("/^[0-9.\/-]{0,60}$/", $colinstr)) throw new exception();
} catch (Exception $e) {
throw new exception("invalid colinstr");
}
}
}
?>