#!/usr/bin/perl -w # # modules/Event.pm - handle events # # Developed by Lubomir Host 'rajo' # Copyright (c) 2004 Platon SDG, http://platon.sk/ # Licensed under terms of GNU General Public License. # All rights reserved. # # Changelog: # 09/02/2004 - created # # $Platon: web-apps/finance-manager/modules/Event.pm,v 1.1 2004/02/11 10:52:53 rajo Exp $ package Event; use strict; use Carp; use Exporter; use DBI qw(:sql_types); use globals; use vars qw($VERSION @ISA @EXPORT $AUTOLOAD); @ISA = qw(Exporter globals); @EXPORT = qw( ); $VERSION = '0.1'; sub new { # {{{ my $this = shift; my ($globals, $dbh) = @_; my $class = ref($this) || $this; my $self = {}; bless $self, $class; $self->{__dbh} = $dbh; $self->_prepare_sql($globals, $dbh); #$self->set(@_) if @_; return $self; } # }}} new # prepare SQL commands sub _prepare_sql($$;) { # {{{ my ($self, $globals, $dbh) = @_; $self->{get_all_events} = $dbh->prepare(" /* get_all_events " . __FILE__ . ":" . __LINE__ . " */ SELECT eventid, active, name, description, note, created, DATE_FORMAT(changed, '%Y-%m-%d %T') AS changed, last_transaction, event_type, from_source, to_source, money, period, start_time, stop_time FROM Event ORDER BY eventid ") or croak "Can't get list of events: " . $dbh::errstr; $self->{get_event} = $dbh->prepare(" /* get_event " . __FILE__ . ":" . __LINE__ . " */ SELECT eventid, active, name, description, note, created, DATE_FORMAT(changed, '%Y-%m-%d %T') AS changed, last_transaction, event_type, from_source, to_source, money, period, start_time, stop_time FROM Event WHERE eventid = ? ") or croak "Can't get event: " . $dbh::errstr; $self->{get_event}->bind_param(1, 0, SQL_INTEGER); $self->{new_event} = $dbh->prepare(" /* new_event " . __FILE__ . ":" . __LINE__ . " */ INSERT INTO Event (created, active, event_type, from_source, to_source, money, period, start_time, stop_time, name, description, note) VALUES (NOW(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) /* 11x otaznik */ ") or croak "Can't create new event: " . $dbh::errstr; foreach my $i (1..6) { $self->{new_event}->bind_param($i, 0, SQL_INTEGER); } $self->{update_event} = $dbh->prepare(" /* new_event " . __FILE__ . ":" . __LINE__ . " */ UPDATE Event SET active = ?, event_type = ?, from_source = ?, to_source = ?, money = ?, period = ?, start_time = ?, stop_time = ?, name = ?, description = ?, note = ? WHERE eventid = ? ") or croak "Can't create new event: " . $dbh::errstr; foreach my $i (1..6, 12) { $self->{update_event}->bind_param($i, 0, SQL_INTEGER); } $self->{log_event_op} = $dbh->prepare(" /* log_event_op " . __FILE__ . ":" . __LINE__ . " */ INSERT INTO EventLog (action_type, created, changed, last_transaction, eventid, active, event_type, money, from_source, to_source, period, start_time, stop_time, name, description, note) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) /* 16x otaznik */ ") or croak "Can't create Event log entry: " . $dbh::errstr; } # }}} _prepare_sql # return list of all finacial events sub list() { # {{{ my ($self) = @_; $self->{get_all_events}->execute(); return $self->{get_all_events}->fetchall_arrayref({}); } # }}} list # return one event info sub get($;) { # {{{ my ($self, $eventid) = @_; $self->{get_event}->execute($eventid); my $event_data = $self->{get_event}->fetchrow_hashref(); $self->{get_event}->finish(); return $event_data; } # }}} get # create new Event sub new_event($;) { # {{{ my ($self, $data) = @_; my $dbh = $self->{__dbh}; foreach my $key ('active', 'from_source', 'to_source', 'money', 'period', 'last_transaction', 'event_type', 'name', 'description', 'note') { unless (defined $data->{$key}) { croak "Undefined REQUIRED parameter '$key'"; } } foreach my $key ('active', 'from_source', 'to_source', 'money', 'period', 'event_type') { if ($data->{$key} !~ m/^\d+$/) { carp "Parameter '$key' ('$data->{$key}') is not numeric"; $data->{$key} = 0; } } $dbh->begin_work(); $self->{new_event}->execute( $data->{active}, $data->{event_type}, $data->{from_source}, $data->{to_source}, $data->{money}, $data->{period}, $data->{start_time}, $data->{stop_time}, $data->{name}, $data->{description}, $data->{note} ) or do { carp "ERROR::Event: " . __FILE__ . ":" . __LINE__ . ": " . $dbh->errstr; $dbh->rollback(); return 0; }; my $eventid = $dbh->{'mysql_insertid'}; $self->{log_event_op}->execute( "added", undef, undef, undef, $eventid, $data->{active}, $data->{event_type}, $data->{money}, $data->{from_source}, $data->{to_source}, $data->{period}, $data->{start_time}, $data->{stop_time}, $data->{name}, $data->{description}, $data->{note} ) or do { carp "ERROR::Event: " . __FILE__ . ":" . __LINE__ . ": " . $dbh->errstr; $dbh->rollback(); return 0; }; $dbh->commit(); return 1; } # }}} get sub update_event($;) { # {{{ my ($self, $data) = @_; my $dbh = $self->{__dbh}; foreach my $key ('eventid', 'active', 'from_source', 'to_source', 'money', 'period','event_type', 'last_transaction', 'start_time', 'stop_time', 'name', 'description', 'note') { unless (defined $data->{$key}) { croak "Undefined REQUIRED parameter '$key'"; } } foreach my $key ('eventid', 'active', 'from_source', 'to_source', 'money', 'period', 'event_type') { if ($data->{$key} !~ m/^\d+$/) { carp "Parameter '$key' ('$data->{$key}') is not numeric"; $data->{$key} = 0; } } $dbh->begin_work(); my $db_data = $self->get($data->{eventid}); $self->{log_event_op}->execute( "changed_from", $db_data->{eventid}, $db_data->{active}, $db_data->{start_money}, $db_data->{stop_money}, $db_data->{money}, $db_data->{low_money}, $db_data->{medium_money}, $db_data->{high_money}, $db_data->{name}, $db_data->{description}, $db_data->{note} ) or do { carp "ERROR::Event: " . __FILE__ . ":" . __LINE__ . ": " . $dbh->errstr; $dbh->rollback(); return 0; }; $self->{update_event}->execute( $data->{active}, $data->{start_money}, $data->{stop_money}, $data->{money}, $data->{low_money}, $data->{medium_money}, $data->{high_money}, $data->{name}, $data->{description}, $data->{note}, $data->{eventid} ) or do { carp "ERROR::Event: " . __FILE__ . ":" . __LINE__ . ": " . $dbh->errstr; $dbh->rollback(); return 0; }; $self->{log_event_op}->execute( "changed_to", $data->{eventid}, $data->{active}, $data->{start_money}, $data->{stop_money}, $data->{money}, $data->{low_money}, $data->{medium_money}, $data->{high_money}, $data->{name}, $data->{description}, $data->{note} ) or do { carp "ERROR::Event: " . __FILE__ . ":" . __LINE__ . ": " . $dbh->errstr; $dbh->rollback(); return 0; }; $dbh->commit(); return 1; } # }}} get 1; __END__ =head1 NAME Event - <<>> =head1 SYNOPSIS use Event; my $xxx = new Event; =head1 DESCRIPTION The Event module allows you ... <<>> =head2 EXPORT <>> =head1 SEE ALSO =head1 AUTHORS Lubomir Host 'rajo', =cut # vim: ts=4 # vim600: fdm=marker fdl=0 fdc=3