summaryrefslogtreecommitdiff
path: root/LedgerSMB/GL.pm
blob: 25b0206d0b61dc6ccff56a97143f4b6449d97b82 (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:
  23. #
  24. #======================================================================
  25. #
  26. # This file has undergone whitespace cleanup.
  27. #
  28. #======================================================================
  29. #
  30. # General ledger backend code
  31. #
  32. #======================================================================
  33. package GL;
  34. sub delete_transaction {
  35. my ( $self, $myconfig, $form ) = @_;
  36. # connect to database
  37. my $dbh = $form->{dbh};
  38. my %audittrail = (
  39. tablename => 'gl',
  40. reference => $form->{reference},
  41. formname => 'transaction',
  42. action => 'deleted',
  43. id => $form->{id}
  44. );
  45. $form->audittrail( $dbh, "", \%audittrail );
  46. my $id = $dbh->quote( $form->{id} );
  47. my $query = qq|DELETE FROM gl WHERE id = $id|;
  48. $dbh->do($query) || $form->dberror($query);
  49. $query = qq|DELETE FROM acc_trans WHERE trans_id = $id|;
  50. $dbh->do($query) || $form->dberror($query);
  51. # commit and redirect
  52. my $rc = $dbh->commit;
  53. $rc;
  54. }
  55. sub post_transaction {
  56. my ( $self, $myconfig, $form ) = @_;
  57. $form->{reference} = $form->update_defaults( $myconfig, 'glnumber', $dbh )
  58. unless $form->{reference};
  59. my $null;
  60. my $project_id;
  61. my $department_id;
  62. my $i;
  63. # connect to database, turn off AutoCommit
  64. my $dbh = $form->{dbh};
  65. my $query;
  66. my $sth;
  67. my $id = $dbh->quote( $form->{id} );
  68. if ($form->{separate_duties}){
  69. $form->{approved} = '0';
  70. }
  71. if ( $form->{id} ) {
  72. $query = qq|SELECT id FROM gl WHERE id = $id|;
  73. ( $form->{id} ) = $dbh->selectrow_array($query);
  74. if ( $form->{id} ) {
  75. # delete individual transactions
  76. $query = qq|
  77. DELETE FROM acc_trans WHERE trans_id = $id|;
  78. $dbh->do($query) || $form->dberror($query);
  79. }
  80. }
  81. if ( !$form->{id} ) {
  82. my $uid = localtime;
  83. $uid .= "$$";
  84. $query = qq|
  85. INSERT INTO gl (reference)
  86. VALUES ('$uid')|;
  87. $sth = $dbh->prepare($query);
  88. $sth->execute() || $form->dberror($query);
  89. $query = qq|
  90. SELECT id
  91. FROM gl
  92. WHERE reference = '$uid'|;
  93. ( $form->{id} ) = $dbh->selectrow_array($query);
  94. }
  95. ( $null, $department_id ) = split /--/, $form->{department};
  96. $department_id *= 1;
  97. $form->{reference} ||= $form->{id};
  98. $query = qq|
  99. UPDATE gl
  100. SET reference = | . $dbh->quote( $form->{reference} ) . qq|,
  101. description = | . $dbh->quote( $form->{description} ) . qq|,
  102. notes = | . $dbh->quote( $form->{notes} ) . qq|,
  103. transdate = ?,
  104. department_id = ?
  105. WHERE id = ?|;
  106. if (defined $form->{approved}) {
  107. my $query = qq| UPDATE gl SET approved = ? WHERE id = ?|;
  108. $dbh->prepare($query)->execute($form->{approved}, $form->{id})
  109. || $form->dberror($query);
  110. if (!$form->{approved} and $form->{batch_id}){
  111. if (not defined $form->{batch_id}){
  112. $form->error($locale->text('Batch ID Missing'));
  113. }
  114. my $query = qq|
  115. INSERT INTO voucher (batch_id, trans_id, batch_class)
  116. VALUES (?, ?, (select id FROM batch_class
  117. WHERE class = ?))|;
  118. my $sth2 = $dbh->prepare($query);
  119. $sth2->execute($form->{batch_id}, $form->{id}, 'gl') ||
  120. $form->dberror($query);
  121. }
  122. }
  123. $sth = $dbh->prepare($query);
  124. print STDERR $query;
  125. $sth->execute( $form->{transdate}, $department_id, $form->{id} )
  126. || $form->dberror($query);
  127. my $amount = 0;
  128. my $posted = 0;
  129. my $debit;
  130. my $credit;
  131. # insert acc_trans transactions
  132. for $i ( 1 .. $form->{rowcount} ) {
  133. $debit = $form->parse_amount( $myconfig, $form->{"debit_$i"} );
  134. $credit = $form->parse_amount( $myconfig, $form->{"credit_$i"} );
  135. # extract accno
  136. ($accno) = split( /--/, $form->{"accno_$i"} );
  137. if ($credit) {
  138. $amount = $credit;
  139. $posted = 0;
  140. }
  141. if ($debit) {
  142. $amount = $debit * -1;
  143. $posted = 0;
  144. }
  145. # add the record
  146. if ( !$posted ) {
  147. ( $null, $project_id ) = split /--/, $form->{"projectnumber_$i"};
  148. $project_id ||= undef;
  149. $query = qq|
  150. INSERT INTO acc_trans
  151. (trans_id, chart_id, amount,
  152. transdate, source, project_id,
  153. fx_transaction, memo, cleared)
  154. VALUES (?, (SELECT id
  155. FROM chart
  156. WHERE accno = ?),
  157. ?, ?, ?, ?, ?, ?, ?)|;
  158. $sth = $dbh->prepare($query);
  159. $sth->execute(
  160. $form->{id}, $accno,
  161. $amount, $form->{transdate},
  162. $form->{"source_$i"}, $project_id,
  163. ($form->{"fx_transaction_$i"} || 0), $form->{"memo_$i"},
  164. ($form->{"cleared_$i"} || 0)
  165. ) || $form->dberror($query);
  166. $posted = 1;
  167. }
  168. }
  169. my %audittrail = (
  170. tablename => 'gl',
  171. reference => $form->{reference},
  172. formname => 'transaction',
  173. action => 'posted',
  174. id => $form->{id}
  175. );
  176. $form->audittrail( $dbh, "", \%audittrail );
  177. $form->save_recurring( $dbh, $myconfig );
  178. # commit and redirect
  179. my $rc = $dbh->commit;
  180. $rc;
  181. }
  182. sub all_transactions {
  183. my ( $self, $myconfig, $form ) = @_;
  184. # connect to database
  185. my $dbh = $form->{dbh};
  186. my $query;
  187. my $sth;
  188. my $var;
  189. my $null;
  190. my ( $glwhere, $arwhere, $apwhere ) = ( "1 = 1", "1 = 1", "1 = 1" );
  191. if ( $form->{reference} ne "" ) {
  192. $var = $dbh->quote( $form->like( lc $form->{reference} ) );
  193. $glwhere .= " AND lower(g.reference) LIKE $var";
  194. $arwhere .= " AND lower(a.invnumber) LIKE $var";
  195. $apwhere .= " AND lower(a.invnumber) LIKE $var";
  196. }
  197. if ( $form->{department} ne "" ) {
  198. ( $null, $var ) = split /--/, $form->{department};
  199. $var = $dbh->quote($var);
  200. $glwhere .= " AND g.department_id = $var";
  201. $arwhere .= " AND a.department_id = $var";
  202. $apwhere .= " AND a.department_id = $var";
  203. }
  204. if ( $form->{source} ne "" ) {
  205. $var = $dbh->quote( $form->like( lc $form->{source} ) );
  206. $glwhere .= " AND lower(ac.source) LIKE $var";
  207. $arwhere .= " AND lower(ac.source) LIKE $var";
  208. $apwhere .= " AND lower(ac.source) LIKE $var";
  209. }
  210. if ( $form->{memo} ne "" ) {
  211. $var = $dbh->quote( $form->like( lc $form->{memo} ) );
  212. $glwhere .= " AND lower(ac.memo) LIKE $var";
  213. $arwhere .= " AND lower(ac.memo) LIKE $var";
  214. $apwhere .= " AND lower(ac.memo) LIKE $var";
  215. }
  216. ( $form->{datefrom}, $form->{dateto} ) =
  217. $form->from_to( $form->{year}, $form->{month}, $form->{interval} )
  218. if $form->{year} && $form->{month};
  219. if ( $form->{datefrom} ) {
  220. $glwhere .= " AND ac.transdate >= " . $dbh->quote( $form->{datefrom} );
  221. $arwhere .= " AND ac.transdate >= " . $dbh->quote( $form->{datefrom} );
  222. $apwhere .= " AND ac.transdate >= " . $dbh->quote( $form->{datefrom} );
  223. }
  224. if ( $form->{dateto} ) {
  225. $glwhere .= " AND ac.transdate <= " . $dbh->quote( $form->{dateto} );
  226. $arwhere .= " AND ac.transdate <= " . $dbh->quote( $form->{dateto} );
  227. $apwhere .= " AND ac.transdate <= " . $dbh->quote( $form->{dateto} );
  228. }
  229. if ( $form->{amountfrom} ) {
  230. $glwhere .=
  231. " AND abs(ac.amount) >= " . $dbh->quote( $form->{amountfrom} );
  232. $arwhere .=
  233. " AND abs(ac.amount) >= " . $dbh->quote( $form->{amountfrom} );
  234. $apwhere .=
  235. " AND abs(ac.amount) >= " . $dbh->quote( $form->{amountfrom} );
  236. }
  237. if ( $form->{amountto} ) {
  238. $glwhere .=
  239. " AND abs(ac.amount) <= " . $dbh->quote( $form->{amountto} );
  240. $arwhere .=
  241. " AND abs(ac.amount) <= " . $dbh->quote( $form->{amountto} );
  242. $apwhere .=
  243. " AND abs(ac.amount) <= " . $dbh->quote( $form->{amountto} );
  244. }
  245. if ( $form->{description} ) {
  246. $var = $dbh->quote( $form->like( lc $form->{description} ) );
  247. $glwhere .= " AND lower(g.description) LIKE $var";
  248. $arwhere .= " AND (lower(ct.name) LIKE $var
  249. OR lower(ac.memo) LIKE $var
  250. OR a.id IN (SELECT DISTINCT trans_id
  251. FROM invoice
  252. WHERE lower(description) LIKE $var))";
  253. $apwhere .= " AND (lower(ct.name) LIKE $var
  254. OR lower(ac.memo) LIKE $var
  255. OR a.id IN (SELECT DISTINCT trans_id
  256. FROM invoice
  257. WHERE lower(description) LIKE $var))";
  258. }
  259. if ( $form->{notes} ) {
  260. $var = $dbh->quote( $form->like( lc $form->{notes} ) );
  261. $glwhere .= " AND lower(g.notes) LIKE $var";
  262. $arwhere .= " AND lower(a.notes) LIKE $var";
  263. $apwhere .= " AND lower(a.notes) LIKE $var";
  264. }
  265. if ( $form->{accno} ) {
  266. $var = $dbh->quote( $form->{accno} );
  267. $glwhere .= " AND c.accno = $var";
  268. $arwhere .= " AND c.accno = $var";
  269. $apwhere .= " AND c.accno = $var";
  270. }
  271. if ( $form->{gifi_accno} ) {
  272. $var = $dbh->quote( $form->{gifi_accno} );
  273. $glwhere .= " AND c.gifi_accno = $var";
  274. $arwhere .= " AND c.gifi_accno = $var";
  275. $apwhere .= " AND c.gifi_accno = $var";
  276. }
  277. if ( $form->{category} ne 'X' ) {
  278. $var = $dbh->quote( $form->{category} );
  279. $glwhere .= " AND c.category = $var";
  280. $arwhere .= " AND c.category = $var";
  281. $apwhere .= " AND c.category = $var";
  282. }
  283. if ( $form->{accno} ) {
  284. my $accno = $dbh->quote( $form->{accno} );
  285. # get category for account
  286. $query = qq|SELECT category, link, contra, description
  287. FROM chart
  288. WHERE accno = $accno|;
  289. (
  290. $form->{category}, $form->{link}, $form->{contra},
  291. $form->{account_description}
  292. ) = $dbh->selectrow_array($query);
  293. if ( $form->{datefrom} ) {
  294. $query = qq|
  295. SELECT SUM(ac.amount)
  296. FROM acc_trans ac
  297. JOIN chart c ON (ac.chart_id = c.id)
  298. WHERE c.accno = $accno
  299. AND ac.transdate < date | . $dbh->quote( $form->{datefrom} );
  300. ( $form->{balance} ) = $dbh->selectrow_array($query);
  301. }
  302. }
  303. if ( $form->{gifi_accno} ) {
  304. my $gifi = $dbh->quote( $form->{gifi_accno} );
  305. # get category for account
  306. $query = qq|SELECT c.category, c.link, c.contra, g.description
  307. FROM chart c
  308. LEFT JOIN gifi g ON (g.accno = c.gifi_accno)
  309. WHERE c.gifi_accno = $gifi|;
  310. (
  311. $form->{category}, $form->{link}, $form->{contra},
  312. $form->{gifi_account_description}
  313. ) = $dbh->selectrow_array($query);
  314. if ( $form->{datefrom} ) {
  315. $query = qq|
  316. SELECT SUM(ac.amount)
  317. FROM acc_trans ac
  318. JOIN chart c ON (ac.chart_id = c.id)
  319. WHERE c.gifi_accno = $gifi
  320. AND ac.transdate < date | . $dbh->quote( $form->{datefrom} );
  321. ( $form->{balance} ) = $dbh->selectrow_array($query);
  322. }
  323. }
  324. my $false = 'FALSE';
  325. my %ordinal = (
  326. id => 1,
  327. reference => 4,
  328. description => 5,
  329. transdate => 6,
  330. source => 7,
  331. accno => 9,
  332. department => 15,
  333. memo => 16
  334. );
  335. my @a = ( id, transdate, reference, source, description, accno );
  336. my $sortorder = $form->sort_order( \@a, \%ordinal );
  337. my $chart_id;
  338. if ($form->{chart_id}){
  339. $chart_id = $dbh->quote($form->{chart_id});
  340. } else {
  341. $chart_id = 'NULL';
  342. }
  343. if (!defined $form->{approved}){
  344. $approved = 'true';
  345. } elsif ($form->{approved} eq 'all') {
  346. $approved = 'NULL';
  347. } else {
  348. $approved = $dbh->quote($form->{approved});
  349. }
  350. my $query = qq|SELECT g.id, 'gl' AS type, $false AS invoice, g.reference,
  351. g.description, ac.transdate, ac.source,
  352. ac.amount, c.accno, c.gifi_accno, g.notes, c.link,
  353. '' AS till, ac.cleared, d.description AS department,
  354. ac.memo, c.description AS accname
  355. FROM gl AS g
  356. JOIN acc_trans ac ON (g.id = ac.trans_id)
  357. JOIN chart c ON (ac.chart_id = c.id)
  358. LEFT JOIN department d ON (d.id = g.department_id)
  359. WHERE $glwhere
  360. AND (ac.chart_id = $chart_id OR
  361. $chart_id IS NULL)
  362. AND ($approved IS NULL OR
  363. $approved =
  364. (ac.approved AND g.approved))
  365. UNION ALL
  366. SELECT a.id, 'ar' AS type, a.invoice, a.invnumber,
  367. e.name, ac.transdate, ac.source,
  368. ac.amount, c.accno, c.gifi_accno, a.notes, c.link,
  369. a.till, ac.cleared, d.description AS department,
  370. ac.memo, c.description AS accname
  371. FROM ar a
  372. JOIN acc_trans ac ON (a.id = ac.trans_id)
  373. JOIN chart c ON (ac.chart_id = c.id)
  374. JOIN entity_credit_account ec ON (a.entity_credit_account = ec.id)
  375. JOIN entity e ON (ec.entity_id = e.id)
  376. LEFT JOIN department d ON (d.id = a.department_id)
  377. WHERE $arwhere
  378. AND (ac.chart_id = $chart_id OR
  379. $chart_id IS NULL)
  380. AND ($approved IS NULL OR
  381. $approved =
  382. (ac.approved AND a.approved))
  383. UNION ALL
  384. SELECT a.id, 'ap' AS type, a.invoice, a.invnumber,
  385. e.name, ac.transdate, ac.source,
  386. ac.amount, c.accno, c.gifi_accno, a.notes, c.link,
  387. a.till, ac.cleared, d.description AS department,
  388. ac.memo, c.description AS accname
  389. FROM ap a
  390. JOIN acc_trans ac ON (a.id = ac.trans_id)
  391. JOIN chart c ON (ac.chart_id = c.id)
  392. JOIN entity_credit_account ec ON (a.entity_credit_account = ec.id)
  393. JOIN entity e ON (ec.entity_id = e.id)
  394. LEFT JOIN department d ON (d.id = a.department_id)
  395. WHERE $apwhere
  396. AND (ac.chart_id = $chart_id OR
  397. $chart_id IS NULL)
  398. AND ($approved IS NULL OR
  399. $approved =
  400. (ac.approved AND a.approved))
  401. ORDER BY $sortorder|;
  402. my $sth = $dbh->prepare($query);
  403. $sth->execute || $form->dberror($query);
  404. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  405. # gl
  406. if ( $ref->{type} eq "gl" ) {
  407. $ref->{module} = "gl";
  408. }
  409. # ap
  410. if ( $ref->{type} eq "ap" ) {
  411. if ( $ref->{invoice} ) {
  412. $ref->{module} = "ir";
  413. }
  414. else {
  415. $ref->{module} = "ap";
  416. }
  417. }
  418. # ar
  419. if ( $ref->{type} eq "ar" ) {
  420. if ( $ref->{invoice} ) {
  421. $ref->{module} = ( $ref->{till} ) ? "ps" : "is";
  422. }
  423. else {
  424. $ref->{module} = "ar";
  425. }
  426. }
  427. if ( $ref->{amount} < 0 ) {
  428. $ref->{debit} = $ref->{amount} * -1;
  429. $ref->{credit} = 0;
  430. }
  431. else {
  432. $ref->{credit} = $ref->{amount};
  433. $ref->{debit} = 0;
  434. }
  435. push @{ $form->{GL} }, $ref;
  436. }
  437. $sth->finish;
  438. $dbh->commit;
  439. }
  440. sub transaction {
  441. my ( $self, $myconfig, $form ) = @_;
  442. my ( $query, $sth, $ref );
  443. # connect to database
  444. my $dbh = $form->{dbh};
  445. if ( $form->{id} ) {
  446. $query = "SELECT setting_key, value
  447. FROM defaults
  448. WHERE setting_key IN
  449. ('closedto',
  450. 'revtrans',
  451. 'separate_duties')";
  452. $sth = $dbh->prepare($query);
  453. $sth->execute || $form->dberror($query);
  454. my $results = $sth->fetchall_hashref('setting_key');
  455. $form->{closedto} = $results->{'closedto'}->{'value'};
  456. $form->{revtrans} = $results->{'revtrans'}->{'value'};
  457. $sth->finish;
  458. $query = qq|SELECT g.*, d.description AS department
  459. FROM gl g
  460. LEFT JOIN department d ON (d.id = g.department_id)
  461. WHERE g.id = ?|;
  462. $sth = $dbh->prepare($query);
  463. $sth->execute( $form->{id} ) || $form->dberror($query);
  464. $ref = $sth->fetchrow_hashref(NAME_lc);
  465. for ( keys %$ref ) { $form->{$_} = $ref->{$_} }
  466. $sth->finish;
  467. # retrieve individual rows
  468. $query = qq|SELECT ac.*, c.accno, c.description, p.projectnumber
  469. FROM acc_trans ac
  470. JOIN chart c ON (ac.chart_id = c.id)
  471. LEFT JOIN project p ON (p.id = ac.project_id)
  472. WHERE ac.trans_id = ?
  473. ORDER BY accno|;
  474. $sth = $dbh->prepare($query);
  475. $sth->execute( $form->{id} ) || $form->dberror($query);
  476. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  477. if ( $ref->{fx_transaction} ) {
  478. $form->{transfer} = 1;
  479. }
  480. push @{ $form->{GL} }, $ref;
  481. }
  482. # get recurring transaction
  483. $form->get_recurring($dbh);
  484. }
  485. else {
  486. $query = "SELECT current_date AS transdate, setting_key, value
  487. FROM defaults
  488. WHERE setting_key IN
  489. ('closedto',
  490. 'separate_duties',
  491. 'revtrans')";
  492. $sth = $dbh->prepare($query);
  493. $sth->execute || $form->dberror($query);
  494. my $results = $sth->fetchall_hashref('setting_key');
  495. $form->{separate_duties} = $results->{'separate_duties'}->{'value'};
  496. $form->{closedto} = $results->{'closedto'}->{'value'};
  497. $form->{revtrans} = $results->{'revtrans'}->{'value'};
  498. if (!$form->{transdate}){
  499. $form->{transdate} = $results->{'revtrans'}->{'transdate'};
  500. }
  501. }
  502. $sth->finish;
  503. # get chart of accounts
  504. $query = qq|SELECT accno,description
  505. FROM chart
  506. WHERE charttype = 'A'
  507. ORDER BY accno|;
  508. $sth = $dbh->prepare($query);
  509. $sth->execute || $form->dberror($query);
  510. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  511. push @{ $form->{all_accno} }, $ref;
  512. }
  513. $sth->finish;
  514. # get departments
  515. $form->all_departments( $myconfig, $dbh );
  516. # get projects
  517. $form->all_projects( $myconfig, $dbh, $form->{transdate} );
  518. $dbh->commit;
  519. }
  520. 1;