summaryrefslogtreecommitdiff
path: root/LedgerSMB/RC.pm
blob: 5828f441420132bbdee1c7960659b379db3f3455 (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. # http://www.ledgersmb.org/
  5. #
  6. # Copyright (C) 2006
  7. # This work contains copyrighted information from a number of sources all used
  8. # with permission.
  9. #
  10. # This file contains source code included with or based on SQL-Ledger which
  11. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  12. # under the GNU General Public License version 2 or, at your option, any later
  13. # version. For a full list including contact information of contributors,
  14. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  15. #
  16. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  17. # Copyright (C) 2002
  18. #
  19. # Author: DWS Systems Inc.
  20. # Web: http://www.sql-ledger.org
  21. #
  22. # Contributors:
  23. #
  24. #======================================================================
  25. #
  26. # This file has undergone whitespace cleanup.
  27. #
  28. #======================================================================
  29. #
  30. # Account reconciliation routines
  31. #
  32. #======================================================================
  33. package RC;
  34. sub getposlines{
  35. my ($self, $myconfig, $form) = @_;
  36. %pos_config = %{$form->{pos_config}};
  37. %pos_sources = %{$form->{pos_sources}};
  38. my $sources = '';
  39. foreach $key (keys %pos_sources){
  40. $sources .= ", '$key'";
  41. }
  42. $sources =~ s/^,\s*//;
  43. my $dbh = $form->{dbh};
  44. my $query = qq|
  45. SELECT sum(amount) AS amount, source FROM acc_trans
  46. WHERE chart_id = (SELECT id FROM chart
  47. WHERE accno = ?)
  48. AND transdate = date 'NOW' AND cleared IS NOT TRUE
  49. GROUP BY source|;
  50. my $sth = $dbh->prepare($query);
  51. $sth->execute($pos_config{till_accno}) || $form->dberror($query);
  52. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  53. push @{$form->{TB}}, $ref;
  54. }
  55. $sth->finish;
  56. my $query = qq|
  57. SELECT sum(amount) AS sum FROM acc_trans
  58. WHERE chart_id = (SELECT id FROM chart WHERE accno = ?)
  59. AND transdate = date 'NOW'
  60. AND cleared IS NOT TRUE|;
  61. my $sth = $dbh->prepare($query);
  62. $sth->execute($pos_config{till_accno}) || $form->dberror($query);
  63. my $ref = $sth->fetchrow_hashref(NAME_lc);
  64. $form->{sum} = $ref->{sum};
  65. $sth->finish;
  66. }
  67. sub clear_till {
  68. my ($self, $myconfig, $form) = @_;
  69. %pos_config = %{$form->{pos_config}};
  70. %pos_sources = %{$form->{pos_sources}};
  71. my $sources = '';
  72. foreach $key (keys %pos_sources){
  73. $sources .= ", '$key'";
  74. }
  75. $sources =~ s/^,\s//;
  76. my $dbh = $form->{dbh};
  77. my $query = qq|
  78. UPDATE acc_trans
  79. SET cleared = TRUE
  80. WHERE chart_id =
  81. (SELECT id FROM chart WHERE accno = ?)
  82. AND transdate = date 'NOW'|;
  83. my $sth = $dbh->prepare($query);
  84. $sth->execute($pos_config{till_accno}) || $form->dberror($query);
  85. }
  86. sub getbalance{
  87. my ($self, $myconfig, $form) = @_;
  88. my $dbh = $form->{dbh};
  89. my $query = qq|
  90. SELECT sum(amount) AS balance
  91. FROM acc_trans
  92. WHERE chart_id = (SELECT id FROM chart WHERE accno = ?)|;
  93. my $sth = $dbh->prepare($query);
  94. $sth->execute($form->{accno}) || $form->dberror($query);
  95. my $ref = $sth->fetchrow_hashref(NAME_lc);
  96. $form->{balance} = $ref->{balance};
  97. }
  98. sub paymentaccounts {
  99. my ($self, $myconfig, $form) = @_;
  100. my $dbh = $form->{dbh};
  101. my $query = qq|
  102. SELECT accno, description
  103. FROM chart
  104. WHERE link LIKE '%_paid%'
  105. AND (category = 'A' OR category = 'L')
  106. ORDER BY accno|;
  107. my $sth = $dbh->prepare($query);
  108. $sth->execute || $form->dberror($query);
  109. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  110. push @{ $form->{PR} }, $ref;
  111. }
  112. $sth->finish;
  113. $form->all_years($myconfig, $dbh);
  114. }
  115. sub payment_transactions {
  116. my ($self, $myconfig, $form) = @_;
  117. my $dbh = $form->{dbh};
  118. my $query;
  119. my $sth;
  120. $query = qq|SELECT category FROM chart WHERE accno = ?|;
  121. $sth = $dbh->prepare($query);
  122. $sth->execute($form->{accno});
  123. ($form->{category}) = $sth->fetchrow_array();
  124. my $cleared;
  125. ($form->{fromdate}, $form->{todate})
  126. = $form->from_to(
  127. $form->{year}, $form->{month}, $form->{interval})
  128. if $form->{year} && $form->{month};
  129. my $transdate = qq| AND ac.transdate < date |.
  130. $dbh->quote($form->{fromdate});
  131. if (! $form->{fromdate}) {
  132. $cleared = qq| AND ac.cleared = '1'|;
  133. $transdate = "";
  134. }
  135. # get beginning balance
  136. $query = qq|
  137. SELECT sum(ac.amount)
  138. FROM acc_trans ac
  139. JOIN chart ch ON (ch.id = ac.chart_id)
  140. WHERE ch.accno = ? $transdate $cleared |;
  141. $sth = $dbh->prepare($query);
  142. $sth->execute($form->{accno});
  143. ($form->{beginningbalance}) = $sth->fetchrow_array();
  144. $query = qq|
  145. SELECT sum(ac.amount)
  146. FROM acc_trans ac
  147. JOIN chart ch ON (ch.id = ac.chart_id)
  148. WHERE ch.accno = ? AND ac.fx_transaction = '1'
  149. $transdate $cleared|;
  150. $sth = $dbh->prepare($query);
  151. $sth->execute($form->{accno});
  152. ($form->{fx_balance}) = $sth->fetchrow_array();
  153. $transdate = "";
  154. if ($form->{todate}) {
  155. $transdate = qq| AND ac.transdate <= date |.
  156. $dbh->quote($form->{todate});
  157. }
  158. # get statement balance
  159. $query = qq|
  160. SELECT sum(ac.amount)
  161. FROM acc_trans ac
  162. JOIN chart ch ON (ch.id = ac.chart_id)
  163. WHERE ch.accno = ? $transdate|;
  164. $sth = $dbh->prepare($query);
  165. $sth->execute($form->{accno});
  166. ($form->{endingbalance}) = $sth->fetchrow_array();
  167. # fx balance
  168. $query = qq|
  169. SELECT sum(ac.amount)
  170. FROM acc_trans ac
  171. JOIN chart ch ON (ch.id = ac.chart_id)
  172. WHERE ch.accno = ? AND ac.fx_transaction = '1' $transdate |;
  173. $sth = $dbh->prepare($query);
  174. $sth->execute($form->{accno});
  175. ($form->{fx_endingbalance}) = $sth->fetchrow_array();
  176. $cleared = qq| AND ac.cleared = '0'| unless $form->{fromdate};
  177. if ($form->{report}) {
  178. $cleared = qq| AND NOT (ac.cleared = '0' OR ac.cleared = '1')|;
  179. if ($form->{cleared}) {
  180. $cleared = qq| AND ac.cleared = '1'|;
  181. }
  182. if ($form->{outstanding}) {
  183. $cleared =
  184. ($form->{cleared})
  185. ? ""
  186. : qq| AND ac.cleared = '0'|;
  187. }
  188. if (! $form->{fromdate}) {
  189. $form->{beginningbalance} = 0;
  190. $form->{fx_balance} = 0;
  191. }
  192. }
  193. my $fx_transaction;
  194. if ($form->{fx_transaction}) {
  195. $fx_transaction = qq|
  196. AND NOT (ac.chart_id IN
  197. (SELECT fxgain_accno_id FROM defaults
  198. UNION
  199. SELECT fxloss_accno_id FROM defaults))|;
  200. } else {
  201. $fx_transaction = qq|
  202. AND ac.fx_transaction = '0'|;
  203. }
  204. if ($form->{summary}) {
  205. $query = qq|
  206. SELECT ac.transdate, ac.source,
  207. sum(ac.amount) AS amount, ac.cleared
  208. FROM acc_trans ac
  209. JOIN chart ch ON (ac.chart_id = ch.id)
  210. WHERE ch.accno = |.$dbh->quote($form->{accno}).qq|
  211. AND ac.amount >= 0 $fx_transaction $cleared|;
  212. $query .= " AND ac.transdate >= ".$dbh->quote($form->{fromdate})
  213. if $form->{fromdate};
  214. $query .= " AND ac.transdate <= ".$dbh->quote($form->{todate})
  215. if $form->{todate};
  216. $query .= " GROUP BY ac.source, ac.transdate, ac.cleared";
  217. $query .= qq|
  218. UNION ALL
  219. SELECT ac.transdate, ac.source,
  220. sum(ac.amount) AS amount, ac.cleared
  221. FROM acc_trans ac
  222. JOIN chart ch ON (ac.chart_id = ch.id)
  223. WHERE ch.accno = |.$dbh->quote($form->{accno}).qq|
  224. AND ac.amount < 0 $fx_transaction $cleared|;
  225. $query .= " AND ac.transdate >= ".$dbh->quote($form->{fromdate})
  226. if $form->{fromdate};
  227. $query .= " AND ac.transdate <= ".$dbh->quote($form->{todate})
  228. if $form->{todate};
  229. $query .= " GROUP BY ac.source, ac.transdate, ac.cleared";
  230. $query .= " ORDER BY 1,2";
  231. } else {
  232. $query = qq|
  233. SELECT ac.transdate, ac.source, ac.fx_transaction,
  234. ac.amount, ac.cleared, g.id, g.description
  235. FROM acc_trans ac
  236. JOIN chart ch ON (ac.chart_id = ch.id)
  237. JOIN gl g ON (g.id = ac.trans_id)
  238. WHERE ch.accno = |.$dbh->quote($form->{accno}).qq|
  239. $fx_transaction $cleared|;
  240. $query .= " AND ac.transdate >= ".$dbh->quote($form->{fromdate})
  241. if $form->{fromdate};
  242. $query .= " AND ac.transdate <= ".$dbh->quote($form->{todate})
  243. if $form->{todate};
  244. $query .= qq|
  245. UNION ALL
  246. SELECT ac.transdate, ac.source, ac.fx_transaction,
  247. ac.amount, ac.cleared, a.id, n.name
  248. FROM acc_trans ac
  249. JOIN chart ch ON (ac.chart_id = ch.id)
  250. JOIN ar a ON (a.id = ac.trans_id)
  251. JOIN customer n ON (n.id = a.customer_id)
  252. WHERE ch.accno = |.$dbh->quote($form->{accno}).qq|
  253. $fx_transaction $cleared|;
  254. $query .= " AND ac.transdate >= ".$dbh->quote($form->{fromdate})
  255. if $form->{fromdate};
  256. $query .= " AND ac.transdate <= ".$dbh->quote($form->{todate})
  257. if $form->{todate};
  258. $query .= qq|
  259. UNION ALL
  260. SELECT ac.transdate, ac.source, ac.fx_transaction,
  261. ac.amount, ac.cleared, a.id, n.name
  262. FROM acc_trans ac
  263. JOIN chart ch ON (ac.chart_id = ch.id)
  264. JOIN ap a ON (a.id = ac.trans_id)
  265. JOIN vendor n ON (n.id = a.vendor_id)
  266. WHERE ch.accno = |.$dbh->quote($form->{accno}).qq|
  267. $fx_transaction $cleared|;
  268. $query .= " AND ac.transdate >= ".$dbh->quote($form->{fromdate})
  269. if $form->{fromdate};
  270. $query .= " AND ac.transdate <= ".$dbh->quote($form->{todate})
  271. if $form->{todate};
  272. $query .= " ORDER BY 1,2,3";
  273. }
  274. $sth = $dbh->prepare($query);
  275. $sth->execute || $form->dberror($query);
  276. my $dr;
  277. my $cr;
  278. if ($form->{summary}) {
  279. $query = qq|
  280. SELECT c.name
  281. FROM customer c
  282. JOIN ar a ON (c.id = a.customer_id)
  283. JOIN acc_trans ac ON (a.id = ac.trans_id)
  284. JOIN chart ch ON (ac.chart_id = ch.id)
  285. WHERE ac.transdate = ?
  286. AND ch.accno = |.$dbh->quote($form->{accno}).qq|
  287. AND (ac.source = ? OR ac.source IS NULL)
  288. AND ac.amount >= 0 $cleared
  289. UNION
  290. SELECT v.name
  291. FROM vendor v
  292. JOIN ap a ON (v.id = a.vendor_id)
  293. JOIN acc_trans ac ON (a.id = ac.trans_id)
  294. JOIN chart ch ON (ac.chart_id = ch.id)
  295. WHERE ac.transdate = ?
  296. AND ch.accno = |.$dbh->quote($form->{accno}).qq|
  297. AND (ac.source = ? OR ac.source IS NULL)
  298. AND ac.amount > 0 $cleared
  299. UNION
  300. SELECT g.description
  301. FROM gl g
  302. JOIN acc_trans ac ON (g.id = ac.trans_id)
  303. JOIN chart ch ON (ac.chart_id = ch.id)
  304. WHERE ac.transdate = ?
  305. AND ch.accno = |.$dbh->quote($form->{accno}).qq|
  306. AND (ac.source = ? OR ac.source IS NULL)
  307. AND ac.amount >= 0 $cleared|;
  308. $query .= " ORDER BY 1";
  309. $dr = $dbh->prepare($query);
  310. $query = qq|
  311. SELECT c.name
  312. FROM customer c
  313. JOIN ar a ON (c.id = a.customer_id)
  314. JOIN acc_trans ac ON (a.id = ac.trans_id)
  315. JOIN chart ch ON (ac.chart_id = ch.id)
  316. WHERE ac.transdate = ?
  317. AND ch.accno = |.$dbh->quote($form->{accno}).qq|
  318. AND (ac.source = ? OR ac.source IS NULL)
  319. AND ac.amount < 0 $cleared
  320. UNION
  321. SELECT v.name
  322. FROM vendor v
  323. JOIN ap a ON (v.id = a.vendor_id)
  324. JOIN acc_trans ac ON (a.id = ac.trans_id)
  325. JOIN chart ch ON (ac.chart_id = ch.id)
  326. WHERE ac.transdate = ?
  327. AND ch.accno = |.$dbh->quote($form->{accno}).qq|
  328. AND (ac.source = ? OR ac.source IS NULL)
  329. AND ac.amount < 0 $cleared
  330. UNION
  331. SELECT g.description
  332. FROM gl g
  333. JOIN acc_trans ac ON (g.id = ac.trans_id)
  334. JOIN chart ch ON (ac.chart_id = ch.id)
  335. WHERE ac.transdate = ?
  336. AND ch.accno = |.$dbh->quote($form->{accno}).qq|
  337. AND (ac.source = ? OR ac.source IS NULL)
  338. AND ac.amount < 0 $cleared|;
  339. $query .= " ORDER BY 1";
  340. $cr = $dbh->prepare($query);
  341. }
  342. my $name;
  343. my $ref;
  344. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  345. if ($form->{summary}) {
  346. if ($ref->{amount} > 0) {
  347. $dr->execute(
  348. $ref->{transdate}, $ref->{source},
  349. $ref->{transdate}, $ref->{source},
  350. $ref->{transdate}, $ref->{source});
  351. $ref->{oldcleared} = $ref->{cleared};
  352. $ref->{name} = ();
  353. while (($name) = $dr->fetchrow_array) {
  354. push @{ $ref->{name} }, $name;
  355. }
  356. $dr->finish;
  357. } else {
  358. $cr->execute(
  359. $ref->{transdate}, $ref->{source},
  360. $ref->{transdate}, $ref->{source},
  361. $ref->{transdate}, $ref->{source});
  362. $ref->{oldcleared} = $ref->{cleared};
  363. $ref->{name} = ();
  364. while (($name) = $cr->fetchrow_array) {
  365. push @{ $ref->{name} }, $name;
  366. }
  367. $cr->finish;
  368. }
  369. } else {
  370. push @{ $ref->{name} }, $ref->{description};
  371. }
  372. push @{ $form->{PR} }, $ref;
  373. }
  374. $sth->finish;
  375. $dbh->commit;
  376. }
  377. sub reconcile {
  378. my ($self, $myconfig, $form) = @_;
  379. my $dbh = $form->dbconnect($myconfig);
  380. my $query = qq|SELECT id FROM chart
  381. WHERE accno = '$form->{accno}'|;
  382. my ($chart_id) = $dbh->selectrow_array($query);
  383. $chart_id *= 1;
  384. $query = qq|
  385. SELECT trans_id FROM acc_trans
  386. WHERE (source = ? OR source IS NULL) AND transdate = ?
  387. AND cleared = '0'
  388. AND chart_id = |.$dbh->quote($chart_id);
  389. my $sth = $dbh->prepare($query) || $form->dberror($query);
  390. my $i;
  391. my $trans_id;
  392. $query = qq|
  393. UPDATE acc_trans
  394. SET cleared = '1'
  395. WHERE cleared = '0' AND trans_id = ? AND transdate = ?
  396. AND chart_id = |.$dbh->quote($chart_id);
  397. my $tth = $dbh->prepare($query) || $form->dberror($query);
  398. # clear flags
  399. for $i (1 .. $form->{rowcount}) {
  400. if ($form->{"cleared_$i"} && ! $form->{"oldcleared_$i"}) {
  401. if ($form->{summary}) {
  402. $sth->execute(
  403. $form->{"source_$i"},
  404. $form->{"transdate_$i"}
  405. ) || $form->dberror;
  406. while (($trans_id) = $sth->fetchrow_array) {
  407. $tth->execute(
  408. $trans_id,
  409. $form->{"transdate_$i"}
  410. ) || $form->dberror;
  411. $tth->finish;
  412. }
  413. $sth->finish;
  414. } else {
  415. $tth->execute(
  416. $form->{"id_$i"},
  417. $form->{"transdate_$i"}
  418. ) || $form->dberror;
  419. $tth->finish;
  420. }
  421. }
  422. }
  423. $dbh->commit;
  424. }
  425. 1;