[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

SF.net SVN: ledger-smb: [876] trunk



Revision: 876
          http://svn.sourceforge.net/ledger-smb/?rev=876&view=rev
Author:   einhverfr
Date:     2007-03-09 22:28:31 -0800 (Fri, 09 Mar 2007)

Log Message:
-----------
Moved defaults handling into separate object

Modified Paths:
--------------
    trunk/LedgerSMB.pm

Added Paths:
-----------
    trunk/LedgerSMB/Setting.pm
    trunk/sql/modules/Settings.sql

Added: trunk/LedgerSMB/Setting.pm
===================================================================
--- trunk/LedgerSMB/Setting.pm	                        (rev 0)
+++ trunk/LedgerSMB/Setting.pm	2007-03-10 06:28:31 UTC (rev 876)
@@ -0,0 +1,139 @@
+=head1 NAME
+
+LedgerSMB::Setting - LedgerSMB class for managing Business Locations
+
+=head1 SYOPSIS
+
+This module creates object instances based on LedgerSMB's in-database ORM.  
+
+=head1 METHODS
+
+The following method is static:
+=item new ($LedgerSMB object);
+
+The following methods are passed through to stored procedures:
+=item get ($self->{key})
+=item set ($self->{key}, $self->{value})
+=item get_default_accounts()
+
+The above list may grow over time, and may depend on other installed modules.
+
+=head1 Copyright (C) 2007, The LedgerSMB core team.
+This file is licensed under the Gnu General Public License version 2, or at your
+option any later version.  A copy of the license should have been included with
+your software.
+
+=back
+
+=cut
+
+package LedgerSMB::Setting;
+use LedgerSMB;
+use LedgerSMB::DBObject;
+our $VERSION = '1.0.0';
+
+our @ISA = qw(LedgerSMB::DBObject);
+
+sub AUTOLOAD {
+	my $self = shift;
+	$AUTOLOAD =~ s/^.*:://;
+	my $procname = "setting_$AUTOLOAD";
+	$self->exec_method($procname, @_);
+}
+
+sub get {
+	$self = shift;
+	$hashref = @{$self->exec_method('setting_get');
+	$self->merge($hashref, 'value');
+
+sub parse {
+
+	$self = shift;
+
+	# Long-run, we may want to run this via Parse::RecDescent, but this is
+	# at least a start for here.  Chris T.
+
+	# Replaces Form::UpdateDefaults
+
+	$_ = $self->incriment;
+	# check for and replace
+	# <?lsmb DATE ?>, <?lsmb YYMMDD ?>, <?lsmb YEAR ?>, <?lsmb MONTH ?>, <?lsmb DAY ?> or variations of
+	# <?lsmb NAME 1 1 3 ?>, <?lsmb BUSINESS ?>, <?lsmb BUSINESS 10 ?>, <?lsmb CURR... ?>
+	# <?lsmb DESCRIPTION 1 1 3 ?>, <?lsmb ITEM 1 1 3 ?>, <?lsmb PARTSGROUP 1 1 3 ?> only for parts
+	# <?lsmb PHONE ?> for customer and vendors
+
+	my $dbvar = $_;
+	my $var = $_;
+	my $str;
+	my $param;
+
+	if (/<\?lsmb /) {
+
+		while (/<\?lsmb /) {
+
+			s/<\?lsmb .*? \?>//;
+			last unless $&;
+		$param = $&;
+			$str = "";
+
+			if ($param =~ /<\?lsmb date \?>/i) {
+				$str = ($self->split_date($myconfig->{dateformat}, $self->{transdate}))[0];
+				$var =~ s/$param/$str/;
+			}
+
+			if ($param =~ /<\?lsmb (name|business|description|item|partsgroup|phone|custom)/i) {
+
+				my $fld = lc $&;
+				$fld =~ s/<\?lsmb //;
+
+				if ($fld =~ /name/) {
+					if ($self->{type}) {
+						$fld = $self->{vc};
+					}
+				}
+
+				my $p = $param;
+				$p =~ s/(<|>|%)//g;
+				my @p = split / /, $p;
+				my @n = split / /, uc $self->{$fld};
+
+				if ($#p > 0) {
+
+					for (my $i = 1; $i <= $#p; $i++) {
+						$str .= substr($n[$i-1], 0, $p[$i]);
+					}
+
+				} else {
+					($str) = split /--/, $self->{$fld};
+				}
+
+				$var =~ s/$param/$str/;
+				$var =~ s/\W//g if $fld eq 'phone';
+			}
+
+			if ($param =~ /<\?lsmb (yy|mm|dd)/i) {
+
+				my $p = $param;
+				$p =~ s/(<|>|%)//g;
+				my $spc = $p;
+				$spc =~ s/\w//g;
+				$spc = substr($spc, 0, 1);
+				my %d = ( yy => 1, mm => 2, dd => 3 );
+				my @p = ();
+
+				my @a = $self->split_date($myconfig->{dateformat}, $self->{transdate});
+				for (sort keys %d) { push @p, $a[$d{$_}] if ($p =~ /$_/) }
+				$str = join $spc, @p;
+				$var =~ s/$param/$str/;
+			}
+
+			if ($param =~ /<\?lsmb curr/i) {
+				$var =~ s/$param/$self->{currency}/;
+			}
+		}
+	}
+
+	$self->{value} = $var;
+	$var;
+}
+

Modified: trunk/LedgerSMB.pm
===================================================================
--- trunk/LedgerSMB.pm	2007-03-10 00:41:53 UTC (rev 875)
+++ trunk/LedgerSMB.pm	2007-03-10 06:28:31 UTC (rev 876)
@@ -2722,151 +2722,7 @@
 }
 
 
-sub update_defaults {
 
-	my ($self, $myconfig, $fld) = @_;
-
-	if (!$self->{dbh} && $self){
-		$self->db_init($myconfig);
-	} 
-		
-	my $dbh = $self->{dbh};
-
-	if (!$self){
-		$dbh = $_[3];
-	}
-
-	my $query = qq|
-		SELECT value FROM defaults 
-		 WHERE setting_key = ? FOR UPDATE|;
-	$sth = $dbh->prepare($query);
-	$sth->execute($fld);
-	($_) = $sth->fetchrow_array();
-
-	$_ = "0" unless $_;
-
-	# check for and replace
-	# <?lsmb DATE ?>, <?lsmb YYMMDD ?>, <?lsmb YEAR ?>, <?lsmb MONTH ?>, <?lsmb DAY ?> or variations of
-	# <?lsmb NAME 1 1 3 ?>, <?lsmb BUSINESS ?>, <?lsmb BUSINESS 10 ?>, <?lsmb CURR... ?>
-	# <?lsmb DESCRIPTION 1 1 3 ?>, <?lsmb ITEM 1 1 3 ?>, <?lsmb PARTSGROUP 1 1 3 ?> only for parts
-	# <?lsmb PHONE ?> for customer and vendors
-
-	my $num = $_;
-	($num) = $num =~ /(\d+)/;
-
-	if (defined $num) {
-		my $incnum;
-		# if we have leading zeros check how long it is
-
-		if ($num =~ /^0/) {
-			my $l = length $num;
-			$incnum = $num + 1;
-			$l -= length $incnum;
-
-			# pad it out with zeros
-			my $padzero = "0" x $l;
-			$incnum = ("0" x $l) . $incnum;
-		} else {
-			$incnum = $num + 1;
-		}
-
-		s/$num/$incnum/;
-	}
-
-	my $dbvar = $_;
-	my $var = $_;
-	my $str;
-	my $param;
-
-	if (/<\?lsmb /) {
-
-		while (/<\?lsmb /) {
-
-			s/<\?lsmb .*? \?>//;
-			last unless $&;
-		$param = $&;
-			$str = "";
-
-			if ($param =~ /<\?lsmb date \?>/i) {
-				$str = ($self->split_date($myconfig->{dateformat}, $self->{transdate}))[0];
-				$var =~ s/$param/$str/;
-			}
-
-			if ($param =~ /<\?lsmb (name|business|description|item|partsgroup|phone|custom)/i) {
-
-				my $fld = lc $&;
-				$fld =~ s/<\?lsmb //;
-
-				if ($fld =~ /name/) {
-					if ($self->{type}) {
-						$fld = $self->{vc};
-					}
-				}
-
-				my $p = $param;
-				$p =~ s/(<|>|%)//g;
-				my @p = split / /, $p;
-				my @n = split / /, uc $self->{$fld};
-
-				if ($#p > 0) {
-
-					for (my $i = 1; $i <= $#p; $i++) {
-						$str .= substr($n[$i-1], 0, $p[$i]);
-					}
-
-				} else {
-					($str) = split /--/, $self->{$fld};
-				}
-
-				$var =~ s/$param/$str/;
-				$var =~ s/\W//g if $fld eq 'phone';
-			}
-
-			if ($param =~ /<\?lsmb (yy|mm|dd)/i) {
-
-				my $p = $param;
-				$p =~ s/(<|>|%)//g;
-				my $spc = $p;
-				$spc =~ s/\w//g;
-				$spc = substr($spc, 0, 1);
-				my %d = ( yy => 1, mm => 2, dd => 3 );
-				my @p = ();
-
-				my @a = $self->split_date($myconfig->{dateformat}, $self->{transdate});
-				for (sort keys %d) { push @p, $a[$d{$_}] if ($p =~ /$_/) }
-				$str = join $spc, @p;
-				$var =~ s/$param/$str/;
-			}
-
-			if ($param =~ /<\?lsmb curr/i) {
-				$var =~ s/$param/$self->{currency}/;
-			}
-		}
-	}
-
-	$query = qq|
-		UPDATE defaults
-		   SET value = ?
-		 WHERE setting_key = ?|;
-
-	$sth = $dbh->prepare($query); 
-	$sth->execute($dbvar, $fld) || $self->dberror($query);
-
-	$dbh->commit;
-
-	$var;
-}
-
-sub db_prepare_vars {
-	my $self = shift;
-
-	for (@_){
-		if (!$self->{$_} and $self->{$_} ne "0"){
-			undef $self->{$_};
-		}
-	}
-}
-
 sub split_date {
 
 	my ($self, $dateformat, $date) = @_;

Added: trunk/sql/modules/Settings.sql
===================================================================
--- trunk/sql/modules/Settings.sql	                        (rev 0)
+++ trunk/sql/modules/Settings.sql	2007-03-10 06:28:31 UTC (rev 876)
@@ -0,0 +1,67 @@
+CREATE OR REPLACE FUNCTION setting_set (in_key varchar, in_value varchar) 
+RETURNS VOID AS
+$$
+BEGIN
+	UPDATE defaults SET value = in_value WHERE setting_key = in_key;
+	RETURN;
+END;
+
+CREATE OR REPLACE FUNCTION setting_get (in_key varchar) RETURNS varchar AS
+$$
+DECLARE
+	out_value varchar;
+BEGIN
+	SELECT value INTO out_value FROM defaults WHERE setting_key = in_key;
+	RETURN value;
+END;
+$$ LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION setting_get_default_accounts () 
+RETURNS SETOF defaults AS
+$$
+DECLARE
+	account defaults%ROWTYPE;
+BEGIN;
+	FOR account IN 
+		SELECT * FROM defaults 
+		WHERE setting_key like '%accno_id'
+	LOOP
+		RETURN NEXT account;
+	END LOOP;
+END;
+$$ LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION setting_incriment (in_key varchar) returns varchar
+AS
+$$
+DECLARE
+	base_value VARCHAR;
+	raw_value VARCHAR;
+	incriment INTEGER;
+	inc_length INTEGER;
+	new_value VARCHAR;
+BEGIN
+	SELECT value INTO raw_value FROM defaults 
+	WHERE setting_key = in_key
+	FOR UPDATE;
+
+	SELECT substring(raw_value from  '(\\d*)(\\D*|<\\?lsmb [^<>] \\?>)*$')
+	INTO base_value;
+
+	IF base_value like '0%' THEN
+		incriment := base_value::integer + 1;
+		SELECT char_length(incriment::text) INTO inc_length;
+
+		SELECT overlay(base_value placing incriment::varchar
+			from (select char_length(base_value) 
+				- inc_length + 1) for inc_length)
+		INTO new_value;
+	ELSE
+		new_value := base_value::integer + 1;
+	END IF;
+	SELECT regexp_replace(raw_value, base_value, new_value) INTO new_value;
+	UPDATE defaults SET value = new_value WHERE setting_key = in_key;
+
+	return new_value;	
+END;
+$$ LANGUAGE PLPGSQL;


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.