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 $coo = $q->cookie(-name=>'domcookie');
my $dbh = "";
my $line_count2 = 0;
my $k=0;
my $ctf = $db->get_content1("contentuser","2");#Operator-Faktura config
my $ctt = $db->get_content1("contenttrans",$rel4tpl->{content_id});
my $ctadr = $db->get_content1("contentadr",$ctt->{int10});
my $vibuchen_mtime = "";
$vibuchen_mtime = "payone post " . $lb->time4de($ctt->{pay_time},1) . " . " if($ctt->{pay_time});
#int9x are not in db
my @tpl_order = ("ct_name=Rechnung","int01=Summe payone Einzug","state=Zahlungsart","int91=TeilRad Gebühren","int92=Disagio","int93=Transaktion","int02=Summe Auszahlung");
my $tplf = $db->get_tpl("196");#Operator-Faktuar
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;
#collects invoices
($cttpos,$rows) = $dbt->collect_contenttrans($dbh,$rel4tpl->{content_id});
#maybe sum 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);
$val .= " ($key)" if($users_dms->{u_id} eq $varenv{superu_id});
print $q->th("$val");
}
print $q->start_form(-name=>'transposform'),"\n";
my $scol = "c_id";
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} <=> $cttpos->{$a}->{$scol} } keys(%$cttpos)){
my $oac = $pri->operator_accounting2calc(\%varenv,$cttpos->{$id},$ctf,$tplf);
#$sum_parts19 += $oac->{int01};#payone Einzug
$sum_parts19 += $oac->{int02};#Auszahlung
#$oac->{int01} = $pri->round($oac->{int01});
$oac->{int01} = sprintf('%.2f', $oac->{int01});
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
";
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'},"$i"),"\n";
#Tablecontent (parameter)
foreach (@tpl_order){
my ($key,$val) = split /=/,$_;
$cttpos->{$id}->{$key} = $q->unescapeHTML($cttpos->{$id}->{$key});
my $txtstyle = "text-align:left;min-width:100px;";
if($key =~ /int\d+/){
$txtstyle = "text-align:right;min-width:50px;";
}
if(1==1){
if($key =~ /ct_name/){
print $q->td({-class=>'tdtxt'},"\# $cttpos->{$id}->{ct_name} $cttpos->{$id}->{txt01}"),"\n";
}
elsif($key =~ /int/){
$oac->{$key} =~ s/\./,/;
print $q->td({-class=>'tdint',-nowrap=>1},"$oac->{$key} €"),"\n";
}
elsif($key =~ /state/){
print $q->td({-class=>'tdint',-nowrap=>1},"$cttpos->{$id}->{$key}"),"\n";
}
elsif($key =~ /txt/){
print $q->td({-class=>'tdtxt'},"$cttpos->{$id}->{$key}"),"\n";
}
}
}
}
}#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 "