[wiaflos-devel] COMMIT - r242 - trunk/wiaflos/server

svn at linuxrulz.org svn at linuxrulz.org
Sun Aug 24 17:03:57 GMT 2008


Author: nkukard
Date: 2008-08-24 17:03:56 +0000 (Sun, 24 Aug 2008)
New Revision: 242

Modified:
   trunk/wiaflos/server/GL.pm
   trunk/wiaflos/server/Reporting.pm
Log:
* getGLAccountBalance now returns Credit/Debit amounts
* Adjusted reporting to work with the new getGLAccountBalance
* Return credit & debit balances for use in reports


Modified: trunk/wiaflos/server/GL.pm
===================================================================
--- trunk/wiaflos/server/GL.pm	2008-08-17 18:51:26 UTC (rev 241)
+++ trunk/wiaflos/server/GL.pm	2008-08-24 17:03:56 UTC (rev 242)
@@ -1216,12 +1216,14 @@
 # Return GL account balance
 #
 # @param data Parameter hash ref
-# @li AccountID Optional account ID
-# @li AccountNumber Optional account number
-# @li StartDate	Optional start date, INCLUDING
-# @li EndDate Optional end date, NOT INCLUDING
+# @li AccountID - Optional account ID
+# @li AccountNumber - Optional account number
+# @li StartDate	- Optional start date, INCLUDING
+# @li EndDate - Optional end date, NOT INCLUDING
 #
-# @returns Account balance
+# @returns Hash of account balances
+# @li CreditBalance - Credit balance
+# @li DebitBalance - Debit balance
 sub getGLAccountBalance
 {
 	my ($data) = @_;
@@ -1281,17 +1283,33 @@
 		return ERR_DB;
 	}
 
-	# Add up
-	my $amount = Math::BigFloat->new();
-	$amount->precision(-2);
+	# These are our balances
+	my $balance = Math::BigFloat->new();
+	my $creditBalance = Math::BigFloat->new();
+	my $debitBalance = Math::BigFloat->new();
+	$balance->precision(-2);
+	$creditBalance->precision(-2);
+	$debitBalance->precision(-2);
+
+	# Add the balances up
 	while (my $row = $sth->fetchrow_hashref()) {
-		$amount->badd($row->{'Amount'});
+		$balance->bzero()->badd($row->{'Amount'});	
+
+		# Check if its credit or debit
+		if ($balance->is_neg()) {
+			$balance->babs();
+			$creditBalance->badd($balance);
+		} else {
+			$debitBalance->badd($balance);
+		}
 	}
 
 	DBFreeRes($sth);
-	
+
+	# Build result	
 	my $res;
-	$res->{'Balance'} = $amount->bstr();
+	$res->{'CreditBalance'} = $creditBalance->bstr();
+	$res->{'DebitBalance'} = $debitBalance->bstr();
 
 	return $res;
 }

Modified: trunk/wiaflos/server/Reporting.pm
===================================================================
--- trunk/wiaflos/server/Reporting.pm	2008-08-17 18:51:26 UTC (rev 241)
+++ trunk/wiaflos/server/Reporting.pm	2008-08-24 17:03:56 UTC (rev 242)
@@ -34,9 +34,23 @@
 
 use Math::BigFloat;
 
+# Exporter stuff
+require Exporter;
+our (@ISA, at EXPORT, at EXPORT_OK);
+ at ISA = qw(Exporter);
+ at EXPORT = qw(
+	REPORT_BALANCE_BF
+);
+ at EXPORT_OK = ();
 
 
+use constant {
+	REPORT_BALANCE_BF	=> 1,
+};
 
+
+
+
 # Our current error message
 my $error = "";
 
@@ -66,30 +80,39 @@
 }
 
 
-## @fn getChartOfAccounts
+## @fn getAccountBalances
 # Function to return the chart of accounts with balances
 #
-# @param detail Parameter hash ref
-# @li StartDate Optional statement start date
-# @li EndDate Optional statement end date
-# @li Levels Optional depth to go into on each parent account
+# @param flags Flags of what we returned, options are below, use || (OR)
+# @li REPORT_BALANCE_BF - Return balance brought forward for each account
 #
+# @param data Parameter hash ref
+# @li StartDate - Optional statement start date
+# @li EndDate - Optional statement end date
+# @li Levels - Optional depth to go into on each parent account
+#
 # @returns Array ref of hash refs containing the accounts and their balances
