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