summaryrefslogtreecommitdiff
path: root/LedgerSMB/CA.pm
blob: c7a9b0bde1d0f6fff835ce9eb3288905d3858c67 (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 $query = qq|
  40. SELECT accno, SUM(acc_trans.amount) AS amount
  41. FROM chart, acc_trans
  42. WHERE chart.id = acc_trans.chart_id
  43. GROUP BY accno|;
  44. my $sth = $dbh->prepare($query);
  45. $sth->execute || $form->dberror($query);
  46. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  47. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  48. $amount{ $ref->{accno} } = $ref->{amount};
  49. }
  50. $sth->finish;
  51. $query = qq|
  52. SELECT accno, description
  53. FROM gifi|;
  54. $sth = $dbh->prepare($query);
  55. $sth->execute || $form->dberror($query);
  56. my $gifi = ();
  57. while ( my ( $accno, $description ) = $sth->fetchrow_array ) {
  58. $gifi{$accno} = $description;
  59. }
  60. $sth->finish;
  61. $query = qq|
  62. SELECT c.id, c.accno, c.description, c.charttype,
  63. c.gifi_accno, c.category, c.link
  64. FROM chart c
  65. ORDER BY accno|;
  66. $sth = $dbh->prepare($query);
  67. $sth->execute || $form->dberror($query);
  68. while ( my $ca = $sth->fetchrow_hashref(NAME_lc) ) {
  69. $ca->{amount} = $amount{ $ca->{accno} };
  70. $ca->{gifi_description} = $gifi{ $ca->{gifi_accno} };
  71. if ( $ca->{amount} < 0 ) {
  72. $ca->{debit} = $ca->{amount} * -1;
  73. }
  74. else {
  75. $ca->{credit} = $ca->{amount};
  76. }
  77. push @{ $form->{CA} }, $ca;
  78. }
  79. $sth->finish;
  80. $dbh->commit;
  81. }
  82. sub all_transactions {
  83. my ( $self, $myconfig, $form ) = @_;
  84. # connect to database
  85. my $dbh = $form->{dbh};
  86. # get chart_id
  87. my $query = qq|
  88. SELECT id
  89. FROM chart
  90. WHERE accno = ?|;
  91. my $accno = $form->{accno};
  92. if ( $form->{accounttype} eq 'gifi' ) {
  93. $query = qq|
  94. SELECT id
  95. FROM chart
  96. WHERE gifi_accno = ?|;
  97. $accno = $form->{gifi_accno};
  98. }
  99. my $sth = $dbh->prepare($query);
  100. $sth->execute($accno) || $form->dberror($query);
  101. my @id = ();
  102. while ( my ($id) = $sth->fetchrow_array ) {
  103. push @id, $id;
  104. }
  105. $sth->finish;
  106. my $fromdate_where;
  107. my $todate_where;
  108. ( $form->{fromdate}, $form->{todate} ) =
  109. $form->from_to( $form->{year}, $form->{month}, $form->{interval} )
  110. if $form->{year} && $form->{month};
  111. my $fdate;
  112. if ( $form->{fromdate} ) {
  113. $fromdate_where = qq| AND ac.transdate >= ? |;
  114. $fdate = $form->{fromdate};
  115. }
  116. my $tdate;
  117. if ( $form->{todate} ) {
  118. $todate_where .= qq| AND ac.transdate <= ? |;
  119. $tdate = $form->{todate};
  120. }
  121. my $false = 'FALSE';
  122. # Oracle workaround, use ordinal positions
  123. my %ordinal = (
  124. transdate => 4,
  125. reference => 2,
  126. description => 3
  127. );
  128. my @a = qw(transdate reference description);
  129. my $sortorder = $form->sort_order( \@a, \%ordinal );
  130. my $null;
  131. my $department_id;
  132. my $dpt_where;
  133. my $dpt_join;
  134. my $union;
  135. ( $null, $department_id ) = split /--/, $form->{department};
  136. my $d_id;
  137. if ($department_id) {
  138. $dpt_join = qq| JOIN department t ON (t.id = a.department_id) |;
  139. $dpt_where = qq| AND t.id = ? |;
  140. $d_id = $department_id;
  141. }
  142. my $project;
  143. my $project_id;
  144. my $p_id;
  145. if ( $form->{projectnumber} ) {
  146. ( $null, $project_id ) = split /--/, $form->{projectnumber};
  147. $project = qq| AND ac.project_id = ? |;
  148. $p_id = $project_id;
  149. }
  150. @queryargs = ();
  151. if ( $form->{accno} || $form->{gifi_accno} ) {
  152. # get category for account
  153. $query = qq|
  154. SELECT description, category, link, contra
  155. FROM chart
  156. WHERE accno = ?|;
  157. $accno = $form->{accno};
  158. if ( $form->{accounttype} eq 'gifi' ) {
  159. $query = qq|
  160. SELECT description, category, link, contra
  161. FROM chart
  162. WHERE gifi_accno = ?
  163. AND charttype = 'A'|;
  164. $accno = $form->{gifi_accno};
  165. }
  166. $sth = $dbh->prepare($query);
  167. $sth->execute($accno);
  168. (
  169. $form->{description}, $form->{category},
  170. $form->{link}, $form->{contra}
  171. ) = $sth->fetchrow_array;
  172. if ( $form->{fromdate} ) {
  173. if ($department_id) {
  174. # get beginning balance
  175. $query = "";
  176. $union = "";
  177. for (qw(ar ap gl)) {
  178. if ( $form->{accounttype} eq 'gifi' ) {
  179. $query = qq|
  180. $union
  181. SELECT SUM(ac.amount)
  182. FROM acc_trans ac
  183. JOIN $_ a
  184. ON
  185. (a.id =
  186. ac.trans_id)
  187. JOIN chart c
  188. ON
  189. (ac.chart_id =
  190. c.id)
  191. WHERE c.gifi_accno = ?
  192. AND ac.transdate
  193. < ?
  194. AND
  195. a.department_id
  196. = ?
  197. $project |;
  198. push @queryargs, $form->{gifi_accno}, $form->{fromdate},
  199. $form->{department_id};
  200. if ($p_id) {
  201. push @queryargs, $p_id;
  202. }
  203. }
  204. else {
  205. $query .= qq|
  206. $union
  207. SELECT SUM(ac.amount)
  208. FROM acc_trans ac
  209. JOIN $_ a ON
  210. (a.id =
  211. ac.trans_id)
  212. JOIN chart c ON
  213. (ac.chart_id =
  214. c.id)
  215. WHERE c.accno = ?
  216. AND ac.transdate
  217. < ?
  218. AND
  219. a.department_id
  220. = ?
  221. $project |;
  222. push @queryargs, $form->{accno}, $form->{fromdate},
  223. $department_id;
  224. if ($p_id) {
  225. push @queryargs, $p_id;
  226. }
  227. }
  228. $union = qq| UNION ALL |;
  229. }
  230. }
  231. else {
  232. if ( $form->{accounttype} eq 'gifi' ) {
  233. $query = qq|
  234. SELECT SUM(ac.amount)
  235. FROM acc_trans ac
  236. JOIN chart c ON
  237. (ac.chart_id = c.id)
  238. WHERE c.gifi_accno = ?
  239. AND ac.transdate < ?
  240. $project |;
  241. @queryargs = ( $form->{gifi_accno}, $form->{fromdate} );
  242. if ($p_id) {
  243. push @query_ags, $p_id;
  244. }
  245. }
  246. else {
  247. $query = qq|
  248. SELECT SUM(ac.amount)
  249. FROM acc_trans ac
  250. JOIN chart c
  251. ON (ac.chart_id = c.id)
  252. WHERE c.accno = ?
  253. AND ac.transdate < ?
  254. $project |;
  255. @queryargs = ( $form->{accno}, $form->{fromdate} );
  256. if ($p_id) {
  257. push @queryargs, $p_id;
  258. }
  259. }
  260. }
  261. $sth = $dbh->prepare($query);
  262. $sth->execute(@queryargs);
  263. my @balance = $sth->fetchrow_array;
  264. $form->db_parse_numeric(sth=>$sth, arrayref=>\@balance);
  265. ( $form->{balance} ) = @balance;
  266. $sth->finish;
  267. @queryargs = ();
  268. }
  269. }
  270. $query = "";
  271. $union = "";
  272. foreach my $id (@id) {
  273. # get all transactions
  274. $query .= qq|
  275. $union
  276. SELECT a.id, a.reference, a.description, ac.transdate,
  277. $false AS invoice, ac.amount, 'gl' as module,
  278. ac.cleared, ac.source, '' AS till, ac.chart_id
  279. FROM gl a
  280. JOIN acc_trans ac ON (ac.trans_id = a.id)
  281. $dpt_join
  282. WHERE ac.chart_id = ?
  283. $fromdate_where
  284. $todate_where
  285. $dpt_where
  286. $project|;
  287. push @queryargs, $id;
  288. if ($fdate) {
  289. push @queryargs, $fdate;
  290. }
  291. if ($tdate) {
  292. push @queryargs, $tdate;
  293. }
  294. if ($d_id) {
  295. push @queryargs, $d_id;
  296. }
  297. if ($p_id) {
  298. push @queryargs, $p_id;
  299. }
  300. $query .= qq|
  301. UNION ALL
  302. SELECT a.id, a.invnumber, c.name, ac.transdate,
  303. a.invoice, ac.amount, 'ar' as module, ac.cleared,
  304. ac.source, a.till, ac.chart_id
  305. FROM ar a
  306. JOIN acc_trans ac ON (ac.trans_id = a.id)
  307. JOIN customer c ON (a.customer_id = c.id)
  308. $dpt_join
  309. WHERE ac.chart_id = ?
  310. $fromdate_where
  311. $todate_where
  312. $dpt_where
  313. $project|;
  314. push @queryargs, $id;
  315. if ($fdate) {
  316. push @queryargs, $fdate;
  317. }
  318. if ($tdate) {
  319. push @queryargs, $tdate;
  320. }
  321. if ($d_id) {
  322. push @queryargs, $d_id;
  323. }
  324. if ($p_id) {
  325. push @queryargs, $p_id;
  326. }
  327. $query .= qq|
  328. UNION ALL
  329. SELECT a.id, a.invnumber, v.name, ac.transdate,
  330. a.invoice, ac.amount, 'ap' as module, ac.cleared,
  331. ac.source, a.till, ac.chart_id
  332. FROM ap a
  333. JOIN acc_trans ac ON (ac.trans_id = a.id)
  334. JOIN vendor v ON (a.vendor_id = v.id)
  335. $dpt_join
  336. WHERE ac.chart_id = ?
  337. $fromdate_where
  338. $todate_where
  339. $dpt_where
  340. $project |;
  341. push @queryargs, $id;
  342. if ($fdate) {
  343. push @queryargs, $fdate;
  344. }
  345. if ($tdate) {
  346. push @queryargs, $tdate;
  347. }
  348. if ($d_id) {
  349. push @queryargs, $d_id;
  350. }
  351. if ($p_id) {
  352. push @queryargs, $p_id;
  353. }
  354. $union = qq| UNION ALL |;
  355. }
  356. $query .= qq| ORDER BY $sortorder |;
  357. $sth = $dbh->prepare($query);
  358. $sth->execute(@queryargs) || $form->dberror($query);
  359. $query = qq|SELECT c.id, c.accno
  360. FROM chart c
  361. JOIN acc_trans ac ON (ac.chart_id = c.id)
  362. WHERE ac.amount >= 0
  363. AND (c.link = 'AR' OR c.link = 'AP')
  364. AND ac.trans_id = ?|;
  365. my $dr = $dbh->prepare($query) || $form->dberror($query);
  366. $query = qq|SELECT c.id, c.accno
  367. FROM chart c
  368. JOIN acc_trans ac ON (ac.chart_id = c.id)
  369. WHERE ac.amount < 0
  370. AND (c.link = 'AR' OR c.link = 'AP')
  371. AND ac.trans_id = ?|;
  372. my $cr = $dbh->prepare($query) || $form->dberror($query);
  373. my $accno;
  374. my $chart_id;
  375. my %accno;
  376. while ( my $ca = $sth->fetchrow_hashref(NAME_lc) ) {
  377. $form->db_parse_numeric(sth=>$sth, hashref=>$ca);
  378. # gl
  379. if ( $ca->{module} eq "gl" ) {
  380. $ca->{module} = "gl";
  381. }
  382. # ap
  383. if ( $ca->{module} eq "ap" ) {
  384. $ca->{module} = ( $ca->{invoice} ) ? 'ir' : 'ap';
  385. $ca->{module} = 'ps' if $ca->{till};
  386. }
  387. # ar
  388. if ( $ca->{module} eq "ar" ) {
  389. $ca->{module} = ( $ca->{invoice} ) ? 'is' : 'ar';
  390. $ca->{module} = 'ps' if $ca->{till};
  391. }
  392. if ( $ca->{amount} ) {
  393. %accno = ();
  394. if ( $ca->{amount} < 0 ) {
  395. $ca->{debit} = $ca->{amount} * -1;
  396. $ca->{credit} = 0;
  397. $dr->execute( $ca->{id} );
  398. $ca->{accno} = ();
  399. while ( my @dr_results = $dr->fetchrow_array ) {
  400. $form->db_parse_numeric(sth=>$dr, arrayref=>\@dr_results);
  401. ($chart_id, $accno) = @dr_results;
  402. $accno{$accno} = 1 if $chart_id ne $ca->{chart_id};
  403. }
  404. $dr->finish;
  405. for ( sort keys %accno ) { push @{ $ca->{accno} }, "$_ " }
  406. }
  407. else {
  408. $ca->{credit} = $ca->{amount};
  409. $ca->{debit} = 0;
  410. $cr->execute( $ca->{id} );
  411. $ca->{accno} = ();
  412. while ( my @cr_results = $cr->fetchrow_array ) {
  413. $form->db_parse_numeric(sth=>$cr, arrayref=>\@cr_results);
  414. ($chart_id, $accno) = @cr_results;
  415. $accno{$accno} = 1 if $chart_id ne $ca->{chart_id};
  416. }
  417. $cr->finish;
  418. for ( keys %accno ) { push @{ $ca->{accno} }, "$_ " }
  419. }
  420. push @{ $form->{CA} }, $ca;
  421. }
  422. }
  423. $sth->finish;
  424. $dbh->commit;
  425. }
  426. 1;