-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbr.php
executable file
·267 lines (194 loc) · 6.66 KB
/
bbr.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
class BanRec {
public $doc;
public $dt;
public $cc;
public $seq;
public $std = 'RecBan';
public $list = array();
/*
Just Ref
accCod
banval
hist
srv
token
*/
public function set($input) {
// From Uil
$this->doc = $input['dt'] . '.' . $input['cc'] . '.'. $input['seq'];
$this->dt = $input['dt']; //"2016-09-06"
$this->cc = $input['cc']; // "2"
$this->seq = $input['seq']; //"1"
$this->std = 'RecBan';
$this->list = array();
foreach ($input['list'] as $value) {
$this->list[] = array(
'cpname' => $value['cpname'],
'dtdue' => $value['dtdue'],
'reccod' => $value['reccod'], // ok
'recdt' => $value['recdt'],
'recval' => $value['recval'],
'banval' => $value['banval']
);
}
}
public function get() {
//From Bll to Dal
$data = array();
foreach ($this->list as $value) {
$data[] = array(
'doc' => $this->doc,
'reccod' => $value['reccod'],
'seq' => 1,
'bandt' => $this->dt,
'bancc' => $this->cc,
'banseq' => $this->seq,
'std' => 'RecBan',
'recdt' => $value['recdt'],
'val' => -$value['banval'],
);
if ($value['banval'] != $value['recval']) {
$std = ($value['banval'] > $value['recval']) ? 'RecAdd' : 'RecSub';
$data[] = array(
'doc' => $this->doc,
'reccod' => $value['reccod'],
'seq' => 2,
'bandt' => $this->dt,
'bancc' => $this->cc,
'banseq' => $this->seq,
'std' => $std,
'recdt' => $value['recdt'],
'val' => $value['banval'] - $value['recval'],
);
}
}
return $data;
}
}
class BanRecService {
public function getList($dbid, $dt0, $dtn) {
/*
Input: token and date range (string format 'yyyy-MM-dd')
Output: Array of mov data arrays, each one:
array(
'cod' => '',
'seq' => 0,
'codrec' => '',
'codban' => '',
'cpcod' => '',
'cpname' => '',
'dtdue' => '',
'dtrec' => '',
'deban' => '',
'cc' => 0,
'ccr' => 0,
'val' => 0,
'valban' => 0
);
'dt' will be 'yyyy-MM-dd'
*/
try {
// Dependencies
$blo = new Bas();
$dao = new BanRecDao();
// Test
if (!$dbid or !is_int($dbid)) throw new Exception('db');
$dt0 = $blo->test_dtStr($dt0);
$dtn = $blo->test_dtStr($dtn);
// Get
$list = $dao->getList($dbid, $dt0, $dtn);
return $list;
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
public function get($dbid, $doc) {
/*
Input:
token: string
cod [(string yyyymmdd)date].[(int)cc].[(int)seq]
Output:
Array [
cod,
dt,
acc,
seq,
hist,
val,
token,
srv,
list = [
reccod,
cpname,
recdue,
recdt,
recval,
banval
]
]
*/
try {
// Dependencies
$dao = new BanRecDao();
// Tests
if (!$dbid or !is_numeric($dbid)) throw new Exception('db');
$this->testDoc($doc);
// Get
$data = $dao->get($dbid, $doc);
return $data;
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
public function put($input) {
try {
// Dependencies
$mov = new BanRec();
$dao = new BanRecDao();
// Tests
if (!$input['dbid'] or !is_numeric($input['dbid'])) throw new Exception('db');
$this->test($input);
$mov->set($input);
$data = $mov->get();
// Put
$data = $dao->put($input['dbid'], $mov->doc, $data);
return true;
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
public function test($input) {
// From Uil
try {
$this->testDt($input['dt']);
if (!preg_match("/^[0-9]{1,2}$/", $input['cc'])) throw new exception("invalid cc");
if (!preg_match("/^[0-9]{1,2}$/", $input['seq'])) throw new exception("invalid seq");
$i = 1;
foreach ($input['list'] as $value) {
if (!$value['cpname']) throw new exception('invalid cpname [{$i}]');
$this->testDt($value['dtdue'], $key);
if (!preg_match("/^[A-Z]{1,3}[0-9.\/]{1,15}.[0-2]{1,10}$/", $value['reccod'])) throw new exception("invalid reccod [{$i}]");
if (!preg_match("/^[0-9]{1,12}[.]{0,1}[0-9]{0,2}$/", $value['recval'])) throw new exception('invalid recval [{$i}]');
if (!preg_match("/^[0-9]{1,12}[.]{0,1}[0-9]{0,2}$/", $value['banval'])) throw new exception('invalid banval [{$i}]');
$i++;
}
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
public function testDoc($doc) {
if (!preg_match("/^[a-zA-Z0-9.\/-]{1,30}$/", $doc)) throw new exception("invalid doc");
}
public function testDt($str, $i = null) {
try {
if (!preg_match("/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/", $str)) throw new exception();
$dt = date_create($str);
if (!$dt) throw new exception();
} catch (Exception $e) {
if ($i === null) throw new exception('invalid date');
else throw new exception('invalid date [{$i}]');
}
}
}
?>