[wiaflos-devel] COMMIT - r256 - trunk/wiaflos/server
svn at linuxrulz.org
svn at linuxrulz.org
Sat Nov 8 16:43:39 GMT 2008
Author: nkukard
Date: 2008-11-08 16:43:39 +0000 (Sat, 08 Nov 2008)
New Revision: 256
Modified:
trunk/wiaflos/server/Reporting.pm
Log:
* Added variable API
* Fixed up error returning of API functions
Modified: trunk/wiaflos/server/Reporting.pm
===================================================================
--- trunk/wiaflos/server/Reporting.pm 2008-11-08 16:42:27 UTC (rev 255)
+++ trunk/wiaflos/server/Reporting.pm 2008-11-08 16:43:39 UTC (rev 256)
@@ -181,11 +181,12 @@
$paccount->{'Balance'} = $balance->bstr();
# Check if we need to pull in balance brought forward
- if ($flags && REPORT_BALANCE_BF == REPORT_BALANCE_BF) {
+ if ($flags & REPORT_BALANCE_BF == REPORT_BALANCE_BF) {
# We can only do this if we have a start date, else its pointless?
if ($search->{'StartDate'}) {
my $bfinfo;
$bfinfo->{'EndDate'} = $search->{'StartDate'};
+ $bfinfo->{'EndDateExcl'} = 1;
$bfinfo->{'AccountID'} = $paccount->{'ID'};
# Pull in balance brought forward
$res = wiaflos::server::GL::getGLAccountBalance($bfinfo);
@@ -239,7 +240,15 @@
{
our ($detail) = @_;
-
+
+ # Variables used in the API functions
+ our %variables = ();
+ # Constants used in API functions
+ use constant {
+ API_FMT_REVERSE => 1,
+ API_FMT_NEGBRACKET => 2,
+ };
+
# Reporting API function to retrieve account balances
sub api_account_balances
{
@@ -250,10 +259,7 @@
my $entries = getAccountBalances(REPORT_BALANCE_BF,$search);
if (ref($entries) ne "ARRAY") {
-# FIXME
-# setError(Error());
-# return $entries;
- return;
+ wiaflos::server::templating::abort('api_account_balances',$entries);
}
our @accountList = ();
@@ -262,7 +268,7 @@
}
# Result to return
- our $resdata;
+ our $resdata = undef;
# Load line items for GL
foreach my $item (@{$entries}) {
@@ -384,8 +390,7 @@
$tmp->{'AccountNumber'} = $detail->{'GLAccountNumber'};
my $account = wiaflos::server::GL::getGLAccount($tmp);
if (ref($account) ne "HASH") {
-# FIXME
- return;
+ wiaflos::server::templating::abort('api_account_entries',$account);
}
$resdata->{'Account'} = $account;
@@ -396,8 +401,7 @@
$tmp->{'BalanceBroughtForward'} = 1;
my $res = wiaflos::server::GL::getGLAccountEntries($tmp);
if (ref($res) ne "ARRAY") {
-# FIXME
- return;
+ wiaflos::server::templating::abort('api_account_entries',$res);
}
# Sort so our balance makes sense
@@ -451,8 +455,7 @@
$tmp->{'EndDate'} = $detail->{'EndDate'};
my $res = wiaflos::server::Inventory::getInventoryStockBalance($tmp);
if (ref($res) ne "HASH") {
-# FIXME
- return;
+ wiaflos::server::templating::abort('api_inventory_stock_balances',$res);
}
# Work out closing balance
@@ -498,6 +501,148 @@
}
+ # Reporting API function to create a variable
+ sub api_variable_new
+ {
+ my ($item,$type) = @_;
+
+ # Check what type we are
+ if (lc($type) eq "float") {
+ $variables{$item}{'type'} = "float";
+ $variables{$item}{'value'} = new Math::BigFloat();
+ $variables{$item}{'value'}->precision(-2);
+ }
+ # We shouldn't return results
+ return undef;
+ }
+
+
+ # Reporting API function to add something to a variable
+ sub api_variable_add
+ {
+ my ($item,$value) = @_;
+
+
+ # If we have a balance, add to it
+ if (defined($variables{$item})) {
+ # If its a float
+ if ($variables{$item}{'type'} eq "float") {
+ $variables{$item}{'value'}->badd($value);
+ } else {
+ wiaflos::server::templating::abort('api_variable_add','Variable type not supported: "'.defined($value) ? $value : ''.'"');
+ }
+ } else {
+ wiaflos::server::templating::abort('api_variable_add','Variable not defined: "'.defined($item) ? $item : ''.'"');
+ }
+
+ # We shouldn't return results
+ return undef;
+ }
+
+
+ # Reporting API function to subtract something to a variable
+ sub api_variable_subtract
+ {
+ my ($item,$value) = @_;
+
+
+ # If we have a balance, subtract from it
+ if (defined($variables{$item})) {
+ # If its a float
+ if ($variables{$item}{'type'} eq "float") {
+ $variables{$item}{'value'}->bsub($value);
+ } else {
+ wiaflos::server::templating::abort('api_variable_subtract','Variable type not supported: "'.defined($value) ? $value : ''.'"');
+ }
+ } else {
+ wiaflos::server::templating::abort('api_variable_subtract','Variable not defined: "'.defined($item) ? $item : ''.'"');
+ }
+ # We shouldn't return results
+ return undef;
+ }
+
+
+ # Reporting API function to get variable
+ sub api_variable_get
+ {
+ my $item = shift;
+
+
+ my $res;
+
+ # If we have a balance, return it
+ if (defined($variables{$item})) {
+ # If its a float
+ if ($variables{$item}{'type'} eq "float") {
+ $res = sprintf('%.2f',$variables{$item}{'value'}->bstr());
+ } else {
+ wiaflos::server::templating::abort('api_variable_get','Variable type not supported: "'.
+ defined($variables{$item}{'type'}) ? $variables{$item}{'type'} : ''.'"');
+ }
+ } else {
+ wiaflos::server::templating::abort('api_variable_get','Variable not defined: "'.defined($item) ? $item : ''.'"');
+ }
+
+ return $res;
+ }
+
+
+ # Reporting API function to get raw variable value
+ sub api_variable_getraw
+ {
+ my $item = shift;
+
+
+ my $res;
+
+ # If we have a balance, return it
+ if (defined($variables{$item})) {
+ $res = $variables{$item}{'value'};
+ } else {
+ wiaflos::server::templating::abort('api_variable_get','Variable not defined: "'.defined($item) ? $item : ''.'"');
+ }
+
+ return $res;
+ }
+
+
+ # Dump contents of parameters on server
+ sub api_format_amount
+ {
+ my ($pamount,$options) = @_;
+
+ my $prefix = "";
+ my $suffix = "";
+
+ # Pull in value
+ my $amount = new Math::BigFloat();
+ $amount->precision(-2);
+ $amount->badd($pamount);
+
+ # Check options
+ if (($options & API_FMT_REVERSE) == API_FMT_REVERSE) {
+ $amount->bmul(-1);
+ }
+
+ if (($options & API_FMT_NEGBRACKET) == API_FMT_NEGBRACKET) {
+ # Check if neg, if it is, use brackets
+ if ($amount->is_neg()) {
+ $prefix = "(";
+ $suffix = ")";
+ $amount->babs();
+ }
+ }
+
+ return $prefix . sprintf('%.2f',$amount->bstr()) . $suffix;
+ }
+
+ # Dump contents of parameters on server
+ sub api_dump
+ {
+ use Data::Dumper;
+ print(STDERR Dumper(\@_));
+ }
+
# Verify Template
if (!defined($detail->{'Template'}) || $detail->{'Template'} eq "") {
setError("Template was not provided");
@@ -525,6 +670,18 @@
'api_account_entries' => \&api_account_entries,
'api_inventory_stock_balances' => \&api_inventory_stock_balances,
+ 'api_variable_new' => \&api_variable_new,
+ 'api_variable_add' => \&api_variable_add,
+ 'api_variable_subtract' => \&api_variable_subtract,
+ 'api_variable_get' => \&api_variable_get,
+ 'api_variable_getraw' => \&api_variable_getraw,
+
+ 'api_format_amount' => \&api_format_amount,
+ 'API_FMT_REVERSE' => API_FMT_REVERSE,
+ 'API_FMT_NEGBRACKET' => API_FMT_NEGBRACKET,
+
+ 'api_dump' => \&api_dump,
+
# Client
'StartDate' => defined($detail->{'StartDate'}) ? $detail->{'StartDate'} : "-",
'EndDate' => defined($detail->{'EndDate'}) ? $detail->{'EndDate'} : "CURRENT",
More information about the wiaflos-devel
mailing list