summaryrefslogtreecommitdiff
path: root/LedgerSMB.pm
blob: 5727edbeb78f503e0bc892b5c2bc467450fd8372 (plain)
  1. =head1 NAME
  2. LedgerSMB The Base class for many LedgerSMB objects, including DBObject.
  3. =head1 SYOPSIS
  4. This module creates a basic request handler with utility functions available
  5. in database objects (LedgerSMB::DBObject)
  6. =head1 METHODS
  7. =item new ()
  8. This method creates a new base request instance.
  9. =item date_to_number (user => $LedgerSMB::User, date => $string);
  10. This function takes the date in the format provided and returns a numeric
  11. string in YYMMDD format. This may be moved to User in the future.
  12. =item debug (file => $path);
  13. This dumps the current object to the file if that is defined and otherwise to
  14. standard output.
  15. =item escape (string => $string);
  16. This function returns the current string escaped using %hexhex notation.
  17. =item unescape (string => $string);
  18. This function returns the $string encoded using %hexhex using ordinary notation.
  19. =item format_amount (user => $LedgerSMB::User::hash, amount => $string, precision => $integer, neg_format => (-|DRCR));
  20. The function takes a monetary amount and formats it according to the user
  21. preferences, the negative format (- or DR/CR). Note that it may move to
  22. LedgerSMB::User at some point in the future.
  23. =item parse_amount (user => $LedgerSMB::User::hash, amount => $variable);
  24. If $amount is a Bigfloat, it is returned as is. If it is a string, it is
  25. parsed according to the user preferences stored in the LedgerSMB::User object.
  26. =item is_blank (name => $string)
  27. This function returns true if $self->{$string} only consists of whitespace
  28. characters or is an empty string.
  29. =item is_run_mode ('(cli|cgi|mod_perl)')
  30. This function returns 1 if the run mode is what is specified. Otherwise
  31. returns 0.
  32. =item num_text_rows (string => $string, cols => $number, max => $number);
  33. This function determines the likely number of rows needed to hold text in a
  34. textbox. It returns either that number or max, which ever is lower.
  35. =item merge ($hashref, keys => @list, index => $number);
  36. This command merges the $hashref into the current object. If keys are
  37. specified, only those keys are used. Otherwise all keys are merged.
  38. If an index is specified, the merged keys are given a form of
  39. "$key" . "_$index", otherwise the key is used on both sides.
  40. =item redirect (msg => $string)
  41. This function redirects to the script and argument set determined by
  42. $self->{callback}, and if this is not set, goes to an info screen and prints
  43. $msg.
  44. =item redo_rows (fields => \@list, count => $integer, [index => $string);
  45. This function is undergoing serious redesign at the moment. If index is
  46. defined, that field is used for ordering the rows. If not, runningnumber is
  47. used. Behavior is not defined when index points to a field containing
  48. non-numbers.
  49. =head1 Copyright (C) 2006, The LedgerSMB core team.
  50. # This work contains copyrighted information from a number of sources all used
  51. # with permission.
  52. #
  53. # This file contains source code included with or based on SQL-Ledger which
  54. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  55. # under the GNU General Public License version 2 or, at your option, any later
  56. # version. For a full list including contact information of contributors,
  57. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  58. #
  59. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  60. # Copyright (C) 2000
  61. #
  62. # Author: DWS Systems Inc.
  63. # Web: http://www.sql-ledger.org
  64. #
  65. # Contributors: Thomas Bayen <bayen@gmx.de>
  66. # Antti Kaihola <akaihola@siba.fi>
  67. # Moritz Bunkus (tex)
  68. # Jim Rawlings <jim@your-dba.com> (DB2)
  69. #======================================================================
  70. =cut
  71. use CGI;
  72. use Math::BigFloat lib => 'GMP';
  73. use LedgerSMB::Sysconfig;
  74. use Data::Dumper;
  75. use strict;
  76. package LedgerSMB;
  77. sub new {
  78. my $type = shift @_;
  79. my $argstr = shift @_;
  80. my $self = {};
  81. $self->{version} = "1.3.0 Alpha 0 Pre";
  82. $self->{dbversion} = "1.2.0";
  83. bless $self, $type;
  84. my $query = ($argstr) ? new CGI($argstr) : new CGI;
  85. my $params = $query->Vars;
  86. $self->merge($params);
  87. $self->{action} =~ s/\W/_/g;
  88. $self->{action} = lc $self->{action};
  89. if ( $self->{path} eq "bin/lynx" ) {
  90. $self->{menubar} = 1;
  91. #menubar will be deprecated, replaced with below
  92. $self->{lynx} = 1;
  93. $self->{path} = "bin/lynx";
  94. }
  95. else {
  96. $self->{path} = "bin/mozilla";
  97. }
  98. if ( ( $self->{script} =~ m#(..|\\|/)# ) ) {
  99. $self->error("Access Denied");
  100. }
  101. $self;
  102. }
  103. sub debug {
  104. my $self = shift @_;
  105. my %args = @_;
  106. my $file = $args{file};
  107. my $d = Data::Dumper->new( [@_] );
  108. $d->Sortkeys(1);
  109. if ($file) {
  110. open( FH, '>', "$file" ) or die $!;
  111. print FH $d->Dump();
  112. close(FH);
  113. }
  114. else {
  115. print "\n";
  116. print $d->Dump();
  117. }
  118. }
  119. sub escape {
  120. my ($self) = @_;
  121. my %args = @_;
  122. my $str = $args{string};
  123. my $regex = qr/([^a-zA-Z0-9_.-])/;
  124. $str =~ s/$regex/sprintf("%%%02x", ord($1))/ge;
  125. $str;
  126. }
  127. sub is_blank {
  128. my $self = shift @_;
  129. my %args = @_;
  130. my $name = $args{name};
  131. my $rc;
  132. if ( $self->{$name} =~ /^\s*$/ ) {
  133. $rc = 1;
  134. }
  135. else {
  136. $rc = 0;
  137. }
  138. $rc;
  139. }
  140. sub is_run_mode {
  141. my $self = shift @_;
  142. my $mode = lc shift @_;
  143. my $rc = 0;
  144. if ( $mode eq 'cgi' && $ENV{GATEWAY_INTERFACE} ) {
  145. $rc = 1;
  146. }
  147. elsif ( $mode eq 'cli' && !( $ENV{GATEWAY_INTERFACE} || $ENV{MOD_PERL} ) ) {
  148. $rc = 1;
  149. }
  150. elsif ( $mode eq 'mod_perl' && $ENV{MOD_PERL} ) {
  151. $rc = 1;
  152. }
  153. $rc;
  154. }
  155. sub num_text_rows {
  156. my $self = shift @_;
  157. my %args = @_;
  158. my $string = $args{string};
  159. my $cols = $args{cols};
  160. my $maxrows = $args{max};
  161. my $rows = 0;
  162. for ( split /\n/, $string ) {
  163. my $line = $_;
  164. while ( length($line) > $cols ) {
  165. my $fragment = substr( $line, 0, $cols + 1 );
  166. my $fragment = s/^(.*)\S*$/$1/;
  167. $line = s/$fragment//;
  168. if ( $line eq $fragment ) { # No word breaks!
  169. $line = "";
  170. }
  171. ++$rows;
  172. }
  173. ++$rows;
  174. }
  175. if ( !defined $maxrows ) {
  176. $maxrows = $rows;
  177. }
  178. return ( $rows > $maxrows ) ? $maxrows : $rows;
  179. }
  180. sub redirect {
  181. my $self = shift @_;
  182. my %args = @_;
  183. my $msg = $args{msg};
  184. if ( $self->{callback} || !$msg ) {
  185. main::redirect();
  186. }
  187. else {
  188. $self->info($msg);
  189. }
  190. }
  191. # TODO: Either we should have an amount class with formats and such attached
  192. # Or maybe we should move this into the user class...
  193. sub format_amount {
  194. # Based on SQL-Ledger's Form::format_amount
  195. my $self = shift @_;
  196. my %args = @_;
  197. my $myconfig = $args{user};
  198. my $amount = $args{amount};
  199. my $places = $args{precision};
  200. my $dash = $args{neg_format};
  201. my $negative;
  202. if ($amount) {
  203. $amount = $self->parse_amount( $myconfig, $amount );
  204. $negative = ( $amount < 0 );
  205. $amount =~ s/-//;
  206. }
  207. if ( $places =~ /\d+/ ) {
  208. #$places = 4 if $places == 2;
  209. $amount = $self->round_amount( $amount, $places );
  210. }
  211. # is the amount negative
  212. # Parse $myconfig->{numberformat}
  213. my ( $ts, $ds ) = ( $1, $2 );
  214. if ($amount) {
  215. if ( $myconfig->{numberformat} ) {
  216. my ( $whole, $dec ) = split /\./, "$amount";
  217. $amount = join '', reverse split //, $whole;
  218. if ($places) {
  219. $dec .= "0" x $places;
  220. $dec = substr( $dec, 0, $places );
  221. }
  222. if ( $myconfig->{numberformat} eq '1,000.00' ) {
  223. $amount =~ s/\d{3,}?/$&,/g;
  224. $amount =~ s/,$//;
  225. $amount = join '', reverse split //, $amount;
  226. $amount .= "\.$dec" if ( $dec ne "" );
  227. }
  228. if ( $myconfig->{numberformat} eq '1 000.00' ) {
  229. $amount =~ s/\d{3,}?/$& /g;
  230. $amount =~ s/\s$//;
  231. $amount = join '', reverse split //, $amount;
  232. $amount .= "\.$dec" if ( $dec ne "" );
  233. }
  234. if ( $myconfig->{numberformat} eq "1'000.00" ) {
  235. $amount =~ s/\d{3,}?/$&'/g;
  236. $amount =~ s/'$//;
  237. $amount = join '', reverse split //, $amount;
  238. $amount .= "\.$dec" if ( $dec ne "" );
  239. }
  240. if ( $myconfig->{numberformat} eq '1.000,00' ) {
  241. $amount =~ s/\d{3,}?/$&./g;
  242. $amount =~ s/\.$//;
  243. $amount = join '', reverse split //, $amount;
  244. $amount .= ",$dec" if ( $dec ne "" );
  245. }
  246. if ( $myconfig->{numberformat} eq '1000,00' ) {
  247. $amount = "$whole";
  248. $amount .= ",$dec" if ( $dec ne "" );
  249. }
  250. if ( $myconfig->{numberformat} eq '1000.00' ) {
  251. $amount = "$whole";
  252. $amount .= ".$dec" if ( $dec ne "" );
  253. }
  254. if ( $dash =~ /-/ ) {
  255. $amount = ($negative) ? "($amount)" : "$amount";
  256. }
  257. elsif ( $dash =~ /DRCR/ ) {
  258. $amount = ($negative) ? "$amount DR" : "$amount CR";
  259. }
  260. else {
  261. $amount = ($negative) ? "-$amount" : "$amount";
  262. }
  263. }
  264. }
  265. else {
  266. if ( $dash eq "0" && $places ) {
  267. if ( $myconfig->{numberformat} eq '1.000,00' ) {
  268. $amount = "0" . "," . "0" x $places;
  269. }
  270. else {
  271. $amount = "0" . "." . "0" x $places;
  272. }
  273. }
  274. else {
  275. $amount = ( $dash ne "" ) ? "$dash" : "";
  276. }
  277. }
  278. $amount;
  279. }
  280. # This should probably go to the User object too.
  281. sub parse_amount {
  282. my $self = shift @_;
  283. my %args = @_;
  284. my $myconfig = $args{user};
  285. my $amount = $args{amount};
  286. if ( $amount eq '' or $amount == undef ) {
  287. return 0;
  288. }
  289. if ( UNIVERSAL::isa( $amount, 'Math::BigFloat' ) )
  290. { # Amount may not be an object
  291. return $amount;
  292. }
  293. my $numberformat = $myconfig->{numberformat};
  294. if ( ( $numberformat eq '1.000,00' )
  295. || ( $numberformat eq '1000,00' ) )
  296. {
  297. $amount =~ s/\.//g;
  298. $amount =~ s/,/./;
  299. }
  300. if ( $numberformat eq '1 000.00' ) {
  301. $amount =~ s/\s//g;
  302. }
  303. if ( $numberformat eq "1'000.00" ) {
  304. $amount =~ s/'//g;
  305. }
  306. $amount =~ s/,//g;
  307. if ( $amount =~ s/\((\d*\.?\d*)\)/$1/ ) {
  308. $amount = $1 * -1;
  309. }
  310. if ( $amount =~ s/(\d*\.?\d*)\s?DR/$1/ ) {
  311. $amount = $1 * -1;
  312. }
  313. $amount =~ s/\s?CR//;
  314. $amount = new Math::BigFloat($amount);
  315. return ( $amount * 1 );
  316. }
  317. sub round_amount {
  318. my ( $self, $amount, $places ) = @_;
  319. # These rounding rules follow from the previous implementation.
  320. # They should be changed to allow different rules for different accounts.
  321. Math::BigFloat->round_mode('+inf') if $amount >= 0;
  322. Math::BigFloat->round_mode('-inf') if $amount < 0;
  323. $amount = Math::BigFloat->new($amount)->ffround( -$places ) if $places >= 0;
  324. $amount = Math::BigFloat->new($amount)->ffround( -( $places - 1 ) )
  325. if $places < 0;
  326. return $amount;
  327. }
  328. sub call_procedure {
  329. my $self = shift @_;
  330. my %args = @_;
  331. my $procname = $args{procname};
  332. my @args = @{ $args{args} };
  333. my $argstr = "";
  334. my @results;
  335. for ( 1 .. scalar @args ) {
  336. $argstr .= "?, ";
  337. }
  338. $argstr =~ s/\, $//;
  339. my $query = "SELECT * FROM $procname()";
  340. $query =~ s/\(\)/($argstr)/;
  341. my $sth = $self->{dbh}->prepare($query);
  342. $sth->execute(@args);
  343. while ( my $ref = $sth->fetchrow_hashref('NAME_lc') ) {
  344. push @results, $ref;
  345. }
  346. @results;
  347. }
  348. # This should probably be moved to User too...
  349. sub date_to_number {
  350. #based on SQL-Ledger's Form::datetonum
  351. my $self = shift @_;
  352. my %args = @_;
  353. my $myconfig = $args{user};
  354. my $date = $args{date};
  355. my ( $yy, $mm, $dd );
  356. if ( $date && $date =~ /\D/ ) {
  357. if ( $myconfig->{dateformat} =~ /^yy/ ) {
  358. ( $yy, $mm, $dd ) = split /\D/, $date;
  359. }
  360. if ( $myconfig->{dateformat} =~ /^mm/ ) {
  361. ( $mm, $dd, $yy ) = split /\D/, $date;
  362. }
  363. if ( $myconfig->{dateformat} =~ /^dd/ ) {
  364. ( $dd, $mm, $yy ) = split /\D/, $date;
  365. }
  366. $dd *= 1;
  367. $mm *= 1;
  368. $yy += 2000 if length $yy == 2;
  369. $dd = substr( "0$dd", -2 );
  370. $mm = substr( "0$mm", -2 );
  371. $date = "$yy$mm$dd";
  372. }
  373. $date;
  374. }
  375. # Database routines used throughout
  376. sub db_init {
  377. my $self = shift @_;
  378. my %args = @_;
  379. my $myconfig = $args{user};
  380. my $dbh = DBI->connect(
  381. $myconfig->{dbconnect}, $myconfig->{dbuser},
  382. $myconfig->{dbpasswd}, { AutoCommit => 0 }
  383. ) or $self->dberror;
  384. $dbh->{pg_server_prepare} = 0;
  385. if ( $myconfig->{dboptions} ) {
  386. $dbh->do( $myconfig->{dboptions} );
  387. }
  388. my $query = "SELECT t.extends,
  389. coalesce (t.table_name, 'custom_' || extends)
  390. || ':' || f.field_name as field_def
  391. FROM custom_table_catalog t
  392. JOIN custom_field_catalog f USING (table_id)";
  393. my $sth = $self->{dbh}->prepare($query);
  394. $sth->execute;
  395. my $ref;
  396. while ( $ref = $sth->fetchrow_hashref('NAME_lc') ) {
  397. push @{ $self->{custom_db_fields}{ $ref->{extends} } },
  398. $ref->{field_def};
  399. }
  400. }
  401. sub redo_rows {
  402. my $self = shift @_;
  403. my %args = @_;
  404. my @flds = @{ $args{fields} };
  405. my $count = $args{count};
  406. my $index = ( $args{index} ) ? $args{index} : 'runningnumber';
  407. my @rows;
  408. my $i; # incriment counter use only
  409. for $i ( 1 .. $count ) {
  410. my $temphash = { _inc => $i };
  411. for my $fld (@flds) {
  412. $temphash->{$fld} = $self->{ "$fld" . "_$i" };
  413. }
  414. push @rows, $temphash;
  415. }
  416. $i = 1;
  417. for my $row ( sort { $a->{index} <=> $b->{index} } @rows ) {
  418. for my $fld (@flds) {
  419. $self->{ "$fld" . "_$i" } = $row->{$fld};
  420. }
  421. ++$i;
  422. }
  423. }
  424. sub merge {
  425. my ( $self, $src ) = @_;
  426. for my $arg ( $self, $src ) {
  427. shift;
  428. }
  429. my %args = @_;
  430. my @keys = @{ $args{keys} };
  431. my $index = $args{index};
  432. if ( !scalar @keys ) {
  433. @keys = keys %{$src};
  434. }
  435. for my $arg ( keys %$src ) {
  436. my $dst_arg;
  437. if ($index) {
  438. $dst_arg = $arg . "_$index";
  439. }
  440. else {
  441. $dst_arg = $arg;
  442. }
  443. $self->{$dst_arg} = $src->{$arg};
  444. }
  445. }
  446. 1;