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