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