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