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