summaryrefslogtreecommitdiff
path: root/LedgerSMB/RC.pm
blob: 8b518cba254dd7a2f339cb5c5243e811e6d77eed (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. # Account reconciliation routines
  13. #
  14. #======================================================================
  15. package RC;
  16. sub paymentaccounts {
  17. my ($self, $myconfig, $form) = @_;
  18. # connect to database
  19. my $dbh = $form->dbconnect($myconfig);
  20. my $query = qq|SELECT accno, description
  21. FROM chart
  22. WHERE link LIKE '%_paid%'
  23. AND (category = 'A' OR category = 'L')
  24. ORDER BY accno|;
  25. my $sth = $dbh->prepare($query);
  26. $sth->execute || $form->dberror($query);
  27. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  28. push @{ $form->{PR} }, $ref;
  29. }
  30. $sth->finish;
  31. $form->all_years($myconfig, $dbh);
  32. $dbh->disconnect;
  33. }
  34. sub payment_transactions {
  35. my ($self, $myconfig, $form) = @_;
  36. # connect to database, turn AutoCommit off
  37. my $dbh = $form->dbconnect_noauto($myconfig);
  38. my $query;
  39. my $sth;
  40. $query = qq|SELECT category FROM chart
  41. WHERE accno = '$form->{accno}'|;
  42. ($form->{category}) = $dbh->selectrow_array($query);
  43. my $cleared;
  44. ($form->{fromdate}, $form->{todate}) = $form->from_to($form->{year}, $form->{month}, $form->{interval}) if $form->{year} && $form->{month};
  45. my $transdate = qq| AND ac.transdate < date '$form->{fromdate}'|;
  46. if (! $form->{fromdate}) {
  47. $cleared = qq| AND ac.cleared = '1'|;
  48. $transdate = "";
  49. }
  50. # get beginning balance
  51. $query = qq|SELECT sum(ac.amount)
  52. FROM acc_trans ac
  53. JOIN chart ch ON (ch.id = ac.chart_id)
  54. WHERE ch.accno = '$form->{accno}'
  55. $transdate
  56. $cleared
  57. |;
  58. ($form->{beginningbalance}) = $dbh->selectrow_array($query);
  59. # fx balance
  60. $query = qq|SELECT sum(ac.amount)
  61. FROM acc_trans ac
  62. JOIN chart ch ON (ch.id = ac.chart_id)
  63. WHERE ch.accno = '$form->{accno}'
  64. AND ac.fx_transaction = '1'
  65. $transdate
  66. $cleared
  67. |;
  68. ($form->{fx_balance}) = $dbh->selectrow_array($query);
  69. $transdate = "";
  70. if ($form->{todate}) {
  71. $transdate = qq| AND ac.transdate <= date '$form->{todate}'|;
  72. }
  73. # get statement balance
  74. $query = qq|SELECT sum(ac.amount)
  75. FROM acc_trans ac
  76. JOIN chart ch ON (ch.id = ac.chart_id)
  77. WHERE ch.accno = '$form->{accno}'
  78. $transdate
  79. |;
  80. ($form->{endingbalance}) = $dbh->selectrow_array($query);
  81. # fx balance
  82. $query = qq|SELECT sum(ac.amount)
  83. FROM acc_trans ac
  84. JOIN chart ch ON (ch.id = ac.chart_id)
  85. WHERE ch.accno = '$form->{accno}'
  86. AND ac.fx_transaction = '1'
  87. $transdate
  88. |;
  89. ($form->{fx_endingbalance}) = $dbh->selectrow_array($query);
  90. $cleared = qq| AND ac.cleared = '0'| unless $form->{fromdate};
  91. if ($form->{report}) {
  92. $cleared = qq| AND NOT (ac.cleared = '0' OR ac.cleared = '1')|;
  93. if ($form->{cleared}) {
  94. $cleared = qq| AND ac.cleared = '1'|;
  95. }
  96. if ($form->{outstanding}) {
  97. $cleared = ($form->{cleared}) ? "" : qq| AND ac.cleared = '0'|;
  98. }
  99. if (! $form->{fromdate}) {
  100. $form->{beginningbalance} = 0;
  101. $form->{fx_balance} = 0;
  102. }
  103. }
  104. my $fx_transaction;
  105. if ($form->{fx_transaction}) {
  106. $fx_transaction = qq|
  107. AND NOT
  108. (ac.chart_id IN
  109. (SELECT fxgain_accno_id FROM defaults
  110. UNION
  111. SELECT fxloss_accno_id FROM defaults))|;
  112. } else {
  113. $fx_transaction = qq|
  114. AND ac.fx_transaction = '0'|;
  115. }
  116. if ($form->{summary}) {
  117. $query = qq|SELECT ac.transdate, ac.source,
  118. sum(ac.amount) AS amount, ac.cleared
  119. FROM acc_trans ac
  120. JOIN chart ch ON (ac.chart_id = ch.id)
  121. WHERE ch.accno = '$form->{accno}'
  122. AND ac.amount >= 0
  123. $fx_transaction
  124. $cleared|;
  125. $query .= " AND ac.transdate >= '$form->{fromdate}'" if $form->{fromdate};
  126. $query .= " AND ac.transdate <= '$form->{todate}'" if $form->{todate};
  127. $query .= " GROUP BY ac.source, ac.transdate, ac.cleared";
  128. $query .= qq|
  129. UNION ALL
  130. SELECT ac.transdate, ac.source,
  131. sum(ac.amount) AS amount, ac.cleared
  132. FROM acc_trans ac
  133. JOIN chart ch ON (ac.chart_id = ch.id)
  134. WHERE ch.accno = '$form->{accno}'
  135. AND ac.amount < 0
  136. $fx_transaction
  137. $cleared|;
  138. $query .= " AND ac.transdate >= '$form->{fromdate}'" if $form->{fromdate};
  139. $query .= " AND ac.transdate <= '$form->{todate}'" if $form->{todate};
  140. $query .= " GROUP BY ac.source, ac.transdate, ac.cleared";
  141. $query .= " ORDER BY 1,2";
  142. } else {
  143. $query = qq|SELECT ac.transdate, ac.source, ac.fx_transaction,
  144. ac.amount, ac.cleared, g.id, g.description
  145. FROM acc_trans ac
  146. JOIN chart ch ON (ac.chart_id = ch.id)
  147. JOIN gl g ON (g.id = ac.trans_id)
  148. WHERE ch.accno = '$form->{accno}'
  149. $fx_transaction
  150. $cleared|;
  151. $query .= " AND ac.transdate >= '$form->{fromdate}'" if $form->{fromdate};
  152. $query .= " AND ac.transdate <= '$form->{todate}'" if $form->{todate};
  153. $query .= qq|
  154. UNION ALL
  155. SELECT ac.transdate, ac.source, ac.fx_transaction,
  156. ac.amount, ac.cleared, a.id, n.name
  157. FROM acc_trans ac
  158. JOIN chart ch ON (ac.chart_id = ch.id)
  159. JOIN ar a ON (a.id = ac.trans_id)
  160. JOIN customer n ON (n.id = a.customer_id)
  161. WHERE ch.accno = '$form->{accno}'
  162. $fx_transaction
  163. $cleared|;
  164. $query .= " AND ac.transdate >= '$form->{fromdate}'" if $form->{fromdate};
  165. $query .= " AND ac.transdate <= '$form->{todate}'" if $form->{todate};
  166. $query .= qq|
  167. UNION ALL
  168. SELECT ac.transdate, ac.source, ac.fx_transaction,
  169. ac.amount, ac.cleared, a.id, n.name
  170. FROM acc_trans ac
  171. JOIN chart ch ON (ac.chart_id = ch.id)
  172. JOIN ap a ON (a.id = ac.trans_id)
  173. JOIN vendor n ON (n.id = a.vendor_id)
  174. WHERE ch.accno = '$form->{accno}'
  175. $fx_transaction
  176. $cleared|;
  177. $query .= " AND ac.transdate >= '$form->{fromdate}'" if $form->{fromdate};
  178. $query .= " AND ac.transdate <= '$form->{todate}'" if $form->{todate};
  179. $query .= " ORDER BY 1,2,3";
  180. }
  181. $sth = $dbh->prepare($query);
  182. $sth->execute || $form->dberror($query);
  183. my $dr;
  184. my $cr;
  185. if ($form->{summary}) {
  186. $query = qq|SELECT c.name
  187. FROM customer c
  188. JOIN ar a ON (c.id = a.customer_id)
  189. JOIN acc_trans ac ON (a.id = ac.trans_id)
  190. JOIN chart ch ON (ac.chart_id = ch.id)
  191. WHERE ac.transdate = ?
  192. AND ch.accno = '$form->{accno}'
  193. AND (ac.source = ? OR ac.source IS NULL)
  194. AND ac.amount >= 0
  195. $cleared
  196. UNION
  197. SELECT v.name
  198. FROM vendor v
  199. JOIN ap a ON (v.id = a.vendor_id)
  200. JOIN acc_trans ac ON (a.id = ac.trans_id)
  201. JOIN chart ch ON (ac.chart_id = ch.id)
  202. WHERE ac.transdate = ?
  203. AND ch.accno = '$form->{accno}'
  204. AND (ac.source = ? OR ac.source IS NULL)
  205. AND ac.amount > 0
  206. $cleared
  207. UNION
  208. SELECT g.description
  209. FROM gl g
  210. JOIN acc_trans ac ON (g.id = ac.trans_id)
  211. JOIN chart ch ON (ac.chart_id = ch.id)
  212. WHERE ac.transdate = ?
  213. AND ch.accno = '$form->{accno}'
  214. AND (ac.source = ? OR ac.source IS NULL)
  215. AND ac.amount >= 0
  216. $cleared
  217. |;
  218. $query .= " ORDER BY 1";
  219. $dr = $dbh->prepare($query);
  220. $query = qq|SELECT c.name
  221. FROM customer c
  222. JOIN ar a ON (c.id = a.customer_id)
  223. JOIN acc_trans ac ON (a.id = ac.trans_id)
  224. JOIN chart ch ON (ac.chart_id = ch.id)
  225. WHERE ac.transdate = ?
  226. AND ch.accno = '$form->{accno}'
  227. AND (ac.source = ? OR ac.source IS NULL)
  228. AND ac.amount < 0
  229. $cleared
  230. UNION
  231. SELECT v.name
  232. FROM vendor v
  233. JOIN ap a ON (v.id = a.vendor_id)
  234. JOIN acc_trans ac ON (a.id = ac.trans_id)
  235. JOIN chart ch ON (ac.chart_id = ch.id)
  236. WHERE ac.transdate = ?
  237. AND ch.accno = '$form->{accno}'
  238. AND (ac.source = ? OR ac.source IS NULL)
  239. AND ac.amount < 0
  240. $cleared
  241. UNION
  242. SELECT g.description
  243. FROM gl g
  244. JOIN acc_trans ac ON (g.id = ac.trans_id)
  245. JOIN chart ch ON (ac.chart_id = ch.id)
  246. WHERE ac.transdate = ?
  247. AND ch.accno = '$form->{accno}'
  248. AND (ac.source = ? OR ac.source IS NULL)
  249. AND ac.amount < 0
  250. $cleared
  251. |;
  252. $query .= " ORDER BY 1";
  253. $cr = $dbh->prepare($query);
  254. }
  255. my $name;
  256. my $ref;
  257. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  258. if ($form->{summary}) {
  259. if ($ref->{amount} > 0) {
  260. $dr->execute($ref->{transdate}, $ref->{source}, $ref->{transdate}, $ref->{source}, $ref->{transdate}, $ref->{source});
  261. $ref->{oldcleared} = $ref->{cleared};
  262. $ref->{name} = ();
  263. while (($name) = $dr->fetchrow_array) {
  264. push @{ $ref->{name} }, $name;
  265. }
  266. $dr->finish;
  267. } else {
  268. $cr->execute($ref->{transdate}, $ref->{source}, $ref->{transdate}, $ref->{source}, $ref->{transdate}, $ref->{source});
  269. $ref->{oldcleared} = $ref->{cleared};
  270. $ref->{name} = ();
  271. while (($name) = $cr->fetchrow_array) {
  272. push @{ $ref->{name} }, $name;
  273. }
  274. $cr->finish;
  275. }
  276. } else {
  277. push @{ $ref->{name} }, $ref->{description};
  278. }
  279. push @{ $form->{PR} }, $ref;
  280. }
  281. $sth->finish;
  282. $dbh->disconnect;
  283. }
  284. sub reconcile {
  285. my ($self, $myconfig, $form) = @_;
  286. # connect to database
  287. my $dbh = $form->dbconnect($myconfig);
  288. my $query = qq|SELECT id FROM chart
  289. WHERE accno = '$form->{accno}'|;
  290. my ($chart_id) = $dbh->selectrow_array($query);
  291. $chart_id *= 1;
  292. $query = qq|SELECT trans_id FROM acc_trans
  293. WHERE (source = ? OR source IS NULL)
  294. AND transdate = ?
  295. AND cleared = '0'
  296. AND chart_id = $chart_id|;
  297. my $sth = $dbh->prepare($query) || $form->dberror($query);
  298. my $i;
  299. my $trans_id;
  300. $query = qq|UPDATE acc_trans SET cleared = '1'
  301. WHERE cleared = '0'
  302. AND trans_id = ?
  303. AND transdate = ?
  304. AND chart_id = $chart_id|;
  305. my $tth = $dbh->prepare($query) || $form->dberror($query);
  306. # clear flags
  307. for $i (1 .. $form->{rowcount}) {
  308. if ($form->{"cleared_$i"} && ! $form->{"oldcleared_$i"}) {
  309. if ($form->{summary}) {
  310. $sth->execute($form->{"source_$i"}, $form->{"transdate_$i"}) || $form->dberror;
  311. while (($trans_id) = $sth->fetchrow_array) {
  312. $tth->execute($trans_id, $form->{"transdate_$i"}) || $form->dberror;
  313. $tth->finish;
  314. }
  315. $sth->finish;
  316. } else {
  317. $tth->execute($form->{"id_$i"}, $form->{"transdate_$i"}) || $form->dberror;
  318. $tth->finish;
  319. }
  320. }
  321. }
  322. $dbh->disconnect;
  323. }
  324. 1;