sigo api reserve and rental

This commit is contained in:
Rainer Gümpelein 2022-04-07 21:07:59 +02:00
parent 5870071d1b
commit 9c0e5f78e8
7 changed files with 444 additions and 68 deletions

View file

@ -6,7 +6,7 @@ package APIfunc;
#Server methods for sharee API
#
#perl -cw
#use lib qw(/var/www/copri4/tinkdms/src);
#use lib qw(/var/www/copri-bike/shareeapp-operator/src);
#
use strict;
use warnings;
@ -25,6 +25,7 @@ use Mod::DBtank;
use Mod::Callib;
use Mod::Basework;
use Mod::Pricing;
#use Mod::APIsigoclient;#no!
use Data::Dumper;
use Sys::Hostname;
my $hostname = hostname;
@ -35,6 +36,7 @@ my $dbt = new DBtank;
my $cal = new Callib;
my $bw = new Basework;
my $pri = new Pricing;
#my $si = new APIsigoclient;
sub new {
my $class = shift;
@ -768,7 +770,223 @@ sub service_work {
}#end service_work
#check and set user-bike rental tarif
sub fetch_bike_tariff {
my $self = shift;
my $varenv = shift;
my $auth = shift;
my $bike = shift;
my $owner = shift;
my $bike_id = "";
$bike_id = $1 if($bike =~ /(\d+)/);
my $main_ids = "";
my ($bike_group,$user_group,$tariff_content,$user_tour) = $self->fetch_tariff($varenv->{dbname},$auth,"");
$main_ids = join(",",@{$bike_group});
$main_ids =~ s/[a-z_]+//ig;
my $ct_bike = {};
my $pref_cc = {
table => "content",
fetch => "one",
main_id => "IN::($main_ids)",
barcode => $bike_id,
#int10 => 1,
};
$ct_bike = $dbt->fetch_record($dbh,$pref_cc) if($main_ids);
#$tariff_nr in contentadr are saved by copri or user tarif-select!!!
my $tariff_nr = "";
my @adr_tariff = ();
if($auth->{txt30}){
@adr_tariff = ("$auth->{txt30}");
@adr_tariff = split(/\s+/,$auth->{txt30}) if($auth->{txt30} =~ /\w\s+\w/);
}
#This is the automatic user tariff setter
if(ref($ct_bike) eq "HASH" && $ct_bike->{main_id}){
foreach my $id (keys (%$tariff_content)){
foreach(@adr_tariff){
$bw->log("booking_request adr_tariff array form $auth->{txt30}",$_,"");
if($tariff_content->{$id}->{int12} && $tariff_content->{$id}->{int12} == $ct_bike->{main_id} && $tariff_content->{$id}->{barcode} && $_ == $tariff_content->{$id}->{barcode}){
$bw->log("booking_request tariff loop matches:",$tariff_content->{$id}->{barcode},"");
$tariff_nr = $tariff_content->{$id}->{barcode};
}
}
}
#if no tarif then update user account to fallback default public or private or hidden tarif
if(!$tariff_nr){
my $update_adr = {
table => "contentadr",
mtime => "now()",
owner => "$owner",
c_id => "$auth->{c_id}",
};
my @txt30 = ();
foreach my $id (keys (%$tariff_content)){
#</sharing_type>
if($tariff_content->{$id}->{int18} && ($tariff_content->{$id}->{int18} == 2 || $tariff_content->{$id}->{int18} == 3 || $tariff_content->{$id}->{int18} == 4)){
#auto set tarif if requested bike matches flot
if($tariff_content->{$id}->{int12} && $tariff_content->{$id}->{int12} == $ct_bike->{main_id} && $tariff_content->{$id}->{barcode}){
$bw->log("booking_request tariff loop matches:",$tariff_content->{$id}->{barcode},"");
$tariff_nr = $tariff_content->{$id}->{barcode};
push(@txt30, "$tariff_content->{$id}->{barcode}");
}
#add also other public tarif
elsif($tariff_content->{$id}->{int18} && $tariff_content->{$id}->{int18} == 2 && $tariff_content->{$id}->{int12} && $tariff_content->{$id}->{barcode}){
push(@txt30, "$tariff_content->{$id}->{barcode}");
}
}
}
$bw->log("booking_request NO user tariff defined, update user account to fallback default public or private or hidden",\@txt30,"");
$dbt->update_one($dbh,$update_adr,"txt30='@txt30'");
}else{
$bw->log("booking_request user tariff selected",$tariff_nr,"");
}
}
$bw->log("booking_request fetch_bike_tariff result ---> bike $ct_bike->{barcode} matching by bike_group: @{$bike_group} main_ids:$main_ids | user_group:@{$user_group} | Tarif selected: $tariff_nr",$tariff_content->{$tariff_nr},"");
return ($ct_bike,$tariff_content->{$tariff_nr});
}
#2022-04-05 refactored bike reservation
sub booking_request(){
my $self = shift;
my $varenv = shift;
my $auth = shift;#app API auth
my $bike = shift || "";#app API request
my $ct_bike = shift || {};
my $ct_tariff = shift;
my $owner = shift || "";
my $gps = shift || "";
my $reserveid = shift || "";
my $dbh = "";
my $bike_id = "";
$bike_id = $1 if($bike =~ /(\d+)/);
my $pos_id="";
my $now_dt = strftime "%Y-%m-%d %H:%M", localtime;
my $response_state = "OK";
my $response_text = "";
$bw->log("booking_request ct_bike $ct_bike->{barcode}, auth $auth->{c_id}, tarif $ct_tariff->{barcode}, requested bike:",$bike,"");
my $update_adr = {
table => "contentadr",
mtime => "now()",
owner => "$owner",
c_id => "$auth->{c_id}",
};
my $pref = {
table => "contenttrans",
fetch => "one",
main_id => 300008,
template_id => 218,
#ca_id => "$auth->{c_id}",#future change
int10 => "$auth->{c_id}",
state => "null",
close_time => "is::null",
};
my $booking_pos = {
table => "contenttranspos",
fetch => "one",
barcode => "$bike_id",
int10 => "IN::('2','3')",
ca_id => "$auth->{c_id}",
};
#if bike and tariff
if($ct_bike->{barcode} && $ct_tariff->{barcode}){
my $ctt = {};
my $rentable_check=0;
$rentable_check = $self->isuser_rentable($auth,$varenv);
$bw->log("booking_request isuser_rentable:",$rentable_check,"");
if($rentable_check == 2){
#get invoice
$ctt = $dbt->fetch_record($dbh,$pref);
if(!$ctt->{c_id}){
my $ct_id = $dbt->insert_contenttrans($dbh,$auth,"300008","218","----","$owner");
$pref->{c_id} = $ct_id;
$ctt = $dbt->fetch_record($dbh,$pref) if($pref->{c_id});
}
$bw->log("booking_request used invoice c_id:",$ctt->{c_id},"");
#if invoice exist
if($ctt->{c_id}){
#2 = "requested"
$pos_id = $dbt->insert_pos($dbh,$ctt->{c_id},$ct_bike,$auth,$ct_tariff,$now_dt,$bike,"2",$owner,$reserveid);
$bw->log("booking_request insert_pos:",$pos_id,"");
if($pos_id){
$response_state = "OK, bike " . $bike . " succesfully requested";
$response_text = "Fahrrad Nr. $bike wurde erfolgreich für 15 Min. reserviert";
$bw->log("booking_request insert contenttranspos pos_id: $pos_id\n","","");
my $update_ctt = {
table => "contenttrans",
c_id => $ctt->{c_id},
};
$dbt->update_one($dbh,$update_ctt,"start_time='$now_dt'");
}else{
$response_state="Failure 1007: booking request fails";
$response_text="Entschuldigung, es ist ein Fehler aufgetreten. Bitte kontaktieren Sie unsere hotline damit wir das Problem lösen können";
}
}
}elsif($rentable_check == 1){
$response_state="Failure 1006: There is no valid payment methode";
$response_text="Bitte überprüfen Sie Ihre Profildaten auf Vollständigkeit, nur dann können wir das Mietradsystem für Sie freischalten";
}else{
my $vde = $auth->{int12} || 1;
$dbt->update_one($dbh,$update_adr,"int12=$vde");
$response_state="Failure 1005: user-account deactivated because of failing data";
$response_text="Bitte überprüfen Sie Ihre Profildaten auf Vollständigkeit, nur dann können wir das Mietradsystem für Sie freischalten";
}
}
my $return = {
bike => "$bike",
state => "requested",
response_state => "$response_state",
response_text => "$response_text"
};
$bw->log("booking_request response_state:",$return,"");
return $return;
}
#int03=1 if sepa, 2=CC
#int04==1 if email Ack
#int13==1 if sms Ack
#int12!=1|2|3 if Vde
#int14==1 if AGB
sub isuser_rentable {
my $self = shift;
my $auth = shift;
my $varenv = shift;
my $rentable_check=0;
if(($auth->{int03} == 1 && $auth->{ct_name} =~ /\w{2}-\d+/ && $auth->{ct_name} !~ /LV-\d+/) || ($auth->{int03} == 2 && length($auth->{ct_name}) >= 19) || ($auth->{int03} == 1 && $varenv->{dbname} eq "sharee_lv" && $auth->{ct_name} =~ /LV-\d+/)){
$rentable_check=1;
if($auth->{txt08} && $auth->{int04} == 1 && $auth->{int13} == 1 && !$auth->{int12} && $auth->{int14}){
$rentable_check=2;
}
}
return $rentable_check;
}
#booking_cancel changed to booking_update
sub booking_update(){
@ -777,6 +995,8 @@ sub booking_update(){
my $varenv = shift;
my $auth = shift;
my $owner = shift || 0;
my $rentalid = shift || "";
my $state = $q->escapeHTML($q->param('state')) || "";
my $lock_state = $q->escapeHTML($q->param('lock_state')) || "";
my $station_state = $q->escapeHTML($q->param('station_state')) || "";
@ -920,6 +1140,7 @@ sub booking_update(){
$update_pos->{owner} = "$owner";
$update_pos->{int10} = "$state_key";
$update_pos->{txt11} = "$rentalid";
$rows = $dbt->update_record($dbh,$update_pos,$record_pos);
if($rows > 0){
@ -938,6 +1159,7 @@ sub booking_update(){
$update_pos->{int03} = "$pricing->{computed_hours}" if(looks_like_number($pricing->{computed_hours}));
$update_pos->{int10} = "$state_key";
$update_pos->{txt11} = "$rentalid";
$rows = $dbt->update_record($dbh,$update_pos,$record_pos);
$booking_values->{response_state} = "OK: available bike " . $q->param('bike') . ", still locked";
@ -973,6 +1195,7 @@ sub booking_update(){
$update_cc->{int04} = "$stations_raw->{$id}->{int04}";
$update_pos->{int24} = "1";
$update_pos->{int10} = "$state_key";
$update_pos->{txt11} = "$rentalid";
$update_cc->{txt06} = "$gps";#end content coordinates
$update_pos->{txt06} = "$gps";#end pos coordinates
$update_pos->{int21} = "$gps_age_minutes";
@ -2459,3 +2682,4 @@ sub authcookie_manager {
}
1;