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