package Prelogic;
#
#Deprecated module, please use Prelib.pm
#
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright (c) Rainer Gümpelein, TeilRad GmbH
#
#should be refactored
#
use strict;
use warnings;
use POSIX;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use CGI ':standard';
use Date::Calc qw(:all);
use Scalar::Util qw(looks_like_number);
use DateTime;
use DateTime::Format::Pg;
use Data::Dumper;
use Lib::Config;
use Mod::Buttons;
use Mod::Libenz;
use Mod::Libenzdb;
use Mod::DBtank;
use Mod::Payment;
use Mod::Pricing;
sub new {
my $class = shift;
my $self = {};
bless($self,$class);
return $self;
}
#Template
sub preinit(){
my $self = shift;
my $q = shift;
my $node_meta = shift;
my $users_dms = shift;
$q->import_names('R');
my @keywords = $q->param;
my $cf = new Config;
my $lb = new Libenz;
my $db = new Libenzdb;
my $dbt = new DBtank;
my $but = new Buttons;
my $payone = new Payment;
my $pri = new Pricing;
my %ib = $but->ibuttons();
my %varenv = $cf->envonline();
my $script = $q->script_name();
my $path_info = $q->path_info();
my $path = $path_info;
#with meta_host,
if("$varenv{metahost}"){
$path = "$script" . "$path_info";
$script="";
}
my $lang = "de";
my $c_id = $R::c_id || "0";#c_id aus content
my $time = time();
my $now_date = strftime "%Y-%m-%d", localtime;
my $today = strftime("%d.%m.%Y",localtime(time));
my $today4db = strftime("%Y-%m-%d %H:%M",localtime(time));
my $now_dt = strftime "%Y-%m-%d %H:%M:%S", localtime;
my $day = strftime "%d", localtime;
my $mon = strftime "%m", localtime;
my $year = strftime "%Y", localtime;
my $i_rows=0;
my $u_rows=0;
my $d_rows=0;
my $dbh = "";
my $api_file = "/var/www/copri4/shareeconf/apikeys.cfg";
my $aconf = Config::General->new($api_file);
my %apikeyconf = $aconf->getall;
open(FILE,">>$varenv{logdir}/Prelogic.log");
print FILE "\n*--> $now_dt| u_id: $users_dms->{u_id}" . Dumper($q) . "\n";
if(!$users_dms->{u_id}){
return "failure::Fehler
Die Sitzung wurde unterbrochen.";
}
if((!looks_like_number($R::c_id4trans) || !looks_like_number($R::tpl_id4trans)) && ($R::ct_trans !~ /delete_trans|new_trans/) && !$R::v_abschluss){
print FILE Dumper($q);
return "failure::Fehler
Daten ID's nicht vollständig (if((!looks_like_number($R::c_id4trans) || !looks_like_number($R::tpl_id4trans)) && ($R::ct_trans !~ /delete_trans|new_trans/) && !$R::v_abschluss))";
}
#international buttons
my ($key,$val,$ib_key);
while (($key,$val) = each(%ib)) {
$ib_key = $key if($R::ct_trans eq $val);
}
$ib_key = $R::ct_trans if(!$ib_key);
my ($ct_last,$c_idnew);
my $table = "contenttrans";
my $ctt = $db->get_content1($table,$R::c_id4trans);
my $buchen_mtime = $lb->time4de($ctt->{mtime});
my $mandant_main_id = 100002;
my $parent_trans = $db->get_node3("$mandant_main_id","Faktura","$lang") if($mandant_main_id);
if(!$parent_trans->{main_id}){
return "failure::Fehler
keine eindeutige Modulzuweisung vorhanden.";
}
my $rel = $db->get_rel4tpl4nd($parent_trans->{main_id},$lang,$R::c_id4trans);
$rel = $db->get_rel4tpl4nd("",$lang,$R::c_id4trans,$rel->{template_id}) if($R::c_id4trans > "0");
my $ct_name = $q->escapeHTML("$R::ct_name");
my $barcode=0;
my $s_owner_id=0;
if($R::s_start_date_time && $varenv{dataflow} =~ /wiki/){
$db->users_up("cal_start","$R::s_start_date_time",$users_dms->{u_id});
}
if($R::s_end_date_time && $varenv{dataflow} =~ /wiki/){
$db->users_up("cal_end","$R::s_end_date_time",$users_dms->{u_id});
}
if($R::view_list && $varenv{dataflow} =~ /wiki/){
$db->users_up("view_list","$R::view_list",$users_dms->{u_id});
}
#OPEN/CLOSE
if($R::ct_trans eq "open"){
$db->update_users4trans($R::c_id4trans,$R::tpl_id4trans,"",$users_dms->{u_id});
$users_dms = $db->select_users($users_dms->{u_id});
}elsif($R::ct_trans eq "close"){
$db->update_users4trans("0","0","",$users_dms->{u_id});
$users_dms = $db->select_users($users_dms->{u_id});
}
#NEW contenttrans
if($ib_key =~ /^new_/){
$ct_name = "----" if(!$ct_name);
my $foreign_key;
my $sort;
my $main_id = $R::main_id;
if($ib_key =~ /new_trans/){
$table = "contenttrans";
$foreign_key="ct_id";
$main_id = $parent_trans->{main_id} if(!$R::main_id);
}
my $node = $db->get_node4multi($main_id,$lang);
if($table && $main_id){
my $rel = $db->get_rel4tpl($main_id,$lang);
$c_idnew = $db->insert_content2($table,$ct_name,$users_dms->{u_id},"");
$i_rows += 1 if($c_idnew > 0);
my $rel_id = $db->insert_relationlist($table,$rel->{main_id},$lang,$c_idnew,$rel->{tpl_id},$foreign_key);
$rel->{content_id} = $c_idnew; #notwendig bei prozeduralem $R::select_part
$db->update_content4change($table,$c_idnew,"",$parent_trans->{parent_id},"int09");#mandant_main_id
$db->update_content4change($table,$c_idnew,"",$node->{main_id},"int12");#zusätzl. Formtyp
$db->update_content4change($table,$c_idnew,"",$node->{node_name},"txt00");#node_name
$db->update_users4trans($c_idnew,$R::tpl_id4trans,"",$users_dms->{u_id});
}else{
return "failure::Fehler, Datensatz kann nicht angelegt werden weil folgende Informationen fehlen: ($main_id)";
}
}
###ADD PARTS/DATE TO Transactions
#FIXME, refactorit
if((($ib_key =~ /add_transpos|add_transdate/) || ($R::select_part && ($R::spart_ct_name || $R::json_select))) && (!$ctt->{close_time})){
$table = "contenttranspos";
my $zcolumn = "barcode";
my $zcontent = "$R::spart_ct_name" || "$R::json_select";
#get part for adding and get from_main_id
my $ct_name = "$R::spart_ct_name" || "$R::json_select" || "";#turn-around!§$
my $c_id = "$R::c_id" || "";
my $ct = { c_id => 0 };
$ct = $db->get_ctrel2("content","$ct_name","","$lang","","$c_id","224,229","$zcolumn","$zcontent","");
$ct_name = $ct->{ct_name} if($ct->{ct_name});
if($ct->{main_id}){
$users_dms = $db->select_users($users_dms->{u_id}) if($ib_key =~ /new_/);#nur bei new_ mit save_
if(!$R::c_id4trans && $ib_key !~ /new_transdate/){
return "failure::Zu welchem Vorgang soll die Aktion verknüpft werden? Bitte erst den entsprechenden Vorgang öffnen.";
}
}
if($ct->{barcode} && $ct->{barcode} > 0 && $rel->{content_id}){
$ct->{int02} = $ct->{int02} * -1 if($ct->{template_id} == 224 && $ct->{int02} > 0);#Gutscheine
#Add Parts from content to contenttranspos#################
my $pos_id = 0;
my $ctt = { c_id => 0 };
if(looks_like_number($R::c_id4trans)){
my $pref_ctt = {
table => "contenttrans",
fetch => "one",
c_id => $R::c_id4trans,
};
$ctt = $dbt->fetch_tablerecord($dbh,$pref_ctt);
}
my $ctadr = { c_id => $ctt->{int10} };
$pos_id = $dbt->insert_pos($dbh,$ctt->{c_id},$ct,"",$ctadr,"","","","0",$users_dms->{u_id},"");
$i_rows += 1 if($pos_id > 0);
$dbt->update_content4comp($dbh,$ct->{c_id},"-","1");
return "pos_id=$pos_id";#new return-code to get opened Part-Position in Transposition
}else{
return "failure::Die Artikelnummer konnte nicht gefunden werden.
::$varenv{wwwhost}/DMS/Waren::Waren verwalten";
}
}
#####
#UPDATE CONTENT
#print "@keywords ";
if($ib_key =~ /save/){
$c_id = $R::c_id if($ib_key eq "save");#only in journal edit
my $table = "contenttrans";
if($ib_key eq "save_adr" || $ib_key =~ /save_text/){
$table = "contenttrans";
$c_id = "$R::c_id4trans";
}elsif($ib_key eq "save_pos"){
$table = "contenttranspos";
$c_id = $R::c_idpos || $R::pos_id;
}
#Miet- bzw. BaseVA- Zeitraum
if($ib_key =~ /save_pos/ && $R::c_idpos && $R::start_date =~ /\d{1,2}\.\d{1,2}\.\d{4}/ && $R::end_date =~ /\d{1,2}\.\d{1,2}\.\d{4}/){
my $pos_id = $R::c_idpos;
my $start_date = "$3-$2-$1" if($R::start_date =~ /(\d{1,2})\.(\d{1,2})\.(\d{4})/);
my $end_date = "$3-$2-$1" if($R::end_date =~ /(\d{1,2})\.(\d{1,2})\.(\d{4})/);
my $s_hh = $q->escapeHTML("$R::s_hh") || "0";
my $s_mi = $q->escapeHTML("$R::s_mi") || "0";
my $e_hh = $q->escapeHTML("$R::e_hh") || "0";
my $e_mi = $q->escapeHTML("$R::e_mi") || "0";
$s_hh = "24" if($s_hh > "24");
$e_hh = "24" if($e_hh > "24");
$s_mi = "59" if($s_mi > "59");
$e_mi = "59" if($e_mi > "59");
$s_hh = sprintf('%.2d',$s_hh);
$e_hh = sprintf('%.2d',$e_hh);
$s_mi = sprintf('%.2d',$s_mi);
$e_mi = sprintf('%.2d',$e_mi);
my $start_time="";
my $end_time="";
$start_time = "$start_date $s_hh:$s_mi:00" if("$start_date $s_hh:$s_mi" =~ /\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}/);
$end_time = "$end_date $e_hh:$e_mi:00" if("$end_date $e_hh:$e_mi" =~ /\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}/);
if($start_time && $end_time){
my $dtnow = DateTime->now( time_zone => "Europe/Berlin" );
my $dt0 = DateTime::Format::Pg->parse_datetime($start_time);
my $dt1 = DateTime::Format::Pg->parse_datetime($end_time);
if($dt0 < $dtnow && $dt1 < $dtnow){
my $pref = {
table => "contenttrans",
table_pos => "contenttranspos",
fetch => "one",
template_id => "218",#Mietjournal tpl_id
c_id => $pos_id,
"ct.close_time" => "is::null",
};
my $record_pos = {};
my $pricing = {};
my $counting = {};
$record_pos = $dbt->collect_post($dbh,$pref);
my $update_pos = {
table => "contenttranspos",
start_time => "$start_time",
end_time => "$end_time",
owner_end => $users_dms->{u_id},
mtime => "now()",
};
my $rows = $dbt->update_record($dbh,$update_pos,$record_pos);
#again to get setted date-times
$record_pos = $dbt->collect_post($dbh,$pref);
($pricing,$counting) = $pri->counting_rental(\%varenv,$record_pos);
#int03 only used for tarif counting backwards compatibility
#$update_pos->{int03} = "$pricing->{computed_hours}" if(looks_like_number($pricing->{computed_hours}));
$update_pos->{int38} = "$counting->{int38}" if(looks_like_number($counting->{int38}));
$update_pos->{int39} = "$counting->{int39}" if(looks_like_number($counting->{int39}));
$update_pos->{int40} = "$counting->{int40}" if(looks_like_number($counting->{int40}));
$update_pos->{int41} = "$counting->{int41}" if(looks_like_number($counting->{int41}));
$update_pos->{int10} = 1;
$update_pos->{int04} = $R::int04 if(looks_like_number($R::int04));
if($R::cc_id =~/^\d+$/){
my $ctpref = {
table => "content",
fetch => "one",
c_id => $R::cc_id,
};
my $ctbike = $dbt->fetch_tablerecord($dbh,$ctpref);
if($ctbike->{int10} && ($ctbike->{int10} == 2 || $ctbike->{int10} == 3)){
$db->updater("content","c_id",$R::cc_id,"int10","1",$users_dms->{u_id});
if($R::int04 =~/^\d+$/){
$db->updater("content","c_id",$R::cc_id,"int04","$R::int04",$users_dms->{u_id});
}
}
}
$rows = $dbt->update_record($dbh,$update_pos,$record_pos);
$db->updater("contenttrans","c_id",$ctt->{c_id},"start_time","$start_time",$users_dms->{u_id});
$db->updater("contenttrans","c_id",$ctt->{c_id},"end_time","$end_time",$users_dms->{u_id});
}
}
}
#hidden select keys to delete first, marked with off_ (Project.pm)
foreach(@keywords){
if($_ =~ /off_(txt\d+)/){
$db->updater("$table","c_id",$c_id,"$1","");
}
}
my $ct_exist2;
$ct_exist2 = $db->get_content2($table,$ct_name) if($ct_name);
$ct_exist2 = $db->get_content1($table,$c_id) if($c_id);
if($ct_exist2->{ct_name} && ($ct_exist2->{c_id} ne $c_id)){
return "failure::$ct_exist2->{ct_name} - $ct_exist2->{c_id} - $c_id
content name exists";
}elsif($c_id){
$users_dms = $db->select_users($users_dms->{u_id});#to get new cal_start
my $j=0;
foreach(@keywords){
$j++;
my $val = $q->param($_);
my $valxx = $q->escapeHTML("$val");
my @val = $q->param($_);
$valxx = $q->escapeHTML("@val");
$_ =~ s/col_//;
$ct_exist2->{$_} =~ s/^\s+//; $ct_exist2->{$_} =~ s/\s+$//;
$valxx =~ s/^\s+//; $valxx =~ s/\s+$//;
$valxx .= ".00" if($valxx =~ /^\d+$/ && $_ =~ /int\d+/);
#print "|$_:$ct_exist2->{$_} -- $valxx|
";
if("$ct_exist2->{$_}" ne "$valxx"){
if($_ eq "ct_name" && $valxx && $table){
$u_rows +=$db->updater("$table","c_id",$c_id,"$_","$valxx");
}
if($_ =~ /txt21|txt6/ && $table){
$db->update_content4change2($table,$c_id,"$R::txt61,$R::txt62,$R::txt63,$R::txt64,$R::txt65,$R::txt66,$R::txt67,$R::txt68,$R::txt69","txt21")
}elsif($_ =~ /txt(\d+)$/ && $table){
$valxx = "null" if(!$valxx);#for empty
my $txtxx = $valxx;
$u_rows +=$db->updater("$table","c_id",$c_id,"$_","$valxx");
}
if($_ =~ /mtime|warn_time/ && $table){
my $timexx = $valxx;
$u_rows += $db->update_content4change($table,$c_id,$ct_name,$timexx,$_,$users_dms->{u_id}) if($timexx =~ /\d{1,2}\.\d{1,2}\.\d{2,4}\s\d{1,2}:\d{1,2}/ || $timexx =~ /\d{1,2}\.\d{1,2}\.\d{2,4}$/);
}
if($_ =~ /state/ && $table){
my $state = $valxx;
$u_rows += $db->update_content4change($table,$c_id,$ct_name,$state,$_,$users_dms->{u_id});
}
if($_ =~ /(xml_export)/ && $table){#boolean
my $key = $1;
my $value = $valxx || "f";
$u_rows += $db->update_content4change($table,$c_id,$ct_name,$value,$key,$users_dms->{u_id});
}
if($_ =~ /int(\d+)/ && $table){
$valxx =~ s/,/./;
$valxx =~ s/\.00//;
$valxx = "null" if(!$valxx);#for empty
my $intxx = $valxx;
#Ausgaben trigger
if($ib_key eq "save_pos" && $_ eq "int02" && $intxx =~ /-\d/){
$u_rows += $db->update_content4change($table,$c_id,$ct_name,$intxx,"int02",$users_dms->{u_id});
}elsif($ib_key eq "save_pos" && $_ eq "int03"){
#Warenbestands trigger
my $op="+";
$op = $1 if($intxx =~ s/(-|\+)//);
if($op =~ /-/){
return "failure::Bitte einen fiktiven Artikel unter \"Ausgaben\" verwenden.";
}
my $add_menge="0";
#differenz, es wird nur der zuwachs verwendet
$ct_exist2->{$_} = 0 if(!$ct_exist2->{$_});
$add_menge = - $ct_exist2->{$_} + $intxx if($op eq "+");
my $rows = $db->update_content4comp("contenttranspos",$R::ct_name,$R::c_idpos,"$op","$add_menge","");
if($rows){
$u_rows += $db->update_content4comp("content",$R::ct_name,"$R::cc_id","-","$add_menge","","");
}
}else{
$u_rows +=$db->updater("$table","c_id",$c_id,"$_","$valxx");
}
}
}
}
}
}
#delete ask
if("$ib_key" eq "remove_chk4rel" && $R::main_id && $R::c_id && $R::template_id && $R::rel_id){
my $node_names; my $i=0;
return "failure::Datensatz wirklich löschen. ::?ct_trans=delete_trans\&exit_box2=1\&c_id=$R::c_id\&rel_id=$R::rel_id ::löschen"
}
#delete Only relation ... without content
if("$ib_key" eq "delete_rel4ct" && $R::main_id && $R::rel_id){
$d_rows += $db->delete_relation($R::main_id,$lang,$R::rel_id);
$db->cleanup_users($users_dms->{u_id}) if($users_dms->{u_id});
}
#DELETE abschluss
if($ib_key eq "delete" || $ib_key =~ /delete_tver/){
$table = "contenttrans";
$table = "contenttver" if($ib_key =~ /delete_tver/);
$d_rows += $db->delete_content("$table","$R::c_id");
$db->update_users4trans("0","0","",$users_dms->{u_id});
}
#DELETE
if($ib_key =~ /delete_trans/){
my $c_id4del = $R::c_id4trans || $R::c_id;
$table = "contenttrans" if($ib_key =~ /delete_trans/);
my $ctt = $db->get_content1("$table","$c_id4del");
$d_rows += $db->delete_content($table,$ctt->{c_id});
$db->update_users4trans("0","0","",$users_dms->{u_id});
print $q->div({-class=>'elementwhite'},"2... redirecting to ... or CLICK ", $q->a({href=>"$varenv{wwwhost}$script$path"},"$varenv{wwwhost}/DMS/Faktura"));
print redirect("$varenv{wwwhost}/DMS/Faktura?redirected=1\&return=0-0-0|$i_rows-$u_rows-$d_rows");
exit 0;
}
my $pos_id = $R::c_idpos || $R::pos_id;
#DELETE pos
if($ib_key =~ /delete_pos/ && $pos_id){
$table = "contenttranspos";
my $cttpos = $db->get_content1($table,$pos_id);
$d_rows += $db->delete_content("$table","$pos_id");
$ct_name = $1 if($R::ct_name =~ /^(\d+)/);
$u_rows += $db->update_content4comp("content",$ct_name,"$cttpos->{cc_id}","+","$cttpos->{int03}","","");
}
###
#Auftragsstatus
if($R::order_state && $R::c_id4trans){
my $table = "contenttrans";
$u_rows += $db->updater($table,"c_id",$R::c_id4trans,"txt22","$R::txt22",$users_dms->{u_id});
}
if($R::warning_state && $R::c_id4trans){
my $table = "contenttrans";
$u_rows += $db->updater($table,"c_id",$R::c_id4trans,"txt19","$R::txt19",$users_dms->{u_id});
}
###SET Relation (like move doc-type)
#executed by Terminal submit-buttons
#2. counter for WaWi and ReNr
if((!$R::close_time && $R::set_main_id) && ($R::set_relation || $R::set_state || $R::open_set_main_id > 300000 || $R::ct_trans =~ /print_pdf/i)){
if($R::open_set_main_id){
$R::set_main_id = $R::open_set_main_id;
$users_dms = $db->select_users($users_dms->{u_id});
}
my $c_id = $R::c_id4trans || "";#Document id
return "failure::Abbruch, die Aktion konnte keinem Dokument zugeordnet werden. Arbeiten Sie mit mehreren Browser-Tabs? Bitte multiple COPRI Tabs schließen und anschließend das Verkauf-Terminal neu öffnen." if(!$c_id);
my $table = "contenttrans";
my $ctt = { c_id => 0 };
my $rel = $db->get_rel4tpl("",$lang,$R::c_id4trans,$R::tpl_id4trans);
$ctt = $db->get_content1("contenttrans",$rel->{content_id});
return "failure::Bitte erst einen Formular-Typ wählen" if($R::set_main_id <= "300000");
$u_rows += $db->update_relation2("",$lang,$R::set_main_id,"",$rel->{rel_id}) if(!$R::close_time && $rel->{rel_id});
my $node_faktura = $dbt->get_node($dbh,$dbt->{shareedms_conf}->{faktura});
my $node = $dbt->get_node($dbh,$R::set_main_id);
my $update_ctt = {
table => "contenttrans",
mtime => "now()",
owner => $users_dms->{u_id},
};
print FILE "c_id4trans:$R::c_id4trans,tpl_id4trans:$R::tpl_id4trans by condition: ($node_faktura->{invoice_nr} > 0) && $ctt->{ct_name} !~ /\d/)\n old-relation $rel->{main_id} \n new-relation $R::set_main_id\n";
if($node_faktura->{invoice_nr} > 0 && $ctt->{c_id} > 0 && $ctt->{ct_name} !~ /\d/){
my $nextNr = $node_faktura->{invoice_nr};
$update_ctt->{ct_name} = "$nextNr";
$update_ctt->{barcode} = "$nextNr";
my $update_node = {
table => "nodes",
main_id => "$dbt->{shareedms_conf}->{faktura}",
change => "no_time",
};
my $invoice_nr = $node_faktura->{invoice_nr} + 1;
$dbt->update_one($dbh,$update_node,"invoice_nr='$invoice_nr'");
$update_ctt->{int12} = $node->{main_id};
$update_ctt->{txt00} = $node->{node_name};
$u_rows += $dbt->update_record($dbh,$update_ctt,$ctt);
}
#SET state alias Zahlung buchen
if($R::set_state && !$R::close_time){
my $sum_paid = "null";
my $sum_operatorcredit = "null";
my $sumgeb_teil = "null";
my $sumgeb_bank = "null";
my $state = $R::state || "";
$update_ctt->{state} = "$state";
$update_ctt->{int14} = 2;#set OPOS
if($R::set_state eq "buchen" && $R::sum_paid){
$sum_paid = $R::sum_paid;
$sum_paid =~ s/,/\./;
$update_ctt->{int01} = $sum_paid;
}
if($R::set_state eq "buchen" && $R::sum_operatorcredit){
$sum_operatorcredit = $R::sum_operatorcredit;
$sum_operatorcredit =~ s/,/\./;
$update_ctt->{int02} = $sum_operatorcredit;
$update_ctt->{int14} = "null";
}
if($R::set_state eq "buchen" && $R::sumgeb_teil){
$sumgeb_teil = $R::sumgeb_teil;
$sumgeb_teil =~ s/,/\./;
$update_ctt->{int08} = $sumgeb_teil;
}
if($R::set_state eq "buchen" && $R::sumgeb_bank){
$sumgeb_bank = $R::sumgeb_bank;
$sumgeb_bank =~ s/,/\./;
$update_ctt->{int07} = $sumgeb_bank;
}
$u_rows += $dbt->update_record($dbh,$update_ctt,$ctt);
if($state =~ /payone/){
my $ctadr = $db->get_content1("contentadr",$ctt->{int10});
$ctt->{payone_reset} = 0;
if($R::payone_reset){
#after delete preauth after 0€ capture sets new TXID and increment reference
$ctt->{payone_reset} = $R::payone_reset;
$ctt->{int01} = 0;
$ctt->{sequence} = $ctt->{int18} || 1;
$ctt->{sequence}++;
if($ctt->{txt16} && $R::state =~ /SEPA/){#SEPA 0
my $payoneret = $payone->captureSEPA_main(\%varenv,$ctadr,$ctt,$users_dms->{u_id});
}
if($ctt->{txt16} && $R::state =~ /Kreditkarte/){#CC 0
my $payoneret = $payone->captureCC_main(\%varenv,$ctadr,$ctt,$users_dms->{u_id});
}
$ctt = $db->get_content1("contenttrans",$ctt->{c_id});
}
if($state !~ /Zahlungseingang/ && !$ctt->{txt16}){
if($ctadr->{ct_name} =~ /\w{2}-\d+/){
my $payoneret = $payone->preauthorizationSEPA_main(\%varenv,$ctadr,$ctt,$users_dms->{u_id});
sleep 2;
}elsif(length($ctadr->{ct_name}) >= 19){
my $payoneret = $payone->preauthorizationCC_main(\%varenv,$ctadr,$ctt,$users_dms->{u_id});
sleep 2;
}
$ctt = $db->get_content1("contenttrans",$ctt->{c_id});
$ctt->{sequence} = $R::payone_sequence || 1;
#SEPA capture
if($ctt->{txt16} && $R::state =~ /SEPA/){#SEPA
my $payoneret = $payone->captureSEPA_main(\%varenv,$ctadr,$ctt,$users_dms->{u_id});
}
#CC capture
elsif($ctt->{txt16} && $R::state =~ /Kreditkarte/){#CC
my $payoneret = $payone->captureCC_main(\%varenv,$ctadr,$ctt,$users_dms->{u_id});
}
else{
my $return_text = "payone capture fails, errorcode ($ctadr->{int03} && TXID:$ctt->{txt16} && $R::state).";
$update_ctt->{txt23} = "$now_dt $return_text\n" . $ctt->{txt23};
$dbt->update_record($dbh,$update_ctt,$ctt);
#return "failure::$return_text";
}
}else{
return "failure::Payone Geldeinzug nicht ausgeführt weil TXID bereits vorhanden. Hatte der Einzug bereits stattgefunden?";
}
}elsif($ctt->{txt00} eq "Storno" || $state eq "Zahlungsausfall"){
$update_ctt->{int14} = "null";
$update_ctt->{pay_time} = "now()";
$u_rows += $dbt->update_record($dbh,$update_ctt,$ctt);
#delete OPOS at all
$db->updater($table,"barcode",$ctt->{barcode},"int14","null","","","","","");
}elsif($state eq "fehlgeschlagener Einzug"){
$update_ctt->{int14} = "null";
$update_ctt->{pay_time} = "now()";
$u_rows += $dbt->update_record($dbh,$update_ctt,$ctt);
}elsif($ctt->{txt00} eq "Rechnung" && $R::sum_paid <= 0){
$update_ctt->{int14} = "null";
$update_ctt->{pay_time} = "now()";
$u_rows += $dbt->update_record($dbh,$update_ctt,$ctt);
}
}
}
###end SET Relation or state (V Terminal submit's)
my $c_id4print = "";
$c_id4print = $R::c_id4trans if(looks_like_number($R::c_id4trans));
if($c_id4print){
open(EMA, ">> $varenv{logdir}/copri-print.log");
print EMA "$today4db\n";
my $print_return = "";
my $exit_code = 1;
my $ctt = { c_id => 0 };
my $pref_ctt = {
table => "contenttrans",
fetch => "one",
c_id => $R::c_id4trans,
};
$ctt = $dbt->fetch_tablerecord($dbh,$pref_ctt);
my $lang_ctt = $ctt->{txt11} || "de";#2023-08-10
my $praefix = "$ctt->{txt00}-$varenv{praefix}";#like Rechnung-sharee_operator
#PDF generate
if($R::ct_trans =~ /print_pdf/ || $R::print_pdf){
my $psize="A4";
my $topdf = "$varenv{basedir}/src/wkhtmltopdf-amd64";
#without system() because we have to wait until PDF is ready
$print_return = `$topdf --page-size $psize "$varenv{wwwhost}$script/Printpreview?printer_id=$R::printer_id\&mandant_main_id=$mandant_main_id\&main_id=$node_meta->{main_id}\&ct_name2print=$ctt->{ct_name}\&c_id4trans=$c_id4print\&u_id=$users_dms->{u_id}\&pkey=$apikeyconf{pdfprinter}->{pkey}" "$varenv{pdf}/$praefix-$ctt->{ct_name}.pdf" 2>&1`;
$exit_code = $?;
my $filesize = -s "$varenv{pdf}/$praefix-$ctt->{ct_name}.pdf";
print EMA "$topdf --page-size $psize $varenv{wwwhost}$script/Printpreview?printer_id=$R::printer_id\&mandant_main_id=$mandant_main_id\&main_id=$node_meta->{main_id}\&ct_name2print=$ctt->{ct_name}\&c_id4trans=$c_id4print\&u_id=$users_dms->{u_id}\&pkey=$apikeyconf{pdfprinter}->{pkey} $varenv{pdf}/$praefix-$ctt->{ct_name}.pdf\nreturn: $print_return\nfilesize: $filesize\nexit_code: $exit_code\nset_state: $R::set_state\n";
#no redirect
if($R::ct_trans eq "print_pdf"){
if( -f "$varenv{basedir}/pdf/$praefix-$ctt->{ct_name}.pdf"){
print "";
}else{
return "failure::PDF konnte nicht generiert werden, bitte Info an: admin\@sharee.bike\n $varenv{wwwhost}/pdf/$praefix-$ctt->{ct_name}.pdf";
}
}
}
#PDF will be generated by "print_pdf" in context "buchen"
#"send_invoice_again" will use formarly generated PDF
if(-f "$varenv{basedir}/pdfinvoice/$praefix-$ctt->{ct_name}.pdf" && (($R::set_state eq "buchen" && $R::send_invoice && $ctt->{int01} && $ctt->{int01} != 0) || ($ib_key eq "send_invoice_again"))){
my $sum_paid = $R::sum_paid || 0;
$sum_paid =~ s/,/\./;
my $cms_message_key = "email-invoice";
$varenv{cms} = $dbt->fetch_cms($dbh,{ lang => $lang_ctt });
if(!$varenv{cms}->{$cms_message_key}->{txt}){
return "failure::Achtung, CMS-Text '$cms_message_key' ist nicht vorhanden. Es wurde keine eMail versandt!";
}
if($sum_paid != $ctt->{int01}){
return "failure::Achtung, die Summe der Positionen $sum_paid enstpricht nicht der Rechnung-Summe $ctt->{int01}. Die Rechnung muss vor dem eMail versand erst gebucht werden!";
}
system("$dbt->{copri_conf}->{basedir}/$varenv{syshost}/src/scripts/mailTransportcms.pl '$varenv{syshost}' 'send_invoice' '$ctt->{int10}' '$ctt->{c_id}' '' '$cms_message_key' '1'");
print EMA "---> Prelogic send_invoice $praefix-$ctt->{ct_name}.pdf email command: $dbt->{copri_conf}->{basedir}/$varenv{syshost}/src/scripts/mailTransportcms.pl '$varenv{syshost}' 'send_invoice' '$ctt->{int10}' '$ctt->{c_id}' '' '$cms_message_key' '1'\n";
}
#send_invoice_cms
if($ib_key eq "send_invoice_cms" && $R::cms_message_key){
my $sum_paid = $R::sum_paid || 0;
$sum_paid =~ s/,/\./;
$varenv{cms} = $dbt->fetch_cms($dbh,{ lang => $lang_ctt });
if(!$varenv{cms}->{$R::cms_message_key}->{txt}){
return "failure::Achtung, CMS-Text '$R::cms_message_key' ist nicht vorhanden. Es wurde keine eMail versandt!";
}
if($sum_paid != $ctt->{int01}){
return "failure::Achtung, die Summe der Positionen $sum_paid enstpricht nicht der Rechnung-Summe $ctt->{int01}. Die Rechnung muss vor dem eMail versand erst gebucht werden!";
}
my $with_pdf = "";
$with_pdf = 1 if(-f "$varenv{basedir}/pdfinvoice/$praefix-$ctt->{ct_name}.pdf" && $R::print_pdf);
system("$dbt->{copri_conf}->{basedir}/$varenv{syshost}/src/scripts/mailTransportcms.pl '$varenv{syshost}' 'send_invoice_cms' '$ctt->{int10}' '$ctt->{c_id}' '' '$R::cms_message_key' '$with_pdf'");
print EMA "---> Prelogic send_invoice_cms $praefix-$ctt->{ct_name}.pdf email command: $dbt->{copri_conf}->{basedir}/$varenv{syshost}/src/scripts/mailTransportcms.pl '$varenv{syshost}' 'send_invoice_cms' '$ctt->{int10}' '$ctt->{c_id}' '' '$R::cms_message_key' '$with_pdf'\n";
}
close EMA;
}#end c_id4print
#SET Tagesabschluss
if($R::v_abschluss){
my $journal_tpl="209";
my $journal_id="300011";
#test node
my $n_exist = $db->get_node4multi($journal_id,$lang);
my $t_exist = $db->get_tpl($journal_tpl);
if(!$n_exist->{n_id} || !$t_exist->{tpl_id}){
return "failure::Die Journal Konfiguration ist fehlerhaft.";
}
#collect sub-nodes
#TODO
my $main_ids = "300008,300009,300011";#Rechnung,Storno,Verkaufsjournal
#my $main_ids = "$parent_trans->{main_id},";
#$main_ids .= $db->collect_noderec($parent_trans->{main_id},$lang,"nothing") if($parent_trans->{main_id});
#$main_ids =~ s/,$//;
my $tpl_vk = "218";
my $table = "contenttrans";
my $sum_start = $R::sum_start || "0";
my $sum_kasse = $R::sum_kasse || "0";
$sum_start = $lb->checkint($sum_start);
$sum_kasse = $lb->checkint($sum_kasse);
my $c_id4kasse = "";
my $c_id4abschluss = $db->get_content6("$table","close_time","null","state","Tagesabschluss","int09","$parent_trans->{parent_id}","","","$s_owner_id");
if($journal_id){
if($c_id4abschluss->{c_id} && $c_id4abschluss->{c_id} > 0){
$c_id4kasse = $c_id4abschluss->{c_id};
}else{
$ct_name = "--- auto ct_name = c_id ---";
$c_id4kasse = $db->insert_content2($table,$ct_name,$users_dms->{u_id},"Tagesabschluss");
$i_rows += 1 if($c_id4kasse > 0);
$db->update_content4change("contenttrans",$c_id4kasse,"$c_id4kasse");
$db->update_content4change("contenttrans",$c_id4kasse,"",$parent_trans->{parent_id},"int09");#mandant_main_id
$db->update_content4change("contenttrans",$c_id4kasse,"",$c_id4kasse,"int11");#last_ab
$db->update_content4change("contenttrans",$c_id4kasse,"",$journal_id,"int12");#main_id
$db->update_content4change("contenttrans",$c_id4kasse,"",$n_exist->{node_name},"txt00");#node_name
my $rel_id = $db->insert_relationlist($table,$journal_id,$lang,$c_id4kasse,$tpl_vk,"ct_id");
}
$u_rows += $db->update_kasse($table,$c_id4kasse,$sum_kasse,$sum_start);
if($journal_id && $journal_tpl && $R::close_trans){
my $opos="null";
$u_rows += $db->update_tagesabschluss($table,$c_id4kasse,$journal_id,$journal_tpl,$tpl_vk,$parent_trans->{parent_id},"$main_ids","$s_owner_id","$opos");
print $q->div({-class=>'elementwhite'},"1... redirecting to ... or CLICK ", $q->a({href=>"$varenv{wwwhost}/DMS/Faktura/Verkaufsjournal"},"$varenv{wwwhost}/DMS/Faktura/Verkaufsjournal"));
print redirect("$varenv{wwwhost}/DMS/Faktura/Verkaufsjournal?redirected=1\&return=0-0-0|$i_rows-$u_rows-$d_rows");
exit 0;
}
}
$db->cleanup_users($users_dms->{u_id});
}
close(FILE);
###
return "$i_rows-$u_rows-$d_rows";
}
1;