summaryrefslogtreecommitdiff
path: root/LedgerSMB/CA.pm
blob: f26547a3f92dd5e85c9781247686c7207ae29c9c (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. #
  5. # Copyright (C) 2006
  6. # This work contains copyrighted information from a number of sources all used
  7. # with permission.
  8. #
  9. # This file contains source code included with or based on SQL-Ledger which
  10. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  11. # under the GNU General Public License version 2 or, at your option, any later
  12. # version. For a full list including contact information of contributors,
  13. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  14. #
  15. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  16. # Copyright (C) 2001
  17. #
  18. # Author: DWS Systems Inc.
  19. # Web: http://www.sql-ledger.org
  20. #
  21. # Contributors:
  22. #
  23. #======================================================================
  24. #
  25. # This file has undergone whitespace cleanup.
  26. #
  27. #======================================================================
  28. #
  29. # chart of accounts
  30. #
  31. #======================================================================
  32. package CA;
  33. sub all_accounts {
  34. my ($self, $myconfig, $form) = @_;
  35. my $amount = ();
  36. # connect to database
  37. my $dbh = $form->dbconnect($myconfig);
  38. my $query = qq|SELECT accno, SUM(acc_trans.amount) AS amount
  39. FROM chart, acc_trans
  40. WHERE chart.id = acc_trans.chart_id
  41. GROUP BY accno|;
  42. my $sth = $dbh->prepare($query);
  43. $sth->execute || $form->dberror($query);
  44. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  45. $amount{$ref->{accno}} = $ref->{amount}
  46. }
  47. $sth->finish;
  48. $query = qq|SELECT accno, description
  49. FROM gifi|;
  50. $sth = $dbh->prepare($query);
  51. $sth->execute || $form->dberror($query);
  52. my $gifi = ();
  53. while (my ($accno, $description) = $sth->fetchrow_array) {
  54. $gifi{$accno} = $description;
  55. }
  56. $sth->finish;
  57. $query = qq|SELECT c.id, c.accno, c.description, c.charttype,
  58. c.gifi_accno, c.category, c.link
  59. FROM chart c
  60. ORDER BY accno|;
  61. $sth = $dbh->prepare($query);
  62. $sth->execute || $form->dberror($query);
  63. while (my $ca = $sth->fetchrow_hashref(NAME_lc)) {
  64. $ca->{amount} = $amount{$ca->{accno}};
  65. $ca->{gifi_description} = $gifi{$ca->{gifi_accno}};
  66. if ($ca->{amount} < 0) {
  67. $ca->{debit} = $ca->{amount} * -1;
  68. } else {
  69. $ca->{credit} = $ca->{amount};
  70. }
  71. push @{ $form->{CA} }, $ca;
  72. }
  73. $sth->finish;
  74. $dbh->disconnect;
  75. }
  76. sub all_transactions {
  77. my ($self, $myconfig, $form) = @_;
  78. # connect to database
  79. my $dbh = $form->dbconnect($myconfig);
  80. # get chart_id
  81. my $query = qq|SELECT id
  82. FROM chart
  83. WHERE accno = '$form->{accno}'|;
  84. if ($form->{accounttype} eq 'gifi') {
  85. $query = qq|SELECT id
  86. FROM chart
  87. WHERE gifi_accno = '$form->{gifi_accno}'|;
  88. }
  89. my $sth = $dbh->prepare($query);
  90. $sth->execute || $form->dberror($query);
  91. my @id = ();
  92. while (my ($id) = $sth->fetchrow_array) {
  93. push @id, $id;
  94. }
  95. $sth->finish;
  96. my $fromdate_where;
  97. my $todate_where;
  98. ($form->{fromdate}, $form->{todate}) = $form->from_to($form->{year}, $form->{month}, $form->{interval}) if $form->{year} && $form->{month};
  99. if ($form->{fromdate}) {
  100. $fromdate_where = qq| AND ac.transdate >= '$form->{fromdate}' |;
  101. }
  102. if ($form->{todate}) {
  103. $todate_where .= qq| AND ac.transdate <= '$form->{todate}' |;
  104. }
  105. my $false = ($myconfig->{dbdriver} =~ /Pg/) ? FALSE : q|'0'|;
  106. # Oracle workaround, use ordinal positions
  107. my %ordinal = ( transdate => 4,
  108. reference => 2,
  109. description => 3 );
  110. my @a = qw(transdate reference description);
  111. my $sortorder = $form->sort_order(\@a, \%ordinal);
  112. my $null;
  113. my $department_id;
  114. my $dpt_where;
  115. my $dpt_join;
  116. my $union;
  117. ($null, $department_id) = split /--/, $form->{department};
  118. if ($department_id) {
  119. $dpt_join = qq| JOIN department t ON (t.id = a.department_id) |;
  120. $dpt_where = qq| AND t.id = $department_id |;
  121. }
  122. my $project;
  123. my $project_id;
  124. if ($form->{projectnumber}) {
  125. ($null, $project_id) = split /--/, $form->{projectnumber};
  126. $project = qq| AND ac.project_id = $project_id |;
  127. }
  128. if ($form->{accno} || $form->{gifi_accno}) {
  129. # get category for account
  130. $query = qq|SELECT description, category, link, contra
  131. FROM chart
  132. WHERE accno = '$form->{accno}'|;
  133. if ($form->{accounttype} eq 'gifi') {
  134. $query = qq|SELECT description, category, link, contra
  135. FROM chart
  136. WHERE gifi_accno = '$form->{gifi_accno}'
  137. AND charttype = 'A'|;
  138. }
  139. ($form->{description}, $form->{category}, $form->{link}, $form->{contra}) = $dbh->selectrow_array($query);
  140. if ($form->{fromdate}) {
  141. if ($department_id) {
  142. # get beginning balance
  143. $query = "";
  144. $union = "";
  145. for (qw(ar ap gl)) {
  146. if ($form->{accounttype} eq 'gifi') {
  147. $query = qq| $union
  148. SELECT SUM(ac.amount)
  149. FROM acc_trans ac
  150. JOIN $_ a ON (a.id = ac.trans_id)
  151. JOIN chart c ON (ac.chart_id = c.id)
  152. WHERE c.gifi_accno = '$form->{gifi_accno}'
  153. AND ac.transdate < '$form->{fromdate}'
  154. AND a.department_id = $department_id
  155. $project |;
  156. } else {
  157. $query .= qq| $union
  158. SELECT SUM(ac.amount)
  159. FROM acc_trans ac
  160. JOIN $_ a ON (a.id = ac.trans_id)
  161. JOIN chart c ON (ac.chart_id = c.id)
  162. WHERE c.accno = '$form->{accno}'
  163. AND ac.transdate < '$form->{fromdate}'
  164. AND a.department_id = $department_id
  165. $project |;
  166. }
  167. $union = qq| UNION ALL |;
  168. }
  169. } else {
  170. if ($form->{accounttype} eq 'gifi') {
  171. $query = qq|SELECT SUM(ac.amount)
  172. FROM acc_trans ac
  173. JOIN chart c ON (ac.chart_id = c.id)
  174. WHERE c.gifi_accno = '$form->{gifi_accno}'
  175. AND ac.transdate < '$form->{fromdate}'
  176. $project |;
  177. } else {
  178. $query = qq|SELECT SUM(ac.amount)
  179. FROM acc_trans ac
  180. JOIN chart c ON (ac.chart_id = c.id)
  181. WHERE c.accno = '$form->{accno}'
  182. AND ac.transdate < '$form->{fromdate}'
  183. $project |;
  184. }
  185. }
  186. ($form->{balance}) = $dbh->selectrow_array($query);
  187. }
  188. }
  189. $query = "";
  190. $union = "";
  191. foreach my $id (@id) {
  192. # get all transactions
  193. $query .= qq|$union
  194. SELECT a.id, a.reference, a.description, ac.transdate,
  195. $false AS invoice, ac.amount, 'gl' as module, ac.cleared,
  196. ac.source, '' AS till, ac.chart_id
  197. FROM gl a
  198. JOIN acc_trans ac ON (ac.trans_id = a.id)
  199. $dpt_join
  200. WHERE ac.chart_id = $id
  201. $fromdate_where
  202. $todate_where
  203. $dpt_where
  204. $project
  205. UNION ALL
  206. SELECT a.id, a.invnumber, c.name, ac.transdate,
  207. a.invoice, ac.amount, 'ar' as module, ac.cleared,
  208. ac.source,
  209. a.till, ac.chart_id
  210. FROM ar a
  211. JOIN acc_trans ac ON (ac.trans_id = a.id)
  212. JOIN customer c ON (a.customer_id = c.id)
  213. $dpt_join
  214. WHERE ac.chart_id = $id
  215. $fromdate_where
  216. $todate_where
  217. $dpt_where
  218. $project
  219. UNION ALL
  220. SELECT a.id, a.invnumber, v.name, ac.transdate,
  221. a.invoice, ac.amount, 'ap' as module, ac.cleared,
  222. ac.source, a.till, ac.chart_id
  223. FROM ap a
  224. JOIN acc_trans ac ON (ac.trans_id = a.id)
  225. JOIN vendor v ON (a.vendor_id = v.id)
  226. $dpt_join
  227. WHERE ac.chart_id = $id
  228. $fromdate_where
  229. $todate_where
  230. $dpt_where
  231. $project |;
  232. $union = qq| UNION ALL |;
  233. }
  234. $query .= qq| ORDER BY $sortorder |;
  235. $sth = $dbh->prepare($query);
  236. $sth->execute || $form->dberror($query);
  237. $query = qq|SELECT c.id, c.accno
  238. FROM chart c
  239. JOIN acc_trans ac ON (ac.chart_id = c.id)
  240. WHERE ac.amount >= 0
  241. AND (c.link = 'AR' OR c.link = 'AP')
  242. AND ac.trans_id = ?|;
  243. my $dr = $dbh->prepare($query) || $form->dberror($query);
  244. $query = qq|SELECT c.id, c.accno
  245. FROM chart c
  246. JOIN acc_trans ac ON (ac.chart_id = c.id)
  247. WHERE ac.amount < 0
  248. AND (c.link = 'AR' OR c.link = 'AP')
  249. AND ac.trans_id = ?|;
  250. my $cr = $dbh->prepare($query) || $form->dberror($query);
  251. my $accno;
  252. my $chart_id;
  253. my %accno;
  254. while (my $ca = $sth->fetchrow_hashref(NAME_lc)) {
  255. # gl
  256. if ($ca->{module} eq "gl") {
  257. $ca->{module} = "gl";
  258. }
  259. # ap
  260. if ($ca->{module} eq "ap") {
  261. $ca->{module} = ($ca->{invoice}) ? 'ir' : 'ap';
  262. $ca->{module} = 'ps' if $ca->{till};
  263. }
  264. # ar
  265. if ($ca->{module} eq "ar") {
  266. $ca->{module} = ($ca->{invoice}) ? 'is' : 'ar';
  267. $ca->{module} = 'ps' if $ca->{till};
  268. }
  269. if ($ca->{amount}) {
  270. %accno = ();
  271. if ($ca->{amount} < 0) {
  272. $ca->{debit} = $ca->{amount} * -1;
  273. $ca->{credit} = 0;
  274. $dr->execute($ca->{id});
  275. $ca->{accno} = ();
  276. while (($chart_id, $accno) = $dr->fetchrow_array) {
  277. $accno{$accno} = 1 if $chart_id ne $ca->{chart_id};
  278. }
  279. $dr->finish;
  280. for (sort keys %accno) { push @{ $ca->{accno} }, "$_ " }
  281. } else {
  282. $ca->{credit} = $ca->{amount};
  283. $ca->{debit} = 0;
  284. $cr->execute($ca->{id});
  285. $ca->{accno} = ();
  286. while (($chart_id, $accno) = $cr->fetchrow_array) {
  287. $accno{$accno} = 1 if $chart_id ne $ca->{chart_id};
  288. }
  289. $cr->finish;
  290. for (keys %accno) { push @{ $ca->{accno} }, "$_ " }
  291. }
  292. push @{ $form->{CA} }, $ca;
  293. }
  294. }
  295. $sth->finish;
  296. $dbh->disconnect;
  297. }
  298. 1;