-# @li ID Account ID
-# @li Number Account number
-# @li Name Name of account
-# @li Balance Account balance
-# @li DebitAmount Debit amount
-# @li CreditAmount Credit amount
-sub getChartOfAccounts
+# @li ID - Account ID
+# @li Number - Account number
+# @li Name - Name of account
+#
+# @li Balance - Account balance
+# Balance of account, can either be - (credit) or + (debit)
+#
+# @li DebitBalance - Optional, this is the total of all debits
+# @li CreditBalance - Optional, this is the total of all credits
+#
+# @li BalanceBroughtForward - Optional, returned when REPORT_BALANCE_BF is specified
+# Thi is the balance of the account for the period up until this reporting period
+sub getAccountBalances
 {
-	my ($detail) = @_;
+	my ($flags,$data) = @_;
 	
 	# GL search criteria
 	my $search;
-	$search->{'StartDate'} = $detail->{'StartDate'};
-	$search->{'EndDate'} = $detail->{'EndDate'};
-	$search->{'Levels'} = $detail->{'Levels'};
+	$search->{'StartDate'} = $data->{'StartDate'};
+	$search->{'EndDate'} = $data->{'EndDate'};
+	$search->{'Levels'} = $data->{'Levels'};
 
 	# Get our account tree
 	my $res = wiaflos::server::GL::getGLAccountTree();
@@ -106,9 +129,11 @@
 		{
 			my ($paccount,$search) = @_;
 
-			# This is our final balance
-			my $balance = Math::BigFloat->new();
-			$balance->precision(-2);
+			# This is our final balances
+			my $creditBalance = Math::BigFloat->new();
+			my $debitBalance = Math::BigFloat->new();
+			$creditBalance->precision(-2);
+			$debitBalance->precision(-2);
 			
 			# Loop with children
 			foreach my $caccount (@{$paccount->{'Children'}}) {
@@ -119,7 +144,8 @@
 					return $pcres;
 				}
 				# Add up balance of child...
-				$balance->badd($caccount->{'Balance'});
+				$creditBalance->badd($caccount->{'CreditBalance'});
+				$debitBalance->badd($caccount->{'DebitBalance'});
 			}
 			
 			# Build param we need to pass to get this accounts balance
@@ -132,17 +158,14 @@
 				return $res;
 			}
 			# Add it up ...
-			$balance->badd($res->{'Balance'});
+			$creditBalance->badd($res->{'CreditBalance'});
+			$debitBalance->badd($res->{'DebitBalance'});
 
 			# Finish it off...
-			$paccount->{'Balance'} = $balance->bstr();
+			$paccount->{'CreditBalance'} = $creditBalance->bstr();
+			$paccount->{'DebitBalance'} = $debitBalance->bstr();
 
-			# Decide wtf we are
-			if ($balance->is_neg()) {
-				$paccount->{'CreditAmount'} = $balance->copy()->babs()->bstr();
-			} else {
-				$paccount->{'DebitAmount'} = $balance->bstr();
-			}
+			$paccount->{'Balance'} = $debitBalance->copy()->bsub($creditBalance)->bstr();
 
 			return 0;
 		}
@@ -188,7 +211,7 @@
 	my $search;
 	$search->{'StartDate'} = $detail->{'StartDate'};
 	$search->{'EndDate'} = $detail->{'EndDate'};
-	my $entries = getChartOfAccounts($search);
+	my $entries = getAccountBalances($search);
 	if (ref $entries ne "ARRAY") {
 		setError(Error());
 		return $entries;
@@ -201,7 +224,7 @@
 	my $subject = (defined($detail->{'Subject'}) && $detail->{'Subject'} ne "") ? $detail->{'Subject'} : "Chart of accounts";
 
 	# Build array of stuff we can use
-	my $vars = {
+	our $vars = {
 		'WiaflosString' => $GENSTRING,
 
 		# Client
@@ -211,22 +234,57 @@
 
 	# Load invoice line items
 	foreach my $item (@{$entries}) {
-		my $titem;
 
-		# Fix some stuff up
-		$titem->{'Number'} = $item->{'Number'};
-		$titem->{'Name'} = $item->{'Name'};
-		$titem->{'DebitAmount'} = defined($item->{'DebitAmount'}) ? sprintf('%.2f',$item->{'DebitAmount'}) : undef;
-		$titem->{'CreditAmount'} = defined($item->{'CreditAmount'}) ? sprintf('%.2f',$item->{'CreditAmount'}) : undef;
+		# Process item
+		sub processItem
+		{
+			my $account = shift;
+
+
+			my $i;
+
+			# Fix some stuff up
+			$i->{'Number'} = $account->{'Number'};
+			$i->{'Name'} = $account->{'Name'};
+
+			$i->{'Balance'} = $account->{'Balance'};
+
+			# Check balance
+			my $balance = Math::BigFloat->new();
+			$balance->precision(-2);
+			$balance->badd($account->{'Balance'});
+			# Check if its negative or positive
+			if ($balance->is_neg()) {
+				$balance->babs();
+				$i->{'CreditAmount'} = sprintf('%.2f',$balance->bstr());
+			} else {
+				$i->{'DebitAmount'} = sprintf('%.2f',$balance->bstr());
+			}
 		
-		$titem->{'ReportWriterCategoryCode'} = $item->{'RwCatCode'};
-		
-		$titem->{'Children'} = $item->{'Children'};
+			$i->{'ReportWriterCategoryCode'} = $account->{'RwCatCode'};
 
-		# File under account number
-		$vars->{'AccountBalances'}{$titem->{'Number'}} = $titem;
+			# If we have children, process them	
+			if ($account->{'Children'}) {
+				# Loop with child accounts
+				foreach my $caccount (@{$account->{'Children'}}) {
+					# Process
+					my $citem = processItem($caccount);
+					# Add to child list
+					push(@{$i->{'Children'}},$citem);
+				}
+			}
+
+			# File under account number
+			$vars->{'AccountBalances'}{$i->{'Number'}} = $i;
+
+			return $i;
+		}
+
+		# Process top level item
+		my $pitem = processItem($item);
+
 		# File under report write category code
-		push(@{$vars->{'ReportWriterCategoryToAccounts'}{$titem->{'ReportWriterCategoryCode'}}},$titem);
+		push(@{$vars->{'ReportWriterCategoryToAccounts'}{$pitem->{'ReportWriterCategoryCode'}}},$pitem);
 	}
 	
 	# Load report write categories



More information about the wiaflos-devel mailing list