summaryrefslogtreecommitdiff
path: root/bin/rp.pl
blob: 53dad4aa1fd7f3bb5b2cfe9cfbded42af79bd450 (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: Antonio Gallardo <agssa@ibw.com.ni>
  22. # Benjamin Lee <benjaminlee@consultant.com>
  23. #
  24. #
  25. # This program is free software; you can redistribute it and/or modify
  26. # it under the terms of the GNU General Public License as published by
  27. # the Free Software Foundation; either version 2 of the License, or
  28. # (at your option) any later version.
  29. #
  30. # This program is distributed in the hope that it will be useful,
  31. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33. # GNU General Public License for more details.
  34. # You should have received a copy of the GNU General Public License
  35. # along with this program; if not, write to the Free Software
  36. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  37. #======================================================================
  38. #
  39. # module for preparing Income Statement and Balance Sheet
  40. #
  41. #======================================================================
  42. use Error qw(:try);
  43. require "bin/arap.pl";
  44. use LedgerSMB::Template;
  45. use LedgerSMB::PE;
  46. use LedgerSMB::RP;
  47. 1;
  48. # end of main
  49. # this is for our long dates
  50. # $locale->text('January')
  51. # $locale->text('February')
  52. # $locale->text('March')
  53. # $locale->text('April')
  54. # $locale->text('May ')
  55. # $locale->text('June')
  56. # $locale->text('July')
  57. # $locale->text('August')
  58. # $locale->text('September')
  59. # $locale->text('October')
  60. # $locale->text('November')
  61. # $locale->text('December')
  62. # this is for our short month
  63. # $locale->text('Jan')
  64. # $locale->text('Feb')
  65. # $locale->text('Mar')
  66. # $locale->text('Apr')
  67. # $locale->text('May')
  68. # $locale->text('Jun')
  69. # $locale->text('Jul')
  70. # $locale->text('Aug')
  71. # $locale->text('Sep')
  72. # $locale->text('Oct')
  73. # $locale->text('Nov')
  74. # $locale->text('Dec')
  75. # $locale->text('Balance Sheet')
  76. # $locale->text('Income Statement')
  77. # $locale->text('Trial Balance')
  78. # $locale->text('AR Aging')
  79. # $locale->text('AP Aging')
  80. # $locale->text('Tax collected')
  81. # $locale->text('Tax paid')
  82. # $locale->text('Receipts')
  83. # $locale->text('Payments')
  84. # $locale->text('Project Transactions')
  85. # $locale->text('Non-taxable Sales')
  86. # $locale->text('Non-taxable Purchases')
  87. sub report {
  88. my %hiddens;
  89. my %report = (
  90. balance_sheet => { title => 'Balance Sheet' },
  91. income_statement => { title => 'Income Statement' },
  92. trial_balance => { title => 'Trial Balance' },
  93. ar_aging => { title => 'AR Aging', vc => 'customer' },
  94. ap_aging => { title => 'AP Aging', vc => 'vendor' },
  95. tax_collected => { title => 'Tax collected', vc => 'customer' },
  96. tax_paid => { title => 'Tax paid' },
  97. nontaxable_sales => { title => 'Non-taxable Sales', vc => 'customer' },
  98. nontaxable_purchases => { title => 'Non-taxable Purchases' },
  99. receipts => { title => 'Receipts', vc => 'customer' },
  100. payments => { title => 'Payments' },
  101. projects => { title => 'Project Transactions' },
  102. inv_activity => { title => 'Inventory Activity' },
  103. );
  104. $form->{title} = $locale->text( $report{ $form->{report} }->{title} );
  105. my $gifi = 1;
  106. # get departments
  107. $form->all_departments(\%myconfig, undef, $report{$form->{report}}->{vc});
  108. if ( ref $form->{all_department} eq 'ARRAY' ) {
  109. $form->{selectdepartment} = {
  110. name => 'department',
  111. options => [{value => '', text => ''}],
  112. };
  113. push @{$form->{selectdepartment}{options}}, {
  114. value => "$_->{description}--$_->{id}",
  115. text => $_->{description},
  116. } foreach @{$form->{all_department}};
  117. }
  118. if (ref $form->{all_years} eq 'ARRAY') {
  119. # accounting years
  120. $form->{selectaccountingyear} = {
  121. name => 'year',
  122. options => [{text => '', value => ''}],
  123. };
  124. push @{$form->{selectaccountingyear}{options}}, {
  125. text => $_,
  126. value => $_,
  127. } foreach ( @{ $form->{all_years} } );
  128. $form->{selectaccountingmonth} = {
  129. name => 'month',
  130. options => [{text => '', value => ''}],
  131. };
  132. push @{$form->{selectaccountingmonth}{options}}, {
  133. text => $locale->text( $form->{all_month}{$_} ),
  134. value => $_,
  135. } foreach ( sort keys %{ $form->{all_month} } );
  136. }
  137. # get projects
  138. $form->all_projects( \%myconfig );
  139. if (ref $form->{all_project} eq 'ARRAY') {
  140. $form->{selectproject} = {
  141. name => 'projectnumber',
  142. options => [{text => '', value => ''}],
  143. };
  144. push @{$form->{selectproject}{options}}, {
  145. text => $_->{projectnumber},
  146. value => "$_->{projectnumber}--$_->{id}",
  147. } foreach ( @{ $form->{all_project} } );
  148. }
  149. $hiddens{title} = $form->{title};
  150. my $subform;
  151. if ( $form->{report} eq "projects" ) {
  152. $hiddens{nextsub} = 'generate_projects';
  153. $subform = 'generate_projects';
  154. } elsif ( $form->{report} eq "inv_activity" ) {
  155. $gifi = 0;
  156. $hiddens{nextsub} = 'generate_inv_activity';
  157. $subform = 'generate_inv_activity';
  158. } elsif ( $form->{report} eq "income_statement" ) {
  159. $hiddens{nextsub} = 'generate_income_statement';
  160. $subform = 'generate_income_statement';
  161. } elsif ( $form->{report} eq "balance_sheet" ) {
  162. $hiddens{nextsub} = 'generate_balance_sheet';
  163. $subform = 'generate_balance_sheet';
  164. } elsif ( $form->{report} eq "trial_balance" ) {
  165. $hiddens{nextsub} = 'generate_trial_balance';
  166. $subform = 'generate_trial_balance';
  167. } elsif ( $form->{report} =~ /^tax_/ ) {
  168. $gifi = 0;
  169. $form->{db} = ( $form->{report} =~ /_collected/ ) ? "ar" : "ap";
  170. RP->get_taxaccounts( \%myconfig, \%$form );
  171. $hiddens{nextsub} = 'generate_tax_report';
  172. $hiddens{db} = $form->{db};
  173. $hiddens{sort} = 'transdate';
  174. $subform = 'generate_tax_report';
  175. my $checked = "checked";
  176. $form->{taxaccountlist} = [];
  177. foreach $ref ( @{ $form->{taxaccounts} } ) {
  178. push @{$form->{taxaccountlist}}, {
  179. name => 'accno',
  180. type => 'radio',
  181. value => $ref->{accno},
  182. label => $ref->{description},
  183. $checked => $checked,
  184. };
  185. $hiddens{"$ref->{accno}_description"} = $ref->{description};
  186. $hiddens{"$ref->{accno}_rate"} = $ref->{rate};
  187. $checked = undef;
  188. }
  189. if (ref $form->{gifi_taxaccounts} eq 'ARRAY') {
  190. $form->{gifitaxaccountlist} = [];
  191. foreach $ref ( @{ $form->{gifi_taxaccounts} } ) {
  192. push @{$form->{taxaccountlist}}, {
  193. name => 'accno',
  194. type => 'radio',
  195. value => "gifi_$ref->{accno}",
  196. label => $ref->{description},
  197. };
  198. $hiddens{"gifi_$ref->{accno}_description"} = $ref->{description};
  199. $hiddens{"gifi_$ref->{accno}_rate"} = $ref->{rate};
  200. }
  201. }
  202. } elsif ( $form->{report} =~ /^nontaxable_/ ) {
  203. $gifi = 0;
  204. $form->{db} = ( $form->{report} =~ /_sales/ ) ? "ar" : "ap";
  205. $hiddens{nextsub} = 'generate_tax_report';
  206. $hiddens{db} = $form->{db};
  207. $hiddens{sort} = 'transdate';
  208. $hiddens{report} = $form->{report};
  209. $subform = 'generate_tax_report';
  210. } elsif ( ( $form->{report} eq "ar_aging" )
  211. || ( $form->{report} eq "ap_aging" ) ) {
  212. $gifi = 0;
  213. $subform = 'aging';
  214. if ( $form->{report} eq 'ar_aging' ) {
  215. $label = $locale->text('Customer');
  216. $form->{vc} = 'customer';
  217. $form->{uvc} = 'Customer';
  218. }
  219. else {
  220. $label = $locale->text('Vendor');
  221. $form->{vc} = 'vendor';
  222. $form->{uvc} = 'Vendor';
  223. }
  224. # setup vc selection
  225. $form->all_vc( \%myconfig, $form->{vc},
  226. ( $form->{vc} eq 'customer' ) ? "AR" : "AP" );
  227. my $vc;
  228. if (ref $form->{"all_$form->{vc}"} eq 'ARRAY') {
  229. $vc = {name => $form->{vc}, options => [{text => '', value => ''}]};
  230. for ( @{ $form->{"all_$form->{vc}"} } ) {
  231. push @{$vc->{options}}, {
  232. text => "$_->{name}",
  233. value => "$_->{name}--$_->{id}",
  234. };
  235. }
  236. $form->{vci} = {type => 'select', input => $vc};
  237. } else {
  238. $vc = {name => $form->{vc}, size => 35};
  239. $form->{vci} = {type => 'input', input => $vc};
  240. }
  241. $hiddens{type} = 'statement';
  242. $hiddens{format} = 'ps' if $myconfig{printer};
  243. $hiddens{media} = $myconfig{printer};
  244. my $nextsub = "generate_$form->{report}";
  245. $hiddens{nextsub} = $nextsub;
  246. $hiddens{action} = $nextsub;
  247. } elsif ( $form->{report} =~ /(receipts|payments)$/ ) {
  248. $gifi = 0;
  249. $subform = 'payments';
  250. $form->{db} = ( $form->{report} =~ /payments$/ ) ? "ap" : "ar";
  251. RP->paymentaccounts( \%myconfig, \%$form );
  252. my $paymentaccounts = '';
  253. $form->{paymentaccounts} = {
  254. name => 'account',
  255. options => [{text => '', value => ''}],
  256. };
  257. foreach $ref ( @{ $form->{PR} } ) {
  258. $paymentaccounts .= "$ref->{accno} ";
  259. push @{$form->{paymentaccounts}{options}}, {
  260. text => "$ref->{accno}--$ref->{description}",
  261. value => "$ref->{accno}--$ref->{description}",
  262. };
  263. }
  264. chop $paymentaccounts;
  265. $hiddens{nextsub} = 'list_payments';
  266. $hiddens{paymentaccounts} = $paymentaccounts;
  267. $hiddens{db} = $form->{db};
  268. $hiddens{sort} = 'transdate';
  269. }
  270. $form->{login} = 'test';
  271. $hiddens{$_} = $form->{$_} foreach qw(path login sessionid);
  272. ##SC: Temporary commenting
  273. ## if ( $form->{lynx} ) {
  274. ## require "bin/menu.pl";
  275. ## &menubar;
  276. ## }
  277. my @buttons = ({
  278. name => 'action',
  279. value => 'continue',
  280. text => $locale->text('Continue'),
  281. });
  282. my $template = LedgerSMB::Template->new_UI(
  283. user => \%myconfig,
  284. locale => $locale,
  285. template => 'rp-search',
  286. );
  287. $template->render({
  288. user => \%myconfig,
  289. form => $form,
  290. subform => $subform,
  291. hiddens => \%hiddens,
  292. options => \@options,
  293. buttons => \@buttons,
  294. gifi => $gifi,
  295. });
  296. }
  297. sub continue { &{ $form->{nextsub} } }
  298. sub generate_inv_activity {
  299. my %hiddens;
  300. my @options;
  301. my $title = $form->{title};
  302. RP->inventory_activity( \%myconfig, \%$form );
  303. # if ($form->{department}) {
  304. # ($department) = split /--/, $form->{department};
  305. # $options = $locale->text('Department')." : $department<br>";
  306. # $department = $form->escape($form->{department});
  307. # }
  308. ## if ($form->{projectnumber}) {
  309. # ($projectnumber) = split /--/, $form->{projectnumber};
  310. # $options .= $locale->text('Project Number')." : $projectnumber<br>";
  311. # $projectnumber = $form->escape($form->{projectnumber});
  312. # }
  313. # if there are any dates
  314. if ( $form->{fromdate} || $form->{todate} ) {
  315. if ( $form->{fromdate} ) {
  316. $fromdate = $locale->date( \%myconfig, $form->{fromdate}, 1 );
  317. }
  318. if ( $form->{todate} ) {
  319. $todate = $locale->date( \%myconfig, $form->{todate}, 1 );
  320. }
  321. $form->{period} = "$fromdate - $todate";
  322. } else {
  323. $form->{period} =
  324. $locale->date( \%myconfig, $form->current_date( \%myconfig ), 1 );
  325. }
  326. push @options, $form->{period};
  327. my @column_index = qw(partnumber description sold revenue received expense);
  328. my $href =
  329. qq|rp.pl?path=$form->{path}&action=continue&accounttype=$form->{accounttype}&login=$form->{login}&sessionid=$form->{sessionid}&fromdate=$form->{fromdate}&todate=$form->{todate}&l_heading=$form->{l_heading}&l_subtotal=$form->{l_subtotal}&department=$department&projectnumber=$projectnumber&project_id=$form->{project_id}&title=$title&nextsub=$form->{nextsub}|;
  330. my %column_header;
  331. $column_header{partnumber} = {
  332. text => $locale->text('Part Number'),
  333. href => "$href&sort_col=partnumber",
  334. };
  335. $column_header{description} = {
  336. text => $locale->text('Description'),
  337. href => "$href&sort_col=description",
  338. };
  339. $column_header{sold} = {
  340. text => $locale->text('Sold'),
  341. href => "$href&sort_col=sold",
  342. };
  343. $column_header{revenue} = {
  344. text => $locale->text('Revenue'),
  345. href => "$href&sort_col=revenue",
  346. };
  347. $column_header{received} = {
  348. text => $locale->text('Received'),
  349. href => "$href&sort_col=received",
  350. };
  351. $column_header{expense} = {
  352. text => $locale->text('Expense'),
  353. href => "$href&sort_col=expense",
  354. };
  355. if ( $form->{sort_col} eq 'qty' || $form->{sort_col} eq 'revenue' ) {
  356. $form->{sort_type} = 'numeric';
  357. }
  358. my $i = 0;
  359. my $cols = "l_transdate=Y&l_name=Y&l_invnumber=Y&summary=1";
  360. my $dates =
  361. "transdatefrom=$form->{fromdate}&transdateto=$form->{todate}&year=$form->{fromyear}&month=$form->{frommonth}&interval=$form->{interval}";
  362. my $base =
  363. "path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}";
  364. $form->{callback} = "rp.pl?action=continue&$base";
  365. $form->{callback} = $form->escape( $form->{callback} );
  366. $callback = "callback=$form->{callback}";
  367. # sort the whole thing by account numbers and display
  368. my @rows;
  369. foreach my $ref ( @{ $form->{TB} } ) {
  370. my $description = $form->escape( $ref->{description} );
  371. my %column_data;
  372. $i = $i % 2;
  373. $column_data{i} = $i;
  374. $pnumhref =
  375. "ic.pl?action=edit&id=$ref->{id}&$base&callback=$form->{callback}";
  376. $soldhref =
  377. "ar.pl?action=transactions&partsid=$ref->{id}&$base&$cols&$dates&$callback";
  378. $rechref =
  379. "ap.pl?action=transactions&partsid=$ref->{id}&$base&$cols&$dates&callback=$form->{callback}";
  380. $ml = ( $ref->{category} =~ /(A|E)/ ) ? -1 : 1;
  381. $debit = $form->format_amount( \%myconfig, $ref->{debit}, 2, " " );
  382. $credit =
  383. $form->format_amount( \%myconfig, $ref->{credit}, 2, " " );
  384. $begbalance =
  385. $form->format_amount( \%myconfig, $ref->{balance} * $ml, 2, " " );
  386. $endbalance =
  387. $form->format_amount( \%myconfig,
  388. ( $ref->{balance} + $ref->{amount} ) * $ml,
  389. 2, " " );
  390. $column_data{partnumber} = {
  391. text => $ref->{partnumber},
  392. href => $pnumhref,
  393. };
  394. $column_data{sold} = {
  395. text => $ref->{sold},
  396. href => $soldhref,
  397. };
  398. $column_data{received} = {
  399. text => $ref->{received},
  400. href => $rechref,
  401. };
  402. push @rows, \%column_data;
  403. ++$i;
  404. }
  405. my $template = LedgerSMB::Template->new_UI(
  406. user => \%myconfig,
  407. locale => $locale,
  408. template => 'form-dynatable',
  409. );
  410. $template->render({
  411. form => $form,
  412. hiddens => \%hiddens,
  413. options => \@options,
  414. columns => \@column_index,
  415. heading => \%column_header,
  416. rows => \@rows,
  417. row_alignment => {
  418. sold => 'right',
  419. revenue => 'right',
  420. received => 'right',
  421. expense => 'right',
  422. },
  423. });
  424. }
  425. sub generate_income_statement {
  426. RP->income_statement( \%myconfig, \%$form );
  427. ( $form->{department} ) = split /--/, $form->{department};
  428. ( $form->{projectnumber} ) = split /--/, $form->{projectnumber};
  429. $form->{period} =
  430. $locale->date( \%myconfig, $form->current_date( \%myconfig ), 1 );
  431. $form->{todate} = $form->current_date( \%myconfig ) unless $form->{todate};
  432. # if there are any dates construct a where
  433. unless ( $form->{todate} ) {
  434. $form->{todate} = $form->current_date( \%myconfig );
  435. }
  436. $longtodate = $locale->date( \%myconfig, $form->{todate}, 1 );
  437. $shorttodate = $locale->date( \%myconfig, $form->{todate}, 0 );
  438. $longfromdate = $locale->date( \%myconfig, $form->{fromdate}, 1 );
  439. $shortfromdate = $locale->date( \%myconfig, $form->{fromdate}, 0 );
  440. $form->{this_period_from} = $shortfromdate;
  441. $form->{this_period_to} = $shorttodate;
  442. # example output: 2006-01-01 To 2007-01-01
  443. $form->{period} = $locale->text('[_1] To [_2]', $longfromdate, $longtodate);
  444. if ( $form->{comparefromdate} || $form->{comparetodate} ) {
  445. $longcomparefromdate =
  446. $locale->date( \%myconfig, $form->{comparefromdate}, 1 );
  447. $shortcomparefromdate =
  448. $locale->date( \%myconfig, $form->{comparefromdate}, 0 );
  449. $longcomparetodate =
  450. $locale->date( \%myconfig, $form->{comparetodate}, 1 );
  451. $shortcomparetodate =
  452. $locale->date( \%myconfig, $form->{comparetodate}, 0 );
  453. $form->{last_period_from} = $shortcomparefromdate;
  454. $form->{last_period_to} = $shortcomparetodate;
  455. $form->{compare_period} = $locale->text('[_1] To [_2]',
  456. $longcomparefromdate, $longcomparetodate);
  457. }
  458. # setup variables for the form
  459. my @vars = qw(company address businessnumber);
  460. for (@vars) { $form->{$_} = $myconfig{$_} }
  461. ##SC: The escaped form will be converted in-template
  462. $form->{address} =~ s/\\n/<br>/g;
  463. my $template = LedgerSMB::Template->new(
  464. user => \%myconfig,
  465. locale => $locale,
  466. template => 'income_statement',
  467. format => 'HTML',
  468. #no_escape => '1'
  469. );
  470. try {
  471. $template->render($form);
  472. $template->output(%{$form});
  473. }
  474. catch Error::Simple with {
  475. my $E = shift;
  476. $form->error( $E->stacktrace );
  477. };
  478. }
  479. sub generate_balance_sheet {
  480. RP->balance_sheet( \%myconfig, \%$form );
  481. $form->{asofdate} = $form->current_date( \%myconfig )
  482. unless $form->{asofdate};
  483. $form->{period} =
  484. $locale->date( \%myconfig, $form->current_date( \%myconfig ), 1 );
  485. ( $form->{department} ) = split /--/, $form->{department};
  486. # define Current Earnings account
  487. push(
  488. @{ $form->{equity_account} }, {
  489. current_earnings => 1,
  490. text => $locale->text('Current Earnings')
  491. },
  492. );
  493. $form->{this_period} = $locale->date( \%myconfig, $form->{asofdate}, 0 );
  494. $form->{last_period} =
  495. $locale->date( \%myconfig, $form->{compareasofdate}, 0 );
  496. # setup company variables for the form
  497. for (qw(company address businessnumber nativecurr login)) {
  498. $form->{$_} = $myconfig{$_};
  499. }
  500. ##SC: The escaped form will be converted in-template
  501. $form->{address} =~ s/\\n/<br>/g;
  502. $form->{templates} = $myconfig{templates};
  503. my $template = LedgerSMB::Template->new(
  504. user => \%myconfig,
  505. locale => $locale,
  506. template => 'balance_sheet',
  507. format => $form->{format}? uc $form->{format}: 'HTML',
  508. no_auto_output => 1,
  509. );
  510. try {
  511. $template->render($form);
  512. $template->output(%{$form});
  513. }
  514. catch Error::Simple with {
  515. my $E = shift;
  516. $form->error( $E->stacktrace );
  517. };
  518. }
  519. sub generate_projects {
  520. $form->{nextsub} = "generate_projects";
  521. $form->{title} = $locale->text('Project Transactions');
  522. RP->trial_balance( \%myconfig, \%$form );
  523. &list_accounts;
  524. }
  525. sub csv_generate_projects { &generate_projects }
  526. sub xls_generate_projects { &generate_projects }
  527. sub ods_generate_projects { &generate_projects }
  528. # Antonio Gallardo
  529. #
  530. # D.S. Feb 16, 2001
  531. # included links to display transactions for period entered
  532. # added headers and subtotals
  533. #
  534. sub generate_trial_balance {
  535. # get for each account initial balance, debits and credits
  536. RP->trial_balance( \%myconfig, \%$form );
  537. $form->{nextsub} = "generate_trial_balance";
  538. $form->{title} = $locale->text('Trial Balance');
  539. $form->{callback} = "$form->{script}?action=generate_trial_balance";
  540. for (
  541. qw(login path sessionid nextsub fromdate todate month year interval l_heading l_subtotal all_accounts accounttype title)
  542. )
  543. {
  544. $form->{callback} .= "&$_=$form->{$_}";
  545. }
  546. $form->{callback} = $form->escape( $form->{callback} );
  547. &list_accounts;
  548. }
  549. sub csv_generate_trial_balance { &generate_trial_balance }
  550. sub xls_generate_trial_balance { &generate_trial_balance }
  551. sub ods_generate_trial_balance { &generate_trial_balance }
  552. sub list_accounts {
  553. $title = $form->escape( $form->{title} );
  554. my %hiddens = (
  555. path => $form->{path},
  556. sessionid => $form->{sessionid},
  557. login => $form->{login},
  558. accounttype => $form->{accounttype},
  559. fromdate => $form->{fromdate},
  560. todate => $form->{todate},
  561. l_heading => $form->{l_heading},
  562. l_subtotal => $form->{l_subtotal},
  563. all_accounts => $form->{all_accounts},
  564. department => $form->{department},
  565. projectnumber => $form->{projectnumber},
  566. project_id => $form->{project_id},
  567. );
  568. my @options;
  569. if ( $form->{department} ) {
  570. ($department) = split /--/, $form->{department};
  571. push @options, $locale->text('Department: [_1]', $department);
  572. $department = $form->escape( $form->{department} );
  573. }
  574. if ( $form->{projectnumber} ) {
  575. ($projectnumber) = split /--/, $form->{projectnumber};
  576. push @options, $locale->text('Project Number: [_1]', $projectnumber);
  577. $projectnumber = $form->escape( $form->{projectnumber} );
  578. }
  579. # if there are any dates
  580. if ( $form->{fromdate} || $form->{todate} ) {
  581. if ( $form->{fromdate} ) {
  582. $fromdate = $locale->date( \%myconfig, $form->{fromdate}, 1 );
  583. }
  584. if ( $form->{todate} ) {
  585. $todate = $locale->date( \%myconfig, $form->{todate}, 1 );
  586. }
  587. $form->{period} = "$fromdate - $todate";
  588. } else {
  589. $form->{period} =
  590. $locale->date( \%myconfig, $form->current_date( \%myconfig ), 1 );
  591. }
  592. push @options, $form->{period};
  593. my @column_index = qw(accno description begbalance debit credit endbalance);
  594. my %column_header;
  595. $column_header{accno} = $locale->text('Account');
  596. $column_header{description} = $locale->text('Description');
  597. $column_header{debit} = $locale->text('Debit');
  598. $column_header{credit} = $locale->text('Credit');
  599. $column_header{begbalance} = $locale->text('Balance');
  600. $column_header{endbalance} = $locale->text('Balance');
  601. if ( $form->{accounttype} eq 'gifi' ) {
  602. $column_header{accno} = $locale->text('GIFI');
  603. }
  604. # sort the whole thing by account numbers and display
  605. my @rows;
  606. foreach $ref ( sort { $a->{accno} cmp $b->{accno} } @{ $form->{TB} } ) {
  607. my %column_data;
  608. my $description = $form->escape( $ref->{description} );
  609. my $href =
  610. qq|ca.pl?path=$form->{path}&action=list_transactions&accounttype=$form->{accounttype}&login=$form->{login}&sessionid=$form->{sessionid}&fromdate=$form->{fromdate}&todate=$form->{todate}&sort=transdate&l_heading=$form->{l_heading}&l_subtotal=$form->{l_subtotal}&department=$department&projectnumber=$projectnumber&project_id=$form->{project_id}&title=$title&nextsub=$form->{nextsub}&prevreport=$form->{callback}|;
  611. if ( $form->{accounttype} eq 'gifi' ) {
  612. $href .= "&gifi_accno=$ref->{accno}&gifi_description=$description";
  613. $na = $locale->text('N/A');
  614. if ( !$ref->{accno} ) {
  615. for (qw(accno description)) { $ref->{$_} = $na }
  616. }
  617. }
  618. else {
  619. $href .= "&accno=$ref->{accno}&description=$description";
  620. }
  621. $ml = ( $ref->{category} =~ /(A|E)/ ) ? -1 : 1;
  622. $ml *= -1 if $ref->{contra};
  623. $debit = $form->format_amount( \%myconfig, $ref->{debit}, 2, " " );
  624. $credit =
  625. $form->format_amount( \%myconfig, $ref->{credit}, 2, " " );
  626. $begbalance =
  627. $form->format_amount( \%myconfig, $ref->{balance} * $ml, 2,
  628. " " );
  629. $endbalance =
  630. $form->format_amount( \%myconfig,
  631. ( $ref->{balance} + $ref->{amount} ) * $ml, 2, " " );
  632. if ( $ref->{charttype} eq "H" && $subtotal && $form->{l_subtotal} ) {
  633. if ($subtotal) {
  634. for (qw(accno begbalance endbalance)) {
  635. $column_data{$_} = " ";
  636. }
  637. $subtotalbegbalance =
  638. $form->format_amount( \%myconfig, $subtotalbegbalance, 2,
  639. " " );
  640. $subtotalendbalance =
  641. $form->format_amount( \%myconfig, $subtotalendbalance, 2,
  642. " " );
  643. $subtotaldebit =
  644. $form->format_amount( \%myconfig, $subtotaldebit, 2,
  645. " " );
  646. $subtotalcredit =
  647. $form->format_amount( \%myconfig, $subtotalcredit, 2,
  648. " " );
  649. $column_data{description} = $subtotaldescription;
  650. $column_data{begbalance} = $subtotalbegbalance;
  651. $column_data{endbalance} = $subtotalendbalance;
  652. $column_data{debit} = $subtotaldebit;
  653. $column_data{credit} = $subtotalcredit;
  654. $column_data{class} = 'subtotal';
  655. push @rows, \%column_data;
  656. }
  657. }
  658. if ( $ref->{charttype} eq "H" ) {
  659. $subtotal = 1;
  660. $subtotaldescription = $ref->{description};
  661. $subtotaldebit = $ref->{debit};
  662. $subtotalcredit = $ref->{credit};
  663. $subtotalbegbalance = 0;
  664. $subtotalendbalance = 0;
  665. if ( $form->{l_heading} ) {
  666. if ( !$form->{all_accounts} and
  667. ( $subtotaldebit + $subtotalcredit ) == 0 ) {
  668. $subtotal = 0;
  669. next;
  670. }
  671. } else {
  672. $subtotal = 0;
  673. if ( $form->{all_accounts} || ( $form->{l_subtotal} &&
  674. ( ( $subtotaldebit + $subtotalcredit ) != 0 ) )) {
  675. $subtotal = 1;
  676. }
  677. next;
  678. }
  679. for (qw(accno debit credit begbalance endbalance)) {
  680. $column_data{$_} = " ";
  681. }
  682. $column_data{description} = $ref->{description};
  683. $column_data{class} = 'heading';
  684. }
  685. if ( $ref->{charttype} eq "A" ) {
  686. $column_data{accno} = {text => $ref->{accno}, href => $href};
  687. $column_data{description} = $ref->{description};
  688. $column_data{debit} = $debit;
  689. $column_data{credit} = $credit;
  690. $column_data{begbalance} = $begbalance;
  691. $column_data{endbalance} = $endbalance;
  692. $totaldebit += $ref->{debit};
  693. $totalcredit += $ref->{credit};
  694. $cml = ( $ref->{contra} ) ? -1 : 1;
  695. $subtotalbegbalance += $ref->{balance} * $ml * $cml;
  696. $subtotalendbalance +=
  697. ( $ref->{balance} + $ref->{amount} ) * $ml * $cml;
  698. }
  699. if ( $ref->{charttype} eq "H" ) {
  700. $column_data{class} = 'heading';
  701. }
  702. if ( $ref->{charttype} eq "A" ) {
  703. $i++;
  704. $i %= 2;
  705. $column_data{i} = $i;
  706. }
  707. push @rows, \%column_data;
  708. }
  709. # print last subtotal
  710. if ( $subtotal && $form->{l_subtotal} ) {
  711. my %column_data;
  712. for (qw(accno begbalance endbalance)) {
  713. $column_data{$_} = " ";
  714. }
  715. $subtotalbegbalance =
  716. $form->format_amount( \%myconfig, $subtotalbegbalance, 2, " " );
  717. $subtotalendbalance =
  718. $form->format_amount( \%myconfig, $subtotalendbalance, 2, " " );
  719. $subtotaldebit =
  720. $form->format_amount( \%myconfig, $subtotaldebit, 2, " " );
  721. $subtotalcredit =
  722. $form->format_amount( \%myconfig, $subtotalcredit, 2, " " );
  723. $column_data{description} = $subtotaldescription;
  724. $column_data{begbalance} = $subtotalbegbalance;
  725. $column_data{endbalance} = $subtotalendbalance;
  726. $column_data{debit} = $subtotaldebit;
  727. $column_data{credit} = $subtotalcredit;
  728. $column_data{class} = 'subtotal';
  729. push @rows, \%column_data;
  730. }
  731. my %column_data;
  732. $totaldebit = $form->format_amount( \%myconfig, $totaldebit, 2, " " );
  733. $totalcredit =
  734. $form->format_amount( \%myconfig, $totalcredit, 2, " " );
  735. for (qw(accno description begbalance endbalance)) {
  736. $column_data{$_} = "";
  737. }
  738. $column_data{debit} = $totaldebit;
  739. $column_data{credit} = $totalcredit;
  740. my @buttons;
  741. for my $type (qw(CSV XLS ODS)) {
  742. push @buttons, {
  743. name => 'action',
  744. value => lc "${type}_$form->{nextsub}",
  745. text => $locale->text("$type Report"),
  746. type => 'submit',
  747. class => 'submit',
  748. };
  749. }
  750. my $format;
  751. if ($form->{action} eq 'continue') {
  752. $format = 'HTML';
  753. } else {
  754. $format = uc substr $form->{action}, 0, 3;
  755. push @column_index, 'class';
  756. $column_header{class} = 'rowtype';
  757. }
  758. my $template = LedgerSMB::Template->new(
  759. user => \%myconfig,
  760. locale => $locale,
  761. template => 'form-dynatable',
  762. path => 'UI',
  763. format => $format,
  764. );
  765. $template->render({
  766. form => $form,
  767. hiddens => \%hiddens,
  768. buttons => \@buttons,
  769. options => \@options,
  770. columns => \@column_index,
  771. heading => \%column_header,
  772. rows => \@rows,
  773. totals => \%column_data,
  774. row_alignment => {
  775. 'credit' => 'right',
  776. 'debit' => 'right',
  777. 'begbalance' => 'right',
  778. 'endbalance' => 'right'
  779. },
  780. });
  781. }
  782. sub generate_ar_aging {
  783. # split customer
  784. ( $form->{customer} ) = split( /--/, $form->{customer} );
  785. $customer = $form->escape( $form->{customer}, 1 );
  786. $title = $form->escape( $form->{title}, 1 );
  787. $media = $form->escape( $form->{media}, 1 );
  788. $form->{ct} = "customer";
  789. $form->{arap} = "ar";
  790. RP->aging( \%myconfig, \%$form );
  791. $form->{callback} =
  792. qq|$form->{script}?path=$form->{path}&action=generate_ar_aging&login=$form->{login}&sessionid=$form->{sessionid}&todate=$form->{todate}&customer=$customer&title=$title&type=$form->{type}&format=$form->{format}&media=$media&summary=$form->{summary}|;
  793. &aging;
  794. }
  795. sub csv_generate_ar_aging { &generate_ar_aging }
  796. sub generate_ap_aging {
  797. # split vendor
  798. ( $form->{vendor} ) = split( /--/, $form->{vendor} );
  799. $vendor = $form->escape( $form->{vendor}, 1 );
  800. $title = $form->escape( $form->{title}, 1 );
  801. $media = $form->escape( $form->{media}, 1 );
  802. $form->{ct} = "vendor";
  803. $form->{arap} = "ap";
  804. RP->aging( \%myconfig, \%$form );
  805. $form->{callback} =
  806. qq|$form->{script}?path=$form->{path}&action=generate_ap_aging&login=$form->{login}&sessionid=$form->{sessionid}&todate=$form->{todate}&vendor=$vendor&title=$title&type=$form->{type}&format=$form->{format}&media=$media&summary=$form->{summary}|;
  807. &aging;
  808. }
  809. sub aging {
  810. my %hiddens;
  811. my @buttons;
  812. my @options;
  813. my %column_header;
  814. my @column_index;
  815. my %row_alignment;
  816. $column_header{statement} = ' ';
  817. $column_header{ct} = $locale->text( ucfirst $form->{ct} );
  818. $column_header{language} = $locale->text('Language');
  819. $column_header{invnumber} = $locale->text('Invoice');
  820. $column_header{ordnumber} = $locale->text('Order');
  821. $column_header{transdate} = $locale->text('Date');
  822. $column_header{duedate} = $locale->text('Due Date');
  823. $column_header{c0} = $locale->text('Current');
  824. $column_header{c30} = '30';
  825. $column_header{c60} = '60';
  826. $column_header{c90} = '90';
  827. $column_header{total} = $locale->text('Total');
  828. @column_index = qw(statement ct);
  829. if ( @{ $form->{all_language} } && $form->{arap} eq 'ar' ) {
  830. push @column_index, "language";
  831. $form->{language_options} = [{text => ' ', value => ''}];
  832. for ( @{ $form->{all_language} } ) {
  833. push @{$form->{language_options}},
  834. {text => $_->{description}, value => $_->{code}};
  835. }
  836. }
  837. my @c = ();
  838. for (qw(c0 c30 c60 c90)) {
  839. if ( $form->{$_} ) {
  840. push @c, $_;
  841. $form->{callback} .= "&$_=$form->{$_}";
  842. }
  843. }
  844. if ( !$form->{summary} ) {
  845. push @column_index, qw(invnumber ordnumber transdate duedate);
  846. }
  847. push @column_index, @c;
  848. push @column_index, "total";
  849. if ( $form->{overdue} ) {
  850. push @options, $locale->text('Aged Overdue');
  851. $form->{callback} .= "&overdue=$form->{overdue}";
  852. } else {
  853. push @options, $locale->text('Aged');
  854. }
  855. if ( $form->{department} ) {
  856. ($department) = split /--/, $form->{department};
  857. push @options, $locale->text('Department: [_1]', $department);
  858. $department = $form->escape( $form->{department}, 1 );
  859. $form->{callback} .= "&department=$department";
  860. }
  861. if ( $form->{arap} eq 'ar' ) {
  862. if ( $form->{customer} ) {
  863. push @options, $form->{customer};
  864. }
  865. }
  866. if ( $form->{arap} eq 'ap' ) {
  867. shift @column_index;
  868. if ( $form->{vendor} ) {
  869. push @options, $form->{vendor};
  870. }
  871. }
  872. $todate = $locale->date( \%myconfig, $form->{todate}, 1 );
  873. push @options, $locale->text('for Period To [_1]', $todate);
  874. $ctid = 0;
  875. $i = 0;
  876. $k = 0;
  877. $l = $#{ $form->{AG} };
  878. my @currencies;
  879. foreach my $ref ( @{ $form->{AG} } ) {
  880. if ( $curr ne $ref->{curr} ) {
  881. my %column_data;
  882. $ctid = 0;
  883. for (@column_index) { $column_data{$_} = ' ' }
  884. if ($curr) {
  885. $c0total = $form->format_amount(\%myconfig, $c0total, 2, ' ');
  886. $c30total = $form->format_amount(\%myconfig, $c30total, 2, ' ');
  887. $c60total = $form->format_amount(\%myconfig, $c60total, 2, ' ');
  888. $c90total = $form->format_amount(\%myconfig, $c90total, 2, ' ');
  889. $total = $form->format_amount(\%myconfig, $total, 2, ' ' );
  890. for (qw(ct statement language)) {
  891. $column_data{$_} = ' ';
  892. }
  893. $column_data{c0} = $c0total;
  894. $column_data{c30} = $c30total;
  895. $column_data{c60} = $c60total;
  896. $column_data{c90} = $c90total;
  897. $column_data{total} = $total;
  898. push @{$currencies[0]{totals}}, \%column_data;
  899. $c0subtotal = 0;
  900. $c30subtotal = 0;
  901. $c60subtotal = 0;
  902. $c90subtotal = 0;
  903. $subtotal = 0;
  904. $c0total = 0;
  905. $c30total = 0;
  906. $c60total = 0;
  907. $c90total = 0;
  908. $total = 0;
  909. }
  910. unshift @currencies, {};
  911. $curr = $ref->{curr};
  912. $currencies[0]{curr} = $curr;
  913. }
  914. $k++;
  915. my %column_data;
  916. if ( $ctid != $ref->{ctid} or $form->{summary}) {
  917. $i++;
  918. $column_data{ct} = $ref->{name};
  919. $column_data{language} = {
  920. name => "language_code_$i",
  921. options => $form->{language_options},
  922. default_value => $ref->{language_code},
  923. } if $form->{language_options};
  924. $column_data{statement} = {
  925. name => "statement_$i",
  926. type => 'checkbox',
  927. value => '1',
  928. };
  929. $column_data{statement}{checked} = 'checked' if $ref->{checked};
  930. $hiddens{"$form->{ct}_id_$i"} = $ref->{ctid};
  931. $hiddens{"curr_$i"} = $ref->{curr};
  932. }
  933. $ctid = $ref->{ctid};
  934. for (qw(c0 c30 c60 c90)) {
  935. $ref->{$_} =
  936. $form->round_amount( $ref->{$_} / $ref->{exchangerate}, 2 );
  937. }
  938. $c0subtotal += $ref->{c0};
  939. $c30subtotal += $ref->{c30};
  940. $c60subtotal += $ref->{c60};
  941. $c90subtotal += $ref->{c90};
  942. $c0total += $ref->{c0};
  943. $c30total += $ref->{c30};
  944. $c60total += $ref->{c60};
  945. $c90total += $ref->{c90};
  946. $ref->{total} =
  947. ( $ref->{c0} + $ref->{c30} + $ref->{c60} + $ref->{c90} );
  948. $subtotal += $ref->{total};
  949. $total += $ref->{total};
  950. $ref->{c0} =
  951. $form->format_amount( \%myconfig, $ref->{c0}, 2, ' ' );
  952. $ref->{c30} =
  953. $form->format_amount( \%myconfig, $ref->{c30}, 2, ' ' );
  954. $ref->{c60} =
  955. $form->format_amount( \%myconfig, $ref->{c60}, 2, ' ' );
  956. $ref->{c90} =
  957. $form->format_amount( \%myconfig, $ref->{c90}, 2, ' ' );
  958. $ref->{total} =
  959. $form->format_amount( \%myconfig, $ref->{total}, 2, ' ' );
  960. $href =
  961. qq|$ref->{module}.pl?path=$form->{path}&action=edit&id=$ref->{id}&login=$form->{login}&sessionid=$form->{sessionid}&callback=|
  962. . $form->escape( $form->{callback} );
  963. $column_data{invnumber} = {text => $ref->{invnumber}, href => $href};
  964. for (qw(c0 c30 c60 c90 total ordnumber transdate duedate)) {
  965. $column_data{$_} = $ref->{$_};
  966. }
  967. if ( !$form->{summary} ) {
  968. $j++;
  969. $j %= 2;
  970. $column_data{i} = $j;
  971. my $rowref = {};
  972. $rowref->{$_} = $column_data{$_} for keys %column_data;
  973. push @{$currencies[0]{rows}}, $rowref;
  974. for (qw(ct statement language)) {
  975. $column_data{$_} = ' ';
  976. }
  977. }
  978. $column_data{ct} = $ref->{name};
  979. # prepare subtotal
  980. $nextid = ( $k <= $l ) ? $form->{AG}->[$k]->{ctid} : 0;
  981. if ( $ctid != $nextid ) {
  982. $c0subtotal =
  983. $form->format_amount( \%myconfig, $c0subtotal, 2, ' ' );
  984. $c30subtotal =
  985. $form->format_amount( \%myconfig, $c30subtotal, 2, ' ' );
  986. $c60subtotal =
  987. $form->format_amount( \%myconfig, $c60subtotal, 2, ' ' );
  988. $c90subtotal =
  989. $form->format_amount( \%myconfig, $c90subtotal, 2, ' ' );
  990. $subtotal =
  991. $form->format_amount( \%myconfig, $subtotal, 2, ' ' );
  992. if ( $form->{summary} ) {
  993. $column_data{c0} = $c0subtotal;
  994. $column_data{c30} = $c30subtotal;
  995. $column_data{c60} = $c60subtotal;
  996. $column_data{c90} = $c90subtotal;
  997. $column_data{total} = $subtotal;
  998. $j++;
  999. $j %= 2;
  1000. $column_data{i} = $j;
  1001. push @{$currencies[0]{rows}}, \%column_data;
  1002. } else {
  1003. for (@column_index) { $column_data{$_} = ' ' }
  1004. $column_data{c0} = $c0subtotal;
  1005. $column_data{c30} = $c30subtotal;
  1006. $column_data{c60} = $c60subtotal;
  1007. $column_data{c90} = $c90subtotal;
  1008. $column_data{total} = $subtotal;
  1009. $column_data{class} = 'subtotal';
  1010. push @{$currencies[0]{rows}}, \%column_data;
  1011. }
  1012. $c0subtotal = 0;
  1013. $c30subtotal = 0;
  1014. $c60subtotal = 0;
  1015. $c90subtotal = 0;
  1016. $subtotal = 0;
  1017. }
  1018. }
  1019. my %column_data;
  1020. for (@column_index) { $column_data{$_} = ' ' }
  1021. $c0total = $form->format_amount( \%myconfig, $c0total, 2, ' ' );
  1022. $c30total = $form->format_amount( \%myconfig, $c30total, 2, ' ' );
  1023. $c60total = $form->format_amount( \%myconfig, $c60total, 2, ' ' );
  1024. $c90total = $form->format_amount( \%myconfig, $c90total, 2, ' ' );
  1025. $total = $form->format_amount( \%myconfig, $total, 2, ' ' );
  1026. $column_data{c0} = $c0total;
  1027. $column_data{c30} = $c30total;
  1028. $column_data{c60} = $c60total;
  1029. $column_data{c90} = $c90total;
  1030. $column_data{total} = $total;
  1031. $currencies[0]{total} = \%column_data;
  1032. $row_alignment{c0} = 'right';
  1033. $row_alignment{c30} = 'right';
  1034. $row_alignment{c60} = 'right';
  1035. $row_alignment{c90} = 'right';
  1036. $row_alignment{total} = 'right';
  1037. $hiddens{rowcount} = $i;
  1038. &print_options if ( $form->{arap} eq 'ar' );
  1039. my @buttons;
  1040. if ( $form->{arap} eq 'ar' ) {
  1041. $hiddens{$_} = $form->{$_} foreach qw(todate title summary overdue c0 c30 c60 c90 callback arap ct department path login sessionid);
  1042. $hiddens{$form->{ct}} = $form->{$form->{ct}};
  1043. # type=submit $locale->text('Select all')
  1044. # type=submit $locale->text('Print')
  1045. # type=submit $locale->text('E-mail')
  1046. my %button = (
  1047. 'select_all' =>
  1048. { ndx => 1, key => 'A', value => $locale->text('Select all') },
  1049. 'print' =>
  1050. { ndx => 2, key => 'P', value => $locale->text('Print') },
  1051. 'e_mail' =>
  1052. { ndx => 5, key => 'E', value => $locale->text('E-mail') },
  1053. );
  1054. for ( sort { $button{$a}->{ndx} <=> $button{$b}->{ndx} } keys %button )
  1055. {
  1056. push @buttons, {
  1057. accesskey => $button{$_}->{key},
  1058. text => $button{$_}->{value},
  1059. title => "$button{$_}->{value} [Alt-$button{$_}->{key}]",
  1060. value => $_,
  1061. name => 'action',
  1062. };
  1063. }
  1064. }
  1065. ##SC: Temporary commenting
  1066. ## if ( $form->{lynx} ) {
  1067. ## require "bin/menu.pl";
  1068. ## &menubar;
  1069. ## }
  1070. for my $type (qw(CSV XLS ODS)) {
  1071. push @buttons, {
  1072. name => 'action',
  1073. value => lc "${type}_$form->{nextsub}",
  1074. text => $locale->text("$type Report"),
  1075. type => 'submit',
  1076. class => 'submit',
  1077. };
  1078. }
  1079. my $format;
  1080. if ($form->{action} =~ /^(continue|generate_)/) {
  1081. $format = 'HTML';
  1082. } else {
  1083. $format = uc substr $form->{action}, 0, 3;
  1084. push @column_index, 'class';
  1085. @column_index = grep {!/^(language|statement)$/} @column_index;
  1086. $column_header{class} = 'rowtype';
  1087. }
  1088. my $template = LedgerSMB::Template->new(
  1089. user => \%myconfig,
  1090. locale => $locale,
  1091. template => 'rp-aging',
  1092. path => 'UI',
  1093. format => $format,
  1094. );
  1095. $template->render({
  1096. form => $form,
  1097. hiddens => \%hiddens,
  1098. buttons => \@buttons,
  1099. options => \@options,
  1100. columns => \@column_index,
  1101. heading => \%column_header,
  1102. currencies => [reverse @currencies],
  1103. row_alignment => {
  1104. 'credit' => 'right',
  1105. 'debit' => 'right',
  1106. 'begbalance' => 'right',
  1107. 'endbalance' => 'right'
  1108. },
  1109. });
  1110. }
  1111. sub select_all {
  1112. RP->aging( \%myconfig, \%$form );
  1113. for ( @{ $form->{AG} } ) { $_->{checked} = "checked" }
  1114. &aging;
  1115. }
  1116. sub print_options {
  1117. $form->{sendmode} = "attachment";
  1118. $form->{copies} = 1 unless $form->{copies};
  1119. $form->{print}{format} = {name => 'format', default_values => $form->{format}};
  1120. $form->{print}{template} = {name => 'type', default_values => $form->{type}};
  1121. my @formats = ();
  1122. my @media = ();
  1123. my @templates = ();
  1124. push @formats, {text => 'HTML', value => 'html'};
  1125. push @templates, {text => $locale->text('Statement'), value => 'statement'};
  1126. if ( $form->{media} eq 'email' ) {
  1127. $form->{print}{medium} = {name => 'sendmode', default_values => $form->{sendmode}};
  1128. push @media, {text => $locale->text('Attachment'), value => 'attachment'};
  1129. push @media, {text => $locale->text('In-line'), value => 'inline'};
  1130. if ($form->{SM}{attachment}) {
  1131. $form->{print}{medium}{default_values} = $form->{SM}{attachment};
  1132. } elsif ($form->{SM}{inline}) {
  1133. $form->{print}{medium}{default_values} = $form->{SM}{inline};
  1134. }
  1135. } else {
  1136. $form->{print}{medium} = {name => 'media'};
  1137. push @media, {text => $locale->text('Screen'), value => 'screen'};
  1138. if ( %{LedgerSMB::Sysconfig::printer}
  1139. && ${LedgerSMB::Sysconfig::latex} )
  1140. {
  1141. for ( sort keys %{LedgerSMB::Sysconfig::printer} ) {
  1142. push @media, {text => $_, value => $_};
  1143. }
  1144. }
  1145. }
  1146. if ( ${LedgerSMB::Sysconfig::latex} ) {
  1147. push @formats, {text => $locale->text('PDF'), value => 'pdf'};
  1148. push @formats, {text => $locale->text('Postscript'), value => 'ps'};
  1149. }
  1150. if ( %{LedgerSMB::Sysconfig::printer}
  1151. && ${LedgerSMB::Sysconfig::latex}
  1152. && $form->{media} ne 'email' )
  1153. {
  1154. $form->{print}{copies} = {
  1155. label => $locale->text('Copies'),
  1156. name => 'copies',
  1157. value => $form->{copies},
  1158. size => 2,
  1159. };
  1160. }
  1161. $form->{print}{template}{options} = \@templates;
  1162. $form->{print}{format}{options} = \@formats;
  1163. $form->{print}{medium}{options} = \@media;
  1164. }
  1165. sub e_mail {
  1166. my %hiddens;
  1167. # get name and email addresses
  1168. for $i ( 1 .. $form->{rowcount} ) {
  1169. if ( $form->{"statement_$i"} ) {
  1170. $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
  1171. $form->{"statement_1"} = 1;
  1172. $form->{"language_code_1"} = $form->{"language_code_$i"};
  1173. $form->{"curr_1"} = $form->{"curr_$i"};
  1174. RP->get_customer( \%myconfig, \%$form );
  1175. $selected = 1;
  1176. last;
  1177. }
  1178. }
  1179. $form->error( $locale->text('Nothing selected!') ) unless $selected;
  1180. $form->{media} = "email";
  1181. &print_options;
  1182. for (qw(email cc bcc subject message type sendmode format action nextsub)) {
  1183. delete $form->{$_};
  1184. }
  1185. for (keys %$form) {
  1186. $hiddens{$_} = $form->{$_} unless ref $form->{$_};
  1187. }
  1188. my @buttons = ({
  1189. name => 'action',
  1190. value => 'send_email',
  1191. text => $locale->text('Continue'),
  1192. });
  1193. my $template = LedgerSMB::Template->new_UI(
  1194. user => \%myconfig,
  1195. locale => $locale,
  1196. template => 'rp-email',
  1197. );
  1198. $template->render({
  1199. form => $form,
  1200. user => \%myconfig,
  1201. hiddens => \%hiddens,
  1202. buttons => \@buttons,
  1203. });
  1204. }
  1205. sub send_email {
  1206. $form->{subject} = $locale->text( 'Statement - [_1]', $form->{todate} )
  1207. unless $form->{subject};
  1208. $form->isblank( "email", $locale->text('E-mail address missing!') );
  1209. RP->aging( \%myconfig, $form );
  1210. my $data = &print_form;
  1211. delete $form->{header};
  1212. my $template = LedgerSMB::Template->new(
  1213. user => \%myconfig,
  1214. template => $form->{'formname'} || $form->{'type'},
  1215. format => uc $form->{format},
  1216. method => 'email',
  1217. output_options => {
  1218. to => $form->{email},
  1219. cc => $form->{cc},
  1220. bcc => $form->{bcc},
  1221. from => $form->{form},
  1222. subject => $form->{subject},
  1223. message => $form->{message},
  1224. notify => $form->{read_receipt},
  1225. attach => ($form->{sendmode} eq 'attachment')? 1: 0,
  1226. },
  1227. );
  1228. try {
  1229. $template->render({data => [$data]});
  1230. }
  1231. catch Error::Simple with {
  1232. my $E = shift;
  1233. $form->error( $E->stacktrace );
  1234. };
  1235. $form->redirect(
  1236. $locale->text( 'Statement sent to [_1]', $form->{ $form->{ct} } ) );
  1237. exit;
  1238. }
  1239. sub print {
  1240. if ( $form->{media} !~ /(screen|email)/ ) {
  1241. $form->error( $locale->text('Select postscript or PDF!') )
  1242. if ( $form->{format} !~ /(postscript|pdf)/ );
  1243. }
  1244. my @batch_data = ();
  1245. my $selected;
  1246. for $i ( 1 .. $form->{rowcount} ) {
  1247. if ( $form->{"statement_$i"} ) {
  1248. $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
  1249. $language_code = $form->{"language_code_$i"};
  1250. $curr = $form->{"curr_$i"};
  1251. $selected = 1;
  1252. if ( $form->{media} !~ /(screen|email)/ ) {
  1253. # SC: I may not need this anymore...
  1254. # But I'll wait until lpr output is working before deciding
  1255. $form->{OUT} = "${LedgerSMB::Sysconfig::printer}{$form->{media}}";
  1256. $form->{"$form->{ct}_id"} = "";
  1257. $SIG{INT} = 'IGNORE';
  1258. }
  1259. else {
  1260. $form->{"statement_1"} = 1;
  1261. $form->{"language_code_1"} = $language_code;
  1262. $form->{"curr_1"} = $curr;
  1263. }
  1264. RP->aging( \%myconfig, \%$form );
  1265. $printhash = &print_form;
  1266. push @batch_data, $printhash;
  1267. }
  1268. }
  1269. $form->error( $locale->text('Nothing selected!') ) unless $selected;
  1270. my $template = LedgerSMB::Template->new(
  1271. user => \%myconfig,
  1272. template => $form->{'formname'} || $form->{'type'},
  1273. format => uc $form->{format}
  1274. );
  1275. try {
  1276. $template->render({data => \@batch_data});
  1277. $template->output($form);
  1278. }
  1279. catch Error::Simple with {
  1280. my $E = shift;
  1281. $form->error( $E->stacktrace );
  1282. };
  1283. $form->redirect( $locale->text('Statements sent to printer!') )
  1284. if ( $form->{media} !~ /(screen|email)/ );
  1285. }
  1286. sub print_form {
  1287. $form->{statementdate} = $locale->date( \%myconfig, $form->{todate}, 1 );
  1288. $form->{templates} = "$myconfig{templates}";
  1289. # setup variables for the form
  1290. my @vars = qw(company address businessnumber tel fax);
  1291. for (@vars) { $form->{$_} = $myconfig{$_} }
  1292. $form->{address} =~ s/\\n/\n/g;
  1293. @vars = qw(name address1 address2 city state zipcode country contact);
  1294. push @vars, "$form->{ct}phone", "$form->{ct}fax", "$form->{ct}taxnumber";
  1295. push @vars, 'email' if !$form->{media} eq 'email';
  1296. my $i = 0;
  1297. while ( @{ $form->{AG} } ) {
  1298. my $ref = shift @{ $form->{AG} };
  1299. if ( $ctid != $ref->{ctid} ) {
  1300. $ctid = $ref->{ctid};
  1301. $i++;
  1302. if ( $form->{"statement_$i"} ) {
  1303. for (@vars) { $form->{$_} = $ref->{$_} }
  1304. $form->{ $form->{ct} } = $form->{name};
  1305. $form->{"$form->{ct}_id"} = $ref->{ctid};
  1306. $form->{language_code} = $form->{"language_code_$i"};
  1307. $form->{currency} = $form->{"curr_$i"};
  1308. for (qw(invnumber ordnumber ponumber notes invdate duedate)) {
  1309. $form->{$_} = ();
  1310. }
  1311. $form->{total} = 0;
  1312. foreach $item (qw(c0 c30 c60 c90)) {
  1313. $form->{$item} = ();
  1314. $form->{"${item}total"} = 0;
  1315. }
  1316. &statement_details($ref) if $ref->{curr} eq $form->{currency};
  1317. while ($ref) {
  1318. if ( scalar( @{ $form->{AG} } ) > 0 ) {
  1319. # one or more left to go
  1320. if ( $ctid == $form->{AG}->[0]->{ctid} ) {
  1321. $ref = shift @{ $form->{AG} };
  1322. &statement_details($ref)
  1323. if $ref->{curr} eq $form->{currency};
  1324. # any more?
  1325. $ref = scalar( @{ $form->{AG} } );
  1326. }
  1327. else {
  1328. $ref = 0;
  1329. }
  1330. }
  1331. else {
  1332. # set initial ref to 0
  1333. $ref = 0;
  1334. }
  1335. }
  1336. for ( "c0", "c30", "c60", "c90", "" ) {
  1337. $form->{"${_}total"} =
  1338. $form->format_amount( \%myconfig, $form->{"${_}total"},
  1339. 2 );
  1340. }
  1341. my $printhash = {};
  1342. for (keys %$form) { $printhash->{$_} = $form->{$_}}
  1343. return $printhash;
  1344. }
  1345. }
  1346. }
  1347. }
  1348. sub statement_details {
  1349. my ($ref) = @_;
  1350. $ref->{invdate} = $ref->{transdate};
  1351. my @a = qw(invnumber ordnumber ponumber notes invdate duedate);
  1352. for (@a) { $form->{"${_}_1"} = $ref->{$_} }
  1353. $form->format_string(qw(invnumber_1 ordnumber_1 ponumber_1 notes_1));
  1354. for (@a) { push @{ $form->{$_} }, $form->{"${_}_1"} }
  1355. foreach $item (qw(c0 c30 c60 c90)) {
  1356. eval {
  1357. $ref->{$item} =
  1358. $form->round_amount( $ref->{$item} / $ref->{exchangerate}, 2 );
  1359. };
  1360. $form->{"${item}total"} += $ref->{$item};
  1361. $form->{total} += $ref->{$item};
  1362. push @{ $form->{$item} },
  1363. $form->format_amount( \%myconfig, $ref->{$item}, 2 );
  1364. }
  1365. }
  1366. sub generate_tax_report {
  1367. RP->tax_report( \%myconfig, \%$form );
  1368. my %hiddens;
  1369. my @options;
  1370. my $descvar = "$form->{accno}_description";
  1371. my $description = $form->escape( $form->{$descvar} );
  1372. my $ratevar = "$form->{accno}_rate";
  1373. my $taxrate = $form->{"$form->{accno}_rate"};
  1374. if ( $form->{accno} =~ /^gifi_/ ) {
  1375. $descvar = "gifi_$form->{accno}_description";
  1376. $description = $form->escape( $form->{$descvar} );
  1377. $ratevar = "gifi_$form->{accno}_rate";
  1378. $taxrate = $form->{"gifi_$form->{accno}_rate"};
  1379. }
  1380. my $department = $form->escape( $form->{department} );
  1381. # construct href
  1382. my $href =
  1383. "$form->{script}?path=$form->{path}&direction=$form->{direction}&oldsort=$form->{oldsort}&action=generate_tax_report&login=$form->{login}&sessionid=$form->{sessionid}&fromdate=$form->{fromdate}&todate=$form->{todate}&db=$form->{db}&method=$form->{method}&summary=$form->{summary}&accno=$form->{accno}&$descvar=$description&department=$department&$ratevar=$taxrate&report=$form->{report}";
  1384. # construct callback
  1385. $description = $form->escape( $form->{$descvar}, 1 );
  1386. $department = $form->escape( $form->{department}, 1 );
  1387. $form->sort_order();
  1388. my $callback =
  1389. "$form->{script}?path=$form->{path}&direction=$form->{direction}&oldsort=$form->{oldsort}&action=generate_tax_report&login=$form->{login}&sessionid=$form->{sessionid}&fromdate=$form->{fromdate}&todate=$form->{todate}&db=$form->{db}&method=$form->{method}&summary=$form->{summary}&accno=$form->{accno}&$descvar=$description&department=$department&$ratevar=$taxrate&report=$form->{report}";
  1390. $form->{title} = $locale->text('GIFI') . " - "
  1391. if ( $form->{accno} =~ /^gifi_/ );
  1392. my $title = $form->escape( $form->{title} );
  1393. $href .= "&title=$title";
  1394. $title = $form->escape( $form->{title}, 1 );
  1395. $callback .= "&title=$title";
  1396. $form->{title} = qq|$form->{title} $form->{"$form->{accno}_description"} |;
  1397. my @columns =
  1398. $form->sort_columns(
  1399. qw(id transdate invnumber name description netamount tax total));
  1400. $form->{"l_description"} = "" if $form->{summary};
  1401. my @column_index;
  1402. foreach my $item (@columns) {
  1403. if ( $form->{"l_$item"} eq "Y" ) {
  1404. push @column_index, $item;
  1405. # add column to href and callback
  1406. $callback .= "&l_$item=Y";
  1407. $href .= "&l_$item=Y";
  1408. }
  1409. }
  1410. if ( $form->{l_subtotal} eq 'Y' ) {
  1411. $callback .= "&l_subtotal=Y";
  1412. $href .= "&l_subtotal=Y";
  1413. }
  1414. if ( $form->{department} ) {
  1415. ($department) = split /--/, $form->{department};
  1416. push @options, $locale->text('Department: [_1]', $department);
  1417. }
  1418. # if there are any dates
  1419. my $fromdate;
  1420. my $todate;
  1421. if ( $form->{fromdate} || $form->{todate} ) {
  1422. if ( $form->{fromdate} ) {
  1423. $fromdate = $locale->date( \%myconfig, $form->{fromdate}, 1 );
  1424. }
  1425. if ( $form->{todate} ) {
  1426. $todate = $locale->date( \%myconfig, $form->{todate}, 1 );
  1427. }
  1428. $form->{period} = "$fromdate - $todate";
  1429. }
  1430. else {
  1431. $form->{period} =
  1432. $locale->date( \%myconfig, $form->current_date( \%myconfig ), 1 );
  1433. }
  1434. my $name;
  1435. my $invoice;
  1436. my $arap;
  1437. if ( $form->{db} eq 'ar' ) {
  1438. $name = $locale->text('Customer');
  1439. $invoice = 'is.pl';
  1440. $arap = 'ar.pl';
  1441. }
  1442. if ( $form->{db} eq 'ap' ) {
  1443. $name = $locale->text('Vendor');
  1444. $invoice = 'ir.pl';
  1445. $arap = 'ap.pl';
  1446. }
  1447. push @options, $form->{period};
  1448. my %column_header;
  1449. $column_header{id} = {
  1450. href => "$href&sort=id",
  1451. text => $locale->text('ID'),
  1452. };
  1453. $column_header{invnumber} = {
  1454. href => "$href&sort=invnumber",
  1455. text => $locale->text('Invoice')
  1456. };
  1457. $column_header{transdate} = {
  1458. href => "$href&sort=transdate",
  1459. text => $locale->text('Date'),
  1460. };
  1461. $column_header{netamount} = $locale->text('Amount');
  1462. $column_header{tax} = $locale->text('Tax');
  1463. $column_header{total} = $locale->text('Total');
  1464. $column_header{name} = {
  1465. href => "$href&sort=name",
  1466. text => $name,
  1467. };
  1468. $column_header{description} = {
  1469. href => "$href&sort=description",
  1470. text => $locale->text('Description'),
  1471. };
  1472. # add sort and escape callback
  1473. $callback = $form->escape( $callback . "&sort=$form->{sort}" );
  1474. my $sameitem;
  1475. if ( @{ $form->{TR} } ) {
  1476. $sameitem = $form->{TR}->[0]->{ $form->{sort} };
  1477. }
  1478. my $totalnetamount;
  1479. my @rows;
  1480. my $i;
  1481. foreach my $ref ( @{ $form->{TR} } ) {
  1482. my %column_data;
  1483. my $module = ( $ref->{invoice} ) ? $invoice : $arap;
  1484. $module = 'ps.pl' if $ref->{till};
  1485. if ( $form->{l_subtotal} eq 'Y' ) {
  1486. if ( $sameitem ne $ref->{ $form->{sort} } ) {
  1487. push @rows, &tax_subtotal(\@column_index);
  1488. $sameitem = $ref->{ $form->{sort} };
  1489. }
  1490. }
  1491. $totalnetamount += $ref->{netamount};
  1492. $totaltax += $ref->{tax};
  1493. $ref->{total} = $ref->{netamount} + $ref->{tax};
  1494. $subtotalnetamount += $ref->{netamount};
  1495. $subtotaltax += $ref->{tax};
  1496. for (qw(netamount tax total)) {
  1497. $ref->{$_} =
  1498. $form->format_amount( \%myconfig, $ref->{$_}, 2, ' ' );
  1499. }
  1500. $column_data{id} = $ref->{id};
  1501. $column_data{invnumber} = {
  1502. href => "$module?path=$form->{path}&action=edit&id=$ref->{id}&login=$form->{login}&sessionid=$form->{sessionid}&callback=$callback",
  1503. text => $ref->{invnumber},
  1504. };
  1505. for (qw(id transdate name partnumber description)) {
  1506. $column_data{$_} = $ref->{$_};
  1507. }
  1508. for (qw(netamount tax total)) {
  1509. $column_data{$_} = $ref->{$_};
  1510. }
  1511. $i++;
  1512. $i %= 2;
  1513. $column_data{i} = $i;
  1514. push @rows, \%column_data;
  1515. }
  1516. if ( $form->{l_subtotal} eq 'Y' ) {
  1517. push @rows, &tax_subtotal(\@column_index);
  1518. }
  1519. my %column_data;
  1520. for (@column_index) { $column_data{$_} = ' ' }
  1521. $total = $form->format_amount( \%myconfig, $totalnetamount + $totaltax,
  1522. 2, ' ' );
  1523. $totalnetamount =
  1524. $form->format_amount( \%myconfig, $totalnetamount, 2, ' ' );
  1525. $totaltax = $form->format_amount( \%myconfig, $totaltax, 2, ' ' );
  1526. $column_data{netamount} = $totalnetamount;
  1527. $column_data{tax} = $totaltax;
  1528. $column_data{total} = $total;
  1529. my $template = LedgerSMB::Template->new_UI(
  1530. user => \%myconfig,
  1531. locale => $locale,
  1532. template => 'form-dynatable',
  1533. );
  1534. $template->render({
  1535. form => $form,
  1536. hiddens => \%hiddens,
  1537. options => \@options,
  1538. columns => \@column_index,
  1539. heading => \%column_header,
  1540. rows => \@rows,
  1541. totals => \%column_data,
  1542. row_alignment => {
  1543. netamount => 'right',
  1544. tax => 'right',
  1545. total => 'right',
  1546. },
  1547. });
  1548. }
  1549. sub tax_subtotal {
  1550. my $column_index = shift;
  1551. my %column_data;
  1552. for (@{$column_index}) { $column_data{$_} = ' ' }
  1553. #SC: Yes, right now these are global, inherited from generate_tax_report
  1554. $subtotal =
  1555. $form->format_amount( \%myconfig, $subtotalnetamount + $subtotaltax,
  1556. 2, ' ' );
  1557. $subtotalnetamount =
  1558. $form->format_amount( \%myconfig, $subtotalnetamount, 2, ' ' );
  1559. $subtotaltax =
  1560. $form->format_amount( \%myconfig, $subtotaltax, 2, ' ' );
  1561. $column_data{netamount} = {
  1562. class => 'subtotal',
  1563. text => $subtotalnetamount,
  1564. };
  1565. $column_data{tax} = {
  1566. class => 'subtotal',
  1567. text => $subtotaltax,
  1568. };
  1569. $column_data{total} = {
  1570. class => 'subtotal',
  1571. text => $subtotal,
  1572. };
  1573. $subtotalnetamount = 0;
  1574. $subtotaltax = 0;
  1575. \%column_data;
  1576. }
  1577. sub list_payments {
  1578. my %hiddens;
  1579. my @options;
  1580. if ( $form->{account} ) {
  1581. ( $form->{paymentaccounts} ) = split /--/, $form->{account};
  1582. }
  1583. if ( $form->{department} ) {
  1584. ( $department, $form->{department_id} ) = split /--/,
  1585. $form->{department};
  1586. push @options, $locale->text('Department: [_1]', $department);
  1587. }
  1588. RP->payments( \%myconfig, \%$form );
  1589. my @columns = $form->sort_columns(qw(transdate name paid source memo));
  1590. if ( $form->{till} ) {
  1591. @columns =
  1592. $form->sort_columns(qw(transdate name paid curr source till));
  1593. if ( $myconfig{role} ne 'user' ) {
  1594. @columns =
  1595. $form->sort_columns(
  1596. qw(transdate name paid curr source till employee));
  1597. }
  1598. }
  1599. # construct href
  1600. my $title = $form->escape( $form->{title} );
  1601. $form->{paymentaccounts} =~ s/ /%20/g;
  1602. my $href =
  1603. "$form->{script}?path=$form->{path}&direction=$form->{direction}&sort=$form->{sort}&oldsort=$form->{oldsort}&action=list_payments&till=$form->{till}&login=$form->{login}&sessionid=$form->{sessionid}&fromdate=$form->{fromdate}&todate=$form->{todate}&fx_transaction=$form->{fx_transaction}&db=$form->{db}&l_subtotal=$form->{l_subtotal}&prepayment=$form->{prepayment}&paymentaccounts=$form->{paymentaccounts}&title="
  1604. . $form->escape( $form->{title} );
  1605. $form->sort_order();
  1606. $form->{callback} =
  1607. "$form->{script}?path=$form->{path}&direction=$form->{direction}&sort=$form->{sort}&oldsort=$form->{oldsort}&action=list_payments&till=$form->{till}&login=$form->{login}&sessionid=$form->{sessionid}&fromdate=$form->{fromdate}&todate=$form->{todate}&fx_transaction=$form->{fx_transaction}&db=$form->{db}&l_subtotal=$form->{l_subtotal}&prepayment=$form->{prepayment}&paymentaccounts=$form->{paymentaccounts}&title="
  1608. . $form->escape( $form->{title}, 1 );
  1609. my $callback;
  1610. if ( $form->{account} ) {
  1611. $callback .= "&account=" . $form->escape( $form->{account}, 1 );
  1612. $href .= "&account=" . $form->escape( $form->{account} );
  1613. push @options, $locale->text('Account: [_1]', $form->{account});
  1614. }
  1615. if ( $form->{department} ) {
  1616. $callback .= "&department=" . $form->escape( $form->{department}, 1 );
  1617. $href .= "&department=" . $form->escape( $form->{department} );
  1618. push @options, $locale->text('Department: [_1]', $form->{department});
  1619. }
  1620. if ( $form->{description} ) {
  1621. $callback .= "&description=" . $form->escape( $form->{description}, 1 );
  1622. $href .= "&description=" . $form->escape( $form->{description} );
  1623. push @options, $locale->text('Description: [_1]', $form->{description});
  1624. }
  1625. if ( $form->{source} ) {
  1626. $callback .= "&source=" . $form->escape( $form->{source}, 1 );
  1627. $href .= "&source=" . $form->escape( $form->{source} );
  1628. push @options, $locale->text('Source: [_1]', $form->{source});
  1629. }
  1630. if ( $form->{memo} ) {
  1631. $callback .= "&memo=" . $form->escape( $form->{memo}, 1 );
  1632. $href .= "&memo=" . $form->escape( $form->{memo} );
  1633. push @options, $locale->text('Memo: [_1]', $form->{memo});
  1634. }
  1635. if ( $form->{fromdate} ) {
  1636. push @options,
  1637. $locale->text('From [_1]',
  1638. $locale->date( \%myconfig, $form->{fromdate}, 1 ));
  1639. }
  1640. if ( $form->{todate} ) {
  1641. push @options,
  1642. $locale->text('To [_1]',
  1643. $locale->date( \%myconfig, $form->{todate}, 1 ));
  1644. }
  1645. $callback = $form->escape( $form->{callback} );
  1646. my %column_header;
  1647. $column_header{name} = {
  1648. href => "$href&sort=name",
  1649. text => $locale->text('Description'),
  1650. };
  1651. $column_header{transdate} = {
  1652. href => "$href&sort=transdate",
  1653. text => $locale->text('Date'),
  1654. };
  1655. $column_header{paid} = $locale->text('Amount');
  1656. $column_header{curr} = $locale->text('Curr');
  1657. $column_header{source} = {
  1658. href => "$href&sort=source",
  1659. text => $locale->text('Source'),
  1660. };
  1661. $column_header{memo} = {
  1662. href => "$href&sort=memo",
  1663. text => $locale->text('Memo'),
  1664. };
  1665. $column_header{employee} = {
  1666. href => "$href&sort=employee",
  1667. text => $locale->text('Salesperson'),
  1668. };
  1669. $column_header{till} = {
  1670. href => "$href&sort=till",
  1671. text => $locale->text('Till'),
  1672. };
  1673. my @column_index = @columns;
  1674. my @accounts;
  1675. my $i;
  1676. foreach my $ref ( sort { $a->{accno} cmp $b->{accno} } @{ $form->{PR} } ) {
  1677. next unless @{ $form->{ $ref->{id} } };
  1678. push @accounts, {header => "$ref->{accno}--$ref->{description}"};
  1679. if ( @{ $form->{ $ref->{id} } } ) {
  1680. $sameitem = $form->{ $ref->{id} }[0]->{ $form->{sort} };
  1681. }
  1682. my @rows;
  1683. foreach my $payment ( @{ $form->{ $ref->{id} } } ) {
  1684. if ( $form->{l_subtotal} ) {
  1685. if ( $payment->{ $form->{sort} } ne $sameitem ) {
  1686. # print subtotal
  1687. push @rows, &payment_subtotal(\@column_index);
  1688. }
  1689. }
  1690. next if ( $form->{till} && !$payment->{till} );
  1691. my %column_data;
  1692. $column_data{name} = $payment->{name};
  1693. $column_data{transdate} = $payment->{transdate};
  1694. $column_data{paid} =
  1695. $form->format_amount(\%myconfig, $payment->{paid}, 2, ' ');
  1696. $column_data{curr} = $payment->{curr};
  1697. $column_data{source} = $payment->{source};
  1698. $column_data{memo} = $payment->{memo};
  1699. $column_data{employee} = $payment->{employee};
  1700. $column_data{till} = $payment->{till};
  1701. $subtotalpaid += $payment->{paid};
  1702. $accounttotalpaid += $payment->{paid};
  1703. $totalpaid += $payment->{paid};
  1704. $i++;
  1705. $i %= 2;
  1706. $column_data{i} = $i;
  1707. push @rows, \%column_data;
  1708. $sameitem = $payment->{ $form->{sort} };
  1709. }
  1710. push @rows, &payment_subtotal(\@column_index) if $form->{l_subtotal};
  1711. $accounts[$#accounts]{rows} = \@rows;
  1712. # print account totals
  1713. my %column_data;
  1714. for (@column_index) { $column_data{$_} = ' ' }
  1715. $column_data{paid} =
  1716. $form->format_amount( \%myconfig, $accounttotalpaid, 2, ' ' );
  1717. $accounts[$#accounts]{totals} = \%column_data;
  1718. $accounttotalpaid = 0;
  1719. }
  1720. # prepare total
  1721. my %column_data;
  1722. for (@column_index) { $column_data{$_} = ' ' }
  1723. $column_data{paid} = $form->format_amount( \%myconfig, $totalpaid, 2, ' ' );
  1724. ##SC: Temporary removal
  1725. ## if ( $form->{lynx} ) {
  1726. ## require "bin/menu.pl";
  1727. ## &menubar;
  1728. ## }
  1729. my $template = LedgerSMB::Template->new_UI(
  1730. user => \%myconfig,
  1731. locale => $locale,
  1732. template => 'rp-payments',
  1733. );
  1734. $template->render({
  1735. form => $form,
  1736. hiddens => \%hiddens,
  1737. options => \@options,
  1738. columns => \@column_index,
  1739. heading => \%column_header,
  1740. accounts => \@accounts,
  1741. totals => \%column_data,
  1742. row_alignment => {
  1743. paid => 'right',
  1744. },
  1745. });
  1746. }
  1747. sub payment_subtotal {
  1748. my $column_index = shift;
  1749. my %column_data;
  1750. if ( $subtotalpaid != 0 ) {
  1751. for (@column_index) { $column_data{$_} = ' ' }
  1752. $column_data{paid} =
  1753. $form->format_amount( \%myconfig, $subtotalpaid, 2, ' ' );
  1754. $column_data{class} = 'subtotal';
  1755. }
  1756. $subtotalpaid = 0;
  1757. \%column_data;
  1758. }