sharee.bike/copri4/main/src/Tpl/TransInvoices.pm

462 lines
20 KiB
Perl
Raw Normal View History

2022-10-14 08:28:51 +02:00
package TransInvoices;
#
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright (c) Rainer Gümpelein, TeilRad GmbH
#
#
use strict;
use warnings;
use POSIX;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use CGI ':standard';
use DateTime;
use DateTime::Format::Pg;
use Date::Calc::Object qw(:ALL);
use Scalar::Util qw(looks_like_number);
use Data::Dumper;
use Lib::Config;
use Mod::Buttons;
use Mod::Libenz;
use Mod::Libenzdb;
use Mod::DBtank;
use Mod::APIfunc;
use Mod::Pricing;
sub new {
my $class = shift;
my $self = {};
bless($self,$class);
return $self;
}
#Template
sub tpl(){
my $self = shift;
my $node_meta = shift;
my $users_dms = shift;
my $set_main_id = shift;
my $rel4tpl = shift;
my $return = shift || "";
my $q = new CGI;
my $cf = new Config;
my $lb = new Libenz;
my $db = new Libenzdb;
my $dbt = new DBtank;
my $apif = new APIfunc;
my $but = new Buttons;
my $pri = new Pricing;
my %varenv = $cf->envonline();
my %ib = $but->ibuttons();
my $today = strftime "%d.%m.%Y",localtime;
my $now_dt = strftime "%Y-%m-%d %H:%M:%S", localtime;
my $dbh = "";
my $channel_map = $dbt->channel_map();
my $coo = $q->cookie(-name=>'domcookie');
my $mapref = {};
my $ct_users = $dbt->users_map($dbh,$mapref);#get serviceAPP and DMS users from contentadr
my $line_count2 = 0;
my $k=0;
my $spart_ct_name = $R::spart_ct_name || "";
my $c_idpos = $R::c_idpos || $R::pos_id || "";
my $ctf = $db->get_content1("contentuser",$dbt->{shareedms_conf}->{parent_id});
my $ctt = $db->get_content1("contenttrans",$rel4tpl->{content_id});
my $ctadr = $db->get_content1("contentadr",$ctt->{int10});
my $buchen_mtime = $lb->time4de($ctt->{mtime});
my $vibuchen_mtime = "";
$vibuchen_mtime = "payone post " . $lb->time4de($ctt->{pay_time},1) . " . " if($ctt->{pay_time});
my @tpl_order = ("txt01=Bezeichnung/Leistung","int03=Anzahl","int02=Einzelpreis","int01=Betrag");
my $tplf = $db->get_tpl("201");#Firma tpl
my @tplf_order = split /,/,$tplf->{tpl_order};
my $txt20 = $R::txt20 || $ctt->{txt20} || "";#Leistungsdatum
my $int05 = $R::int05 || $ctt->{int05} || "";#manuell
my $cttpos = { c_id => 0 };
my $rows = 0;
($cttpos,$rows) = $dbt->collect_contenttrans($dbh,$rel4tpl->{content_id});
#TODO collect payment types
#foreach my $id (keys(%$cttpos)){
# if(ref($cttpos->{$id}) eq "HASH"){
# if($cttpos->{$id}->{int26}){
# }
# }
#}
print $q->start_table({-class=>'list', -border=>'0', -width=>'100%',-align=>'left', -cellpadding=>'3', -cellspacing=>'0'});
print $q->hidden(-name=>'offset', -override=>'1', -value=>"$R::offset") if($R::offset);
print $q->hidden(-name=>'limit', -override=>'1', -value=>"$R::limit") if($R::limit);
print $q->hidden(-name=>'c_id4trans', -override=>'1', -value=>"$rel4tpl->{content_id}");
print $q->hidden(-name=>'tpl_id4trans', -override=>'1', -value=>"$node_meta->{tpl_id}");
$line_count2++;
print $q->Tr();
print $q->th("Pos."),"\n";
foreach (@tpl_order){
my ($key,$val) = split /=/,$_;
$k++ if($val);
print $q->th("$val");
}
print $q->start_form(-name=>'transposform'),"\n";
#Tablecontent (buttons and ct_name(primary key))
#my $scol = "c_id";#changed to itime because of Storno resorts
my $scol = "itime";
my $sum_parts19=0;
my $diff19 = 100 + 19;
my $sum_umst19=0;
my $i=0;
my $accounting_start = "";
my $accounting_end = "";
foreach my $id (sort { $cttpos->{$b}->{$scol} cmp $cttpos->{$a}->{$scol} } keys(%$cttpos)){
my $gesamt = 0;
my $pricing = {};
my $counting = {};
my $rental_feed = {};
#accounting prices
if($cttpos->{$id}->{int35} && $cttpos->{$id}->{start_time} && $cttpos->{$id}->{end_time}){
$cttpos->{$id}->{end_time} = $now_dt if($cttpos->{$id}->{int10} == 3);
($pricing,$counting) = $pri->counting_rental(\%varenv,$cttpos->{$id},"calc_price");
$rental_feed = $pri->fetch_rentalfeed(\%varenv,$cttpos->{$id},$counting);
$sum_parts19 += $pricing->{total_price};
$gesamt = $pri->round($pricing->{total_price});
$gesamt = sprintf('%.2f', $gesamt);
}else{
($gesamt,my $rabatt) = $pri->price2calc($cttpos->{$id});
$sum_parts19 += $gesamt;
$gesamt = $pri->round($gesamt);
$gesamt = sprintf('%.2f', $gesamt);
}
my $set_style="";
my $occupied_style = "";
my $time_style = "";
$occupied_style = "color:#ff1493" if($cttpos->{$id}->{int10} == 2 ||$cttpos->{$id}->{int10} == 3 || $cttpos->{$id}->{int10} == 6);
if(1==1){
$i++;
if($i==1){
$accounting_end = "$3.$2.$1" if($cttpos->{$id}->{pay_time} =~ /(\d+)\-(\d+)\-(\d+)/);
$accounting_start = $accounting_end;
}else{
$accounting_start = "$3.$2.$1" if($cttpos->{$id}->{pay_time} =~ /(\d+)\-(\d+)\-(\d+)/);
}
#print "$accounting_start - $accounting_end<br>";
my @line_txt01 = split(/\n/,$cttpos->{$id}->{txt01});
if($cttpos->{$id}->{int02} != 0){
$line_count2++;
#$line_count2 += scalar(@line_txt01);
}
#1. Spalte
print $q->Tr(),"\n";
print $q->td({-class=>'tdint'},"$cttpos->{$id}->{c_id}"),"\n";
#Tablecontent (parameter)
foreach (@tpl_order){
my ($key,$val,$inputsize) = split /=/,$_;
$cttpos->{$id}->{$key} = $q->unescapeHTML($cttpos->{$id}->{$key});
$cttpos->{$id}->{$key} = $lb->newline($cttpos->{$id}->{$key},"",$R::trans2edit) if($R::trans2edit);
my $ct_pos = "$cttpos->{$id}->{ct_name}";
my $txtstyle = "text-align:left;min-width:130px;";
my $isize = "30";
$isize = $inputsize if($inputsize);
if($key =~ /int\d+/){
$txtstyle = "text-align:right;min-width:50px;";
$isize = "5";
}
if(1==1){
if($key =~ /ct_name/){
#print $q->td({-class=>'tdint',-style=>"min-width:60px;"},"$ct_pos");
my $stamm_style = "background-color:#98c13b;padding:2px;";
my $article = $cttpos->{$id}->{ct_name};
if($cttpos->{$id}->{int09}){
print $q->td({-class=>'tdint',-style=>"min-width:60px;padding-top:5px;"}, $q->a({-class=>"linknav3",-style=>"$stamm_style",-href=>"/DMS/Waren/?detail_search=1&s_barcode=$cttpos->{$id}->{barcode}",-title=>"Im Warenstamm"},"$article")),"\n";
}else{
print $q->td({-class=>'tdint'},"$article"),"\n";
}
}elsif($key eq "int26"){
my $co2saving = "";
if($cttpos->{$id}->{int26}){
$co2saving = "Einsparung</br>";
my $co2diff = $pri->co2calc($cttpos->{$id});
my $sprit_price = $pri->sprit2calc($cttpos->{$id});
$co2saving .= "$co2diff kg CO&sup2;<br />";
$co2saving .= "$sprit_price EUR<br />";
$cttpos->{$id}->{int26} =~ s/\./,/;
$co2saving .= "bei $cttpos->{$id}->{int26} KM";
}
print $q->td({-class=>'tdint'},"$co2saving");
}
#yes, int03=Menge on parts, int35=unit_price1 on rental
elsif($key =~ /int03/){
if($cttpos->{$id}->{int35} && $cttpos->{$id}->{start_time} && $cttpos->{$id}->{end_time}){
print $q->td({-class=>'tdint',-nowrap=>1},"$pricing->{real_clock} $pricing->{freed_time}"),"\n";
}else{
$cttpos->{$id}->{$key} =~ s/\./,/;
print $q->td({-class=>'tdint'},"$cttpos->{$id}->{$key}"),"\n";
}
}elsif($key =~ /int02/){
if($cttpos->{$id}->{int35} && $cttpos->{$id}->{start_time} && $cttpos->{$id}->{end_time}){
print "<td class='tdint'>\n";
foreach my $fid (sort keys(%{ $rental_feed->{rental_description}->{tarif_elements} })){
if(ref($rental_feed->{rental_description}->{tarif_elements}->{$fid}) eq "ARRAY"){
print "$rental_feed->{rental_description}->{tarif_elements}->{$fid}[0]: $rental_feed->{rental_description}->{tarif_elements}->{$fid}[1]<br />\n";
}
}
print "</td>\n";
}else{
$cttpos->{$id}->{$key} =~ s/\./,/;
print $q->td({-class=>'tdint'},"$cttpos->{$id}->{$key} €"),"\n";
}
}elsif($key =~ /int04/){
$gesamt =~ s/\./,/;
print $q->td({-class=>'tdint',-nowrap=>1},"$gesamt €"),"\n";
}elsif($key =~ /int07/){
my $proz="";
$proz = "%" if($cttpos->{$id}->{$key} && $cttpos->{$id}->{$key} != 0);
$proz = "€" if($cttpos->{$id}->{$key} && $cttpos->{$id}->{$key} != 0 && $cttpos->{$id}->{int08} && $cttpos->{$id}->{int08} == 1);
$cttpos->{$id}->{$key} =~ s/\./,/;
print $q->td({-class=>'tdint',-nowrap=>"1"},"$cttpos->{$id}->{$key} $proz"),"\n";
}elsif($key =~ /txt01/){
$cttpos->{$id}->{$key} = $q->unescapeHTML("$cttpos->{$id}->{$key}");
$cttpos->{$id}->{$key} = $lb->newline($cttpos->{$id}->{$key},"","");
print "<td class='tdtxt', style='$occupied_style;'>\n";
if($cttpos->{$id}->{barcode} && $cttpos->{$id}->{int09}){#bike with tariff-nr
my $u_name = $cttpos->{$id}->{owner};
my $u_name_end = $cttpos->{$id}->{owner_end};
foreach my $ctu_id (keys (%$ct_users)){
if($channel_map->{$u_name}){
$u_name = $channel_map->{$u_name};
}elsif($cttpos->{$id}->{owner} eq $ct_users->{$ctu_id}->{c_id}){
$u_name = $ct_users->{$ctu_id}->{txt01};
}
if($channel_map->{$u_name_end}){
$u_name_end = $channel_map->{$u_name_end};
}elsif($cttpos->{$id}->{owner_end} eq $ct_users->{$ctu_id}->{c_id}){
$u_name_end = $ct_users->{$ctu_id}->{txt01};
}
}
if($cttpos->{$id}->{itime} =~ /(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2})/){
print $q->span("$dbt->{copri_conf}->{bike_state}->{$cttpos->{$id}->{int10}} &rarr; $dbt->{copri_conf}->{lock_state}->{$cttpos->{$id}->{int20}} &rarr; $u_name / $u_name_end"),"\n";
my $pos_raw = "";
if($users_dms->{u_id} && $users_dms->{u_id} =~ /1842/){
my $pos_details = "";
foreach my $did (sort keys (%{$pricing->{rentalog}})){
$pos_details .= $did . " = " . $pricing->{rentalog}->{$did} . "</br>" if($pricing->{rentalog}->{$did});
}
#$pos_details = Dumper($pricing->{rentalog});
$pos_raw = $q->div({-class=>"popup",-onclick=>"toggle_box('$id')"},"$cttpos->{$id}->{c_id}", $q->span({-class=>"popuptext",-id=>"$id"},"$pos_details"));
print "(raw $pos_raw)<br />\n";
}
}
print "<br />\n";
}
if($cttpos->{$id}->{txt01} || $cttpos->{$id}->{int09}){
#$line_count2++;
$cttpos->{$id}->{txt01} =~ s/fixed/\<span style='color:red'\>fixed\<\/span\>/;
$cttpos->{$id}->{txt01} =~ s/defect/\<span style='color:red'\>defect\<\/span\>/;
my $bike="";
my $tariff = "";
$bike = "$cttpos->{$id}->{$key}" if($cttpos->{$id}->{$key});
$tariff = ", Tarif: $cttpos->{$id}->{int09} $cttpos->{$id}->{txt04}" if($cttpos->{$id}->{txt04});
print $q->span("$bike $tariff<br />"),"\n";
}
if($cttpos->{$id}->{int06} || $cttpos->{$id}->{int04}){
print $q->span("Start/End Station: $cttpos->{$id}->{int06} / $cttpos->{$id}->{int04}, GPS: $cttpos->{$id}->{txt06}"),"\n";
}
if($cttpos->{$id}->{start_time} && $cttpos->{$id}->{end_time}){
my $start_time = $lb->time4de($cttpos->{$id}->{start_time},"1");
my $end_time = $lb->time4de($cttpos->{$id}->{end_time},"1");
print "<br />\n";
print $q->span({-style=>"$time_style"}, "Mietzeit: $start_time &rarr; $end_time"),"\n";
}
print "</td>\n";
}elsif($key =~ /txt/){
print $q->td({-class=>'tdtxt'},"$cttpos->{$id}->{$key}");
}elsif($key =~ /int/){
print $q->td({-class=>'tdint',-nowrap=>"1"},"&nbsp;\n");
}
}#end view position
}
}
}#foreach end
if($sum_parts19 && $sum_parts19 != 0){
$sum_umst19 = $sum_parts19 / $diff19 * 19;
$sum_umst19 = $pri->round($sum_umst19);
}
my $sum_netto19 = $sum_parts19 - $sum_umst19;
$sum_netto19 = sprintf('%.2f', $sum_netto19);
$sum_netto19 =~ s/\./,/;
my $sum_paid = $sum_parts19;
$sum_paid = $pri->round($sum_paid);
my $sum_preauth = $sum_paid || 0;
$sum_paid = sprintf('%.2f', $sum_paid);
$sum_paid =~ s/\./,/;
$sum_parts19 = sprintf('%.2f', $sum_parts19);
$sum_umst19 = sprintf('%.2f', $sum_umst19);
$sum_umst19 =~ s/\./,/;
my $payment_text = "";
foreach(@tplf_order){
my ($key,$des,$size) = split /=/,$_;
if($key =~ /txt5\d/){
$ctf->{$key} = $q->unescapeHTML("$ctf->{$key}");
$ctf->{$key} = $lb->newline($ctf->{$key},"","");
$ctt->{state} =~ s/\(payone.*//;
if($des =~ /$ctt->{state}/){
if($sum_parts19 < 0){
$payment_text = "$ctf->{txt58}";
}else{
$payment_text = "$ctf->{$key}";
}
}
}
}
my $n="5";
my $m= 1 + $k - $n;
$m++;
print "<tr>\n";
print "<td colspan='$m'><div style='font-size:0.81em;padding:0.3em 0em;border:0px;'>$payment_text</div></td>\n";
print "<td style='font-size:1em;' colspan='$n'>\n";
print $q->start_table({-class=>'list',-style=>'border-top:1px;border-style:solid;border-color:black;', -border=>'0', -width=>'100%',-align=>'center', -cellpadding=>'3', -cellspacing=>'0'}),"\n";
print $q->Tr("\n");
print $q->td("&nbsp;");
if($node_meta->{node_name} !~ /steuerfrei/){
print $q->Tr("\n"); $line_count2++;
print $q->td({-class=>'tdint'},"Nettobetrag:");
print $q->td({-class=>'tdint',-nowrap=>"1"},"$sum_netto19 €");
if($sum_netto19 != 0){
print $q->Tr("\n");$line_count2++;
#print $q->td({-class=>'tdint',-nowrap=>"1"},"$umst1619% UmSt auf $sum_netto19 €:");
print $q->td({-class=>'tdint',-nowrap=>"1"},"19% UmSt auf $sum_netto19 €:");
print $q->td({-class=>'tdint',-nowrap=>"1"},"$sum_umst19 €");
}
}
my $summe = "Summe";
print $q->Tr("\n");$line_count2++;
print $q->td({-class=>'tdsum'},"<b>$summe:</b>");
print $q->td({-class=>'tdint',-nowrap=>"1"},"<b>$sum_paid €<b/>");
print $q->hidden(-name=>'sum_paid', -override=>'1',-value=>"$sum_paid");
print $q->end_table;
print "</td><td>&nbsp;</td>";
print "</tr>";
print $q->end_table;
print $q->hidden(-name=>'owner', -override=>'1', -value=>"$users_dms->{u_id}");
print $q->hidden(-name=>'c_id4trans', -override=>'1', -value=>"$rel4tpl->{content_id}");
print $q->hidden(-name=>'tpl_id4trans', -override=>'1', -value=>"$node_meta->{tpl_id}");
print $q->hidden(-name=>'offset', -override=>'1', -value=>"$R::offset") if($R::offset);
print $q->hidden(-name=>'limit', -override=>'1', -value=>"$R::limit") if($R::limit);
print $q->hidden(-name=>'relids', -override=>'1', -value=>"$R::relids") if($R::relids);
$set_main_id=$rel4tpl->{main_id} if($rel4tpl->{main_id} && $rel4tpl->{main_id} > "300000");
print $q->hidden(-name=>'set_main_id', -value=>"$set_main_id", -override=>'1');
if($users_dms->{int03} == 2){
#only if user is also a primary DMS user with invoice rw
print $q->hidden(-name=>'printer_id', -value=>"PDF", -override=>'1');
my $dbh_primary = $dbt->dbconnect_extern("sharee_primary");
my $users_dms_primary = { u_id => 0 };
$users_dms_primary = $dbt->select_users($dbh_primary,$users_dms->{u_id},"and int03=2");
if($users_dms_primary->{u_id} && $users_dms_primary->{int03} == 2 && !$ctt->{close_time}){
my @_paymentstate = split(/\|/,$dbt->{shareedms_conf}->{payment_state});
push @_paymentstate, "";
my $kind_of_payment = "";
if($ctadr->{int03} == 1 && ($ctadr->{ct_name} =~ /PO-\d+/ || $ctadr->{ct_name} =~ /TM-\d+/)){
$kind_of_payment = "$_paymentstate[0]";
}else{
undef $_paymentstate[0];
}
if($ctadr->{int03} == 2 && length($ctadr->{ct_name}) >= 19){
$kind_of_payment = "$_paymentstate[1]";
}else{
undef $_paymentstate[1];
}
$kind_of_payment = "$ctt->{state}" if($ctt->{state} && $ctt->{int01});
my $send_invoice_checkbox = 1;
$send_invoice_checkbox = 0 if($ctt->{txt30});
print $q->div({-class=>'element6',-style=>'float:right;'},
"buchen incl. drucken ",$but->checkbox("print_pdf","ct_trans","1","PDF drucken",""),
" eMail Versand ",$but->checkbox("1","send_invoice","$send_invoice_checkbox","eMail Rechnung",""),
$but->singlesubmit1("set_state","buchen"),
$but->selector("state","250px",$kind_of_payment,@_paymentstate)),"\n";
}
#further payone capture are only allowed if OPOS && sequence && TXID is set
if($ctt->{int14} && $ctt->{int18} && $ctt->{txt16}){
print $q->div({-class=>'element6',-style=>'clear:both;'},"Weitere Payone Einzüge über die vorhandene TXID $ctt->{txt16} (vorautorisiert Summe beachten) durch manuelle inkrement der Sequencenr mit +1", $q->textfield(-class=>'etxt',-name=>"payone_sequence",-default=>"", -override=>'1',-size=>"1",-maxlength=>1)),"\n";
}
#Payone reset if OPOS and TXID
if($ctt->{int14} && $ctt->{txt16}){
print $q->div({-class=>'element6',-style=>'clear:both;'},"Payone reset löscht den Vorgang mit der TXID $ctt->{txt16} und versucht einen erneuten Einzug", $but->checkbox("1","payone_reset","0","Payone reset","")),"\n";
}
}
print $q->end_form;
my $praefix = "$ctt->{txt00}-$varenv{praefix}";
print $q->div({-style=>"clear:both;height:0.1px;"},""),"\n";
if($ctt->{txt30}){
print $q->div({-style=>"padding:0.5em;font-size:0.81em;width:98%;text-align:right;"}, "$ctt->{txt30}"),"\n";
}elsif( -f "$varenv{pdf}/$praefix-$ctt->{ct_name}.pdf" ){
print $q->start_form(),"\n";
print $q->hidden(-name=>'offset', -override=>'1', -value=>"$R::offset") if($R::offset);
print $q->hidden(-name=>'limit', -override=>'1', -value=>"$R::limit") if($R::limit);
print $q->hidden(-name=>'relids', -override=>'1', -value=>"$R::relids") if($R::relids);
print $q->div({-style=>"padding:0.5em;font-size:0.81em;width:98%;text-align:right;"}, "eMail wurde nicht versandt! Rechnung eMail ", $but->singlesubmit1("ct_trans","send_invoice_again","send_invoice_again")),"\n" if($users_dms->{u_id} == $dbt->{copri_conf}->{superu_id});
print $q->end_form;
}
if($ctt->{state} && $ctt->{int01}){
$ctt->{int01} =~ s/\./,/;
my $style = "color:red;" if($ctt->{int01} ne $sum_paid);
my $opos = "";
$opos = "OPOS" if($ctt->{int14} && $ctt->{int14} > 0);
print $q->div({-style=>"padding:0.5em;font-size:0.81em;width:98%;text-align:right;"},"<span style='color:red;'>$opos</span> $vibuchen_mtime Gebucht $ctt->{int01} € \"$ctt->{state}\"") if($ctt->{state});
}
if( -f "$varenv{basedir}/pdfinvoice/$praefix-$ctt->{ct_name}.pdf"){
#print $q->div({-style=>"padding:0.5em;font-size:0.81em;width:98%;text-align:right;"}, "Download: ", $q->a({-href=>"$varenv{metahost}/pdf/$praefix-$ctt->{ct_name}.pdf", -target=>'_blank'},"$praefix-$ctt->{ct_name}.pdf"));
print $q->div({-style=>'padding:0.5em;font-size:0.81em;width:98%;text-align:right;'}, "Download: ", $q->a({-href=>"$varenv{wwwhost}/FileOut?file=$praefix-$ctt->{ct_name}.pdf&sessionid=$coo", -target=>"_blank" , -type=>'application/octet-stream', -style=>'text-decoration:underline;'}), "Rechnung $ctt->{ct_name}.pdf"),"\n";
}
if($ctt->{state} && $ctt->{state} =~ /payone/ && $ctt->{txt28} && $ctt->{txt28} =~ /error/i){
print $q->div({-style=>"clear:both;padding:0.5em;font-size:0.81em;width:98%;text-align:right;color:red;"}, "Payone error: $ctt->{txt28}"),"\n";
}elsif($ctt->{txt28} =~ /settleaccount=yes/i){
print $q->div({-style=>"clear:both;padding:0.5em;font-size:0.81em;width:98%;text-align:right;color:green;"}, "Payone SEPA-Lastschrifteinzug war erfolgreich"),"\n";
}
$db->updater("contenttrans","c_id","$rel4tpl->{content_id}","txt20","$accounting_start - $accounting_end","","","","","no_time") if(!$int05 && $accounting_start && $accounting_end);
$db->updater("contenttrans","c_id","$rel4tpl->{content_id}","int15","$sum_preauth","","","","","no_time") if($sum_preauth || $sum_preauth == 0);
return "$line_count2";
}
1;