summaryrefslogtreecommitdiff
path: root/bin/ca.pl
blob: 09cc8dbe63940bd347ed211ce86765c9c7cee6de (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. use LedgerSMB::Template;
  49. 1;
  50. # end of main
  51. # this is for our long dates
  52. # $locale->text('January')
  53. # $locale->text('February')
  54. # $locale->text('March')
  55. # $locale->text('April')
  56. # $locale->text('May ')
  57. # $locale->text('June')
  58. # $locale->text('July')
  59. # $locale->text('August')
  60. # $locale->text('September')
  61. # $locale->text('October')
  62. # $locale->text('November')
  63. # $locale->text('December')
  64. # this is for our short month
  65. # $locale->text('Jan')
  66. # $locale->text('Feb')
  67. # $locale->text('Mar')
  68. # $locale->text('Apr')
  69. # $locale->text('May')
  70. # $locale->text('Jun')
  71. # $locale->text('Jul')
  72. # $locale->text('Aug')
  73. # $locale->text('Sep')
  74. # $locale->text('Oct')
  75. # $locale->text('Nov')
  76. # $locale->text('Dec')
  77. sub chart_of_accounts {
  78. CA->all_accounts( \%myconfig, \%$form );
  79. @column_index = qw(accno gifi_accno description debit credit);
  80. $column_header{accno} = $locale->text('Account');
  81. $column_header{gifi_accno} = $locale->text('GIFI');
  82. $column_header{description} = $locale->text('Description');
  83. $column_header{debit} = $locale->text('Debit');
  84. $column_header{credit} = $locale->text('Credit');
  85. $form->{title} = $locale->text('Chart of Accounts');
  86. $form->{callback} =
  87. qq|$form->{script}?path=$form->{path}&action=chart_of_accounts&login=$form->{login}&sessionid=$form->{sessionid}|;
  88. my @rows;
  89. my $totaldebit = 0;
  90. my $totalcredit = 0;
  91. foreach my $ca ( @{ $form->{CA} } ) {
  92. my %column_data;
  93. my $description = $form->escape( $ca->{description} );
  94. my $gifi_description = $form->escape( $ca->{gifi_description} );
  95. my $href =
  96. 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|;
  97. if ( $ca->{charttype} eq "H" ) {
  98. $column_data{heading} = 'H';
  99. for (qw(accno description)) {
  100. $column_data{$_} = $ca->{$_};
  101. }
  102. $column_data{gifi_accno} = $ca->{gifi_accno};
  103. }
  104. else {
  105. $i++;
  106. $i %= 2;
  107. $column_data{i} = $i;
  108. $column_data{accno} = {
  109. text => $ca->{accno},
  110. href => $href};
  111. $column_data{gifi_accno} = {
  112. text => $ca->{gifi_accno},
  113. href => "$href&accounttype=gifi"};
  114. $column_data{description} = $ca->{description};
  115. }
  116. $column_data{debit} =
  117. $form->format_amount( \%myconfig, $ca->{debit}, 2, " " );
  118. $column_data{credit} =
  119. $form->format_amount( \%myconfig, $ca->{credit}, 2, " " );
  120. $totaldebit += $ca->{debit};
  121. $totalcredit += $ca->{credit};
  122. push @rows, \%column_data;
  123. }
  124. for (qw(accno gifi_accno description)) {
  125. $column_data{$_} = " ";
  126. }
  127. $column_data{debit} = $form->format_amount( \%myconfig, $totaldebit, 2, 0 );
  128. $column_data{credit} = $form->format_amount(\%myconfig, $totalcredit, 2, 0);
  129. my @buttons;
  130. push @buttons, {
  131. name => 'action',
  132. value => 'csv_chart_of_accounts',
  133. text => $locale->text('CSV Report'),
  134. type => 'submit',
  135. class => 'submit',
  136. };
  137. my $template = LedgerSMB::Template->new(
  138. user => \%myconfig,
  139. locale => $locale,
  140. path => 'UI',
  141. template => 'am-list-accounts',
  142. format => ($form->{action} =~ /^csv/)? 'CSV': 'HTML');
  143. $template->render({
  144. form => \%$form,
  145. buttons => \@buttons,
  146. columns => \@column_index,
  147. heading => \%column_header,
  148. totals => \%column_data,
  149. rows => \@rows,
  150. });
  151. }
  152. sub csv_chart_of_accounts { &chart_of_accounts }
  153. sub list {
  154. $form->{title} = $locale->text('List Transactions');
  155. if ( $form->{accounttype} eq 'gifi' ) {
  156. $form->{title} .= " - "
  157. . $locale->text('GIFI')
  158. . " $form->{gifi_accno} - $form->{gifi_description}";
  159. }
  160. else {
  161. $form->{title} .= " - "
  162. . $locale->text('Account')
  163. . " $form->{accno} - $form->{description}";
  164. }
  165. # get departments
  166. $form->all_departments( \%myconfig );
  167. if ( @{ $form->{all_department} } ) {
  168. $form->{selectdepartment} = "<option>\n";
  169. for ( @{ $form->{all_department} } ) {
  170. $form->{selectdepartment} .=
  171. qq|<option value="$_->{description}--$_->{id}">$_->{description}\n|;
  172. }
  173. }
  174. $department = qq|
  175. <tr>
  176. <th align=right nowrap>| . $locale->text('Department') . qq|</th>
  177. <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
  178. </tr>
  179. | if $form->{selectdepartment};
  180. if ( @{ $form->{all_years} } ) {
  181. # accounting years
  182. $form->{selectaccountingyear} = "<option>\n";
  183. for ( @{ $form->{all_years} } ) {
  184. $form->{selectaccountingyear} .= qq|<option>$_\n|;
  185. }
  186. $form->{selectaccountingmonth} = "<option>\n";
  187. for ( sort keys %{ $form->{all_month} } ) {
  188. $form->{selectaccountingmonth} .=
  189. qq|<option value=$_>|
  190. . $locale->text( $form->{all_month}{$_} ) . qq|\n|;
  191. }
  192. $selectfrom = qq|
  193. <tr>
  194. <th align=right>| . $locale->text('Period') . qq|</th>
  195. <td colspan=3>
  196. <select name=month>$form->{selectaccountingmonth}</select>
  197. <select name=year>$form->{selectaccountingyear}</select>
  198. <input name=interval class=radio type=radio value=0 checked>&nbsp;|
  199. . $locale->text('Current') . qq|
  200. <input name=interval class=radio type=radio value=1>&nbsp;|
  201. . $locale->text('Month') . qq|
  202. <input name=interval class=radio type=radio value=3>&nbsp;|
  203. . $locale->text('Quarter') . qq|
  204. <input name=interval class=radio type=radio value=12>&nbsp;|
  205. . $locale->text('Year') . qq|
  206. </td>
  207. </tr>
  208. |;
  209. }
  210. $form->header;
  211. print qq|
  212. <body>
  213. <form method=post action=$form->{script}>
  214. <input type=hidden name=accno value=$form->{accno}>
  215. <input type=hidden name=description value="$form->{description}">
  216. <input type=hidden name=sort value=transdate>
  217. <input type=hidden name=oldsort value=transdate>
  218. <input type=hidden name=accounttype value=$form->{accounttype}>
  219. <input type=hidden name=gifi_accno value=$form->{gifi_accno}>
  220. <input type=hidden name=gifi_description value="$form->{gifi_description}">
  221. <table border=0 width=100%>
  222. <tr><th class=listtop>$form->{title}</th></tr>
  223. <tr height="5"></tr>
  224. <tr valign=top>
  225. <td>
  226. <table>
  227. $department
  228. <tr>
  229. <th align=right>| . $locale->text('From') . qq|</th>
  230. <td><input class="date" name=fromdate size=11 title="$myconfig{dateformat}"></td>
  231. <th align=right>| . $locale->text('To') . qq|</th>
  232. <td><input class="date" name=todate size=11 title="$myconfig{dateformat}"></td>
  233. </tr>
  234. $selectfrom
  235. <tr>
  236. <th align=right>| . $locale->text('Include in Report') . qq|</th>
  237. <td colspan=3>
  238. <input name=l_accno class=checkbox type=checkbox value=Y>&nbsp;|
  239. . $locale->text('AR/AP') . qq|
  240. <input name=l_subtotal class=checkbox type=checkbox value=Y>&nbsp;|
  241. . $locale->text('Subtotal') . qq|
  242. </td>
  243. </tr>
  244. </table>
  245. </td>
  246. </tr>
  247. <tr><td><hr size=3 noshade></td></tr>
  248. </table>
  249. <input type="hidden" name="login" value="$form->{login}">
  250. <input type="hidden" name="path" value="$form->{path}">
  251. <input type="hidden" name="sessionid" value="$form->{sessionid}">
  252. <br><button class="submit" type="submit" name="action" value="list_transactions">|
  253. . $locale->text('List Transactions')
  254. . qq|</button>
  255. </form>
  256. </body>
  257. </html>
  258. |;
  259. }
  260. sub list_transactions {
  261. CA->all_transactions( \%myconfig, \%$form );
  262. $department = $form->escape( $form->{department} );
  263. $projectnumber = $form->escape( $form->{projectnumber} );
  264. $title = $form->escape( $form->{title} );
  265. # construct href
  266. $href =
  267. "$form->{script}?action=list_transactions&department=$department&projectnumber=$projectnumber&title=$title";
  268. for (
  269. qw(path oldsort accno login sessionid fromdate todate accounttype gifi_accno l_heading l_subtotal l_accno)
  270. )
  271. {
  272. $href .= "&$_=$form->{$_}";
  273. }
  274. $drilldown = $href;
  275. $drilldown .= "&sort=$form->{sort}";
  276. $href .= "&direction=$form->{direction}";
  277. $form->sort_order();
  278. $drilldown .= "&direction=$form->{direction}";
  279. $form->{prevreport} = $href unless $form->{prevreport};
  280. $href .= "&prevreport=" . $form->escape( $form->{prevreport} );
  281. $drilldown .= "&prevreport=" . $form->escape( $form->{prevreport} );
  282. # figure out which column comes first
  283. $column_header{transdate} =
  284. qq|<th><a class=listheading href=$href&sort=transdate>|
  285. . $locale->text('Date')
  286. . qq|</a></th>|;
  287. $column_header{reference} =
  288. qq|<th><a class=listheading href=$href&sort=reference>|
  289. . $locale->text('Reference')
  290. . qq|</a></th>|;
  291. $column_header{description} =
  292. qq|<th><a class=listheading href=$href&sort=description>|
  293. . $locale->text('Description')
  294. . qq|</a></th>|;
  295. $column_header{cleared} =
  296. qq|<th class=listheading>| . $locale->text('R') . qq|</th>|;
  297. $column_header{source} =
  298. qq|<th class=listheading>| . $locale->text('Source') . qq|</th>|;
  299. $column_header{debit} =
  300. qq|<th class=listheading>| . $locale->text('Debit') . qq|</th>|;
  301. $column_header{credit} =
  302. qq|<th class=listheading>| . $locale->text('Credit') . qq|</th>|;
  303. $column_header{balance} =
  304. qq|<th class=listheading>| . $locale->text('Balance') . qq|</th>|;
  305. $column_header{accno} =
  306. qq|<th class=listheading>| . $locale->text('AR/AP') . qq|</th>|;
  307. @columns = qw(transdate reference description debit credit);
  308. if ( $form->{link} =~ /_paid/ ) {
  309. @columns =
  310. qw(transdate reference description source cleared debit credit);
  311. }
  312. push @columns, "accno" if $form->{l_accno};
  313. @column_index = $form->sort_columns(@columns);
  314. if ( $form->{accounttype} eq 'gifi' ) {
  315. for (qw(accno description)) { $form->{$_} = $form->{"gifi_$_"} }
  316. }
  317. if ( $form->{accno} ) {
  318. push @column_index, "balance";
  319. }
  320. $form->{title} =
  321. ( $form->{accounttype} eq 'gifi' )
  322. ? $locale->text('GIFI')
  323. : $locale->text('Account');
  324. $form->{title} .= " $form->{accno} - $form->{description}";
  325. if ( $form->{department} ) {
  326. ($department) = split /--/, $form->{department};
  327. $options = $locale->text('Department') . " : $department<br>";
  328. }
  329. if ( $form->{projectnumber} ) {
  330. ($projectnumber) = split /--/, $form->{projectnumber};
  331. $options .= $locale->text('Project Number') . " : $projectnumber<br>";
  332. }
  333. if ( $form->{fromdate} || $form->{todate} ) {
  334. if ( $form->{fromdate} ) {
  335. $fromdate = $locale->date( \%myconfig, $form->{fromdate}, 1 );
  336. }
  337. if ( $form->{todate} ) {
  338. $todate = $locale->date( \%myconfig, $form->{todate}, 1 );
  339. }
  340. $form->{period} = "$fromdate - $todate";
  341. }
  342. else {
  343. $form->{period} =
  344. $locale->date( \%myconfig, $form->current_date( \%myconfig ), 1 );
  345. }
  346. $form->{period} = "<a href=$form->{prevreport}>$form->{period}</a>"
  347. if $form->{prevreport};
  348. $options .= $form->{period};
  349. # construct callback
  350. $department = $form->escape( $form->{department}, 1 );
  351. $projectnumber = $form->escape( $form->{projectnumber}, 1 );
  352. $title = $form->escape( $form->{title}, 1 );
  353. $form->{prevreport} = $form->escape( $form->{prevreport}, 1 );
  354. $form->{callback} =
  355. "$form->{script}?action=list_transactions&department=$department&projectnumber=$projectnumber&title=$title";
  356. for (
  357. qw(path direction oldsort accno login sessionid fromdate todate accounttype gifi_accno l_heading l_subtotal l_accno prevreport)
  358. )
  359. {
  360. $form->{callback} .= "&$_=$form->{$_}";
  361. }
  362. $form->header;
  363. print qq|
  364. <body>
  365. <table width=100%>
  366. <tr>
  367. <th class=listtop>$form->{title}</th>
  368. </tr>
  369. <tr height="5"></tr>
  370. <tr>
  371. <td>$options</td>
  372. </tr>
  373. <tr>
  374. <td>
  375. <table width=100%>
  376. <tr class=listheading>
  377. |;
  378. for (@column_index) { print "$column_header{$_}\n" }
  379. print qq|
  380. </tr>
  381. |;
  382. # add sort to callback
  383. $form->{callback} =
  384. $form->escape( $form->{callback} . "&sort=$form->{sort}" );
  385. if ( @{ $form->{CA} } ) {
  386. $sameitem = $form->{CA}->[0]->{ $form->{sort} };
  387. }
  388. $ml = ( $form->{category} =~ /(A|E)/ ) ? -1 : 1;
  389. $ml *= -1 if $form->{contra};
  390. if ( $form->{accno} && $form->{balance} ) {
  391. for (@column_index) { $column_data{$_} = "<td>&nbsp;</td>" }
  392. $column_data{balance} =
  393. "<td align=right>"
  394. . $form->format_amount( \%myconfig, $form->{balance} * $ml, 2, 0 )
  395. . "</td>";
  396. $i++;
  397. $i %= 2;
  398. print qq|
  399. <tr class=listrow$i>
  400. |;
  401. for (@column_index) { print "$column_data{$_}\n" }
  402. print qq|
  403. </tr>
  404. |;
  405. }
  406. foreach $ca ( @{ $form->{CA} } ) {
  407. if ( $form->{l_subtotal} eq 'Y' ) {
  408. if ( $sameitem ne $ca->{ $form->{sort} } ) {
  409. &ca_subtotal;
  410. }
  411. }
  412. # construct link to source
  413. $href =
  414. "<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>";
  415. $column_data{debit} =
  416. "<td align=right>"
  417. . $form->format_amount( \%myconfig, $ca->{debit}, 2, "&nbsp;" )
  418. . "</td>";
  419. $column_data{credit} =
  420. "<td align=right>"
  421. . $form->format_amount( \%myconfig, $ca->{credit}, 2, "&nbsp;" )
  422. . "</td>";
  423. $form->{balance} += $ca->{amount};
  424. $column_data{balance} =
  425. "<td align=right>"
  426. . $form->format_amount( \%myconfig, $form->{balance} * $ml, 2, 0 )
  427. . "</td>";
  428. $subtotaldebit += $ca->{debit};
  429. $subtotalcredit += $ca->{credit};
  430. $totaldebit += $ca->{debit};
  431. $totalcredit += $ca->{credit};
  432. $column_data{transdate} = qq|<td>$ca->{transdate}</td>|;
  433. $column_data{reference} = qq|<td>$href</td>|;
  434. $column_data{description} = qq|<td>$ca->{description}&nbsp;</td>|;
  435. $column_data{cleared} =
  436. ( $ca->{cleared} ) ? qq|<td>*</td>| : qq|<td>&nbsp;</td>|;
  437. $column_data{source} = qq|<td>$ca->{source}&nbsp;</td>|;
  438. $column_data{accno} = qq|<td>|;
  439. for ( @{ $ca->{accno} } ) {
  440. $column_data{accno} .= "<a href=$drilldown&accno=$_>$_</a> ";
  441. }
  442. $column_data{accno} .= qq|&nbsp;</td>|;
  443. if ( $ca->{id} != $sameid ) {
  444. $i++;
  445. $i %= 2;
  446. }
  447. $sameid = $ca->{id};
  448. print qq|
  449. <tr class=listrow$i>
  450. |;
  451. for (@column_index) { print "$column_data{$_}\n" }
  452. print qq|
  453. </tr>
  454. |;
  455. }
  456. if ( $form->{l_subtotal} eq 'Y' ) {
  457. &ca_subtotal;
  458. }
  459. for (@column_index) { $column_data{$_} = "<td>&nbsp;</td>" }
  460. $column_data{debit} =
  461. "<th align=right class=listtotal>"
  462. . $form->format_amount( \%myconfig, $totaldebit, 2, "&nbsp;" ) . "</th>";
  463. $column_data{credit} =
  464. "<th align=right class=listtotal>"
  465. . $form->format_amount( \%myconfig, $totalcredit, 2, "&nbsp;" ) . "</th>";
  466. $column_data{balance} =
  467. "<th align=right class=listtotal>"
  468. . $form->format_amount( \%myconfig, $form->{balance} * $ml, 2, 0 )
  469. . "</th>";
  470. print qq|
  471. <tr class=listtotal>
  472. |;
  473. for (@column_index) { print "$column_data{$_}\n" }
  474. print qq|
  475. </tr>
  476. </table>
  477. </td>
  478. </tr>
  479. <tr>
  480. <td><hr size=3 noshade></td>
  481. </tr>
  482. </table>
  483. </body>
  484. </html>
  485. |;
  486. }
  487. sub ca_subtotal {
  488. for (@column_index) { $column_data{$_} = "<td>&nbsp;</td>" }
  489. $column_data{debit} =
  490. "<th align=right class=listsubtotal>"
  491. . $form->format_amount( \%myconfig, $subtotaldebit, 2, "&nbsp;" )
  492. . "</th>";
  493. $column_data{credit} =
  494. "<th align=right class=listsubtotal>"
  495. . $form->format_amount( \%myconfig, $subtotalcredit, 2, "&nbsp;" )
  496. . "</th>";
  497. $subtotaldebit = 0;
  498. $subtotalcredit = 0;
  499. $sameitem = $ca->{ $form->{sort} };
  500. print qq|
  501. <tr class=listsubtotal>
  502. |;
  503. for (@column_index) { print "$column_data{$_}\n" }
  504. print qq|
  505. </tr>
  506. |;
  507. }