summaryrefslogtreecommitdiff
path: root/bin/ca.pl
blob: 7ec5a435bc2dffe93165c81eef3f13bc0abea504 (plain)
  1. #=====================================================================
  2. # LedgerSMB Small Medium Business Accounting
  3. # http://www.ledgersmb.org/
  4. #
  5. # Copyright (C) 2006
  6. # This work contains copyrighted information from a number of sources all used
  7. # with permission.
  8. #
  9. # This file contains source code included with or based on SQL-Ledger which
  10. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  11. # under the GNU General Public License version 2 or, at your option, any later
  12. # version. For a full list including contact information of contributors,
  13. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  14. #
  15. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  16. # Copyright (C) 2001
  17. #
  18. # Author: DWS Systems Inc.
  19. # Web: http://www.sql-ledger.org
  20. #
  21. # Contributors:
  22. #
  23. #
  24. # Author: DWS Systems Inc.
  25. # Web: http://www.ledgersmb.org/
  26. #
  27. # Contributors:
  28. #
  29. # This program is free software; you can redistribute it and/or modify
  30. # it under the terms of the GNU General Public License as published by
  31. # the Free Software Foundation; either version 2 of the License, or
  32. # (at your option) any later version.
  33. #
  34. # This program is distributed in the hope that it will be useful,
  35. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. # GNU General Public License for more details.
  38. # You should have received a copy of the GNU General Public License
  39. # along with this program; if not, write to the Free Software
  40. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  41. #======================================================================
  42. #
  43. # module for Chart of Accounts, Income Statement and Balance Sheet
  44. # search and edit transactions posted by the GL, AR and AP
  45. #
  46. #======================================================================
  47. use LedgerSMB::CA;
  48. 1;
  49. # end of main
  50. # this is for our long dates
  51. # $locale->text('January')
  52. # $locale->text('February')
  53. # $locale->text('March')
  54. # $locale->text('April')
  55. # $locale->text('May ')
  56. # $locale->text('June')
  57. # $locale->text('July')
  58. # $locale->text('August')
  59. # $locale->text('September')
  60. # $locale->text('October')
  61. # $locale->text('November')
  62. # $locale->text('December')
  63. # this is for our short month
  64. # $locale->text('Jan')
  65. # $locale->text('Feb')
  66. # $locale->text('Mar')
  67. # $locale->text('Apr')
  68. # $locale->text('May')
  69. # $locale->text('Jun')
  70. # $locale->text('Jul')
  71. # $locale->text('Aug')
  72. # $locale->text('Sep')
  73. # $locale->text('Oct')
  74. # $locale->text('Nov')
  75. # $locale->text('Dec')
  76. sub chart_of_accounts {
  77. CA->all_accounts(\%myconfig, \%$form);
  78. @column_index = qw(accno gifi_accno description debit credit);
  79. $column_header{accno} = qq|<th class=listtop>|.$locale->text('Account').qq|</th>\n|;
  80. $column_header{gifi_accno} = qq|<th class=listtop>|.$locale->text('GIFI').qq|</th>\n|;
  81. $column_header{description} = qq|<th class=listtop>|.$locale->text('Description').qq|</th>\n|;
  82. $column_header{debit} = qq|<th class=listtop>|.$locale->text('Debit').qq|</th>\n|;
  83. $column_header{credit} = qq|<th class=listtop>|.$locale->text('Credit').qq|</th>\n|;
  84. $form->{title} = $locale->text('Chart of Accounts');
  85. $colspan = $#column_index + 1;
  86. $form->header;
  87. print qq|
  88. <body>
  89. <table border=0 width=100%>
  90. <tr><th class=listtop colspan=$colspan>$form->{title}</th></tr>
  91. <tr height="5"></tr>
  92. <tr class=listheading>|;
  93. for (@column_index) { print $column_header{$_} }
  94. print qq|
  95. </tr>
  96. |;
  97. foreach $ca (@{ $form->{CA} }) {
  98. $description = $form->escape($ca->{description});
  99. $gifi_description = $form->escape($ca->{gifi_description});
  100. $href = qq|$form->{script}?path=$form->{path}&action=list&accno=$ca->{accno}&login=$form->{login}&sessionid=$form->{sessionid}&description=$description&gifi_accno=$ca->{gifi_accno}&gifi_description=$gifi_description|;
  101. if ($ca->{charttype} eq "H") {
  102. print qq|<tr class=listheading>|;
  103. for (qw(accno description)) { $column_data{$_} = "<th class=listheading>$ca->{$_}</th>" }
  104. $column_data{gifi_accno} = "<th class=listheading>$ca->{gifi_accno}&nbsp;</th>";
  105. } else {
  106. $i++; $i %= 2;
  107. print qq|<tr class=listrow$i>|;
  108. $column_data{accno} = "<td><a href=$href>$ca->{accno}</a></td>";
  109. $column_data{gifi_accno} = "<td><a href=$href&accounttype=gifi>$ca->{gifi_accno}</a>&nbsp;</td>";
  110. $column_data{description} = "<td>$ca->{description}</td>";
  111. }
  112. $column_data{debit} = "<td align=right>".$form->format_amount(\%myconfig, $ca->{debit}, 2, "&nbsp;")."</td>\n";
  113. $column_data{credit} = "<td align=right>".$form->format_amount(\%myconfig, $ca->{credit}, 2, "&nbsp;")."</td>\n";
  114. $totaldebit += $ca->{debit};
  115. $totalcredit += $ca->{credit};
  116. for (@column_index) { print "$column_data{$_}\n" }
  117. print qq|
  118. </tr>
  119. |;
  120. }
  121. for (qw(accno gifi_accno description)) { $column_data{$_} = "<td>&nbsp;</td>" }
  122. $column_data{debit} = "<th align=right class=listtotal>".$form->format_amount(\%myconfig, $totaldebit, 2, 0)."</th>";
  123. $column_data{credit} = "<th align=right class=listtotal>".$form->format_amount(\%myconfig, $totalcredit, 2, 0)."</th>";
  124. print "<tr class=listtotal>";
  125. for (@column_index) { print "$column_data{$_}\n" }
  126. print qq|
  127. </tr>
  128. <tr>
  129. <td colspan=$colspan><hr size=3 noshade></td>
  130. </tr>
  131. </table>
  132. </body>
  133. </html>
  134. |;
  135. }
  136. sub list {
  137. $form->{title} = $locale->text('List Transactions');
  138. if ($form->{accounttype} eq 'gifi') {
  139. $form->{title} .= " - ".$locale->text('GIFI')." $form->{gifi_accno} - $form->{gifi_description}";
  140. } else {
  141. $form->{title} .= " - ".$locale->text('Account')." $form->{accno} - $form->{description}";
  142. }
  143. # get departments
  144. $form->all_departments(\%myconfig);
  145. if (@{ $form->{all_department} }) {
  146. $form->{selectdepartment} = "<option>\n";
  147. for (@{ $form->{all_department} }) { $form->{selectdepartment} .= qq|<option value="$_->{description}--$_->{id}">$_->{description}\n| }
  148. }
  149. $department = qq|
  150. <tr>
  151. <th align=right nowrap>|.$locale->text('Department').qq|</th>
  152. <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
  153. </tr>
  154. | if $form->{selectdepartment};
  155. if (@{ $form->{all_years} }) {
  156. # accounting years
  157. $form->{selectaccountingyear} = "<option>\n";
  158. for (@{ $form->{all_years} }) { $form->{selectaccountingyear} .= qq|<option>$_\n| }
  159. $form->{selectaccountingmonth} = "<option>\n";
  160. for (sort keys %{ $form->{all_month} }) { $form->{selectaccountingmonth} .= qq|<option value=$_>|.$locale->text($form->{all_month}{$_}).qq|\n| }
  161. $selectfrom = qq|
  162. <tr>
  163. <th align=right>|.$locale->text('Period').qq|</th>
  164. <td colspan=3>
  165. <select name=month>$form->{selectaccountingmonth}</select>
  166. <select name=year>$form->{selectaccountingyear}</select>
  167. <input name=interval class=radio type=radio value=0 checked>&nbsp;|.$locale->text('Current').qq|
  168. <input name=interval class=radio type=radio value=1>&nbsp;|.$locale->text('Month').qq|
  169. <input name=interval class=radio type=radio value=3>&nbsp;|.$locale->text('Quarter').qq|
  170. <input name=interval class=radio type=radio value=12>&nbsp;|.$locale->text('Year').qq|
  171. </td>
  172. </tr>
  173. |;
  174. }
  175. $form->header;
  176. print qq|
  177. <body>
  178. <form method=post action=$form->{script}>
  179. <input type=hidden name=accno value=$form->{accno}>
  180. <input type=hidden name=description value="$form->{description}">
  181. <input type=hidden name=sort value=transdate>
  182. <input type=hidden name=oldsort value=transdate>
  183. <input type=hidden name=accounttype value=$form->{accounttype}>
  184. <input type=hidden name=gifi_accno value=$form->{gifi_accno}>
  185. <input type=hidden name=gifi_description value="$form->{gifi_description}">
  186. <table border=0 width=100%>
  187. <tr><th class=listtop>$form->{title}</th></tr>
  188. <tr height="5"></tr
  189. <tr valign=top>
  190. <td>
  191. <table>
  192. $department
  193. <tr>
  194. <th align=right>|.$locale->text('From').qq|</th>
  195. <td><input name=fromdate size=11 title="$myconfig{dateformat}"></td>
  196. <th align=right>|.$locale->text('To').qq|</th>
  197. <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
  198. </tr>
  199. $selectfrom
  200. <tr>
  201. <th align=right>|.$locale->text('Include in Report').qq|</th>
  202. <td colspan=3>
  203. <input name=l_accno class=checkbox type=checkbox value=Y>&nbsp;|.$locale->text('AR/AP').qq|
  204. <input name=l_subtotal class=checkbox type=checkbox value=Y>&nbsp;|.$locale->text('Subtotal').qq|
  205. </td>
  206. </tr>
  207. </table>
  208. </td>
  209. </tr>
  210. <tr><td><hr size=3 noshade></td></tr>
  211. </table>
  212. <input type=hidden name=login value=$form->{login}>
  213. <input type=hidden name=path value=$form->{path}>
  214. <input type=hidden name=sessionid value=$form->{sessionid}>
  215. <br><input class=submit type=submit name=action value="|.$locale->text('List Transactions').qq|">
  216. </form>
  217. </body>
  218. </html>
  219. |;
  220. }
  221. sub list_transactions {
  222. CA->all_transactions(\%myconfig, \%$form);
  223. $department = $form->escape($form->{department});
  224. $projectnumber = $form->escape($form->{projectnumber});
  225. $title = $form->escape($form->{title});
  226. # construct href
  227. $href = "$form->{script}?action=list_transactions&department=$department&projectnumber=$projectnumber&title=$title";
  228. for (qw(path oldsort accno login sessionid fromdate todate accounttype gifi_accno l_heading l_subtotal l_accno)) { $href .= "&$_=$form->{$_}" }
  229. $drilldown = $href;
  230. $drilldown .= "&sort=$form->{sort}";
  231. $href .= "&direction=$form->{direction}";
  232. $form->sort_order();
  233. $drilldown .= "&direction=$form->{direction}";
  234. $form->{prevreport} = $href unless $form->{prevreport};
  235. $href .= "&prevreport=".$form->escape($form->{prevreport});
  236. $drilldown .= "&prevreport=".$form->escape($form->{prevreport});
  237. # figure out which column comes first
  238. $column_header{transdate} = qq|<th><a class=listheading href=$href&sort=transdate>|.$locale->text('Date').qq|</a></th>|;
  239. $column_header{reference} = qq|<th><a class=listheading href=$href&sort=reference>|.$locale->text('Reference').qq|</a></th>|;
  240. $column_header{description} = qq|<th><a class=listheading href=$href&sort=description>|.$locale->text('Description').qq|</a></th>|;
  241. $column_header{cleared} = qq|<th class=listheading>|.$locale->text('R').qq|</th>|;
  242. $column_header{source} = qq|<th class=listheading>|.$locale->text('Source').qq|</th>|;
  243. $column_header{debit} = qq|<th class=listheading>|.$locale->text('Debit').qq|</th>|;
  244. $column_header{credit} = qq|<th class=listheading>|.$locale->text('Credit').qq|</th>|;
  245. $column_header{balance} = qq|<th class=listheading>|.$locale->text('Balance').qq|</th>|;
  246. $column_header{accno} = qq|<th class=listheading>|.$locale->text('AR/AP').qq|</th>|;
  247. @columns = qw(transdate reference description debit credit);
  248. if ($form->{link} =~ /_paid/) {
  249. @columns = qw(transdate reference description source cleared debit credit);
  250. }
  251. push @columns, "accno" if $form->{l_accno};
  252. @column_index = $form->sort_columns(@columns);
  253. if ($form->{accounttype} eq 'gifi') {
  254. for (qw(accno description)) { $form->{$_} = $form->{"gifi_$_"} }
  255. }
  256. if ($form->{accno}) {
  257. push @column_index, "balance";
  258. }
  259. $form->{title} = ($form->{accounttype} eq 'gifi') ? $locale->text('GIFI') : $locale->text('Account');
  260. $form->{title} .= " $form->{accno} - $form->{description}";
  261. if ($form->{department}) {
  262. ($department) = split /--/, $form->{department};
  263. $options = $locale->text('Department')." : $department<br>";
  264. }
  265. if ($form->{projectnumber}) {
  266. ($projectnumber) = split /--/, $form->{projectnumber};
  267. $options .= $locale->text('Project Number')." : $projectnumber<br>";
  268. }
  269. if ($form->{fromdate} || $form->{todate}) {
  270. if ($form->{fromdate}) {
  271. $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
  272. }
  273. if ($form->{todate}) {
  274. $todate = $locale->date(\%myconfig, $form->{todate}, 1);
  275. }
  276. $form->{period} = "$fromdate - $todate";
  277. } else {
  278. $form->{period} = $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
  279. }
  280. $form->{period} = "<a href=$form->{prevreport}>$form->{period}</a>" if $form->{prevreport};
  281. $options .= $form->{period};
  282. # construct callback
  283. $department = $form->escape($form->{department},1);
  284. $projectnumber = $form->escape($form->{projectnumber},1);
  285. $title = $form->escape($form->{title},1);
  286. $form->{prevreport} = $form->escape($form->{prevreport},1);
  287. $form->{callback} = "$form->{script}?action=list_transactions&department=$department&projectnumber=$projectnumber&title=$title";
  288. for (qw(path direction oldsort accno login sessionid fromdate todate accounttype gifi_accno l_heading l_subtotal l_accno prevreport)) { $form->{callback} .= "&$_=$form->{$_}" }
  289. $form->header;
  290. print qq|
  291. <body>
  292. <table width=100%>
  293. <tr>
  294. <th class=listtop>$form->{title}</th>
  295. </tr>
  296. <tr height="5"></tr>
  297. <tr>
  298. <td>$options</td>
  299. </tr>
  300. <tr>
  301. <td>
  302. <table width=100%>
  303. <tr class=listheading>
  304. |;
  305. for (@column_index) { print "$column_header{$_}\n" }
  306. print qq|
  307. </tr>
  308. |;
  309. # add sort to callback
  310. $form->{callback} = $form->escape($form->{callback} . "&sort=$form->{sort}");
  311. if (@{ $form->{CA} }) {
  312. $sameitem = $form->{CA}->[0]->{$form->{sort}};
  313. }
  314. $ml = ($form->{category} =~ /(A|E)/) ? -1 : 1;
  315. $ml *= -1 if $form->{contra};
  316. if ($form->{accno} && $form->{balance}) {
  317. for (@column_index) { $column_data{$_} = "<td>&nbsp;</td>" }
  318. $column_data{balance} = "<td align=right>".$form->format_amount(\%myconfig, $form->{balance} * $ml, 2, 0)."</td>";
  319. $i++; $i %= 2;
  320. print qq|
  321. <tr class=listrow$i>
  322. |;
  323. for (@column_index) { print "$column_data{$_}\n" }
  324. print qq|
  325. </tr>
  326. |;
  327. }
  328. foreach $ca (@{ $form->{CA} }) {
  329. if ($form->{l_subtotal} eq 'Y') {
  330. if ($sameitem ne $ca->{$form->{sort}}) {
  331. &ca_subtotal;
  332. }
  333. }
  334. # construct link to source
  335. $href = "<a href=$ca->{module}.pl?path=$form->{path}&action=edit&id=$ca->{id}&login=$form->{login}&sessionid=$form->{sessionid}&callback=$form->{callback}>$ca->{reference}</a>";
  336. $column_data{debit} = "<td align=right>".$form->format_amount(\%myconfig, $ca->{debit}, 2, "&nbsp;")."</td>";
  337. $column_data{credit} = "<td align=right>".$form->format_amount(\%myconfig, $ca->{credit}, 2, "&nbsp;")."</td>";
  338. $form->{balance} += $ca->{amount};
  339. $column_data{balance} = "<td align=right>".$form->format_amount(\%myconfig, $form->{balance} * $ml, 2, 0)."</td>";
  340. $subtotaldebit += $ca->{debit};
  341. $subtotalcredit += $ca->{credit};
  342. $totaldebit += $ca->{debit};
  343. $totalcredit += $ca->{credit};
  344. $column_data{transdate} = qq|<td>$ca->{transdate}</td>|;
  345. $column_data{reference} = qq|<td>$href</td>|;
  346. $column_data{description} = qq|<td>$ca->{description}&nbsp;</td>|;
  347. $column_data{cleared} = ($ca->{cleared}) ? qq|<td>*</td>| : qq|<td>&nbsp;</td>|;
  348. $column_data{source} = qq|<td>$ca->{source}&nbsp;</td>|;
  349. $column_data{accno} = qq|<td>|;
  350. for (@{ $ca->{accno} }) { $column_data{accno} .= "<a href=$drilldown&accno=$_>$_</a> " }
  351. $column_data{accno} .= qq|&nbsp;</td>|;
  352. if ($ca->{id} != $sameid) {
  353. $i++; $i %= 2;
  354. }
  355. $sameid = $ca->{id};
  356. print qq|
  357. <tr class=listrow$i>
  358. |;
  359. for (@column_index) { print "$column_data{$_}\n" }
  360. print qq|
  361. </tr>
  362. |;
  363. }
  364. if ($form->{l_subtotal} eq 'Y') {
  365. &ca_subtotal;
  366. }
  367. for (@column_index) { $column_data{$_} = "<td>&nbsp;</td>" }
  368. $column_data{debit} = "<th align=right class=listtotal>".$form->format_amount(\%myconfig, $totaldebit, 2, "&nbsp;")."</th>";
  369. $column_data{credit} = "<th align=right class=listtotal>".$form->format_amount(\%myconfig, $totalcredit, 2, "&nbsp;")."</th>";
  370. $column_data{balance} = "<th align=right class=listtotal>".$form->format_amount(\%myconfig, $form->{balance} * $ml, 2, 0)."</th>";
  371. print qq|
  372. <tr class=listtotal>
  373. |;
  374. for (@column_index) { print "$column_data{$_}\n" }
  375. print qq|
  376. </tr>
  377. </table>
  378. </td>
  379. </tr>
  380. <tr>
  381. <td><hr size=3 noshade></td>
  382. </tr>
  383. </table>
  384. </body>
  385. </html>
  386. |;
  387. }
  388. sub ca_subtotal {
  389. for (@column_index) { $column_data{$_} = "<td>&nbsp;</td>" }
  390. $column_data{debit} = "<th align=right class=listsubtotal>".$form->format_amount(\%myconfig, $subtotaldebit, 2, "&nbsp;") . "</th>";
  391. $column_data{credit} = "<th align=right class=listsubtotal>".$form->format_amount(\%myconfig, $subtotalcredit, 2, "&nbsp;") . "</th>";
  392. $subtotaldebit = 0;
  393. $subtotalcredit = 0;
  394. $sameitem = $ca->{$form->{sort}};
  395. print qq|
  396. <tr class=listsubtotal>
  397. |;
  398. for (@column_index) { print "$column_data{$_}\n" }
  399. print qq|
  400. </tr>
  401. |;
  402. }