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

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



Revision: 2725
          http://ledger-smb.svn.sourceforge.net/ledger-smb/?rev=2725&view=rev
Author:   einhverfr
Date:     2009-07-14 23:23:40 +0000 (Tue, 14 Jul 2009)

Log Message:
-----------
Moving yearend over to new framework.   This:
check to ensure no unposted transactions in closed period
and
Merges close books and yearend processes.

Modified Paths:
--------------
    trunk/LedgerSMB/AM.pm
    trunk/bin/am.pl
    trunk/scripts/account.pl
    trunk/sql/Pg-database.sql
    trunk/sql/modules/EndOfYear.sql

Added Paths:
-----------
    trunk/LedgerSMB/DBObject/EOY.pm

Modified: trunk/LedgerSMB/AM.pm
===================================================================
--- trunk/LedgerSMB/AM.pm	2009-07-14 22:23:41 UTC (rev 2724)
+++ trunk/LedgerSMB/AM.pm	2009-07-14 23:23:40 UTC (rev 2725)
@@ -128,148 +128,6 @@
     $dbh->commit;
 }
 
-=item AM->save_account($myconfig, $form);
-
-Adds or updates an account in the chart of accounts.  If $form->{id} is set,
-the existing account with an id of $form->{id} is updated, otherwise a new
-account is created.  The values for accno, description, charttype, gifi_accno,
-category, and contra are taken directly from the $form attributes of the same
-name.  The link value is generated in $form->{link} as a colon seperated list of
-non-empty values from the $form attributes 'AR', 'AR_amount', 'AR_tax',
-'AR_paid', 'AP', 'AP_amount', 'AP_tax', 'AP_paid', 'IC', 'IC_income', 'IC_sale',
-'IC_expense', 'IC_cogs', 'IC_taxpart', and 'IC_taxservice'.
-
-If the account is a tax account, indicated by any of IC_taxpart, IC_taxservice,
-AR_tax, and AP_tax being true, and there is no entry in the tax table for the
-account, a tax entry is added for the account.  If none of those tax flags are
-set and this function is updating an existing account, all entries in the tax
-table for the account are deleted.
-
-$myconfig is unused.
-
-=cut
-
-sub save_account {
-
-    my ( $self, $myconfig, $form ) = @_;
-
-    # connect to database, turn off AutoCommit
-    my $dbh = $form->{dbh};
-
-    $form->{link} = "";
-    foreach my $item (
-        $form->{AR},        $form->{AR_amount},  $form->{AR_tax}, $form->{AR_overpayment},
-        $form->{AR_discount}, $form->{AR_paid},  $form->{AP},     $form->{AP_amount},
-        $form->{AP_overpayment}, $form->{AP_discount}, $form->{AP_tax},
-        $form->{AP_paid},    $form->{IC}, $form->{IC_income}, $form->{IC_sale},
-        $form->{IC_expense}, $form->{IC_cogs},   $form->{IC_taxpart}, $form->{IC_taxservice}
-      )
-    {
-        $form->{link} .= "${item}:" if ($item);
-    }
-
-    chop $form->{link};
-
-    # strip blanks from accno
-    for (qw(accno gifi_accno)) { $form->{$_} =~ s/( |')//g }
-
-    foreach my $item (qw(accno gifi_accno description)) {
-        $form->{$item} =~ s/-(-+)/-/g;
-        $form->{$item} =~ s/ ( )+/ /g;
-    }
-
-    my $query;
-    my $sth;
-
-    $form->{contra} *= 1;
-
-    my @queryargs;
-    @queryargs = (
-        $form->{accno},      $form->{description}, $form->{charttype},
-        $form->{gifi_accno}, $form->{category},    $form->{"link"},
-        $form->{contra}
-    );
-
-    # if we have an id then replace the old record
-    if ( $form->{id} ) {
-        $query = qq|
-			UPDATE chart SET accno = ?,
-			       description = ?,
-			       charttype = ?,
-			       gifi_accno = ?,
-			       category = ?,
-			       link = ?,
-			       contra = ?
-			 WHERE id = ?|;
-        push @queryargs, $form->{id};
-    }
-    else {
-        $query = qq|
-			INSERT INTO chart 
-                                    (accno, description, charttype, 
-			            gifi_accno, category, link, contra)
-			     VALUES (?, ?, ?, ?, ?, ?, ?)|;
-    }
-
-    $sth = $dbh->prepare($query);
-    $sth->execute(@queryargs) || $form->dberror($query);
-    $sth->finish;
-
-    $chart_id = $dbh->quote( $form->{id} );
-
-    if ( !$form->{id} ) {
-
-        # get id from chart
-        $query = qq|
-			SELECT id
-			FROM   chart
-			WHERE  accno = ?|;
-
-        $sth = $dbh->prepare($query);
-        $sth->execute( $form->{accno} );
-        ($chart_id) = $sth->fetchrow_array();
-        $sth->finish;
-    }
-
-    if (   $form->{IC_taxpart}
-        || $form->{IC_taxservice}
-        || $form->{AR_tax}
-        || $form->{AP_tax} )
-    {
-
-        # add account if it doesn't exist in tax
-        $query = qq|SELECT chart_id
-					  FROM tax
-					 WHERE chart_id = $chart_id|;
-
-        my ($tax_id) = $dbh->selectrow_array($query);
-
-        # add tax if it doesn't exist
-        unless ($tax_id) {
-            $query = qq|INSERT INTO tax (chart_id, rate)
-							 VALUES ($chart_id, 0)|;
-
-            $dbh->do($query) || $form->dberror($query);
-        }
-
-    }
-    else {
-
-        # remove tax
-        if ( $form->{id} ) {
-            $query = qq|DELETE FROM tax
-							  WHERE chart_id = $form->{id}|;
-
-            $dbh->do($query) || $form->dberror($query);
-        }
-    }
-
-    # commit
-    my $rc = $dbh->commit;
-
-    $rc;
-}
-
 =item AM->delete_account($myconfig, $form);
 
 Deletes the account with the id $form->{id}.  Calls $form->error if there are

Added: trunk/LedgerSMB/DBObject/EOY.pm
===================================================================
--- trunk/LedgerSMB/DBObject/EOY.pm	                        (rev 0)
+++ trunk/LedgerSMB/DBObject/EOY.pm	2009-07-14 23:23:40 UTC (rev 2725)
@@ -0,0 +1,99 @@
+=head1 NAME
+
+LedgerSMB::DBObject::EOY: End of Year handling module
+
+=head1 SYNOPSYS
+
+This class contains methods for end of year entry.
+
+=head1 BASIC PROPERTIES
+
+=over
+
+=item end_date specifies the end date for a closed period.
+
+=item reference specifies the gl reference field associated with the account 
+closure
+
+=item description specifies the gl description field associated with the account
+closure
+
+=item retention_acc_id specifies the account id used as a retaining account.
+
+=head1 METHODS
+
+=over
+
+=cut
+
+use strict;
+package LedgerSMB::DBObject::EOY;
+use base qw(LedgerSMB::DBObject);
+
+=item $eoy->checkpoint_only();
+
+This creates account checkpoints at $eoy->{end_date}.  This has two uses:
+1)  Can be used to "close" books without zeroing income/expense accounts.  This
+prevents data from being inserted for earlier dates.
+
+2)  This can be used to improve performance by creating a "lookback" point.
+
+=cut
+
+sub checkpoint_only {
+    my ($self) = @_;
+   $self->exec_method(funcname => 'eoy_create_checkpoint');
+   $self->{dbh}->commit;
+}
+
+=item $eoy->reopen_books()
+
+This reverses any end of year transaction on $eoy->{end_date}, and deletes 
+checkpoints for that day.
+
+=cut
+
+sub reopen_books {
+    my ($self) = @_;
+   $self->exec_method(funcname => 'eoy_reopen_books');
+   $self->{dbh}->commit;
+}
+
+=item $eoy->close_books()
+
+Requires all properies in BASIC PROPERTIES to be set.  This creates a gl 
+yearend transaction, and moves income/expenses to the selected equity account
+for retained earnings.
+
+=cut
+
+sub close_books {
+    my ($self) = @_;
+   $self->exec_method(funcname => 'eoy_reopen_books');
+   $self->{dbh}->commit;
+}
+
+=item $eoy->list_earnings_accounts
+
+Returns a list of equity accounts, and sets $eoy->{earnings_accounts} to a 
+list of hashrefs.  These are used to select retained earnings accounts in 
+closing books.
+
+=cut
+
+sub list_earnings_accounts{
+    my ($self) = @_;
+    my @results = $self->exec_method(funcname => 'earnings_accounts');
+    $self->{earnings_accounts} = ..hidden..;
+    return @results;
+}
+
+1;
+
+=back
+
+=head1 COPYRYIGHT
+
+Copyright (C) 2009 The LedgerSMB Core Team.  This may be re-used as permitted by
+the GNU General Public License v 2 or at your option any later version.  Please
+see included License.txt for details.

