-
Notifications
You must be signed in to change notification settings - Fork 0
/
automobile_api.pm
363 lines (246 loc) · 8.98 KB
/
automobile_api.pm
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package VCS::Site::automobile_api;
use strict;
use VCS::Vars;
use VCS::Site::autodata;
use Data::Dumper;
use JSON;
sub get_mobile_api
# //////////////////////////////////////////////////
{
my ( $self, $token ) = @_;
my $vars = $self->{ 'VCS::Vars' };
my $table_id = $self->get_current_table_id();
my $api = $vars->getparam( 'mobile_api' ) || '';
return get_api_head( $self, 1 ) if $self->{ token } =~ /^\d\d$/;
return get_values_for_api( $self ) if $api =~ /^get_appdata$/i;
return set_values_from_api( $self ) if $api =~ /^set_appdata$/i;
return get_api_centers( $self ) if $api =~ /^get_centers$/i;
return get_doc_status( $self ) if $api =~ /^get_doc_status$/i;
return get_advertisment() if $api =~ /^get_advertisment$/i;
return get_push( $self, $1 ) if $api =~ /^push_(on|off)$/i;
return get_api_head( $self, 2 ) if $api !~ /^get_token$/i;
return get_api_head( $self, 0 );
}
sub get_values_for_api
# //////////////////////////////////////////////////
{
my $self = shift;
my $delete_fields = VCS::Site::autodata::get_mobile_api_fields( 'to_delete' );
my $tables_id = $self->get_current_table_id();
my $result = get_api_head( $self, 0 );
$result->{ data }->{ appointments } = $self->query( 'selallkeys', __LINE__, "
SELECT * FROM AutoAppointments WHERE ID = ?", $tables_id->{ AutoAppointments }
)->[0];
$result->{ data }->{ date } = $result->{ data }->{ appointments }->{ AppDate };
my ( $start, $end ) = $self->query( 'sel1', __LINE__, "
SELECT TStart, TEnd FROM TimeData WHERE SlotID = ?", $result->{ data }->{ appointments }->{ TimeslotID }
);
$_ = $self->{ vars }->get_system->time_to_str( $_ ) for ( $start, $end );
$result->{ data }->{ time } = "$start - $end";
$result->{ data }->{ center } = $self->query( 'sel1', __LINE__, "
SELECT BName FROM Branches WHERE ID = ?",
$result->{ data }->{ appointments }->{ CenterID }
);
my ( $status, $appid ) = $self->query( 'sel1', __LINE__, "
SELECT Finished, CreatedApp FROM AutoToken WHERE ID = ?", $tables_id->{ AutoToken }
);
$result->{ data }->{ status } = ( $status ? 'finished' : 'ongoing' );
if ( $status) {
my $appnum = $self->query( 'sel1', __LINE__, "
SELECT AppNum FROM Appointments WHERE ID = ?", $appid
);
$appnum =~ s!(\d{3})(\d{4})(\d{2})(\d{2})(\d{4})!$1/$2/$3/$4/$5!;
$result->{ data }->{ appnum } = $appnum;
}
delete $result->{ data }->{ appointments }->{ $_ } for ( @{ $delete_fields->{ appointments } } );
my $all_applicants = $self->query( 'selallkeys', __LINE__, "
SELECT * FROM AutoAppData WHERE AppID = ?", $tables_id->{ AutoAppointments }
);
for my $app ( 0..$#$all_applicants ) {
my $sch_app = {};
if ( $all_applicants->[ $app ]->{ SchengenAppDataID } != 0 ) {
$sch_app = $self->query( 'selallkeys', __LINE__, "
SELECT * FROM AutoSchengenAppData WHERE ID = ?", $all_applicants->[ $app ]->{ SchengenAppDataID }
)->[0];
delete $sch_app->{ ID };
}
$result->{ data }->{ schengen }->[ $app ] = $sch_app;
delete $all_applicants->[ $app ]->{ $_ } for ( @{ $delete_fields->{ appdata } } );
$result->{ data }->{ appdata }->[ $app ] = $all_applicants->[ $app ];
}
return $result;
}
sub set_values_from_api
# //////////////////////////////////////////////////
{
my $self = shift;
my $enabled_fields_array = VCS::Site::autodata::get_mobile_api_fields();
my $vars = $self->{ 'VCS::Vars' };
my $json = $vars->getparam( 'data' );
my $data = decode_json( $json );
for my $table ( keys %{ $enabled_fields_array } ) {
my %field_for_table = map { $_ => 1 } @{ $enabled_fields_array->{ $table } };
if ( $table =~ /^(appdata|schengen)$/ ) {
for my $app ( @{ $data->{ $table } } ) {
for my $field ( keys %{ $app } ) {
delete $app->{ $field } unless exists $field_for_table{ $field };
}
}
}
else {
for my $field ( keys %{ $data->{ $table } } ) {
delete $data->{ $table }->{ $field } unless exists $field_for_table{ $field };
}
}
}
my $app_id = $self->insert_hash_table( 'AutoAppointments', $data->{ appointments } );
$self->query( 'query', __LINE__, "
UPDATE AutoToken SET AutoAppID = ? WHERE Token = ?", {}, $app_id, $self->{ token }
);
my $app_max = 0;
for ( 0..$#{ $data->{ appdata } } ) {
my $sch_id = $self->insert_hash_table( 'AutoSchengenAppData', $data->{ schengen }->[ $_ ] );
$data->{ appdata }->[ $_ ]->{ AppID } = $app_id;
$data->{ appdata }->[ $_ ]->{ SchengenAppDataID } = $sch_id;
$self->insert_hash_table( 'AutoAppData', $data->{ appdata }->[ $_ ] );
}
$self->redirect( $self->{ token } . '&mobile_app=on' );
}
sub get_api_centers
# //////////////////////////////////////////////////
{
my $self = shift;
my $result = {};
my $all_centers = $self->query( 'selallkeys', __LINE__, "
SELECT ID, BName, BAddr as address, phone, email, submissionTime, collectionTime
FROM Branches
WHERE isDeleted = 0 AND Display = 1"
);
my $geo_data = VCS::Site::autodata::get_geo_branches();
for ( @$all_centers ) {
$result->{ data }->{ $_->{ BName } } = $_;
$result->{ data }->{ $_->{ BName } }->{ latitude } = $geo_data->{ $_->{ ID } }->[ 0 ];
$result->{ data }->{ $_->{ BName } }->{ longitude } = $geo_data->{ $_->{ ID } }->[ 1 ];
utf8::decode( $result->{ data }->{ $_->{ BName } }->{ address } );
delete $result->{ data }->{ $_->{ BName } }->{ BName };
}
return $result;
}
sub get_doc_status
# //////////////////////////////////////////////////
{
my $self = shift;
my $result = {
'data' => {
'status' => "0"
}
};
my $param = get_all_param( $self, 'docnum', 'birthdate' );
$result->{ data }->{ docnum } = $param->{ docnum };
return $result if !$param->{ docnum } or !$param->{ birthdate };
my $docid = $self->query( 'sel1', __LINE__, "
SELECT ID FROM DocPack WHERE AgreementNo = ? AND PStatus != 7",
$param->{ docnum }
) || 0;
return $result unless $docid;
return $result unless $param->{ birthdate } =~ /^([0-9]{2})([0-9]{2})([0-9]{4})$/;
my $birthdate = "$3-$2-$1";
my $all_status = $self->query( 'selallkeys', __LINE__, "
SELECT ApplID, Status, BthDate
FROM DocPackInfo
JOIN DocPackList ON DocPackInfo.ID = PackInfoID
WHERE PackID = ?", $docid
);
for ( @$all_status ) {
$result->{ data }->{ status } = $_->{ Status } if $birthdate eq $_->{ BthDate };
}
$result->{ data }->{ status } = "0" if $result->{ data }->{ status } == 7;
$result->{ data }->{ status } = "3" if $result->{ data }->{ status } > 7;
return $result;
}
sub get_advertisment
# //////////////////////////////////////////////////
{
return { data => [ '' ] };
}
sub get_push
# //////////////////////////////////////////////////
{
my ( $self, $swich ) = @_;
my $result = get_api_head( $self, 2, 'without_token' );
my $param = get_all_param( $self, 'docnum', 'mid', 'birthdate' );
return $result if !$param->{ docnum } or !$param->{ mid } or !$param->{ birthdate };
return $result unless $param->{ birthdate } =~ /^([0-9]{2})([0-9]{2})([0-9]{4})$/;
my $birthdate = "$3-$2-$1";
my $docid = $self->query( 'sel1', __LINE__, "
SELECT DocPack.ID
FROM DocPack
JOIN DocPackInfo ON DocPack.ID = DocPackInfo.PackID
JOIN DocPackList On DocPackInfo.ID = DocPackList.PackInfoID
WHERE AgreementNo = ? AND DocPackList.BthDate = ? AND PStatus != 7",
$param->{ docnum }, $birthdate
);
return get_api_head( $self, 3, 'without_token' ) unless $docid;
$result = get_api_head( $self, 0, 'without_token' );
my ( $pushid, $pushstatus ) = $self->query( 'sel1', __LINE__, "
SELECT ID, PushStatus FROM DocPackMobilePush WHERE DocPackID = ? AND MobileID = ?",
$docid, $param->{ mid }
);
if ( $swich =~ /on/i ) {
$result->{ data }->{ push } = 'enabled';
if ( !$pushid ) {
$self->query( 'query', __LINE__, "
INSERT INTO DocPackMobilePush (DocPackID, PushStatus, MobileID) VALUES (?, 1, ?)", {},
$docid, $param->{ mid }
);
}
elsif ( $pushstatus != 1 ) {
$self->query( 'query', __LINE__, "
UPDATE DocPackMobilePush SET PushStatus = 1 WHERE ID = ?", {},
$pushid
);
}
}
else {
$result->{ data }->{ push } = 'disabled';
if ( $pushid and $pushstatus == 1 ) {
$self->query( 'query', __LINE__, "
UPDATE DocPackMobilePush SET PushStatus = 0 WHERE ID = ?", {},
$pushid
);
}
}
return $result;
}
sub get_api_head
# //////////////////////////////////////////////////
{
my ( $self, $error_number, $without_token ) = @_;
my $error_text = [
'',
'ошибка токена',
'ошибка API-запроса',
'договор не найден',
'у записи истёк срок действия',
];
my $result = {
'error' => {
'error' => $error_number,
'error_text' => $self->lang( $error_text->[ $error_number ] ),
}
};
$result->{ token } = $self->{ token } unless $without_token;
return $result;
}
sub get_all_param
# //////////////////////////////////////////////////
{
my $self = shift;
my $param = {};
for ( @_ ) {
$param->{ $_ } = $self->{ vars }->getparam( $_ ) || '';
$param->{ $_ } =~ s/[^0-9]//g unless $_ eq 'mid';
}
return $param;
}
1;