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