summaryrefslogtreecommitdiff
path: root/bin/rp.pl
blob: 1426824ec6fa889523b4d04b2789f235bc49a4bb (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. my %column_data;
  634. if ($subtotal) {
  635. for (qw(accno begbalance endbalance)) {
  636. $column_data{$_} = " ";
  637. }
  638. $subtotalbegbalance =
  639. $form->format_amount( \%myconfig, $subtotalbegbalance, 2,
  640. " " );
  641. $subtotalendbalance =
  642. $form->format_amount( \%myconfig, $subtotalendbalance, 2,
  643. " " );
  644. $subtotaldebit =
  645. $form->format_amount( \%myconfig, $subtotaldebit, 2,
  646. " " );
  647. $subtotalcredit =
  648. $form->format_amount( \%myconfig, $subtotalcredit, 2,
  649. " " );
  650. $column_data{description} = $subtotaldescription;
  651. $column_data{begbalance} = $subtotalbegbalance;
  652. $column_data{endbalance} = $subtotalendbalance;
  653. $column_data{debit} = $subtotaldebit;
  654. $column_data{credit} = $subtotalcredit;
  655. $column_data{class} = 'subtotal';
  656. push @rows, \%column_data;
  657. }
  658. }
  659. if ( $ref->{charttype} eq "H" ) {
  660. $subtotal = 1;
  661. $subtotaldescription = $ref->{description};
  662. $subtotaldebit = $ref->{debit};
  663. $subtotalcredit = $ref->{credit};
  664. $subtotalbegbalance = 0;
  665. $subtotalendbalance = 0;
  666. if ( $form->{l_heading} ) {
  667. if ( !$form->{all_accounts} and
  668. ( $subtotaldebit + $subtotalcredit ) == 0 ) {
  669. $subtotal = 0;
  670. next;
  671. }
  672. } else {
  673. $subtotal = 0;
  674. if ( $form->{all_accounts} || ( $form->{l_subtotal} &&
  675. ( ( $subtotaldebit + $subtotalcredit ) != 0 ) )) {
  676. $subtotal = 1;
  677. }
  678. next;
  679. }
  680. for (qw(accno debit credit begbalance endbalance)) {
  681. $column_data{$_} = " ";
  682. }
  683. $column_data{description} = $ref->{description};
  684. $column_data{class} = 'heading';
  685. }
  686. if ( $ref->{charttype} eq "A" ) {
  687. $column_data{accno} = {text => $ref->{accno}, href => $href};
  688. $column_data{description} = $ref->{description};
  689. $column_data{debit} = $debit;
  690. $column_data{credit} = $credit;
  691. $column_data{begbalance} = $begbalance;
  692. $column_data{endbalance} = $endbalance;
  693. $totaldebit += $ref->{debit};
  694. $totalcredit += $ref->{credit};
  695. $cml = ( $ref->{contra} ) ? -1 : 1;
  696. $subtotalbegbalance += $ref->{balance} * $ml * $cml;
  697. $subtotalendbalance +=
  698. ( $ref->{balance} + $ref->{amount} ) * $ml * $cml;
  699. }
  700. if ( $ref->{charttype} eq "H" ) {
  701. $column_data{class} = 'heading';
  702. }
  703. if ( $ref->{charttype} eq "A" ) {
  704. $i++;
  705. $i %= 2;
  706. $column_data{i} = $i;
  707. }
  708. push @rows, \%column_data;
  709. }
  710. # print last subtotal
  711. if ( $subtotal && $form->{l_subtotal} ) {
  712. my %column_data;
  713. for (qw(accno begbalance endbalance)) {
  714. $column_data{$_} = " ";
  715. }
  716. $subtotalbegbalance =
  717. $form->format_amount( \%myconfig, $subtotalbegbalance, 2, " " );
  718. $subtotalendbalance =
  719. $form->format_amount( \%myconfig, $subtotalendbalance, 2, " " );
  720. $subtotaldebit =
  721. $form->format_amount( \%myconfig, $subtotaldebit, 2, " " );
  722. $subtotalcredit =
  723. $form->format_amount( \%myconfig, $subtotalcredit, 2, " " );
  724. $column_data{description} = $subtotaldescription;
  725. $column_data{begbalance} = $subtotalbegbalance;
  726. $column_data{endbalance} = $subtotalendbalance;
  727. $column_data{debit} = $subtotaldebit;
  728. $column_data{credit} = $subtotalcredit;
  729. $column_data{class} = 'subtotal';
  730. push @rows, \%column_data;
  731. }
  732. my %column_data;
  733. $totaldebit = $form->format_amount( \%myconfig, $totaldebit, 2, " " );
  734. $totalcredit =
  735. $form->format_amount( \%myconfig, $totalcredit, 2, " " );
  736. for (qw(accno description begbalance endbalance)) {
  737. $column_data{$_} = "";
  738. }
  739. $column_data{debit} = $totaldebit;
  740. $column_data{credit} = $totalcredit;
  741. my @buttons;
  742. for my $type (qw(CSV XLS ODS)) {
  743. push @buttons, {
  744. name => 'action',
  745. value => lc "${type}_$form->{nextsub}",
  746. text => $locale->text("$type Report"),
  747. type => 'submit',
  748. class => 'submit',
  749. };
  750. }
  751. my $format;
  752. if ($form->{action} eq 'continue') {
  753. $format = 'HTML';
  754. } else {
  755. $format = uc substr $form->{action}, 0, 3;
  756. push @column_index, 'class';
  757. $column_header{class} = 'rowtype';
  758. }
  759. my $template = LedgerSMB::Template->new(
  760. user => \%myconfig,
  761. locale => $locale,
  762. template => 'form-dynatable',
  763. path => 'UI',
  764. format => $format,
  765. );
  766. $template->render({
  767. form => $form,
  768. hiddens => \%hiddens,
  769. buttons => \@buttons,
  770. options => \@options,
  771. columns => \@column_index,
  772. heading => \%column_header,
  773. rows => \@rows,
  774. totals => \%column_data,
  775. row_alignment => {
  776. 'credit' => 'right',
  777. 'debit' => 'right',
  778. 'begbalance' => 'right',
  779. 'endbalance' => 'right'
  780. },
  781. });
  782. }
  783. sub generate_ar_aging {
  784. # split customer
  785. ( $form->{customer} ) = split( /--/, $form->{customer} );
  786. $customer = $form->escape( $form->{customer}, 1 );
  787. $title = $form->escape( $form->{title}, 1 );
  788. $media = $form->escape( $form->{media}, 1 );
  789. $form->{ct} = "customer";
  790. $form->{arap} = "ar";
  791. RP->aging( \%myconfig, \%$form );
  792. $form->{callback} =
  793. 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}|;
  794. &aging;
  795. }
  796. sub csv_generate_ar_aging { &generate_ar_aging }
  797. sub generate_ap_aging {
  798. # split vendor
  799. ( $form->{vendor} ) = split( /--/, $form->{vendor} );
  800. $vendor = $form->escape( $form->{vendor}, 1 );
  801. $title = $form->escape( $form->{title}, 1 );
  802. $media = $form->escape( $form->{media}, 1 );
  803. $form->{ct} = "vendor";
  804. $form->{arap} = "ap";
  805. RP->aging( \%myconfig, \%$form );
  806. $form->{callback} =
  807. 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}|;
  808. &aging;
  809. }
  810. sub aging {
  811. my %hiddens;
  812. my @buttons;
  813. my @options;
  814. my %column_header;
  815. my @column_index;
  816. my %row_alignment;
  817. $column_header{statement} = ' ';
  818. $column_header{ct} = $locale->text( ucfirst $form->{ct} );
  819. $column_header{language} = $locale->text('Language');
  820. $column_header{invnumber} = $locale->text('Invoice');
  821. $column_header{ordnumber} = $locale->text('Order');
  822. $column_header{transdate} = $locale->text('Date');
  823. $column_header{duedate} = $locale->text('Due Date');
  824. $column_header{c0} = $locale->text('Current');
  825. $column_header{c30} = '30';
  826. $column_header{c60} = '60';
  827. $column_header{c90} = '90';
  828. $column_header{total} = $locale->text('Total');
  829. @column_index = qw(statement ct);
  830. if ( @{ $form->{all_language} } && $form->{arap} eq 'ar' ) {
  831. push @column_index, "language";
  832. $form->{language_options} = [{text => ' ', value => ''}];
  833. for ( @{ $form->{all_language} } ) {
  834. push @{$form->{language_options}},
  835. {text => $_->{description}, value => $_->{code}};
  836. }
  837. }
  838. my @c = ();
  839. for (qw(c0 c30 c60 c90)) {
  840. if ( $form->{$_} ) {
  841. push @c, $_;
  842. $form->{callback} .= "&$_=$form->{$_}";
  843. }
  844. }
  845. if ( !$form->{summary} ) {
  846. push @column_index, qw(invnumber ordnumber transdate duedate);
  847. }
  848. push @column_index, @c;
  849. push @column_index, "total";
  850. if ( $form->{overdue} ) {
  851. push @options, $locale->text('Aged Overdue');
  852. $form->{callback} .= "&overdue=$form->{overdue}";
  853. } else {
  854. push @options, $locale->text('Aged');
  855. }
  856. if ( $form->{department} ) {
  857. ($department) = split /--/, $form->{department};
  858. push @options, $locale->text('Department: [_1]', $department);
  859. $department = $form->escape( $form->{department}, 1 );
  860. $form->{callback} .= "&department=$department";
  861. }
  862. if ( $form->{arap} eq 'ar' ) {
  863. if ( $form->{customer} ) {
  864. push @options, $form->{customer};
  865. }
  866. }
  867. if ( $form->{arap} eq 'ap' ) {
  868. shift @column_index;
  869. if ( $form->{vendor} ) {
  870. push @options, $form->{vendor};
  871. }
  872. }
  873. $todate = $locale->date( \%myconfig, $form->{todate}, 1 );
  874. push @options, $locale->text('for Period To [_1]', $todate);
  875. $ctid = 0;
  876. $i = 0;
  877. $k = 0;
  878. $l = $#{ $form->{AG} };
  879. my @currencies;
  880. foreach my $ref ( @{ $form->{AG} } ) {
  881. if ( $curr ne $ref->{curr} ) {
  882. my %column_data;
  883. $ctid = 0;
  884. for (@column_index) { $column_data{$_} = ' ' }
  885. if ($curr) {
  886. $c0total = $form->format_amount(\%myconfig, $c0total, 2, ' ');
  887. $c30total = $form->format_amount(\%myconfig, $c30total, 2, ' ');
  888. $c60total = $form->format_amount(\%myconfig, $c60total, 2, ' ');
  889. $c90total = $form->format_amount(\%myconfig, $c90total, 2, ' ');
  890. $total = $form->format_amount(\%myconfig, $total, 2, ' ' );
  891. for (qw(ct statement language)) {
  892. $column_data{$_} = ' ';
  893. }
  894. $column_data{c0} = $c0total;
  895. $column_data{c30} = $c30total;
  896. $column_data{c60} = $c60total;
  897. $column_data{c90} = $c90total;
  898. $column_data{total} = $total;
  899. push @{$currencies[0]{totals}}, \%column_data;
  900. $c0subtotal = 0;
  901. $c30subtotal = 0;
  902. $c60subtotal = 0;
  903. $c90subtotal = 0;
  904. $subtotal = 0;
  905. $c0total = 0;
  906. $c30total = 0;
  907. $c60total = 0;
  908. $c90total = 0;
  909. $total = 0;
  910. }
  911. unshift @currencies, {};
  912. $curr = $ref->{curr};
  913. $currencies[0]{curr} = $curr;
  914. }
  915. $k++;
  916. my %column_data;
  917. if ( $ctid != $ref->{ctid} or $form->{summary}) {
  918. $i++;
  919. $column_data{ct} = $ref->{name};
  920. $column_data{language} = {
  921. name => "language_code_$i",
  922. options => $form->{language_options},
  923. default_value => $ref->{language_code},
  924. } if $form->{language_options};
  925. $column_data{statement} = {
  926. name => "statement_$i",
  927. type => 'checkbox',
  928. value => '1',
  929. };
  930. $column_data{statement}{checked} = 'checked' if $ref->{checked};
  931. $hiddens{"$form->{ct}_id_$i"} = $ref->{ctid};
  932. $hiddens{"curr_$i"} = $ref->{curr};
  933. }
  934. $ctid = $ref->{ctid};
  935. for (qw(c0 c30 c60 c90)) {
  936. $ref->{$_} =
  937. $form->round_amount( $ref->{$_} / $ref->{exchangerate}, 2 );
  938. }
  939. $c0subtotal += $ref->{c0};
  940. $c30subtotal += $ref->{c30};
  941. $c60subtotal += $ref->{c60};
  942. $c90subtotal += $ref->{c90};
  943. $c0total += $ref->{c0};
  944. $c30total += $ref->{c30};
  945. $c60total += $ref->{c60};
  946. $c90total += $ref->{c90};
  947. $ref->{total} =
  948. ( $ref->{c0} + $ref->{c30} + $ref->{c60} + $ref->{c90} );
  949. $subtotal += $ref->{total};
  950. $total += $ref->{total};
  951. $ref->{c0} =
  952. $form->format_amount( \%myconfig, $ref->{c0}, 2, ' ' );
  953. $ref->{c30} =
  954. $form->format_amount( \%myconfig, $ref->{c30}, 2, ' ' );
  955. $ref->{c60} =
  956. $form->format_amount( \%myconfig, $ref->{c60}, 2, ' ' );
  957. $ref->{c90} =
  958. $form->format_amount( \%myconfig, $ref->{c90}, 2, ' ' );
  959. $ref->{total} =
  960. $form->format_amount( \%myconfig, $ref->{total}, 2, ' ' );
  961. $href =
  962. qq|$ref->{module}.pl?path=$form->{path}&action=edit&id=$ref->{id}&login=$form->{login}&sessionid=$form->{sessionid}&callback=|
  963. . $form->escape( $form->{callback} );
  964. $column_data{invnumber} = {text => $ref->{invnumber}, href => $href};
  965. for (qw(c0 c30 c60 c90 total ordnumber transdate duedate)) {
  966. $column_data{$_} = $ref->{$_};
  967. }
  968. if ( !$form->{summary} ) {
  969. $j++;
  970. $j %= 2;
  971. $column_data{i} = $j;
  972. my $rowref = {};
  973. $rowref->{$_} = $column_data{$_} for keys %column_data;
  974. push @{$currencies[0]{rows}}, $rowref;
  975. for (qw(ct statement language)) {
  976. $column_data{$_} = ' ';
  977. }
  978. }
  979. $column_data{ct} = $ref->{name};
  980. # prepare subtotal
  981. $nextid = ( $k <= $l ) ? $form->{AG}->[$k]->{ctid} : 0;
  982. if ( $ctid != $nextid ) {
  983. $c0subtotal =
  984. $form->format_amount( \%myconfig, $c0subtotal, 2, ' ' );
  985. $c30subtotal =
  986. $form->format_amount( \%myconfig, $c30subtotal, 2, ' ' );
  987. $c60subtotal =
  988. $form->format_amount( \%myconfig, $c60subtotal, 2, ' ' );
  989. $c90subtotal =
  990. $form->format_amount( \%myconfig, $c90subtotal, 2, ' ' );
  991. $subtotal =
  992. $form->format_amount( \%myconfig, $subtotal, 2, ' ' );
  993. if ( $form->{summary} ) {
  994. $column_data{c0} = $c0subtotal;
  995. $column_data{c30} = $c30subtotal;
  996. $column_data{c60} = $c60subtotal;
  997. $column_data{c90} = $c90subtotal;
  998. $column_data{total} = $subtotal;
  999. $j++;
  1000. $j %= 2;
  1001. $column_data{i} = $j;
  1002. push @{$currencies[0]{rows}}, \%column_data;
  1003. } else {
  1004. for (@column_index) { $column_data{$_} = ' ' }
  1005. $column_data{c0} = $c0subtotal;
  1006. $column_data{c30} = $c30subtotal;
  1007. $column_data{c60} = $c60subtotal;
  1008. $column_data{c90} = $c90subtotal;
  1009. $column_data{total} = $subtotal;
  1010. $column_data{class} = 'subtotal';
  1011. push @{$currencies[0]{rows}}, \%column_data;
  1012. }
  1013. $c0subtotal = 0;
  1014. $c30subtotal = 0;
  1015. $c60subtotal = 0;
  1016. $c90subtotal = 0;
  1017. $subtotal = 0;
  1018. }
  1019. }
  1020. my %column_data;
  1021. for (@column_index) { $column_data{$_} = ' ' }
  1022. $c0total = $form->format_amount( \%myconfig, $c0total, 2, ' ' );
  1023. $c30total = $form->format_amount( \%myconfig, $c30total, 2, ' ' );
  1024. $c60total = $form->format_amount( \%myconfig, $c60total, 2, ' ' );
  1025. $c90total = $form->format_amount( \%myconfig, $c90total, 2, ' ' );
  1026. $total = $form->format_amount( \%myconfig, $total, 2, ' ' );
  1027. $column_data{c0} = $c0total;
  1028. $column_data{c30} = $c30total;
  1029. $column_data{c60} = $c60total;
  1030. $column_data{c90} = $c90total;
  1031. $column_data{total} = $total;
  1032. $currencies[0]{total} = \%column_data;
  1033. $row_alignment{c0} = 'right';
  1034. $row_alignment{c30} = 'right';
  1035. $row_alignment{c60} = 'right';
  1036. $row_alignment{c90} = 'right';
  1037. $row_alignment{total} = 'right';
  1038. $hiddens{rowcount} = $i;
  1039. &print_options if ( $form->{arap} eq 'ar' );
  1040. my @buttons;
  1041. if ( $form->{arap} eq 'ar' ) {
  1042. $hiddens{$_} = $form->{$_} foreach qw(todate title summary overdue c0 c30 c60 c90 callback arap ct department path login sessionid);
  1043. $hiddens{$form->{ct}} = $form->{$form->{ct}};
  1044. # type=submit $locale->text('Select all')
  1045. # type=submit $locale->text('Print')
  1046. # type=submit $locale->text('E-mail')
  1047. my %button = (
  1048. 'select_all' =>
  1049. { ndx => 1, key => 'A', value => $locale->text('Select all') },
  1050. 'print' =>
  1051. { ndx => 2, key => 'P', value => $locale->text('Print') },
  1052. 'e_mail' =>
  1053. { ndx => 5, key => 'E', value => $locale->text('E-mail') },
  1054. );
  1055. for ( sort { $button{$a}->{ndx} <=> $button{$b}->{ndx} } keys %button )
  1056. {
  1057. push @buttons, {
  1058. accesskey => $button{$_}->{key},
  1059. text => $button{$_}->{value},
  1060. title => "$button{$_}->{value} [Alt-$button{$_}->{key}]",
  1061. value => $_,
  1062. name => 'action',
  1063. };
  1064. }
  1065. }
  1066. ##SC: Temporary commenting
  1067. ## if ( $form->{lynx} ) {
  1068. ## require "bin/menu.pl";
  1069. ## &menubar;
  1070. ## }
  1071. for my $type (qw(CSV XLS ODS)) {
  1072. push @buttons, {
  1073. name => 'action',
  1074. value => lc "${type}_$form->{nextsub}",
  1075. text => $locale->text("$type Report"),
  1076. type => 'submit',
  1077. class => 'submit',
  1078. };
  1079. }
  1080. my $format;
  1081. if ($form->{action} =~ /^(continue|generate_)/) {
  1082. $format = 'HTML';
  1083. } else {
  1084. $format = uc substr $form->{action}, 0, 3;
  1085. push @column_index, 'class';
  1086. @column_index = grep {!/^(language|statement)$/} @column_index;
  1087. $column_header{class} = 'rowtype';
  1088. }
  1089. my $template = LedgerSMB::Template->new(
  1090. user => \%myconfig,
  1091. locale => $locale,
  1092. template => 'rp-aging',
  1093. path => 'UI',
  1094. format => $format,
  1095. );
  1096. $template->render({
  1097. form => $form,
  1098. hiddens => \%hiddens,
  1099. buttons => \@buttons,
  1100. options => \@options,
  1101. columns => \@column_index,
  1102. heading => \%column_header,
  1103. currencies => [reverse @currencies],
  1104. row_alignment => {
  1105. 'credit' => 'right',
  1106. 'debit' => 'right',
  1107. 'begbalance' => 'right',
  1108. 'endbalance' => 'right'
  1109. },
  1110. });
  1111. }
  1112. sub select_all {
  1113. RP->aging( \%myconfig, \%$form );
  1114. for ( @{ $form->{AG} } ) { $_->{checked} = "checked" }
  1115. &aging;
  1116. }
  1117. sub print_options {
  1118. $form->{sendmode} = "attachment";
  1119. $form->{copies} = 1 unless $form->{copies};
  1120. $form->{print}{format} = {name => 'format', default_values => $form->{format}};
  1121. $form->{print}{template} = {name => 'type', default_values => $form->{type}};
  1122. my @formats = ();
  1123. my @media = ();
  1124. my @templates = ();
  1125. push @formats, {text => 'HTML', value => 'html'};
  1126. push @templates, {text => $locale->text('Statement'), value => 'statement'};
  1127. if ( $form->{media} eq 'email' ) {
  1128. $form->{print}{medium} = {name => 'sendmode', default_values => $form->{sendmode}};
  1129. push @media, {text => $locale->text('Attachment'), value => 'attachment'};
  1130. push @media, {text => $locale->text('In-line'), value => 'inline'};
  1131. if ($form->{SM}{attachment}) {
  1132. $form->{print}{medium}{default_values} = $form->{SM}{attachment};
  1133. } elsif ($form->{SM}{inline}) {
  1134. $form->{print}{medium}{default_values} = $form->{SM}{inline};
  1135. }
  1136. } else {
  1137. $form->{print}{medium} = {name => 'media'};
  1138. push @media, {text => $locale->text('Screen'), value => 'screen'};
  1139. if ( %{LedgerSMB::Sysconfig::printer}
  1140. && ${LedgerSMB::Sysconfig::latex} )
  1141. {
  1142. for ( sort keys %{LedgerSMB::Sysconfig::printer} ) {
  1143. push @media, {text => $_, value => $_};
  1144. }
  1145. }
  1146. }
  1147. if ( ${LedgerSMB::Sysconfig::latex} ) {
  1148. push @formats, {text => $locale->text('PDF'), value => 'pdf'};
  1149. push @formats, {text => $locale->text('Postscript'), value => 'ps'};
  1150. }
  1151. if ( %{LedgerSMB::Sysconfig::printer}
  1152. && ${LedgerSMB::Sysconfig::latex}
  1153. && $form->{media} ne 'email' )
  1154. {
  1155. $form->{print}{copies} = {
  1156. label => $locale->text('Copies'),
  1157. name => 'copies',
  1158. value => $form->{copies},
  1159. size => 2,
  1160. };
  1161. }
  1162. $form->{print}{template}{options} = \@templates;
  1163. $form->{print}{format}{options} = \@formats;
  1164. $form->{print}{medium}{options} = \@media;
  1165. }
  1166. sub e_mail {
  1167. my %hiddens;
  1168. # get name and email addresses
  1169. for $i ( 1 .. $form->{rowcount} ) {
  1170. if ( $form->{"statement_$i"} ) {
  1171. $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
  1172. $form->{"statement_1"} = 1;
  1173. $form->{"language_code_1"} = $form->{"language_code_$i"};
  1174. $form->{"curr_1"} = $form->{"curr_$i"};
  1175. RP->get_customer( \%myconfig, \%$form );
  1176. $selected = 1;
  1177. last;
  1178. }
  1179. }
  1180. $form->error( $locale->text('Nothing selected!') ) unless $selected;
  1181. $form->{media} = "email";
  1182. &print_options;
  1183. for (qw(email cc bcc subject message type sendmode format action nextsub)) {
  1184. delete $form->{$_};
  1185. }
  1186. for (keys %$form) {
  1187. $hiddens{$_} = $form->{$_} unless ref $form->{$_};
  1188. }
  1189. my @buttons = ({
  1190. name => 'action',
  1191. value => 'send_email',
  1192. text => $locale->text('Continue'),
  1193. });
  1194. my $template = LedgerSMB::Template->new_UI(
  1195. user => \%myconfig,
  1196. locale => $locale,
  1197. template => 'rp-email',
  1198. );
  1199. $template->render({
  1200. form => $form,
  1201. user => \%myconfig,
  1202. hiddens => \%hiddens,
  1203. buttons => \@buttons,
  1204. });
  1205. }
  1206. sub send_email {
  1207. $form->{subject} = $locale->text( 'Statement - [_1]', $form->{todate} )
  1208. unless $form->{subject};
  1209. $form->isblank( "email", $locale->text('E-mail address missing!') );
  1210. RP->aging( \%myconfig, $form );
  1211. my $data = &print_form;
  1212. delete $form->{header};
  1213. my $template = LedgerSMB::Template->new(
  1214. user => \%myconfig,
  1215. template => $form->{'formname'} || $form->{'type'},
  1216. format => uc $form->{format},
  1217. method => 'email',
  1218. output_options => {
  1219. to => $form->{email},
  1220. cc => $form->{cc},
  1221. bcc => $form->{bcc},
  1222. from => $form->{form},
  1223. subject => $form->{subject},
  1224. message => $form->{message},
  1225. notify => $form->{read_receipt},
  1226. attach => ($form->{sendmode} eq 'attachment')? 1: 0,
  1227. },
  1228. );
  1229. try {
  1230. $template->render({data => [$data]});
  1231. }
  1232. catch Error::Simple with {
  1233. my $E = shift;
  1234. $form->error( $E->stacktrace );
  1235. };
  1236. $form->redirect(
  1237. $locale->text( 'Statement sent to [_1]', $form->{ $form->{ct} } ) );
  1238. exit;
  1239. }
  1240. sub print {
  1241. if ( $form->{media} !~ /(screen|email)/ ) {
  1242. $form->error( $locale->text('Select postscript or PDF!') )
  1243. if ( $form->{format} !~ /(postscript|pdf)/ );
  1244. }
  1245. my @batch_data = ();
  1246. my $selected;
  1247. for $i ( 1 .. $form->{rowcount} ) {
  1248. if ( $form->{"statement_$i"} ) {
  1249. $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
  1250. $language_code = $form->{"language_code_$i"};
  1251. $curr = $form->{"curr_$i"};
  1252. $selected = 1;
  1253. if ( $form->{media} !~ /(screen|email)/ ) {
  1254. # SC: I may not need this anymore...
  1255. # But I'll wait until lpr output is working before deciding
  1256. $form->{OUT} = "${LedgerSMB::Sysconfig::printer}{$form->{media}}";
  1257. $form->{"$form->{ct}_id"} = "";
  1258. $SIG{INT} = 'IGNORE';
  1259. }
  1260. else {
  1261. $form->{"statement_1"} = 1;
  1262. $form->{"language_code_1"} = $language_code;
  1263. $form->{"curr_1"} = $curr;
  1264. }
  1265. RP->aging( \%myconfig, \%$form );
  1266. $printhash = &print_form;
  1267. push @batch_data, $printhash;
  1268. }
  1269. }
  1270. $form->error( $locale->text('Nothing selected!') ) unless $selected;
  1271. my $template = LedgerSMB::Template->new(
  1272. user => \%myconfig,
  1273. template => $form->{'formname'} || $form->{'type'},
  1274. format => uc $form->{format}
  1275. );
  1276. try {
  1277. $template->render({data => \@batch_data});
  1278. $template->output($form);
  1279. }
  1280. catch Error::Simple with {
  1281. my $E = shift;
  1282. $form->error( $E->stacktrace );
  1283. };
  1284. $form->redirect( $locale->text('Statements sent to printer!') )
  1285. if ( $form->{media} !~ /(screen|email)/ );
  1286. }
  1287. sub print_form {
  1288. $form->{statementdate} = $locale->date( \%myconfig, $form->{todate}, 1 );
  1289. $form->{templates} = "$myconfig{templates}";
  1290. # setup variables for the form
  1291. my @vars = qw(company address businessnumber tel fax);
  1292. for (@vars) { $form->{$_} = $myconfig{$_} }
  1293. $form->{address} =~ s/\\n/\n/g;
  1294. @vars = qw(name address1 address2 city state zipcode country contact);
  1295. push @vars, "$form->{ct}phone", "$form->{ct}fax", "$form->{ct}taxnumber";
  1296. push @vars, 'email' if !$form->{media} eq 'email';
  1297. my $i = 0;
  1298. while ( @{ $form->{AG} } ) {
  1299. my $ref = shift @{ $form->{AG} };
  1300. if ( $ctid != $ref->{ctid} ) {
  1301. $ctid = $ref->{ctid};
  1302. $i++;
  1303. if ( $form->{"statement_$i"} ) {
  1304. for (@vars) { $form->{$_} = $ref->{$_} }
  1305. $form->{ $form->{ct} } = $form->{name};
  1306. $form->{"$form->{ct}_id"} = $ref->{ctid};
  1307. $form->{language_code} = $form->{"language_code_$i"};
  1308. $form->{currency} = $form->{"curr_$i"};
  1309. for (qw(invnumber ordnumber ponumber notes invdate duedate)) {
  1310. $form->{$_} = ();
  1311. }
  1312. $form->{total} = 0;
  1313. foreach $item (qw(c0 c30 c60 c90)) {
  1314. $form->{$item} = ();
  1315. $form->{"${item}total"} = 0;
  1316. }
  1317. &statement_details($ref) if $ref->{curr} eq $form->{currency};
  1318. while ($ref) {
  1319. if ( scalar( @{ $form->{AG} } ) > 0 ) {
  1320. # one or more left to go
  1321. if ( $ctid == $form->{AG}->[0]->{ctid} ) {
  1322. $ref = shift @{ $form->{AG} };
  1323. &statement_details($ref)
  1324. if $ref->{curr} eq $form->{currency};
  1325. # any more?
  1326. $ref = scalar( @{ $form->{AG} } );
  1327. }
  1328. else {
  1329. $ref = 0;
  1330. }
  1331. }
  1332. else {
  1333. # set initial ref to 0
  1334. $ref = 0;
  1335. }
  1336. }
  1337. for ( "c0", "c30", "c60", "c90", "" ) {
  1338. $form->{"${_}total"} =
  1339. $form->format_amount( \%myconfig, $form->{"${_}total"},
  1340. 2 );
  1341. }
  1342. my $printhash = {};
  1343. for (keys %$form) { $printhash->{$_} = $form->{$_}}
  1344. return $printhash;
  1345. }
  1346. }
  1347. }
  1348. }
  1349. sub statement_details {
  1350. my ($ref) = @_;
  1351. $ref->{invdate} = $ref->{transdate};
  1352. my @a = qw(invnumber ordnumber ponumber notes invdate duedate);
  1353. for (@a) { $form->{"${_}_1"} = $ref->{$_} }
  1354. $form->format_string(qw(invnumber_1 ordnumber_1 ponumber_1 notes_1));
  1355. for (@a) { push @{ $form->{$_} }, $form->{"${_}_1"} }
  1356. foreach $item (qw(c0 c30 c60 c90)) {
  1357. eval {
  1358. $ref->{$item} =
  1359. $form->round_amount( $ref->{$item} / $ref->{exchangerate}, 2 );
  1360. };
  1361. $form->{"${item}total"} += $ref->{$item};
  1362. $form->{total} += $ref->{$item};
  1363. push @{ $form->{$item} },
  1364. $form->format_amount( \%myconfig, $ref->{$item}, 2 );
  1365. }
  1366. }
  1367. sub generate_tax_report {
  1368. RP->tax_report( \%myconfig, \%$form );
  1369. my %hiddens;
  1370. my @options;
  1371. my $descvar = "$form->{accno}_description";
  1372. my $description = $form->escape( $form->{$descvar} );
  1373. my $ratevar = "$form->{accno}_rate";
  1374. my $taxrate = $form->{"$form->{accno}_rate"};
  1375. if ( $form->{accno} =~ /^gifi_/ ) {
  1376. $descvar = "gifi_$form->{accno}_description";
  1377. $description = $form->escape( $form->{$descvar} );
  1378. $ratevar = "gifi_$form->{accno}_rate";
  1379. $taxrate = $form->{"gifi_$form->{accno}_rate"};
  1380. }
  1381. my $department = $form->escape( $form->{department} );
  1382. # construct href
  1383. my $href =
  1384. "$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}";
  1385. # construct callback
  1386. $description = $form->escape( $form->{$descvar}, 1 );
  1387. $department = $form->escape( $form->{department}, 1 );
  1388. $form->sort_order();
  1389. my $callback =
  1390. "$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}";
  1391. $form->{title} = $locale->text('GIFI') . " - "
  1392. if ( $form->{accno} =~ /^gifi_/ );
  1393. my $title = $form->escape( $form->{title} );
  1394. $href .= "&title=$title";
  1395. $title = $form->escape( $form->{title}, 1 );
  1396. $callback .= "&title=$title";
  1397. $form->{title} = qq|$form->{title} $form->{"$form->{accno}_description"} |;
  1398. my @columns =
  1399. $form->sort_columns(
  1400. qw(id transdate invnumber name description netamount tax total));
  1401. $form->{"l_description"} = "" if $form->{summary};
  1402. my @column_index;
  1403. foreach my $item (@columns) {
  1404. if ( $form->{"l_$item"} eq "Y" ) {
  1405. push @column_index, $item;
  1406. # add column to href and callback
  1407. $callback .= "&l_$item=Y";
  1408. $href .= "&l_$item=Y";
  1409. }
  1410. }
  1411. if ( $form->{l_subtotal} eq 'Y' ) {
  1412. $callback .= "&l_subtotal=Y";
  1413. $href .= "&l_subtotal=Y";
  1414. }
  1415. if ( $form->{department} ) {
  1416. ($department) = split /--/, $form->{department};
  1417. push @options, $locale->text('Department: [_1]', $department);
  1418. }
  1419. # if there are any dates
  1420. my $fromdate;
  1421. my $todate;
  1422. if ( $form->{fromdate} || $form->{todate} ) {
  1423. if ( $form->{fromdate} ) {
  1424. $fromdate = $locale->date( \%myconfig, $form->{fromdate}, 1 );
  1425. }
  1426. if ( $form->{todate} ) {
  1427. $todate = $locale->date( \%myconfig, $form->{todate}, 1 );
  1428. }
  1429. $form->{period} = "$fromdate - $todate";
  1430. }
  1431. else {
  1432. $form->{period} =
  1433. $locale->date( \%myconfig, $form->current_date( \%myconfig ), 1 );
  1434. }
  1435. my $name;
  1436. my $invoice;
  1437. my $arap;
  1438. if ( $form->{db} eq 'ar' ) {
  1439. $name = $locale->text('Customer');
  1440. $invoice = 'is.pl';
  1441. $arap = 'ar.pl';
  1442. }
  1443. if ( $form->{db} eq 'ap' ) {
  1444. $name = $locale->text('Vendor');
  1445. $invoice = 'ir.pl';
  1446. $arap = 'ap.pl';
  1447. }
  1448. push @options, $form->{period};
  1449. my %column_header;
  1450. $column_header{id} = {
  1451. href => "$href&sort=id",
  1452. text => $locale->text('ID'),
  1453. };
  1454. $column_header{invnumber} = {
  1455. href => "$href&sort=invnumber",
  1456. text => $locale->text('Invoice')
  1457. };
  1458. $column_header{transdate} = {
  1459. href => "$href&sort=transdate",
  1460. text => $locale->text('Date'),
  1461. };
  1462. $column_header{netamount} = $locale->text('Amount');
  1463. $column_header{tax} = $locale->text('Tax');
  1464. $column_header{total} = $locale->text('Total');
  1465. $column_header{name} = {
  1466. href => "$href&sort=name",
  1467. text => $name,
  1468. };
  1469. $column_header{description} = {
  1470. href => "$href&sort=description",
  1471. text => $locale->text('Description'),
  1472. };
  1473. # add sort and escape callback
  1474. $callback = $form->escape( $callback . "&sort=$form->{sort}" );
  1475. my $sameitem;
  1476. if ( @{ $form->{TR} } ) {
  1477. $sameitem = $form->{TR}->[0]->{ $form->{sort} };
  1478. }
  1479. my $totalnetamount;
  1480. my @rows;
  1481. my $i;
  1482. foreach my $ref ( @{ $form->{TR} } ) {
  1483. my %column_data;
  1484. my $module = ( $ref->{invoice} ) ? $invoice : $arap;
  1485. $module = 'ps.pl' if $ref->{till};
  1486. if ( $form->{l_subtotal} eq 'Y' ) {
  1487. if ( $sameitem ne $ref->{ $form->{sort} } ) {
  1488. push @rows, &tax_subtotal(\@column_index);
  1489. $sameitem = $ref->{ $form->{sort} };
  1490. }
  1491. }
  1492. $totalnetamount += $ref->{netamount};
  1493. $totaltax += $ref->{tax};
  1494. $ref->{total} = $ref->{netamount} + $ref->{tax};
  1495. $subtotalnetamount += $ref->{netamount};
  1496. $subtotaltax += $ref->{tax};
  1497. for (qw(netamount tax total)) {
  1498. $ref->{$_} =
  1499. $form->format_amount( \%myconfig, $ref->{$_}, 2, ' ' );
  1500. }
  1501. $column_data{id} = $ref->{id};
  1502. $column_data{invnumber} = {
  1503. href => "$module?path=$form->{path}&action=edit&id=$ref->{id}&login=$form->{login}&sessionid=$form->{sessionid}&callback=$callback",
  1504. text => $ref->{invnumber},
  1505. };
  1506. for (qw(id transdate name partnumber description)) {
  1507. $column_data{$_} = $ref->{$_};
  1508. }
  1509. for (qw(netamount tax total)) {
  1510. $column_data{$_} = $ref->{$_};
  1511. }
  1512. $i++;
  1513. $i %= 2;
  1514. $column_data{i} = $i;
  1515. push @rows, \%column_data;
  1516. }
  1517. if ( $form->{l_subtotal} eq 'Y' ) {
  1518. push @rows, &tax_subtotal(\@column_index);
  1519. }
  1520. my %column_data;
  1521. for (@column_index) { $column_data{$_} = ' ' }
  1522. $total = $form->format_amount( \%myconfig, $totalnetamount + $totaltax,
  1523. 2, ' ' );
  1524. $totalnetamount =
  1525. $form->format_amount( \%myconfig, $totalnetamount, 2, ' ' );
  1526. $totaltax = $form->format_amount( \%myconfig, $totaltax, 2, ' ' );
  1527. $column_data{netamount} = $totalnetamount;
  1528. $column_data{tax} = $totaltax;
  1529. $column_data{total} = $total;
  1530. my $template = LedgerSMB::Template->new_UI(
  1531. user => \%myconfig,
  1532. locale => $locale,
  1533. template => 'form-dynatable',
  1534. );
  1535. $template->render({
  1536. form => $form,
  1537. hiddens => \%hiddens,
  1538. options => \@options,
  1539. columns => \@column_index,
  1540. heading => \%column_header,
  1541. rows => \@rows,
  1542. totals => \%column_data,
  1543. row_alignment => {
  1544. netamount => 'right',
  1545. tax => 'right',
  1546. total => 'right',
  1547. },
  1548. });
  1549. }
  1550. sub tax_subtotal {
  1551. my $column_index = shift;
  1552. my %column_data;
  1553. for (@{$column_index}) { $column_data{$_} = ' ' }
  1554. #SC: Yes, right now these are global, inherited from generate_tax_report
  1555. $subtotal =
  1556. $form->format_amount( \%myconfig, $subtotalnetamount + $subtotaltax,
  1557. 2, ' ' );
  1558. $subtotalnetamount =
  1559. $form->format_amount( \%myconfig, $subtotalnetamount, 2, ' ' );
  1560. $subtotaltax =
  1561. $form->format_amount( \%myconfig, $subtotaltax, 2, ' ' );
  1562. $column_data{netamount} = {
  1563. class => 'subtotal',
  1564. text => $subtotalnetamount,
  1565. };
  1566. $column_data{tax} = {
  1567. class => 'subtotal',
  1568. text => $subtotaltax,
  1569. };
  1570. $column_data{total} = {
  1571. class => 'subtotal',
  1572. text => $subtotal,
  1573. };
  1574. $subtotalnetamount = 0;
  1575. $subtotaltax = 0;
  1576. \%column_data;
  1577. }
  1578. sub list_payments {
  1579. my %hiddens;
  1580. my @options;
  1581. my $vc = ($form->{db} eq 'ar') ? 'Customer' : 'Vendor';
  1582. if ( $form->{account} ) {
  1583. ( $form->{paymentaccounts} ) = split /--/, $form->{account};
  1584. }
  1585. if ( $form->{department} ) {
  1586. ( $department, $form->{department_id} ) = split /--/,
  1587. $form->{department};
  1588. push @options, $locale->text('Department: [_1]', $department);
  1589. }
  1590. RP->payments( \%myconfig, \%$form );
  1591. my @columns = $form->sort_columns(qw(meta_number transdate name paid source
  1592. memo));
  1593. if ( $form->{till} ) {
  1594. @columns =
  1595. $form->sort_columns(qw(transdate name paid curr source meta_number till));
  1596. if ( $myconfig{role} ne 'user' ) {
  1597. @columns =
  1598. $form->sort_columns(
  1599. qw(transdate name paid curr source till employee));
  1600. }
  1601. }
  1602. # construct href
  1603. my $title = $form->escape( $form->{title} );
  1604. $form->{paymentaccounts} =~ s/ /%20/g;
  1605. my $href =
  1606. "$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="
  1607. . $form->escape( $form->{title} );
  1608. $form->sort_order();
  1609. $form->{callback} =
  1610. "$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="
  1611. . $form->escape( $form->{title}, 1 );
  1612. my $callback;
  1613. if ( $form->{account} ) {
  1614. $callback .= "&account=" . $form->escape( $form->{account}, 1 );
  1615. $href .= "&account=" . $form->escape( $form->{account} );
  1616. push @options, $locale->text('Account: [_1]', $form->{account});
  1617. }
  1618. if ( $form->{department} ) {
  1619. $callback .= "&department=" . $form->escape( $form->{department}, 1 );
  1620. $href .= "&department=" . $form->escape( $form->{department} );
  1621. push @options, $locale->text('Department: [_1]', $form->{department});
  1622. }
  1623. if ( $form->{description} ) {
  1624. $callback .= "&description=" . $form->escape( $form->{description}, 1 );
  1625. $href .= "&description=" . $form->escape( $form->{description} );
  1626. push @options, $locale->text('Description: [_1]', $form->{description});
  1627. }
  1628. if ( $form->{source} ) {
  1629. $callback .= "&source=" . $form->escape( $form->{source}, 1 );
  1630. $href .= "&source=" . $form->escape( $form->{source} );
  1631. push @options, $locale->text('Source: [_1]', $form->{source});
  1632. }
  1633. if ( $form->{memo} ) {
  1634. $callback .= "&memo=" . $form->escape( $form->{memo}, 1 );
  1635. $href .= "&memo=" . $form->escape( $form->{memo} );
  1636. push @options, $locale->text('Memo: [_1]', $form->{memo});
  1637. }
  1638. if ( $form->{fromdate} ) {
  1639. push @options,
  1640. $locale->text('From [_1]',
  1641. $locale->date( \%myconfig, $form->{fromdate}, 1 ));
  1642. }
  1643. if ( $form->{todate} ) {
  1644. push @options,
  1645. $locale->text('To [_1]',
  1646. $locale->date( \%myconfig, $form->{todate}, 1 ));
  1647. }
  1648. $callback = $form->escape( $form->{callback} );
  1649. my %column_header;
  1650. $column_header{name} = {
  1651. href => "$href&sort=name",
  1652. text => $locale->text('Description'),
  1653. };
  1654. $column_header{transdate} = {
  1655. href => "$href&sort=transdate",
  1656. text => $locale->text('Date'),
  1657. };
  1658. $column_header{paid} = $locale->text('Amount');
  1659. $column_header{curr} = $locale->text('Curr');
  1660. $column_header{source} = {
  1661. href => "$href&sort=source",
  1662. text => $locale->text('Source'),
  1663. };
  1664. $column_header{meta_number} = {
  1665. href => "$href&sort=meta_number",
  1666. text => $locale->text("$vc Number"),
  1667. };
  1668. $column_header{employee} = {
  1669. href => "$href&sort=employee",
  1670. text => $locale->text('Salesperson'),
  1671. };
  1672. $column_header{till} = {
  1673. href => "$href&sort=till",
  1674. text => $locale->text('Till'),
  1675. };
  1676. my @column_index = @columns;
  1677. my @accounts;
  1678. my $i;
  1679. foreach my $ref ( sort { $a->{accno} cmp $b->{accno} } @{ $form->{PR} } ) {
  1680. next unless @{ $form->{ $ref->{id} } };
  1681. push @accounts, {header => "$ref->{accno}--$ref->{description}"};
  1682. if ( @{ $form->{ $ref->{id} } } ) {
  1683. $sameitem = $form->{ $ref->{id} }[0]->{ $form->{sort} };
  1684. }
  1685. my @rows;
  1686. foreach my $payment ( @{ $form->{ $ref->{id} } } ) {
  1687. if ( $form->{l_subtotal} ) {
  1688. if ( $payment->{ $form->{sort} } ne $sameitem ) {
  1689. # print subtotal
  1690. push @rows, &payment_subtotal(\@column_index);
  1691. }
  1692. }
  1693. next if ( $form->{till} && !$payment->{till} );
  1694. my %column_data;
  1695. $column_data{meta_number} = $payment->{meta_number};
  1696. $column_data{name} = $payment->{name};
  1697. $column_data{transdate} = $payment->{transdate};
  1698. $column_data{paid} =
  1699. $form->format_amount(\%myconfig, $payment->{paid}, 2, ' ');
  1700. $column_data{curr} = $payment->{curr};
  1701. $column_data{source} = $payment->{source};
  1702. $column_data{memo} = $payment->{memo};
  1703. $column_data{employee} = $payment->{employee};
  1704. $column_data{till} = $payment->{till};
  1705. $subtotalpaid += $payment->{paid};
  1706. $accounttotalpaid += $payment->{paid};
  1707. $totalpaid += $payment->{paid};
  1708. $i++;
  1709. $i %= 2;
  1710. $column_data{i} = $i;
  1711. push @rows, \%column_data;
  1712. $sameitem = $payment->{ $form->{sort} };
  1713. }
  1714. push @rows, &payment_subtotal(\@column_index) if $form->{l_subtotal};
  1715. $accounts[$#accounts]{rows} = \@rows;
  1716. # print account totals
  1717. my %column_data;
  1718. for (@column_index) { $column_data{$_} = ' ' }
  1719. $column_data{paid} =
  1720. $form->format_amount( \%myconfig, $accounttotalpaid, 2, ' ' );
  1721. $accounts[$#accounts]{totals} = \%column_data;
  1722. $accounttotalpaid = 0;
  1723. }
  1724. # prepare total
  1725. my %column_data;
  1726. for (@column_index) { $column_data{$_} = ' ' }
  1727. $column_data{paid} = $form->format_amount( \%myconfig, $totalpaid, 2, ' ' );
  1728. ##SC: Temporary removal
  1729. ## if ( $form->{lynx} ) {
  1730. ## require "bin/menu.pl";
  1731. ## &menubar;
  1732. ## }
  1733. my $template = LedgerSMB::Template->new_UI(
  1734. user => \%myconfig,
  1735. locale => $locale,
  1736. template => 'rp-payments',
  1737. );
  1738. $template->render({
  1739. form => $form,
  1740. hiddens => \%hiddens,
  1741. options => \@options,
  1742. columns => \@column_index,
  1743. heading => \%column_header,
  1744. accounts => \@accounts,
  1745. totals => \%column_data,
  1746. row_alignment => {
  1747. paid => 'right',
  1748. },
  1749. });
  1750. }
  1751. sub payment_subtotal {
  1752. my $column_index = shift;
  1753. my %column_data;
  1754. if ( $subtotalpaid != 0 ) {
  1755. for (@column_index) { $column_data{$_} = ' ' }
  1756. $column_data{paid} =
  1757. $form->format_amount( \%myconfig, $subtotalpaid, 2, ' ' );
  1758. $column_data{class} = 'subtotal';
  1759. }
  1760. $subtotalpaid = 0;
  1761. \%column_data;
  1762. }