summaryrefslogtreecommitdiff
path: root/LedgerSMB/Form.pm
blob: 76f4877fbe0295b80e12ca13574f10764e923d1d (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. # http://www.ledgersmb.org/
  5. #
  6. # Copyright (C) 2006
  7. # This work contains copyrighted information from a number of sources all used
  8. # with permission.
  9. #
  10. # This file contains source code included with or based on SQL-Ledger which
  11. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  12. # under the GNU General Public License version 2 or, at your option, any later
  13. # version. For a full list including contact information of contributors,
  14. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  15. #
  16. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  17. # Copyright (C) 2000
  18. #
  19. # Author: DWS Systems Inc.
  20. # Web: http://www.sql-ledger.org
  21. #
  22. # Contributors: Thomas Bayen <bayen@gmx.de>
  23. # Antti Kaihola <akaihola@siba.fi>
  24. # Moritz Bunkus (tex)
  25. # Jim Rawlings <jim@your-dba.com> (DB2)
  26. #======================================================================
  27. #
  28. # This file has undergone whitespace cleanup.
  29. #
  30. #======================================================================
  31. #
  32. # main package
  33. #
  34. #======================================================================
  35. use Math::BigFloat lib => 'GMP';
  36. use LedgerSMB::Sysconfig;
  37. package Form;
  38. sub new {
  39. my $type = shift;
  40. my $argstr = shift;
  41. read( STDIN, $_, $ENV{CONTENT_LENGTH} );
  42. if ($argstr) {
  43. $_ = $argstr;
  44. }
  45. elsif ( $ENV{QUERY_STRING} ) {
  46. $_ = $ENV{QUERY_STRING};
  47. }
  48. elsif ( $ARGV[0] ) {
  49. $_ = $ARGV[0];
  50. }
  51. my $self = {};
  52. %$self = split /[&=]/;
  53. for ( keys %$self ) { $self->{$_} = unescape( "", $self->{$_} ) }
  54. if ( substr( $self->{action}, 0, 1 ) !~ /( |\.)/ ) {
  55. $self->{action} = lc $self->{action};
  56. $self->{action} =~ s/( |-|,|\#|\/|\.$)/_/g;
  57. $self->{nextsub} = lc $self->{nextsub};
  58. $self->{nextsub} =~ s/( |-|,|\#|\/|\.$)/_/g;
  59. }
  60. $self->{menubar} = 1 if $self->{path} =~ /lynx/i;
  61. #menubar will be deprecated, replaced with below
  62. $self->{lynx} = 1 if $self->{path} =~ /lynx/i;
  63. $self->{version} = "1.3.0 Alpha 0 Pre";
  64. $self->{dbversion} = "1.2.0";
  65. bless $self, $type;
  66. if ( $self->{path} eq "bin/lynx" ) {
  67. $self->{menubar} = 1;
  68. #menubar will be deprecated, replaced with below
  69. $self->{lynx} = 1;
  70. $self->{path} = "bin/lynx";
  71. }
  72. else {
  73. $self->{path} = "bin/mozilla";
  74. }
  75. if ( ( $self->{script} =~ m#(\.\.|\\|/)# ) ) {
  76. $self->error("Access Denied");
  77. }
  78. if ( ( $self->{action} =~ /:/ ) || ( $self->{nextsub} =~ /:/ ) ) {
  79. $self->error("Access Denied");
  80. }
  81. $self;
  82. }
  83. sub debug {
  84. my ( $self, $file ) = @_;
  85. if ($file) {
  86. open( FH, '>', "$file" ) or die $!;
  87. for ( sort keys %$self ) { print FH "$_ = $self->{$_}\n" }
  88. close(FH);
  89. }
  90. else {
  91. print "\n";
  92. for ( sort keys %$self ) { print "$_ = $self->{$_}\n" }
  93. }
  94. }
  95. sub escape {
  96. my ( $self, $str, $beenthere ) = @_;
  97. # for Apache 2 we escape strings twice
  98. if ( ( $ENV{SERVER_SIGNATURE} =~ /Apache\/2\.(\d+)\.(\d+)/ )
  99. && !$beenthere )
  100. {
  101. $str = $self->escape( $str, 1 ) if $1 == 0 && $2 < 44;
  102. }
  103. $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
  104. $str;
  105. }
  106. sub unescape {
  107. my ( $self, $str ) = @_;
  108. $str =~ tr/+/ /;
  109. $str =~ s/\\$//;
  110. $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
  111. $str =~ s/\r?\n/\n/g;
  112. $str;
  113. }
  114. sub quote {
  115. my ( $self, $str ) = @_;
  116. if ( $str && !ref($str) ) {
  117. $str =~ s/"/&quot;/g;
  118. }
  119. $str;
  120. }
  121. sub format_date {
  122. # takes an iso date in, and converts it to the date for printing
  123. my ( $self, $date ) = @_;
  124. my $datestring;
  125. if ( $date =~ /^\d{4}\D/ ) { # is an ISO date
  126. $datestring = $self->{db_dateformat};
  127. my ( $yyyy, $mm, $dd ) = split( /\W/, $date );
  128. $datestring =~ s/y+/$yyyy/;
  129. $datestring =~ s/mm/$mm/;
  130. $datestring =~ s/dd/$dd/;
  131. }
  132. else { # return date
  133. $datestring = $date;
  134. }
  135. $datestring;
  136. }
  137. sub unquote {
  138. my ( $self, $str ) = @_;
  139. if ( $str && !ref($str) ) {
  140. $str =~ s/&quot;/"/g;
  141. }
  142. $str;
  143. }
  144. sub hide_form {
  145. my $self = shift;
  146. if (@_) {
  147. for (@_) {
  148. print qq|<input type="hidden" name="$_" value="|
  149. . $self->quote( $self->{$_} )
  150. . qq|" />\n|;
  151. }
  152. }
  153. else {
  154. delete $self->{header};
  155. for ( sort keys %$self ) {
  156. print qq|<input type="hidden" name="$_" value="|
  157. . $self->quote( $self->{$_} )
  158. . qq|" />\n|;
  159. }
  160. }
  161. }
  162. sub error {
  163. my ( $self, $msg ) = @_;
  164. if ( $ENV{GATEWAY_INTERFACE} ) {
  165. $self->{msg} = $msg;
  166. $self->{format} = "html";
  167. $self->format_string('msg');
  168. delete $self->{pre};
  169. if ( !$self->{header} ) {
  170. $self->header;
  171. }
  172. print
  173. qq|<body><h2 class="error">Error!</h2> <p><b>$self->{msg}</b></body>|;
  174. exit;
  175. }
  176. else {
  177. if ( $ENV{error_function} ) {
  178. &{ $ENV{error_function} }($msg);
  179. }
  180. else {
  181. die "Error: $msg\n";
  182. }
  183. }
  184. }
  185. sub info {
  186. my ( $self, $msg ) = @_;
  187. if ( $ENV{GATEWAY_INTERFACE} ) {
  188. $msg =~ s/\n/<br>/g;
  189. delete $self->{pre};
  190. if ( !$self->{header} ) {
  191. $self->header;
  192. print qq| <body>|;
  193. $self->{header} = 1;
  194. }
  195. print "<b>$msg</b>";
  196. }
  197. else {
  198. if ( $ENV{info_function} ) {
  199. &{ $ENV{info_function} }($msg);
  200. }
  201. else {
  202. print "$msg\n";
  203. }
  204. }
  205. }
  206. sub numtextrows {
  207. my ( $self, $str, $cols, $maxrows ) = @_;
  208. my $rows = 0;
  209. for ( split /\n/, $str ) {
  210. $rows += int( ( (length) - 2 ) / $cols ) + 1;
  211. }
  212. $maxrows = $rows unless defined $maxrows;
  213. return ( $rows > $maxrows ) ? $maxrows : $rows;
  214. }
  215. sub dberror {
  216. my ( $self, $msg ) = @_;
  217. $self->error( "$msg\n" . $DBI::errstr );
  218. }
  219. sub isblank {
  220. my ( $self, $name, $msg ) = @_;
  221. $self->error($msg) if $self->{$name} =~ /^\s*$/;
  222. }
  223. sub header {
  224. my ( $self, $init, $headeradd ) = @_;
  225. return if $self->{header};
  226. my ( $stylesheet, $favicon, $charset );
  227. if ( $ENV{GATEWAY_INTERFACE} ) {
  228. if ( $self->{stylesheet} && ( -f "css/$self->{stylesheet}" ) ) {
  229. $stylesheet =
  230. qq|<link rel="stylesheet" href="css/$self->{stylesheet}" type="text/css" title="LedgerSMB stylesheet" />\n|;
  231. }
  232. if ( $self->{charset} ) {
  233. $charset =
  234. qq|<meta http-equiv="content-type" content="text/html; charset=$self->{charset}" />\n|;
  235. }
  236. $self->{titlebar} =
  237. ( $self->{title} )
  238. ? "$self->{title} - $self->{titlebar}"
  239. : $self->{titlebar};
  240. print qq|Content-Type: text/html; charset=utf-8\n\n
  241. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  242. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  243. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  244. <head>
  245. <title>$self->{titlebar}</title>
  246. <meta http-equiv="Pragma" content="no-cache" />
  247. <meta http-equiv="Expires" content="-1" />
  248. <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
  249. $stylesheet
  250. $charset
  251. <meta name="robots" content="noindex,nofollow" />
  252. $headeradd
  253. </head>
  254. $self->{pre} \n|;
  255. }
  256. $self->{header} = 1;
  257. }
  258. sub redirect {
  259. my ( $self, $msg ) = @_;
  260. use List::Util qw(first);
  261. if ( $self->{callback} || !$msg ) {
  262. main::redirect();
  263. }
  264. else {
  265. $self->info($msg);
  266. }
  267. }
  268. sub sort_columns {
  269. my ( $self, @columns ) = @_;
  270. if ( $self->{sort} ) {
  271. if (@columns) {
  272. @columns = grep !/^$self->{sort}$/, @columns;
  273. splice @columns, 0, 0, $self->{sort};
  274. }
  275. }
  276. @columns;
  277. }
  278. sub sort_order {
  279. my ( $self, $columns, $ordinal ) = @_;
  280. # setup direction
  281. if ( $self->{direction} ) {
  282. if ( $self->{sort} eq $self->{oldsort} ) {
  283. if ( $self->{direction} eq 'ASC' ) {
  284. $self->{direction} = "DESC";
  285. }
  286. else {
  287. $self->{direction} = "ASC";
  288. }
  289. }
  290. }
  291. else {
  292. $self->{direction} = "ASC";
  293. }
  294. $self->{oldsort} = $self->{sort};
  295. my @a = $self->sort_columns( @{$columns} );
  296. if (%$ordinal) {
  297. $a[0] =
  298. ( $ordinal->{ $a[$_] } )
  299. ? "$ordinal->{$a[0]} $self->{direction}"
  300. : "$a[0] $self->{direction}";
  301. for ( 1 .. $#a ) {
  302. $a[$_] = $ordinal->{ $a[$_] } if $ordinal->{ $a[$_] };
  303. }
  304. }
  305. else {
  306. $a[0] .= " $self->{direction}";
  307. }
  308. $sortorder = join ',', @a;
  309. $sortorder;
  310. }
  311. sub format_amount {
  312. my ( $self, $myconfig, $amount, $places, $dash ) = @_;
  313. my $negative;
  314. if ($amount) {
  315. $amount = $self->parse_amount( $myconfig, $amount );
  316. $negative = ( $amount < 0 );
  317. $amount =~ s/-//;
  318. }
  319. if ( $places =~ /\d+/ ) {
  320. #$places = 4 if $places == 2;
  321. $amount = $self->round_amount( $amount, $places );
  322. }
  323. # is the amount negative
  324. # Parse $myconfig->{numberformat}
  325. my ( $ts, $ds ) = ( $1, $2 );
  326. if ($amount) {
  327. if ( $myconfig->{numberformat} ) {
  328. my ( $whole, $dec ) = split /\./, "$amount";
  329. $amount = join '', reverse split //, $whole;
  330. if ($places) {
  331. $dec .= "0" x $places;
  332. $dec = substr( $dec, 0, $places );
  333. }
  334. if ( $myconfig->{numberformat} eq '1,000.00' ) {
  335. $amount =~ s/\d{3,}?/$&,/g;
  336. $amount =~ s/,$//;
  337. $amount = join '', reverse split //, $amount;
  338. $amount .= "\.$dec" if ( $dec ne "" );
  339. }
  340. if ( $myconfig->{numberformat} eq '1 000.00' ) {
  341. $amount =~ s/\d{3,}?/$& /g;
  342. $amount =~ s/\s$//;
  343. $amount = join '', reverse split //, $amount;
  344. $amount .= "\.$dec" if ( $dec ne "" );
  345. }
  346. if ( $myconfig->{numberformat} eq "1'000.00" ) {
  347. $amount =~ s/\d{3,}?/$&'/g;
  348. $amount =~ s/'$//;
  349. $amount = join '', reverse split //, $amount;
  350. $amount .= "\.$dec" if ( $dec ne "" );
  351. }
  352. if ( $myconfig->{numberformat} eq '1.000,00' ) {
  353. $amount =~ s/\d{3,}?/$&./g;
  354. $amount =~ s/\.$//;
  355. $amount = join '', reverse split //, $amount;
  356. $amount .= ",$dec" if ( $dec ne "" );
  357. }
  358. if ( $myconfig->{numberformat} eq '1000,00' ) {
  359. $amount = "$whole";
  360. $amount .= ",$dec" if ( $dec ne "" );
  361. }
  362. if ( $myconfig->{numberformat} eq '1000.00' ) {
  363. $amount = "$whole";
  364. $amount .= ".$dec" if ( $dec ne "" );
  365. }
  366. if ( $dash =~ /-/ ) {
  367. $amount = ($negative) ? "($amount)" : "$amount";
  368. }
  369. elsif ( $dash =~ /DRCR/ ) {
  370. $amount = ($negative) ? "$amount DR" : "$amount CR";
  371. }
  372. else {
  373. $amount = ($negative) ? "-$amount" : "$amount";
  374. }
  375. }
  376. }
  377. else {
  378. if ( $dash eq "0" && $places ) {
  379. if ( $myconfig->{numberformat} eq '1.000,00' ) {
  380. $amount = "0" . "," . "0" x $places;
  381. }
  382. else {
  383. $amount = "0" . "." . "0" x $places;
  384. }
  385. }
  386. else {
  387. $amount = ( $dash ne "" ) ? "$dash" : "";
  388. }
  389. }
  390. $amount;
  391. }
  392. sub parse_amount {
  393. my ( $self, $myconfig, $amount ) = @_;
  394. if ( $amount eq '' or $amount == undef ) {
  395. return 0;
  396. }
  397. if ( UNIVERSAL::isa( $amount, 'Math::BigFloat' ) )
  398. { # Amount may not be an object
  399. return $amount;
  400. }
  401. my $numberformat = $myconfig->{numberformat};
  402. if ( ( $numberformat eq '1.000,00' )
  403. || ( $numberformat eq '1000,00' ) )
  404. {
  405. $amount =~ s/\.//g;
  406. $amount =~ s/,/./;
  407. }
  408. if ( $numberformat eq '1 000.00' ) {
  409. $amount =~ s/\s//g;
  410. }
  411. if ( $numberformat eq "1'000.00" ) {
  412. $amount =~ s/'//g;
  413. }
  414. $amount =~ s/,//g;
  415. if ( $amount =~ s/\((\d*\.?\d*)\)/$1/ ) {
  416. $amount = $1 * -1;
  417. }
  418. if ( $amount =~ s/(\d*\.?\d*)\s?DR/$1/ ) {
  419. $amount = $1 * -1;
  420. }
  421. $amount =~ s/\s?CR//;
  422. $amount = new Math::BigFloat($amount);
  423. return ( $amount * 1 );
  424. }
  425. sub round_amount {
  426. my ( $self, $amount, $places ) = @_;
  427. # These rounding rules follow from the previous implementation.
  428. # They should be changed to allow different rules for different accounts.
  429. Math::BigFloat->round_mode('+inf') if $amount >= 0;
  430. Math::BigFloat->round_mode('-inf') if $amount < 0;
  431. $amount = Math::BigFloat->new($amount)->ffround( -$places ) if $places >= 0;
  432. $amount = Math::BigFloat->new($amount)->ffround( -( $places - 1 ) )
  433. if $places < 0;
  434. return $amount;
  435. }
  436. sub callproc {
  437. my $procname = shift @_;
  438. my $argstr = "";
  439. my @results;
  440. for ( 1 .. $#_ ) {
  441. $argstr .= "?, ";
  442. }
  443. $argstr =~ s/\, $//;
  444. $query = "SELECT * FROM $procname";
  445. $query =~ s/\(\)/$argstr/;
  446. my $sth = $self->{dbh}->prepare($query);
  447. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  448. push @results, $ref;
  449. }
  450. @results;
  451. }
  452. sub get_my_emp_num {
  453. my ( $self, $myconfig, $form ) = @_;
  454. %myconfig = %{$myconfig};
  455. my $dbh = $form->{dbh};
  456. # we got a connection, check the version
  457. my $query = qq|
  458. SELECT employeenumber FROM employees
  459. WHERE login = ?|;
  460. my $sth = $dbh->prepare($query);
  461. $sth->execute( $form->{login} ) || $form->dberror($query);
  462. $sth->execute;
  463. my ($id) = $sth->fetchrow_array;
  464. $sth->finish;
  465. $form->{'emp_num'} = $id;
  466. }
  467. sub parse_template {
  468. my ( $self, $myconfig ) = @_;
  469. my ( $chars_per_line, $lines_on_first_page, $lines_on_second_page ) =
  470. ( 0, 0, 0 );
  471. my ( $current_page, $current_line ) = ( 1, 1 );
  472. print STDERR "Using deprecated Form::parse_template function\n";
  473. my $pagebreak = "";
  474. my $sum = 0;
  475. my $subdir = "";
  476. my $err = "";
  477. my %include = ();
  478. my $ok;
  479. $self->{images} = "${LedgerSMB::Sysconfig::images}/$self->{templates}";
  480. if ( $self->{language_code} ) {
  481. if ( $self->{language_code} =~ /(\.\.|\/|\*)/ ) {
  482. $self->error("Invalid Language Code");
  483. }
  484. if ( -f "$self->{templates}/$self->{language_code}/$self->{IN}" ) {
  485. open( IN, '<',
  486. "$self->{templates}/$self->{language_code}/$self->{IN}" )
  487. or $self->error("$self->{IN} : $!");
  488. }
  489. else {
  490. open( IN, '<', "$self->{templates}/$self->{IN}" )
  491. or $self->error("$self->{IN} : $!");
  492. }
  493. }
  494. else {
  495. open( IN, '<', "$self->{templates}/$self->{IN}" )
  496. or $self->error("$self->{IN} : $!");
  497. }
  498. @_ = <IN>;
  499. close(IN);
  500. $self->{copies} = 1 if ( ( $self->{copies} *= 1 ) <= 0 );
  501. # OUT is used for the media, screen, printer, email
  502. # for postscript we store a copy in a temporary file
  503. my $fileid = time;
  504. my $tmpfile = $self->{IN};
  505. $tmpfile =~ s/\./_$self->{fileid}./ if $self->{fileid};
  506. $self->{tmpfile} =
  507. "${LedgerSMB::Sysconfig::userspath}/${fileid}_${tmpfile}";
  508. my %temphash;
  509. if ( $self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email' ) {
  510. $temphash{out} = $self->{OUT};
  511. $self->{OUT} = "$self->{tmpfile}";
  512. $temphash{printmode} = $self->{printmode};
  513. $self->{printmode} = '>';
  514. }
  515. if ( $self->{OUT} ) {
  516. open( OUT, $self->{printmode}, "$self->{OUT}" )
  517. or $self->error("$self->{OUT} : $!");
  518. }
  519. else {
  520. open( OUT, ">-" ) or $self->error("STDOUT : $!");
  521. $self->header;
  522. }
  523. # first we generate a tmpfile
  524. # read file and replace <?lsmb variable ?>
  525. while ( $_ = shift ) {
  526. $par = "";
  527. $var = $_;
  528. # detect pagebreak block and its parameters
  529. if (/<\?lsmb pagebreak ([0-9]+) ([0-9]+) ([0-9]+) \?>/) {
  530. $chars_per_line = $1;
  531. $lines_on_first_page = $2;
  532. $lines_on_second_page = $3;
  533. while ( $_ = shift ) {
  534. last if (/<\?lsmb end pagebreak \?>/);
  535. $pagebreak .= $_;
  536. }
  537. }
  538. if (/<\?lsmb foreach /) {
  539. # this one we need for the count
  540. chomp $var;
  541. $var =~ s/.*?<\?lsmb foreach (.+?) \?>/$1/;
  542. while ( $_ = shift ) {
  543. last if (/<\?lsmb end $var \?>/);
  544. # store line in $par
  545. $par .= $_;
  546. }
  547. # display contents of $self->{number}[] array
  548. for $i ( 0 .. $#{ $self->{$var} } ) {
  549. if ( $var =~ /^(part|service)$/ ) {
  550. next if $self->{$var}[$i] eq 'NULL';
  551. }
  552. # Try to detect whether a manual page break is necessary
  553. # but only if there was a <?lsmb pagebreak ... ?> block before
  554. if ( $var eq 'number' || $var eq 'part' || $var eq 'service' ) {
  555. if ( $chars_per_line && defined $self->{$var} ) {
  556. my $line;
  557. my $lines = 0;
  558. my @d = qw(description);
  559. push @d, "itemnotes" if $self->{countitemnotes};
  560. foreach my $item (@d) {
  561. if ( $self->{$item}[$i] ) {
  562. foreach $line ( split /\r?\n/,
  563. $self->{$item}[$i] )
  564. {
  565. $lines++;
  566. $lines +=
  567. int( length($line) / $chars_per_line );
  568. }
  569. }
  570. }
  571. my $lpp;
  572. if ( $current_page == 1 ) {
  573. $lpp = $lines_on_first_page;
  574. }
  575. else {
  576. $lpp = $lines_on_second_page;
  577. }
  578. # Yes we need a manual page break
  579. if ( ( $current_line + $lines ) > $lpp ) {
  580. my $pb = $pagebreak;
  581. # replace the special variables <?lsmb sumcarriedforward ?>
  582. # and <?lsmb lastpage ?>
  583. my $psum =
  584. $self->format_amount( $myconfig, $sum, 2 );
  585. $pb =~ s/<\?lsmb sumcarriedforward \?>/$psum/g;
  586. $pb =~ s/<\?lsmb lastpage \?>/$current_page/g;
  587. # only "normal" variables are supported here
  588. # (no <?lsmb if, no <?lsmb foreach, no <?lsmb include)
  589. $pb =~ s/<\?lsmb (.+?) \?>/$self->{$1}/g;
  590. # page break block is ready to rock
  591. print( OUT $pb );
  592. $current_page++;
  593. $current_line = 1;
  594. $lines = 0;
  595. }
  596. $current_line += $lines;
  597. }
  598. $sum +=
  599. $self->parse_amount( $myconfig, $self->{linetotal}[$i] );
  600. }
  601. # don't parse par, we need it for each line
  602. print OUT $self->format_line( $par, $i );
  603. }
  604. next;
  605. }
  606. # if not comes before if!
  607. if (/<\?lsmb if not /) {
  608. # check if it is not set and display
  609. chop;
  610. s/.*?<\?lsmb if not (.+?) \?>/$1/;
  611. if ( !$self->{$_} ) {
  612. while ( $_ = shift ) {
  613. last if (/<\?lsmb end /);
  614. # store line in $par
  615. $par .= $_;
  616. }
  617. $_ = $par;
  618. }
  619. else {
  620. while ( $_ = shift ) {
  621. last if (/<\?lsmb end /);
  622. }
  623. next;
  624. }
  625. }
  626. if (/<\?lsmb if /) {
  627. # check if it is set and display
  628. chop;
  629. s/.*?<\?lsmb if (.+?) \?>/$1/;
  630. if (/\s/) {
  631. @a = split;
  632. $ok = eval "$self->{$a[0]} $a[1] $a[2]";
  633. }
  634. else {
  635. $ok = $self->{$_};
  636. }
  637. if ($ok) {
  638. while ( $_ = shift ) {
  639. last if (/<\?lsmb end /);
  640. # store line in $par
  641. $par .= $_;
  642. }
  643. $_ = $par;
  644. }
  645. else {
  646. while ( $_ = shift ) {
  647. last if (/<\?lsmb end /);
  648. }
  649. next;
  650. }
  651. }
  652. # check for <?lsmb include filename ?>
  653. if (/<\?lsmb include /) {
  654. # get the filename
  655. chomp $var;
  656. $var =~ s/.*?<\?lsmb include (.+?) \?>/$1/;
  657. # remove / .. for security reasons
  658. $var =~ s/(\/|\.\.)//g;
  659. # assume loop after 10 includes of the same file
  660. next if ( $include{$var} > 10 );
  661. unless (
  662. open( INC, '<', "$self->{templates}/$self->{language_code}/$var"
  663. )
  664. )
  665. {
  666. $err = $!;
  667. $self->cleanup;
  668. $self->error(
  669. "$self->{templates}/$self->{language_code}/$var : $err");
  670. }
  671. unshift( @_, <INC> );
  672. close(INC);
  673. $include{$var}++;
  674. next;
  675. }
  676. print OUT $self->format_line($_);
  677. }
  678. close(OUT);
  679. delete $self->{countitemnotes};
  680. # Convert the tex file to postscript
  681. if ( $self->{format} =~ /(postscript|pdf)/ ) {
  682. use Cwd;
  683. $self->{cwd} = cwd();
  684. $self->{tmpdir} = "$self->{cwd}/${LedgerSMB::Sysconfig::userspath}";
  685. $self->{tmpdir} = "${LedgerSMB::Sysconfig::userspath}"
  686. if ${LedgerSMB::Sysconfig::userspath} =~ /^\//;
  687. unless ( chdir("${LedgerSMB::Sysconfig::userspath}") ) {
  688. $err = $!;
  689. $self->cleanup;
  690. $self->error("chdir : $err");
  691. }
  692. $self->{tmpfile} =~ s/${LedgerSMB::Sysconfig::userspath}\///g;
  693. $self->{errfile} = $self->{tmpfile};
  694. $self->{errfile} =~ s/tex$/err/;
  695. my $r = 1;
  696. if ( $self->{format} eq 'postscript' ) {
  697. system(
  698. "latex --interaction=nonstopmode $self->{tmpfile} > $self->{errfile}"
  699. );
  700. while ( $self->rerun_latex ) {
  701. system(
  702. "latex --interaction=nonstopmode $self->{tmpfile} > $self->{errfile}"
  703. );
  704. last if ++$r > 4;
  705. }
  706. $self->{tmpfile} =~ s/tex$/dvi/;
  707. $self->error( $self->cleanup ) if !( -f $self->{tmpfile} );
  708. system("dvips $self->{tmpfile} -o -q");
  709. $self->error( $self->cleanup . "dvips : $!" ) if ($?);
  710. $self->{tmpfile} =~ s/dvi$/ps/;
  711. }
  712. if ( $self->{format} eq 'pdf' ) {
  713. system(
  714. "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{errfile}"
  715. );
  716. while ( $self->rerun_latex ) {
  717. system(
  718. "pdflatex --interaction=nonstopmode $self->{tmpfile} > $self->{errfile}"
  719. );
  720. last if ++$r > 4;
  721. }
  722. $self->{tmpfile} =~ s/tex$/pdf/;
  723. $self->error( $self->cleanup ) if !( -f $self->{tmpfile} );
  724. }
  725. }
  726. if ( $self->{format} =~ /(postscript|pdf)/ || $self->{media} eq 'email' ) {
  727. if ( $self->{media} eq 'email' ) {
  728. use LedgerSMB::Mailer;
  729. my $mail = new Mailer;
  730. for (qw(cc bcc subject message version format charset)) {
  731. $mail->{$_} = $self->{$_};
  732. }
  733. $mail->{to} = qq|$self->{email}|;
  734. $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
  735. $mail->{notify} = $self->{notify};
  736. $mail->{fileid} = "$fileid.";
  737. # if we send html or plain text inline
  738. if ( ( $self->{format} =~ /(html|txt)/ )
  739. && ( $self->{sendmode} eq 'inline' ) )
  740. {
  741. my $br = "";
  742. $br = "<br>" if $self->{format} eq 'html';
  743. $mail->{contenttype} = "text/$self->{format}";
  744. $mail->{message} =~ s/\r?\n/$br\n/g;
  745. $myconfig->{signature} =~ s/\\n/$br\n/g;
  746. $mail->{message} .= "$br\n-- $br\n$myconfig->{signature}\n$br"
  747. if $myconfig->{signature};
  748. unless ( open( IN, '<', $self->{tmpfile} ) ) {
  749. $err = $!;
  750. $self->cleanup;
  751. $self->error("$self->{tmpfile} : $err");
  752. }
  753. while (<IN>) {
  754. $mail->{message} .= $_;
  755. }
  756. close(IN);
  757. }
  758. else {
  759. @{ $mail->{attachments} } = ( $self->{tmpfile} );
  760. $myconfig->{signature} =~ s/\\n/\n/g;
  761. $mail->{message} .= "\n-- \n$myconfig->{signature}"
  762. if $myconfig->{signature};
  763. }
  764. if ( $err = $mail->send ) {
  765. $self->cleanup;
  766. $self->error($err);
  767. }
  768. }
  769. else {
  770. $self->{OUT} = $temphash{out};
  771. $self->{printmode} = $temphash{printmode};
  772. unless ( open( IN, '<', $self->{tmpfile} ) ) {
  773. $err = $!;
  774. $self->cleanup;
  775. $self->error("$self->{tmpfile} : $err");
  776. }
  777. binmode(IN);
  778. $self->{copies} = 1 if $self->{media} =~ /(screen|email|queue)/;
  779. chdir("$self->{cwd}");
  780. for my $i ( 1 .. $self->{copies} ) {
  781. if ( $self->{OUT} ) {
  782. unless ( open( OUT, $self->{printmode}, $self->{OUT} ) ) {
  783. $err = $!;
  784. $self->cleanup;
  785. $self->error("$self->{OUT} : $err");
  786. }
  787. }
  788. else {
  789. # launch application
  790. print qq|Content-Type: application/$self->{format}\n|
  791. . qq|Content-Disposition: attachment; filename="$self->{tmpfile}"\n\n|;
  792. unless ( open( OUT, ">-" ) ) {
  793. $err = $!;
  794. $self->cleanup;
  795. $self->error("STDOUT : $err");
  796. }
  797. }
  798. binmode(OUT);
  799. while (<IN>) {
  800. print OUT $_;
  801. }
  802. close(OUT);
  803. seek IN, 0, 0;
  804. }
  805. close(IN);
  806. }
  807. $self->cleanup;
  808. }
  809. }
  810. sub format_line {
  811. my $self = shift;
  812. $_ = shift;
  813. my $i = shift;
  814. my $str;
  815. my $newstr;
  816. my $pos;
  817. my $l;
  818. my $lf;
  819. my $line;
  820. my $var = "";
  821. my %a;
  822. my $offset;
  823. my $pad;
  824. my $item;
  825. while (/<\?lsmb (.+?) \?>/) {
  826. %a = ();
  827. foreach $item ( split / /, $1 ) {
  828. my ( $key, $value ) = split /=/, $item;
  829. if ( $value ne "" ) {
  830. $a{$key} = $value;
  831. }
  832. else {
  833. $var = $item;
  834. }
  835. }
  836. $str = ( defined $i ) ? $self->{$var}[$i] : $self->{$var};
  837. $newstr = $str;
  838. $self->{countitemnotes} = 1 if $var eq 'itemnotes';
  839. $var = $1;
  840. if ( $var =~ /^if\s+not\s+/ ) {
  841. if ($str) {
  842. $var =~ s/if\s+not\s+//;
  843. s/<\?lsmb if\s+not\s+$var \?>.*?(<\?lsmb end\s+$var \?>|$)//s;
  844. }
  845. else {
  846. s/<\?lsmb $var \?>//;
  847. }
  848. next;
  849. }
  850. if ( $var =~ /^if\s+/ ) {
  851. if ($str) {
  852. s/<\?lsmb $var \?>//;
  853. }
  854. else {
  855. $var =~ s/if\s+//;
  856. s/<\?lsmb if\s+$var \?>.*?(<\?lsmb end\s+$var \?>|$)//s;
  857. }
  858. next;
  859. }
  860. if ( $var =~ /^end\s+/ ) {
  861. s/<\?lsmb $var \?>//;
  862. next;
  863. }
  864. if ( $a{align} || $a{width} || $a{offset} ) {
  865. $newstr = "";
  866. $offset = 0;
  867. $lf = "";
  868. foreach $str ( split /\n/, $str ) {
  869. $line = $str;
  870. $l = length $str;
  871. do {
  872. if ( ( $pos = length $str ) > $a{width} ) {
  873. if ( ( $pos = rindex $str, " ", $a{width} ) > 0 ) {
  874. $line = substr( $str, 0, $pos );
  875. }
  876. $pos = length $str if $pos == -1;
  877. }
  878. $l = length $line;
  879. # pad left, right or center
  880. $l = ( $a{width} - $l );
  881. $pad = " " x $l;
  882. if ( $a{align} =~ /right/i ) {
  883. $line = " " x $offset . $pad . $line;
  884. }
  885. if ( $a{align} =~ /left/i ) {
  886. $line = " " x $offset . $line . $pad;
  887. }
  888. if ( $a{align} =~ /center/i ) {
  889. $pad = " " x ( $l / 2 );
  890. $line = " " x $offset . $pad . $line;
  891. $pad = " " x ( $l / 2 );
  892. $line .= $pad;
  893. }
  894. $newstr .= "$lf$line";
  895. $str = substr( $str, $pos + 1 );
  896. $line = $str;
  897. $lf = "\n";
  898. $offset = $a{offset};
  899. } while ($str);
  900. }
  901. }
  902. s/<\?lsmb (.+?) \?>/$newstr/;
  903. }
  904. $_;
  905. }
  906. sub cleanup {
  907. my $self = shift;
  908. chdir("$self->{tmpdir}");
  909. my @err = ();
  910. if ( -f "$self->{errfile}" ) {
  911. open( FH, '<', "$self->{errfile}" );
  912. @err = <FH>;
  913. close(FH);
  914. }
  915. if ( $self->{tmpfile} ) {
  916. # strip extension
  917. $self->{tmpfile} =~ s/\.\w+$//g;
  918. my $tmpfile = $self->{tmpfile};
  919. unlink(<$tmpfile.*>);
  920. }
  921. chdir("$self->{cwd}");
  922. "@err";
  923. }
  924. sub rerun_latex {
  925. my $self = shift;
  926. my $a = 0;
  927. if ( -f "$self->{errfile}" ) {
  928. open( FH, '<', "$self->{errfile}" );
  929. $a = grep /(longtable Warning:|Warning:.*?LastPage)/, <FH>;
  930. close(FH);
  931. }
  932. $a;
  933. }
  934. sub format_string {
  935. my ( $self, @fields ) = @_;
  936. my $format = $self->{format};
  937. if ( $self->{format} =~ /(postscript|pdf)/ ) {
  938. $format = 'tex';
  939. }
  940. my %replace = (
  941. 'order' => {
  942. html => [ '<', '>', '\n', '\r' ],
  943. txt => [ '\n', '\r' ],
  944. tex => [
  945. quotemeta('\\'), '&', '\n', '\r',
  946. '\$', '%', '_', '#',
  947. quotemeta('^'), '{', '}', '<',
  948. '>', '£'
  949. ]
  950. },
  951. html => {
  952. '<' => '&lt;',
  953. '>' => '&gt;',
  954. '\n' => '<br />',
  955. '\r' => '<br />'
  956. },
  957. txt => { '\n' => "\n", '\r' => "\r" },
  958. tex => {
  959. '&' => '\&',
  960. '$' => '\$',
  961. '%' => '\%',
  962. '_' => '\_',
  963. '#' => '\#',
  964. quotemeta('^') => '\^\\',
  965. '{' => '\{',
  966. '}' => '\}',
  967. '<' => '$<$',
  968. '>' => '$>$',
  969. '\n' => '\newline ',
  970. '\r' => '\newline ',
  971. '£' => '\pounds ',
  972. quotemeta('\\') => '/'
  973. }
  974. );
  975. my $key;
  976. foreach $key ( @{ $replace{order}{$format} } ) {
  977. for (@fields) { $self->{$_} =~ s/$key/$replace{$format}{$key}/g }
  978. }
  979. }
  980. sub datetonum {
  981. my ( $self, $myconfig, $date, $picture ) = @_;
  982. if ( $date && $date =~ /\D/ ) {
  983. if ( $myconfig->{dateformat} =~ /^yy/ ) {
  984. ( $yy, $mm, $dd ) = split /\D/, $date;
  985. }
  986. if ( $myconfig->{dateformat} =~ /^mm/ ) {
  987. ( $mm, $dd, $yy ) = split /\D/, $date;
  988. }
  989. if ( $myconfig->{dateformat} =~ /^dd/ ) {
  990. ( $dd, $mm, $yy ) = split /\D/, $date;
  991. }
  992. $dd *= 1;
  993. $mm *= 1;
  994. $yy += 2000 if length $yy == 2;
  995. $dd = substr( "0$dd", -2 );
  996. $mm = substr( "0$mm", -2 );
  997. $date = "$yy$mm$dd";
  998. }
  999. $date;
  1000. }
  1001. sub add_date {
  1002. my ( $self, $myconfig, $date, $repeat, $unit ) = @_;
  1003. use Time::Local;
  1004. my $diff = 0;
  1005. my $spc = $myconfig->{dateformat};
  1006. $spc =~ s/\w//g;
  1007. $spc = substr( $spc, 0, 1 );
  1008. if ($date) {
  1009. if ( $date =~ /\D/ ) {
  1010. if ( $myconfig->{dateformat} =~ /^yy/ ) {
  1011. ( $yy, $mm, $dd ) = split /\D/, $date;
  1012. }
  1013. if ( $myconfig->{dateformat} =~ /^mm/ ) {
  1014. ( $mm, $dd, $yy ) = split /\D/, $date;
  1015. }
  1016. if ( $myconfig->{dateformat} =~ /^dd/ ) {
  1017. ( $dd, $mm, $yy ) = split /\D/, $date;
  1018. }
  1019. }
  1020. else {
  1021. # ISO
  1022. ( $yy, $mm, $dd ) =~ /(....)(..)(..)/;
  1023. }
  1024. if ( $unit eq 'days' ) {
  1025. $diff = $repeat * 86400;
  1026. }
  1027. if ( $unit eq 'weeks' ) {
  1028. $diff = $repeat * 604800;
  1029. }
  1030. if ( $unit eq 'months' ) {
  1031. $diff = $mm + $repeat;
  1032. my $whole = int( $diff / 12 );
  1033. $yy += $whole;
  1034. $mm = ( $diff % 12 ) + 1;
  1035. $diff = 0;
  1036. }
  1037. if ( $unit eq 'years' ) {
  1038. $yy++;
  1039. }
  1040. $mm--;
  1041. @t = localtime( timelocal( 0, 0, 0, $dd, $mm, $yy ) + $diff );
  1042. $t[4]++;
  1043. $mm = substr( "0$t[4]", -2 );
  1044. $dd = substr( "0$t[3]", -2 );
  1045. $yy = $t[5] + 1900;
  1046. if ( $date =~ /\D/ ) {
  1047. if ( $myconfig->{dateformat} =~ /^yy/ ) {
  1048. $date = "$yy$spc$mm$spc$dd";
  1049. }
  1050. if ( $myconfig->{dateformat} =~ /^mm/ ) {
  1051. $date = "$mm$spc$dd$spc$yy";
  1052. }
  1053. if ( $myconfig->{dateformat} =~ /^dd/ ) {
  1054. $date = "$dd$spc$mm$spc$yy";
  1055. }
  1056. }
  1057. else {
  1058. $date = "$yy$mm$dd";
  1059. }
  1060. }
  1061. $date;
  1062. }
  1063. sub print_button {
  1064. my ( $self, $button, $name ) = @_;
  1065. print
  1066. qq|<button class="submit" type="submit" name="action" value="$name" accesskey="$button->{$name}{key}" title="$button->{$name}{value} [Alt-$button->{$name}{key}]">$button->{$name}{value}</button>\n|;
  1067. }
  1068. # Database routines used throughout
  1069. sub db_init {
  1070. my ( $self, $myconfig ) = @_;
  1071. $self->{dbh} = $self->dbconnect_noauto($myconfig) || $self->dberror();
  1072. my $query = "SELECT t.extends,
  1073. coalesce (t.table_name, 'custom_' || extends)
  1074. || ':' || f.field_name as field_def
  1075. FROM custom_table_catalog t
  1076. JOIN custom_field_catalog f USING (table_id)";
  1077. my $sth = $self->{dbh}->prepare($query);
  1078. $sth->execute;
  1079. my $ref;
  1080. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1081. push @{ $self->{custom_db_fields}{ $ref->{extends} } },
  1082. $ref->{field_def};
  1083. }
  1084. }
  1085. sub run_custom_queries {
  1086. my ( $self, $tablename, $query_type, $linenum ) = @_;
  1087. my $dbh = $self->{dbh};
  1088. if ( $query_type !~ /^(select|insert|update)$/i ) {
  1089. $self->error(
  1090. $locale->text(
  1091. "Passed incorrect query type to run_custom_queries."
  1092. )
  1093. );
  1094. }
  1095. my @rc;
  1096. my %temphash;
  1097. my @templist;
  1098. my @elements;
  1099. my $query;
  1100. my $ins_values;
  1101. if ($linenum) {
  1102. $linenum = "_$linenum";
  1103. }
  1104. $query_type = uc($query_type);
  1105. for ( @{ $self->{custom_db_fields}{$tablename} } ) {
  1106. @elements = split( /:/, $_ );
  1107. push @{ $temphash{ $elements[0] } }, $elements[1];
  1108. }
  1109. for ( keys %temphash ) {
  1110. my @data;
  1111. my $ins_values;
  1112. $query = "$query_type ";
  1113. if ( $query_type eq 'UPDATE' ) {
  1114. $query = "DELETE FROM $_ WHERE row_id = ?";
  1115. my $sth = $dbh->prepare($query);
  1116. $sth->execute->( $self->{ "id" . "$linenum" } )
  1117. || $self->dberror($query);
  1118. }
  1119. elsif ( $query_type eq 'INSERT' ) {
  1120. $query .= " INTO $_ (";
  1121. }
  1122. my $first = 1;
  1123. for ( @{ $temphash{$_} } ) {
  1124. $query .= "$_";
  1125. if ( $query_type eq 'UPDATE' ) {
  1126. $query .= '= ?';
  1127. }
  1128. $ins_values .= "?, ";
  1129. $query .= ", ";
  1130. $first = 0;
  1131. if ( $query_type eq 'UPDATE' or $query_type eq 'INSERT' ) {
  1132. push @data, $self->{"$_$linenum"};
  1133. }
  1134. }
  1135. if ( $query_type ne 'INSERT' ) {
  1136. $query =~ s/, $//;
  1137. }
  1138. if ( $query_type eq 'SELECT' ) {
  1139. $query .= " FROM $_";
  1140. }
  1141. if ( $query_type eq 'SELECT' or $query_type eq 'UPDATE' ) {
  1142. $query .= " WHERE row_id = ?";
  1143. }
  1144. if ( $query_type eq 'INSERT' ) {
  1145. $query .= " row_id) VALUES ($ins_values ?)";
  1146. }
  1147. if ( $query_type eq 'SELECT' ) {
  1148. push @rc, [$query];
  1149. }
  1150. else {
  1151. unshift( @data, $query );
  1152. push @rc, [@data];
  1153. }
  1154. }
  1155. if ( $query_type eq 'INSERT' ) {
  1156. for (@rc) {
  1157. $query = shift( @{$_} );
  1158. $sth = $dbh->prepare($query)
  1159. || $self->db_error($query);
  1160. $sth->execute( @{$_}, $self->{id} )
  1161. || $self->dberror($query);
  1162. $sth->finish;
  1163. $did_insert = 1;
  1164. }
  1165. }
  1166. elsif ( $query_type eq 'UPDATE' ) {
  1167. @rc = $self->run_custom_queries( $tablename, 'INSERT', $linenum );
  1168. }
  1169. elsif ( $query_type eq 'SELECT' ) {
  1170. for (@rc) {
  1171. $query = shift @{$_};
  1172. $sth = $self->{dbh}->prepare($query);
  1173. $sth->execute( $self->{id} );
  1174. $ref = $sth->fetchrow_hashref(NAME_lc);
  1175. for ( keys %{$ref} ) {
  1176. $self->{$_} = $ref->{$_};
  1177. }
  1178. }
  1179. }
  1180. @rc;
  1181. }
  1182. sub dbconnect {
  1183. my ( $self, $myconfig ) = @_;
  1184. # connect to database
  1185. my $dbh = DBI->connect( $myconfig->{dbconnect},
  1186. $myconfig->{dbuser}, $myconfig->{dbpasswd} )
  1187. or $self->dberror;
  1188. # set db options
  1189. if ( $myconfig->{dboptions} ) {
  1190. $dbh->do( $myconfig->{dboptions} )
  1191. || $self->dberror( $myconfig->{dboptions} );
  1192. }
  1193. $dbh;
  1194. }
  1195. sub dbconnect_noauto {
  1196. my ( $self, $myconfig ) = @_;
  1197. # connect to database
  1198. $dbh = DBI->connect(
  1199. $myconfig->{dbconnect}, $myconfig->{dbuser},
  1200. $myconfig->{dbpasswd}, { AutoCommit => 0 }
  1201. ) or $self->dberror;
  1202. # set db options
  1203. if ( $myconfig->{dboptions} ) {
  1204. $dbh->do( $myconfig->{dboptions} );
  1205. }
  1206. $dbh;
  1207. }
  1208. sub dbquote {
  1209. my ( $self, $var ) = @_;
  1210. if ( $var eq '' ) {
  1211. $_ = "NULL";
  1212. }
  1213. else {
  1214. $_ = $self->{dbh}->quote($var);
  1215. }
  1216. $_;
  1217. }
  1218. sub update_balance {
  1219. # This is a dangerous private function. All apps calling it must
  1220. # be careful to avoid SQL injection issues
  1221. my ( $self, $dbh, $table, $field, $where, $value ) = @_;
  1222. # if we have a value, go do it
  1223. if ($value) {
  1224. # retrieve balance from table
  1225. my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
  1226. my ($balance) = $dbh->selectrow_array($query);
  1227. $balance += $value;
  1228. # update balance
  1229. $query = "UPDATE $table SET $field = $balance WHERE $where";
  1230. $dbh->do($query) || $self->dberror($query);
  1231. }
  1232. }
  1233. sub update_exchangerate {
  1234. my ( $self, $dbh, $curr, $transdate, $buy, $sell ) = @_;
  1235. # some sanity check for currency
  1236. return if ( $curr eq "" );
  1237. my $query = qq|
  1238. SELECT curr
  1239. FROM exchangerate
  1240. WHERE curr = ?
  1241. AND transdate = ?
  1242. FOR UPDATE|;
  1243. my $sth = $self->{dbh}->prepare($query);
  1244. $sth->execute( $curr, $transdate ) || $self->dberror($query);
  1245. my $set;
  1246. my @queryargs;
  1247. if ( $buy && $sell ) {
  1248. $set = "buy = ?, sell = ?";
  1249. @queryargs = ( $buy, $sell );
  1250. }
  1251. elsif ($buy) {
  1252. $set = "buy = ?";
  1253. @queryargs = ($buy);
  1254. }
  1255. elsif ($sell) {
  1256. $set = "sell = ?";
  1257. @queryargs = ($sell);
  1258. }
  1259. if ( $sth->fetchrow_array ) {
  1260. $query = qq|UPDATE exchangerate
  1261. SET $set
  1262. WHERE curr = ?
  1263. AND transdate = ?|;
  1264. push( @queryargs, $curr, $transdate );
  1265. }
  1266. else {
  1267. $query = qq|
  1268. INSERT INTO exchangerate (
  1269. curr, buy, sell, transdate)
  1270. VALUES (?, ?, ?, ?)|;
  1271. @queryargs = ( $curr, $buy, $sell, $transdate );
  1272. }
  1273. $sth->finish;
  1274. $sth = $self->{dbh}->prepare($query);
  1275. $sth->execute(@queryargs) || $self->dberror($query);
  1276. }
  1277. sub save_exchangerate {
  1278. my ( $self, $myconfig, $currency, $transdate, $rate, $fld ) = @_;
  1279. my ( $buy, $sell ) = ( 0, 0 );
  1280. $buy = $rate if $fld eq 'buy';
  1281. $sell = $rate if $fld eq 'sell';
  1282. $self->update_exchangerate( $self->{dbh}, $currency, $transdate, $buy,
  1283. $sell );
  1284. $dbh->commit;
  1285. }
  1286. sub get_exchangerate {
  1287. my ( $self, $dbh, $curr, $transdate, $fld ) = @_;
  1288. my $exchangerate = 1;
  1289. if ($transdate) {
  1290. my $query = qq|
  1291. SELECT $fld FROM exchangerate
  1292. WHERE curr = ? AND transdate = ?|;
  1293. $sth = $self->{dbh}->prepare($query);
  1294. $sth->execute( $curr, $transdate );
  1295. ($exchangerate) = $sth->fetchrow_array;
  1296. }
  1297. $exchangerate;
  1298. $sth->finish;
  1299. $self->{dbh}->commit;
  1300. }
  1301. sub check_exchangerate {
  1302. my ( $self, $myconfig, $currency, $transdate, $fld ) = @_;
  1303. return "" unless $transdate;
  1304. my $query = qq|
  1305. SELECT $fld
  1306. FROM exchangerate
  1307. WHERE curr = ? AND transdate = ?|;
  1308. my $sth = $self->{dbh}->prepare($query);
  1309. $sth->execute( $currenct, $transdate );
  1310. my ($exchangerate) = $sth->fetchrow_array;
  1311. $sth->finish;
  1312. $self->{dbh}->commit;
  1313. $exchangerate;
  1314. }
  1315. sub add_shipto {
  1316. my ( $self, $dbh, $id ) = @_;
  1317. my $shipto;
  1318. foreach my $item (
  1319. qw(name address1 address2 city state
  1320. zipcode country contact phone fax email)
  1321. )
  1322. {
  1323. if ( $self->{"shipto$item"} ne "" ) {
  1324. $shipto = 1 if ( $self->{$item} ne $self->{"shipto$item"} );
  1325. }
  1326. }
  1327. if ($shipto) {
  1328. my $query = qq|
  1329. INSERT INTO shipto
  1330. (trans_id, shiptoname, shiptoaddress1,
  1331. shiptoaddress2, shiptocity, shiptostate,
  1332. shiptozipcode, shiptocountry, shiptocontact,
  1333. shiptophone, shiptofax, shiptoemail)
  1334. VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  1335. |;
  1336. $sth = $self->{dbh}->prepare($query) || $self->dberror($query);
  1337. $sth->execute(
  1338. $id, $self->{shiptoname},
  1339. $self->{shiptoaddress1}, $self->{shiptoaddress2},
  1340. $self->{shiptocity}, $self->{shiptostate},
  1341. $self->{shiptozipcode}, $self->{shiptocountry},
  1342. $self->{shiptocontact}, $self->{shiptophone},
  1343. $self->{shiptofax}, $self->{shiptoemail}
  1344. ) || $self->dberror($query);
  1345. $sth->finish;
  1346. $self->{dbh}->commit;
  1347. }
  1348. }
  1349. sub get_employee {
  1350. my ( $self, $dbh ) = @_;
  1351. my $login = $self->{login};
  1352. $login =~ s/@.*//;
  1353. my $query = qq|SELECT name, id
  1354. FROM employees
  1355. WHERE login = ?|;
  1356. $sth = $self->{dbh}->prepare($query);
  1357. $sth->execute($login);
  1358. my (@a) = $sth->fetchrow_array();
  1359. $a[1] *= 1;
  1360. $sth->finish;
  1361. $self->{dbh}->commit;
  1362. @a;
  1363. }
  1364. # this sub gets the id and name from $table
  1365. sub get_name {
  1366. my ( $self, $myconfig, $table, $transdate ) = @_;
  1367. # connect to database
  1368. my @queryargs;
  1369. my $where;
  1370. if ($transdate) {
  1371. $where = qq|
  1372. AND (startdate IS NULL OR startdate <= ?)
  1373. AND (enddate IS NULL OR enddate >= ?)|;
  1374. @queryargs = ( $transdate, $transdate );
  1375. }
  1376. my $name = $self->like( lc $self->{$table} );
  1377. my $query = qq|
  1378. SELECT * FROM $table
  1379. WHERE (lower(name) LIKE ? OR ${table}number LIKE ?)
  1380. $where
  1381. ORDER BY name|;
  1382. unshift( @queryargs, $name, $name );
  1383. my $sth = $self->{dbh}->prepare($query);
  1384. $sth->execute(@queryargs) || $self->dberror($query);
  1385. my $i = 0;
  1386. @{ $self->{name_list} } = ();
  1387. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1388. push( @{ $self->{name_list} }, $ref );
  1389. $i++;
  1390. }
  1391. $sth->finish;
  1392. $self->{dbh}->commit;
  1393. $i;
  1394. }
  1395. sub all_vc {
  1396. my ( $self, $myconfig, $vc, $module, $dbh, $transdate, $job ) = @_;
  1397. my $ref;
  1398. my $disconnect = 0;
  1399. $dbh = $self->{dbh};
  1400. my $sth;
  1401. my $query = qq|SELECT count(*) FROM $vc|;
  1402. my $where;
  1403. my @queryargs = ();
  1404. if ($transdate) {
  1405. $query .= qq| WHERE (startdate IS NULL OR startdate <= ?)
  1406. AND (enddate IS NULL OR enddate >= ?)|;
  1407. @queryargs = ( $transdate, $transdate );
  1408. }
  1409. $sth = $dbh->prepare($query);
  1410. $sth->execute(@queryargs);
  1411. my ($count) = $sth->fetchrow_array;
  1412. $sth->finish;
  1413. @queryargs = ();
  1414. # build selection list
  1415. if ( $count < $myconfig->{vclimit} ) {
  1416. $self->{"${vc}_id"} *= 1;
  1417. $query = qq|SELECT id, name
  1418. FROM $vc
  1419. WHERE 1=1
  1420. $where
  1421. UNION
  1422. SELECT id,name
  1423. FROM $vc
  1424. WHERE id = ?
  1425. ORDER BY name|;
  1426. push( @queryargs, $self->{"${vc}_id"} );
  1427. $sth = $dbh->prepare($query);
  1428. $sth->execute(@queryargs) || $self->dberror($query);
  1429. @{ $self->{"all_$vc"} } = ();
  1430. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1431. push @{ $self->{"all_$vc"} }, $ref;
  1432. }
  1433. $sth->finish;
  1434. }
  1435. # get self
  1436. if ( !$self->{employee_id} ) {
  1437. ( $self->{employee}, $self->{employee_id} ) = split /--/,
  1438. $self->{employee};
  1439. ( $self->{employee}, $self->{employee_id} ) = $self->get_employee($dbh)
  1440. unless $self->{employee_id};
  1441. }
  1442. $self->all_employees( $myconfig, $dbh, $transdate, 1 );
  1443. $self->all_departments( $myconfig, $dbh, $vc );
  1444. $self->all_projects( $myconfig, $dbh, $transdate, $job );
  1445. # get language codes
  1446. $query = qq|SELECT *
  1447. FROM language
  1448. ORDER BY 2|;
  1449. $sth = $dbh->prepare($query);
  1450. $sth->execute || $self->dberror($query);
  1451. $self->{all_language} = ();
  1452. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1453. push @{ $self->{all_language} }, $ref;
  1454. }
  1455. $sth->finish;
  1456. $self->all_taxaccounts( $myconfig, $dbh, $transdate );
  1457. $self->{dbh}->commit;
  1458. }
  1459. sub all_taxaccounts {
  1460. my ( $self, $myconfig, $dbh2, $transdate ) = @_;
  1461. my $dbh = $self->{dbh};
  1462. my $sth;
  1463. my $query;
  1464. my $where;
  1465. my @queryargs = ();
  1466. if ($transdate) {
  1467. $where = qq| AND (t.validto >= ? OR t.validto IS NULL)|;
  1468. push( @queryargs, $transdate );
  1469. }
  1470. if ( $self->{taxaccounts} ) {
  1471. # rebuild tax rates
  1472. $query = qq|SELECT t.rate, t.taxnumber
  1473. FROM tax t
  1474. JOIN chart c ON (c.id = t.chart_id)
  1475. WHERE c.accno = ?
  1476. $where
  1477. ORDER BY accno, validto|;
  1478. $sth = $dbh->prepare($query) || $self->dberror($query);
  1479. foreach my $accno ( split / /, $self->{taxaccounts} ) {
  1480. $sth->execute( $accno, @queryargs );
  1481. ( $self->{"${accno}_rate"}, $self->{"${accno}_taxnumber"} ) =
  1482. $sth->fetchrow_array;
  1483. $sth->finish;
  1484. }
  1485. }
  1486. $self->{dbh}->commit;
  1487. }
  1488. sub all_employees {
  1489. my ( $self, $myconfig, $dbh2, $transdate, $sales ) = @_;
  1490. my $dbh = $self->{dbh};
  1491. my @whereargs = ();
  1492. # setup employees/sales contacts
  1493. my $query = qq|SELECT id, name
  1494. FROM employees
  1495. WHERE 1 = 1|;
  1496. if ($transdate) {
  1497. $query .= qq| AND (startdate IS NULL OR startdate <= ?)
  1498. AND (enddate IS NULL OR enddate >= ?)|;
  1499. @whereargs = ( $transdate, $transdate );
  1500. }
  1501. else {
  1502. $query .= qq| AND enddate IS NULL|;
  1503. }
  1504. if ($sales) {
  1505. $query .= qq| AND sales = '1'|;
  1506. }
  1507. $query .= qq| ORDER BY name|;
  1508. my $sth = $dbh->prepare($query);
  1509. $sth->execute(@whereargs) || $self->dberror($query);
  1510. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1511. push @{ $self->{all_employee} }, $ref;
  1512. }
  1513. $sth->finish;
  1514. $dbh->commit;
  1515. }
  1516. sub all_projects {
  1517. my ( $self, $myconfig, $dbh2, $transdate, $job ) = @_;
  1518. my $dbh = $self->{dbh};
  1519. my @queryargs = ();
  1520. my $where = "1 = 1";
  1521. $where = qq|id NOT IN (SELECT id
  1522. FROM parts
  1523. WHERE project_id > 0)| if !$job;
  1524. my $query = qq|SELECT *
  1525. FROM project
  1526. WHERE $where|;
  1527. if ( $self->{language_code} ) {
  1528. $query = qq|
  1529. SELECT pr.*, t.description AS translation
  1530. FROM project pr
  1531. LEFT JOIN translation t ON (t.trans_id = pr.id)
  1532. WHERE t.language_code = ?|;
  1533. push( @queryargs, $self->{language_code} );
  1534. }
  1535. if ($transdate) {
  1536. $query .= qq| AND (startdate IS NULL OR startdate <= ?)
  1537. AND (enddate IS NULL OR enddate >= ?)|;
  1538. push( @queryargs, $transdate, $transdate );
  1539. }
  1540. $query .= qq| ORDER BY projectnumber|;
  1541. $sth = $dbh->prepare($query);
  1542. $sth->execute(@queryargs) || $self->dberror($query);
  1543. @{ $self->{all_project} } = ();
  1544. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1545. push @{ $self->{all_project} }, $ref;
  1546. }
  1547. $sth->finish;
  1548. $dbh->commit;
  1549. }
  1550. sub all_departments {
  1551. my ( $self, $myconfig, $dbh2, $vc ) = @_;
  1552. $dbh = $self->{dbh};
  1553. my $where = "1 = 1";
  1554. if ($vc) {
  1555. if ( $vc eq 'customer' ) {
  1556. $where = " role = 'P'";
  1557. }
  1558. }
  1559. my $query = qq|SELECT id, description
  1560. FROM department
  1561. WHERE $where
  1562. ORDER BY 2|;
  1563. my $sth = $dbh->prepare($query);
  1564. $sth->execute || $self->dberror($query);
  1565. @{ $self->{all_department} } = ();
  1566. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1567. push @{ $self->{all_department} }, $ref;
  1568. }
  1569. $sth->finish;
  1570. $self->all_years($myconfig);
  1571. $dbh->commit;
  1572. }
  1573. sub all_years {
  1574. my ( $self, $myconfig, $dbh2 ) = @_;
  1575. $dbh = $self->{dbh};
  1576. # get years
  1577. my $query = qq|
  1578. SELECT (SELECT MIN(transdate) FROM acc_trans),
  1579. (SELECT MAX(transdate) FROM acc_trans)|;
  1580. my ( $startdate, $enddate ) = $dbh->selectrow_array($query);
  1581. if ( $myconfig->{dateformat} =~ /^yy/ ) {
  1582. ($startdate) = split /\W/, $startdate;
  1583. ($enddate) = split /\W/, $enddate;
  1584. }
  1585. else {
  1586. (@_) = split /\W/, $startdate;
  1587. $startdate = $_[2];
  1588. (@_) = split /\W/, $enddate;
  1589. $enddate = $_[2];
  1590. }
  1591. $self->{all_years} = ();
  1592. $startdate = substr( $startdate, 0, 4 );
  1593. $enddate = substr( $enddate, 0, 4 );
  1594. while ( $enddate >= $startdate ) {
  1595. push @{ $self->{all_years} }, $enddate--;
  1596. }
  1597. #this should probably be changed to use locale
  1598. %{ $self->{all_month} } = (
  1599. '01' => 'January',
  1600. '02' => 'February',
  1601. '03' => 'March',
  1602. '04' => 'April',
  1603. '05' => 'May ',
  1604. '06' => 'June',
  1605. '07' => 'July',
  1606. '08' => 'August',
  1607. '09' => 'September',
  1608. '10' => 'October',
  1609. '11' => 'November',
  1610. '12' => 'December'
  1611. );
  1612. $dbh->commit;
  1613. }
  1614. sub create_links {
  1615. my ( $self, $module, $myconfig, $vc, $job ) = @_;
  1616. # get last customers or vendors
  1617. my ( $query, $sth );
  1618. $dbh = $self->{dbh};
  1619. my %xkeyref = ();
  1620. # now get the account numbers
  1621. $query = qq|SELECT accno, description, link
  1622. FROM chart
  1623. WHERE link LIKE ?
  1624. ORDER BY accno|;
  1625. $sth = $dbh->prepare($query);
  1626. $sth->execute( "%" . "$module%" ) || $self->dberror($query);
  1627. $self->{accounts} = "";
  1628. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1629. foreach my $key ( split /:/, $ref->{link} ) {
  1630. if ( $key =~ /$module/ ) {
  1631. # cross reference for keys
  1632. $xkeyref{ $ref->{accno} } = $key;
  1633. push @{ $self->{"${module}_links"}{$key} },
  1634. {
  1635. accno => $ref->{accno},
  1636. description => $ref->{description}
  1637. };
  1638. $self->{accounts} .= "$ref->{accno} "
  1639. unless $key =~ /tax/;
  1640. }
  1641. }
  1642. }
  1643. $sth->finish;
  1644. my $arap = ( $vc eq 'customer' ) ? 'ar' : 'ap';
  1645. if ( $self->{id} ) {
  1646. $query = qq|
  1647. SELECT a.invnumber, a.transdate,
  1648. a.${vc}_id, a.datepaid, a.duedate, a.ordnumber,
  1649. a.taxincluded, a.curr AS currency, a.notes,
  1650. a.intnotes, c.name AS $vc, a.department_id,
  1651. d.description AS department,
  1652. a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
  1653. a.employee_id, e.name AS employee,
  1654. c.language_code, a.ponumber
  1655. FROM $arap a
  1656. JOIN $vc c ON (a.${vc}_id = c.id)
  1657. LEFT JOIN employees e ON (e.id = a.employee_id)
  1658. LEFT JOIN department d ON (d.id = a.department_id)
  1659. WHERE a.id = ?|;
  1660. $sth = $dbh->prepare($query);
  1661. $sth->execute( $self->{id} ) || $self->dberror($query);
  1662. $ref = $sth->fetchrow_hashref(NAME_lc);
  1663. foreach $key ( keys %$ref ) {
  1664. $self->{$key} = $ref->{$key};
  1665. }
  1666. $sth->finish;
  1667. # get printed, emailed
  1668. $query = qq|
  1669. SELECT s.printed, s.emailed, s.spoolfile, s.formname
  1670. FROM status s WHERE s.trans_id = ?|;
  1671. $sth = $dbh->prepare($query);
  1672. $sth->execute( $self->{id} ) || $self->dberror($query);
  1673. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1674. $self->{printed} .= "$ref->{formname} "
  1675. if $ref->{printed};
  1676. $self->{emailed} .= "$ref->{formname} "
  1677. if $ref->{emailed};
  1678. $self->{queued} .= "$ref->{formname} " . "$ref->{spoolfile} "
  1679. if $ref->{spoolfile};
  1680. }
  1681. $sth->finish;
  1682. for (qw(printed emailed queued)) { $self->{$_} =~ s/ +$//g }
  1683. # get recurring
  1684. $self->get_recurring($dbh);
  1685. # get amounts from individual entries
  1686. $query = qq|
  1687. SELECT c.accno, c.description, a.source, a.amount,
  1688. a.memo, a.transdate, a.cleared, a.project_id,
  1689. p.projectnumber
  1690. FROM acc_trans a
  1691. JOIN chart c ON (c.id = a.chart_id)
  1692. LEFT JOIN project p ON (p.id = a.project_id)
  1693. WHERE a.trans_id = ?
  1694. AND a.fx_transaction = '0'
  1695. ORDER BY transdate|;
  1696. $sth = $dbh->prepare($query);
  1697. $sth->execute( $self->{id} ) || $self->dberror($query);
  1698. my $fld = ( $vc eq 'customer' ) ? 'buy' : 'sell';
  1699. $self->{exchangerate} =
  1700. $self->get_exchangerate( $dbh, $self->{currency}, $self->{transdate},
  1701. $fld );
  1702. # store amounts in {acc_trans}{$key} for multiple accounts
  1703. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1704. $ref->{exchangerate} =
  1705. $self->get_exchangerate( $dbh, $self->{currency},
  1706. $ref->{transdate}, $fld );
  1707. push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
  1708. }
  1709. $sth->finish;
  1710. for (qw(curr closedto revtrans)) {
  1711. $query = qq|
  1712. SELECT value FROM defaults
  1713. WHERE setting_key = '$_'|;
  1714. $sth = $dbh->prepare($query);
  1715. $sth->execute || $self->dberror($query);
  1716. ($val) = $sth->fetchrow_array();
  1717. if ( $_ eq 'curr' ) {
  1718. $self->{currencies} = $val;
  1719. }
  1720. else {
  1721. $self->{$_} = $val;
  1722. }
  1723. $sth->finish;
  1724. }
  1725. }
  1726. else {
  1727. for (qw(current_date curr closedto revtrans)) {
  1728. $query = qq|
  1729. SELECT value FROM defaults
  1730. WHERE setting_key = '$_'|;
  1731. $sth = $dbh->prepare($query);
  1732. $sth->execute || $self->dberror($query);
  1733. ($val) = $sth->fetchrow_array();
  1734. if ( $_ eq 'curr' ) {
  1735. $self->{currencies} = $val;
  1736. }
  1737. elsif ( $_ eq 'current_date' ) {
  1738. $self->{transdate} = $val;
  1739. }
  1740. else {
  1741. $self->{$_} = $val;
  1742. }
  1743. $sth->finish;
  1744. }
  1745. if ( !$self->{"$self->{vc}_id"} ) {
  1746. $self->lastname_used( $myconfig, $dbh, $vc, $module );
  1747. }
  1748. }
  1749. $self->all_vc( $myconfig, $vc, $module, $dbh, $self->{transdate}, $job );
  1750. $self->{dbh}->commit;
  1751. }
  1752. sub lastname_used {
  1753. my ( $self, $myconfig, $dbh2, $vc, $module ) = @_;
  1754. $vc ||= $self->{vc};
  1755. my $dbh = $self->{dbh};
  1756. my $arap = ( $vc eq 'customer' ) ? "ar" : "ap";
  1757. my $where = "1 = 1";
  1758. my $sth;
  1759. if ( $self->{type} =~ /_order/ ) {
  1760. $arap = 'oe';
  1761. $where = "quotation = '0'";
  1762. }
  1763. if ( $self->{type} =~ /_quotation/ ) {
  1764. $arap = 'oe';
  1765. $where = "quotation = '1'";
  1766. }
  1767. my $query = qq|
  1768. SELECT id
  1769. FROM $arap
  1770. WHERE id IN
  1771. (SELECT MAX(id)
  1772. FROM $arap
  1773. WHERE $where AND ${vc}_id > 0)|;
  1774. my ($trans_id) = $dbh->selectrow_array($query);
  1775. $trans_id *= 1;
  1776. $query = qq|
  1777. SELECT ct.name AS $vc, a.curr AS currency, a.${vc}_id,
  1778. current_date + ct.terms AS duedate,
  1779. a.department_id, d.description AS department, ct.notes,
  1780. ct.curr AS currency
  1781. FROM $arap a
  1782. JOIN $vc ct ON (a.${vc}_id = ct.id)
  1783. LEFT JOIN department d ON (a.department_id = d.id)
  1784. WHERE a.id = ?|;
  1785. $sth = $dbh->prepare($query);
  1786. $sth->execute($trans_id) || $self->dberror($query);
  1787. my $ref = $sth->fetchrow_hashref(NAME_lc);
  1788. for ( keys %$ref ) { $self->{$_} = $ref->{$_} }
  1789. $sth->finish;
  1790. $dbh->commit;
  1791. }
  1792. sub current_date {
  1793. my ( $self, $myconfig, $thisdate, $days ) = @_;
  1794. my $dbh = $self->{dbh};
  1795. my $query;
  1796. $days *= 1;
  1797. if ($thisdate) {
  1798. my $dateformat = $myconfig->{dateformat};
  1799. if ( $myconfig->{dateformat} !~ /^y/ ) {
  1800. my @a = split /\D/, $thisdate;
  1801. $dateformat .= "yy" if ( length $a[2] > 2 );
  1802. }
  1803. if ( $thisdate !~ /\D/ ) {
  1804. $dateformat = 'yyyymmdd';
  1805. }
  1806. $query = qq|SELECT to_date(?, ?)
  1807. + ?::interval AS thisdate|;
  1808. @queryargs = ( $thisdate, $dateformat, $days );
  1809. }
  1810. else {
  1811. $query = qq|SELECT current_date AS thisdate|;
  1812. @queryargs = ();
  1813. }
  1814. $sth = $dbh->prepare($query);
  1815. $sth->execute(@queryargs);
  1816. ($thisdate) = $sth->fetchrow_array;
  1817. $dbh->commit;
  1818. $thisdate;
  1819. }
  1820. sub like {
  1821. my ( $self, $str ) = @_;
  1822. "%$str%";
  1823. }
  1824. sub redo_rows {
  1825. my ( $self, $flds, $new, $count, $numrows ) = @_;
  1826. my @ndx = ();
  1827. for ( 1 .. $count ) {
  1828. push @ndx, { num => $new->[ $_ - 1 ]->{runningnumber}, ndx => $_ };
  1829. }
  1830. my $i = 0;
  1831. # fill rows
  1832. foreach my $item ( sort { $a->{num} <=> $b->{num} } @ndx ) {
  1833. $i++;
  1834. $j = $item->{ndx} - 1;
  1835. for ( @{$flds} ) { $self->{"${_}_$i"} = $new->[$j]->{$_} }
  1836. }
  1837. # delete empty rows
  1838. for $i ( $count + 1 .. $numrows ) {
  1839. for ( @{$flds} ) { delete $self->{"${_}_$i"} }
  1840. }
  1841. }
  1842. sub get_partsgroup {
  1843. my ( $self, $myconfig, $p ) = @_;
  1844. my $dbh = $self->{dbh};
  1845. my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
  1846. FROM partsgroup pg
  1847. JOIN parts p ON (p.partsgroup_id = pg.id)|;
  1848. my $where;
  1849. my $sortorder = "partsgroup";
  1850. if ( $p->{searchitems} eq 'part' ) {
  1851. $where = qq| WHERE (p.inventory_accno_id > 0
  1852. AND p.income_accno_id > 0)|;
  1853. }
  1854. if ( $p->{searchitems} eq 'service' ) {
  1855. $where = qq| WHERE p.inventory_accno_id IS NULL|;
  1856. }
  1857. if ( $p->{searchitems} eq 'assembly' ) {
  1858. $where = qq| WHERE p.assembly = '1'|;
  1859. }
  1860. if ( $p->{searchitems} eq 'labor' ) {
  1861. $where =
  1862. qq| WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
  1863. }
  1864. if ( $p->{searchitems} eq 'nolabor' ) {
  1865. $where = qq| WHERE p.income_accno_id > 0|;
  1866. }
  1867. if ( $p->{all} ) {
  1868. $query = qq|SELECT id, partsgroup
  1869. FROM partsgroup|;
  1870. }
  1871. my @queryargs = ();
  1872. if ( $p->{language_code} ) {
  1873. $sortorder = "translation";
  1874. $query = qq|
  1875. SELECT DISTINCT pg.id, pg.partsgroup,
  1876. t.description AS translation
  1877. FROM partsgroup pg
  1878. JOIN parts p ON (p.partsgroup_id = pg.id)
  1879. LEFT JOIN translation t ON (t.trans_id = pg.id
  1880. AND t.language_code = ?)|;
  1881. @queryargs = ( $p->{language_code} );
  1882. }
  1883. $query .= qq| $where ORDER BY $sortorder|;
  1884. my $sth = $dbh->prepare($query);
  1885. $sth->execute(@queryargs) || $self->dberror($query);
  1886. $self->{all_partsgroup} = ();
  1887. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1888. push @{ $self->{all_partsgroup} }, $ref;
  1889. }
  1890. $sth->finish;
  1891. $dbh->commit;
  1892. }
  1893. sub update_status {
  1894. my ( $self, $myconfig ) = @_;
  1895. # no id return
  1896. return unless $self->{id};
  1897. my $dbh = $self->{dbh};
  1898. my %queued = split / +/, $self->{queued};
  1899. my $spoolfile =
  1900. ( $queued{ $self->{formname} } ) ? "'$queued{$self->{formname}}'" : undef;
  1901. my $query = qq|DELETE FROM status
  1902. WHERE formname = ?
  1903. AND trans_id = ?|;
  1904. $sth = $dbh->prepare($query);
  1905. $sth->execute( $self->{formname}, $self->{id} ) || $self->dberror($query);
  1906. $sth->finish;
  1907. my $printed = ( $self->{printed} =~ /$self->{formname}/ ) ? "1" : "0";
  1908. my $emailed = ( $self->{emailed} =~ /$self->{formname}/ ) ? "1" : "0";
  1909. $query = qq|
  1910. INSERT INTO status
  1911. (trans_id, printed, emailed, spoolfile, formname)
  1912. VALUES (?, ?, ?, ?, ?)|;
  1913. $sth = $dbh->prepare($query);
  1914. $sth->execute( $self->{id}, $printed, $emailed, $spoolfile,
  1915. $self->{formname} );
  1916. $sth->finish;
  1917. $dbh->commit;
  1918. }
  1919. sub save_status {
  1920. my ($self) = @_;
  1921. $dbh = $self->{dbh};
  1922. my $formnames = $self->{printed};
  1923. my $emailforms = $self->{emailed};
  1924. my $query = qq|DELETE FROM status
  1925. WHERE trans_id = ?|;
  1926. my $sth = $dbh->prepare($query);
  1927. $sth->execute( $self->{id} );
  1928. $sth->finish;
  1929. my %queued;
  1930. my $formname;
  1931. if ( $self->{queued} ) {
  1932. %queued = split / +/, $self->{queued};
  1933. foreach $formname ( keys %queued ) {
  1934. $printed = ( $self->{printed} =~ /$formname/ ) ? "1" : "0";
  1935. $emailed = ( $self->{emailed} =~ /$formname/ ) ? "1" : "0";
  1936. if ( $queued{$formname} ) {
  1937. $query = qq|
  1938. INSERT INTO status
  1939. (trans_id, printed, emailed,
  1940. spoolfile, formname)
  1941. VALUES (?, ?, ?, ?, ?)|;
  1942. $sth = $dbh->prepare($query);
  1943. $sth->execute( $self->{id}, $pinted, $emailed,
  1944. $queued{$formname}, $formname )
  1945. || $self->dberror($query);
  1946. $sth->finish;
  1947. }
  1948. $formnames =~ s/$formname//;
  1949. $emailforms =~ s/$formname//;
  1950. }
  1951. }
  1952. # save printed, emailed info
  1953. $formnames =~ s/^ +//g;
  1954. $emailforms =~ s/^ +//g;
  1955. my %status = ();
  1956. for ( split / +/, $formnames ) { $status{$_}{printed} = 1 }
  1957. for ( split / +/, $emailforms ) { $status{$_}{emailed} = 1 }
  1958. foreach my $formname ( keys %status ) {
  1959. $printed = ( $formnames =~ /$self->{formname}/ ) ? "1" : "0";
  1960. $emailed = ( $emailforms =~ /$self->{formname}/ ) ? "1" : "0";
  1961. $query = qq|
  1962. INSERT INTO status (trans_id, printed, emailed,
  1963. formname)
  1964. VALUES (?, ?, ?, ?)|;
  1965. $sth = $dbh->prepare($query);
  1966. $sth->execute( $self->{id}, $printed, $emailed, $formname );
  1967. $sth->finish;
  1968. }
  1969. $dbh->commit;
  1970. }
  1971. sub get_recurring {
  1972. my ($self) = @_;
  1973. $dbh = $self->{dbh};
  1974. my $query = qq/
  1975. SELECT s.*, se.formname || ':' || se.format AS emaila,
  1976. se.message, sp.formname || ':' ||
  1977. sp.format || ':' || sp.printer AS printa
  1978. FROM recurring s
  1979. LEFT JOIN recurringemail se ON (s.id = se.id)
  1980. LEFT JOIN recurringprint sp ON (s.id = sp.id)
  1981. WHERE s.id = ?/;
  1982. my $sth = $dbh->prepare($query);
  1983. $sth->execute( $self->{id} ) || $self->dberror($query);
  1984. for (qw(email print)) { $self->{"recurring$_"} = "" }
  1985. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1986. for ( keys %$ref ) { $self->{"recurring$_"} = $ref->{$_} }
  1987. $self->{recurringemail} .= "$ref->{emaila}:";
  1988. $self->{recurringprint} .= "$ref->{printa}:";
  1989. for (qw(emaila printa)) { delete $self->{"recurring$_"} }
  1990. }
  1991. $sth->finish;
  1992. chop $self->{recurringemail};
  1993. chop $self->{recurringprint};
  1994. if ( $self->{recurringstartdate} ) {
  1995. $self->{recurringreference} =
  1996. $self->escape( $self->{recurringreference}, 1 );
  1997. $self->{recurringmessage} =
  1998. $self->escape( $self->{recurringmessage}, 1 );
  1999. for (
  2000. qw(reference startdate repeat unit howmany
  2001. payment print email message)
  2002. )
  2003. {
  2004. $self->{recurring} .= qq|$self->{"recurring$_"},|;
  2005. }
  2006. chop $self->{recurring};
  2007. }
  2008. $dbh->commit;
  2009. }
  2010. sub save_recurring {
  2011. my ( $self, $dbh2, $myconfig ) = @_;
  2012. my $dbh = $self->{dbh};
  2013. my $query;
  2014. $query = qq|DELETE FROM recurring
  2015. WHERE id = ?|;
  2016. $sth = $dbh->prepare($query);
  2017. $sth->execute( $self->{id} ) || $self->dberror($query);
  2018. $query = qq|DELETE FROM recurringemail
  2019. WHERE id = ?|;
  2020. $sth = $dbh->prepare($query);
  2021. $sth->execute( $self->{id} ) || $self->dberror($query);
  2022. $query = qq|DELETE FROM recurringprint
  2023. WHERE id = ?|;
  2024. $sth = $dbh->prepare($query);
  2025. $sth->execute( $self->{id} ) || $self->dberror($query);
  2026. if ( $self->{recurring} ) {
  2027. my %s = ();
  2028. (
  2029. $s{reference}, $s{startdate}, $s{repeat},
  2030. $s{unit}, $s{howmany}, $s{payment},
  2031. $s{print}, $s{email}, $s{message}
  2032. ) = split /,/, $self->{recurring};
  2033. if ( $s{howmany} == 0 ) {
  2034. $self->error("Cannot set to recur 0 times");
  2035. }
  2036. for (qw(reference message)) { $s{$_} = $self->unescape( $s{$_} ) }
  2037. for (qw(repeat howmany payment)) { $s{$_} *= 1 }
  2038. # calculate enddate
  2039. my $advance = $s{repeat} * ( $s{howmany} - 1 );
  2040. my %interval;
  2041. $interval{'Pg'} =
  2042. "(date '$s{startdate}' + interval '$advance $s{unit}')";
  2043. $query = qq|SELECT $interval{$myconfig->{dbdriver}}|;
  2044. my ($enddate) = $dbh->selectrow_array($query);
  2045. # calculate nextdate
  2046. $query = qq|
  2047. SELECT current_date - date ? AS a,
  2048. date ? - current_date AS b|;
  2049. $sth = $dbh->prepare($query);
  2050. $sth->execute( $s{startdate}, $enddate );
  2051. my ( $a, $b ) = $sth->fetchrow_array;
  2052. if ( $a + $b ) {
  2053. $advance =
  2054. int( ( $a / ( $a + $b ) ) * ( $s{howmany} - 1 ) + 1 ) *
  2055. $s{repeat};
  2056. }
  2057. else {
  2058. $advance = 0;
  2059. }
  2060. my $nextdate = $enddate;
  2061. if ( $advance > 0 ) {
  2062. if ( $advance < ( $s{repeat} * $s{howmany} ) ) {
  2063. %interval = (
  2064. 'Pg' =>
  2065. "(date '$s{startdate}' + interval '$advance $s{unit}')",
  2066. 'DB2' => qq|(date ('$s{startdate}') + "$advance $s{unit}")|,
  2067. );
  2068. $interval{Oracle} = $interval{PgPP} = $interval{Pg};
  2069. $query = qq|SELECT $interval{$myconfig->{dbdriver}}|;
  2070. ($nextdate) = $dbh->selectrow_array($query);
  2071. }
  2072. }
  2073. else {
  2074. $nextdate = $s{startdate};
  2075. }
  2076. if ( $self->{recurringnextdate} ) {
  2077. $nextdate = $self->{recurringnextdate};
  2078. $query = qq|SELECT '$enddate' - date '$nextdate'|;
  2079. if ( $dbh->selectrow_array($query) < 0 ) {
  2080. undef $nextdate;
  2081. }
  2082. }
  2083. $self->{recurringpayment} *= 1;
  2084. $query = qq|
  2085. INSERT INTO recurring
  2086. (id, reference, startdate, enddate, nextdate,
  2087. repeat, unit, howmany, payment)
  2088. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)|;
  2089. $sth = $dbh->prepare($query);
  2090. $sth->execute(
  2091. $self->{id}, $s{reference}, $s{startdate},
  2092. $enddate, $nextdate, $s{repeat},
  2093. $s{unit}, $s{howmany}, $s{payment}
  2094. );
  2095. my @p;
  2096. my $p;
  2097. my $i;
  2098. my $sth;
  2099. if ( $s{email} ) {
  2100. # formname:format
  2101. @p = split /:/, $s{email};
  2102. $query =
  2103. qq|INSERT INTO recurringemail (id, formname, format, message)
  2104. VALUES (?, ?, ?, ?)|;
  2105. $sth = $dbh->prepare($query) || $self->dberror($query);
  2106. for ( $i = 0 ; $i <= $#p ; $i += 2 ) {
  2107. $sth->execute( $self->{id}, $p[$i], $p[ $i + 1 ], $s{message} );
  2108. }
  2109. $sth->finish;
  2110. }
  2111. if ( $s{print} ) {
  2112. # formname:format:printer
  2113. @p = split /:/, $s{print};
  2114. $query =
  2115. qq|INSERT INTO recurringprint (id, formname, format, printer)
  2116. VALUES (?, ?, ?, ?)|;
  2117. $sth = $dbh->prepare($query) || $self->dberror($query);
  2118. for ( $i = 0 ; $i <= $#p ; $i += 3 ) {
  2119. $p = ( $p[ $i + 2 ] ) ? $p[ $i + 2 ] : "";
  2120. $sth->execute( $self->{id}, $p[$i], $p[ $i + 1 ], $p );
  2121. }
  2122. $sth->finish;
  2123. }
  2124. }
  2125. $dbh->commit;
  2126. }
  2127. sub save_intnotes {
  2128. my ( $self, $myconfig, $vc ) = @_;
  2129. # no id return
  2130. return unless $self->{id};
  2131. my $dbh = $self->dbconnect($myconfig);
  2132. my $query = qq|UPDATE $vc SET intnotes = ? WHERE id = ?|;
  2133. $sth = $dbh->prepare($query);
  2134. $sth->execute( $self->{intnotes}, $self->{id} ) || $self->dberror($query);
  2135. $dbh->commit;
  2136. }
  2137. sub update_defaults {
  2138. my ( $self, $myconfig, $fld ) = @_;
  2139. if ( !$self->{dbh} && $self ) {
  2140. $self->db_init($myconfig);
  2141. }
  2142. my $dbh = $self->{dbh};
  2143. if ( !$self ) {
  2144. $dbh = $_[3];
  2145. }
  2146. my $query = qq|
  2147. SELECT value FROM defaults
  2148. WHERE setting_key = ? FOR UPDATE|;
  2149. $sth = $dbh->prepare($query);
  2150. $sth->execute($fld);
  2151. ($_) = $sth->fetchrow_array();
  2152. $_ = "0" unless $_;
  2153. # check for and replace
  2154. # <?lsmb DATE ?>, <?lsmb YYMMDD ?>, <?lsmb YEAR ?>, <?lsmb MONTH ?>, <?lsmb DAY ?> or variations of
  2155. # <?lsmb NAME 1 1 3 ?>, <?lsmb BUSINESS ?>, <?lsmb BUSINESS 10 ?>, <?lsmb CURR... ?>
  2156. # <?lsmb DESCRIPTION 1 1 3 ?>, <?lsmb ITEM 1 1 3 ?>, <?lsmb PARTSGROUP 1 1 3 ?> only for parts
  2157. # <?lsmb PHONE ?> for customer and vendors
  2158. my $num = $_;
  2159. ($num) = $num =~ /(\d+)/;
  2160. if ( defined $num ) {
  2161. my $incnum;
  2162. # if we have leading zeros check how long it is
  2163. if ( $num =~ /^0/ ) {
  2164. my $l = length $num;
  2165. $incnum = $num + 1;
  2166. $l -= length $incnum;
  2167. # pad it out with zeros
  2168. my $padzero = "0" x $l;
  2169. $incnum = ( "0" x $l ) . $incnum;
  2170. }
  2171. else {
  2172. $incnum = $num + 1;
  2173. }
  2174. s/$num/$incnum/;
  2175. }
  2176. my $dbvar = $_;
  2177. my $var = $_;
  2178. my $str;
  2179. my $param;
  2180. if (/<\?lsmb /) {
  2181. while (/<\?lsmb /) {
  2182. s/<\?lsmb .*? \?>//;
  2183. last unless $&;
  2184. $param = $&;
  2185. $str = "";
  2186. if ( $param =~ /<\?lsmb date \?>/i ) {
  2187. $str = (
  2188. $self->split_date(
  2189. $myconfig->{dateformat},
  2190. $self->{transdate}
  2191. )
  2192. )[0];
  2193. $var =~ s/$param/$str/;
  2194. }
  2195. if ( $param =~
  2196. /<\?lsmb (name|business|description|item|partsgroup|phone|custom)/i
  2197. )
  2198. {
  2199. my $fld = lc $&;
  2200. $fld =~ s/<\?lsmb //;
  2201. if ( $fld =~ /name/ ) {
  2202. if ( $self->{type} ) {
  2203. $fld = $self->{vc};
  2204. }
  2205. }
  2206. my $p = $param;
  2207. $p =~ s/(<|>|%)//g;
  2208. my @p = split / /, $p;
  2209. my @n = split / /, uc $self->{$fld};
  2210. if ( $#p > 0 ) {
  2211. for ( my $i = 1 ; $i <= $#p ; $i++ ) {
  2212. $str .= substr( $n[ $i - 1 ], 0, $p[$i] );
  2213. }
  2214. }
  2215. else {
  2216. ($str) = split /--/, $self->{$fld};
  2217. }
  2218. $var =~ s/$param/$str/;
  2219. $var =~ s/\W//g if $fld eq 'phone';
  2220. }
  2221. if ( $param =~ /<\?lsmb (yy|mm|dd)/i ) {
  2222. my $p = $param;
  2223. $p =~ s/(<|>|%)//g;
  2224. my $spc = $p;
  2225. $spc =~ s/\w//g;
  2226. $spc = substr( $spc, 0, 1 );
  2227. my %d = ( yy => 1, mm => 2, dd => 3 );
  2228. my @p = ();
  2229. my @a = $self->split_date( $myconfig->{dateformat},
  2230. $self->{transdate} );
  2231. for ( sort keys %d ) { push @p, $a[ $d{$_} ] if ( $p =~ /$_/ ) }
  2232. $str = join $spc, @p;
  2233. $var =~ s/$param/$str/;
  2234. }
  2235. if ( $param =~ /<\?lsmb curr/i ) {
  2236. $var =~ s/$param/$self->{currency}/;
  2237. }
  2238. }
  2239. }
  2240. $query = qq|
  2241. UPDATE defaults
  2242. SET value = ?
  2243. WHERE setting_key = ?|;
  2244. $sth = $dbh->prepare($query);
  2245. $sth->execute( $dbvar, $fld ) || $self->dberror($query);
  2246. $dbh->commit;
  2247. $var;
  2248. }
  2249. sub db_prepare_vars {
  2250. my $self = shift;
  2251. for (@_) {
  2252. if ( !$self->{$_} and $self->{$_} ne "0" ) {
  2253. undef $self->{$_};
  2254. }
  2255. }
  2256. }
  2257. sub split_date {
  2258. my ( $self, $dateformat, $date ) = @_;
  2259. my @d = localtime;
  2260. my $mm;
  2261. my $dd;
  2262. my $yy;
  2263. my $rv;
  2264. if ( !$date ) {
  2265. $dd = $d[3];
  2266. $mm = ++$d[4];
  2267. $yy = substr( $d[5], -2 );
  2268. $mm = substr( "0$mm", -2 );
  2269. $dd = substr( "0$dd", -2 );
  2270. }
  2271. if ( $dateformat =~ /^yy/ ) {
  2272. if ($date) {
  2273. if ( $date =~ /\D/ ) {
  2274. ( $yy, $mm, $dd ) = split /\D/, $date;
  2275. $mm *= 1;
  2276. $dd *= 1;
  2277. $mm = substr( "0$mm", -2 );
  2278. $dd = substr( "0$dd", -2 );
  2279. $yy = substr( $yy, -2 );
  2280. $rv = "$yy$mm$dd";
  2281. }
  2282. else {
  2283. $rv = $date;
  2284. }
  2285. }
  2286. else {
  2287. $rv = "$yy$mm$dd";
  2288. }
  2289. }
  2290. if ( $dateformat =~ /^mm/ ) {
  2291. if ($date) {
  2292. if ( $date =~ /\D/ ) {
  2293. ( $mm, $dd, $yy ) = split /\D/, $date;
  2294. $mm *= 1;
  2295. $dd *= 1;
  2296. $mm = substr( "0$mm", -2 );
  2297. $dd = substr( "0$dd", -2 );
  2298. $yy = substr( $yy, -2 );
  2299. $rv = "$mm$dd$yy";
  2300. }
  2301. else {
  2302. $rv = $date;
  2303. }
  2304. }
  2305. else {
  2306. $rv = "$mm$dd$yy";
  2307. }
  2308. }
  2309. if ( $dateformat =~ /^dd/ ) {
  2310. if ($date) {
  2311. if ( $date =~ /\D/ ) {
  2312. ( $dd, $mm, $yy ) = split /\D/, $date;
  2313. $mm *= 1;
  2314. $dd *= 1;
  2315. $mm = substr( "0$mm", -2 );
  2316. $dd = substr( "0$dd", -2 );
  2317. $yy = substr( $yy, -2 );
  2318. $rv = "$dd$mm$yy";
  2319. }
  2320. else {
  2321. $rv = $date;
  2322. }
  2323. }
  2324. else {
  2325. $rv = "$dd$mm$yy";
  2326. }
  2327. }
  2328. ( $rv, $yy, $mm, $dd );
  2329. }
  2330. sub from_to {
  2331. my ( $self, $yyyy, $mm, $interval ) = @_;
  2332. my @t;
  2333. my $dd = 1;
  2334. my $fromdate = "$yyyy-${mm}-01";
  2335. my $bd = 1;
  2336. if ( defined $interval ) {
  2337. if ( $interval == 12 ) {
  2338. $yyyy++;
  2339. }
  2340. else {
  2341. if ( ( $mm += $interval ) > 12 ) {
  2342. $mm -= 12;
  2343. $yyyy++;
  2344. }
  2345. if ( $interval == 0 ) {
  2346. @t = localtime(time);
  2347. $dd = $t[3];
  2348. $mm = $t[4] + 1;
  2349. $yyyy = $t[5] + 1900;
  2350. $bd = 0;
  2351. }
  2352. }
  2353. }
  2354. else {
  2355. if ( ++$mm > 12 ) {
  2356. $mm -= 12;
  2357. $yyyy++;
  2358. }
  2359. }
  2360. $mm--;
  2361. @t = localtime( Time::Local::timelocal( 0, 0, 0, $dd, $mm, $yyyy ) - $bd );
  2362. $t[4]++;
  2363. $t[4] = substr( "0$t[4]", -2 );
  2364. $t[3] = substr( "0$t[3]", -2 );
  2365. $t[5] += 1900;
  2366. ( $self->format_date($fromdate), $self->format_date("$t[5]-$t[4]-$t[3]") );
  2367. }
  2368. sub audittrail {
  2369. my ( $self, $dbh, $myconfig, $audittrail ) = @_;
  2370. # table, $reference, $formname, $action, $id, $transdate) = @_;
  2371. my $query;
  2372. my $rv;
  2373. my $disconnect;
  2374. if ( !$dbh ) {
  2375. $dbh = $self->{dbh};
  2376. }
  2377. # if we have an id add audittrail, otherwise get a new timestamp
  2378. my @queryargs;
  2379. if ( $audittrail->{id} ) {
  2380. $query = qq|
  2381. SELECT value FROM defaults
  2382. WHERE setting_key = 'audittrail'|;
  2383. if ( $dbh->selectrow_array($query) ) {
  2384. my ( $null, $employee_id ) = $self->get_employee($dbh);
  2385. if ( $self->{audittrail} && !$myconfig ) {
  2386. chop $self->{audittrail};
  2387. my @a = split /\|/, $self->{audittrail};
  2388. my %newtrail = ();
  2389. my $key;
  2390. my $i;
  2391. my @flds = qw(tablename reference formname action transdate);
  2392. # put into hash and remove dups
  2393. while (@a) {
  2394. $key = "$a[2]$a[3]";
  2395. $i = 0;
  2396. $newtrail{$key} = { map { $_ => $a[ $i++ ] } @flds };
  2397. splice @a, 0, 5;
  2398. }
  2399. $query = qq|
  2400. INSERT INTO audittrail
  2401. (trans_id, tablename, reference,
  2402. formname, action, transdate,
  2403. employee_id)
  2404. VALUES (?, ?, ?, ?, ?, ?, ?)|;
  2405. my $sth = $dbh->prepare($query) || $self->dberror($query);
  2406. foreach $key (
  2407. sort {
  2408. $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate}
  2409. } keys %newtrail
  2410. )
  2411. {
  2412. $i = 2;
  2413. $sth->bind_param( 1, $audittrail->{id} );
  2414. for (@flds) {
  2415. $sth->bind_param( $i++, $newtrail{$key}{$_} );
  2416. }
  2417. $sth->bind_param( $i++, $employee_id );
  2418. $sth->execute || $self->dberror;
  2419. $sth->finish;
  2420. }
  2421. }
  2422. if ( $audittrail->{transdate} ) {
  2423. $query = qq|
  2424. INSERT INTO audittrail (
  2425. trans_id, tablename, reference,
  2426. formname, action, employee_id,
  2427. transdate)
  2428. VALUES (?, ?, ?, ?, ?, ?, ?)|;
  2429. @queryargs = (
  2430. $audittrail->{id}, $audittrail->{tablename},
  2431. $audittrail->{reference}, $audittrail->{formname},
  2432. $audittrail->{action}, $employee_id,
  2433. $audittrail->{transdate}
  2434. );
  2435. }
  2436. else {
  2437. $query = qq|
  2438. INSERT INTO audittrail
  2439. (trans_id, tablename, reference,
  2440. formname, action, employee_id)
  2441. VALUES (?, ?, ?, ?, ?, ?)|;
  2442. @queryargs = (
  2443. $audittrail->{id}, $audittrail->{tablename},
  2444. $audittrail->{reference}, $audittrail->{formname},
  2445. $audittrail->{action}, $employee_id,
  2446. );
  2447. }
  2448. $sth = $dbh->prepare($query);
  2449. $sth->execute(@queryargs) || $self->dberror($query);
  2450. }
  2451. }
  2452. else {
  2453. $query = qq|SELECT current_timestamp|;
  2454. my ($timestamp) = $dbh->selectrow_array($query);
  2455. $rv =
  2456. "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
  2457. }
  2458. $dbh->commit;
  2459. $rv;
  2460. }
  2461. 1;