#!/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 ; #------------------------------------------------------------------------------------------ &today ; 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') ; if ($type eq 'csv') { &db_min_ro('event_production_time_unmatched','id,unmatched_line','','','') ; foreach (keys %{$db{event_production_time_unmatched}}) { my $event_code = substr($db{event_production_time_unmatched}{$_}{unmatched_line},-8) ; $unmatched_event_lines{$event_code} = $db{event_production_time_unmatched}{$_}{unmatched_line} ; $max_unmatched_id = $_ if !$max_unmatched_id || ($max_unmatched_id && $max_unmatched_id < $_) ; $event_code_unmatched_id{$event_code} = $_ ; } # # # $max_unmatched_id++ ; # while ( <$upload_filehandle> ) { # # foreach (@imported_file_data) { # chomp; # next if !$_ || ($_ !~ /\d+/ && $_ !~ /[a-zA-Z]/) ; # chop $_ ; # my @linex = split(/\;/,$_) ; # my $event_code = substr($linex[-1],-8) ; # $event_lines{$event_code} = $_ ; # } } } #------------------------------------------------------------------------------------------ sub update { my %ii = () ; my %iii = %i ; &db_min_ro('event_production_time','quote_id,id,event_system_production_seconds,event_system_cloud_recording_seconds,matched_events',"") ; my $max_id = 0 ; foreach my $event_id (keys %{$db{event_production_time}}) { $max_id = $db{event_production_time}{$event_id}{id} if !$db{event_production_time}{$event_id}{id} || int($db{event_production_time}{$event_id}{id}) > int($max_id) ; if ($db{event_production_time}{$event_id}{event_system_production_seconds}) { foreach (sort split(/\|/,$db{event_production_time}{$event_id}{event_system_production_seconds})) { my @abc = split(":",$_) ; $ii{$event_id}{$abc[0]}{event_system_production_seconds} += $abc[1] ; } } if ($db{event_production_time}{$event_id}{event_system_cloud_recording_seconds}) { foreach (sort split(/\|/,$db{event_production_time}{$event_id}{event_system_cloud_recording_seconds})) { my @abc = split(":",$_) ; $ii{$event_id}{$abc[0]}{event_system_cloud_recording_seconds} += $abc[1] ; } } $ii{$event_id}{NA}{matched_events} = $db{event_production_time}{$event_id}{matched_events} ; } foreach (sort keys %i) { my @get_row = split("_",$_) ; my $row_nr = $get_row[-1] ; my $system_field = "system_$row_nr" ; my $event_code_field = "event_code_$row_nr" ; if ($_ !~ /event_code_/ && $_ =~ /event_/ && (!$i{$_} || !$i{$system_field}) && !$unmatched_event_lines{$i{$i{$event_code_field}}}) { $max_unmatched_id++ ; $i{$i{$event_code_field}} =~ s/\'//g ; my %iiii = %i ; %i = () ; $i{id} = $max_unmatched_id ; $i{unmatched_line} = $iiii{$iiii{$event_code_field}} ; &db_min_insert('event_production_time_unmatched') ; # print "\n INSERT INTO event_production_time_unmatched (id,unmatched_line) VALUES ($max_unmatched_id,'$i{unmatched_line}') ; " ; %i = %iiii ; } elsif ($_ !~ /event_code_/ && $_ =~ /event_/ && $i{$_} && $i{$system_field} && $unmatched_event_lines{$i{$event_code_field}}) { # print "\n DELETE FROM event_production_time_unmatched WHERE id = '$event_code_unmatched_id{$i{$event_code_field}}' " ; &db_min_delete('event_production_time_unmatched',"`id`='$event_code_unmatched_id{$i{$event_code_field}}'") ; } next if $_ =~ /event_code_/ || $_ !~ /event_/ || ($_ =~ /event_/ && $_ !~ /event_code_/ && !$i{$_}) ; my $system_field = "system_$row_nr" ; my $event_code_field = "event_code_$row_nr" ; next unless $i{$system_field} ; my $production_seconds = $i{"time_$row_nr"} ; my $cloud_recording_seconds = $i{"cloud_time_$row_nr"} ; next if $ii{$i{$_}}{NA}{matched_events} =~ /$i{$event_code_field}/ ; $ii{$i{$_}}{$i{$system_field}}{event_system_production_seconds} += $production_seconds ; $ii{$i{$_}}{$i{$system_field}}{event_system_cloud_recording_seconds} += $cloud_recording_seconds ; $ii{$i{$_}}{NA}{matched_events} .= ($ii{$i{$_}}{NA}{matched_events}) ? qq~|$i{$event_code_field}~ : qq~$i{$event_code_field}~ ; } foreach my $event_id (sort keys %ii) { %i = () ; foreach my $system_id (sort keys %{$ii{$event_id}}) { next if $system_id eq 'NA' ; $i{event_system_production_seconds} .= qq~$system_id:$ii{$event_id}{$system_id}{event_system_production_seconds}|~ ; $i{event_system_cloud_recording_seconds} .= qq~$system_id:$ii{$event_id}{$system_id}{event_system_cloud_recording_seconds}|~ ; } chop $i{event_system_production_seconds} if $i{event_system_production_seconds} ; chop $i{event_system_cloud_recording_seconds} if $i{event_system_cloud_recording_seconds} ; $i{matched_events} = $ii{$event_id}{NA}{matched_events} ; next if !$i{event_system_production_seconds} && !$i{event_system_cloud_recording_seconds} && !$i{matched_events} ; $ignore{event_system_production_seconds} = ($i{event_system_production_seconds} && $i{event_system_production_seconds} eq $db{event_production_time}{$event_id}{event_system_production_seconds}) ? 1 : 0 ; $ignore{event_system_cloud_recording_seconds} = ($i{event_system_cloud_recording_seconds} && $i{event_system_cloud_recording_seconds} eq $db{event_production_time}{$event_id}{event_system_cloud_recording_seconds}) ? 1 : 0 ; ; $ignore{matched_events} = ($i{matched_events} && $i{matched_events} eq $db{event_production_time}{$event_id}{matched_events}) ? 1 : 0 ; next if $ignore{event_system_production_seconds} && $ignore{event_system_cloud_recording_seconds} && $ignore{matched_events} ; next unless $i{matched_events} ; if ($db{event_production_time}{$event_id}{quote_id}) { # print "\n UPDATE event_production_time SET event_system_production_seconds='$i{event_system_production_seconds}',event_system_cloud_recording_seconds='$i{event_system_cloud_recording_seconds}',matched_events='$i{matched_events}'" ; &db_min_upd('event_production_time',"quote_id='$event_id'") ; } else { $i{quote_id} = $event_id ; $max_id ++ ; $i{id} = $max_id ; # print "\n INSERT INTO event_production_time (id,event_system_production_seconds,event_system_cloud_recording_seconds,matched_events,quote_id) VALUES ($i{id},$i{event_system_production_seconds},$i{event_system_cloud_recording_seconds},$i{matched_events},$i{quote_id})" ; &db_min_insert('event_production_time') ; } } %i = %iii ; } #------------------------------------------------------------------------------------------ sub read_import { my $upload_filehandle = $q->upload('iattachdoc') ; unless ($upload_filehandle) { &screen1 ; } &db_min_ro('event_production_time_unmatched','id,unmatched_line','','','') ; foreach (keys %{$db{event_production_time_unmatched}}) { my $event_code = substr($db{event_production_time_unmatched}{$_}{unmatched_line},-8) ; $event_code_unmatched_id{$event_code} = $_ ; } &db_min_ro('event_production_time','id,matched_events',"","","") ; our %matched_event = () ; foreach my $quote_id (keys %{$db{event_production_time}}) { foreach my $event_code (split(/\|/,$db{event_production_time}{$quote_id}{matched_events})) { $matched_event{$event_code} = 1 ; } } # our $start_date = ($upload_filehandle =~ /August/) ? "$now_year-08-01" : ($upload_filehandle =~ /September/) ? "$now_year-09-01" : "" ; # our $end_date = ($upload_filehandle =~ /August/) ? "$now_year-08-31" : ($upload_filehandle =~ /September/) ? "$now_year-09-30" : "" ; # &db_open_upd ; # our $db_ignore_open_close = 1 ; # do one open and one close instead of repeating it # $print_tbody .= qq~