#!/usr/bin/perl BEGIN { use lib '/usr/home/cfg' ; require push_inc ; } use CGI qw( :standard ); use CGI::Carp qw(fatalsToBrowser); use LWP::Simple qw($ua get); use Date::Calc qw(:all); use MIME::Lite; use Mail::Sendmail; use Time::Piece ; require cfg ; print header; # CGI.pm method #------------------------------------------------------------------------------- my @ARGV = split(/\&/, $ENV{'QUERY_STRING'}); my $type = $ARGV[0] ; our ($q) = CGI -> new() ; my $iaction = $q -> param('iaction') ; my $iattachdoc = $q -> param('iattachdoc') ; # our $debug = 1 ; # our $testing = 1 ; #------------------------------------------------------------------------------------------ &today ; # my $string = qq~6565,2,328.00~ ; if ($iaction eq '') { &screen1 ; } elsif ($iaction eq 'upload') { &db_open_ro ; our $db_ignore_open_close = 1 ; # do one open and one close instead of repeating it &read_import ; &read_db ; &process ; &screen3 ; $db_ignore_open_close = 0 ; # do one open and one close instead of repeating it &db_close_conn ; } elsif ($iaction eq 'save') { &common_min_load_params ; &db_open_upd ; our $db_ignore_open_close = 1 ; # do one open and one close instead of repeating it &read_read_import_lines ; &update ; $db_ignore_open_close = 0 ; # do one open and one close instead of repeating it &db_close_conn ; &screen2 ; } exit; #------------------------------------------------------------------------------------------ sub read_read_import_lines { our $max_unmatched_id = 0 ; my $upload_filehandle = $q->upload('iattachdoc') ; } #------------------------------------------------------------------------------------------ sub update { &db_min_ro('logistics_costs','event_nr,amounts,file_name_ids,cost_type',"",'','') ; &db_min_ro('logistics_costs_tables',"1,MAX(id)+1 AS 'max_id'","",'','') ; $db{logistics_costs_tables}{1}{max_id} = 1 unless $db{logistics_costs_tables}{1}{max_id} ; my %ii = %i ; my $updated_logistics_costs_tables = 0 ; my $file_id = 0 ; my %cost_types = () ; my %event_has_diff_cost_types = () ; my %prev_cost_type = () ; foreach (keys %ii) { if ($_ =~ /costtype_/) { my ($NA,$event_id,$amnt_cnt) = split(/\_/,$_) ; $event_id = ($event_id =~ /\d+/) ? $event_id : -1 ; $cost_types{$event_id}{$amnt_cnt} = $ii{$_} ; if ($prev_cost_type{$event_id} && $prev_cost_type{$event_id} ne $ii{$_}) { $event_has_diff_cost_types{$event_id} = 1 ; } $prev_cost_type{$event_id} = $ii{$_} ; } } foreach (keys %ii) { if ($_ =~ /breakdownamnts_/) { unless ($updated_logistics_costs_tables) { $updated_logistics_costs_tables = 1 ; %i = () ; $i{id} = $db{logistics_costs_tables}{1}{max_id} ; $file_id = $i{id} ; $i{file_name} = $ii{uploaded_file_name} ; $i{file_total} = $ii{uploaded_file_total} ; $i{file_total} = sprintf("%.0f",$i{file_total}) ; $i{file_nr_of_unique_events} = $ii{file_nr_of_unique_events} ; $i{file_nr_of_rows} = $ii{file_nr_of_rows} ; $i{date_uploaded} = "$now_ccyy_mm_dd $now_hour:$now_min:$now_sec" ; $i{uploaded_by} = $userid ; &db_min_insert('logistics_costs_tables') ; } my ($NA,$event_id) = split(/\_/,$_) ; %i = () ; $i{event_nr} = ($event_id =~ /\d+/) ? $event_id : -1 ; $i{cost_type} = ($db{logistics_costs}{$i{event_nr}}{cost_type}) ? "$db{logistics_costs}{$i{event_nr}}{cost_type}::" : "" ; my $added_to_cost_type = 0 ; foreach my $amnt_cnt (sort {$a <=> $b} keys %{$cost_types{$i{event_nr}}}) { $added_to_cost_type = 1 ; $i{cost_type} .= qq~$cost_types{$i{event_nr}}{$amnt_cnt};;~ ; last unless $event_has_diff_cost_types{$i{event_nr}} ; } $i{cost_type} = substr($i{cost_type},0,-2) if $added_to_cost_type ; # $i{cost_type} = ($db{logistics_costs}{$i{event_nr}}{cost_type} ) ? "$db{logistics_costs}{$i{event_nr}}{cost_type}::" . $ii{"costtype_$event_id"} : $ii{"costtype_$event_id"} ; $i{file_name_ids} = ($db{logistics_costs}{$i{event_nr}}{file_name_ids}) ? "$db{logistics_costs}{$i{event_nr}}{file_name_ids}:$file_id" : "$file_id" ; $i{amounts} = ($db{logistics_costs}{$i{event_nr}}{amounts} ) ? "$db{logistics_costs}{$i{event_nr}}{amounts}:$ii{$_}" : "$ii{$_}" ; ; $i{amounts} =~ s/\,//g ; $i{amounts} =~ s/\.00//g ; if ($i{event_nr} && $db{logistics_costs}{$i{event_nr}}{event_nr}) { $ignore{event_nr} = 1 ; &db_min_upd('logistics_costs',"event_nr='$i{event_nr}'") ; } else { &db_min_ro('logistics_costs',"1,MAX(id)+1 AS 'max_id'","",'','') ; $db{logistics_costs}{1}{max_id} = 1 unless $db{logistics_costs}{1}{max_id} ; $i{id} = $db{logistics_costs}{1}{max_id} ; &db_min_insert('logistics_costs') ; } } } } #------------------------------------------------------------------------------------------ sub read_import { $print_box_content_rows .= &common_min_forms_start('import-logistics-costs') ; my $upload_filehandle = $q->upload('iattachdoc') ; unless ($upload_filehandle) { $warning = "PLEASE UPLOAD A CSV FILE!!!" ; &common_min_alert_type ; &screen1 ; } our %totals = () ; my $line_cnt = 0 ; my %event_cnt = () ; if ($upload_filehandle !~ /.csv/) { $error = "PLEASE UPLOAD A CSV TYPE FILE!!!" ; &common_min_alert_type ; &screen1 ; } my $file_total_amnt = 0 ; my $file_nr_of_rows = 0 ; if ($type eq 'csv') { while ( <$upload_filehandle> ) { # foreach (@imported_file_data) { next if $_ !~ /\d+/ ; $file_nr_of_rows++ ; chomp $_; $_ =~ s/\R\z// ; # Removes any line-ending character (\n, \r\n, or \r) $_ =~ s/\"//g ; # $_ =~ s/\,//g ; # $_ =~ s/\s+$//g; $_ =~ s/ //g ; $_ =~ s/^,|(?<=\W),(?=\w)//g; $_ =~ s/^(\d+),(\d+),(\d+\.\d+)/$1,$2$3/g; ## REWRITE 1,1,000.11 AS 1,1000.11 $_ =~ s/^(\d+),(\d+),(\d+),(\d+\.\d+)/$1,$2$3$4/g; ## REWRITE 1,1,000,000.11 AS 1,1000000.11 $_ =~ s/^(\d+),(\d+),(\d+),(\d+),(\d+\.\d+)/$1,$2$3$4$5/g; ## REWRITE 1,1,000,000,000.11 AS 1,1000000000.11 $_ =~ s/^([^,]+),(\d+),(\d+\.\d+)/$1,$2$3/; ## REWRITE OTHER,1,000.11 AS OTHER,1000.11 $_ =~ s/^([^,]+),(\d+),(\d+),(\d+\.\d+)/$1,$2$3$4/; ## REWRITE OTHER,1,000,000.11 AS OTHER,1000000.11 $_ =~ s/^([^,]+),(\d+),(\d+),(\d+),(\d+\.\d+)/$1,$2$3$4$5/; ## REWRITE OTHER,1,000,000,000.11 AS OTHER,1000000000.11 my ($event_id,$total) = split(/\,/,$_) ; $event_cnt{$event_id} += 1 ; $totals{$event_id} += $total ; $file_total_amnt += $total ; $total = &common_commify(sprintf("%.02f",$total)) ; $totals{breakdown}{$event_id} .= qq~$total;~ ; $totals{seperate}{$event_id}{$event_cnt{$event_id}} = $total ; } } $file_total_amnt = sprintf("%.0f",$file_total_amnt) ; our @sql_col_display = ("event_nr","cost_type","logistics_totals") ; my $file_total = 0 ; my $file_nr_of_unique_events = 0 ; &db_min_ro('event_quotes','id,quote_completed,quote_accepted,fixed_system_overall_status,event_system_id_multiple,quote_rejected,quote_cancelled,quote_created,quote_pending,sss_quote_nr',"",'','') ; &db_min_ro('event_systems',"*",'','','') ; my %seen_event = () ; foreach my $event_id (sort {$a <=> $b} keys %totals) { next if $event_id eq 'breakdown' ; my $amnt_cnt = 0 ; foreach my $sep_amnt (split(/\;/,$totals{breakdown}{$event_id})) { $print_tbody .= qq~