summaryrefslogtreecommitdiff
path: root/LedgerSMB/RP.pm
blob: b51d729019d5381823982261f536baf4846ada69 (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. $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. $dpt_join
  915. WHERE $where $ywhere $dpt_where $category
  916. $project
  917. GROUP BY g.accno, g.description, c.category
  918. UNION ALL
  919. SELECT '' AS accno, SUM(ac.amount) AS amount,
  920. '' AS description, c.category
  921. FROM acc_trans ac
  922. JOIN chart c ON (c.id = ac.chart_id)
  923. $dpt_join
  924. WHERE $where $ywhere $dpt_where $category
  925. AND c.gifi_accno = '' $project
  926. GROUP BY c.category|;
  927. if ($excludeyearend) {
  928. $query .= qq|
  929. UNION ALL
  930. SELECT g.accno,
  931. sum(ac.amount)
  932. AS amount,
  933. g.description,
  934. c.category
  935. FROM yearend y
  936. JOIN gl a
  937. ON (a.id = y.trans_id)
  938. JOIN acc_trans ac
  939. ON (ac.trans_id =
  940. y.trans_id)
  941. JOIN chart c
  942. ON
  943. (c.id = ac.chart_id)
  944. JOIN gifi g
  945. ON (g.accno =
  946. c.gifi_accno)
  947. $dpt_join
  948. WHERE $yearendwhere
  949. AND c.category = 'Q'
  950. $dpt_where $project
  951. GROUP BY g.accno,
  952. g.description,
  953. c.category|;
  954. }
  955. }
  956. }
  957. else { # standard account
  958. if ( $form->{method} eq 'cash' ) {
  959. $query = qq|
  960. SELECT c.accno, sum(ac.amount) AS amount,
  961. c.description, c.category
  962. FROM acc_trans ac
  963. JOIN chart c ON (c.id = ac.chart_id)
  964. JOIN ar a ON (a.id = ac.trans_id) $dpt_join
  965. WHERE $where $ywhere $dpt_where $category
  966. AND ac.trans_id IN (
  967. SELECT trans_id FROM acc_trans
  968. JOIN chart ON (chart_id = id)
  969. WHERE link LIKE '%AR_paid%' $subwhere)
  970. $project
  971. GROUP BY c.accno, c.description, c.category
  972. UNION ALL
  973. SELECT c.accno, sum(ac.amount) AS amount,
  974. c.description, c.category
  975. FROM acc_trans ac
  976. JOIN chart c ON (c.id = ac.chart_id)
  977. JOIN ap a ON (a.id = ac.trans_id) $dpt_join
  978. WHERE $where $ywhere $dpt_where $category
  979. AND ac.trans_id IN (
  980. SELECT trans_id FROM acc_trans
  981. JOIN chart ON (chart_id = id)
  982. WHERE link LIKE '%AP_paid%' $subwhere)
  983. $project
  984. GROUP BY c.accno, c.description, c.category
  985. UNION ALL
  986. SELECT c.accno, sum(ac.amount) AS amount,
  987. c.description, c.category
  988. FROM acc_trans ac
  989. JOIN chart c ON (c.id = ac.chart_id)
  990. JOIN gl a ON (a.id = ac.trans_id) $dpt_join
  991. WHERE $where $ywhere $glwhere $dpt_where $category
  992. AND NOT (c.link = 'AR' OR c.link = 'AP')
  993. $project
  994. GROUP BY c.accno, c.description, c.category|;
  995. if ($excludeyearend) {
  996. # this is for the yearend
  997. $query .= qq|
  998. UNION ALL
  999. SELECT c.accno,
  1000. sum(ac.amount) AS amount,
  1001. c.description, c.category
  1002. FROM yearend y
  1003. JOIN gl a ON (a.id = y.trans_id)
  1004. JOIN acc_trans ac
  1005. ON (ac.trans_id = y.trans_id)
  1006. JOIN chart c
  1007. ON (c.id = ac.chart_id)
  1008. $dpt_join
  1009. WHERE $yearendwhere AND
  1010. c.category = 'Q' $dpt_where
  1011. $project
  1012. GROUP BY c.accno, c.description,
  1013. c.category|;
  1014. }
  1015. }
  1016. else {
  1017. if ($department_id) {
  1018. $dpt_join = qq|
  1019. JOIN dpt_trans t
  1020. ON (t.trans_id = ac.trans_id)|;
  1021. $dpt_where =
  1022. qq| AND t.department_id = | . $dbh->quote($department_id);
  1023. }
  1024. $query = qq|
  1025. SELECT c.accno, sum(ac.amount) AS amount,
  1026. c.description, c.category
  1027. FROM acc_trans ac
  1028. JOIN chart c ON (c.id = ac.chart_id)
  1029. $dpt_join
  1030. WHERE $where $ywhere $dpt_where $category
  1031. $project
  1032. GROUP BY c.accno, c.description, c.category|;
  1033. if ($excludeyearend) {
  1034. $query .= qq|
  1035. UNION ALL
  1036. SELECT c.accno,
  1037. sum(ac.amount) AS amount,
  1038. c.description, c.category
  1039. FROM yearend y
  1040. JOIN gl a ON (a.id = y.trans_id)
  1041. JOIN acc_trans ac
  1042. ON (ac.trans_id = y.trans_id)
  1043. JOIN chart c
  1044. ON (c.id = ac.chart_id)
  1045. $dpt_join
  1046. WHERE $yearendwhere AND
  1047. c.category = 'Q' $dpt_where
  1048. $project
  1049. GROUP BY c.accno, c.description,
  1050. c.category|;
  1051. }
  1052. }
  1053. }
  1054. my @accno;
  1055. my $accno;
  1056. my $ref;
  1057. my $sth = $dbh->prepare($query);
  1058. $sth->execute || $form->dberror($query);
  1059. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1060. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1061. # get last heading account
  1062. @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
  1063. $accno = pop @accno;
  1064. if ( $accno && ( $accno ne $ref->{accno} ) ) {
  1065. if ($last_period) {
  1066. $form->{ $ref->{category} }{$accno}{last} += $ref->{amount};
  1067. }
  1068. else {
  1069. $form->{ $ref->{category} }{$accno}{this} += $ref->{amount};
  1070. }
  1071. }
  1072. $form->{ $ref->{category} }{ $ref->{accno} }{accno} = $ref->{accno};
  1073. $form->{ $ref->{category} }{ $ref->{accno} }{description} =
  1074. $ref->{description};
  1075. $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "A";
  1076. if ($last_period) {
  1077. $form->{ $ref->{category} }{ $ref->{accno} }{last} +=
  1078. $ref->{amount};
  1079. }
  1080. else {
  1081. $form->{ $ref->{category} }{ $ref->{accno} }{this} +=
  1082. $ref->{amount};
  1083. }
  1084. }
  1085. $sth->finish;
  1086. # remove accounts with zero balance
  1087. foreach $category ( @{$categories} ) {
  1088. foreach $accno ( keys %{ $form->{$category} } ) {
  1089. $form->{$category}{$accno}{last} =
  1090. $form->round_amount( $form->{$category}{$accno}{last},
  1091. $form->{decimalplaces} );
  1092. $form->{$category}{$accno}{this} =
  1093. $form->round_amount( $form->{$category}{$accno}{this},
  1094. $form->{decimalplaces} );
  1095. delete $form->{$category}{$accno}
  1096. if ( $form->{$category}{$accno}{this} == 0
  1097. && $form->{$category}{$accno}{last} == 0 );
  1098. }
  1099. }
  1100. }
  1101. sub trial_balance {
  1102. my ( $self, $myconfig, $form ) = @_;
  1103. my $dbh = $form->{dbh};
  1104. my ( $query, $sth, $ref );
  1105. my %balance = ();
  1106. my %trb = ();
  1107. my $null;
  1108. my $department_id;
  1109. my $project_id;
  1110. my @headingaccounts = ();
  1111. my $dpt_where;
  1112. my $dpt_join;
  1113. my $project;
  1114. my $where = "1 = 1";
  1115. my $invwhere = $where;
  1116. ( $null, $department_id ) = split /--/, $form->{department};
  1117. ( $null, $project_id ) = split /--/, $form->{projectnumber};
  1118. if ($department_id) {
  1119. $dpt_join = qq|
  1120. JOIN dpt_trans t ON (ac.trans_id = t.trans_id)|;
  1121. $dpt_where = qq|
  1122. AND t.department_id = | . $dbh->quote($department_id);
  1123. }
  1124. if ($project_id) {
  1125. $project = qq|
  1126. AND ac.project_id = | . $dbh->quote($project_id);
  1127. }
  1128. ( $form->{fromdate}, $form->{todate} ) =
  1129. $form->from_to( $form->{year}, $form->{month}, $form->{interval} )
  1130. if $form->{year} && $form->{month};
  1131. # get beginning balances
  1132. if ( $form->{fromdate} ) {
  1133. if ( $form->{accounttype} eq 'gifi' ) {
  1134. $query = qq|
  1135. SELECT g.accno, c.category,
  1136. SUM(ac.amount) AS amount,
  1137. g.description, c.contra
  1138. FROM acc_trans ac
  1139. JOIN chart c ON (ac.chart_id = c.id)
  1140. JOIN gifi g ON (c.gifi_accno = g.accno)
  1141. $dpt_join
  1142. WHERE ac.transdate < '$form->{fromdate}'
  1143. $dpt_where $project
  1144. GROUP BY g.accno, c.category, g.description,
  1145. c.contra|;
  1146. }
  1147. else {
  1148. $query = qq|
  1149. SELECT c.accno, c.category,
  1150. SUM(ac.amount) AS amount,
  1151. c.description, c.contra
  1152. FROM acc_trans ac
  1153. JOIN chart c ON (ac.chart_id = c.id)
  1154. $dpt_join
  1155. WHERE ac.transdate < '$form->{fromdate}'
  1156. $dpt_where $project
  1157. GROUP BY c.accno, c.category, c.description,
  1158. c.contra|;
  1159. }
  1160. $sth = $dbh->prepare($query);
  1161. $sth->execute || $form->dberror($query);
  1162. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1163. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1164. $ref->{amount} = $form->round_amount( $ref->{amount}, 2 );
  1165. $balance{ $ref->{accno} } = $ref->{amount};
  1166. if ( $form->{all_accounts} ) {
  1167. $trb{ $ref->{accno} }{description} = $ref->{description};
  1168. $trb{ $ref->{accno} }{charttype} = 'A';
  1169. $trb{ $ref->{accno} }{category} = $ref->{category};
  1170. $trb{ $ref->{accno} }{contra} = $ref->{contra};
  1171. }
  1172. }
  1173. $sth->finish;
  1174. }
  1175. # get headings
  1176. $query = qq|
  1177. SELECT c.accno, c.description, c.category FROM chart c
  1178. WHERE c.charttype = 'H'
  1179. ORDER by c.accno|;
  1180. if ( $form->{accounttype} eq 'gifi' ) {
  1181. $query = qq|
  1182. SELECT g.accno, g.description, c.category, c.contra
  1183. FROM gifi g
  1184. JOIN chart c ON (c.gifi_accno = g.accno)
  1185. WHERE c.charttype = 'H'
  1186. ORDER BY g.accno|;
  1187. }
  1188. $sth = $dbh->prepare($query);
  1189. $sth->execute || $form->dberror($query);
  1190. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1191. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1192. $trb{ $ref->{accno} }{description} = $ref->{description};
  1193. $trb{ $ref->{accno} }{charttype} = 'H';
  1194. $trb{ $ref->{accno} }{category} = $ref->{category};
  1195. $trb{ $ref->{accno} }{contra} = $ref->{contra};
  1196. push @headingaccounts, $ref->{accno};
  1197. }
  1198. $sth->finish;
  1199. if ( $form->{fromdate} || $form->{todate} ) {
  1200. if ( $form->{fromdate} ) {
  1201. $where .=
  1202. " AND ac.transdate >= " . $dbh->quote( $form->{fromdate} );
  1203. $invwhere .=
  1204. " AND a.transdate >= " . $dbh->quote( $form->{fromdate} );
  1205. }
  1206. if ( $form->{todate} ) {
  1207. $where .= " AND ac.transdate <= " . $dbh->quote( $form->{todate} );
  1208. $invwhere .=
  1209. " AND a.transdate <= " . $dbh->quote( $form->{todate} );
  1210. }
  1211. }
  1212. if ( $form->{accounttype} eq 'gifi' ) {
  1213. $query = qq|
  1214. SELECT g.accno, g.description, c.category,
  1215. SUM(ac.amount) AS amount, c.contra
  1216. FROM acc_trans ac
  1217. JOIN chart c ON (c.id = ac.chart_id)
  1218. JOIN gifi g ON (c.gifi_accno = g.accno)
  1219. $dpt_join
  1220. WHERE $where $dpt_where $project
  1221. GROUP BY g.accno, g.description, c.category, c.contra
  1222. ORDER BY accno|;
  1223. }
  1224. else {
  1225. $query = qq|
  1226. SELECT c.accno, c.description, c.category,
  1227. SUM(ac.amount) AS amount, c.contra
  1228. FROM acc_trans ac
  1229. JOIN chart c ON (c.id = ac.chart_id)
  1230. $dpt_join
  1231. WHERE $where $dpt_where $project
  1232. GROUP BY c.accno, c.description, c.category, c.contra
  1233. ORDER BY accno|;
  1234. }
  1235. $sth = $dbh->prepare($query);
  1236. $sth->execute || $form->dberror($query);
  1237. # prepare query for each account
  1238. $query = qq|
  1239. SELECT (SELECT SUM(ac.amount) * -1 FROM acc_trans ac
  1240. JOIN chart c ON (c.id = ac.chart_id)
  1241. $dpt_join
  1242. WHERE $where $dpt_where $project AND ac.amount < 0
  1243. AND c.accno = ?) AS debit,
  1244. (SELECT SUM(ac.amount) FROM acc_trans ac
  1245. JOIN chart c ON (c.id = ac.chart_id)
  1246. $dpt_join
  1247. WHERE $where $dpt_where $project AND ac.amount > 0
  1248. AND c.accno = ?) AS credit |;
  1249. if ( $form->{accounttype} eq 'gifi' ) {
  1250. $query = qq|
  1251. SELECT (SELECT SUM(ac.amount) * -1
  1252. FROM acc_trans ac
  1253. JOIN chart c ON (c.id = ac.chart_id)
  1254. $dpt_join
  1255. WHERE $where $dpt_where $project AND ac.amount < 0
  1256. AND c.gifi_accno = ?) AS debit,
  1257. (SELECT SUM(ac.amount)
  1258. FROM acc_trans ac
  1259. JOIN chart c ON (c.id = ac.chart_id)
  1260. $dpt_join
  1261. WHERE $where $dpt_where $project AND ac.amount > 0
  1262. AND c.gifi_accno = ?) AS credit|;
  1263. }
  1264. $drcr = $dbh->prepare($query);
  1265. # calculate debit and credit for the period
  1266. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1267. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1268. $trb{ $ref->{accno} }{description} = $ref->{description};
  1269. $trb{ $ref->{accno} }{charttype} = 'A';
  1270. $trb{ $ref->{accno} }{category} = $ref->{category};
  1271. $trb{ $ref->{accno} }{contra} = $ref->{contra};
  1272. $trb{ $ref->{accno} }{amount} += $ref->{amount};
  1273. }
  1274. $sth->finish;
  1275. my ( $debit, $credit );
  1276. foreach my $accno ( sort keys %trb ) {
  1277. $ref = ();
  1278. $ref->{accno} = $accno;
  1279. for (qw(description category contra charttype amount)) {
  1280. $ref->{$_} = $trb{$accno}{$_};
  1281. }
  1282. $ref->{balance} = $balance{ $ref->{accno} };
  1283. if ( $trb{$accno}{charttype} eq 'A' ) {
  1284. if ($project_id) {
  1285. if ( $ref->{amount} < 0 ) {
  1286. $ref->{debit} = $ref->{amount} * -1;
  1287. }
  1288. else {
  1289. $ref->{credit} = $ref->{amount};
  1290. }
  1291. next if $form->round_amount( $ref->{amount}, 2 ) == 0;
  1292. }
  1293. else {
  1294. # get DR/CR
  1295. $drcr->execute( $ref->{accno}, $ref->{accno} )
  1296. || $form->dberror($query);
  1297. ( $debit, $credit ) = ( 0, 0 );
  1298. while ( my @drcrlist = $drcr->fetchrow_array ) {
  1299. $form->db_parse_numeric(sth=>$drcr, arrayref=>\@drcrlist);
  1300. ($debit, $credit) = @drcrlist;
  1301. $ref->{debit} += $debit;
  1302. $ref->{credit} += $credit;
  1303. }
  1304. $drcr->finish;
  1305. }
  1306. $ref->{debit} = $form->round_amount( $ref->{debit}, 2 );
  1307. $ref->{credit} = $form->round_amount( $ref->{credit}, 2 );
  1308. if ( !$form->{all_accounts} ) {
  1309. next
  1310. if $form->round_amount( $ref->{debit} + $ref->{credit}, 2 ) ==
  1311. 0;
  1312. }
  1313. }
  1314. # add subtotal
  1315. @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
  1316. $accno = pop @accno;
  1317. if ($accno) {
  1318. $trb{$accno}{debit} += $ref->{debit};
  1319. $trb{$accno}{credit} += $ref->{credit};
  1320. }
  1321. push @{ $form->{TB} }, $ref;
  1322. }
  1323. $dbh->commit;
  1324. # debits and credits for headings
  1325. foreach $accno (@headingaccounts) {
  1326. foreach $ref ( @{ $form->{TB} } ) {
  1327. if ( $accno eq $ref->{accno} ) {
  1328. $ref->{debit} = $trb{$accno}{debit};
  1329. $ref->{credit} = $trb{$accno}{credit};
  1330. }
  1331. }
  1332. }
  1333. }
  1334. sub aging {
  1335. my ( $self, $myconfig, $form ) = @_;
  1336. my $ref;
  1337. my $department_id;
  1338. my $null;
  1339. my $dbh = $form->{dbh};
  1340. my $invoice = ( $form->{arap} eq 'ar' ) ? 'is' : 'ir';
  1341. my $query = qq|SELECT value FROM defaults WHERE setting_key = 'curr'|;
  1342. ( $form->{currencies} ) = $dbh->selectrow_array($query);
  1343. ( $null, $form->{todate} ) = $form->from_to( $form->{year}, $form->{month} )
  1344. if $form->{year} && $form->{month};
  1345. if ( !$form->{todate} ) {
  1346. $query = qq|SELECT current_date|;
  1347. ( $form->{todate} ) = $dbh->selectrow_array($query);
  1348. }
  1349. my $transdate = ( $form->{overdue} ) ? "duedate" : "transdate";
  1350. if ( $form->{department} ) {
  1351. ( $null, $department_id ) = split /--/, $form->{department};
  1352. }
  1353. my $buysell = ( $form->{arap} eq 'ar' ) ? 'buy' : 'sell';
  1354. my $todate = $dbh->quote( $form->{todate} );
  1355. my %interval = (
  1356. 'c0' => "(date $todate - interval '0 days')",
  1357. 'c30' => "(date $todate - interval '30 days')",
  1358. 'c60' => "(date $todate - interval '60 days')",
  1359. 'c90' => "(date $todate - interval '90 days')"
  1360. );
  1361. # for each company that has some stuff outstanding
  1362. $form->{currencies} ||= ":";
  1363. $where = qq|a.paid != a.amount|;
  1364. if ( $form->{"$form->{ct}_id"} ) {
  1365. $where .= qq| AND c.entity_id = | . $dbh->quote( $form->{"$form->{ct}_id"} );
  1366. }
  1367. if ($department_id) {
  1368. $where .= qq| AND a.department_id = | . $dbh->quote($department_id);
  1369. }
  1370. $query = "";
  1371. my $union = "";
  1372. $query .= qq|
  1373. SELECT c.entity_id AS ctid,
  1374. c.meta_number as $form->{ct}number, e.legal_name as name,
  1375. '' as address1, '' as address2, '' as city,
  1376. '' as state,
  1377. '' as zipcode,
  1378. '' as country, '' as contact, '' as email,
  1379. '' as $form->{ct}phone,
  1380. '' as $form->{ct}fax,
  1381. '' as $form->{ct}taxnumber,
  1382. a.invnumber, a.transdate, a.till, a.ordnumber,
  1383. a.ponumber, a.notes, c.language_code,
  1384. CASE WHEN
  1385. EXTRACT(days FROM age(a.transdate)/30)
  1386. = 0
  1387. THEN (a.amount - a.paid) ELSE 0 END
  1388. as c0,
  1389. CASE WHEN EXTRACT(days FROM age(a.transdate)/30)
  1390. = 1
  1391. THEN (a.amount - a.paid) ELSE 0 END
  1392. as c30,
  1393. CASE WHEN EXTRACT(days FROM age(a.transdate)/30)
  1394. = 2
  1395. THEN (a.amount - a.paid) ELSE 0 END
  1396. as c60,
  1397. CASE WHEN EXTRACT(days FROM age(a.transdate)/30)
  1398. > 2
  1399. THEN (a.amount - a.paid) ELSE 0 END
  1400. as c90,
  1401. a.duedate, a.invoice, a.id, a.curr,
  1402. (SELECT $buysell FROM exchangerate e
  1403. WHERE a.curr = e.curr
  1404. AND e.transdate = a.transdate)
  1405. AS exchangerate
  1406. FROM $form->{arap} a
  1407. JOIN entity_credit_account c USING (entity_id)
  1408. JOIN company e USING (entity_id)
  1409. WHERE $where|;
  1410. $query .= qq| ORDER BY ctid, curr, $transdate, invnumber|;
  1411. $sth = $dbh->prepare($query) || $form->dberror($query);
  1412. $sth->execute();
  1413. while ( $ref = $sth->fetchrow_hashref('NAME_lc') ) {
  1414. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1415. $ref->{module} =
  1416. ( $ref->{invoice} )
  1417. ? $invoice
  1418. : $form->{arap};
  1419. $ref->{module} = 'ps' if $ref->{till};
  1420. $ref->{exchangerate} = 1
  1421. unless $ref->{exchangerate};
  1422. push @{ $form->{AG} }, $ref;
  1423. }
  1424. $sth->finish;
  1425. # get language
  1426. my $query = qq|SELECT code, description FROM language ORDER BY 2|;
  1427. $sth = $dbh->prepare($query);
  1428. $sth->execute || $form->dberror($query);
  1429. while ( $ref = $sth->fetchrow_hashref('NAME_lc') ) {
  1430. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1431. push @{ $form->{all_language} }, $ref;
  1432. }
  1433. $sth->finish;
  1434. $dbh->commit;
  1435. }
  1436. sub get_customer {
  1437. my ( $self, $myconfig, $form ) = @_;
  1438. my $dbh = $form->{dbh};
  1439. my $query = qq|
  1440. SELECT name, email, cc, bcc FROM $form->{ct} ct
  1441. WHERE ct.id = ?|;
  1442. $sth = $dbh->prepare($query);
  1443. $sth->execute( $form->{"$form->{ct}_id"} );
  1444. ( $form->{ $form->{ct} }, $form->{email}, $form->{cc}, $form->{bcc} ) =
  1445. $sth->fetchrow_array();
  1446. $dbh->commit;
  1447. }
  1448. sub get_taxaccounts {
  1449. my ( $self, $myconfig, $form ) = @_;
  1450. my $dbh = $form->{dbh};
  1451. my $ARAP = uc $form->{db};
  1452. # get tax accounts
  1453. my $query = qq|
  1454. SELECT DISTINCT c.accno, c.description
  1455. FROM chart c
  1456. JOIN tax t ON (c.id = t.chart_id)
  1457. WHERE c.link LIKE '%${ARAP}_tax%'
  1458. ORDER BY c.accno|;
  1459. my $sth = $dbh->prepare($query);
  1460. $sth->execute || $form->dberror;
  1461. my $ref = ();
  1462. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1463. push @{ $form->{taxaccounts} }, $ref;
  1464. }
  1465. $sth->finish;
  1466. # get gifi tax accounts
  1467. my $query = qq|
  1468. SELECT DISTINCT g.accno, g.description
  1469. FROM gifi g
  1470. JOIN chart c ON (c.gifi_accno= g.accno)
  1471. JOIN tax t ON (c.id = t.chart_id)
  1472. WHERE c.link LIKE '%${ARAP}_tax%'
  1473. ORDER BY accno|;
  1474. my $sth = $dbh->prepare($query);
  1475. $sth->execute || $form->dberror;
  1476. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1477. push @{ $form->{gifi_taxaccounts} }, $ref;
  1478. }
  1479. $sth->finish;
  1480. $dbh->commit;
  1481. }
  1482. sub tax_report {
  1483. my ( $self, $myconfig, $form ) = @_;
  1484. my $dbh = $form->{dbh};
  1485. my ( $null, $department_id ) = split /--/, $form->{department};
  1486. # build WHERE
  1487. my $where = "1 = 1";
  1488. my $cashwhere = "";
  1489. if ($department_id) {
  1490. $where .= qq|AND a.department_id = | . $dbh->quote($department_id);
  1491. }
  1492. my $query;
  1493. my $sth;
  1494. my $accno;
  1495. if ( $form->{accno} ) {
  1496. if ( $form->{accno} =~ /^gifi_/ ) {
  1497. ( $null, $accno ) = split /_/, $form->{accno};
  1498. $accno = $dbh->quote($accno);
  1499. $accno = qq| AND ch.gifi_accno = $accno|;
  1500. }
  1501. else {
  1502. $accno = $dbh->quote( $form->{accno} );
  1503. $accno = qq| AND ch.accno = $accno|;
  1504. }
  1505. }
  1506. my $table;
  1507. my $ARAP;
  1508. if ( $form->{db} eq 'ar' ) {
  1509. $table = "customer";
  1510. $ARAP = "AR";
  1511. }
  1512. if ( $form->{db} eq 'ap' ) {
  1513. $table = "vendor";
  1514. $ARAP = "AP";
  1515. }
  1516. my $transdate = "a.transdate";
  1517. ( $form->{fromdate}, $form->{todate} ) =
  1518. $form->from_to( $form->{year}, $form->{month}, $form->{interval} )
  1519. if $form->{year} && $form->{month};
  1520. # if there are any dates construct a where
  1521. if ( $form->{fromdate} || $form->{todate} ) {
  1522. if ( $form->{fromdate} ) {
  1523. $where .= " AND $transdate >= '$form->{fromdate}'";
  1524. }
  1525. if ( $form->{todate} ) {
  1526. $where .= " AND $transdate <= '$form->{todate}'";
  1527. }
  1528. }
  1529. if ( $form->{method} eq 'cash' ) {
  1530. $transdate = "a.datepaid";
  1531. my $todate = $form->{todate};
  1532. if ( !$todate ) {
  1533. ($todate) = $dbh->selectrow_array(qq|SELECT current_date|);
  1534. }
  1535. $cashwhere = qq|
  1536. AND ac.trans_id IN (
  1537. SELECT trans_id
  1538. FROM acc_trans
  1539. JOIN chart ON (chart_id = chart.id)
  1540. WHERE link LIKE '%${ARAP}_paid%'
  1541. AND $transdate <= | . $dbh->quote($todate) . qq|
  1542. AND a.paid = a.amount)|;
  1543. }
  1544. my $ml = ( $form->{db} eq 'ar' ) ? 1 : -1;
  1545. my %ordinal = ( 'transdate' => 3, 'invnumber' => 4, 'name' => 5 );
  1546. my @a = qw(transdate invnumber name);
  1547. my $sortorder = $form->sort_order( \@a, \%ordinal );
  1548. if ( $form->{summary} ) {
  1549. $query = qq|
  1550. SELECT a.id, a.invoice, $transdate AS transdate,
  1551. a.invnumber, c.legal_name AS name, a.netamount,
  1552. ac.amount * $ml AS tax, a.till
  1553. FROM acc_trans ac
  1554. JOIN $form->{db} a ON (a.id = ac.trans_id)
  1555. JOIN chart ch ON (ch.id = ac.chart_id)
  1556. JOIN $table n ON (n.entity_id = a.entity_id)
  1557. JOIN company c ON (c.entity_id = n.entity_id)
  1558. WHERE $where $accno $cashwhere |;
  1559. if ( $form->{fromdate} ) {
  1560. # include open transactions from previous period
  1561. if ($cashwhere) {
  1562. $query .= qq|
  1563. UNION
  1564. SELECT a.id, a.invoice,
  1565. $transdate AS transdate, a.invnumber,
  1566. c.legal_name AS name, a.netamount, ac.
  1567. amount * $ml AS tax, a.till
  1568. FROM acc_trans ac
  1569. JOIN $form->{db} a ON (a.id = ac.trans_id)
  1570. JOIN chart ch ON (ch.id = ac.chart_id)
  1571. JOIN $table n ON (n.entity_id = a.entity_id)
  1572. JOIN company c ON (c.entity_id = n.entity_id)
  1573. WHERE a.datepaid >= '$form->{fromdate}'
  1574. $accno $cashwhere|;
  1575. }
  1576. }
  1577. }
  1578. else {
  1579. $query = qq|
  1580. SELECT a.id, '0' AS invoice, $transdate AS transdate,
  1581. a.invnumber, c.legal_name AS name, a.netamount,
  1582. ac.amount * $ml AS tax, a.notes AS description,
  1583. a.till
  1584. FROM acc_trans ac
  1585. JOIN $form->{db} a ON (a.id = ac.trans_id)
  1586. JOIN chart ch ON (ch.id = ac.chart_id)
  1587. JOIN $table n ON (n.entity_id = a.entity_id)
  1588. JOIN company c ON (c.entity_id = a.entity_id)
  1589. WHERE $where $accno AND a.invoice = '0' $cashwhere
  1590. UNION
  1591. SELECT a.id, '1' AS invoice, $transdate AS transdate,
  1592. a.invnumber, c.legal_name AS name,
  1593. i.sellprice * i.qty * $ml AS netamount,
  1594. i.sellprice * i.qty * $ml *
  1595. (SELECT tx.rate FROM tax tx
  1596. WHERE tx.chart_id = ch.id
  1597. AND (tx.validto > $transdate
  1598. OR tx.validto IS NULL)
  1599. ORDER BY validto LIMIT 1)
  1600. AS tax, i.description, a.till
  1601. FROM acc_trans ac
  1602. JOIN $form->{db} a ON (a.id = ac.trans_id)
  1603. JOIN chart ch ON (ch.id = ac.chart_id)
  1604. JOIN $table n ON (n.entity_id = a.entity_id)
  1605. JOIN company c ON (c.entity_id = n.entity_id)
  1606. JOIN ${table}tax t
  1607. ON (t.${table}_id = n.id AND t.chart_id = ch.id)
  1608. JOIN invoice i ON (i.trans_id = a.id)
  1609. JOIN partstax pt
  1610. ON (pt.parts_id = i.parts_id
  1611. AND pt.chart_id = ch.id)
  1612. WHERE $where $accno AND a.invoice = '1' $cashwhere|;
  1613. if ( $form->{fromdate} ) {
  1614. if ($cashwhere) {
  1615. $query .= qq|
  1616. UNION
  1617. SELECT a.id, '0' AS invoice,
  1618. $transdate AS transdate,
  1619. a.invnumber, c.legal_name AS name,
  1620. a.netamount,
  1621. ac.amount * $ml AS tax,
  1622. a.notes AS 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
  1628. ON (n.entity_id = a.entity_id)
  1629. JOIN company c
  1630. ON (c.entity_id = n.entity_id)
  1631. WHERE a.datepaid >= '$form->{fromdate}'
  1632. $accno AND a.invoice = '0'
  1633. $cashwhere
  1634. UNION
  1635. SELECT a.id, '1' AS invoice,
  1636. $transdate AS transdate,
  1637. a.invnumber,
  1638. c.legal_name AS name,
  1639. i.sellprice * i.qty * $ml
  1640. AS netamount, i.sellprice
  1641. * i.qty * $ml *
  1642. (SELECT tx.rate FROM tax tx
  1643. WHERE tx.chart_id = ch.id
  1644. AND
  1645. (tx.validto > $transdate
  1646. OR tx.validto IS NULL)
  1647. ORDER BY validto LIMIT 1)
  1648. AS tax, i.description, a.till
  1649. FROM acc_trans ac
  1650. JOIN $form->{db} a
  1651. ON (a.id = ac.trans_id)
  1652. JOIN chart ch ON (ch.id = ac.chart_id)
  1653. JOIN $table n ON
  1654. (n.entity_id = a.entity_id)
  1655. JOIN company c ON
  1656. (c.entity_id = n.entity_id)
  1657. JOIN ${table}tax t
  1658. ON (t.${table}_id = n.id
  1659. AND t.chart_id = ch.id)
  1660. JOIN invoice i ON (i.trans_id = a.id)
  1661. JOIN partstax pt
  1662. ON (pt.parts_id = i.parts_id
  1663. AND pt.chart_id = ch.id)
  1664. WHERE a.datepaid >= '$form->{fromdate}'
  1665. $accno AND a.invoice = '1'
  1666. $cashwhere|;
  1667. }
  1668. }
  1669. }
  1670. if ( $form->{report} =~ /nontaxable/ ) {
  1671. if ( $form->{summary} ) {
  1672. # only gather up non-taxable transactions
  1673. $query = qq|
  1674. SELECT DISTINCT a.id, a.invoice,
  1675. $transdate AS transdate, a.invnumber,
  1676. c.legal_name AS name, a.netamount, a.till
  1677. FROM acc_trans ac
  1678. JOIN $form->{db} a ON (a.id = ac.trans_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 AND a.netamount = a.amount
  1682. $cashwhere|;
  1683. if ( $form->{fromdate} ) {
  1684. if ($cashwhere) {
  1685. $query .= qq|
  1686. UNION
  1687. SELECT DISTINCT a.id, a.invoice,
  1688. $transdate AS transdate,
  1689. a.invnumber,
  1690. c.legal_name AS name,
  1691. a.netamount, a.till
  1692. FROM acc_trans ac
  1693. JOIN $form->{db} a
  1694. ON (a.id = ac.trans_id)
  1695. JOIN $table n
  1696. ON (n.entity_id = a.entity_id)
  1697. JOIN company c
  1698. ON (c.entity_id = n.entity_id)
  1699. WHERE a.datepaid
  1700. >= '$form->{fromdate}'
  1701. AND
  1702. a.netamount = a.amount
  1703. $cashwhere|;
  1704. }
  1705. }
  1706. }
  1707. else {
  1708. # gather up details for non-taxable transactions
  1709. $query = qq|
  1710. SELECT a.id, '0' AS invoice,
  1711. $transdate AS transdate, a.invnumber,
  1712. c.legal_name AS name, a.netamount,
  1713. a.notes AS description, a.till
  1714. FROM acc_trans ac
  1715. JOIN $form->{db} a ON (a.id = ac.trans_id)
  1716. JOIN $table n ON (n.entity_id = a.entity_id)
  1717. JOIN company c ON (c.entity_id = n.entity_id)
  1718. WHERE $where AND a.invoice = '0'
  1719. AND a.netamount = a.amount $cashwhere
  1720. GROUP BY a.id, $transdate, a.invnumber, name,
  1721. a.netamount, a.notes, a.till
  1722. UNION
  1723. SELECT a.id, '1' AS invoice,
  1724. $transdate AS transdate, a.invnumber,
  1725. c.legal_name AS name,
  1726. sum(ac.sellprice * ac.qty)
  1727. * $ml AS netamount, ac.description,
  1728. a.till
  1729. FROM invoice ac
  1730. JOIN $form->{db} a ON (a.id = ac.trans_id)
  1731. JOIN $table n ON (n.entity_id = a.entity_id)
  1732. JOIN company c ON (c.entity_id = n.entity_id)
  1733. WHERE $where AND a.invoice = '1' AND
  1734. (a.entity_id NOT IN
  1735. (SELECT ${table}_id FROM ${table}tax t
  1736. (${table}_id)
  1737. ) OR ac.parts_id NOT IN
  1738. (SELECT parts_id FROM partstax p
  1739. (parts_id))) $cashwhere
  1740. GROUP BY a.id, a.invnumber, $transdate, name,
  1741. ac.description, a.till|;
  1742. if ( $form->{fromdate} ) {
  1743. if ($cashwhere) {
  1744. $query .= qq|
  1745. UNION
  1746. SELECT a.id, '0' AS invoice,
  1747. $transdate AS transdate,
  1748. a.invnumber,
  1749. c.legal_name AS name,
  1750. a.netamount,
  1751. a.notes AS description,
  1752. a.till
  1753. FROM acc_trans ac
  1754. JOIN $form->{db} a
  1755. ON (a.id = ac.trans_id)
  1756. JOIN $table n
  1757. ON (n.entity_id = a.entity_id)
  1758. JOIN company c
  1759. ON (c.entity_id = n.entity_id)
  1760. WHERE a.datepaid
  1761. >= '$form->{fromdate}'
  1762. AND a.invoice = '0'
  1763. AND a.netamount
  1764. = a.amount $cashwhere
  1765. GROUP BY a.id, $transdate,
  1766. a.invnumber, name,
  1767. a.netamount, a.notes,
  1768. a.till
  1769. UNION
  1770. SELECT a.id, '1' AS invoice,
  1771. $transdate AS transdate,
  1772. a.invnumber,
  1773. c.legal_name AS name,
  1774. sum(ac.sellprice
  1775. * ac.qty) * $ml
  1776. AS netamount,
  1777. ac.description, a.till
  1778. FROM invoice ac
  1779. JOIN $form->{db} a
  1780. ON (a.id = ac.trans_id)
  1781. JOIN $table n
  1782. ON (n.entity_id = a.entity_id)
  1783. JOIN company c
  1784. ON (c.entity_id = n.entity_id)
  1785. WHERE a.datepaid
  1786. >= '$form->{fromdate}'
  1787. AND a.invoice = '1' AND
  1788. (a.entity_id NOT IN
  1789. (SELECT ${table}_id
  1790. FROM ${table}tax t
  1791. (${table}_id)) OR
  1792. ac.parts_id NOT IN
  1793. (SELECT parts_id
  1794. FROM partstax p
  1795. (parts_id)))
  1796. $cashwhere
  1797. GROUP BY a.id, a.invnumber,
  1798. $transdate, name,
  1799. ac.description, a.till|;
  1800. }
  1801. }
  1802. }
  1803. }
  1804. $query .= qq| ORDER by $sortorder|;
  1805. $sth = $dbh->prepare($query);
  1806. $sth->execute || $form->dberror($query);
  1807. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1808. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1809. $ref->{tax} = $form->round_amount( $ref->{tax}, 2 );
  1810. if ( $form->{report} =~ /nontaxable/ ) {
  1811. push @{ $form->{TR} }, $ref if $ref->{netamount};
  1812. }
  1813. else {
  1814. push @{ $form->{TR} }, $ref if $ref->{tax};
  1815. }
  1816. }
  1817. $sth->finish;
  1818. $dbh->commit;
  1819. }
  1820. sub paymentaccounts {
  1821. my ( $self, $myconfig, $form ) = @_;
  1822. my $dbh = $form->{dbh};
  1823. my $ARAP = uc $form->{db};
  1824. # get A(R|P)_paid accounts
  1825. my $query = qq|
  1826. SELECT accno, description FROM chart
  1827. WHERE link LIKE '%${ARAP}_paid%'
  1828. ORDER BY accno|;
  1829. my $sth = $dbh->prepare($query);
  1830. $sth->execute || $form->dberror($query);
  1831. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1832. push @{ $form->{PR} }, $ref;
  1833. }
  1834. $sth->finish;
  1835. $form->all_years( $myconfig, $dbh );
  1836. $dbh->{dbh};
  1837. }
  1838. sub payments {
  1839. my ( $self, $myconfig, $form ) = @_;
  1840. my $dbh = $form->{dbh};
  1841. my $ml = 1;
  1842. if ( $form->{db} eq 'ar' ) {
  1843. $table = 'customer';
  1844. $account_class = 2;
  1845. $ml = -1;
  1846. }
  1847. if ( $form->{db} eq 'ap' ) {
  1848. $table = 'vendor';
  1849. $account_class = 1;
  1850. }
  1851. my $query;
  1852. my $sth;
  1853. my $dpt_join;
  1854. my $where;
  1855. my $var;
  1856. if ( $form->{department_id} ) {
  1857. $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id)|;
  1858. $where =
  1859. qq| AND t.department_id = | . $dbh->quote( $form->{department_id} );
  1860. }
  1861. ( $form->{fromdate}, $form->{todate} ) =
  1862. $form->from_to( $form->{year}, $form->{month}, $form->{interval} )
  1863. if $form->{year} && $form->{month};
  1864. if ( $form->{fromdate} ) {
  1865. $where .= " AND ac.transdate >= " . $dbh->quote( $form->{fromdate} );
  1866. }
  1867. if ($form->{meta_number} ) {
  1868. $where .= " AND c.meta_number = " . $dbh->quote($form->{meta_number});
  1869. }
  1870. if ( $form->{todate} ) {
  1871. $where .= " AND ac.transdate <= " . $dbh->quote( $form->{todate} );
  1872. }
  1873. if ( !$form->{fx_transaction} ) {
  1874. $where .= " AND ac.fx_transaction = '0'";
  1875. }
  1876. if ( $form->{description} ne "" ) {
  1877. $var = $dbh->quote( $form->like( lc $form->{description} ) );
  1878. $where .= " AND lower(ce.name) LIKE $var";
  1879. }
  1880. if ( $form->{source} ne "" ) {
  1881. $var = $dbh->quote( $form->like( lc $form->{source} ) );
  1882. $where .= " AND lower(ac.source) LIKE $var";
  1883. }
  1884. if ( $form->{memo} ne "" ) {
  1885. $var = $dbh->quote( $form->like( lc $form->{memo} ) );
  1886. $where .= " AND lower(ac.memo) LIKE $var";
  1887. }
  1888. my %ordinal = (
  1889. 'name' => 1,
  1890. 'transdate' => 2,
  1891. 'source' => 4,
  1892. 'employee' => 6,
  1893. 'till' => 7
  1894. );
  1895. my @a = qw(name transdate employee);
  1896. my $sortorder = $form->sort_order( \@a, \%ordinal );
  1897. my $glwhere = $where;
  1898. $glwhere =~ s/\(c.name\)/\(g.description\)/;
  1899. # cycle through each id
  1900. foreach my $accno ( split( / /, $form->{paymentaccounts} ) ) {
  1901. $query = qq|
  1902. SELECT id, accno, description
  1903. FROM chart
  1904. WHERE accno = ?|;
  1905. $sth = $dbh->prepare($query);
  1906. $sth->execute($accno) || $form->dberror($query);
  1907. my $ref = $sth->fetchrow_hashref(NAME_lc);
  1908. push @{ $form->{PR} }, $ref;
  1909. $sth->finish;
  1910. $query = qq|
  1911. SELECT ce.name, ac.transdate,
  1912. sum(ac.amount) * $ml AS paid, ac.source,
  1913. ac.memo, ee.name AS employee, a.till, a.curr,
  1914. c.meta_number
  1915. FROM acc_trans ac
  1916. JOIN $form->{db} a ON (ac.trans_id = a.id)
  1917. JOIN entity_credit_account c ON
  1918. (c.id = a.entity_credit_account)
  1919. JOIN entity ce ON (ce.id = c.entity_id)
  1920. LEFT JOIN entity_employee e ON
  1921. (a.person_id = e.entity_id)
  1922. LEFT JOIN entity ee ON (e.entity_id = ee.id)
  1923. $dpt_join
  1924. WHERE ac.chart_id = $ref->{id} $where|;
  1925. if ( $form->{till} ne "" ) {
  1926. $query .= " AND a.invoice = '1' AND NOT a.till IS NULL";
  1927. if ( $myconfig->{role} eq 'user' ) {
  1928. $query .= " AND e.login = '$form->{login}'";
  1929. }
  1930. }
  1931. $query .= qq|
  1932. GROUP BY ce.name, ac.transdate, ac.source, ac.memo,
  1933. ee.name, a.till, a.curr, c.meta_number|;
  1934. if ( $form->{till} eq "" && !$form->{meta_number}) {
  1935. $query .= qq|
  1936. UNION
  1937. SELECT g.description, ac.transdate,
  1938. sum(ac.amount) * $ml AS paid, ac.source,
  1939. ac.memo, ee.name AS employee, '' AS till,
  1940. '' AS curr, '' AS meta_number
  1941. FROM acc_trans ac
  1942. JOIN gl g ON (g.id = ac.trans_id)
  1943. LEFT
  1944. JOIN entity_employee e ON
  1945. (g.person_id = e.entity_id)
  1946. JOIN entity ee ON (e.entity_id = ee.id)
  1947. $dpt_join
  1948. WHERE ac.chart_id = $ref->{id} $glwhere
  1949. AND (ac.amount * $ml) > 0
  1950. GROUP BY g.description, ac.transdate,
  1951. ac.source, ac.memo, ee.name|;
  1952. }
  1953. $query .= qq| ORDER BY $sortorder|;
  1954. $sth = $dbh->prepare($query);
  1955. $sth->execute || $form->dberror($query);
  1956. while ( my $pr = $sth->fetchrow_hashref(NAME_lc) ) {
  1957. $form->db_parse_numeric(sth=>$sth, hashref=>$pr);
  1958. push @{ $form->{ $ref->{id} } }, $pr;
  1959. }
  1960. $sth->finish;
  1961. }
  1962. $dbh->commit;
  1963. }
  1964. 1;