|
|
@@ -34,8 +34,14 @@ use POSIX;
|
|
|
use Data::Dumper;
|
|
|
|
|
|
#use OrgDB;
|
|
|
+#push @INC, "/home/bus/popufare/server/scripts";
|
|
|
+use lib qw( . );
|
|
|
+use RideLogic;
|
|
|
|
|
|
-my $ORG = "ORG";
|
|
|
+#my $ORG = "ORG";
|
|
|
+my $ORG = "TEST-ORG";
|
|
|
+
|
|
|
+my $isMySQL = 0;
|
|
|
|
|
|
#my $database_path = 'DBI:mysql:busdb';
|
|
|
my $database_path = 'DBI:SQLite:dbname=../bus.sqlite';
|
|
|
@@ -157,22 +163,26 @@ sub ExpirePass {
|
|
|
my $query = $dbh->prepare("select p.user_pass_id, p.queue_order, p.rule, p.nrides_remain, p.nday_expiration, rc.ruleclass
|
|
|
from user_pass p, rule_class rc
|
|
|
where p.logical_card_id = ? and p.active = 1 and p.expired = 0 and
|
|
|
- ( ( rc.ruleclass = 'NDAY' and p.nday_expiration < now() ) or
|
|
|
+ ( ( rc.ruleclass = 'NDAY' and p.nday_expiration < " . ($isMySQL ? "now()" : "datetime('now', 'localtime')") . ") or
|
|
|
( rc.ruleclass = 'NRIDE' and p.nrides_remain <= 0 ) or
|
|
|
( rc.rulename = 'PREACTIVE' ) ) ");
|
|
|
$query->execute($cardid);
|
|
|
+ my $href = $query->fetchrow_hashref;
|
|
|
if ($query->rows == 0) { $dbh->commit; return; }
|
|
|
|
|
|
- my $href = $query->fetchrow_hashref;
|
|
|
+print "#bill: cp0\n";
|
|
|
+
|
|
|
my $passid = $href->{'user_pass_id'};
|
|
|
my $current_q_num = $href->{'queue_order'};
|
|
|
|
|
|
# expire old pass
|
|
|
my $audit_pass_id = audit_user_pass_start($dbh, $passid, "billing_server: ExpirePass: deactivating and expiring pass");
|
|
|
- $query = $dbh->prepare("update user_pass set active = 0, expired = 1, deactivated = now() where user_pass_id = ?");
|
|
|
+ $query = $dbh->prepare("update user_pass set active = 0, expired = 1, deactivated = " . ($isMySQL ? "now()" : "datetime('now', 'localtime')") . " where user_pass_id = ?");
|
|
|
$query->execute($passid);
|
|
|
audit_user_pass_end($dbh, $passid, $audit_pass_id);
|
|
|
|
|
|
+print "#bill: cp1\n";
|
|
|
+
|
|
|
# activate new pass
|
|
|
$query = $dbh->prepare("select p.user_pass_id, p.rule, p.nday_orig, p.nday_expiration, p.nrides_orig, p.queue_order, rc.ruleclass
|
|
|
from user_pass p, rule_class rc
|
|
|
@@ -185,21 +195,31 @@ sub ExpirePass {
|
|
|
and t.expired = 0) ");
|
|
|
$query->execute($cardid, $cardid, $current_q_num);
|
|
|
|
|
|
+
|
|
|
+print "#bill: cp2\n";
|
|
|
+ $href = $query->fetchrow_hashref;
|
|
|
+
|
|
|
# no passes left, put in reject rule, finish transaction
|
|
|
if ($query->rows == 0) {
|
|
|
- $query = $dbh->prepare("lock tables active_rider_table write");
|
|
|
- $query->execute();
|
|
|
+ if ($isMySQL) {
|
|
|
+ $query = $dbh->prepare("lock tables active_rider_table write");
|
|
|
+ $query->execute();
|
|
|
+ }
|
|
|
$query = $dbh->prepare("insert into active_rider_table (logical_card_id, rfid_token, mag_token, rule_name, rule_param, deleted, notes)
|
|
|
values (?,?,?,?,?,?,?)");
|
|
|
$query->execute($cardid, @oldrow[1,2], $ORG . '-REJECT', 'reject', 0, $oldrow[7]);
|
|
|
$dbh->commit;
|
|
|
- $query = $dbh->prepare("unlock tables");
|
|
|
- $query->execute();
|
|
|
+ if ($isMySQL) {
|
|
|
+ $query = $dbh->prepare("unlock tables");
|
|
|
+ $query->execute();
|
|
|
+ }
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
+print "#bill: cp3\n";
|
|
|
+
|
|
|
# else make new pass active and update art with new pass
|
|
|
- $href = $query->fetchrow_hashref;
|
|
|
+ #$href = $query->fetchrow_hashref;
|
|
|
|
|
|
my $pass_param = '';
|
|
|
if ($href->{'ruleclass'} eq 'NRIDE') {
|
|
|
@@ -214,14 +234,22 @@ sub ExpirePass {
|
|
|
$query->execute($ride_time, $href->{'user_pass_id'} );
|
|
|
audit_user_pass_end($dbh, $href->{'user_pass_id'}, $audit_pass_id);
|
|
|
|
|
|
- $query = $dbh->prepare("lock tables active_rider_table write");
|
|
|
- $query->execute();
|
|
|
+print "#bill: cp4\n";
|
|
|
+
|
|
|
+ if ($isMySQL) {
|
|
|
+ $query = $dbh->prepare("lock tables active_rider_table write");
|
|
|
+ $query->execute();
|
|
|
+ }
|
|
|
$query = $dbh->prepare("insert into active_rider_table (logical_card_id, rfid_token, mag_token, rule_name, rule_param, deleted, notes)
|
|
|
values (?,?,?,?,?,?,?)");
|
|
|
$query->execute($cardid, @oldrow[1,2], $href->{'rule'}, $pass_param, 0, $oldrow[7]);
|
|
|
$dbh->commit;
|
|
|
- $query = $dbh->prepare("unlock tables");
|
|
|
- $query->execute();
|
|
|
+ if ($isMySQL) {
|
|
|
+ $query = $dbh->prepare("unlock tables");
|
|
|
+ $query->execute();
|
|
|
+ }
|
|
|
+
|
|
|
+print "#bill: cp5\n";
|
|
|
|
|
|
}
|
|
|
|
|
|
@@ -239,8 +267,21 @@ sub AdvanceRiderPass {
|
|
|
$dbh->{RaiseError} = 1;
|
|
|
$dbh->{PrintError} = 1;
|
|
|
|
|
|
+print "#bill: advance: cp0, logical_card_id " . $logical_card_id . "\n";
|
|
|
+
|
|
|
$dbh->begin_work;
|
|
|
|
|
|
+# my $sth_find = $dbh->prepare('SELECT active_rider_table.logical_card_id, active_rider_table.rfid_token,
|
|
|
+# active_rider_table.mag_token, active_rider_table.rule_name,
|
|
|
+# active_rider_table.rule_param, active_rider_table.deleted,
|
|
|
+# active_rider_table.parent_entity, active_rider_table.notes,
|
|
|
+# active_rider_table.seq_num
|
|
|
+# FROM active_rider_table
|
|
|
+# WHERE logical_card_id = ?
|
|
|
+# AND NOT(deleted)
|
|
|
+# AND seq_num = (SELECT max(seq_num) FROM active_rider_table WHERE logical_card_id = ?) ');
|
|
|
+# my $xx = $sth_find->execute($logical_card_id, $logical_card_id);
|
|
|
+
|
|
|
my $sth_find = $dbh->prepare('SELECT active_rider_table.logical_card_id, active_rider_table.rfid_token,
|
|
|
active_rider_table.mag_token, active_rider_table.rule_name,
|
|
|
active_rider_table.rule_param, active_rider_table.deleted,
|
|
|
@@ -249,9 +290,10 @@ sub AdvanceRiderPass {
|
|
|
FROM active_rider_table
|
|
|
WHERE logical_card_id = ?
|
|
|
AND NOT(deleted)
|
|
|
- AND seq_num = (SELECT max(seq_num) FROM active_rider_table WHERE logical_card_id = ?) ');
|
|
|
- $sth_find->execute($logical_card_id, $logical_card_id);
|
|
|
- if ($sth_find->rows != 1) { $dbh->commit; return; }
|
|
|
+ order by seq_num desc limit 1;');
|
|
|
+ $sth_find->execute($logical_card_id);
|
|
|
+
|
|
|
+ #if ($sth_find->rows != 1) { $dbh->commit; return; }
|
|
|
|
|
|
#@oldrow:
|
|
|
#0. logical_card_id
|
|
|
@@ -265,6 +307,10 @@ sub AdvanceRiderPass {
|
|
|
#8. seq_num
|
|
|
my @oldrow = $sth_find->fetchrow_array();
|
|
|
|
|
|
+ if (not @oldrow) { $dbh->commit; return; }
|
|
|
+
|
|
|
+print ">> $logical_card_id, $billing_ride_time\n";
|
|
|
+
|
|
|
my $sth_pass = $dbh->prepare("select p.user_pass_id, p.nrides_remain, p.nday_orig, p.nday_expiration, p.rule
|
|
|
from user_pass p, user_card c
|
|
|
where p.logical_card_id = ?
|
|
|
@@ -274,44 +320,86 @@ sub AdvanceRiderPass {
|
|
|
and p.expired = 0
|
|
|
and p.activated <= ?");
|
|
|
$sth_pass->execute($logical_card_id, $billing_ride_time);
|
|
|
+
|
|
|
+ my $pass = $sth_pass->fetchrow_hashref;
|
|
|
+
|
|
|
+if ($pass) {
|
|
|
+print ">>>>" . $pass . "\n";
|
|
|
+print " ok?\n";
|
|
|
+}
|
|
|
+
|
|
|
if ($sth_pass->rows != 1) {
|
|
|
if (uc($billing_action) ne "REJECT") {
|
|
|
- my $sth = $dbh->prepare("insert into diagnostic_log (loglvl, message)
|
|
|
- values ('warning', concat('billing_server: logical_card_id ', ?, ', billing_cksum ', ?, ', art seq_num ', ?, ', dropping billing entry: no matching pass entry') ) ");
|
|
|
+ my $sth;
|
|
|
+ if ($isMySQL) {
|
|
|
+ $sth = $dbh->prepare("insert into diagnostic_log (loglvl, message)
|
|
|
+ values ('warning', concat('billing_server: logical_card_id ', ?, ', billing_cksum ', ?, ', art seq_num ', ?, ', dropping billing entry: no matching pass entry') ) ");
|
|
|
+ } else {
|
|
|
+ $sth = $dbh->prepare("insert into diagnostic_log (loglvl, message)
|
|
|
+ values ('warning', 'billing_server: logical_card_id ' || ? || ', billing_cksum ' || ? || ', art seq_num ' || ? || ', dropping billing entry: no matching pass entry' ) ");
|
|
|
+ }
|
|
|
$sth->execute($logical_card_id, $billing_cksum, $oldrow[8]);
|
|
|
}
|
|
|
$dbh->commit;
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- my $pass = $sth_pass->fetchrow_hashref;
|
|
|
+print "#bill: advance: cp2\n";
|
|
|
+
|
|
|
+ #my $pass = $sth_pass->fetchrow_hashref;
|
|
|
|
|
|
my $t = $dbh->prepare("select ruleclass from rule_class where rulename = ?");
|
|
|
$t->execute($pass->{'rule'});
|
|
|
+
|
|
|
+ my $tref = $t->fetchrow_hashref;
|
|
|
+
|
|
|
+print ">>> \$t->rows " . $t->rows . "\n";
|
|
|
+
|
|
|
my $rule_class = 'OTHER';
|
|
|
if ($t->rows == 1) {
|
|
|
- $rule_class = $t->fetchrow_hashref->{'ruleclass'};
|
|
|
+ #$rule_class = $t->fetchrow_hashref->{'ruleclass'};
|
|
|
+ $rule_class = $tref->{'ruleclass'};
|
|
|
} elsif ($t->rows < 1) {
|
|
|
- my $sth = $dbh->prepare("insert into diagnostic_log (loglvl, message)
|
|
|
- values ('warning', concat('billing_server: logical_card_id ', ?, ', billing_cksum ', ?, ', art seq_num ', ?, ', no rule class found, dropping billing entry') ) ");
|
|
|
+ my $sth;
|
|
|
+ if ($isMySQL) {
|
|
|
+ my $sth = $dbh->prepare("insert into diagnostic_log (loglvl, message)
|
|
|
+ values ('warning', concat('billing_server: logical_card_id ', ?, ', billing_cksum ', ?, ', art seq_num ', ?, ', no rule class found, dropping billing entry') ) ");
|
|
|
+ } else {
|
|
|
+ my $sth = $dbh->prepare("insert into diagnostic_log (loglvl, message)
|
|
|
+ values ('warning', 'billing_server: logical_card_id ' || ? || ', billing_cksum ' || ? || ', art seq_num ' || ? || ', no rule class found, dropping billing entry' ) ");
|
|
|
+ }
|
|
|
$sth->execute($logical_card_id, $billing_cksum, $oldrow[8]);
|
|
|
$dbh->commit;
|
|
|
return;
|
|
|
} else {
|
|
|
- my $sth = $dbh->prepare("insert into diagnostic_log (loglvl, message)
|
|
|
- values ('warning', concat('billing_server: logical_card_id ', ?, ', billing_cksum ', ?, ', art seq_num ', ?, ', multiple rule classes found, dropping billing entry') ) ");
|
|
|
+ my $sth;
|
|
|
+ if ($isMySQL) {
|
|
|
+ my $sth = $dbh->prepare("insert into diagnostic_log (loglvl, message)
|
|
|
+ values ('warning', concat('billing_server: logical_card_id ', ?, ', billing_cksum ', ?, ', art seq_num ', ?, ', multiple rule classes found, dropping billing entry') ) ");
|
|
|
+ } else {
|
|
|
+ my $sth = $dbh->prepare("insert into diagnostic_log (loglvl, message)
|
|
|
+ values ('warning', 'billing_server: logical_card_id ' || ? || ', billing_cksum ' || ? || ', art seq_num ' || ? || ', multiple rule classes found, dropping billing entry' ) ");
|
|
|
+ }
|
|
|
$sth->execute($logical_card_id, $billing_cksum, $oldrow[8]);
|
|
|
$dbh->commit;
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
+print "#bill: advance: cp3\n";
|
|
|
+
|
|
|
if (uc($billing_action) eq "REJECT") {
|
|
|
# bus not sync'd?
|
|
|
$dbh->commit;
|
|
|
} elsif ($oldrow[3] ne $pass->{'rule'}) {
|
|
|
# raise warning?
|
|
|
- my $sth = $dbh->prepare("insert into diagnostic_log (loglvl, message)
|
|
|
- values ('warning', concat('billing_server: logical_card_id ',?,', billing_cksum ',?,', art seq_num ',?,', rule mismatch(1): art rule \"',?,'\" != user_pass_id ',?,' rule \"',?,'\"') )");
|
|
|
+ my $sth;
|
|
|
+ if ($isMySQL) {
|
|
|
+ my $sth = $dbh->prepare("insert into diagnostic_log (loglvl, message)
|
|
|
+ values ('warning', concat('billing_server: logical_card_id ',?,', billing_cksum ',?,', art seq_num ',?,', rule mismatch(1): art rule \"',?,'\" != user_pass_id ',?,' rule \"',?,'\"') )");
|
|
|
+ } else {
|
|
|
+ my $sth = $dbh->prepare("insert into diagnostic_log (loglvl, message)
|
|
|
+ values ('warning', 'billing_server: logical_card_id ' || ? || ', billing_cksum ' || ? || ', art seq_num ' || ? || ', rule mismatch(1): art rule \"' || ? || '\" != user_pass_id ' || ? || ' rule \"' || ? || '\"' )");
|
|
|
+ }
|
|
|
$sth->execute($logical_card_id, $billing_cksum, $oldrow[8], $oldrow[3], $pass->{'user_pass_id'}, $pass->{'rule'});
|
|
|
$dbh->commit;
|
|
|
} elsif ($billing_rule ne $pass->{'rule'}) {
|
|
|
@@ -333,36 +421,58 @@ sub AdvanceRiderPass {
|
|
|
|
|
|
# expire passes will take care of it if #rides == 0
|
|
|
if ($cur_rides>0) {
|
|
|
- $q = $dbh->prepare("lock tables active_rider_table write");
|
|
|
- $q->execute();
|
|
|
+ if ($isMySQL) {
|
|
|
+ $q = $dbh->prepare("lock tables active_rider_table write");
|
|
|
+ $q->execute();
|
|
|
+ }
|
|
|
$q = $dbh->prepare('insert into active_rider_table (logical_card_id, rfid_token, mag_token, rule_name, rule_param, deleted, parent_entity, notes)
|
|
|
values (?, ?, ?,?, ?, ?, ?, ?)');
|
|
|
$q->execute(@oldrow[0..7]);
|
|
|
}
|
|
|
$dbh->commit;
|
|
|
- if ($cur_rides>0) { $q = $dbh->prepare("unlock tables"); $q->execute(); }
|
|
|
+ if ($cur_rides>0) {
|
|
|
+ if ($isMySQL) {
|
|
|
+ $q = $dbh->prepare("unlock tables");
|
|
|
+ $q->execute();
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
} elsif ($rule_class eq 'NDAY') {
|
|
|
|
|
|
+print "#>> nday expiration " . $pass->{'nday_expiration'} . "\n";
|
|
|
+
|
|
|
# update user_pass with expiration and update active_rider_table with new param
|
|
|
if (!$pass->{'nday_expiration'}) {
|
|
|
my $audit_pass_id = audit_user_pass_start($dbh, $pass->{'user_pass_id'}, "billing_server: AdvanceRiderPass: updating nday");
|
|
|
- my $q = $dbh->prepare("update user_pass
|
|
|
- set nday_expiration = addtime( adddate(convert(date(?), datetime), nday_orig), '2:30'), firstused = ?, lastused = ?
|
|
|
- where user_pass_id = ?");
|
|
|
- $q->execute($billing_ride_time, $billing_ride_time, $billing_ride_time, $pass->{'user_pass_id'});
|
|
|
+
|
|
|
+ my $q;
|
|
|
+ if ($isMySQL) {
|
|
|
+ my $q = $dbh->prepare("update user_pass
|
|
|
+ set nday_expiration = addtime( adddate(convert(date(?), datetime), nday_orig), '2:30'), firstused = ?, lastused = ?
|
|
|
+ where user_pass_id = ?");
|
|
|
+ $q->execute($billing_ride_time, $billing_ride_time, $billing_ride_time, $pass->{'user_pass_id'});
|
|
|
+ } else {
|
|
|
+ my $q = $dbh->prepare("update user_pass
|
|
|
+ set nday_expiration = strftime('%Y-%m-%d %H:%M:%S', date(?, '+? days'), '+150 minutes'), firstused = ?, lastused = ?
|
|
|
+ where user_pass_id = ?");
|
|
|
+ $q->execute($billing_ride_time, $billing_ride_time, $billing_ride_time, $pass->{'user_pass_id'});
|
|
|
+ }
|
|
|
audit_user_pass_end($dbh, $pass->{'user_pass_id'}, $audit_pass_id);
|
|
|
|
|
|
$oldrow[4] = $pass->{'nday_orig'} . " " . join('-', Add_Delta_Days(Today, $pass->{'nday_orig'} )) . " 2:30:00";
|
|
|
|
|
|
- $q = $dbh->prepare("lock tables active_rider_table write"); $q->execute();
|
|
|
+ if ($isMySQL) {
|
|
|
+ $q = $dbh->prepare("lock tables active_rider_table write"); $q->execute();
|
|
|
+ }
|
|
|
my $sth_new_expires = $dbh->prepare('INSERT INTO active_rider_table (logical_card_id, rfid_token, mag_token, rule_name, rule_param, deleted, parent_entity, notes)
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)');
|
|
|
$sth_new_expires->execute(@oldrow[0..7]);
|
|
|
|
|
|
$dbh->commit;
|
|
|
- $q = $dbh->prepare("unlock tables");
|
|
|
- $q->execute();
|
|
|
+ if ($isMySQL) {
|
|
|
+ $q = $dbh->prepare("unlock tables");
|
|
|
+ $q->execute();
|
|
|
+ }
|
|
|
|
|
|
} else { # else just update last used
|
|
|
|
|
|
@@ -382,8 +492,11 @@ sub AdvanceRiderPass {
|
|
|
$dbh->commit;
|
|
|
}
|
|
|
|
|
|
+print "#bill: advance: cp4\n";
|
|
|
+
|
|
|
ExpirePass( $dbh, $logical_card_id, $pass->{'user_pass_id'}, $billing_ride_time, @oldrow );
|
|
|
|
|
|
+print "#bill: advance: cp5\n";
|
|
|
}
|
|
|
|
|
|
sub ServerReply
|
|
|
@@ -415,11 +528,13 @@ sub ServerReply
|
|
|
$message =~ s/^.//;
|
|
|
|
|
|
try {
|
|
|
- $sth = $dbh->prepare('INSERT IGNORE INTO diagnostic_log (loglvl, message) VALUES (?, ?)')
|
|
|
+ $sth = $dbh->prepare('INSERT ' . ($isMySQL ? '' : ' OR ') . 'IGNORE INTO diagnostic_log (loglvl, message) VALUES (?, ?)')
|
|
|
or die "Couldn't prepare statement: " . $dbh->errstr;
|
|
|
|
|
|
$sth->execute($loglvl, $message) # Execute the query
|
|
|
or die "Couldn't execute statement: " . $sth->errstr;
|
|
|
+
|
|
|
+ if (not $isMySQL) { $sth->fetch; }
|
|
|
}
|
|
|
catch {
|
|
|
$logmsg .= $_ . "\n";
|