diff --git a/copri4/main/src/Mod/APIfunc.pm b/copri4/main/src/Mod/APIfunc.pm index 1264753..5c687f4 100755 --- a/copri4/main/src/Mod/APIfunc.pm +++ b/copri4/main/src/Mod/APIfunc.pm @@ -18,6 +18,7 @@ use Digest::SHA qw(sha256_base64); use Scalar::Util qw(looks_like_number); use DateTime; use DateTime::Format::Pg; +use Date::Calc qw(Add_Delta_YMD); use URI::Encode; use Config::General; @@ -885,18 +886,17 @@ sub service_work { }#end service_work -#2022-04-05 refactored bike reservation +#bike reservation sub booking_request(){ my $self = shift; my $q = shift; my $varenv = shift; my $auth = shift;#app API auth - my $bike = shift || "";#app API request + my $bike = shift || 0;#app API request my $ct_bike = shift || {}; - my $ct_tariff = shift; - my $owner = shift || ""; - my $gps = shift || ""; + my $ct_tariff = shift || {}; my $sig_book = shift || {}; + my $owner = shift || 0; my $state = $q->escapeHTML($q->param('state')) || ""; my $lock_state = $q->escapeHTML($q->param('lock_state')) || ""; @@ -928,6 +928,32 @@ sub booking_request(){ close_time => "is::null", }; + #for free rental + #depends on operator, only 1 rental get freed_time + my $pref_pos = { + table => "contenttranspos", + fetch => "all", + keyfield => "c_id", + ca_id => "$auth->{c_id}", + }; + if($dbt->{operator}->{$varenv->{dbname}}->{project} eq "Konstanz"){ + $pref_pos->{int10} = "IN::(2,3)"; + }else{ + my $day = strftime "%d", localtime; + my $mon = strftime "%m", localtime; + my $year = strftime "%Y", localtime; + my ($nyear,$nmon,$nday) = Add_Delta_YMD($year,$mon,$day, 0,0,1); + $pref_pos->{time_range} = "start_time >= '$year-$mon-$day' and start_time < '$nyear-$nmon-$nday'"; + } + my $record_pos = $dbt->fetch_tablerecord($dbh,$pref_pos); + + my $activ_count=1;#+1 because counting before insert. + foreach my $pid (sort { $record_pos->{$a}->{c_id} <=> $record_pos->{$b}->{c_id} } keys (%$record_pos)){ + $activ_count++; + } + if($activ_count > 1){ + delete $ct_tariff->{time02}; + } #if bike and tariff if($ct_bike->{barcode} && $ct_tariff->{barcode}){ @@ -962,7 +988,7 @@ sub booking_request(){ if($ctt->{c_id}){ #2 = "requested" - $pos_id = $dbt->insert_pos($dbh,$ctt->{c_id},$ct_bike,$ct_station,$auth,$ct_tariff,$now_dt,$bike,"2",$owner,$sig_book); + $pos_id = $dbt->insert_pos($dbh,$ctt->{c_id},$ct_bike,$ct_station,$auth,$ct_tariff,$now_dt,$bike,"2",$owner,$sig_book,$activ_count); $bw->log("booking_request insert_pos:",$pos_id,""); if($pos_id){ @@ -981,12 +1007,12 @@ sub booking_request(){ } } }elsif($rentable_check == 1){ - $response_state="Failure 1006: There is no valid payment methode"; + $response_state="Failure 1006: rental is prohibited because of user profil"; $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_state="Failure 1005: user-account deactivated because of user profil"; $response_text="Bitte überprüfen Sie Ihre Profildaten auf Vollständigkeit, nur dann können wir das Mietradsystem für Sie freischalten"; } @@ -1223,13 +1249,14 @@ sub booking_update(){ my $geo_distance_next = 100000; my $station_next = 0; my $geo_debug=""; + my $rows_end = 0; my ($stations,$stations_raw) = $self->stations_available($q,$varenv,$auth,$record_pos,""); foreach my $id (sort { $stations_raw->{$a}->{barcode} <=> $stations_raw->{$b}->{barcode} } keys (%$stations_raw)){ my $latitude_station = $1 if($stations_raw->{$id}->{txt06} =~ /^(\d+\.\d+)/); my $longitude_station = $1 if($stations_raw->{$id}->{txt06} =~ /(\d+\.\d+)$/); if((!looks_like_number($gps_data->{latitude}) || !looks_like_number($gps_data->{longitude})) && ($record_pos->{int11} != 3)){ - $geo_debug .= "ERROR no user GPS: $stations_raw->{$id}->{barcode}|$gps_data->{latitude},$gps_data->{longitude},$latitude_station,$longitude_station --> $gps_data->{geo_distance} Meter\n"; + $geo_debug .= "ERROR no user GPS: $stations_raw->{$id}->{int04}|$gps_data->{latitude},$gps_data->{longitude},$latitude_station,$longitude_station --> $gps_data->{geo_distance} Meter\n"; }elsif((looks_like_number($gps_data->{latitude}) && looks_like_number($gps_data->{longitude}) && looks_like_number($latitude_station) && looks_like_number($longitude_station)) || ($record_pos->{int11} == 3)){ $update_pos->{owner_end} = "$owner"; $update_pos->{end_time} = "now()"; @@ -1264,9 +1291,9 @@ sub booking_update(){ $update_pos->{int40} = "$counting->{int40}" if(looks_like_number($counting->{int40})); $update_pos->{int41} = "$counting->{int41}" if(looks_like_number($counting->{int41})); - $rows = $dbt->update_record($dbh,$update_pos,$record_pos); + $rows_end = $dbt->update_record($dbh,$update_pos,$record_pos); - if($rows > 0){ + if($rows_end > 0){ $update_cc->{int10} = 1; $booking_values->{response_state} = "OK: available bike " . $q->param('bike'); $booking_values->{response_text} = "Danke! Die Miete Fahrrad Nr. " . $q->param('bike') . " wurde beendet."; @@ -1298,18 +1325,24 @@ sub booking_update(){ $update_pos->{int40} = "$counting->{int40}" if(looks_like_number($counting->{int40})); $update_pos->{int41} = "$counting->{int41}" if(looks_like_number($counting->{int41})); - $rows = $dbt->update_record($dbh,$update_pos,$record_pos); + $rows_end = $dbt->update_record($dbh,$update_pos,$record_pos); - $geo_debug .= "Out of station distance: $stations_raw->{$id}->{barcode}|$gps_data->{latitude},$gps_data->{longitude},$latitude_station,$longitude_station --> $geo_distance_next Meter ($geo_distance_next <= $gps_data->{geo_distance}) station_next: $station_next\n"; + $geo_debug .= "Out of station distance: $stations_raw->{$id}->{int04}|$gps_data->{latitude},$gps_data->{longitude},$latitude_station,$longitude_station --> $geo_distance_next Meter ($geo_distance_next <= $gps_data->{geo_distance}) station_next: $station_next\n"; $booking_values->{response_state} = "Failure 2178: bike " . $q->param('bike') . " out of GEO fencing. $geo_distance_next meter distance to next station $station_next ."; $booking_values->{response_text} = "Achtung! Ihr aktueller Standort liegt außerhalb einer Fahrradstation. Die Miete Fahrrad Nr. " . $q->param('bike') . " kann nicht $state_text werden. $geo_distance_next Meter Entfernung zur nächsten Station $station_next . Falls Sie sich doch an einer Station befinden, dann wiederholen Sie \"Miete beenden\"."; } }else{ - $geo_debug .= "ERROR no station GPS: $stations_raw->{$id}->{barcode}|$gps_data->{latitude},$gps_data->{longitude},$latitude_station,$longitude_station --> $gps_data->{geo_distance} Meter\n"; + $geo_debug .= "ERROR no station GPS: $stations_raw->{$id}->{int04}|$gps_data->{latitude},$gps_data->{longitude},$latitude_station,$longitude_station --> $gps_data->{geo_distance} Meter\n"; } } + #$geo_distance_next == 100000 defaults to if no station in stations_raw + if($record_pos->{int42} == 0 && !$rows_end && $geo_distance_next == 100000){ + $geo_distance_next = "undefined"; + $booking_values->{response_state} = "Failure 2244: this bike not accepted on A-A station, state change forbidden."; + $booking_values->{response_text} = "Ein Mietende mit diesem Rad ist an dieser Station nicht erlaubt, da es sich hier um eine sog. A-A Station handelt. An A-A Stationen dürfen nur Mieträder zurück gegeben werden die von dort gemietet wurden."; + } $booking_values->{geo_distance} = "$geo_distance_next"; #print "$geo_debug\n"; $bw->log("GEOfencing geo_debug:$geo_debug",$q,""); @@ -1633,8 +1666,8 @@ sub user_rentals_history(){ } foreach my $sharee_operator (keys (%operator_hash)){ my $dbh_operator = $dbt->dbconnect_extern("$sharee_operator"); - my $record = $dbt->collect_post($dbh_operator,$pref); - $record_all = { %$record_all, %$record }; + my $cttpos = $dbt->collect_post($dbh_operator,$pref); + $record_all = { %$record_all, %$cttpos }; } } }else{ diff --git a/copri4/main/src/Mod/APIjsonserver.pm b/copri4/main/src/Mod/APIjsonserver.pm index 20f934e..23bacfa 100755 --- a/copri4/main/src/Mod/APIjsonserver.pm +++ b/copri4/main/src/Mod/APIjsonserver.pm @@ -221,10 +221,10 @@ elsif($q->param('request') eq "booking_request"){ #check count of occcupied/requested bikes my $record = $apif->user_bikes_occupied($dbh,$authraw,""); - my $count=0; + my $rental_count=0; my $still_requested = 0; foreach my $id (keys(%$record)){ - $count++; + $rental_count++; if($bike_id && $bike_id == $record->{$id}->{barcode}){ $still_requested = 1; $response->{response_state} = "OK, bike " . $bike . " already requested or occupied"; @@ -234,7 +234,7 @@ elsif($q->param('request') eq "booking_request"){ if(!$still_requested){ #only if not App debuglevel defined - if(!$authraw->{int11} && $count >= 3){ + if(!$authraw->{int11} && $rental_count >= 3){ $response->{response_state} = "Failure: booking_request declined. max count of 3 occupied bikes has been reached"; $response->{response_text} = "Die maximale Anzahl von 3 Reservierungen wurde erreicht"; }else{ @@ -282,7 +282,7 @@ elsif($q->param('request') eq "booking_request"){ $sig_book->{bikeId} = "$ct_bike->{txt22}"; $sig_book->{rentalId} = "$R::rentalId" if($R::rentalId);#only for cmd tests } - $response_book = $apif->booking_request($q,\%varenv,$authraw,$bike,$ct_bike,$ct_tariff,$aowner,$gps,$sig_book); + $response_book = $apif->booking_request($q,\%varenv,$authraw,$bike,$ct_bike,$ct_tariff,$sig_book,$aowner); $apif->stations_caching($q,\%varenv,$authraw); } diff --git a/copri4/main/src/Mod/DBtank.pm b/copri4/main/src/Mod/DBtank.pm index 34404d4..0a714e2 100755 --- a/copri4/main/src/Mod/DBtank.pm +++ b/copri4/main/src/Mod/DBtank.pm @@ -780,7 +780,7 @@ sub fetch_record(){ $record = $sth->fetchrow_hashref(); } return $record; -} +}#end fetch_record #fetch just in one table --> without relation ... sub fetch_tablerecord(){ @@ -821,6 +821,8 @@ sub fetch_tablerecord(){ $where .= " and $key $op $value"; }elsif($key =~ /time$/ && $value){ $where .= " and $key $op '$value'"; + }elsif($key eq "time_range"){#used for freed_time on request + $where .= " and $value"; }elsif($key =~ /^(c_id|u_id|cc_id|ct_id|ca_id|barcode|int\d+|owner|template_id)$/ && (looks_like_number($value) || $value eq "null")){ if($value eq "null"){ $where .= " and ($key is null OR $key = 0)"; @@ -829,6 +831,8 @@ sub fetch_tablerecord(){ }else{ $where .= " and $key $op $value"; } + }elsif($key =~ /int\d+/ && $op eq "IN"){ + $where .= " and $key $op $value"; } } } @@ -852,7 +856,7 @@ sub fetch_tablerecord(){ $record = $sth->fetchrow_hashref(); } return $record; -} +}#end fetch_tablerecord #update sql sub update_sql(){ @@ -1439,6 +1443,7 @@ sub insert_pos(){ my $status = shift || ""; my $owner = shift || ""; my $sig_book = shift || {}; + my $activ_count = shift || 1; my $aa_station = $ct_station->{int42} || 0; @@ -1473,7 +1478,6 @@ sub insert_pos(){ my $tariff_nr = 0; my $tariff_desc; - my $free_hours = 0;#old my $sharing_type = 0; my $menge = 0; if(ref($ct_tariff) eq "HASH" && $ct_tariff->{barcode}){ @@ -1483,7 +1487,6 @@ sub insert_pos(){ $unit_price2 = $ct_tariff->{int36} || 0; $start_price = $ct_tariff->{int37} || 0; $abo_price = $ct_tariff->{int15} || 0;#not used - $free_hours = $ct_tariff->{int16} || 0;#old $daymax_price = $ct_tariff->{int17} || 0; $tariff_nr = $ct_tariff->{barcode} || 0; $tariff_desc = $ct_tariff->{ct_name}; @@ -1495,7 +1498,7 @@ sub insert_pos(){ my $sth; #Verleihräder if($ct->{template_id} && $ct->{template_id} == 205){#Leihrad_list - $sth = $dbh->prepare("INSERT INTO contenttranspos (ct_id,cc_id,ca_id,ct_name,barcode,txt01,txt08,txt02,txt09,txt12,itime,start_time,end_time,int01,int02,int03,int06,int04,txt05,txt06,txt07,int10,int12,template_id,int13,owner,int07,txt04,int09,int17,int15,int16,int11,int18,int19,txt17,txt18,int20,int25,int29,int34,txt22,txt11,int35,int36,int37,int42,time01,time02) VALUES ('$ctt_id','$ct->{c_id}','$ctadr->{c_id}','$ct_name','$ct->{barcode}','$ct->{txt01}','$user_name','$ct->{txt02}','$ctadr->{txt09}','$prefix',now(),now(),'$endRental','1','$unit_price','$menge','$station','$station','$ct->{txt06}','$ct->{txt06}','$ct->{txt07}','$status','$from_main_id','$from_template_id','$deviceId','$owner','$rabatt','$tariff_desc','$tariff_nr','$daymax_price','$abo_price','$free_hours','$ct->{int11}','$sharing_type','$bike_charge','$ct->{txt17}','$ct->{txt18}','1','$trackon','$bike_type_id','$staff','$sig_book->{bikeId}','$sig_book->{rentalId}','$unit_price1','$unit_price2','$start_price','$aa_station','$unit_time','$free_time') RETURNING c_id"); + $sth = $dbh->prepare("INSERT INTO contenttranspos (ct_id,cc_id,ca_id,ct_name,barcode,txt01,txt08,txt02,txt09,txt12,itime,start_time,end_time,int01,int02,int03,int06,int04,txt05,txt06,txt07,int10,int12,template_id,int13,owner,int07,txt04,int09,int17,int15,int16,int11,int18,int19,txt17,txt18,int20,int25,int29,int34,txt22,txt11,int35,int36,int37,int42,time01,time02) VALUES ('$ctt_id','$ct->{c_id}','$ctadr->{c_id}','$ct_name','$ct->{barcode}','$ct->{txt01}','$user_name','$ct->{txt02}','$ctadr->{txt09}','$prefix',now(),now(),'$endRental','1','$unit_price','$menge','$station','$station','$ct->{txt06}','$ct->{txt06}','$ct->{txt07}','$status','$from_main_id','$from_template_id','$deviceId','$owner','$rabatt','$tariff_desc','$tariff_nr','$daymax_price','$abo_price','$activ_count','$ct->{int11}','$sharing_type','$bike_charge','$ct->{txt17}','$ct->{txt18}','1','$trackon','$bike_type_id','$staff','$sig_book->{bikeId}','$sig_book->{rentalId}','$unit_price1','$unit_price2','$start_price','$aa_station','$unit_time','$free_time') RETURNING c_id"); }else{ $sth = $dbh->prepare("INSERT INTO contenttranspos (ct_id,cc_id,ca_id,ct_name,barcode,txt08,txt09,itime,int01,int02,int03,txt01,txt06,txt07,int10,int12,template_id,owner) VALUES ('$ctt_id','$ct->{c_id}','$ctadr->{c_id}','$ct_name','$ct->{barcode}','$user_name','$ctadr->{txt09}',now(),'1','$unit_price','1','$ct->{txt01}','$ct->{txt06}','$ct->{txt07}','0','$from_main_id','$from_template_id','$owner') RETURNING c_id"); } diff --git a/copri4/main/src/Mod/Payment.pm b/copri4/main/src/Mod/Payment.pm index 57970c6..46b74b2 100755 --- a/copri4/main/src/Mod/Payment.pm +++ b/copri4/main/src/Mod/Payment.pm @@ -459,7 +459,7 @@ sub rpc { my $now_dt = strftime "%Y-%m-%d %H:%M:%S", localtime; open(FILE,">>$varenv->{logdir}/payone-return-post.log") if($debug); - print FILE "\n*** $now_dt (ctadr_id:$ctadr->{c_id}, ctt_id:$ctt->{c_id}) from payone_post.pl\n$httpReqServer \n" if($debug); + print FILE "\n*** $now_dt (ctadr_id:$ctadr->{c_id}, ctt_id:$ctt->{c_id}) from Payment.pm\n$httpReqServer \n" if($debug); print FILE "---> request to payone $todo:\n$post\n"; #Check the outcome of the response @@ -575,7 +575,14 @@ sub rpc { $update_adr->{owner} = "$owner"; $update_adr->{mtime} = "now()"; } - $dbt->update_record($dbh,$update_adr,$ctadr) if($ctadr->{c_id} > 0); + + if($ctadr->{c_id} > 0){ + $dbt->update_record($dbh,$update_adr,$ctadr); + #2023-04-11 set it global by update adr also on primary + #disabled, because isuser_rentable will be only used by operator rental + #my $dbh_primary = $dbt->dbconnect($dbt->{primary}->{sharee_primary}->{database}->{dbname}); + #$dbt->update_record($dbh_primary,$update_adr,$ctadr); + } $dbt->update_record($dbh,$update_ctt,$ctt) if($ctt->{c_id} > 0); return $payoneret; } diff --git a/copri4/main/src/Mod/Prelogic.pm b/copri4/main/src/Mod/Prelogic.pm index b335c7a..0c4dd5d 100755 --- a/copri4/main/src/Mod/Prelogic.pm +++ b/copri4/main/src/Mod/Prelogic.pm @@ -652,7 +652,8 @@ sub preinit(){ $update_ctt->{int14} = "null"; $update_ctt->{pay_time} = "now()"; $u_rows += $dbt->update_record($dbh,$update_ctt,$ctt); - $db->updater($table,"ct_name",$ctt->{barcode},"int14","null","","","","","");#delete OPOS also on basic invoice + #delete OPOS at all + $db->updater($table,"barcode",$ctt->{barcode},"int14","null","","","","",""); }elsif($ctt->{txt00} eq "Rechnung" && $R::sum_paid <= 0){ $update_ctt->{int14} = "null"; $update_ctt->{pay_time} = "now()"; diff --git a/copri4/main/src/Mod/Pricing.pm b/copri4/main/src/Mod/Pricing.pm index 4feb8a1..ada7479 100755 --- a/copri4/main/src/Mod/Pricing.pm +++ b/copri4/main/src/Mod/Pricing.pm @@ -15,18 +15,15 @@ use CGI; # only for debugging use Scalar::Util qw(looks_like_number); use DateTime; use DateTime::Format::Pg; - use Lib::Config; use Mod::Libenz; use Mod::DBtank; -use Mod::Callib; use Mod::Basework; use Data::Dumper; my $cf = new Config; my $lb = new Libenz; my $dbt = new DBtank; -my $cal = new Callib; my $bw = new Basework; sub new { @@ -59,7 +56,7 @@ sub only_first_free(){ ct_id => "=::$ctpos->{ct_id}", c_id => "!=::$ctpos->{c_id}", #txt10 => "IN::('available','canceled')", - int10 => "IN::('1','6')", + int10 => "IN::(1,6)", }; $pref = { %$pref, time_range => "cp.start_time >= '$ctpos->{start_time}' and cp.start_time < '$ctpos->{end_time}' and cp.start_time != cp.end_time" }; @@ -128,10 +125,11 @@ sub counting_rental { } #substract free time from rental time ex. 00:30 Min/Gratis + #only if $activ_count==1 rental get freed_time my $freed_time = ""; - if($ctpos->{time02} && $ctpos->{time02} =~ /[1-9]/){ - $ctpos_freed = $self->only_first_free($ctpos); - if(!$ctpos_freed->{c_id}){ + if($ctpos->{int16} && $ctpos->{int16} == 1 && $ctpos->{time02} && $ctpos->{time02} =~ /[1-9]/){ + #$ctpos_freed = $self->only_first_free($ctpos); + #if(!$ctpos_freed->{c_id}){ my ($dhh,$dmm) = split(/:/,$ctpos->{time02}); $freed_time = "- $dhh:$dmm" if($dhh || $dmm); #adding free minutes to start_time @@ -145,7 +143,7 @@ sub counting_rental { $computed_clock = "$durdd day $durhh:$durmm" if($durdd); $rental_minute = $self->clock_minutes($computed_clock); #print "$computed_clock|$rental_minute"; - } + #} } @@ -260,6 +258,7 @@ sub counting_rental { $return->{rentalog}->{real_clock} = "$real_clock"; $return->{rentalog}->{freed_time} = "$freed_time"; + $return->{rentalog}->{activ_count} = "$ctpos->{int16}"; $return->{rentalog}->{computed_clock} = "$computed_clock"; $return->{rentalog}->{computed_hours} = "$computed_hours"; $return->{rentalog}->{rental_minute} = "$rental_minute"; diff --git a/copri4/shareeapp-operator/src/Tpl/FormEdit.pm b/copri4/shareeapp-operator/src/Tpl/FormEdit.pm index 2f6632f..c3617dc 100755 --- a/copri4/shareeapp-operator/src/Tpl/FormEdit.pm +++ b/copri4/shareeapp-operator/src/Tpl/FormEdit.pm @@ -386,7 +386,9 @@ Nach Abschluss der Registrierung erhalten Sie sowohl auf die von Ihnen hinterleg if($pricing->{start_time} && $pricing->{end_time}){ $pricing->{start_time} = $lb->time4de($pricing->{start_time},"1"); $pricing->{end_time} = $lb->time4de($pricing->{end_time},"1"); - print $q->span("→ $pricing->{start_time}
← $pricing->{end_time}"); + my $rental_time = ""; + $rental_time = "(rental debug: $pricing->{real_clock} $pricing->{freed_time})" if($debug); + print $q->span("→ $pricing->{start_time}
← $pricing->{end_time} $rental_time"); } print "\n"; }elsif($key =~ /int04/){