#!/usr/bin/perl

print "Content-type: text/html\n\n";

BEGIN { use lib '/usr/home/cfg' ; require push_inc ; }
    
require cfg ;

&today ;

#------------------------------------------------------------------------------------------

@ARGV = split(/\\*\&/, $ENV{'QUERY_STRING'});

my $event_id 				= $ARGV[0] ;
my $system_id 				= $ARGV[1] ;
my $production_seconds 		= $ARGV[2] ;
my $cloud_recording_seconds	= $ARGV[3] ;
my $event_code				= $ARGV[4] ;

#------------------------------------------------------------------------------------------

use DBI;
use CGI::Carp qw(fatalsToBrowser);

exit if !$event_id || !$system_id ;

&db_min_ro('event_production_time_unmatched','1,unmatched_line,id',"unmatched_line LIKE '%$event_code'",'','') ; 

if ($db{event_production_time_unmatched}{1}{unmatched_line}) {
	
	# print "\n DELETE FROM event_production_time_unmatched WHERE id = '$db{event_production_time_unmatched}{1}{id}'" ;
	&db_min_delete('event_production_time_unmatched',"`id`='$db{event_production_time_unmatched}{1}{id}'") ;  
	
}

&db_min_ro('event_production_time','1,event_system_production_seconds,event_system_cloud_recording_seconds,quote_id,matched_events',"quote_id='$event_id'") ; 

our %i = () ; my %ii = () ;
 
if ($db{event_production_time}{1}{event_system_production_seconds}) {
	foreach (sort split(/\|/,$db{event_production_time}{1}{event_system_production_seconds})) {
		my @abc = split(/\:/,$_) ;   
		$ii{$event_id}{$abc[0]}{event_system_production_seconds} += $abc[1] ;
	}	
}

if ($db{event_production_time}{1}{event_system_cloud_recording_seconds}) {
	foreach (sort split(/\|/,$db{event_production_time}{1}{event_system_cloud_recording_seconds})) {
		my @abc = split(":",$_) ;  
		$ii{$event_id}{$abc[0]}{event_system_cloud_recording_seconds} += $abc[1] ;
	}	
}  

$ii{$event_id}{matched_events} = $db{event_production_time}{1}{matched_events} ;

if ($ii{$i{$_}}{matched_events} !~ /$event_code/) {
	$ii{$event_id}{$system_id}{event_system_production_seconds} += $production_seconds ; 
	$ii{$event_id}{$system_id}{event_system_cloud_recording_seconds} += $cloud_recording_seconds ; 
	$ii{$event_id}{matched_events} .= ($ii{$event_id}{matched_events}) ? qq~|$event_code~ : qq~$event_code~ ;		
}

foreach my $sys_id (sort keys %{$ii{$event_id}}) {
	next if $sys_id eq 'matched_events' ;
	$i{event_system_production_seconds}			.= qq~$sys_id:$ii{$event_id}{$sys_id}{event_system_production_seconds}|~ ;
	$i{event_system_cloud_recording_seconds}	.= qq~$sys_id:$ii{$event_id}{$sys_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}{matched_events} ;
$i{quote_id}		= $event_id ;	

$ignore{event_system_production_seconds} = 1 if $i{event_system_production_seconds} && $i{event_system_production_seconds} eq $db{event_production_time}{1}{event_system_production_seconds} ;
$ignore{event_system_cloud_recording_seconds} = 1 if $i{event_system_cloud_recording_seconds} && $i{event_system_cloud_recording_seconds} eq $db{event_production_time}{1}{event_system_cloud_recording_seconds} ;
$ignore{matched_events} = 1 if $i{matched_events} && $i{matched_events} eq $db{event_production_time}{1}{matched_events} ;

if ($db{event_production_time}{1}{quote_id}) {
	$ignore{quote_id} = 1 ;
	&db_min_upd('event_production_time',"quote_id='$event_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}' WHERE quote_id='$event_id'" ;
} else {
	# print "\n INSERT INTO event_production_time (event_system_production_seconds,event_system_cloud_recording_seconds,matched_events,quote_id) VALUES ($i{event_system_production_seconds},$i{event_system_cloud_recording_seconds},$i{matched_events},$i{quote_id})" ;
	$i{id} = &db_min_get_max('event_production_time','id') ; 
	&db_min_insert('event_production_time') ; 
}

exit ;

#------------------------------------------------------------------------------------------

use db ;
use today ;
use common ; 

1;