summaryrefslogtreecommitdiff
path: root/LedgerSMB/RP.pm
blob: c46ee788b8fd68b3fcb421290fd43932751aeb0c (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. # backend code for reports
  31. #
  32. #======================================================================
  33. package RP;
  34. sub inventory_activity {
  35. my ( $self, $myconfig, $form ) = @_;
  36. ( $form->{fromdate}, $form->{todate} ) =
  37. $form->from_to( $form->{fromyear}, $form->{frommonth}, $form->{interval} )
  38. if $form->{fromyear} && $form->{frommonth};
  39. my $dbh = $form->{dbh};
  40. unless ( $form->{sort_col} ) {
  41. $form->{sort_col} = 'partnumber';
  42. }
  43. my $where = '';
  44. if ( $form->{fromdate} ) {
  45. $where .=
  46. "AND coalesce(ar.duedate, ap.duedate) >= "
  47. . $dbh->quote( $form->{fromdate} );
  48. }
  49. if ( $form->{todate} ) {
  50. $where .=
  51. "AND coalesce(ar.duedate, ap.duedate) < "
  52. . $dbh->quote( $form->{todate} ) . " ";
  53. }
  54. if ( $form->{partnumber} ) {
  55. $where .=
  56. qq| AND p.partnumber ILIKE |
  57. . $dbh->quote( '%' . "$form->{partnumber}%" );
  58. }
  59. if ( $form->{description} ) {
  60. $where .=
  61. q| AND p.description ILIKE |
  62. . $dbh->quote( '%' . "$form->{description}%" );
  63. }
  64. $where =~ s/^\s?AND/WHERE/;
  65. my $query = qq|
  66. SELECT min(p.description) AS description,
  67. min(p.partnumber) AS partnumber, sum(
  68. CASE WHEN i.qty > 0 THEN i.qty ELSE 0 END) AS sold,
  69. sum (CASE WHEN i.qty > 0
  70. THEN i.sellprice * i.qty
  71. ELSE 0 END) AS revenue,
  72. sum(CASE WHEN i.qty < 0 THEN i.qty * -1 ELSE 0 END)
  73. AS received, sum(CASE WHEN i.qty < 0
  74. THEN i.sellprice * i.qty * -1
  75. ELSE 0 END) as expenses,
  76. min(p.id) as id
  77. FROM invoice i
  78. JOIN parts p ON (i.parts_id = p.id)
  79. LEFT JOIN ar ON (ar.id = i.trans_id)
  80. LEFT JOIN ap ON (ap.id = i.trans_id)
  81. $where
  82. GROUP BY i.parts_id
  83. ORDER BY $form->{sort_col}|;
  84. my $sth = $dbh->prepare($query) || $form->dberror($query);
  85. $sth->execute() || $form->dberror($query);
  86. @cols = qw(description sold revenue partnumber received expense);
  87. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  88. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  89. $ref->{net_income} = $ref->{revenue} - $ref->{expense};
  90. map { $ref->{$_} =~ s/^\s*// } @cols;
  91. map { $ref->{$_} =~ s/\s*$// } @cols;
  92. push @{ $form->{TB} }, $ref;
  93. }
  94. $sth->finish;
  95. $dbh->commit;
  96. }
  97. sub yearend_statement {
  98. my ( $self, $myconfig, $form ) = @_;
  99. my $dbh = $form->{dbh};
  100. # if todate < existing yearends, delete GL and yearends
  101. my $query = qq|SELECT trans_id FROM yearend WHERE transdate >= ?|;
  102. my $sth = $dbh->prepare($query);
  103. $sth->execute( $form->{todate} ) || $form->dberror($query);
  104. my @trans_id = ();
  105. my $id;
  106. while ( ($id) = $sth->fetchrow_array ) {
  107. push @trans_id, $id;
  108. }
  109. $sth->finish;
  110. $query = qq|DELETE FROM gl WHERE id = ?|;
  111. $sth = $dbh->prepare($query) || $form->dberror($query);
  112. $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
  113. my $ath = $dbh->prepare($query) || $form->dberror($query);
  114. foreach $id (@trans_id) {
  115. $sth->execute($id);
  116. $ath->execute($id);
  117. $sth->finish;
  118. $ath->finish;
  119. }
  120. my $last_period = 0;
  121. my @categories = qw(I E);
  122. my $category;
  123. $form->{decimalplaces} *= 1;
  124. &get_accounts( $dbh, 0, $form->{fromdate}, $form->{todate}, $form,
  125. \@categories );
  126. $dbh->commit;
  127. # now we got $form->{I}{accno}{ }
  128. # and $form->{E}{accno}{ }
  129. my %account = (
  130. 'I' => {
  131. 'label' => 'income',
  132. 'labels' => 'income',
  133. 'ml' => 1
  134. },
  135. 'E' => {
  136. 'label' => 'expense',
  137. 'labels' => 'expenses',
  138. 'ml' => -1
  139. }
  140. );
  141. foreach $category (@categories) {
  142. foreach $key ( sort keys %{ $form->{$category} } ) {
  143. if ( $form->{$category}{$key}{charttype} eq 'A' ) {
  144. $form->{"total_$account{$category}{labels}_this_period"} +=
  145. $form->{$category}{$key}{this} * $account{$category}{ml};
  146. }
  147. }
  148. }
  149. # totals for income and expenses
  150. $form->{total_income_this_period} =
  151. $form->round_amount( $form->{total_income_this_period},
  152. $form->{decimalplaces} );
  153. $form->{total_expenses_this_period} =
  154. $form->round_amount( $form->{total_expenses_this_period},
  155. $form->{decimalplaces} );
  156. # total for income/loss
  157. $form->{total_this_period} =
  158. $form->{total_income_this_period} - $form->{total_expenses_this_period};
  159. }
  160. sub income_statement {
  161. my ( $self, $myconfig, $form ) = @_;
  162. my $dbh = $form->{dbh};
  163. my $last_period = 0;
  164. my @categories = qw(I E);
  165. my $category;
  166. $form->{decimalplaces} *= 1;
  167. if ( !( $form->{fromdate} || $form->{todate} ) ) {
  168. if ( $form->{fromyear} && $form->{frommonth} ) {
  169. ( $form->{fromdate}, $form->{todate} ) =
  170. $form->from_to( $form->{fromyear}, $form->{frommonth},
  171. $form->{interval} );
  172. }
  173. }
  174. &get_accounts( $dbh, $last_period, $form->{fromdate}, $form->{todate},
  175. $form, \@categories, 1 );
  176. if ( !( $form->{comparefromdate} || $form->{comparetodate} ) ) {
  177. if ( $form->{compareyear} && $form->{comparemonth} ) {
  178. ( $form->{comparefromdate}, $form->{comparetodate} ) =
  179. $form->from_to( $form->{compareyear}, $form->{comparemonth},
  180. $form->{interval} );
  181. }
  182. }
  183. # if there are any compare dates
  184. if ( $form->{comparefromdate} || $form->{comparetodate} ) {
  185. $last_period = 1;
  186. &get_accounts(
  187. $dbh, $last_period,
  188. $form->{comparefromdate},
  189. $form->{comparetodate},
  190. $form, \@categories, 1
  191. );
  192. }
  193. $dbh->commit;
  194. # now we got $form->{I}{accno}{ }
  195. # and $form->{E}{accno}{ }
  196. my %account = (
  197. 'I' => {
  198. 'label' => 'income',
  199. 'labels' => 'income',
  200. 'ml' => 1
  201. },
  202. 'E' => {
  203. 'label' => 'expense',
  204. 'labels' => 'expenses',
  205. 'ml' => -1
  206. }
  207. );
  208. my $str;
  209. foreach $category (@categories) {
  210. foreach $key ( sort keys %{ $form->{$category} } ) {
  211. # push description onto array
  212. $str = ( $form->{l_heading} ) ? $form->{padding} : "";
  213. if ( $form->{$category}{$key}{charttype} eq "A" ) {
  214. $str .=
  215. ( $form->{l_accno} )
  216. ? "$form->{$category}{$key}{accno} - $form->{$category}{$key}{description}"
  217. : "$form->{$category}{$key}{description}";
  218. }
  219. if ( $form->{$category}{$key}{charttype} eq "H" ) {
  220. if ( $account{$category}{subtotal}
  221. && $form->{l_subtotal} )
  222. {
  223. $dash = "- ";
  224. push(
  225. @{ $form->{"$account{$category}{label}_account"} },
  226. "$str$form->{bold}$account{$category}{subdescription}$form->{endbold}"
  227. );
  228. push(
  229. @{
  230. $form->{"$account{$category}{labels}_this_period"}
  231. },
  232. $form->format_amount(
  233. $myconfig,
  234. $account{$category}{subthis} *
  235. $account{$category}{ml},
  236. $form->{decimalplaces},
  237. $dash
  238. )
  239. );
  240. if ($last_period) {
  241. # Chris T: Giving up on
  242. # Formatting this one :-(
  243. push(
  244. @{
  245. $form->{
  246. "$account{$category}{labels}_last_period"}
  247. },
  248. $form->format_amount(
  249. $myconfig,
  250. $account{$category}{sublast} *
  251. $account{$category}{ml},
  252. $form->{decimalplaces},
  253. $dash
  254. )
  255. );
  256. }
  257. }
  258. $str =
  259. "$form->{br}$form->{bold}$form->{$category}{$key}{description}$form->{endbold}";
  260. $account{$category}{subthis} = $form->{$category}{$key}{this};
  261. $account{$category}{sublast} = $form->{$category}{$key}{last};
  262. $account{$category}{subdescription} =
  263. $form->{$category}{$key}{description};
  264. $account{$category}{subtotal} = 1;
  265. $form->{$category}{$key}{this} = 0;
  266. $form->{$category}{$key}{last} = 0;
  267. next unless $form->{l_heading};
  268. $dash = " ";
  269. }
  270. push( @{ $form->{"$account{$category}{label}_account"} }, $str );
  271. if ( $form->{$category}{$key}{charttype} eq 'A' ) {
  272. $form->{"total_$account{$category}{labels}_this_period"} +=
  273. $form->{$category}{$key}{this} * $account{$category}{ml};
  274. $dash = "- ";
  275. }
  276. push(
  277. @{ $form->{"$account{$category}{labels}_this_period"} },
  278. $form->format_amount(
  279. $myconfig,
  280. $form->{$category}{$key}{this} * $account{$category}{ml},
  281. $form->{decimalplaces}, $dash
  282. )
  283. );
  284. # add amount or - for last period
  285. if ($last_period) {
  286. $form->{"total_$account{$category}{labels}_last_period"} +=
  287. $form->{$category}{$key}{last} * $account{$category}{ml};
  288. push(
  289. @{ $form->{"$account{$category}{labels}_last_period"} },
  290. $form->format_amount(
  291. $myconfig,
  292. $form->{$category}{$key}{last} *
  293. $account{$category}{ml},
  294. $form->{decimalplaces},
  295. $dash
  296. )
  297. );
  298. }
  299. }
  300. $str = ( $form->{l_heading} ) ? $form->{padding} : "";
  301. if ( $account{$category}{subtotal} && $form->{l_subtotal} ) {
  302. push(
  303. @{ $form->{"$account{$category}{label}_account"} },
  304. "$str$form->{bold}$account{$category}{subdescription}$form->{endbold}"
  305. );
  306. push(
  307. @{ $form->{"$account{$category}{labels}_this_period"} },
  308. $form->format_amount(
  309. $myconfig,
  310. $account{$category}{subthis} * $account{$category}{ml},
  311. $form->{decimalplaces}, $dash
  312. )
  313. );
  314. if ($last_period) {
  315. push(
  316. @{ $form->{"$account{$category}{labels}_last_period"} },
  317. $form->format_amount(
  318. $myconfig,
  319. $account{$category}{sublast} * $account{$category}{ml},
  320. $form->{decimalplaces},
  321. $dash
  322. )
  323. );
  324. }
  325. }
  326. }
  327. # totals for income and expenses
  328. $form->{total_income_this_period} =
  329. $form->round_amount( $form->{total_income_this_period},
  330. $form->{decimalplaces} );
  331. $form->{total_expenses_this_period} =
  332. $form->round_amount( $form->{total_expenses_this_period},
  333. $form->{decimalplaces} );
  334. # total for income/loss
  335. $form->{total_this_period} =
  336. $form->{total_income_this_period} - $form->{total_expenses_this_period};
  337. if ($last_period) {
  338. # total for income/loss
  339. $form->{total_last_period} = $form->format_amount(
  340. $myconfig,
  341. $form->{total_income_last_period} -
  342. $form->{total_expenses_last_period},
  343. $form->{decimalplaces},
  344. "- "
  345. );
  346. # totals for income and expenses for last_period
  347. $form->{total_income_last_period} = $form->format_amount(
  348. $myconfig,
  349. $form->{total_income_last_period},
  350. $form->{decimalplaces}, "- "
  351. );
  352. $form->{total_expenses_last_period} = $form->format_amount(
  353. $myconfig,
  354. $form->{total_expenses_last_period},
  355. $form->{decimalplaces}, "- "
  356. );
  357. }
  358. $form->{total_income_this_period} = $form->format_amount(
  359. $myconfig,
  360. $form->{total_income_this_period},
  361. $form->{decimalplaces}, "- "
  362. );
  363. $form->{total_expenses_this_period} = $form->format_amount(
  364. $myconfig,
  365. $form->{total_expenses_this_period},
  366. $form->{decimalplaces}, "- "
  367. );
  368. $form->{total_this_period} = $form->format_amount(
  369. $myconfig,
  370. $form->{total_this_period},
  371. $form->{decimalplaces}, "- "
  372. );
  373. }
  374. sub balance_sheet {
  375. my ( $self, $myconfig, $form ) = @_;
  376. my $dbh = $form->{dbh};
  377. my $last_period = 0;
  378. my @categories = qw(A L Q);
  379. my $null;
  380. if ( $form->{asofdate} ) {
  381. if ( $form->{asofyear} && $form->{asofmonth} ) {
  382. if ( $form->{asofdate} !~ /\W/ ) {
  383. $form->{asofdate} =
  384. "$form->{asofyear}$form->{asofmonth}$form->{asofdate}";
  385. }
  386. }
  387. }
  388. else {
  389. if ( $form->{asofyear} && $form->{asofmonth} ) {
  390. ( $null, $form->{asofdate} ) =
  391. $form->from_to( $form->{asofyear}, $form->{asofmonth} );
  392. }
  393. }
  394. # if there are any dates construct a where
  395. if ( $form->{asofdate} ) {
  396. $form->{this_period} = "$form->{asofdate}";
  397. $form->{period} = "$form->{asofdate}";
  398. }
  399. $form->{decimalplaces} *= 1;
  400. &get_accounts( $dbh, $last_period, "", $form->{asofdate}, $form,
  401. \@categories, 1 );
  402. if ( $form->{compareasofdate} ) {
  403. if ( $form->{compareasofyear} && $form->{compareasofmonth} ) {
  404. if ( $form->{compareasofdate} !~ /\W/ ) {
  405. $form->{compareasofdate} =
  406. "$form->{compareasofyear}$form->{compareasofmonth}$form->{compareasofdate}";
  407. }
  408. }
  409. }
  410. else {
  411. if ( $form->{compareasofyear} && $form->{compareasofmonth} ) {
  412. ( $null, $form->{compareasofdate} ) =
  413. $form->from_to( $form->{compareasofyear},
  414. $form->{compareasofmonth} );
  415. }
  416. }
  417. # if there are any compare dates
  418. if ( $form->{compareasofdate} ) {
  419. $last_period = 1;
  420. &get_accounts( $dbh, $last_period, "", $form->{compareasofdate},
  421. $form, \@categories, 1 );
  422. $form->{last_period} = "$form->{compareasofdate}";
  423. }
  424. $dbh->commit;
  425. # now we got $form->{A}{accno}{ } assets
  426. # and $form->{L}{accno}{ } liabilities
  427. # and $form->{Q}{accno}{ } equity
  428. # build asset accounts
  429. my $str;
  430. my $key;
  431. my %account = (
  432. 'A' => {
  433. 'label' => 'asset',
  434. 'labels' => 'assets',
  435. 'ml' => -1
  436. },
  437. 'L' => {
  438. 'label' => 'liability',
  439. 'labels' => 'liabilities',
  440. 'ml' => 1
  441. },
  442. 'Q' => {
  443. 'label' => 'equity',
  444. 'labels' => 'equity',
  445. 'ml' => 1
  446. }
  447. );
  448. foreach $category (@categories) {
  449. foreach $key ( sort keys %{ $form->{$category} } ) {
  450. ## $str = ( $form->{l_heading} ) ? $form->{padding} : "";
  451. $str = "";
  452. if ( $form->{$category}{$key}{charttype} eq "A" ) {
  453. $str .=
  454. ( $form->{l_accno} )
  455. ? "$form->{$category}{$key}{accno} - $form->{$category}{$key}{description}"
  456. : "$form->{$category}{$key}{description}";
  457. $str = {account => $form->{$category}{$key}{accno}, text => $str};
  458. $str->{gifi_account} = 1 if $form->{accounttype} eq 'gifi';
  459. }
  460. elsif ( $form->{$category}{$key}{charttype} eq "H" ) {
  461. if ( $account{$category}{subtotal}
  462. && $form->{l_subtotal} )
  463. {
  464. $dash = "- ";
  465. push(
  466. @{ $form->{"$account{$category}{label}_account"} },
  467. {
  468. text => "$account{$category}{subdescription}",
  469. subtotal => 1
  470. },
  471. );
  472. push(
  473. @{ $form->{"$account{$category}{label}_this_period"} },
  474. $form->format_amount(
  475. $myconfig,
  476. $account{$category}{subthis} *
  477. $account{$category}{ml},
  478. $form->{decimalplaces},
  479. $dash
  480. )
  481. );
  482. if ($last_period) {
  483. push(
  484. @{
  485. $form->{
  486. "$account{$category}{label}_last_period"}
  487. },
  488. $form->format_amount(
  489. $myconfig,
  490. $account{$category}{sublast} *
  491. $account{$category}{ml},
  492. $form->{decimalplaces},
  493. $dash
  494. )
  495. );
  496. }
  497. }
  498. $str = {
  499. text => "$form->{$category}{$key}{description}",
  500. heading => 1
  501. };
  502. $account{$category}{subthis} = $form->{$category}{$key}{this};
  503. $account{$category}{sublast} = $form->{$category}{$key}{last};
  504. $account{$category}{subdescription} =
  505. $form->{$category}{$key}{description};
  506. $account{$category}{subtotal} = 1;
  507. $form->{$category}{$key}{this} = 0;
  508. $form->{$category}{$key}{last} = 0;
  509. next unless $form->{l_heading};
  510. $dash = " ";
  511. }
  512. # push description onto array
  513. push( @{ $form->{"$account{$category}{label}_account"} }, $str );
  514. if ( $form->{$category}{$key}{charttype} eq 'A' ) {
  515. $form->{"total_$account{$category}{labels}_this_period"} +=
  516. $form->{$category}{$key}{this} * $account{$category}{ml};
  517. $dash = "- ";
  518. }
  519. push(
  520. @{ $form->{"$account{$category}{label}_this_period"} },
  521. $form->format_amount(
  522. $myconfig,
  523. $form->{$category}{$key}{this} * $account{$category}{ml},
  524. $form->{decimalplaces}, $dash
  525. )
  526. );
  527. if ($last_period) {
  528. $form->{"total_$account{$category}{labels}_last_period"} +=
  529. $form->{$category}{$key}{last} * $account{$category}{ml};
  530. push(
  531. @{ $form->{"$account{$category}{label}_last_period"} },
  532. $form->format_amount(
  533. $myconfig,
  534. $form->{$category}{$key}{last} *
  535. $account{$category}{ml},
  536. $form->{decimalplaces},
  537. $dash
  538. )
  539. );
  540. }
  541. }
  542. #$str = ( $form->{l_heading} ) ? $form->{padding} : "";
  543. $str = "";
  544. if ( $account{$category}{subtotal} && $form->{l_subtotal} ) {
  545. push(
  546. @{ $form->{"$account{$category}{label}_account"} }, {
  547. text => "$account{$category}{subdescription}",
  548. subtotal => 1,
  549. },
  550. );
  551. push(
  552. @{ $form->{"$account{$category}{label}_this_period"} },
  553. $form->format_amount(
  554. $myconfig,
  555. $account{$category}{subthis} * $account{$category}{ml},
  556. $form->{decimalplaces}, $dash
  557. )
  558. );
  559. if ($last_period) {
  560. push(
  561. @{ $form->{"$account{$category}{label}_last_period"} },
  562. $form->format_amount(
  563. $myconfig,
  564. $account{$category}{sublast} * $account{$category}{ml},
  565. $form->{decimalplaces},
  566. $dash
  567. )
  568. );
  569. }
  570. }
  571. }
  572. # totals for assets, liabilities
  573. $form->{total_assets_this_period} =
  574. $form->round_amount( $form->{total_assets_this_period},
  575. $form->{decimalplaces} );
  576. $form->{total_liabilities_this_period} =
  577. $form->round_amount( $form->{total_liabilities_this_period},
  578. $form->{decimalplaces} );
  579. $form->{total_equity_this_period} =
  580. $form->round_amount( $form->{total_equity_this_period},
  581. $form->{decimalplaces} );
  582. # calculate earnings
  583. $form->{earnings_this_period} =
  584. $form->{total_assets_this_period} -
  585. $form->{total_liabilities_this_period} -
  586. $form->{total_equity_this_period};
  587. push(
  588. @{ $form->{equity_this_period} },
  589. $form->format_amount(
  590. $myconfig, $form->{earnings_this_period},
  591. $form->{decimalplaces}, "- "
  592. )
  593. );
  594. $form->{total_equity_this_period} =
  595. $form->round_amount(
  596. $form->{total_equity_this_period} + $form->{earnings_this_period},
  597. $form->{decimalplaces} );
  598. # add liability + equity
  599. $form->{total_this_period} = $form->format_amount(
  600. $myconfig,
  601. $form->{total_liabilities_this_period} +
  602. $form->{total_equity_this_period},
  603. $form->{decimalplaces},
  604. "- "
  605. );
  606. if ($last_period) {
  607. # totals for assets, liabilities
  608. $form->{total_assets_last_period} =
  609. $form->round_amount( $form->{total_assets_last_period},
  610. $form->{decimalplaces} );
  611. $form->{total_liabilities_last_period} =
  612. $form->round_amount( $form->{total_liabilities_last_period},
  613. $form->{decimalplaces} );
  614. $form->{total_equity_last_period} =
  615. $form->round_amount( $form->{total_equity_last_period},
  616. $form->{decimalplaces} );
  617. # calculate retained earnings
  618. $form->{earnings_last_period} =
  619. $form->{total_assets_last_period} -
  620. $form->{total_liabilities_last_period} -
  621. $form->{total_equity_last_period};
  622. push(
  623. @{ $form->{equity_last_period} },
  624. $form->format_amount(
  625. $myconfig, $form->{earnings_last_period},
  626. $form->{decimalplaces}, "- "
  627. )
  628. );
  629. $form->{total_equity_last_period} =
  630. $form->round_amount(
  631. $form->{total_equity_last_period} + $form->{earnings_last_period},
  632. $form->{decimalplaces} );
  633. # add liability + equity
  634. $form->{total_last_period} = $form->format_amount(
  635. $myconfig,
  636. $form->{total_liabilities_last_period} +
  637. $form->{total_equity_last_period},
  638. $form->{decimalplaces},
  639. "- "
  640. );
  641. }
  642. $form->{total_liabilities_last_period} = $form->format_amount(
  643. $myconfig,
  644. $form->{total_liabilities_last_period},
  645. $form->{decimalplaces}, "- "
  646. ) if ( $form->{total_liabilities_last_period} );
  647. $form->{total_equity_last_period} = $form->format_amount(
  648. $myconfig,
  649. $form->{total_equity_last_period},
  650. $form->{decimalplaces}, "- "
  651. ) if ( $form->{total_equity_last_period} );
  652. $form->{total_assets_last_period} = $form->format_amount(
  653. $myconfig,
  654. $form->{total_assets_last_period},
  655. $form->{decimalplaces}, "- "
  656. ) if ( $form->{total_assets_last_period} );
  657. $form->{total_assets_this_period} = $form->format_amount(
  658. $myconfig,
  659. $form->{total_assets_this_period},
  660. $form->{decimalplaces}, "- "
  661. );
  662. $form->{total_liabilities_this_period} = $form->format_amount(
  663. $myconfig,
  664. $form->{total_liabilities_this_period},
  665. $form->{decimalplaces}, "- "
  666. );
  667. $form->{total_equity_this_period} = $form->format_amount(
  668. $myconfig,
  669. $form->{total_equity_this_period},
  670. $form->{decimalplaces}, "- "
  671. );
  672. }
  673. sub get_accounts {
  674. my ( $dbh, $last_period, $fromdate, $todate, $form, $categories,
  675. $excludeyearend )
  676. = @_;
  677. my $department_id;
  678. my $project_id;
  679. ( $null, $department_id ) = split /--/, $form->{department};
  680. ( $null, $project_id ) = split /--/, $form->{projectnumber};
  681. my $query;
  682. my $dpt_where;
  683. my $dpt_join;
  684. my $project;
  685. my $where = "1 = 1";
  686. my $glwhere = "";
  687. my $subwhere = "";
  688. my $yearendwhere = "1 = 1";
  689. my $item;
  690. my $category = "AND (";
  691. foreach $item ( @{$categories} ) {
  692. $category .= qq|c.category = | . $dbh->quote($item) . qq| OR |;
  693. }
  694. $category =~ s/OR $/\)/;
  695. # get headings
  696. $query = qq|
  697. SELECT accno, description, category
  698. FROM chart c
  699. WHERE c.charttype = 'H' $category
  700. ORDER BY c.accno|;
  701. if ( $form->{accounttype} eq 'gifi' ) {
  702. $query = qq|
  703. SELECT g.accno, g.description, c.category
  704. FROM gifi g
  705. JOIN chart c ON (c.gifi_accno = g.accno)
  706. WHERE c.charttype = 'H' $category
  707. ORDER BY g.accno|;
  708. }
  709. $sth = $dbh->prepare($query);
  710. $sth->execute || $form->dberror($query);
  711. my @headingaccounts = ();
  712. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  713. $form->{ $ref->{category} }{ $ref->{accno} }{description} =
  714. "$ref->{description}";
  715. $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "H";
  716. $form->{ $ref->{category} }{ $ref->{accno} }{accno} = $ref->{accno};
  717. push @headingaccounts, $ref->{accno};
  718. }
  719. $sth->finish;
  720. if ( $form->{method} eq 'cash' && !$todate ) {
  721. ($todate) = $dbh->selectrow_array(qq|SELECT current_date|);
  722. }
  723. if ($fromdate) {
  724. if ( $form->{method} eq 'cash' ) {
  725. $subwhere .= " AND transdate >= " . $dbh->quote($fromdate);
  726. $glwhere = " AND ac.transdate >= " . $dbh->quote($fromdate);
  727. }
  728. else {
  729. $where .= " AND ac.transdate >= " . $dbh->quote($fromdate);
  730. }
  731. }
  732. if ($todate) {
  733. $where .= " AND ac.transdate <= " . $dbh->quote($todate);
  734. $subwhere .= " AND transdate <= " . $dbh->quote($todate);
  735. $yearendwhere = "ac.transdate < " . $dbh->quote($todate);
  736. }
  737. if ($excludeyearend) {
  738. $ywhere = "
  739. AND ac.trans_id NOT IN (SELECT trans_id FROM yearend)";
  740. if ($todate) {
  741. $ywhere = "
  742. AND ac.trans_id NOT IN
  743. (SELECT trans_id FROM yearend
  744. WHERE transdate <= " . $dbh->quote($todate) . ")";
  745. }
  746. if ($fromdate) {
  747. $ywhere = "
  748. AND ac.trans_id NOT IN
  749. (SELECT trans_id FROM yearend
  750. WHERE transdate >= " . $dbh->quote($fromdate) . ")";
  751. if ($todate) {
  752. $ywhere = "
  753. AND ac.trans_id NOT IN
  754. (SELECT trans_id FROM yearend
  755. WHERE transdate >= "
  756. . $dbh->quote($fromdate) . "
  757. AND transdate <= " . $dbh->quote($todate) . ")";
  758. }
  759. }
  760. }
  761. if ($department_id) {
  762. $dpt_join = qq|
  763. JOIN department t ON (a.department_id = t.id)|;
  764. $dpt_where = qq|
  765. AND t.id = $department_id|;
  766. }
  767. if ($project_id) {
  768. $project = qq|
  769. AND ac.project_id = $project_id|;
  770. }
  771. if ( $form->{accounttype} eq 'gifi' ) {
  772. if ( $form->{method} eq 'cash' ) {
  773. $query = qq|
  774. SELECT g.accno, sum(ac.amount) AS amount,
  775. g.description, c.category
  776. FROM acc_trans ac
  777. JOIN chart c ON (c.id = ac.chart_id)
  778. JOIN ar a ON (a.id = ac.trans_id)
  779. JOIN gifi g ON (g.accno = c.gifi_accno)
  780. $dpt_join
  781. WHERE $where $ywhere $dpt_where $category
  782. AND ac.trans_id IN (
  783. SELECT trans_id
  784. FROM acc_trans
  785. JOIN chart ON (chart_id = id)
  786. WHERE link LIKE '%AR_paid%'
  787. $subwhere)
  788. $project
  789. GROUP BY g.accno, g.description, c.category
  790. UNION ALL
  791. SELECT '' AS accno, SUM(ac.amount) AS amount,
  792. '' AS description, c.category
  793. FROM acc_trans ac
  794. JOIN chart c ON (c.id = ac.chart_id)
  795. JOIN ar a ON (a.id = ac.trans_id)
  796. $dpt_join
  797. WHERE $where $ywhere $dpt_where $category
  798. AND c.gifi_accno = '' AND
  799. ac.trans_id IN
  800. (SELECT trans_id FROM acc_trans
  801. JOIN chart ON (chart_id = id)
  802. WHERE link LIKE '%AR_paid%'
  803. $subwhere) $project
  804. GROUP BY c.category
  805. UNION ALL
  806. SELECT g.accno, sum(ac.amount) AS amount,
  807. g.description, c.category
  808. FROM acc_trans ac
  809. JOIN chart c ON (c.id = ac.chart_id)
  810. JOIN ap a ON (a.id = ac.trans_id)
  811. JOIN gifi g ON (g.accno = c.gifi_accno)
  812. $dpt_join
  813. WHERE $where $ywhere $dpt_where $category
  814. AND ac.trans_id IN
  815. (SELECT trans_id FROM acc_trans
  816. JOIN chart ON (chart_id = id)
  817. WHERE link LIKE '%AP_paid%'
  818. $subwhere) $project
  819. GROUP BY g.accno, g.description, c.category
  820. UNION ALL
  821. SELECT '' AS accno, SUM(ac.amount) AS amount,
  822. '' AS description, c.category
  823. FROM acc_trans ac
  824. JOIN chart c ON (c.id = ac.chart_id)
  825. JOIN ap a ON (a.id = ac.trans_id)
  826. $dpt_join
  827. WHERE $where $ywhere $dpt_where $category
  828. AND c.gifi_accno = ''
  829. AND ac.trans_id IN
  830. (SELECT trans_id FROM acc_trans
  831. JOIN chart ON (chart_id = id)
  832. WHERE link LIKE '%AP_paid%' $subwhere)
  833. $project
  834. GROUP BY c.category
  835. UNION ALL
  836. SELECT g.accno, sum(ac.amount) AS amount,
  837. g.description, c.category
  838. FROM acc_trans ac
  839. JOIN chart c ON (c.id = ac.chart_id)
  840. JOIN gifi g ON (g.accno = c.gifi_accno)
  841. JOIN gl a ON (a.id = ac.trans_id)
  842. $dpt_join
  843. WHERE $where $ywhere $glwhere $dpt_where
  844. $category AND NOT
  845. (c.link = 'AR' OR c.link = 'AP')
  846. $project
  847. GROUP BY g.accno, g.description, c.category
  848. UNION ALL
  849. SELECT '' AS accno, SUM(ac.amount) AS amount,
  850. '' AS description, c.category
  851. FROM acc_trans ac
  852. JOIN chart c ON (c.id = ac.chart_id)
  853. JOIN gl a ON (a.id = ac.trans_id)
  854. $dpt_join
  855. WHERE $where $ywhere $glwhere $dpt_where
  856. $category AND c.gifi_accno = ''
  857. AND NOT
  858. (c.link = 'AR' OR c.link = 'AP')
  859. $project
  860. GROUP BY c.category|;
  861. if ($excludeyearend) {
  862. $query .= qq|
  863. UNION ALL
  864. SELECT g.accno,
  865. sum(ac.amount) AS amount,
  866. g.description, c.category
  867. FROM yearend y
  868. JOIN gl a ON (a.id = y.trans_id)
  869. JOIN acc_trans ac
  870. ON (ac.trans_id = y.trans_id)
  871. JOIN chart c
  872. ON (c.id = ac.chart_id)
  873. JOIN gifi g
  874. ON (g.accno = c.gifi_accno)
  875. $dpt_join
  876. WHERE $yearendwhere
  877. AND c.category = 'Q'
  878. $dpt_where $project
  879. GROUP BY g.accno, g.description,
  880. c.category|;
  881. }
  882. }
  883. else {
  884. if ($department_id) {
  885. $dpt_join = qq|
  886. JOIN dpt_trans t
  887. ON (t.trans_id = ac.trans_id)|;
  888. $dpt_where = qq|
  889. AND t.department_id = | . $dbh->quote($department_id);
  890. }
  891. $query = qq|
  892. SELECT g.accno, SUM(ac.amount) AS amount,
  893. g.description, c.category
  894. FROM acc_trans ac
  895. JOIN chart c ON (c.id = ac.chart_id)
  896. JOIN gifi g ON (c.gifi_accno = g.accno)
  897. $dpt_join
  898. WHERE $where $ywhere $dpt_where $category
  899. $project
  900. GROUP BY g.accno, g.description, c.category
  901. UNION ALL
  902. SELECT '' AS accno, SUM(ac.amount) AS amount,
  903. '' AS description, c.category
  904. FROM acc_trans ac
  905. JOIN chart c ON (c.id = ac.chart_id)
  906. $dpt_join
  907. WHERE $where $ywhere $dpt_where $category
  908. AND c.gifi_accno = '' $project
  909. GROUP BY c.category|;
  910. if ($excludeyearend) {
  911. $query .= qq|
  912. UNION ALL
  913. SELECT g.accno,
  914. sum(ac.amount)
  915. AS amount,
  916. g.description,
  917. c.category
  918. FROM yearend y
  919. JOIN gl a
  920. ON (a.id = y.trans_id)
  921. JOIN acc_trans ac
  922. ON (ac.trans_id =
  923. y.trans_id)
  924. JOIN chart c
  925. ON
  926. (c.id = ac.chart_id)
  927. JOIN gifi g
  928. ON (g.accno =
  929. c.gifi_accno)
  930. $dpt_join
  931. WHERE $yearendwhere
  932. AND c.category = 'Q'
  933. $dpt_where $project
  934. GROUP BY g.accno,
  935. g.description,
  936. c.category|;
  937. }
  938. }
  939. }
  940. else { # standard account
  941. if ( $form->{method} eq 'cash' ) {
  942. $query = qq|
  943. SELECT c.accno, sum(ac.amount) AS amount,
  944. c.description, c.category
  945. FROM acc_trans ac
  946. JOIN chart c ON (c.id = ac.chart_id)
  947. JOIN ar a ON (a.id = ac.trans_id) $dpt_join
  948. WHERE $where $ywhere $dpt_where $category
  949. AND ac.trans_id IN (
  950. SELECT trans_id FROM acc_trans
  951. JOIN chart ON (chart_id = id)
  952. WHERE link LIKE '%AR_paid%' $subwhere)
  953. $project
  954. GROUP BY c.accno, c.description, c.category
  955. UNION ALL
  956. SELECT c.accno, sum(ac.amount) AS amount,
  957. c.description, c.category
  958. FROM acc_trans ac
  959. JOIN chart c ON (c.id = ac.chart_id)
  960. JOIN ap a ON (a.id = ac.trans_id) $dpt_join
  961. WHERE $where $ywhere $dpt_where $category
  962. AND ac.trans_id IN (
  963. SELECT trans_id FROM acc_trans
  964. JOIN chart ON (chart_id = id)
  965. WHERE link LIKE '%AP_paid%' $subwhere)
  966. $project
  967. GROUP BY c.accno, c.description, c.category
  968. UNION ALL
  969. SELECT c.accno, sum(ac.amount) AS amount,
  970. c.description, c.category
  971. FROM acc_trans ac
  972. JOIN chart c ON (c.id = ac.chart_id)
  973. JOIN gl a ON (a.id = ac.trans_id) $dpt_join
  974. WHERE $where $ywhere $glwhere $dpt_where $category
  975. AND NOT (c.link = 'AR' OR c.link = 'AP')
  976. $project
  977. GROUP BY c.accno, c.description, c.category|;
  978. if ($excludeyearend) {
  979. # this is for the yearend
  980. $query .= qq|
  981. UNION ALL
  982. SELECT c.accno,
  983. sum(ac.amount) AS amount,
  984. c.description, c.category
  985. FROM yearend y
  986. JOIN gl a ON (a.id = y.trans_id)
  987. JOIN acc_trans ac
  988. ON (ac.trans_id = y.trans_id)
  989. JOIN chart c
  990. ON (c.id = ac.chart_id)
  991. $dpt_join
  992. WHERE $yearendwhere AND
  993. c.category = 'Q' $dpt_where
  994. $project
  995. GROUP BY c.accno, c.description,
  996. c.category|;
  997. }
  998. }
  999. else {
  1000. if ($department_id) {
  1001. $dpt_join = qq|
  1002. JOIN dpt_trans t
  1003. ON (t.trans_id = ac.trans_id)|;
  1004. $dpt_where =
  1005. qq| AND t.department_id = | . $dbh->quote($department_id);
  1006. }
  1007. $query = qq|
  1008. SELECT c.accno, sum(ac.amount) AS amount,
  1009. c.description, c.category
  1010. FROM acc_trans ac
  1011. JOIN chart c ON (c.id = ac.chart_id)
  1012. $dpt_join
  1013. WHERE $where $ywhere $dpt_where $category
  1014. $project
  1015. GROUP BY c.accno, c.description, c.category|;
  1016. if ($excludeyearend) {
  1017. $query .= qq|
  1018. UNION ALL
  1019. SELECT c.accno,
  1020. sum(ac.amount) AS amount,
  1021. c.description, c.category
  1022. FROM yearend y
  1023. JOIN gl a ON (a.id = y.trans_id)
  1024. JOIN acc_trans ac
  1025. ON (ac.trans_id = y.trans_id)
  1026. JOIN chart c
  1027. ON (c.id = ac.chart_id)
  1028. $dpt_join
  1029. WHERE $yearendwhere AND
  1030. c.category = 'Q' $dpt_where
  1031. $project
  1032. GROUP BY c.accno, c.description,
  1033. c.category|;
  1034. }
  1035. }
  1036. }
  1037. my @accno;
  1038. my $accno;
  1039. my $ref;
  1040. my $sth = $dbh->prepare($query);
  1041. $sth->execute || $form->dberror($query);
  1042. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1043. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1044. # get last heading account
  1045. @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
  1046. $accno = pop @accno;
  1047. if ( $accno && ( $accno ne $ref->{accno} ) ) {
  1048. if ($last_period) {
  1049. $form->{ $ref->{category} }{$accno}{last} += $ref->{amount};
  1050. }
  1051. else {
  1052. $form->{ $ref->{category} }{$accno}{this} += $ref->{amount};
  1053. }
  1054. }
  1055. $form->{ $ref->{category} }{ $ref->{accno} }{accno} = $ref->{accno};
  1056. $form->{ $ref->{category} }{ $ref->{accno} }{description} =
  1057. $ref->{description};
  1058. $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "A";
  1059. if ($last_period) {
  1060. $form->{ $ref->{category} }{ $ref->{accno} }{last} +=
  1061. $ref->{amount};
  1062. }
  1063. else {
  1064. $form->{ $ref->{category} }{ $ref->{accno} }{this} +=
  1065. $ref->{amount};
  1066. }
  1067. }
  1068. $sth->finish;
  1069. # remove accounts with zero balance
  1070. foreach $category ( @{$categories} ) {
  1071. foreach $accno ( keys %{ $form->{$category} } ) {
  1072. $form->{$category}{$accno}{last} =
  1073. $form->round_amount( $form->{$category}{$accno}{last},
  1074. $form->{decimalplaces} );
  1075. $form->{$category}{$accno}{this} =
  1076. $form->round_amount( $form->{$category}{$accno}{this},
  1077. $form->{decimalplaces} );
  1078. delete $form->{$category}{$accno}
  1079. if ( $form->{$category}{$accno}{this} == 0
  1080. && $form->{$category}{$accno}{last} == 0 );
  1081. }
  1082. }
  1083. }
  1084. sub trial_balance {
  1085. my ( $self, $myconfig, $form ) = @_;
  1086. my $dbh = $form->{dbh};
  1087. my ( $query, $sth, $ref );
  1088. my %balance = ();
  1089. my %trb = ();
  1090. my $null;
  1091. my $department_id;
  1092. my $project_id;
  1093. my @headingaccounts = ();
  1094. my $dpt_where;
  1095. my $dpt_join;
  1096. my $project;
  1097. my $where = "1 = 1";
  1098. my $invwhere = $where;
  1099. ( $null, $department_id ) = split /--/, $form->{department};
  1100. ( $null, $project_id ) = split /--/, $form->{projectnumber};
  1101. if ($department_id) {
  1102. $dpt_join = qq|
  1103. JOIN dpt_trans t ON (ac.trans_id = t.trans_id)|;
  1104. $dpt_where = qq|
  1105. AND t.department_id = | . $dbh->quote($department_id);
  1106. }
  1107. if ($project_id) {
  1108. $project = qq|
  1109. AND ac.project_id = | . $dbh->quote($project_id);
  1110. }
  1111. ( $form->{fromdate}, $form->{todate} ) =
  1112. $form->from_to( $form->{year}, $form->{month}, $form->{interval} )
  1113. if $form->{year} && $form->{month};
  1114. # get beginning balances
  1115. if ( $form->{fromdate} ) {
  1116. if ( $form->{accounttype} eq 'gifi' ) {
  1117. $query = qq|
  1118. SELECT g.accno, c.category,
  1119. SUM(ac.amount) AS amount,
  1120. g.description, c.contra
  1121. FROM acc_trans ac
  1122. JOIN chart c ON (ac.chart_id = c.id)
  1123. JOIN gifi g ON (c.gifi_accno = g.accno)
  1124. $dpt_join
  1125. WHERE ac.transdate < '$form->{fromdate}'
  1126. $dpt_where $project
  1127. GROUP BY g.accno, c.category, g.description,
  1128. c.contra|;
  1129. }
  1130. else {
  1131. $query = qq|
  1132. SELECT c.accno, c.category,
  1133. SUM(ac.amount) AS amount,
  1134. c.description, c.contra
  1135. FROM acc_trans ac
  1136. JOIN chart c ON (ac.chart_id = c.id)
  1137. $dpt_join
  1138. WHERE ac.transdate < '$form->{fromdate}'
  1139. $dpt_where $project
  1140. GROUP BY c.accno, c.category, c.description,
  1141. c.contra|;
  1142. }
  1143. $sth = $dbh->prepare($query);
  1144. $sth->execute || $form->dberror($query);
  1145. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1146. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1147. $ref->{amount} = $form->round_amount( $ref->{amount}, 2 );
  1148. $balance{ $ref->{accno} } = $ref->{amount};
  1149. if ( $form->{all_accounts} ) {
  1150. $trb{ $ref->{accno} }{description} = $ref->{description};
  1151. $trb{ $ref->{accno} }{charttype} = 'A';
  1152. $trb{ $ref->{accno} }{category} = $ref->{category};
  1153. $trb{ $ref->{accno} }{contra} = $ref->{contra};
  1154. }
  1155. }
  1156. $sth->finish;
  1157. }
  1158. # get headings
  1159. $query = qq|
  1160. SELECT c.accno, c.description, c.category FROM chart c
  1161. WHERE c.charttype = 'H'
  1162. ORDER by c.accno|;
  1163. if ( $form->{accounttype} eq 'gifi' ) {
  1164. $query = qq|
  1165. SELECT g.accno, g.description, c.category, c.contra
  1166. FROM gifi g
  1167. JOIN chart c ON (c.gifi_accno = g.accno)
  1168. WHERE c.charttype = 'H'
  1169. ORDER BY g.accno|;
  1170. }
  1171. $sth = $dbh->prepare($query);
  1172. $sth->execute || $form->dberror($query);
  1173. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1174. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1175. $trb{ $ref->{accno} }{description} = $ref->{description};
  1176. $trb{ $ref->{accno} }{charttype} = 'H';
  1177. $trb{ $ref->{accno} }{category} = $ref->{category};
  1178. $trb{ $ref->{accno} }{contra} = $ref->{contra};
  1179. push @headingaccounts, $ref->{accno};
  1180. }
  1181. $sth->finish;
  1182. if ( $form->{fromdate} || $form->{todate} ) {
  1183. if ( $form->{fromdate} ) {
  1184. $where .=
  1185. " AND ac.transdate >= " . $dbh->quote( $form->{fromdate} );
  1186. $invwhere .=
  1187. " AND a.transdate >= " . $dbh->quote( $form->{fromdate} );
  1188. }
  1189. if ( $form->{todate} ) {
  1190. $where .= " AND ac.transdate <= " . $dbh->quote( $form->{todate} );
  1191. $invwhere .=
  1192. " AND a.transdate <= " . $dbh->quote( $form->{todate} );
  1193. }
  1194. }
  1195. if ( $form->{accounttype} eq 'gifi' ) {
  1196. $query = qq|
  1197. SELECT g.accno, g.description, c.category,
  1198. SUM(ac.amount) AS amount, c.contra
  1199. FROM acc_trans ac
  1200. JOIN chart c ON (c.id = ac.chart_id)
  1201. JOIN gifi g ON (c.gifi_accno = g.accno)
  1202. $dpt_join
  1203. WHERE $where $dpt_where $project
  1204. GROUP BY g.accno, g.description, c.category, c.contra
  1205. ORDER BY accno|;
  1206. }
  1207. else {
  1208. $query = qq|
  1209. SELECT c.accno, c.description, c.category,
  1210. SUM(ac.amount) AS amount, c.contra
  1211. FROM acc_trans ac
  1212. JOIN chart c ON (c.id = ac.chart_id)
  1213. $dpt_join
  1214. WHERE $where $dpt_where $project
  1215. GROUP BY c.accno, c.description, c.category, c.contra
  1216. ORDER BY accno|;
  1217. }
  1218. $sth = $dbh->prepare($query);
  1219. $sth->execute || $form->dberror($query);
  1220. # prepare query for each account
  1221. $query = qq|
  1222. SELECT (SELECT SUM(ac.amount) * -1 FROM acc_trans ac
  1223. JOIN chart c ON (c.id = ac.chart_id)
  1224. $dpt_join
  1225. WHERE $where $dpt_where $project AND ac.amount < 0
  1226. AND c.accno = ?) AS debit,
  1227. (SELECT SUM(ac.amount) FROM acc_trans ac
  1228. JOIN chart c ON (c.id = ac.chart_id)
  1229. $dpt_join
  1230. WHERE $where $dpt_where $project AND ac.amount > 0
  1231. AND c.accno = ?) AS credit |;
  1232. if ( $form->{accounttype} eq 'gifi' ) {
  1233. $query = qq|
  1234. SELECT (SELECT SUM(ac.amount) * -1
  1235. FROM acc_trans ac
  1236. JOIN chart c ON (c.id = ac.chart_id)
  1237. $dpt_join
  1238. WHERE $where $dpt_where $project AND ac.amount < 0
  1239. AND c.gifi_accno = ?) AS debit,
  1240. (SELECT SUM(ac.amount)
  1241. FROM acc_trans ac
  1242. JOIN chart c ON (c.id = ac.chart_id)
  1243. $dpt_join
  1244. WHERE $where $dpt_where $project AND ac.amount > 0
  1245. AND c.gifi_accno = ?) AS credit|;
  1246. }
  1247. $drcr = $dbh->prepare($query);
  1248. # calculate debit and credit for the period
  1249. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1250. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1251. $trb{ $ref->{accno} }{description} = $ref->{description};
  1252. $trb{ $ref->{accno} }{charttype} = 'A';
  1253. $trb{ $ref->{accno} }{category} = $ref->{category};
  1254. $trb{ $ref->{accno} }{contra} = $ref->{contra};
  1255. $trb{ $ref->{accno} }{amount} += $ref->{amount};
  1256. }
  1257. $sth->finish;
  1258. my ( $debit, $credit );
  1259. foreach my $accno ( sort keys %trb ) {
  1260. $ref = ();
  1261. $ref->{accno} = $accno;
  1262. for (qw(description category contra charttype amount)) {
  1263. $ref->{$_} = $trb{$accno}{$_};
  1264. }
  1265. $ref->{balance} = $balance{ $ref->{accno} };
  1266. if ( $trb{$accno}{charttype} eq 'A' ) {
  1267. if ($project_id) {
  1268. if ( $ref->{amount} < 0 ) {
  1269. $ref->{debit} = $ref->{amount} * -1;
  1270. }
  1271. else {
  1272. $ref->{credit} = $ref->{amount};
  1273. }
  1274. next if $form->round_amount( $ref->{amount}, 2 ) == 0;
  1275. }
  1276. else {
  1277. # get DR/CR
  1278. $drcr->execute( $ref->{accno}, $ref->{accno} )
  1279. || $form->dberror($query);
  1280. ( $debit, $credit ) = ( 0, 0 );
  1281. while ( my @drcrlist = $drcr->fetchrow_array ) {
  1282. $form->db_parse_numeric(sth=>$drcr, arrayref=>\@drcrlist);
  1283. ($debit, $credit) = @drcrlist;
  1284. $ref->{debit} += $debit;
  1285. $ref->{credit} += $credit;
  1286. }
  1287. $drcr->finish;
  1288. }
  1289. $ref->{debit} = $form->round_amount( $ref->{debit}, 2 );
  1290. $ref->{credit} = $form->round_amount( $ref->{credit}, 2 );
  1291. if ( !$form->{all_accounts} ) {
  1292. next
  1293. if $form->round_amount( $ref->{debit} + $ref->{credit}, 2 ) ==
  1294. 0;
  1295. }
  1296. }
  1297. # add subtotal
  1298. @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
  1299. $accno = pop @accno;
  1300. if ($accno) {
  1301. $trb{$accno}{debit} += $ref->{debit};
  1302. $trb{$accno}{credit} += $ref->{credit};
  1303. }
  1304. push @{ $form->{TB} }, $ref;
  1305. }
  1306. $dbh->commit;
  1307. # debits and credits for headings
  1308. foreach $accno (@headingaccounts) {
  1309. foreach $ref ( @{ $form->{TB} } ) {
  1310. if ( $accno eq $ref->{accno} ) {
  1311. $ref->{debit} = $trb{$accno}{debit};
  1312. $ref->{credit} = $trb{$accno}{credit};
  1313. }
  1314. }
  1315. }
  1316. }
  1317. sub aging {
  1318. my ( $self, $myconfig, $form ) = @_;
  1319. my $ref;
  1320. my $department_id;
  1321. my $null;
  1322. my $dbh = $form->{dbh};
  1323. my $invoice = ( $form->{arap} eq 'ar' ) ? 'is' : 'ir';
  1324. my $query = qq|SELECT value FROM defaults WHERE setting_key = 'curr'|;
  1325. ( $form->{currencies} ) = $dbh->selectrow_array($query);
  1326. ( $null, $form->{todate} ) = $form->from_to( $form->{year}, $form->{month} )
  1327. if $form->{year} && $form->{month};
  1328. if ( !$form->{todate} ) {
  1329. $query = qq|SELECT current_date|;
  1330. ( $form->{todate} ) = $dbh->selectrow_array($query);
  1331. }
  1332. my $transdate = ( $form->{overdue} ) ? "duedate" : "transdate";
  1333. if ( $form->{department} ) {
  1334. ( $null, $department_id ) = split /--/, $form->{department};
  1335. }
  1336. my $buysell = ( $form->{arap} eq 'ar' ) ? 'buy' : 'sell';
  1337. my $todate = $dbh->quote( $form->{todate} );
  1338. my %interval = (
  1339. 'c0' => "(date $todate - interval '0 days')",
  1340. 'c30' => "(date $todate - interval '30 days')",
  1341. 'c60' => "(date $todate - interval '60 days')",
  1342. 'c90' => "(date $todate - interval '90 days')"
  1343. );
  1344. # for each company that has some stuff outstanding
  1345. $form->{currencies} ||= ":";
  1346. $where = qq|a.paid != a.amount|;
  1347. if ( $form->{"$form->{ct}_id"} ) {
  1348. $where .= qq| AND c.entity_id = | . $dbh->quote( $form->{"$form->{ct}_id"} );
  1349. }
  1350. if ($department_id) {
  1351. $where .= qq| AND a.department_id = | . $dbh->quote($department_id);
  1352. }
  1353. $query = "";
  1354. my $union = "";
  1355. $query .= qq|
  1356. SELECT c.entity_id AS ctid,
  1357. c.meta_number as $form->{ct}number, '' as name,
  1358. '' as address1, '' as address2, '' as city,
  1359. '' as state,
  1360. '' as zipcode,
  1361. '' as country, '' as contact, '' as email,
  1362. '' as $form->{ct}phone,
  1363. '' as $form->{ct}fax,
  1364. '' as $form->{ct}taxnumber,
  1365. a.invnumber, a.transdate, a.till, a.ordnumber,
  1366. a.ponumber, a.notes, c.language_code,
  1367. CASE WHEN
  1368. EXTRACT(days FROM age(a.transdate)/30)
  1369. = 0
  1370. THEN (a.amount - a.paid) ELSE 0 END
  1371. as c0,
  1372. CASE WHEN EXTRACT(days FROM age(a.transdate)/30)
  1373. = 1
  1374. THEN (a.amount - a.paid) ELSE 0 END
  1375. as c30,
  1376. CASE WHEN EXTRACT(days FROM age(a.transdate)/30)
  1377. = 2
  1378. THEN (a.amount - a.paid) ELSE 0 END
  1379. as c60,
  1380. CASE WHEN EXTRACT(days FROM age(a.transdate)/30)
  1381. > 2
  1382. THEN (a.amount - a.paid) ELSE 0 END
  1383. as c90,
  1384. a.duedate, a.invoice, a.id, a.curr,
  1385. (SELECT $buysell FROM exchangerate e
  1386. WHERE a.curr = e.curr
  1387. AND e.transdate = a.transdate)
  1388. AS exchangerate
  1389. FROM $form->{arap} a
  1390. JOIN entity_credit_account c USING (entity_id)
  1391. WHERE $where|;
  1392. $query .= qq| ORDER BY ctid, curr, $transdate, invnumber|;
  1393. $sth = $dbh->prepare($query) || $form->dberror($query);
  1394. $sth->execute();
  1395. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1396. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1397. $ref->{module} =
  1398. ( $ref->{invoice} )
  1399. ? $invoice
  1400. : $form->{arap};
  1401. $ref->{module} = 'ps' if $ref->{till};
  1402. $ref->{exchangerate} = 1
  1403. unless $ref->{exchangerate};
  1404. push @{ $form->{AG} }, $ref;
  1405. }
  1406. $sth->finish;
  1407. # get language
  1408. my $query = qq|SELECT * FROM language ORDER BY 2|;
  1409. $sth = $dbh->prepare($query);
  1410. $sth->execute || $form->dberror($query);
  1411. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1412. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1413. push @{ $form->{all_language} }, $ref;
  1414. }
  1415. $sth->finish;
  1416. $dbh->commit;
  1417. }
  1418. sub get_customer {
  1419. my ( $self, $myconfig, $form ) = @_;
  1420. my $dbh = $form->{dbh};
  1421. my $query = qq|
  1422. SELECT name, email, cc, bcc FROM $form->{ct} ct
  1423. WHERE ct.id = ?|;
  1424. $sth = $dbh->prepare($query);
  1425. $sth->execute( $form->{"$form->{ct}_id"} );
  1426. ( $form->{ $form->{ct} }, $form->{email}, $form->{cc}, $form->{bcc} ) =
  1427. $sth->fetchrow_array();
  1428. $dbh->commit;
  1429. }
  1430. sub get_taxaccounts {
  1431. my ( $self, $myconfig, $form ) = @_;
  1432. my $dbh = $form->{dbh};
  1433. my $ARAP = uc $form->{db};
  1434. # get tax accounts
  1435. my $query = qq|
  1436. SELECT DISTINCT c.accno, c.description
  1437. FROM chart c
  1438. JOIN tax t ON (c.id = t.chart_id)
  1439. WHERE c.link LIKE '%${ARAP}_tax%'
  1440. ORDER BY c.accno|;
  1441. my $sth = $dbh->prepare($query);
  1442. $sth->execute || $form->dberror;
  1443. my $ref = ();
  1444. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1445. push @{ $form->{taxaccounts} }, $ref;
  1446. }
  1447. $sth->finish;
  1448. # get gifi tax accounts
  1449. my $query = qq|
  1450. SELECT DISTINCT g.accno, g.description
  1451. FROM gifi g
  1452. JOIN chart c ON (c.gifi_accno= g.accno)
  1453. JOIN tax t ON (c.id = t.chart_id)
  1454. WHERE c.link LIKE '%${ARAP}_tax%'
  1455. ORDER BY accno|;
  1456. my $sth = $dbh->prepare($query);
  1457. $sth->execute || $form->dberror;
  1458. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1459. push @{ $form->{gifi_taxaccounts} }, $ref;
  1460. }
  1461. $sth->finish;
  1462. $dbh->commit;
  1463. }
  1464. sub tax_report {
  1465. my ( $self, $myconfig, $form ) = @_;
  1466. my $dbh = $form->{dbh};
  1467. my ( $null, $department_id ) = split /--/, $form->{department};
  1468. # build WHERE
  1469. my $where = "1 = 1";
  1470. my $cashwhere = "";
  1471. if ($department_id) {
  1472. $where .= qq|AND a.department_id = | . $dbh->quote($department_id);
  1473. }
  1474. my $query;
  1475. my $sth;
  1476. my $accno;
  1477. if ( $form->{accno} ) {
  1478. if ( $form->{accno} =~ /^gifi_/ ) {
  1479. ( $null, $accno ) = split /_/, $form->{accno};
  1480. $accno = $dbh->quote($accno);
  1481. $accno = qq| AND ch.gifi_accno = $accno|;
  1482. }
  1483. else {
  1484. $accno = $dbh->quote( $form->{accno} );
  1485. $accno = qq| AND ch.accno = $accno|;
  1486. }
  1487. }
  1488. my $table;
  1489. my $ARAP;
  1490. if ( $form->{db} eq 'ar' ) {
  1491. $table = "customer";
  1492. $ARAP = "AR";
  1493. }
  1494. if ( $form->{db} eq 'ap' ) {
  1495. $table = "vendor";
  1496. $ARAP = "AP";
  1497. }
  1498. my $transdate = "a.transdate";
  1499. ( $form->{fromdate}, $form->{todate} ) =
  1500. $form->from_to( $form->{year}, $form->{month}, $form->{interval} )
  1501. if $form->{year} && $form->{month};
  1502. # if there are any dates construct a where
  1503. if ( $form->{fromdate} || $form->{todate} ) {
  1504. if ( $form->{fromdate} ) {
  1505. $where .= " AND $transdate >= '$form->{fromdate}'";
  1506. }
  1507. if ( $form->{todate} ) {
  1508. $where .= " AND $transdate <= '$form->{todate}'";
  1509. }
  1510. }
  1511. if ( $form->{method} eq 'cash' ) {
  1512. $transdate = "a.datepaid";
  1513. my $todate = $form->{todate};
  1514. if ( !$todate ) {
  1515. ($todate) = $dbh->selectrow_array(qq|SELECT current_date|);
  1516. }
  1517. $cashwhere = qq|
  1518. AND ac.trans_id IN (
  1519. SELECT trans_id
  1520. FROM acc_trans
  1521. JOIN chart ON (chart_id = chart.id)
  1522. WHERE link LIKE '%${ARAP}_paid%'
  1523. AND $transdate <= | . $dbh->quote($todate) . qq|
  1524. AND a.paid = a.amount)|;
  1525. }
  1526. my $ml = ( $form->{db} eq 'ar' ) ? 1 : -1;
  1527. my %ordinal = ( 'transdate' => 3, 'invnumber' => 4, 'name' => 5 );
  1528. my @a = qw(transdate invnumber name);
  1529. my $sortorder = $form->sort_order( \@a, \%ordinal );
  1530. if ( $form->{summary} ) {
  1531. $query = qq|
  1532. SELECT a.id, a.invoice, $transdate AS transdate,
  1533. a.invnumber, n.name, a.netamount,
  1534. ac.amount * $ml AS tax, a.till
  1535. FROM acc_trans ac
  1536. JOIN $form->{db} a ON (a.id = ac.trans_id)
  1537. JOIN chart ch ON (ch.id = ac.chart_id)
  1538. JOIN $table n ON (n.id = a.${table}_id)
  1539. WHERE $where $accno $cashwhere |;
  1540. if ( $form->{fromdate} ) {
  1541. # include open transactions from previous period
  1542. if ($cashwhere) {
  1543. $query .= qq|
  1544. UNION
  1545. SELECT a.id, a.invoice,
  1546. $transdate AS transdate, a.invnumber,
  1547. n.name, a.netamount, ac.
  1548. amount * $ml AS tax, a.till
  1549. FROM acc_trans ac
  1550. JOIN $form->{db} a ON (a.id = ac.trans_id)
  1551. JOIN chart ch ON (ch.id = ac.chart_id)
  1552. JOIN $table n ON (n.id = a.${table}_id)
  1553. WHERE a.datepaid >= '$form->{fromdate}'
  1554. $accno $cashwhere|;
  1555. }
  1556. }
  1557. }
  1558. else {
  1559. $query = qq|
  1560. SELECT a.id, '0' AS invoice, $transdate AS transdate,
  1561. a.invnumber, n.name, a.netamount,
  1562. ac.amount * $ml AS tax, a.notes AS description,
  1563. a.till
  1564. FROM acc_trans ac
  1565. JOIN $form->{db} a ON (a.id = ac.trans_id)
  1566. JOIN chart ch ON (ch.id = ac.chart_id)
  1567. JOIN $table n ON (n.id = a.${table}_id)
  1568. WHERE $where $accno AND a.invoice = '0' $cashwhere
  1569. UNION
  1570. SELECT a.id, '1' AS invoice, $transdate AS transdate,
  1571. a.invnumber, n.name,
  1572. i.sellprice * i.qty * $ml AS netamount,
  1573. i.sellprice * i.qty * $ml *
  1574. (SELECT tx.rate FROM tax tx
  1575. WHERE tx.chart_id = ch.id
  1576. AND (tx.validto > $transdate
  1577. OR tx.validto IS NULL)
  1578. ORDER BY validto LIMIT 1)
  1579. AS tax, i.description, a.till
  1580. FROM acc_trans ac
  1581. JOIN $form->{db} a ON (a.id = ac.trans_id)
  1582. JOIN chart ch ON (ch.id = ac.chart_id)
  1583. JOIN $table n ON (n.id = a.${table}_id)
  1584. JOIN ${table}tax t
  1585. ON (t.${table}_id = n.id AND t.chart_id = ch.id)
  1586. JOIN invoice i ON (i.trans_id = a.id)
  1587. JOIN partstax pt
  1588. ON (pt.parts_id = i.parts_id
  1589. AND pt.chart_id = ch.id)
  1590. WHERE $where $accno AND a.invoice = '1' $cashwhere|;
  1591. if ( $form->{fromdate} ) {
  1592. if ($cashwhere) {
  1593. $query .= qq|
  1594. UNION
  1595. SELECT a.id, '0' AS invoice,
  1596. $transdate AS transdate,
  1597. a.invnumber, n.name, a.netamount,
  1598. ac.amount * $ml AS tax,
  1599. a.notes AS description, a.till
  1600. FROM acc_trans ac
  1601. JOIN $form->{db} a
  1602. ON (a.id = ac.trans_id)
  1603. JOIN chart ch ON (ch.id = ac.chart_id)
  1604. JOIN $table n
  1605. ON (n.id = a.${table}_id)
  1606. WHERE a.datepaid >= '$form->{fromdate}'
  1607. $accno AND a.invoice = '0'
  1608. $cashwhere
  1609. UNION
  1610. SELECT a.id, '1' AS invoice,
  1611. $transdate AS transdate,
  1612. a.invnumber, n.name,
  1613. i.sellprice * i.qty * $ml
  1614. AS netamount, i.sellprice
  1615. * i.qty * $ml *
  1616. (SELECT tx.rate FROM tax tx
  1617. WHERE tx.chart_id = ch.id
  1618. AND
  1619. (tx.validto > $transdate
  1620. OR tx.validto IS NULL)
  1621. ORDER BY validto LIMIT 1)
  1622. AS tax, i.description, a.till
  1623. FROM acc_trans ac
  1624. JOIN $form->{db} a
  1625. ON (a.id = ac.trans_id)
  1626. JOIN chart ch ON (ch.id = ac.chart_id)
  1627. JOIN $table n ON
  1628. (n.id = a.${table}_id)
  1629. JOIN ${table}tax t
  1630. ON (t.${table}_id = n.id
  1631. AND t.chart_id = ch.id)
  1632. JOIN invoice i ON (i.trans_id = a.id)
  1633. JOIN partstax pt
  1634. ON (pt.parts_id = i.parts_id
  1635. AND pt.chart_id = ch.id)
  1636. WHERE a.datepaid >= '$form->{fromdate}'
  1637. $accno AND a.invoice = '1'
  1638. $cashwhere|;
  1639. }
  1640. }
  1641. }
  1642. if ( $form->{report} =~ /nontaxable/ ) {
  1643. if ( $form->{summary} ) {
  1644. # only gather up non-taxable transactions
  1645. $query = qq|
  1646. SELECT DISTINCT a.id, a.invoice,
  1647. $transdate AS transdate, a.invnumber,
  1648. n.name, a.netamount, a.till
  1649. FROM acc_trans ac
  1650. JOIN $form->{db} a ON (a.id = ac.trans_id)
  1651. JOIN $table n ON (n.id = a.${table}_id)
  1652. WHERE $where AND a.netamount = a.amount
  1653. $cashwhere|;
  1654. if ( $form->{fromdate} ) {
  1655. if ($cashwhere) {
  1656. $query .= qq|
  1657. UNION
  1658. SELECT DISTINCT a.id, a.invoice,
  1659. $transdate AS transdate,
  1660. a.invnumber, n.name,
  1661. a.netamount, a.till
  1662. FROM acc_trans ac
  1663. JOIN $form->{db} a
  1664. ON (a.id = ac.trans_id)
  1665. JOIN $table n
  1666. ON (n.id = a.${table}_id)
  1667. WHERE a.datepaid
  1668. >= '$form->{fromdate}'
  1669. AND
  1670. a.netamount = a.amount
  1671. $cashwhere|;
  1672. }
  1673. }
  1674. }
  1675. else {
  1676. # gather up details for non-taxable transactions
  1677. $query = qq|
  1678. SELECT a.id, '0' AS invoice,
  1679. $transdate AS transdate, a.invnumber,
  1680. n.name, a.netamount,
  1681. a.notes AS description, a.till
  1682. FROM acc_trans ac
  1683. JOIN $form->{db} a ON (a.id = ac.trans_id)
  1684. JOIN $table n ON (n.id = a.${table}_id)
  1685. WHERE $where AND a.invoice = '0'
  1686. AND a.netamount = a.amount $cashwhere
  1687. GROUP BY a.id, $transdate, a.invnumber, n.name,
  1688. a.netamount, a.notes, a.till
  1689. UNION
  1690. SELECT a.id, '1' AS invoice,
  1691. $transdate AS transdate, a.invnumber,
  1692. n.name, sum(ac.sellprice * ac.qty)
  1693. * $ml AS netamount, ac.description,
  1694. a.till
  1695. FROM invoice ac
  1696. JOIN $form->{db} a ON (a.id = ac.trans_id)
  1697. JOIN $table n ON (n.id = a.${table}_id)
  1698. WHERE $where AND a.invoice = '1' AND
  1699. (a.${table}_id NOT IN
  1700. (SELECT ${table}_id FROM ${table}tax t
  1701. (${table}_id)
  1702. ) OR ac.parts_id NOT IN
  1703. (SELECT parts_id FROM partstax p
  1704. (parts_id))) $cashwhere
  1705. GROUP BY a.id, a.invnumber, $transdate, n.name,
  1706. ac.description, a.till|;
  1707. if ( $form->{fromdate} ) {
  1708. if ($cashwhere) {
  1709. $query .= qq|
  1710. UNION
  1711. SELECT a.id, '0' AS invoice,
  1712. $transdate AS transdate,
  1713. a.invnumber, n.name,
  1714. a.netamount,
  1715. a.notes AS description,
  1716. a.till
  1717. FROM acc_trans ac
  1718. JOIN $form->{db} a
  1719. ON (a.id = ac.trans_id)
  1720. JOIN $table n
  1721. ON (n.id = a.${table}_id)
  1722. WHERE a.datepaid
  1723. >= '$form->{fromdate}'
  1724. AND a.invoice = '0'
  1725. AND a.netamount
  1726. = a.amount $cashwhere
  1727. GROUP BY a.id, $transdate,
  1728. a.invnumber, n.name,
  1729. a.netamount, a.notes,
  1730. a.till
  1731. UNION
  1732. SELECT a.id, '1' AS invoice,
  1733. $transdate AS transdate,
  1734. a.invnumber, n.name,
  1735. sum(ac.sellprice
  1736. * ac.qty) * $ml
  1737. AS netamount,
  1738. ac.description, a.till
  1739. FROM invoice ac
  1740. JOIN $form->{db} a
  1741. ON (a.id = ac.trans_id)
  1742. JOIN $table n
  1743. ON (n.id = a.${table}_id)
  1744. WHERE a.datepaid
  1745. >= '$form->{fromdate}'
  1746. AND a.invoice = '1' AND
  1747. (a.${table}_id NOT IN
  1748. (SELECT ${table}_id
  1749. FROM ${table}tax t
  1750. (${table}_id)) OR
  1751. ac.parts_id NOT IN
  1752. (SELECT parts_id
  1753. FROM partstax p
  1754. (parts_id)))
  1755. $cashwhere
  1756. GROUP BY a.id, a.invnumber,
  1757. $transdate, n.name,
  1758. ac.description, a.till|;
  1759. }
  1760. }
  1761. }
  1762. }
  1763. $query .= qq| ORDER by $sortorder|;
  1764. $sth = $dbh->prepare($query);
  1765. $sth->execute || $form->dberror($query);
  1766. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1767. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1768. $ref->{tax} = $form->round_amount( $ref->{tax}, 2 );
  1769. if ( $form->{report} =~ /nontaxable/ ) {
  1770. push @{ $form->{TR} }, $ref if $ref->{netamount};
  1771. }
  1772. else {
  1773. push @{ $form->{TR} }, $ref if $ref->{tax};
  1774. }
  1775. }
  1776. $sth->finish;
  1777. $dbh->commit;
  1778. }
  1779. sub paymentaccounts {
  1780. my ( $self, $myconfig, $form ) = @_;
  1781. my $dbh = $form->{dbh};
  1782. my $ARAP = uc $form->{db};
  1783. # get A(R|P)_paid accounts
  1784. my $query = qq|
  1785. SELECT accno, description FROM chart
  1786. WHERE link LIKE '%${ARAP}_paid%'
  1787. ORDER BY accno|;
  1788. my $sth = $dbh->prepare($query);
  1789. $sth->execute || $form->dberror($query);
  1790. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1791. push @{ $form->{PR} }, $ref;
  1792. }
  1793. $sth->finish;
  1794. $form->all_years( $myconfig, $dbh );
  1795. $dbh->{dbh};
  1796. }
  1797. sub payments {
  1798. my ( $self, $myconfig, $form ) = @_;
  1799. my $dbh = $form->{dbh};
  1800. my $ml = 1;
  1801. if ( $form->{db} eq 'ar' ) {
  1802. $table = 'customer';
  1803. $ml = -1;
  1804. }
  1805. if ( $form->{db} eq 'ap' ) {
  1806. $table = 'vendor';
  1807. }
  1808. my $query;
  1809. my $sth;
  1810. my $dpt_join;
  1811. my $where;
  1812. my $var;
  1813. if ( $form->{department_id} ) {
  1814. $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id)|;
  1815. $where =
  1816. qq| AND t.department_id = | . $dbh->quote( $form->{department_id} );
  1817. }
  1818. ( $form->{fromdate}, $form->{todate} ) =
  1819. $form->from_to( $form->{year}, $form->{month}, $form->{interval} )
  1820. if $form->{year} && $form->{month};
  1821. if ( $form->{fromdate} ) {
  1822. $where .= " AND ac.transdate >= " . $dbh->quote( $form->{fromdate} );
  1823. }
  1824. if ( $form->{todate} ) {
  1825. $where .= " AND ac.transdate <= " . $dbh->quote( $form->{todate} );
  1826. }
  1827. if ( !$form->{fx_transaction} ) {
  1828. $where .= " AND ac.fx_transaction = '0'";
  1829. }
  1830. if ( $form->{description} ne "" ) {
  1831. $var = $dbh->quote( $form->like( lc $form->{description} ) );
  1832. $where .= " AND lower(c.name) LIKE $var";
  1833. }
  1834. if ( $form->{source} ne "" ) {
  1835. $var = $dbh->quote( $form->like( lc $form->{source} ) );
  1836. $where .= " AND lower(ac.source) LIKE $var";
  1837. }
  1838. if ( $form->{memo} ne "" ) {
  1839. $var = $dbh->quote( $form->like( lc $form->{memo} ) );
  1840. $where .= " AND lower(ac.memo) LIKE $var";
  1841. }
  1842. my %ordinal = (
  1843. 'name' => 1,
  1844. 'transdate' => 2,
  1845. 'source' => 4,
  1846. 'employee' => 6,
  1847. 'till' => 7
  1848. );
  1849. my @a = qw(name transdate employee);
  1850. my $sortorder = $form->sort_order( \@a, \%ordinal );
  1851. my $glwhere = $where;
  1852. $glwhere =~ s/\(c.name\)/\(g.description\)/;
  1853. # cycle through each id
  1854. foreach my $accno ( split( / /, $form->{paymentaccounts} ) ) {
  1855. $query = qq|
  1856. SELECT id, accno, description
  1857. FROM chart
  1858. WHERE accno = ?|;
  1859. $sth = $dbh->prepare($query);
  1860. $sth->execute($accno) || $form->dberror($query);
  1861. my $ref = $sth->fetchrow_hashref(NAME_lc);
  1862. push @{ $form->{PR} }, $ref;
  1863. $sth->finish;
  1864. $query = qq|
  1865. SELECT c.name, ac.transdate,
  1866. sum(ac.amount) * $ml AS paid, ac.source,
  1867. ac.memo, e.name AS employee, a.till, a.curr
  1868. FROM acc_trans ac
  1869. JOIN $form->{db} a ON (ac.trans_id = a.id)
  1870. JOIN $table c ON (c.id = a.${table}_id)
  1871. LEFT JOIN employee e ON (a.employee_id = e.id)
  1872. $dpt_join
  1873. WHERE ac.chart_id = $ref->{id} $where|;
  1874. if ( $form->{till} ne "" ) {
  1875. $query .= " AND a.invoice = '1' AND NOT a.till IS NULL";
  1876. if ( $myconfig->{role} eq 'user' ) {
  1877. $query .= " AND e.login = '$form->{login}'";
  1878. }
  1879. }
  1880. $query .= qq|
  1881. GROUP BY c.name, ac.transdate, ac.source, ac.memo,
  1882. e.name, a.till, a.curr|;
  1883. if ( $form->{till} eq "" ) {
  1884. $query .= qq|
  1885. UNION
  1886. SELECT g.description, ac.transdate,
  1887. sum(ac.amount) * $ml AS paid, ac.source,
  1888. ac.memo, e.name AS employee, '' AS till,
  1889. '' AS curr
  1890. FROM acc_trans ac
  1891. JOIN gl g ON (g.id = ac.trans_id)
  1892. LEFT
  1893. JOIN employee e ON (g.employee_id = e.id)
  1894. $dpt_join
  1895. WHERE ac.chart_id = $ref->{id} $glwhere
  1896. AND (ac.amount * $ml) > 0
  1897. GROUP BY g.description, ac.transdate,
  1898. ac.source, ac.memo, e.name|;
  1899. }
  1900. $query .= qq| ORDER BY $sortorder|;
  1901. $sth = $dbh->prepare($query);
  1902. $sth->execute || $form->dberror($query);
  1903. while ( my $pr = $sth->fetchrow_hashref(NAME_lc) ) {
  1904. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1905. push @{ $form->{ $ref->{id} } }, $pr;
  1906. }
  1907. $sth->finish;
  1908. }
  1909. $dbh->commit;
  1910. }
  1911. 1;