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