Modified: trunk/bin/am.pl
===================================================================
--- trunk/bin/am.pl	2009-07-14 22:23:41 UTC (rev 2724)
+++ trunk/bin/am.pl	2009-07-14 23:23:40 UTC (rev 2725)
@@ -57,85 +57,6 @@
 
 }
 
-sub add_account {
-
-    $form->{title}     = "Add";
-    $form->{charttype} = "A";
-
-    $form->{callback} =
-"$form->{script}?action=list_account&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}"
-      unless $form->{callback};
-
-    my %hiddens;
-    my @buttons;
-    my $checked = &account_header(\%hiddens);
-    &form_footer_buttons(\%hiddens, ..hidden..);
-
-    my $template = LedgerSMB::Template->new_UI(
-        user => \%myconfig, 
-        locale => $locale,
-        template => 'am-account-form');
-    $template->render({
-        form => $form,
-        checked => $checked,
-        buttons => ..hidden..,
-        hiddens => \%hiddens,
-    });
-
-}
-
-sub edit_account {
-
-    $form->{title} = "Edit";
-
-    $form->{accno} =~ s/\\'/'/g;
-    $form->{accno} =~ s/\\\\/\\/g;
-
-    AM->get_account( \%myconfig, \%$form );
-
-    foreach my $item ( split( /:/, $form->{link} ) ) {
-        $form->{$item} = "checked";
-    }
-
-    my %hiddens;
-    my @buttons;
-    my $checked = &account_header(\%hiddens);
-    &form_footer_buttons(\%hiddens, ..hidden..);
-
-    my $template = LedgerSMB::Template->new_UI(
-        user => \%myconfig, 
-        locale => $locale,
-        template => 'am-account-form');
-    $template->render({
-        form => $form,
-        checked => $checked,
-        buttons => ..hidden..,
-        hiddens => \%hiddens,
-    });
-}
-
-sub account_header {
-
-    my $hiddens = shift;
-    $form->{title} = $locale->text("$form->{title} Account");
-
-    my %checked;
-    $checked{ $form->{charttype} } = "checked";
-    $checked{contra} = "checked" if $form->{contra};
-    $checked{"$form->{category}_"} = "checked";
-
-    for (qw(accno description)) { $form->{$_} = $form->quote( $form->{$_} ) }
-
-    # this is for our parser only!
-    # type=submit $locale->text('Add Account')
-    # type=submit $locale->text('Edit Account')
-
-    $hiddens->{type} = 'account';
-    $hiddens->{$_} = $form->{$_} foreach qw(id inventory_accno_id income_accno_id expense_accno_id fxgain_accno_id fxloss_accno_id);
-
-    \%checked;
-}
-
 sub form_footer_buttons {
 
     my ($hiddens, $buttons) = @_;

Modified: trunk/scripts/account.pl
===================================================================
--- trunk/scripts/account.pl	2009-07-14 22:23:41 UTC (rev 2724)
+++ trunk/scripts/account.pl	2009-07-14 23:23:40 UTC (rev 2725)
@@ -87,7 +87,7 @@
     my $template = LedgerSMB::Template->new_UI(
         user => $form->{_user}, 
         locale => $locale,
-        template => 'am-account-form');
+        template => 'accounts/edit');
     $template->render({
         form => $form,
         checked => $checked,
@@ -97,7 +97,34 @@
 
 }
 
+sub yearend_info {
+    use LedgerSMB::DBObject::EOY;
+    my ($request) = @_;
+    my $eoy =  LedgerSMB::DBObject::EOY->new(base => $request);
+    $eoy->list_earnings_accounts;
+    $eoy->user = $request->{_user};    
+    my $template = LedgerSMB::Template->new_UI(
+        user => $form->{_user}, 
+        locale => $locale,
+        template => 'accounts/yearend'
+    );
+    $temlate->render($eoy);
+}
 
+sub post_yearend {
+    use LedgerSMB::DBObject::EOY;
+    my ($request) = @_;
+    my $eoy =  LedgerSMB::DBObject::EOY->new(base => $request);
+    $eoy->close_books;
+    my $template = LedgerSMB::Template->new_UI(
+        user => $form->{_user}, 
+        locale => $locale,
+        template => 'accounts/yearend_complete'
+    );
+    $temlate->render($eoy);
+    
+}
+
 # From AM.pm, modified for better API.
 
 1;

Modified: trunk/sql/Pg-database.sql
===================================================================
--- trunk/sql/Pg-database.sql	2009-07-14 22:23:41 UTC (rev 2724)
+++ trunk/sql/Pg-database.sql	2009-07-14 23:23:40 UTC (rev 2725)
@@ -2417,8 +2417,8 @@
 130	taxes	audit_control	341
 131	action	defaults	342
 130	action	taxes	343
-132	module	am.pl	346
-132	action	yearend	347
+132	module	account.pl	346
+132	action	yearend_info	347
 133	menu	1	348
 134	module	am.pl	349
 135	module	am.pl	350

Modified: trunk/sql/modules/EndOfYear.sql
===================================================================
--- trunk/sql/modules/EndOfYear.sql	2009-07-14 22:23:41 UTC (rev 2724)
+++ trunk/sql/modules/EndOfYear.sql	2009-07-14 23:23:40 UTC (rev 2725)
@@ -154,3 +154,10 @@
 	RETURN balance
 END;
 $$ LANGUAGE PLPGSQL;
+
+CREATE OR REPLACE FUNCTION eoy_earnings_accounts() RETURNS setof account AS 
+$$
+SELECT id, accno, description from account 
+WHERE category = 'Q'
+ORDER BY accno;
+$$ language sql;


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