<?

require "/usr/lib/cherry/dbaccess";
use Time::Local;

use Data::Dumper;

#################################################################################
sub time2str {
        my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(shift @_);
        $mon++;
        $year += 1900;
        return sprintf("%.4i-%.2i-%.2i %.2i:%.2i",$year,$mon,$mday,$hour,$min);
}

######################################################################################3
sub str2time {
        my($date) = @_;
        my ($year, $mon, $mday, $hour, $min, $sec) = $date =~ /^(\d\d\d\d)-(\d\d)-(\d\d)(?: (\d\d)\:(\d\d)(?:\:(\d\d))?)?/;
        return timelocal($sec, $min, $hour, $mday, $mon - 1, $year - 1900);
}
######################################################################################


my $where;
$where="AND descr LIKE '%client_id=\"$client_id\"%' " if($client_id);
$where="AND ($action) " if ($action);


my $sql="SELECT operator,date,action,descr FROM base_history WHERE 1=1 $where ORDER BY date";
my $sth=$dbh->prepare($sql);
$sth->execute;

my %hc;
my %service_edit;
my %hdiff;
my %payments;
my %bills;
my %clients;
my %given_services;

while(my($operator,$date,$action,$descr) = $sth->fetchrow_array ) {
	
	my $str=$descr;
	my %h;
	while ($str=~ s/(\w+)="([^"]+)//) {
		my $key=$1;
		my $value=$2;


		$h{$key}=$value;

	}



        if($action=~/base\/(client_edit_commit.tpl|change_bookkeeper_balance_commit.tpl|change_balance_commit.tpl)/) {

                my %lh= %{ $clients{ $h{client_id} } };
                my %changes;
		if(%lh) {
                	foreach my $key (%lh) {
                                if(! ($lh{$key} eq $h{$key}) ) {
                                        $changes{$key}=$h{$key};
                                }
                	}
                } else {
                        foreach my $key (%h) {
                                $changes{$key}=$h{$key};
                        }
                }

                if(%changes) {
                        #$changes{client_id} = $h{client_id};
                        $hdiff{$date}{$operator}{$action}={%changes};
                }

                $clients{ $h{client_id} } = {%h};

        }



        if($action=~/bill\/bill_edit_service_(add|delete)_commit.tpl/) {

                my %changes;
                foreach my $key (%h) {
                	$changes{$key}=$h{$key};
                }
                if(%changes) {
                        $hdiff{$date}{$operator}{$action}={%changes};
                }

        }



        if($action=~/bill\/given_service_(edit|delete)_commit.tpl/) {

                my %changes;
                foreach my $key (%h) {
                	$changes{$key}=$h{$key};
                }
                if(%changes) {
                        $hdiff{$date}{$operator}{$action}={%changes};
                }

        }


	if($action=~/payment\/payment_(edit|delete)_commit.tpl/) {

                my %changes;
                foreach my $key (%h) {
                                $changes{$key}=$h{$key};
                }
		if(%changes) {
                	$hdiff{$date}{$operator}{$action}={%changes};
		}

	}

	if($action=~/\/service_(edit|lock|delete)_commit/) {
		my %lh= %{ $service_edit{ $h{user_id} } };
		my %changes;
		if(%lh) {
			foreach my $key (%lh) {
		
				if(! ($lh{$key} eq $h{$key}) ) {
					$changes{$key}=$h{$key};
				}
			}
                } else {
                        foreach my $key (%h) {
                                $changes{$key}=$h{$key};
                        }
                }


		if(%changes) {
			$changes{user_id} = $h{user_id};
			$hdiff{$date}{$operator}{$action}={%changes};
		}

		$service_edit{ $h{user_id} } = {%h};
	}
		
}

?>
<TABLE width="98%" border=1 style="border:solid windowtext 1pt;">
<COL width="90px"><COL width="60px"><COL width="200px"><COL>
<THEAD>
	<TD style="border:solid windowtext 1pt;"></TD>
	<TD style="border:solid windowtext 1pt;"></TD>
	<TD style="border:solid windowtext 1pt;"></TD>
	<TD style="border:solid windowtext 1pt;"></TD>
</THEAD>
<?


$da{'payment/payment_edit_commit.tpl'}=' ';
$da{'payment/payment_delete_commit.tpl'}=' ';
$da{'bill/given_service_edit_commit.tpl'}=' ';
$da{'base/change_bookkeeper_balance_commit.tpl'}=' ';
$da{'bill/given_service_delete_commit.tpl'} = ' ';
$da{'bill/bill_delete_commit.tpl'} = ' ';
$da{'base/client_edit_commit.tpl'} = ' ';
$da{'unix_shell/service_edit_commit.tpl'} = '.  UNIX  -  ';
$da{'dry_pair/service_edit_commit.tpl'} = '.  -  ';
$da{'channel/service_edit_commit.tpl'} = ' -  ';
$da{'inet_fixed_ip/service_edit_commit.tpl'} = '. . IP -  ';
$da{'inet_dynamic_ip/service_edit_commit.tpl'} = '. . IP -  ';
$da{'telephone/service_edit_commit.tpl'} = '.  -  ';
$da{'ani_phone/service_edit_commit.tpl'} = 'ANI .  -  ';
$da{'voip_ip/service_edit_commit.tpl'} = '. IP -  ';
$da{'dry_pair/service_delete_commit.tpl'} = '.  -  ';
$da{'unix_shell/service_delete_commit.tpl'} = '.  UNIX  -  ';
$da{'channel/service_delete_commit.tpl'} = ' -  ';
$da{'inet_fixed_ip/service_delete_commit.tpl'} = '. . IP -  ';
$da{'inet_dynamic_ip/service_delete_commit.tpl'} = '. . IP -  ';
$da{'telephone/service_delete_commit.tpl'} = '.  -  ';
$da{'ani_phone/service_delete_commit.tpl'} = 'ANI .  -  ';
$da{'voip_ip/service_delete_commit.tpl'} = '. IP -  ';
$da{'inet_fixed_ip/service_lock_commit.tpl'} = '. . IP - ./.';
$da{'inet_dynamic_ip/service_lock_commit.tpl'} = '. . IP - ./.';
$da{'base/change_balance_commit.tpl'}=' ';
$da{'bill/bill_edit_service_delete_commit.tpl'} = '   ';
$da{'bill/bill_edit_service_add_commit.tpl'} = '   ';





foreach my $date (sort{$b cmp $a} keys %hdiff) {
		foreach my $operator (sort keys %{ $hdiff{$date} } ) {
			foreach my $action (sort keys %{ $hdiff{$date}{$operator} } ) {
				$date_=time2str(str2time($date));
				if($da{$action}) {
					$action_descr=$da{$action};	
				} else {
					$action_descr=$action;
				}

				$param_descr='';

				foreach my $key (keys %{$hdiff{$date}{$operator}{$action}} ) {
						my $value=$hdiff{$date}{$operator}{$action}->{$key};
						next if($key eq 'tpl' || $key eq 'filename' || 
							$key eq 'content_type' || $key eq 'oper_id' ||
							$key eq 'return_form' || $key eq 'report' 



						 );
						next if(!$value);
					$param_descr.=" $key='$value'";
				}
?>
<TR>
	<TD style="border:solid windowtext 1pt;" >$date_</TD>
        <TD style="border:solid windowtext 1pt;" >$operator</TD>
        <TD style="border:solid windowtext 1pt;">$action_descr</TD>
        <TD style="border:solid windowtext 1pt;">$param_descr</TD>
</TR>
<?
			}
		}

}
?>

</TABLE>
