summaryrefslogtreecommitdiff
path: root/LedgerSMB/CA.pm
blob: d6df616d5c5943354c11a6bfa20985e1d97fed80 (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. #
  5. # See COPYRIGHT file for copyright information
  6. #======================================================================
  7. #
  8. # This file has undergone whitespace cleanup.
  9. #
  10. #======================================================================
  11. #
  12. # chart of accounts
  13. #
  14. #======================================================================
  15. package CA;
  16. sub all_accounts {
  17. my ($self, $myconfig, $form) = @_;
  18. my $amount = ();
  19. # connect to database
  20. my $dbh = $form->dbconnect($myconfig);
  21. my $query = qq|SELECT accno, SUM(acc_trans.amount) AS amount
  22. FROM chart, acc_trans
  23. WHERE chart.id = acc_trans.chart_id
  24. GROUP BY accno|;
  25. my $sth = $dbh->prepare($query);
  26. $sth->execute || $form->dberror($query);
  27. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  28. $amount{$ref->{accno}} = $ref->{amount}
  29. }
  30. $sth->finish;
  31. $query = qq|SELECT accno, description
  32. FROM gifi|;
  33. $sth = $dbh->prepare($query);
  34. $sth->execute || $form->dberror($query);
  35. my $gifi = ();
  36. while (my ($accno, $description) = $sth->fetchrow_array) {
  37. $gifi{$accno} = $description;
  38. }
  39. $sth->finish;
  40. $query = qq|SELECT c.id, c.accno, c.description, c.charttype,
  41. c.gifi_accno, c.category, c.link
  42. FROM chart c
  43. ORDER BY accno|;
  44. $sth = $dbh->prepare($query);
  45. $sth->execute || $form->dberror($query);
  46. while (my $ca = $sth->fetchrow_hashref(NAME_lc)) {
  47. $ca->{amount} = $amount{$ca->{accno}};
  48. $ca->{gifi_description} = $gifi{$ca->{gifi_accno}};
  49. if ($ca->{amount} < 0) {
  50. $ca->{debit} = $ca->{amount} * -1;
  51. } else {
  52. $ca->{credit} = $ca->{amount};
  53. }
  54. push @{ $form->{CA} }, $ca;
  55. }
  56. $sth->finish;
  57. $dbh->disconnect;
  58. }
  59. sub all_transactions {
  60. my ($self, $myconfig, $form) = @_;
  61. # connect to database
  62. my $dbh = $form->dbconnect($myconfig);
  63. # get chart_id
  64. my $query = qq|SELECT id
  65. FROM chart
  66. WHERE accno = '$form->{accno}'|;
  67. if ($form->{accounttype} eq 'gifi') {
  68. $query = qq|SELECT id
  69. FROM chart
  70. WHERE gifi_accno = '$form->{gifi_accno}'|;
  71. }
  72. my $sth = $dbh->prepare($query);
  73. $sth->execute || $form->dberror($query);
  74. my @id = ();
  75. while (my ($id) = $sth->fetchrow_array) {
  76. push @id, $id;
  77. }
  78. $sth->finish;
  79. my $fromdate_where;
  80. my $todate_where;
  81. ($form->{fromdate}, $form->{todate}) = $form->from_to($form->{year}, $form->{month}, $form->{interval}) if $form->{year} && $form->{month};
  82. if ($form->{fromdate}) {
  83. $fromdate_where = qq| AND ac.transdate >= '$form->{fromdate}' |;
  84. }
  85. if ($form->{todate}) {
  86. $todate_where .= qq| AND ac.transdate <= '$form->{todate}' |;
  87. }
  88. my $false = ($myconfig->{dbdriver} =~ /Pg/) ? FALSE : q|'0'|;
  89. # Oracle workaround, use ordinal positions
  90. my %ordinal = ( transdate => 4,
  91. reference => 2,
  92. description => 3 );
  93. my @a = qw(transdate reference description);
  94. my $sortorder = $form->sort_order(\@a, \%ordinal);
  95. my $null;
  96. my $department_id;
  97. my $dpt_where;
  98. my $dpt_join;
  99. my $union;
  100. ($null, $department_id) = split /--/, $form->{department};
  101. if ($department_id) {
  102. $dpt_join = qq| JOIN department t ON (t.id = a.department_id) |;
  103. $dpt_where = qq| AND t.id = $department_id |;
  104. }
  105. my $project;
  106. my $project_id;
  107. if ($form->{projectnumber}) {
  108. ($null, $project_id) = split /--/, $form->{projectnumber};
  109. $project = qq| AND ac.project_id = $project_id |;
  110. }
  111. if ($form->{accno} || $form->{gifi_accno}) {
  112. # get category for account
  113. $query = qq|SELECT description, category, link, contra
  114. FROM chart
  115. WHERE accno = '$form->{accno}'|;
  116. if ($form->{accounttype} eq 'gifi') {
  117. $query = qq|SELECT description, category, link, contra
  118. FROM chart
  119. WHERE gifi_accno = '$form->{gifi_accno}'
  120. AND charttype = 'A'|;
  121. }
  122. ($form->{description}, $form->{category}, $form->{link}, $form->{contra}) = $dbh->selectrow_array($query);
  123. if ($form->{fromdate}) {
  124. if ($department_id) {
  125. # get beginning balance
  126. $query = "";
  127. $union = "";
  128. for (qw(ar ap gl)) {
  129. if ($form->{accounttype} eq 'gifi') {
  130. $query = qq| $union
  131. SELECT SUM(ac.amount)
  132. FROM acc_trans ac
  133. JOIN $_ a ON (a.id = ac.trans_id)
  134. JOIN chart c ON (ac.chart_id = c.id)
  135. WHERE c.gifi_accno = '$form->{gifi_accno}'
  136. AND ac.transdate < '$form->{fromdate}'
  137. AND a.department_id = $department_id
  138. $project |;
  139. } else {
  140. $query .= qq| $union
  141. SELECT SUM(ac.amount)
  142. FROM acc_trans ac
  143. JOIN $_ a ON (a.id = ac.trans_id)
  144. JOIN chart c ON (ac.chart_id = c.id)
  145. WHERE c.accno = '$form->{accno}'
  146. AND ac.transdate < '$form->{fromdate}'
  147. AND a.department_id = $department_id
  148. $project |;
  149. }
  150. $union = qq| UNION ALL |;
  151. }
  152. } else {
  153. if ($form->{accounttype} eq 'gifi') {
  154. $query = qq|SELECT SUM(ac.amount)
  155. FROM acc_trans ac
  156. JOIN chart c ON (ac.chart_id = c.id)
  157. WHERE c.gifi_accno = '$form->{gifi_accno}'
  158. AND ac.transdate < '$form->{fromdate}'
  159. $project |;
  160. } else {
  161. $query = qq|SELECT SUM(ac.amount)
  162. FROM acc_trans ac
  163. JOIN chart c ON (ac.chart_id = c.id)
  164. WHERE c.accno = '$form->{accno}'
  165. AND ac.transdate < '$form->{fromdate}'
  166. $project |;
  167. }
  168. }
  169. ($form->{balance}) = $dbh->selectrow_array($query);
  170. }
  171. }
  172. $query = "";
  173. $union = "";
  174. foreach my $id (@id) {
  175. # get all transactions
  176. $query .= qq|$union
  177. SELECT a.id, a.reference, a.description, ac.transdate,
  178. $false AS invoice, ac.amount, 'gl' as module, ac.cleared,
  179. ac.source, '' AS till, ac.chart_id
  180. FROM gl a
  181. JOIN acc_trans ac ON (ac.trans_id = a.id)
  182. $dpt_join
  183. WHERE ac.chart_id = $id
  184. $fromdate_where
  185. $todate_where
  186. $dpt_where
  187. $project
  188. UNION ALL
  189. SELECT a.id, a.invnumber, c.name, ac.transdate,
  190. a.invoice, ac.amount, 'ar' as module, ac.cleared,
  191. ac.source,
  192. a.till, ac.chart_id
  193. FROM ar a
  194. JOIN acc_trans ac ON (ac.trans_id = a.id)
  195. JOIN customer c ON (a.customer_id = c.id)
  196. $dpt_join
  197. WHERE ac.chart_id = $id
  198. $fromdate_where
  199. $todate_where
  200. $dpt_where
  201. $project
  202. UNION ALL
  203. SELECT a.id, a.invnumber, v.name, ac.transdate,
  204. a.invoice, ac.amount, 'ap' as module, ac.cleared,
  205. ac.source, a.till, ac.chart_id
  206. FROM ap a
  207. JOIN acc_trans ac ON (ac.trans_id = a.id)
  208. JOIN vendor v ON (a.vendor_id = v.id)
  209. $dpt_join
  210. WHERE ac.chart_id = $id
  211. $fromdate_where
  212. $todate_where
  213. $dpt_where
  214. $project |;
  215. $union = qq| UNION ALL |;
  216. }
  217. $query .= qq| ORDER BY $sortorder |;
  218. $sth = $dbh->prepare($query);
  219. $sth->execute || $form->dberror($query);
  220. $query = qq|SELECT c.id, c.accno
  221. FROM chart c
  222. JOIN acc_trans ac ON (ac.chart_id = c.id)
  223. WHERE ac.amount >= 0
  224. AND (c.link = 'AR' OR c.link = 'AP')
  225. AND ac.trans_id = ?|;
  226. my $dr = $dbh->prepare($query) || $form->dberror($query);
  227. $query = qq|SELECT c.id, c.accno
  228. FROM chart c
  229. JOIN acc_trans ac ON (ac.chart_id = c.id)
  230. WHERE ac.amount < 0
  231. AND (c.link = 'AR' OR c.link = 'AP')
  232. AND ac.trans_id = ?|;
  233. my $cr = $dbh->prepare($query) || $form->dberror($query);
  234. my $accno;
  235. my $chart_id;
  236. my %accno;
  237. while (my $ca = $sth->fetchrow_hashref(NAME_lc)) {
  238. # gl
  239. if ($ca->{module} eq "gl") {
  240. $ca->{module} = "gl";
  241. }
  242. # ap
  243. if ($ca->{module} eq "ap") {
  244. $ca->{module} = ($ca->{invoice}) ? 'ir' : 'ap';
  245. $ca->{module} = 'ps' if $ca->{till};
  246. }
  247. # ar
  248. if ($ca->{module} eq "ar") {
  249. $ca->{module} = ($ca->{invoice}) ? 'is' : 'ar';
  250. $ca->{module} = 'ps' if $ca->{till};
  251. }
  252. if ($ca->{amount}) {
  253. %accno = ();
  254. if ($ca->{amount} < 0) {
  255. $ca->{debit} = $ca->{amount} * -1;
  256. $ca->{credit} = 0;
  257. $dr->execute($ca->{id});
  258. $ca->{accno} = ();
  259. while (($chart_id, $accno) = $dr->fetchrow_array) {
  260. $accno{$accno} = 1 if $chart_id ne $ca->{chart_id};
  261. }
  262. $dr->finish;
  263. for (sort keys %accno) { push @{ $ca->{accno} }, "$_ " }
  264. } else {
  265. $ca->{credit} = $ca->{amount};
  266. $ca->{debit} = 0;
  267. $cr->execute($ca->{id});
  268. $ca->{accno} = ();
  269. while (($chart_id, $accno) = $cr->fetchrow_array) {
  270. $accno{$accno} = 1 if $chart_id ne $ca->{chart_id};
  271. }
  272. $cr->finish;
  273. for (keys %accno) { push @{ $ca->{accno} }, "$_ " }
  274. }
  275. push @{ $form->{CA} }, $ca;
  276. }
  277. }
  278. $sth->finish;
  279. $dbh->disconnect;
  280. }
  281. 1;