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