summaryrefslogtreecommitdiff
path: root/bin/ca.pl
blob: 99a7ef6634154447448c28b154e68a67684a65ff (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. my $selectdepartment;
  168. if ( @{ $form->{all_department} } ) {
  169. $selectdepartment = {name => 'department', options => []};
  170. for ( @{ $form->{all_department} } ) {
  171. push @{$selectdepartment->{options}}, {
  172. value => "$_->{description}--$_->{id}",
  173. text => $_->{description}};
  174. }
  175. }
  176. my $selectmonth;
  177. my $selectyear;
  178. my $interval;
  179. if ( @{ $form->{all_years} } ) {
  180. # accounting years
  181. $selectyear = {name => 'year', options => []};
  182. for ( @{ $form->{all_years} } ) {
  183. push @{$selectyear->{options}}, {value => $_, text => $_};
  184. }
  185. $selectmonth = {name => 'month', options => []};
  186. for ( sort keys %{ $form->{all_month} } ) {
  187. push @{$selectmonth->{options}}, {value => $_,
  188. text => $locale->text($form->{all_month}{$_})};
  189. }
  190. $intervals = [
  191. {type => 'radio', name => 'interval', value => '0',
  192. checked => 'checked', text => $locale->text('Current')},
  193. {type => 'radio', name => 'interval', value => '1',
  194. text => $locale->text('Month')},
  195. {type => 'radio', name => 'interval', value => '3',
  196. text => $locale->text('Quarter')},
  197. {type => 'radio', name => 'interval', value => '12',
  198. text => $locale->text('Year')}];
  199. }
  200. my @includes = ({
  201. type => 'checkbox',
  202. name => 'l_accno',
  203. value => 'Y',
  204. text => $locale->text('AR/AP'),
  205. },{
  206. type => 'checkbox',
  207. name => 'l_subtotal',
  208. value => 'Y',
  209. text => $locale->text('Subtotal'),
  210. });
  211. my $template = LedgerSMB::Template->new_UI(
  212. user => \%myconfig,
  213. locale => $locale,
  214. template => 'ca-list-selector');
  215. $template->render({
  216. form => $form,
  217. includes => \@includes,
  218. selectmonth => $selectmonth,
  219. selectyear => $selectyear,
  220. selectdepartment => $selectdepartment,
  221. intervals => $intervals,
  222. });
  223. }
  224. sub list_transactions {
  225. CA->all_transactions( \%myconfig, \%$form );
  226. $department = $form->escape( $form->{department} );
  227. $projectnumber = $form->escape( $form->{projectnumber} );
  228. $title = $form->escape( $form->{title} );
  229. # construct href
  230. $href =
  231. "$form->{script}?action=list_transactions&department=$department&projectnumber=$projectnumber&title=$title";
  232. for (
  233. qw(path oldsort accno login sessionid fromdate todate accounttype gifi_accno l_heading l_subtotal l_accno)
  234. )
  235. {
  236. $href .= "&$_=$form->{$_}";
  237. }
  238. $drilldown = $href;
  239. $drilldown .= "&sort=$form->{sort}";
  240. $href .= "&direction=$form->{direction}";
  241. $form->sort_order();
  242. $drilldown .= "&direction=$form->{direction}";
  243. $form->{prevreport} = $href unless $form->{prevreport};
  244. $href .= "&prevreport=" . $form->escape( $form->{prevreport} );
  245. $drilldown .= "&prevreport=" . $form->escape( $form->{prevreport} );
  246. # figure out which column comes first
  247. $column_header{transdate} = {
  248. text => $locale->text('Date'),
  249. href => "$href&sort=transdate"};
  250. $column_header{reference} = {
  251. text => $locale->text('Reference'),
  252. href => "$href&sort=reference"};
  253. $column_header{description} = {
  254. text => $locale->text('Description'),
  255. href => "$href&sort=description"};
  256. $column_header{cleared} = $locale->text('R');
  257. $column_header{source} = $locale->text('Source');
  258. $column_header{debit} = $locale->text('Debit');
  259. $column_header{credit} = $locale->text('Credit');
  260. $column_header{balance} = $locale->text('Balance');
  261. $column_header{accno} = $locale->text('AR/AP');
  262. @columns = qw(transdate reference description debit credit);
  263. if ( $form->{link} =~ /_paid/ ) {
  264. @columns =
  265. qw(transdate reference description source cleared debit credit);
  266. }
  267. push @columns, "accno" if $form->{l_accno};
  268. @column_index = $form->sort_columns(@columns);
  269. if ( $form->{accounttype} eq 'gifi' ) {
  270. for (qw(accno description)) { $form->{$_} = $form->{"gifi_$_"} }
  271. }
  272. if ( $form->{accno} ) {
  273. push @column_index, "balance";
  274. }
  275. $form->{title} =
  276. ( $form->{accounttype} eq 'gifi' )
  277. ? $locale->text('GIFI')
  278. : $locale->text('Account');
  279. $form->{title} .= " $form->{accno} - $form->{description}";
  280. my @options;
  281. if ( $form->{department} ) {
  282. ($department) = split /--/, $form->{department};
  283. push @options, $locale->text('Department') . " : $department";
  284. }
  285. if ( $form->{projectnumber} ) {
  286. ($projectnumber) = split /--/, $form->{projectnumber};
  287. push @options, $locale->text('Project Number') . " : $projectnumber";
  288. }
  289. if ( $form->{fromdate} || $form->{todate} ) {
  290. if ( $form->{fromdate} ) {
  291. $fromdate = $locale->date( \%myconfig, $form->{fromdate}, 1 );
  292. }
  293. if ( $form->{todate} ) {
  294. $todate = $locale->date( \%myconfig, $form->{todate}, 1 );
  295. }
  296. $form->{period} = "$fromdate - $todate";
  297. }
  298. else {
  299. $form->{period} =
  300. $locale->date( \%myconfig, $form->current_date( \%myconfig ), 1 );
  301. }
  302. if ($form->{prevreport}) {
  303. push @options, {text => $form->{period}, href=> $form->{prevreport}};
  304. $form->{period} = "<a href=$form->{prevreport}>$form->{period}</a>";
  305. }
  306. # construct callback
  307. $department = $form->escape( $form->{department}, 1 );
  308. $projectnumber = $form->escape( $form->{projectnumber}, 1 );
  309. $title = $form->escape( $form->{title}, 1 );
  310. $form->{prevreport} = $form->escape( $form->{prevreport}, 1 );
  311. $form->{callback} =
  312. "$form->{script}?action=list_transactions&department=$department&projectnumber=$projectnumber&title=$title";
  313. for (
  314. qw(path direction oldsort accno login sessionid fromdate todate accounttype gifi_accno l_heading l_subtotal l_accno prevreport)
  315. )
  316. {
  317. $form->{callback} .= "&$_=$form->{$_}";
  318. }
  319. # add sort to callback
  320. $form->{callback} =
  321. $form->escape( $form->{callback} . "&sort=$form->{sort}" );
  322. my @rows;
  323. if ( @{ $form->{CA} } ) {
  324. $sameitem = $form->{CA}->[0]->{ $form->{sort} };
  325. }
  326. $ml = ( $form->{category} =~ /(A|E)/ ) ? -1 : 1;
  327. $ml *= -1 if $form->{contra};
  328. if ( $form->{accno} && $form->{balance} ) {
  329. my %column_data;
  330. for (@column_index) { $column_data{$_} = " " }
  331. $column_data{balance} =
  332. $form->format_amount( \%myconfig, $form->{balance} * $ml, 2, 0 );
  333. $i++;
  334. $i %= 2;
  335. $column_data{i} = $i;
  336. push @rows, \%column_data;
  337. }
  338. foreach my $ca ( @{ $form->{CA} } ) {
  339. my %column_data;
  340. if ( $form->{l_subtotal} eq 'Y' ) {
  341. if ( $sameitem ne $ca->{ $form->{sort} } ) {
  342. push @rows, &ca_subtotal;
  343. }
  344. }
  345. $column_data{debit} =
  346. $form->format_amount( \%myconfig, $ca->{debit}, 2, " " );
  347. $column_data{credit} =
  348. $form->format_amount( \%myconfig, $ca->{credit}, 2, " " );
  349. $form->{balance} += $ca->{amount};
  350. $column_data{balance} =
  351. $form->format_amount( \%myconfig, $form->{balance} * $ml, 2, 0 );
  352. $subtotaldebit += $ca->{debit};
  353. $subtotalcredit += $ca->{credit};
  354. $totaldebit += $ca->{debit};
  355. $totalcredit += $ca->{credit};
  356. $column_data{transdate} = $ca->{transdate};
  357. $column_data{reference} = {
  358. text => $ca->{reference},
  359. href => "$ca->{module}.pl?path=$form->{path}&action=edit&id=$ca->{id}&login=$form->{login}&sessionid=$form->{sessionid}&callback=$form->{callback}"};
  360. $column_data{description} = $ca->{description};
  361. $column_data{cleared} =
  362. ( $ca->{cleared} ) ? "*" : " ";
  363. $column_data{source} = $ca->{source};
  364. $column_data{accno} = [];
  365. for ( @{ $ca->{accno} } ) {
  366. push @{$column_data{accno}}, {text => $_, href=> "$drilldown&accno=$_>"};
  367. }
  368. if ( $ca->{id} != $sameid ) {
  369. $i++;
  370. $i %= 2;
  371. }
  372. $sameid = $ca->{id};
  373. $column_data{i} = $i;
  374. push @rows, \%column_data;
  375. }
  376. if ( $form->{l_subtotal} eq 'Y' ) {
  377. push @rows, &ca_subtotal;
  378. }
  379. for (@column_index) { $column_data{$_} = " " }
  380. $column_data{debit} =
  381. $form->format_amount( \%myconfig, $totaldebit, 2, " " );
  382. $column_data{credit} =
  383. $form->format_amount( \%myconfig, $totalcredit, 2, " " );
  384. $column_data{balance} =
  385. $form->format_amount( \%myconfig, $form->{balance} * $ml, 2, 0 );
  386. my @buttons;
  387. push @buttons, {
  388. name => 'action',
  389. value => 'csv_list_transactions',
  390. text => $locale->text('CSV Report'),
  391. type => 'submit',
  392. class => 'submit',
  393. };
  394. $form->{callback} = $form->unescape($form->{callback});
  395. my $template = LedgerSMB::Template->new(
  396. user => \%myconfig,
  397. locale => $locale,
  398. path => 'UI',
  399. template => 'ca-list-transactions',
  400. format => ($form->{action} =~ /^csv/)? 'CSV': 'HTML');
  401. $template->render({
  402. form => \%$form,
  403. options => \@options,
  404. buttons => \@buttons,
  405. columns => \@column_index,
  406. heading => \%column_header,
  407. totals => \%column_data,
  408. rows => \@rows,
  409. });
  410. }
  411. sub csv_list_transactions { &list_transactions }
  412. sub ca_subtotal {
  413. my %column_data;
  414. for (@column_index) { $column_data{$_} = " " }
  415. $column_data{debit} =
  416. $form->format_amount( \%myconfig, $subtotaldebit, 2, " " );
  417. $column_data{credit} =
  418. $form->format_amount( \%myconfig, $subtotalcredit, 2, " " );
  419. $subtotaldebit = 0;
  420. $subtotalcredit = 0;
  421. $sameitem = $ca->{ $form->{sort} };
  422. $column_data{is_subtotal} = 1;
  423. return \%column_data;
  424. }