summaryrefslogtreecommitdiff
path: root/LedgerSMB/CA.pm
blob: bd61e11382f1ff1589117dd187e2bd24ae4bc371 (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) 2001
  18. #
  19. # Author: DWS Systems Inc.
  20. # Web: http://www.sql-ledger.org
  21. #
  22. # Contributors:
  23. #
  24. #======================================================================
  25. #
  26. # This file has undergone whitespace cleanup.
  27. #
  28. #======================================================================
  29. #
  30. # chart of accounts
  31. #
  32. #======================================================================
  33. package CA;
  34. sub all_accounts {
  35. my ( $self, $myconfig, $form ) = @_;
  36. my $amount = ();
  37. # connect to database
  38. my $dbh = $form->{dbh};
  39. my $approved = ($form->{approved})? 'TRUE' : 'FALSE';
  40. my $query = qq|
  41. SELECT accno, SUM(acc_trans.amount) AS amount
  42. FROM chart
  43. JOIN acc_trans ON (chart.id = acc_trans.chart_id)
  44. JOIN transactions ON (acc_trans.trans_id = transactions.id)
  45. JOIN (SELECT id, approved, 'ap' AS tablename FROM ap
  46. UNION
  47. SELECT id, approved, 'ar' as tablename FROM ar
  48. UNION
  49. SELECT id, approved, 'gl' as tablename FROM gl
  50. ) g ON (g.id = acc_trans.trans_id
  51. AND transactions.table_name = g.tablename)
  52. WHERE ($approved OR acc_trans.approved)
  53. AND (g.approved OR $approved)
  54. GROUP BY accno|;
  55. my $sth = $dbh->prepare($query);
  56. $sth->execute || $form->dberror($query);
  57. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  58. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  59. $amount{ $ref->{accno} } = $ref->{amount};
  60. }
  61. $sth->finish;
  62. $query = qq|
  63. SELECT accno, description
  64. FROM gifi|;
  65. $sth = $dbh->prepare($query);
  66. $sth->execute || $form->dberror($query);
  67. my $gifi = ();
  68. while ( my ( $accno, $description ) = $sth->fetchrow_array ) {
  69. $gifi{$accno} = $description;
  70. }
  71. $sth->finish;
  72. $query = qq|
  73. SELECT c.id, c.accno, c.description, c.charttype,
  74. c.gifi_accno, c.category, c.link
  75. FROM chart c
  76. ORDER BY accno|;
  77. $sth = $dbh->prepare($query);
  78. $sth->execute || $form->dberror($query);
  79. while ( my $ca = $sth->fetchrow_hashref(NAME_lc) ) {
  80. $ca->{amount} = $amount{ $ca->{accno} };
  81. $ca->{gifi_description} = $gifi{ $ca->{gifi_accno} };
  82. if ( $ca->{amount} < 0 ) {
  83. $ca->{debit} = $ca->{amount} * -1;
  84. }
  85. else {
  86. $ca->{credit} = $ca->{amount};
  87. }
  88. push @{ $form->{CA} }, $ca;
  89. }
  90. $sth->finish;
  91. $dbh->commit;
  92. }
  93. 1;