#!/usr/bin/perl -w # # modules/Source.pm # # 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/Source.pm,v 1.4 2004/04/09 09:17:44 rajo Exp $ package Source; use strict; use Carp; use Exporter; use DBI qw(:sql_types); use globals; use vars qw($VERSION @ISA @EXPORT $AUTOLOAD); @ISA = qw(Exporter); @EXPORT = qw( list get new_source update_source ); $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_sources} = $dbh->prepare(" /* get_all_sources " . __FILE__ . ":" . __LINE__ . " */ SELECT sourceid, active, name, description, note, created, DATE_FORMAT(changed, '%Y-%m-%d %T') AS changed, last_transaction, money, start_money, stop_money, low_money, medium_money, high_money FROM Source ORDER BY sourceid ") or croak "Can't get list of sources: " . $dbh::errstr; $self->{get_source} = $dbh->prepare(" /* get_source " . __FILE__ . ":" . __LINE__ . " */ SELECT sourceid, active, name, description, note, created, DATE_FORMAT(changed, '%Y-%m-%d %T') AS changed, last_transaction, money, start_money, stop_money, low_money, medium_money, high_money FROM Source WHERE sourceid = ? ") or croak "Can't get source: " . $dbh::errstr; $self->{get_source}->bind_param(1, 0, SQL_INTEGER); $self->{new_source} = $dbh->prepare(" /* new_source " . __FILE__ . ":" . __LINE__ . " */ INSERT INTO Source (created, active, start_money, stop_money, money, low_money, medium_money, high_money, name, description, note) VALUES (NOW(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) /* 10x otaznik */ ") or croak "Can't create new source: " . $dbh::errstr; foreach my $i (1..7) { $self->{new_source}->bind_param($i, 0, SQL_INTEGER); } $self->{update_source} = $dbh->prepare(" /* new_source " . __FILE__ . ":" . __LINE__ . " */ UPDATE Source SET active = ?, start_money = ?, stop_money = ?, money = ?, low_money = ?, medium_money = ?, high_money = ?, name = ?, description = ?, note = ? WHERE sourceid = ? ") or croak "Can't create new source: " . $dbh::errstr; foreach my $i (1..7, 11) { $self->{update_source}->bind_param($i, 0, SQL_INTEGER); } $self->{log_source_op} = $dbh->prepare(" /* log_source_op " . __FILE__ . ":" . __LINE__ . " */ INSERT INTO SourceLog (action_type, created, changed, last_transaction, sourceid, active, start_money, stop_money, money, low_money, medium_money, high_money, name, description, note) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) /* 15x otaznik */ ") or croak "Can't create Source log entry: " . $dbh::errstr; } # }}} _prepare_sql # return list of all finacial sources sub list() { # {{{ my ($self) = @_; $self->{get_all_sources}->execute(); return $self->{get_all_sources}->fetchall_arrayref({}); } # }}} list # return one source info sub get($;) { # {{{ my ($self, $sourceid) = @_; $self->{get_source}->execute($sourceid); my $source_data = $self->{get_source}->fetchrow_hashref(); $self->{get_source}->finish(); return $source_data; } # }}} get # create new Source sub new_source($;) { # {{{ my ($self, $data) = @_; my $dbh = $self->{__dbh}; foreach my $key ('active', 'start_money', 'stop_money', 'money', 'low_money', 'medium_money', 'high_money', 'name', 'description', 'note') { unless (defined $data->{$key}) { croak "Undefined REQUIRED parameter '$key'"; } } foreach my $key ('active', 'start_money', 'stop_money', 'money', 'low_money', 'medium_money', 'high_money') { if ($data->{$key} !~ m/^\d+$/) { carp "Parameter '$key' ('$data->{$key}') is not numeric"; $data->{$key} = 0; } } $dbh->begin_work(); $self->{new_source}->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} ) or do { carp "ERROR::Source: " . __FILE__ . ":" . __LINE__ . ": " . $dbh->errstr; $dbh->rollback(); return 0; }; my $sourceid = $dbh->{'mysql_insertid'}; $self->{log_source_op}->execute( "added", undef, undef, undef, $sourceid, $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::Source: " . __FILE__ . ":" . __LINE__ . ": " . $dbh->errstr; $dbh->rollback(); return 0; }; $dbh->commit(); return 1; } # }}} get sub update_source($;) { # {{{ my ($self, $data) = @_; my $dbh = $self->{__dbh}; foreach my $key ('active', 'start_money', 'stop_money', 'money', 'low_money', 'medium_money', 'high_money', 'name', 'description', 'note') { unless (defined $data->{$key}) { croak "Undefined REQUIRED parameter '$key'"; } } foreach my $key ('sourceid', 'active', 'start_money', 'stop_money', 'money', 'low_money', 'medium_money', 'high_money') { 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->{sourceid}); $self->{log_source_op}->execute( "changed_from", $db_data->{created}, $db_data->{changed}, $db_data->{last_transaction}, $db_data->{sourceid}, $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::Source: " . __FILE__ . ":" . __LINE__ . ": " . $dbh->errstr; $dbh->rollback(); return 0; }; $self->{update_source}->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->{sourceid} ) or do { carp "ERROR::Source: " . __FILE__ . ":" . __LINE__ . ": " . $dbh->errstr; $dbh->rollback(); return 0; }; $self->{log_source_op}->execute( "changed_to", $db_data->{created}, $db_data->{changed}, $db_data->{last_transaction}, $data->{sourceid}, $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::Source: " . __FILE__ . ":" . __LINE__ . ": " . $dbh->errstr; $dbh->rollback(); return 0; }; $dbh->commit(); return 1; } # }}} get 1; __END__ =head1 NAME Source - <<>> =head1 SYNOPSIS use Source; my $xxx = new Source; =head1 DESCRIPTION The Source module allows you ... <<>> =head2 EXPORT <>> =head1 SEE ALSO =head1 AUTHORS Lubomir Host 'rajo', =cut # vim: ts=4 # vim600: fdm=marker fdl=0 fdc=3