summaryrefslogtreecommitdiff
path: root/LedgerSMB/RC.pm
blob: d7a4ba8ad2295e5c609dff7b2feeafa0d89a6c4a (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. # connect to database, turn AutoCommit off
  118. my $dbh = $form->dbconnect_noauto($myconfig);
  119. my $query;
  120. my $sth;
  121. $query = qq|SELECT category FROM chart
  122. WHERE accno = '$form->{accno}'|;
  123. ($form->{category}) = $dbh->selectrow_array($query);
  124. my $cleared;
  125. ($form->{fromdate}, $form->{todate}) = $form->from_to($form->{year}, $form->{month}, $form->{interval}) if $form->{year} && $form->{month};
  126. my $transdate = qq| AND ac.transdate < date '$form->{fromdate}'|;
  127. if (! $form->{fromdate}) {
  128. $cleared = qq| AND ac.cleared = '1'|;
  129. $transdate = "";
  130. }
  131. # get beginning balance
  132. $query = qq|SELECT sum(ac.amount)
  133. FROM acc_trans ac
  134. JOIN chart ch ON (ch.id = ac.chart_id)
  135. WHERE ch.accno = '$form->{accno}'
  136. $transdate
  137. $cleared
  138. |;
  139. ($form->{beginningbalance}) = $dbh->selectrow_array($query);
  140. # fx balance
  141. $query = qq|SELECT sum(ac.amount)
  142. FROM acc_trans ac
  143. JOIN chart ch ON (ch.id = ac.chart_id)
  144. WHERE ch.accno = '$form->{accno}'
  145. AND ac.fx_transaction = '1'
  146. $transdate
  147. $cleared
  148. |;
  149. ($form->{fx_balance}) = $dbh->selectrow_array($query);
  150. $transdate = "";
  151. if ($form->{todate}) {
  152. $transdate = qq| AND ac.transdate <= date '$form->{todate}'|;
  153. }
  154. # get statement balance
  155. $query = qq|SELECT sum(ac.amount)
  156. FROM acc_trans ac
  157. JOIN chart ch ON (ch.id = ac.chart_id)
  158. WHERE ch.accno = '$form->{accno}'
  159. $transdate
  160. |;
  161. ($form->{endingbalance}) = $dbh->selectrow_array($query);
  162. # fx balance
  163. $query = qq|SELECT sum(ac.amount)
  164. FROM acc_trans ac
  165. JOIN chart ch ON (ch.id = ac.chart_id)
  166. WHERE ch.accno = '$form->{accno}'
  167. AND ac.fx_transaction = '1'
  168. $transdate
  169. |;
  170. ($form->{fx_endingbalance}) = $dbh->selectrow_array($query);
  171. $cleared = qq| AND ac.cleared = '0'| unless $form->{fromdate};
  172. if ($form->{report}) {
  173. $cleared = qq| AND NOT (ac.cleared = '0' OR ac.cleared = '1')|;
  174. if ($form->{cleared}) {
  175. $cleared = qq| AND ac.cleared = '1'|;
  176. }
  177. if ($form->{outstanding}) {
  178. $cleared = ($form->{cleared}) ? "" : qq| AND ac.cleared = '0'|;
  179. }
  180. if (! $form->{fromdate}) {
  181. $form->{beginningbalance} = 0;
  182. $form->{fx_balance} = 0;
  183. }
  184. }
  185. my $fx_transaction;
  186. if ($form->{fx_transaction}) {
  187. $fx_transaction = qq|
  188. AND NOT
  189. (ac.chart_id IN
  190. (SELECT fxgain_accno_id FROM defaults
  191. UNION
  192. SELECT fxloss_accno_id FROM defaults))|;
  193. } else {
  194. $fx_transaction = qq|
  195. AND ac.fx_transaction = '0'|;
  196. }
  197. if ($form->{summary}) {
  198. $query = qq|SELECT ac.transdate, ac.source,
  199. sum(ac.amount) AS amount, ac.cleared
  200. FROM acc_trans ac
  201. JOIN chart ch ON (ac.chart_id = ch.id)
  202. WHERE ch.accno = '$form->{accno}'
  203. AND ac.amount >= 0
  204. $fx_transaction
  205. $cleared|;
  206. $query .= " AND ac.transdate >= '$form->{fromdate}'" if $form->{fromdate};
  207. $query .= " AND ac.transdate <= '$form->{todate}'" if $form->{todate};
  208. $query .= " GROUP BY ac.source, ac.transdate, ac.cleared";
  209. $query .= qq|
  210. UNION ALL
  211. SELECT ac.transdate, ac.source,
  212. sum(ac.amount) AS amount, ac.cleared
  213. FROM acc_trans ac
  214. JOIN chart ch ON (ac.chart_id = ch.id)
  215. WHERE ch.accno = '$form->{accno}'
  216. AND ac.amount < 0
  217. $fx_transaction
  218. $cleared|;
  219. $query .= " AND ac.transdate >= '$form->{fromdate}'" if $form->{fromdate};
  220. $query .= " AND ac.transdate <= '$form->{todate}'" if $form->{todate};
  221. $query .= " GROUP BY ac.source, ac.transdate, ac.cleared";
  222. $query .= " ORDER BY 1,2";
  223. } else {
  224. $query = qq|SELECT ac.transdate, ac.source, ac.fx_transaction,
  225. ac.amount, ac.cleared, g.id, g.description
  226. FROM acc_trans ac
  227. JOIN chart ch ON (ac.chart_id = ch.id)
  228. JOIN gl g ON (g.id = ac.trans_id)
  229. WHERE ch.accno = '$form->{accno}'
  230. $fx_transaction
  231. $cleared|;
  232. $query .= " AND ac.transdate >= '$form->{fromdate}'" if $form->{fromdate};
  233. $query .= " AND ac.transdate <= '$form->{todate}'" if $form->{todate};
  234. $query .= qq|
  235. UNION ALL
  236. SELECT ac.transdate, ac.source, ac.fx_transaction,
  237. ac.amount, ac.cleared, a.id, n.name
  238. FROM acc_trans ac
  239. JOIN chart ch ON (ac.chart_id = ch.id)
  240. JOIN ar a ON (a.id = ac.trans_id)
  241. JOIN customer n ON (n.id = a.customer_id)
  242. WHERE ch.accno = '$form->{accno}'
  243. $fx_transaction
  244. $cleared|;
  245. $query .= " AND ac.transdate >= '$form->{fromdate}'" if $form->{fromdate};
  246. $query .= " AND ac.transdate <= '$form->{todate}'" if $form->{todate};
  247. $query .= qq|
  248. UNION ALL
  249. SELECT ac.transdate, ac.source, ac.fx_transaction,
  250. ac.amount, ac.cleared, a.id, n.name
  251. FROM acc_trans ac
  252. JOIN chart ch ON (ac.chart_id = ch.id)
  253. JOIN ap a ON (a.id = ac.trans_id)
  254. JOIN vendor n ON (n.id = a.vendor_id)
  255. WHERE ch.accno = '$form->{accno}'
  256. $fx_transaction
  257. $cleared|;
  258. $query .= " AND ac.transdate >= '$form->{fromdate}'" if $form->{fromdate};
  259. $query .= " AND ac.transdate <= '$form->{todate}'" if $form->{todate};
  260. $query .= " ORDER BY 1,2,3";
  261. }
  262. $sth = $dbh->prepare($query);
  263. $sth->execute || $form->dberror($query);
  264. my $dr;
  265. my $cr;
  266. if ($form->{summary}) {
  267. $query = qq|SELECT c.name
  268. FROM customer c
  269. JOIN ar a ON (c.id = a.customer_id)
  270. JOIN acc_trans ac ON (a.id = ac.trans_id)
  271. JOIN chart ch ON (ac.chart_id = ch.id)
  272. WHERE ac.transdate = ?
  273. AND ch.accno = '$form->{accno}'
  274. AND (ac.source = ? OR ac.source IS NULL)
  275. AND ac.amount >= 0
  276. $cleared
  277. UNION
  278. SELECT v.name
  279. FROM vendor v
  280. JOIN ap a ON (v.id = a.vendor_id)
  281. JOIN acc_trans ac ON (a.id = ac.trans_id)
  282. JOIN chart ch ON (ac.chart_id = ch.id)
  283. WHERE ac.transdate = ?
  284. AND ch.accno = '$form->{accno}'
  285. AND (ac.source = ? OR ac.source IS NULL)
  286. AND ac.amount > 0
  287. $cleared
  288. UNION
  289. SELECT g.description
  290. FROM gl g
  291. JOIN acc_trans ac ON (g.id = ac.trans_id)
  292. JOIN chart ch ON (ac.chart_id = ch.id)
  293. WHERE ac.transdate = ?
  294. AND ch.accno = '$form->{accno}'
  295. AND (ac.source = ? OR ac.source IS NULL)
  296. AND ac.amount >= 0
  297. $cleared
  298. |;
  299. $query .= " ORDER BY 1";
  300. $dr = $dbh->prepare($query);
  301. $query = qq|SELECT c.name
  302. FROM customer c
  303. JOIN ar a ON (c.id = a.customer_id)
  304. JOIN acc_trans ac ON (a.id = ac.trans_id)
  305. JOIN chart ch ON (ac.chart_id = ch.id)
  306. WHERE ac.transdate = ?
  307. AND ch.accno = '$form->{accno}'
  308. AND (ac.source = ? OR ac.source IS NULL)
  309. AND ac.amount < 0
  310. $cleared
  311. UNION
  312. SELECT v.name
  313. FROM vendor v
  314. JOIN ap a ON (v.id = a.vendor_id)
  315. JOIN acc_trans ac ON (a.id = ac.trans_id)
  316. JOIN chart ch ON (ac.chart_id = ch.id)
  317. WHERE ac.transdate = ?
  318. AND ch.accno = '$form->{accno}'
  319. AND (ac.source = ? OR ac.source IS NULL)
  320. AND ac.amount < 0
  321. $cleared
  322. UNION
  323. SELECT g.description
  324. FROM gl g
  325. JOIN acc_trans ac ON (g.id = ac.trans_id)
  326. JOIN chart ch ON (ac.chart_id = ch.id)
  327. WHERE ac.transdate = ?
  328. AND ch.accno = '$form->{accno}'
  329. AND (ac.source = ? OR ac.source IS NULL)
  330. AND ac.amount < 0
  331. $cleared
  332. |;
  333. $query .= " ORDER BY 1";
  334. $cr = $dbh->prepare($query);
  335. }
  336. my $name;
  337. my $ref;
  338. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  339. if ($form->{summary}) {
  340. if ($ref->{amount} > 0) {
  341. $dr->execute($ref->{transdate}, $ref->{source}, $ref->{transdate}, $ref->{source}, $ref->{transdate}, $ref->{source});
  342. $ref->{oldcleared} = $ref->{cleared};
  343. $ref->{name} = ();
  344. while (($name) = $dr->fetchrow_array) {
  345. push @{ $ref->{name} }, $name;
  346. }
  347. $dr->finish;
  348. } else {
  349. $cr->execute($ref->{transdate}, $ref->{source}, $ref->{transdate}, $ref->{source}, $ref->{transdate}, $ref->{source});
  350. $ref->{oldcleared} = $ref->{cleared};
  351. $ref->{name} = ();
  352. while (($name) = $cr->fetchrow_array) {
  353. push @{ $ref->{name} }, $name;
  354. }
  355. $cr->finish;
  356. }
  357. } else {
  358. push @{ $ref->{name} }, $ref->{description};
  359. }
  360. push @{ $form->{PR} }, $ref;
  361. }
  362. $sth->finish;
  363. $dbh->disconnect;
  364. }
  365. sub reconcile {
  366. my ($self, $myconfig, $form) = @_;
  367. my $dbh = $form->dbconnect($myconfig);
  368. my $query = qq|SELECT id FROM chart
  369. WHERE accno = '$form->{accno}'|;
  370. my ($chart_id) = $dbh->selectrow_array($query);
  371. $chart_id *= 1;
  372. $query = qq|
  373. SELECT trans_id FROM acc_trans
  374. WHERE (source = ? OR source IS NULL) AND transdate = ?
  375. AND cleared = '0'
  376. AND chart_id = |.$dbh->quote($chart_id);
  377. my $sth = $dbh->prepare($query) || $form->dberror($query);
  378. my $i;
  379. my $trans_id;
  380. $query = qq|
  381. UPDATE acc_trans
  382. SET cleared = '1'
  383. WHERE cleared = '0' AND trans_id = ? AND transdate = ?
  384. AND chart_id = |.$dbh->quote($chart_id);
  385. my $tth = $dbh->prepare($query) || $form->dberror($query);
  386. # clear flags
  387. for $i (1 .. $form->{rowcount}) {
  388. if ($form->{"cleared_$i"} && ! $form->{"oldcleared_$i"}) {
  389. if ($form->{summary}) {
  390. $sth->execute(
  391. $form->{"source_$i"},
  392. $form->{"transdate_$i"}
  393. ) || $form->dberror;
  394. while (($trans_id) = $sth->fetchrow_array) {
  395. $tth->execute(
  396. $trans_id,
  397. $form->{"transdate_$i"}
  398. ) || $form->dberror;
  399. $tth->finish;
  400. }
  401. $sth->finish;
  402. } else {
  403. $tth->execute(
  404. $form->{"id_$i"},
  405. $form->{"transdate_$i"}
  406. ) || $form->dberror;
  407. $tth->finish;
  408. }
  409. }
  410. }
  411. $dbh->commit;
  412. }
  413. 1;