1#=====================================================================
2# LedgerSMB Small Medium Business Accounting
3# http://www.ledgersmb.org/
4#
5
6# Copyright (C) 2006
7# This work contains copyrighted information from a number of sources all used
8# with permission.
9#
10# This file contains source code included with or based on SQL-Ledger which
11# is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
12# under the GNU General Public License version 2 or, at your option, any later
13# version.  For a full list including contact information of contributors,
14# maintainers, and copyright holders, see the CONTRIBUTORS file.
15#
16# Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
17# Copyright (c) 2001
18#
19#  Author: DWS Systems Inc.
20#     Web: http://www.sql-ledger.org
21#
22#  Contributors: Antonio Gallardo <agssa@ibw.com.ni>
23#                Benjamin Lee <benjaminlee@consultant.com>
24#
25#
26# This program is free software; you can redistribute it and/or modify
27# it under the terms of the GNU General Public License as published by
28# the Free Software Foundation; either version 2 of the License, or
29# (at your option) any later version.
30#
31# This program is distributed in the hope that it will be useful,
32# but WITHOUT ANY WARRANTY; without even the implied warranty of
33# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34# GNU General Public License for more details.
35# You should have received a copy of the GNU General Public License
36# along with this program; if not, write to the Free Software
37# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
38#======================================================================
39#
40# module for preparing Income Statement and Balance Sheet
41#
42#======================================================================
43
44require "bin/arap.pl";
45
46use LedgerSMB::PE;
47use LedgerSMB::RP;
48
491;
50
51# end of main
52
53# this is for our long dates
54# $locale->text('January')
55# $locale->text('February')
56# $locale->text('March')
57# $locale->text('April')
58# $locale->text('May ')
59# $locale->text('June')
60# $locale->text('July')
61# $locale->text('August')
62# $locale->text('September')
63# $locale->text('October')
64# $locale->text('November')
65# $locale->text('December')
66
67# this is for our short month
68# $locale->text('Jan')
69# $locale->text('Feb')
70# $locale->text('Mar')
71# $locale->text('Apr')
72# $locale->text('May')
73# $locale->text('Jun')
74# $locale->text('Jul')
75# $locale->text('Aug')
76# $locale->text('Sep')
77# $locale->text('Oct')
78# $locale->text('Nov')
79# $locale->text('Dec')
80
81# $locale->text('Balance Sheet')
82# $locale->text('Income Statement')
83# $locale->text('Trial Balance')
84# $locale->text('AR Aging')
85# $locale->text('AP Aging')
86# $locale->text('Tax collected')
87# $locale->text('Tax paid')
88# $locale->text('Receipts')
89# $locale->text('Payments')
90# $locale->text('Project Transactions')
91# $locale->text('Non-taxable Sales')
92# $locale->text('Non-taxable Purchases')
93
94sub report {
95
96    %report = (
97        balance_sheet    => { title => 'Balance Sheet' },
98        income_statement => { title => 'Income Statement' },
99        trial_balance    => { title => 'Trial Balance' },
100        ar_aging         => { title => 'AR Aging', vc => 'customer' },
101        ap_aging         => { title => 'AP Aging', vc => 'vendor' },
102        tax_collected    => { title => 'Tax collected', vc => 'customer' },
103        tax_paid         => { title => 'Tax paid' },
104        nontaxable_sales => { title => 'Non-taxable Sales', vc => 'customer' },
105        nontaxable_purchases => { title => 'Non-taxable Purchases' },
106        receipts             => { title => 'Receipts', vc => 'customer' },
107        payments             => { title => 'Payments' },
108        projects             => { title => 'Project Transactions' },
109        inv_activity         => { title => 'Inventory Activity' },
110    );
111
112    $form->{title} = $locale->text( $report{ $form->{report} }->{title} );
113
114    $gifi = qq|
115<tr>
116  <th align=right>| . $locale->text('Accounts') . qq|</th>
117  <td><input name=accounttype class=radio type=radio value=standard checked> |
118      . $locale->text('Standard') . qq|
119
120      <input name=accounttype class=radio type=radio value=gifi> |
121      . $locale->text('GIFI') . qq|
122  </td>
123</tr>
124|;
125
126    # get departments
127    $form->all_departments( \%myconfig, undef,
128        $report{ $form->{report} }->{vc} );
129    if ( @{ $form->{all_department} } ) {
130        $form->{selectdepartment} = "<option>\n";
131
132        for ( @{ $form->{all_department} } ) {
133            $form->{selectdepartment} .=
134qq|<option value="$_->{description}--$_->{id}">$_->{description}\n|;
135        }
136    }
137
138    $department = qq|
139	<tr>
140	  <th align=right nowrap>| . $locale->text('Department') . qq|</th>
141	  <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
142	</tr>
143| if $form->{selectdepartment};
144
145    if ( @{ $form->{all_years} } ) {
146
147        # accounting years
148        $form->{selectaccountingyear} = "<option>\n";
149        for ( @{ $form->{all_years} } ) {
150            $form->{selectaccountingyear} .= qq|<option>$_\n|;
151        }
152
153        $form->{selectaccountingmonth} = "<option>\n";
154        for ( sort keys %{ $form->{all_month} } ) {
155            $form->{selectaccountingmonth} .=
156              qq|<option value=$_>|
157              . $locale->text( $form->{all_month}{$_} ) . qq|\n|;
158        }
159
160        $selectfrom = qq|
161        <tr>
162	  <th align=right>| . $locale->text('Period') . qq|</th>
163	  <td colspan=3>
164	  <select name=month>$form->{selectaccountingmonth}</select>
165	  <select name=year>$form->{selectaccountingyear}</select>
166	  <input name=interval class=radio type=radio value=0 checked>&nbsp;|
167          . $locale->text('Current') . qq|
168	  <input name=interval class=radio type=radio value=1>&nbsp;|
169          . $locale->text('Month') . qq|
170	  <input name=interval class=radio type=radio value=3>&nbsp;|
171          . $locale->text('Quarter') . qq|
172	  <input name=interval class=radio type=radio value=12>&nbsp;|
173          . $locale->text('Year') . qq|
174	  </td>
175	</tr>
176|;
177
178        $selectto = qq|
179        <tr>
180	  <th align=right></th>
181	  <td>
182	  <select name=month>$form->{selectaccountingmonth}</select>
183	  <select name=year>$form->{selectaccountingyear}</select>
184	  </td>
185	</tr>
186|;
187    }
188
189    $summary = qq|
190	<tr>
191	  <th></th>
192	  <td><input name=summary type=radio class=radio value=1 checked> |
193      . $locale->text('Summary') . qq|
194	  <input name=summary type=radio class=radio value=0> |
195      . $locale->text('Detail') . qq|
196	  </td>
197	</tr>
198|;
199
200    # get projects
201    $form->all_projects( \%myconfig );
202    if ( @{ $form->{all_project} } ) {
203        $form->{selectproject} = "<option>\n";
204        for ( @{ $form->{all_project} } ) {
205            $form->{selectproject} .=
206qq|<option value="$_->{projectnumber}--$_->{id}">$_->{projectnumber}\n|;
207        }
208
209        $project = qq|
210	<tr>
211	  <th align=right nowrap>| . $locale->text('Project') . qq|</th>
212	  <td colspan=3><select name=projectnumber>$form->{selectproject}</select></td>
213	</tr>|;
214
215    }
216
217    $form->header;
218
219    print qq|
220<body>
221
222<form method=post action=$form->{script}>
223
224<input type=hidden name=title value="$form->{title}">
225
226<table width=100%>
227  <tr>
228    <th class=listtop>$form->{title}</th>
229  </tr>
230  <tr height="5"></tr>
231  <tr>
232    <td>
233      <table>
234      $department
235|;
236
237    if ( $form->{report} eq "projects" ) {
238        print qq|
239        $project
240        <input type=hidden name=nextsub value=generate_projects>
241        <tr>
242	  <th align=right>| . $locale->text('From') . qq|</th>
243	  <td><input name=fromdate size=11 title="$myconfig{dateformat}" value="$form->{fromdate}"></td>
244	  <th align=right>| . $locale->text('To') . qq|</th>
245	  <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
246	</tr>
247	$selectfrom
248      </table>
249    </td>
250  </tr>
251  <tr>
252    <td>
253      <table>
254	<tr>
255	  <th align=right nowrap>| . $locale->text('Include in Report') . qq|</th>
256	  <td><input name=l_heading class=checkbox type=checkbox value=Y>&nbsp;|
257          . $locale->text('Heading') . qq|
258	  <input name=l_subtotal class=checkbox type=checkbox value=Y>&nbsp;|
259          . $locale->text('Subtotal')
260          . qq|</td>
261	</tr>
262|;
263    }
264
265    if ( $form->{report} eq "inv_activity" ) {
266        $gifi = '';
267        print qq|
268       <input type=hidden name=nextsub value=generate_inv_activity>
269	<tr>
270	  <th align=right>| . $locale->text('From') . qq|</th>
271	  <td><input name=fromdate size=11 title="$myconfig{dateformat}" value="$form->{fromdate}"></td>
272	  <th align=right>| . $locale->text('To') . qq|</th>
273	  <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
274	</tr>
275       <tr>
276	  <th align=right>| . $locale->text('Period') . qq|</th>
277	  <td colspan=3>
278	  <select name=frommonth>$form->{selectaccountingmonth}</select>
279	  <select name=fromyear>$form->{selectaccountingyear}</select>
280	  <input name=interval class=radio type=radio value=0 checked>|
281          . $locale->text('Current') . qq|
282	  <input name=interval class=radio type=radio value=1>|
283          . $locale->text('Month') . qq|
284	  <input name=interval class=radio type=radio value=3>|
285          . $locale->text('Quarter') . qq|
286	  <input name=interval class=radio type=radio value=12>|
287          . $locale->text('Year') . qq|
288	  </td>
289	</tr>
290        </table>
291        <table>
292          <tr>
293            <th>| . $locale->text("Part Number") . qq|</th>
294            <td><input name=partnumber></td>
295            <th>| . $locale->text('Description') . qq|</th>
296            <td><input type=text name=description></td>
297          </tr>|;
298    }
299    if ( $form->{report} eq "income_statement" ) {
300        print qq|
301	$project
302        <input type=hidden name=nextsub value=generate_income_statement>
303	<tr>
304	  <th align=right>| . $locale->text('From') . qq|</th>
305	  <td><input name=fromdate size=11 title="$myconfig{dateformat}" value="$form->{fromdate}"></td>
306	  <th align=right>| . $locale->text('To') . qq|</th>
307	  <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
308	</tr>
309|;
310
311        if ($selectfrom) {
312            print qq|
313        <tr>
314	  <th align=right>| . $locale->text('Period') . qq|</th>
315	  <td colspan=3>
316	  <select name=frommonth>$form->{selectaccountingmonth}</select>
317	  <select name=fromyear>$form->{selectaccountingyear}</select>
318	  <input name=interval class=radio type=radio value=0 checked>&nbsp;|
319              . $locale->text('Current') . qq|
320	  <input name=interval class=radio type=radio value=1>&nbsp;|
321              . $locale->text('Month') . qq|
322	  <input name=interval class=radio type=radio value=3>&nbsp;|
323              . $locale->text('Quarter') . qq|
324	  <input name=interval class=radio type=radio value=12>&nbsp;|
325              . $locale->text('Year') . qq|
326	  </td>
327	</tr>
328|;
329        }
330
331        print qq|
332
333	<tr>
334	  <th align=right>| . $locale->text('Compare to') . qq|</th>
335	</tr>
336	<tr>
337	  <th align=right>| . $locale->text('From') . qq|</th>
338	  <td><input name=comparefromdate size=11 title="$myconfig{dateformat}"></td>
339	  <th align=right>| . $locale->text('To') . qq|</th>
340	  <td><input name=comparetodate size=11 title="$myconfig{dateformat}"></td>
341	</tr>
342|;
343
344        if ($selectto) {
345            print qq|
346        <tr>
347	  <th align=right>| . $locale->text('Period') . qq|</th>
348	  <td>
349	  <select name=comparemonth>$form->{selectaccountingmonth}</select>
350	  <select name=compareyear>$form->{selectaccountingyear}</select>
351	  </td>
352	</tr>
353|;
354        }
355
356        print qq|
357	<tr>
358	  <th align=right>| . $locale->text('Decimalplaces') . qq|</th>
359	  <td><input name=decimalplaces size=3 value=2></td>
360	</tr>
361      </table>
362    </td>
363  </tr>
364  <tr>
365    <td>
366      <table>
367	<tr>
368	  <th align=right>| . $locale->text('Method') . qq|</th>
369	  <td colspan=3><input name=method class=radio type=radio value=accrual checked>|
370          . $locale->text('Accrual') . qq|
371	  &nbsp;<input name=method class=radio type=radio value=cash>|
372          . $locale->text('Cash')
373          . qq|</td>
374	</tr>
375
376	<tr>
377	  <th align=right nowrap>| . $locale->text('Include in Report') . qq|</th>
378	  <td colspan=3><input name=l_heading class=checkbox type=checkbox value=Y>&nbsp;|
379          . $locale->text('Heading') . qq|
380	  <input name=l_subtotal class=checkbox type=checkbox value=Y>&nbsp;|
381          . $locale->text('Subtotal') . qq|
382	  <input name=l_accno class=checkbox type=checkbox value=Y>&nbsp;|
383          . $locale->text('Account Number')
384          . qq|</td>
385	</tr>
386|;
387    }
388
389    if ( $form->{report} eq "balance_sheet" ) {
390        print qq|
391        <input type=hidden name=nextsub value=generate_balance_sheet>
392	<tr>
393	  <th align=right>| . $locale->text('as at') . qq|</th>
394	  <td><input name=asofdate size=11 title="$myconfig{dateformat}" value="$form->{asofdate}"></td>
395|;
396
397        if ($selectfrom) {
398            print qq|
399	  <td>
400	  <select name=asofmonth>$form->{selectaccountingmonth}</select>
401	  <select name=asofyear>$form->{selectaccountingyear}</select>
402	  </td>
403|;
404        }
405
406        print qq|
407	</tr>
408
409	  <th align=right nowrap>| . $locale->text('Compare to') . qq|</th>
410	  <td><input name=compareasofdate size=11 title="$myconfig{dateformat}"></td>
411	  <td>
412|;
413
414        if ($selectto) {
415            print qq|
416	  <select name=compareasofmonth>$form->{selectaccountingmonth}</select>
417	  <select name=compareasofyear>$form->{selectaccountingyear}</select>
418	  </td>
419|;
420        }
421
422        print qq|
423	</tr>
424	<tr>
425	  <th align=right>| . $locale->text('Decimalplaces') . qq|</th>
426	  <td><input name=decimalplaces size=3 value=2></td>
427	</tr>
428      </table>
429    </td>
430  </tr>
431  <tr>
432    <td>
433      <table>
434	<tr>
435	  <th align=right>| . $locale->text('Method') . qq|</th>
436	  <td colspan=3><input name=method class=radio type=radio value=accrual checked>|
437          . $locale->text('Accrual') . qq|
438	  &nbsp;<input name=method class=radio type=radio value=cash>|
439          . $locale->text('Cash')
440          . qq|</td>
441	</tr>
442
443	<tr>
444	  <th align=right nowrap>| . $locale->text('Include in Report') . qq|</th>
445	  <td><input name=l_heading class=checkbox type=checkbox value=Y>&nbsp;|
446          . $locale->text('Heading') . qq|
447	  <input name=l_subtotal class=checkbox type=checkbox value=Y>&nbsp;|
448          . $locale->text('Subtotal') . qq|
449	  <input name=l_accno class=checkbox type=checkbox value=Y>&nbsp;|
450          . $locale->text('Account Number')
451          . qq|</td>
452	</tr>
453|;
454    }
455
456    if ( $form->{report} eq "trial_balance" ) {
457        print qq|
458        <input type=hidden name=nextsub value=generate_trial_balance>
459        <tr>
460	  <th align=right>| . $locale->text('From') . qq|</th>
461	  <td><input name=fromdate size=11 title="$myconfig{dateformat}" value="$form->{fromdate}"></td>
462	  <th align=right>| . $locale->text('To') . qq|</th>
463	  <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
464	</tr>
465	$selectfrom
466      </table>
467    </td>
468  </tr>
469  <tr>
470    <td>
471      <table>
472	<tr>
473	  <th align=right nowrap>| . $locale->text('Include in Report') . qq|</th>
474	  <td><input name=l_heading class=checkbox type=checkbox value=Y>&nbsp;|
475          . $locale->text('Heading') . qq|
476	  <input name=l_subtotal class=checkbox type=checkbox value=Y>&nbsp;|
477          . $locale->text('Subtotal') . qq|
478	  <input name=all_accounts class=checkbox type=checkbox value=Y>&nbsp;|
479          . $locale->text('All Accounts')
480          . qq|</td>
481	</tr>
482|;
483    }
484
485    if ( $form->{report} =~ /^tax_/ ) {
486        $gifi = "";
487
488        $form->{db} = ( $form->{report} =~ /_collected/ ) ? "ar" : "ap";
489
490        RP->get_taxaccounts( \%myconfig, \%$form );
491
492        print qq|
493        <input type=hidden name=nextsub value=generate_tax_report>
494	<tr>
495	  <th align=right>| . $locale->text('From') . qq|</th>
496	  <td><input name=fromdate size=11 title="$myconfig{dateformat}" value="$form->{fromdate}"></td>
497	  <th align=right>| . $locale->text('To') . qq|</th>
498	  <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
499	</tr>
500	$selectfrom
501	$summary
502	<tr>
503	  <th align=right>| . $locale->text('Report for') . qq|</th>
504	  <td colspan=3>
505|;
506
507        $checked = "checked";
508        foreach $ref ( @{ $form->{taxaccounts} } ) {
509
510            print
511qq|<input name=accno class=radio type=radio value=$ref->{accno} $checked>&nbsp;$ref->{description}
512
513    <input name="$ref->{accno}_description" type=hidden value="$ref->{description}">
514    <input name="$ref->{accno}_rate" type=hidden value="$ref->{rate}">|;
515
516            $checked = "";
517
518        }
519
520        print qq|
521  <input type=hidden name=db value="$form->{db}">
522  <input type=hidden name=sort value=transdate>
523
524	  </td>
525	</tr>
526|;
527
528        if ( @{ $form->{gifi_taxaccounts} } ) {
529            print qq|
530        <tr>
531	  <th align=right>| . $locale->text('GIFI') . qq|</th>
532	  <td colspan=3>
533|;
534
535            foreach $ref ( @{ $form->{gifi_taxaccounts} } ) {
536
537                print
538qq|<input name=accno class=radio type=radio value="gifi_$ref->{accno}">&nbsp;$ref->{description}
539
540      <input name="gifi_$ref->{accno}_description" type=hidden value="$ref->{description}">
541      <input name="gifi_$ref->{accno}_rate" type=hidden value="$ref->{rate}">|;
542
543            }
544
545            print qq|
546	  </td>
547	</tr>
548|;
549        }
550
551        print qq|
552	<tr>
553	  <th align=right>| . $locale->text('Method') . qq|</th>
554	  <td colspan=3><input name=method class=radio type=radio value=accrual checked>|
555          . $locale->text('Accrual') . qq|
556	  &nbsp;<input name=method class=radio type=radio value=cash>|
557          . $locale->text('Cash')
558          . qq|</td>
559	</tr>
560      </table>
561    </td>
562  </tr>
563  <tr>
564    <td>
565      <table>
566        <tr>
567	  <th align=right>| . $locale->text('Include in Report') . qq|</th>
568	  <td>
569	    <table>
570	      <tr>
571		<td><input name="l_id" class=checkbox type=checkbox value=Y></td>
572		<td>| . $locale->text('ID') . qq|</td>
573		<td><input name="l_invnumber" class=checkbox type=checkbox value=Y checked></td>
574		<td>| . $locale->text('Invoice') . qq|</td>
575		<td><input name="l_transdate" class=checkbox type=checkbox value=Y checked></td>
576		<td>| . $locale->text('Date') . qq|</td>
577	      </tr>
578	      <tr>
579		<td><input name="l_name" class=checkbox type=checkbox value=Y checked></td>
580|;
581
582        if ( $form->{db} eq 'ar' ) {
583            print qq|<td>| . $locale->text('Customer') . qq|</td>|;
584        }
585        if ( $form->{db} eq 'ap' ) {
586            print qq|<td>| . $locale->text('Vendor') . qq|</td>|;
587        }
588
589        print qq|
590  	        <td><input name="l_description" class=checkbox type=checkbox value=Y checked></td>
591		<td>| . $locale->text('Description') . qq|</td>
592		<td><input name="l_netamount" class=checkbox type=checkbox value=Y checked></td>
593		<td>| . $locale->text('Amount') . qq|</td>
594
595		<td><input name="l_tax" class=checkbox type=checkbox value=Y checked></td>
596		<td>| . $locale->text('Tax') . qq|</td>
597
598                <td><input name="l_total" class=checkbox type=checkbox value=Y checked></td>
599		<td>| . $locale->text('Total') . qq|</td>
600	      </tr>
601	      <tr>
602	      </tr>
603	      <tr>
604	        <td><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
605		<td>| . $locale->text('Subtotal') . qq|</td>
606	      </tr>
607	    </table>
608	  </td>
609	</tr>
610|;
611
612    }
613
614    if ( $form->{report} =~ /^nontaxable_/ ) {
615        $gifi = "";
616
617        $form->{db} = ( $form->{report} =~ /_sales/ ) ? "ar" : "ap";
618
619        print qq|
620        <input type=hidden name=nextsub value=generate_tax_report>
621
622        <input type=hidden name=db value="$form->{db}">
623        <input type=hidden name=sort value=transdate>
624        <input type=hidden name=report value="$form->{report}">
625
626	<tr>
627	  <th align=right>| . $locale->text('From') . qq|</th>
628	  <td><input name=fromdate size=11 title="$myconfig{dateformat}" value="$form->{fromdate}"></td>
629	  <th align=right>| . $locale->text('To') . qq|</th>
630	  <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
631	</tr>
632	$selectfrom
633	$summary
634	<tr>
635	  <th align=right>| . $locale->text('Method') . qq|</th>
636	  <td colspan=3><input name=method class=radio type=radio value=accrual checked>|
637          . $locale->text('Accrual') . qq|
638	  &nbsp;<input name=method class=radio type=radio value=cash>|
639          . $locale->text('Cash')
640          . qq|</td>
641	</tr>
642        <tr>
643	  <th align=right>| . $locale->text('Include in Report') . qq|</th>
644	  <td colspan=3>
645	    <table>
646	      <tr>
647		<td><input name="l_id" class=checkbox type=checkbox value=Y></td>
648		<td>| . $locale->text('ID') . qq|</td>
649		<td><input name="l_invnumber" class=checkbox type=checkbox value=Y checked></td>
650		<td>| . $locale->text('Invoice') . qq|</td>
651		<td><input name="l_transdate" class=checkbox type=checkbox value=Y checked></td>
652		<td>| . $locale->text('Date') . qq|</td>
653	      </tr>
654	      <tr>
655		<td><input name="l_name" class=checkbox type=checkbox value=Y checked></td>
656|;
657
658        if ( $form->{db} eq 'ar' ) {
659            print qq|<td>| . $locale->text('Customer') . qq|</td>|;
660        }
661        if ( $form->{db} eq 'ap' ) {
662            print qq|<td>| . $locale->text('Vendor') . qq|</td>|;
663        }
664
665        print qq|
666	        <td><input name="l_description" class=checkbox type=checkbox value=Y checked></td>
667		<td>| . $locale->text('Description') . qq|</td>
668                <td><input name="l_netamount" class=checkbox type=checkbox value=Y checked></td>
669		<td>| . $locale->text('Amount') . qq|</td>
670	      </tr>
671	      <tr>
672	        <td><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
673		<td>| . $locale->text('Subtotal') . qq|</td>
674	      </tr>
675	    </table>
676	  </td>
677	</tr>
678|;
679
680    }
681
682    if (   ( $form->{report} eq "ar_aging" )
683        || ( $form->{report} eq "ap_aging" ) )
684    {
685        $gifi = "";
686
687        if ( $form->{report} eq 'ar_aging' ) {
688            $label = $locale->text('Customer');
689            $form->{vc} = 'customer';
690        }
691        else {
692            $label = $locale->text('Vendor');
693            $form->{vc} = 'vendor';
694        }
695
696        $nextsub = "generate_$form->{report}";
697
698        # setup vc selection
699        $form->all_vc( \%myconfig, $form->{vc},
700            ( $form->{vc} eq 'customer' ) ? "AR" : "AP" );
701
702        for ( @{ $form->{"all_$form->{vc}"} } ) {
703            $vc .= qq|<option value="$_->{name}--$_->{id}">$_->{name}\n|;
704        }
705
706        $vc =
707          ($vc)
708          ? qq|<select name=$form->{vc}><option>\n$vc</select>|
709          : qq|<input name=$form->{vc} size=35>|;
710
711        $postscript = "postscript" if $myconfig{printer};
712
713        print qq|
714	<tr>
715	  <th align=right>| . $locale->text($label) . qq|</th>
716	  <td>$vc</td>
717	</tr>
718	<tr>
719	  <th align=right>| . $locale->text('To') . qq|</th>
720	  <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
721	</tr>
722	$selectto
723        <input type=hidden name=type value=statement>
724        <input type=hidden name=format value="$postscript">
725	<input type=hidden name=media value="$myconfig{printer}">
726
727	<input type=hidden name=nextsub value="$nextsub">
728	<input type=hidden name=action value="$nextsub">
729	$summary
730	<tr>
731	  <table>
732	    <tr>
733	      <th>| . $locale->text('Include in Report') . qq|</th>
734
735	      <td>
736	        <table>
737		  <tr>
738	            <td nowrap><input name=overdue type=radio class=radio value=0 checked> |
739          . $locale->text('Aged')
740          . qq|</td>
741 	            <td nowrap><input name=overdue type=radio class=radio value=1> |
742          . $locale->text('Overdue')
743          . qq|</td>
744	          </tr>
745		  <tr>
746		    <td nowrap width=70><input name=c0 type=checkbox class=checkbox value=1 checked> |
747          . $locale->text('Current')
748          . qq|</td>
749		    <td nowrap width=70><input name=c30 type=checkbox class=checkbox value=1 checked> 30</td>
750		    <td nowrap width=70><input name=c60 type=checkbox class=checkbox value=1 checked> 60</td>
751		    <td nowrap width=70><input name=c90 type=checkbox class=checkbox value=1 checked> 90</td>
752		    </td>
753		  </tr>
754		</table>
755	      </td>
756	    </tr>
757	  </table>
758	</tr>
759
760|;
761    }
762
763    # above action can be removed if there is more than one input field
764
765    if ( $form->{report} =~ /(receipts|payments)$/ ) {
766        $gifi = "";
767
768        $form->{db} = ( $form->{report} =~ /payments$/ ) ? "ap" : "ar";
769
770        RP->paymentaccounts( \%myconfig, \%$form );
771
772        $selection = "<option>\n";
773        foreach $ref ( @{ $form->{PR} } ) {
774            $paymentaccounts .= "$ref->{accno} ";
775            $selection       .= "<option>$ref->{accno}--$ref->{description}\n";
776        }
777
778        chop $paymentaccounts;
779
780        print qq|
781        <input type=hidden name=nextsub value=list_payments>
782        <tr>
783	  <th align=right nowrap>| . $locale->text('Account') . qq|</th>
784          <td colspan=3><select name=account>$selection</select>
785	    <input type=hidden name=paymentaccounts value="$paymentaccounts">
786	  </td>
787	</tr>
788        <tr>
789	  <th align=right nowrap>| . $locale->text('Description') . qq|</th>
790          <td colspan=3><input name=description size=35></td>
791	</tr>
792        <tr>
793	  <th align=right nowrap>| . $locale->text('Source') . qq|</th>
794          <td colspan=3><input name=source></td>
795	</tr>
796        <tr>
797	  <th align=right nowrap>| . $locale->text('Memo') . qq|</th>
798          <td colspan=3><input name=memo size=30></td>
799	</tr>
800	<tr>
801	  <th align=right>| . $locale->text('From') . qq|</th>
802	  <td><input name=fromdate size=11 title="$myconfig{dateformat}" value="$form->{fromdate}"></td>
803	  <th align=right>| . $locale->text('To') . qq|</th>
804	  <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
805	</tr>
806	$selectfrom
807        <tr>
808	  <td align=right><input type=checkbox class=checkbox name=fx_transaction value=1 checked></td>
809	  <td colspan=3>|
810          . $locale->text('Include Exchange Rate Difference')
811          . qq|</td>
812	</tr>
813        <tr>
814	  <td align=right><input name=l_subtotal class=checkbox type=checkbox value=Y></td>
815	  <td align=left colspan=3>| . $locale->text('Subtotal') . qq|</th>
816	</tr>
817
818	  <input type=hidden name=db value="$form->{db}">
819	  <input type=hidden name=sort value=transdate>
820|;
821
822    }
823
824    print qq|
825
826$gifi
827
828      </table>
829    </td>
830  </tr>
831  <tr>
832    <td><hr size=3 noshade></td>
833  </tr>
834</table>
835
836<br>
837<input type="hidden" name="path" value="$form->{path}">
838<input type="hidden" name="login" value="$form->{login}">
839<input type="hidden" name="sessionid" value="$form->{sessionid}">
840
841<button type="submit" class="submit" name="action" value="continue">|
842      . $locale->text('Continue')
843      . qq|</button>
844
845</form>
846|;
847
848    if ( $form->{lynx} ) {
849        require "bin/menu.pl";
850        &menubar;
851    }
852
853    print qq|
854
855</body>
856</html>
857|;
858
859}
860
861sub continue { &{ $form->{nextsub} } }
862
863sub generate_inv_activity {
864    $form->header;
865
866    RP->inventory_activity( \%myconfig, \%$form );
867
868    $title = $form->escape( $form->{title} );
869
870    #  if ($form->{department}) {
871    #    ($department) = split /--/, $form->{department};
872    #    $options = $locale->text('Department')." : $department<br>";
873    #    $department = $form->escape($form->{department});
874    #  }
875##  if ($form->{projectnumber}) {
876    #    ($projectnumber) = split /--/, $form->{projectnumber};
877    #    $options .= $locale->text('Project Number')." : $projectnumber<br>";
878    #    $projectnumber = $form->escape($form->{projectnumber});
879    #  }
880
881    # if there are any dates
882    if ( $form->{fromdate} || $form->{todate} ) {
883        if ( $form->{fromdate} ) {
884            $fromdate = $locale->date( \%myconfig, $form->{fromdate}, 1 );
885        }
886        if ( $form->{todate} ) {
887            $todate = $locale->date( \%myconfig, $form->{todate}, 1 );
888        }
889
890        $form->{period} = "$fromdate - $todate";
891    }
892    else {
893        $form->{period} =
894          $locale->date( \%myconfig, $form->current_date( \%myconfig ), 1 );
895
896    }
897    $options .= $form->{period};
898
899    @column_index = qw(partnumber description sold revenue received expense);
900
901    $href =
902qq|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}|;
903
904    $column_header{partnumber} = qq|
905	<th class=listheading><a class=listheading href="$href&sort_col=partnumber">|
906      . $locale->text('Part Number') . qq|</a></th>|;
907    $column_header{description} = qq|
908	<th class=listheading><a class=listheading href="$href&sort_col=description">|
909      . $locale->text('Description') . qq|</a></th>|;
910    $column_header{sold} = qq|
911	<th class=listheading><a class=listheading href="$href&sort_col=sold">|
912      . $locale->text('Sold') . qq|</a></th>|;
913    $column_header{revenue} = qq|
914	<th class=listheading><a class=listheading href="$href&sort_col=revenue">|
915      . $locale->text('Revenue') . qq|</a></th>|;
916    $column_header{received} = qq|
917	<th class=listheading><a class=listheading href="$href&sort_col=received">|
918      . $locale->text('Received') . qq|</a></th>|;
919    $column_header{expense} = qq|
920	<th class=listheading><a class=listheading href="$href&sort_col=expense">|
921      . $locale->text('Expense') . qq|</a></th>|;
922
923    print qq|
924<body>
925
926<table width=100%>
927  <tr>
928    <th class=listtop>$form->{title}</th>
929  </tr>
930  <tr height="5"></tr>
931  <tr>
932    <td>$options</td>
933  </tr>
934  <tr>
935    <td>
936      <table width=100%>
937	<tr>|;
938
939    map { print "$column_header{$_}\n" } @column_index;
940
941    print qq|
942        </tr>
943|;
944
945    if ( $form->{sort_col} eq 'qty' || $form->{sort_col} eq 'revenue' ) {
946        $form->{sort_type} = 'numeric';
947    }
948    $i    = 0;
949    $cols = "l_transdate=Y&l_name=Y&l_invnumber=Y&summary=1";
950    $dates =
951"transdatefrom=$form->{fromdate}&transdateto=$form->{todate}&year=$form->{fromyear}&month=$form->{frommonth}&interval=$form->{interval}";
952    $base =
953      "path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}";
954
955    $form->{callback} = "rp.pl?action=continue&$base";
956    $form->{callback} = $form->escape( $form->{callback} );
957    $callback         = "callback=$form->{callback}";
958
959    # sort the whole thing by account numbers and display
960    foreach $ref ( @{ $form->{TB} } ) {
961        $description = $form->escape( $ref->{description} );
962        $i           = $i % 2;
963
964        $pnumhref =
965          "ic.pl?action=edit&id=$ref->{id}&$base&callback=$form->{callback}";
966        $soldhref =
967"ar.pl?action=transactions&partsid=$ref->{id}&$base&$cols&$dates&$callback";
968        $rechref =
969"ap.pl?action=transactions&partsid=$ref->{id}&$base&$cols&$dates&callback=$form->{callback}";
970
971        $ml = ( $ref->{category} =~ /(A|E)/ ) ? -1 : 1;
972
973        $debit = $form->format_amount( \%myconfig, $ref->{debit}, 2, "&nbsp;" );
974        $credit =
975          $form->format_amount( \%myconfig, $ref->{credit}, 2, "&nbsp;" );
976        $begbalance =
977          $form->format_amount( \%myconfig, $ref->{balance} * $ml, 2,
978            "&nbsp;" );
979        $endbalance =
980          $form->format_amount( \%myconfig,
981            ( $ref->{balance} + $ref->{amount} ) * $ml,
982            2, "&nbsp;" );
983
984        $ref->{partnumber} = qq|<a href="$pnumhref">$ref->{partnumber}</a>|;
985        $ref->{sold}       = qq|<a href="$soldhref">$ref->{sold}</a>|;
986        $ref->{received}   = qq|<a href="$rechref">$ref->{received}<a/>|;
987        $ref->{revenue}    =
988          $form->format_amount( \%myconfig, $ref->{revenue}, 2, '&nbsp;' );
989        $ref->{expenses}   =
990          $form->format_amount( \%myconfig, $ref->{expenses}, 2, '&nbsp;' );
991        $ref->{expense}    = $ref->{expenses};
992        map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
993
994        print qq|
995      <tr class="listrow$i">
996      |;
997        for (@column_index) {
998            if ($_ eq 'description' or $_ eq 'partnumber') {
999                print '<td>';
1000            } else {
1001                print '<td align="right">';
1002            }
1003            print "$ref->{$_}</td>\n";
1004        }
1005        print qq|
1006      </tr>
1007|;
1008        ++$i;
1009    }
1010
1011    print qq|
1012	</tr>
1013      </table>
1014    </td>
1015  </tr>
1016  <tr>
1017    <td><hr size=3 noshade></td>
1018  </tr>
1019</table>
1020
1021</body>
1022</html>
1023|;
1024
1025}
1026
1027sub generate_income_statement {
1028
1029    $form->{padding} = "&nbsp;&nbsp;";
1030    $form->{bold}    = "<b>";
1031    $form->{endbold} = "</b>";
1032    $form->{br}      = "<br>";
1033
1034    RP->income_statement( \%myconfig, \%$form );
1035
1036    ( $form->{department} )    = split /--/, $form->{department};
1037    ( $form->{projectnumber} ) = split /--/, $form->{projectnumber};
1038
1039    $form->{period} =
1040      $locale->date( \%myconfig, $form->current_date( \%myconfig ), 1 );
1041    $form->{todate} = $form->current_date( \%myconfig ) unless $form->{todate};
1042
1043    # if there are any dates construct a where
1044    if ( $form->{fromdate} || $form->{todate} ) {
1045
1046        unless ( $form->{todate} ) {
1047            $form->{todate} = $form->current_date( \%myconfig );
1048        }
1049
1050        $longtodate  = $locale->date( \%myconfig, $form->{todate}, 1 );
1051        $shorttodate = $locale->date( \%myconfig, $form->{todate}, 0 );
1052
1053        $longfromdate  = $locale->date( \%myconfig, $form->{fromdate}, 1 );
1054        $shortfromdate = $locale->date( \%myconfig, $form->{fromdate}, 0 );
1055
1056        $form->{this_period} = "$shortfromdate<br>\n$shorttodate";
1057        $form->{period} =
1058            $locale->text('for Period')
1059          . qq|<br>\n$longfromdate |
1060          . $locale->text('To')
1061          . qq| $longtodate|;
1062    }
1063
1064    if ( $form->{comparefromdate} || $form->{comparetodate} ) {
1065        $longcomparefromdate =
1066          $locale->date( \%myconfig, $form->{comparefromdate}, 1 );
1067        $shortcomparefromdate =
1068          $locale->date( \%myconfig, $form->{comparefromdate}, 0 );
1069
1070        $longcomparetodate =
1071          $locale->date( \%myconfig, $form->{comparetodate}, 1 );
1072        $shortcomparetodate =
1073          $locale->date( \%myconfig, $form->{comparetodate}, 0 );
1074
1075        $form->{last_period} = "$shortcomparefromdate<br>\n$shortcomparetodate";
1076        $form->{period} .=
1077            "<br>\n$longcomparefromdate "
1078          . $locale->text('To')
1079          . qq| $longcomparetodate|;
1080    }
1081
1082    # setup variables for the form
1083    @a = qw(company address businessnumber);
1084    for (@a) { $form->{$_} = $myconfig{$_} }
1085    $form->{address} =~ s/\\n/<br>/g;
1086
1087    $form->{templates} = $myconfig{templates};
1088
1089    $form->{IN} = "income_statement.html";
1090
1091    $form->parse_template;
1092
1093}
1094
1095sub generate_balance_sheet {
1096
1097    $form->{padding} = "&nbsp;&nbsp;";
1098    $form->{bold}    = "<b>";
1099    $form->{endbold} = "</b>";
1100    $form->{br}      = "<br>";
1101
1102    RP->balance_sheet( \%myconfig, \%$form );
1103
1104    $form->{asofdate} = $form->current_date( \%myconfig )
1105      unless $form->{asofdate};
1106    $form->{period} =
1107      $locale->date( \%myconfig, $form->current_date( \%myconfig ), 1 );
1108
1109    ( $form->{department} ) = split /--/, $form->{department};
1110
1111    # define Current Earnings account
1112    $padding = ( $form->{l_heading} ) ? $form->{padding} : "";
1113    push(
1114        @{ $form->{equity_account} },
1115        $padding . $locale->text('Current Earnings')
1116    );
1117
1118    $form->{this_period} = $locale->date( \%myconfig, $form->{asofdate}, 0 );
1119    $form->{last_period} =
1120      $locale->date( \%myconfig, $form->{compareasofdate}, 0 );
1121
1122    $form->{IN} = "balance_sheet.html";
1123
1124    # setup company variables for the form
1125    for (qw(company address businessnumber nativecurr)) {
1126        $form->{$_} = $myconfig{$_};
1127    }
1128    $form->{address} =~ s/\\n/<br>/g;
1129
1130    $form->{templates} = $myconfig{templates};
1131
1132    $form->parse_template;
1133
1134}
1135
1136sub generate_projects {
1137
1138    $form->{nextsub} = "generate_projects";
1139    $form->{title}   = $locale->text('Project Transactions');
1140
1141    RP->trial_balance( \%myconfig, \%$form );
1142
1143    &list_accounts;
1144
1145}
1146
1147# Antonio Gallardo
1148#
1149# D.S. Feb 16, 2001
1150# included links to display transactions for period entered
1151# added headers and subtotals
1152#
1153sub generate_trial_balance {
1154
1155    # get for each account initial balance, debits and credits
1156    RP->trial_balance( \%myconfig, \%$form );
1157
1158    $form->{nextsub} = "generate_trial_balance";
1159    $form->{title}   = $locale->text('Trial Balance');
1160
1161    $form->{callback} = "$form->{script}?action=generate_trial_balance";
1162    for (
1163        qw(login path sessionid nextsub fromdate todate month year interval l_heading l_subtotal all_accounts accounttype title)
1164      )
1165    {
1166        $form->{callback} .= "&$_=$form->{$_}";
1167    }
1168    $form->{callback} = $form->escape( $form->{callback} );
1169
1170    &list_accounts;
1171
1172}
1173
1174sub list_accounts {
1175
1176    $title = $form->escape( $form->{title} );
1177
1178    if ( $form->{department} ) {
1179        ($department) = split /--/, $form->{department};
1180        $options    = $locale->text('Department') . " : $department<br>";
1181        $department = $form->escape( $form->{department} );
1182    }
1183    if ( $form->{projectnumber} ) {
1184        ($projectnumber) = split /--/, $form->{projectnumber};
1185        $options .= $locale->text('Project Number') . " : $projectnumber<br>";
1186        $projectnumber = $form->escape( $form->{projectnumber} );
1187    }
1188
1189    # if there are any dates
1190    if ( $form->{fromdate} || $form->{todate} ) {
1191
1192        if ( $form->{fromdate} ) {
1193            $fromdate = $locale->date( \%myconfig, $form->{fromdate}, 1 );
1194        }
1195        if ( $form->{todate} ) {
1196            $todate = $locale->date( \%myconfig, $form->{todate}, 1 );
1197        }
1198
1199        $form->{period} = "$fromdate - $todate";
1200    }
1201    else {
1202        $form->{period} =
1203          $locale->date( \%myconfig, $form->current_date( \%myconfig ), 1 );
1204
1205    }
1206    $options .= $form->{period};
1207
1208    @column_index = qw(accno description begbalance debit credit endbalance);
1209
1210    $column_header{accno} =
1211      qq|<th class=listheading>| . $locale->text('Account') . qq|</th>|;
1212    $column_header{description} =
1213      qq|<th class=listheading>| . $locale->text('Description') . qq|</th>|;
1214    $column_header{debit} =
1215      qq|<th class=listheading>| . $locale->text('Debit') . qq|</th>|;
1216    $column_header{credit} =
1217      qq|<th class=listheading>| . $locale->text('Credit') . qq|</th>|;
1218    $column_header{begbalance} =
1219      qq|<th class=listheading>| . $locale->text('Balance') . qq|</th>|;
1220    $column_header{endbalance} =
1221      qq|<th class=listheading>| . $locale->text('Balance') . qq|</th>|;
1222
1223    if ( $form->{accounttype} eq 'gifi' ) {
1224        $column_header{accno} =
1225          qq|<th class=listheading>| . $locale->text('GIFI') . qq|</th>|;
1226    }
1227
1228    $form->header;
1229
1230    print qq|
1231<body>
1232
1233<table width=100%>
1234  <tr>
1235    <th class=listtop>$form->{title}</th>
1236  </tr>
1237  <tr height="5"></tr>
1238  <tr>
1239    <td>$options</td>
1240  </tr>
1241  <tr>
1242    <td>
1243      <table width=100%>
1244	<tr>|;
1245
1246    for (@column_index) { print "$column_header{$_}\n" }
1247
1248    print qq|
1249        </tr>
1250|;
1251
1252    # sort the whole thing by account numbers and display
1253    foreach $ref ( sort { $a->{accno} cmp $b->{accno} } @{ $form->{TB} } ) {
1254
1255        $description = $form->escape( $ref->{description} );
1256
1257        $href =
1258qq|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}|;
1259
1260        if ( $form->{accounttype} eq 'gifi' ) {
1261            $href .= "&gifi_accno=$ref->{accno}&gifi_description=$description";
1262            $na = $locale->text('N/A');
1263            if ( !$ref->{accno} ) {
1264                for (qw(accno description)) { $ref->{$_} = $na }
1265            }
1266        }
1267        else {
1268            $href .= "&accno=$ref->{accno}&description=$description";
1269        }
1270
1271        $ml = ( $ref->{category} =~ /(A|E)/ ) ? -1 : 1;
1272        $ml *= -1 if $ref->{contra};
1273
1274        $debit = $form->format_amount( \%myconfig, $ref->{debit}, 2, "&nbsp;" );
1275        $credit =
1276          $form->format_amount( \%myconfig, $ref->{credit}, 2, "&nbsp;" );
1277        $begbalance =
1278          $form->format_amount( \%myconfig, $ref->{balance} * $ml, 2,
1279            "&nbsp;" );
1280        $endbalance =
1281          $form->format_amount( \%myconfig,
1282            ( $ref->{balance} + $ref->{amount} ) * $ml,
1283            2, "&nbsp;" );
1284
1285        if ( $ref->{charttype} eq "H" && $subtotal && $form->{l_subtotal} ) {
1286
1287            if ($subtotal) {
1288
1289                for (qw(accno begbalance endbalance)) {
1290                    $column_data{$_} = "<th>&nbsp;</th>";
1291                }
1292
1293                $subtotalbegbalance =
1294                  $form->format_amount( \%myconfig, $subtotalbegbalance, 2,
1295                    "&nbsp;" );
1296                $subtotalendbalance =
1297                  $form->format_amount( \%myconfig, $subtotalendbalance, 2,
1298                    "&nbsp;" );
1299                $subtotaldebit =
1300                  $form->format_amount( \%myconfig, $subtotaldebit, 2,
1301                    "&nbsp;" );
1302                $subtotalcredit =
1303                  $form->format_amount( \%myconfig, $subtotalcredit, 2,
1304                    "&nbsp;" );
1305
1306                $column_data{description} =
1307                  "<th class=listsubtotal>$subtotaldescription</th>";
1308                $column_data{begbalance} =
1309                  "<th align=right class=listsubtotal>$subtotalbegbalance</th>";
1310                $column_data{endbalance} =
1311                  "<th align=right class=listsubtotal>$subtotalendbalance</th>";
1312                $column_data{debit} =
1313                  "<th align=right class=listsubtotal>$subtotaldebit</th>";
1314                $column_data{credit} =
1315                  "<th align=right class=listsubtotal>$subtotalcredit</th>";
1316
1317                print qq|
1318	  <tr class=listsubtotal>
1319|;
1320                for (@column_index) { print "$column_data{$_}\n" }
1321
1322                print qq|
1323	  </tr>
1324|;
1325            }
1326        }
1327
1328        if ( $ref->{charttype} eq "H" ) {
1329            $subtotal            = 1;
1330            $subtotaldescription = $ref->{description};
1331            $subtotaldebit       = $ref->{debit};
1332            $subtotalcredit      = $ref->{credit};
1333            $subtotalbegbalance  = 0;
1334            $subtotalendbalance  = 0;
1335
1336            if ( $form->{l_heading} ) {
1337                if ( !$form->{all_accounts} ) {
1338                    if ( ( $subtotaldebit + $subtotalcredit ) == 0 ) {
1339                        $subtotal = 0;
1340                        next;
1341                    }
1342                }
1343            }
1344            else {
1345                $subtotal = 0;
1346                if (
1347                    $form->{all_accounts}
1348                    || ( $form->{l_subtotal}
1349                        && ( ( $subtotaldebit + $subtotalcredit ) != 0 ) )
1350                  )
1351                {
1352                    $subtotal = 1;
1353                }
1354                next;
1355            }
1356
1357            for (qw(accno debit credit begbalance endbalance)) {
1358                $column_data{$_} = "<th>&nbsp;</th>";
1359            }
1360            $column_data{description} =
1361              "<th class=listheading>$ref->{description}</th>";
1362        }
1363
1364        if ( $ref->{charttype} eq "A" ) {
1365            $column_data{accno} = "<td><a href=$href>$ref->{accno}</a></td>";
1366            $column_data{description} = "<td>$ref->{description}</td>";
1367            $column_data{debit}       = "<td align=right>$debit</td>";
1368            $column_data{credit}      = "<td align=right>$credit</td>";
1369            $column_data{begbalance}  = "<td align=right>$begbalance</td>";
1370            $column_data{endbalance}  = "<td align=right>$endbalance</td>";
1371
1372            $totaldebit  += $ref->{debit};
1373            $totalcredit += $ref->{credit};
1374
1375            $cml = ( $ref->{contra} ) ? -1 : 1;
1376
1377            $subtotalbegbalance += $ref->{balance} * $ml * $cml;
1378            $subtotalendbalance +=
1379              ( $ref->{balance} + $ref->{amount} ) * $ml * $cml;
1380
1381        }
1382
1383        if ( $ref->{charttype} eq "H" ) {
1384            print qq|
1385      <tr class=listheading>
1386|;
1387        }
1388        if ( $ref->{charttype} eq "A" ) {
1389            $i++;
1390            $i %= 2;
1391            print qq|
1392      <tr class=listrow$i>
1393|;
1394        }
1395
1396        for (@column_index) { print "$column_data{$_}\n" }
1397
1398        print qq|
1399      </tr>
1400|;
1401    }
1402
1403    # print last subtotal
1404    if ( $subtotal && $form->{l_subtotal} ) {
1405        for (qw(accno begbalance endbalance)) {
1406            $column_data{$_} = "<th>&nbsp;</th>";
1407        }
1408        $subtotalbegbalance =
1409          $form->format_amount( \%myconfig, $subtotalbegbalance, 2, "&nbsp;" );
1410        $subtotalendbalance =
1411          $form->format_amount( \%myconfig, $subtotalendbalance, 2, "&nbsp;" );
1412        $subtotaldebit =
1413          $form->format_amount( \%myconfig, $subtotaldebit, 2, "&nbsp;" );
1414        $subtotalcredit =
1415          $form->format_amount( \%myconfig, $subtotalcredit, 2, "&nbsp;" );
1416        $column_data{description} =
1417          "<th class=listsubtotal>$subtotaldescription</th>";
1418        $column_data{begbalance} =
1419          "<th align=right class=listsubtotal>$subtotalbegbalance</th>";
1420        $column_data{endbalance} =
1421          "<th align=right class=listsubtotal>$subtotalendbalance</th>";
1422        $column_data{debit} =
1423          "<th align=right class=listsubtotal>$subtotaldebit</th>";
1424        $column_data{credit} =
1425          "<th align=right class=listsubtotal>$subtotalcredit</th>";
1426
1427        print qq|
1428    <tr class=listsubtotal>
1429|;
1430        for (@column_index) { print "$column_data{$_}\n" }
1431
1432        print qq|
1433    </tr>
1434|;
1435    }
1436
1437    $totaldebit = $form->format_amount( \%myconfig, $totaldebit, 2, "&nbsp;" );
1438    $totalcredit =
1439      $form->format_amount( \%myconfig, $totalcredit, 2, "&nbsp;" );
1440
1441    for (qw(accno description begbalance endbalance)) {
1442        $column_data{$_} = "<th>&nbsp;</th>";
1443    }
1444
1445    $column_data{debit} = qq|<th align=right class=listtotal>$totaldebit</th>|;
1446    $column_data{credit} =
1447      qq|<th align=right class=listtotal>$totalcredit</th>|;
1448
1449    print qq|
1450        <tr class=listtotal>
1451|;
1452
1453    for (@column_index) { print "$column_data{$_}\n" }
1454
1455    print qq|
1456	</tr>
1457      </table>
1458    </td>
1459  </tr>
1460  <tr>
1461    <td><hr size=3 noshade></td>
1462  </tr>
1463</table>
1464
1465</body>
1466</html>
1467|;
1468
1469}
1470
1471sub generate_ar_aging {
1472
1473    # split customer
1474    ( $form->{customer} ) = split( /--/, $form->{customer} );
1475    $customer = $form->escape( $form->{customer}, 1 );
1476    $title    = $form->escape( $form->{title},    1 );
1477    $media    = $form->escape( $form->{media},    1 );
1478
1479    $form->{ct}   = "customer";
1480    $form->{arap} = "ar";
1481
1482    RP->aging( \%myconfig, \%$form );
1483
1484    $form->{callback} =
1485qq|$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}|;
1486
1487    &aging;
1488
1489}
1490
1491sub generate_ap_aging {
1492
1493    # split vendor
1494    ( $form->{vendor} ) = split( /--/, $form->{vendor} );
1495    $vendor = $form->escape( $form->{vendor}, 1 );
1496    $title  = $form->escape( $form->{title},  1 );
1497    $media  = $form->escape( $form->{media},  1 );
1498
1499    $form->{ct}   = "vendor";
1500    $form->{arap} = "ap";
1501
1502    RP->aging( \%myconfig, \%$form );
1503
1504    $form->{callback} =
1505qq|$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}|;
1506
1507    &aging;
1508
1509}
1510
1511sub aging {
1512
1513    $form->header;
1514
1515    $column_header{statement} = qq|<th class=listheading width=1%>&nbsp;</th>|;
1516    $column_header{ct} =
1517        qq|<th class=listheading width=60%>|
1518      . $locale->text( ucfirst $form->{ct} )
1519      . qq|</th>|;
1520    $column_header{language} =
1521      qq|<th class=listheading>| . $locale->text('Language') . qq|</th>|;
1522    $column_header{invnumber} =
1523      qq|<th class=listheading>| . $locale->text('Invoice') . qq|</th>|;
1524    $column_header{ordnumber} =
1525      qq|<th class=listheading>| . $locale->text('Order') . qq|</th>|;
1526    $column_header{transdate} =
1527      qq|<th class=listheading nowrap>| . $locale->text('Date') . qq|</th>|;
1528    $column_header{duedate} =
1529      qq|<th class=listheading nowrap>| . $locale->text('Due Date') . qq|</th>|;
1530    $column_header{c0} =
1531        qq|<th class=listheading width=10% nowrap>|
1532      . $locale->text('Current')
1533      . qq|</th>|;
1534    $column_header{c30} = qq|<th class=listheading width=10% nowrap>30</th>|;
1535    $column_header{c60} = qq|<th class=listheading width=10% nowrap>60</th>|;
1536    $column_header{c90} = qq|<th class=listheading width=10% nowrap>90</th>|;
1537    $column_header{total} =
1538        qq|<th class=listheading width=10% nowrap>|
1539      . $locale->text('Total')
1540      . qq|</th>|;
1541
1542    @column_index = qw(statement ct);
1543
1544    if ( @{ $form->{all_language} } && $form->{arap} eq 'ar' ) {
1545        push @column_index, "language";
1546        $form->{selectlanguage} = qq|<option>\n|;
1547
1548        for ( @{ $form->{all_language} } ) {
1549            $form->{selectlanguage} .=
1550              qq|<option value="$_->{code}">$_->{description}\n|;
1551        }
1552    }
1553
1554    @c = ();
1555    for (qw(c0 c30 c60 c90)) {
1556        if ( $form->{$_} ) {
1557            push @c, $_;
1558            $form->{callback} .= "&$_=$form->{$_}";
1559        }
1560    }
1561
1562    if ( !$form->{summary} ) {
1563        push @column_index, qw(invnumber ordnumber transdate duedate);
1564    }
1565    push @column_index, @c;
1566    push @column_index, "total";
1567
1568    $option = $locale->text('Aged');
1569    if ( $form->{overdue} ) {
1570        $option = $locale->text('Aged Overdue');
1571        $form->{callback} .= "&overdue=$form->{overdue}";
1572    }
1573
1574    if ( $form->{department} ) {
1575        $option .= "\n<br>" if $option;
1576        ($department) = split /--/, $form->{department};
1577        $option .= $locale->text('Department') . " : $department";
1578        $department = $form->escape( $form->{department}, 1 );
1579        $form->{callback} .= "&department=$department";
1580    }
1581
1582    if ( $form->{arap} eq 'ar' ) {
1583        if ( $form->{customer} ) {
1584            $option .= "\n<br>" if $option;
1585            $option .= $form->{customer};
1586        }
1587    }
1588    if ( $form->{arap} eq 'ap' ) {
1589        shift @column_index;
1590        if ( $form->{vendor} ) {
1591            $option .= "\n<br>" if $option;
1592            $option .= $form->{vendor};
1593        }
1594    }
1595
1596    $todate = $locale->date( \%myconfig, $form->{todate}, 1 );
1597    $option .= "\n<br>" if $option;
1598    $option .=
1599      $locale->text('for Period') . " " . $locale->text('To') . " $todate";
1600
1601    print qq|
1602<body>
1603
1604<form method=post action=$form->{script}>
1605
1606<table width=100%>
1607  <tr>
1608    <th class=listtop>$form->{title}</th>
1609  </tr>
1610  <tr height="5"></tr>
1611  <tr>
1612    <td>$option</td>
1613  </tr>
1614  <tr>
1615    <td>
1616      <table width=100%>
1617|;
1618
1619    $ctid = 0;
1620    $i    = 0;
1621    $k    = 0;
1622    $l    = $#{ $form->{AG} };
1623
1624    foreach $ref ( @{ $form->{AG} } ) {
1625
1626        if ( $curr ne $ref->{curr} ) {
1627            $ctid = 0;
1628            for (@column_index) { $column_data{$_} = qq|<th>&nbsp;</th>| }
1629            if ($curr) {
1630                $c0total =
1631                  $form->format_amount( \%myconfig, $c0total, 2, "&nbsp" );
1632                $c30total =
1633                  $form->format_amount( \%myconfig, $c30total, 2, "&nbsp" );
1634                $c60total =
1635                  $form->format_amount( \%myconfig, $c60total, 2, "&nbsp" );
1636                $c90total =
1637                  $form->format_amount( \%myconfig, $c90total, 2, "&nbsp" );
1638                $total = $form->format_amount( \%myconfig, $total, 2, "&nbsp" );
1639
1640                for (qw(ct statement language)) {
1641                    $column_data{$_} = qq|<td>&nbsp;</td>|;
1642                }
1643                $column_data{c0}    = qq|<th align=right>$c0total</th>|;
1644                $column_data{c30}   = qq|<th align=right>$c30total</th>|;
1645                $column_data{c60}   = qq|<th align=right>$c60total</th>|;
1646                $column_data{c90}   = qq|<th align=right>$c90total</th>|;
1647                $column_data{total} = qq|<th align=right>$total</th>|;
1648
1649                print qq|
1650	<tr class=listtotal>
1651|;
1652
1653                for (@column_index) { print "$column_data{$_}\n" }
1654
1655                print qq|
1656	  </tr>
1657|;
1658
1659                $c0subtotal  = 0;
1660                $c30subtotal = 0;
1661                $c60subtotal = 0;
1662                $c90subtotal = 0;
1663                $subtotal    = 0;
1664
1665                $c0total  = 0;
1666                $c30total = 0;
1667                $c60total = 0;
1668                $c90total = 0;
1669                $total    = 0;
1670
1671            }
1672
1673            $curr = $ref->{curr};
1674            print qq|
1675        <tr>
1676	  <td></td>
1677	  <th>$curr</th>
1678	</tr>
1679
1680	<tr class=listheading>
1681|;
1682
1683            for (@column_index) { print "$column_header{$_}\n" }
1684
1685            print qq|
1686	</tr>
1687|;
1688        }
1689
1690        $k++;
1691
1692        if ( $ctid != $ref->{ctid} ) {
1693
1694            $i++;
1695
1696            $column_data{ct} = qq|<td>$ref->{name}</td>|;
1697
1698            if ( $form->{selectlanguage} ) {
1699                $form->{"selectlanguage_$i"} = $form->{selectlanguage};
1700                $form->{"selectlanguage_$i"} =~
1701                  s/(<option value="\Q$ref->{language_code}\E")/$1 selected/;
1702                $column_data{language} =
1703qq|<td><select name="language_code_$i">$form->{"selectlanguage_$i"}</select></td>|;
1704            }
1705
1706            $column_data{statement} =
1707qq|<td><input name="statement_$i" type=checkbox class=checkbox value=1 $ref->{checked}>
1708      <input type=hidden name="$form->{ct}_id_$i" value="$ref->{ctid}">
1709      <input type=hidden name="curr_$i" value="$ref->{curr}">
1710      </td>|;
1711
1712        }
1713
1714        $ctid = $ref->{ctid};
1715
1716        for (qw(c0 c30 c60 c90)) {
1717            $ref->{$_} =
1718              $form->round_amount( $ref->{$_} / $ref->{exchangerate}, 2 );
1719        }
1720
1721        $c0subtotal  += $ref->{c0};
1722        $c30subtotal += $ref->{c30};
1723        $c60subtotal += $ref->{c60};
1724        $c90subtotal += $ref->{c90};
1725
1726        $c0total  += $ref->{c0};
1727        $c30total += $ref->{c30};
1728        $c60total += $ref->{c60};
1729        $c90total += $ref->{c90};
1730
1731        $ref->{total} =
1732          ( $ref->{c0} + $ref->{c30} + $ref->{c60} + $ref->{c90} );
1733        $subtotal += $ref->{total};
1734        $total    += $ref->{total};
1735
1736        $ref->{c0} =
1737          $form->format_amount( \%myconfig, $ref->{c0}, 2, "&nbsp;" );
1738        $ref->{c30} =
1739          $form->format_amount( \%myconfig, $ref->{c30}, 2, "&nbsp;" );
1740        $ref->{c60} =
1741          $form->format_amount( \%myconfig, $ref->{c60}, 2, "&nbsp;" );
1742        $ref->{c90} =
1743          $form->format_amount( \%myconfig, $ref->{c90}, 2, "&nbsp;" );
1744        $ref->{total} =
1745          $form->format_amount( \%myconfig, $ref->{total}, 2, "&nbsp;" );
1746
1747        $href =
1748qq|$ref->{module}.pl?path=$form->{path}&action=edit&id=$ref->{id}&login=$form->{login}&sessionid=$form->{sessionid}&callback=|
1749          . $form->escape( $form->{callback} );
1750
1751        $column_data{invnumber} =
1752          qq|<td><a href=$href>$ref->{invnumber}</a></td>|;
1753        for (qw(ordnumber transdate duedate)) {
1754            $column_data{$_} = qq|<td>$ref->{$_}</td>|;
1755        }
1756        for (qw(c0 c30 c60 c90 total)) {
1757            $column_data{$_} = qq|<td align=right>$ref->{$_}</td>|;
1758        }
1759
1760        if ( !$form->{summary} ) {
1761
1762            $j++;
1763            $j %= 2;
1764            print qq|
1765        <tr class=listrow$j>
1766|;
1767
1768            for (@column_index) { print "$column_data{$_}\n" }
1769
1770            print qq|
1771        </tr>
1772|;
1773
1774            for (qw(ct statement language)) {
1775                $column_data{$_} = qq|<td>&nbsp;</td>|;
1776            }
1777
1778        }
1779
1780        # print subtotal
1781        $nextid = ( $k <= $l ) ? $form->{AG}->[$k]->{ctid} : 0;
1782        if ( $ctid != $nextid ) {
1783
1784            $c0subtotal =
1785              $form->format_amount( \%myconfig, $c0subtotal, 2, "&nbsp" );
1786            $c30subtotal =
1787              $form->format_amount( \%myconfig, $c30subtotal, 2, "&nbsp" );
1788            $c60subtotal =
1789              $form->format_amount( \%myconfig, $c60subtotal, 2, "&nbsp" );
1790            $c90subtotal =
1791              $form->format_amount( \%myconfig, $c90subtotal, 2, "&nbsp" );
1792            $subtotal =
1793              $form->format_amount( \%myconfig, $subtotal, 2, "&nbsp" );
1794
1795            if ( $form->{summary} ) {
1796                $column_data{c0}    = qq|<td align=right>$c0subtotal</th>|;
1797                $column_data{c30}   = qq|<td align=right>$c30subtotal</th>|;
1798                $column_data{c60}   = qq|<td align=right>$c60subtotal</th>|;
1799                $column_data{c90}   = qq|<td align=right>$c90subtotal</th>|;
1800                $column_data{total} = qq|<td align=right>$subtotal</th>|;
1801
1802                $j++;
1803                $j %= 2;
1804                print qq|
1805      <tr class=listrow$j>
1806|;
1807
1808                for (@column_index) { print "$column_data{$_}\n" }
1809
1810                print qq|
1811      </tr>
1812|;
1813
1814            }
1815            else {
1816
1817                for (@column_index) { $column_data{$_} = qq|<th>&nbsp;</th>| }
1818
1819                $column_data{c0} =
1820                  qq|<th class=listsubtotal align=right>$c0subtotal</th>|;
1821                $column_data{c30} =
1822                  qq|<th class=listsubtotal align=right>$c30subtotal</th>|;
1823                $column_data{c60} =
1824                  qq|<th class=listsubtotal align=right>$c60subtotal</th>|;
1825                $column_data{c90} =
1826                  qq|<th class=listsubtotal align=right>$c90subtotal</th>|;
1827                $column_data{total} =
1828                  qq|<th class=listsubtotal align=right>$subtotal</th>|;
1829
1830                # print subtotals
1831                print qq|
1832	<tr class=listsubtotal>
1833|;
1834                for (@column_index) { print "$column_data{$_}\n" }
1835
1836                print qq|
1837	</tr>
1838|;
1839
1840            }
1841
1842            $c0subtotal  = 0;
1843            $c30subtotal = 0;
1844            $c60subtotal = 0;
1845            $c90subtotal = 0;
1846            $subtotal    = 0;
1847
1848        }
1849    }
1850
1851    print qq|
1852        </tr>
1853        <tr class=listtotal>
1854|;
1855
1856    for (@column_index) { $column_data{$_} = qq|<th>&nbsp;</th>| }
1857
1858    $c0total  = $form->format_amount( \%myconfig, $c0total,  2, "&nbsp;" );
1859    $c30total = $form->format_amount( \%myconfig, $c30total, 2, "&nbsp;" );
1860    $c60total = $form->format_amount( \%myconfig, $c60total, 2, "&nbsp;" );
1861    $c90total = $form->format_amount( \%myconfig, $c90total, 2, "&nbsp;" );
1862    $total    = $form->format_amount( \%myconfig, $total,    2, "&nbsp;" );
1863
1864    $column_data{c0}    = qq|<th align=right class=listtotal>$c0total</th>|;
1865    $column_data{c30}   = qq|<th align=right class=listtotal>$c30total</th>|;
1866    $column_data{c60}   = qq|<th align=right class=listtotal>$c60total</th>|;
1867    $column_data{c90}   = qq|<th align=right class=listtotal>$c90total</th>|;
1868    $column_data{total} = qq|<th align=right class=listtotal>$total</th>|;
1869
1870    for (@column_index) { print "$column_data{$_}\n" }
1871
1872    print qq|
1873	</tr>
1874	<input type=hidden name=rowcount value="$i">
1875      </table>
1876    </td>
1877  </tr>
1878
1879  <tr>
1880    <td>
1881|;
1882
1883    &print_options if ( $form->{arap} eq 'ar' );
1884
1885    print qq|
1886    </td>
1887  </tr>
1888  <tr>
1889    <td><hr size=3 noshade></td>
1890  </tr>
1891</table>
1892|;
1893
1894    if ( $form->{arap} eq 'ar' ) {
1895
1896        $form->hide_form(
1897            qw(todate title summary overdue c0 c30 c60 c90 callback arap ct department path login sessionid)
1898        );
1899
1900        print qq|
1901<input type=hidden name=$form->{ct} value="$form->{$form->{ct}}">
1902|;
1903
1904        # type=submit $locale->text('Select all')
1905        # type=submit $locale->text('Print')
1906        # type=submit $locale->text('E-mail')
1907
1908        %button = (
1909            'select_all' =>
1910              { ndx => 1, key => 'A', value => $locale->text('Select all') },
1911            'print' =>
1912              { ndx => 2, key => 'P', value => $locale->text('Print') },
1913            'e_mail' =>
1914              { ndx => 5, key => 'E', value => $locale->text('E-mail') },
1915        );
1916
1917        for ( sort { $button{$a}->{ndx} <=> $button{$b}->{ndx} } keys %button )
1918        {
1919            $form->print_button( \%button, $_ );
1920        }
1921
1922    }
1923
1924    if ( $form->{lynx} ) {
1925        require "bin/menu.pl";
1926        &menubar;
1927    }
1928
1929    print qq|
1930</form>
1931
1932</body>
1933</html>
1934|;
1935
1936}
1937
1938sub select_all {
1939
1940    RP->aging( \%myconfig, \%$form );
1941
1942    for ( @{ $form->{AG} } ) { $_->{checked} = "checked" }
1943
1944    &aging;
1945
1946}
1947
1948sub print_options {
1949
1950    $form->{sendmode} = "attachment";
1951    $form->{copies} = 1 unless $form->{copies};
1952
1953    $form->{PD}{ $form->{type} }     = "selected";
1954    $form->{DF}{ $form->{format} }   = "selected";
1955    $form->{SM}{ $form->{sendmode} } = "selected";
1956
1957    $format = qq|
1958            <option value=html $form->{PD}{format}>html|;
1959
1960    $type = qq|
1961	    <option value=statement $form->{PD}{statement}>|
1962      . $locale->text('Statement');
1963
1964    if ( $form->{media} eq 'email' ) {
1965        $media = qq|
1966            <td><select name=sendmode>
1967	    <option value=attachment $form->{SM}{attachment}>|
1968          . $locale->text('Attachment') . qq|
1969	    <option value=inline $form->{SM}{inline}>| . $locale->text('In-line');
1970    }
1971    else {
1972        $media = qq|
1973            <td><select name=media>
1974	    <option value=screen>| . $locale->text('Screen');
1975        if (   %{LedgerSMB::Sysconfig::printer}
1976            && ${LedgerSMB::Sysconfig::latex} )
1977        {
1978            for ( sort keys %{LedgerSMB::Sysconfig::printer} ) {
1979                $media .= qq|
1980            <option value="$_">$_|;
1981            }
1982        }
1983    }
1984
1985    $media =~ s/(<option value="\Q$form->{media}\E")/$1 selected/;
1986    $media .= qq|</select></td>|;
1987
1988    if ( ${LedgerSMB::Sysconfig::latex} ) {
1989        $format .= qq|
1990            <option value=postscript $form->{DF}{postscript}>|
1991          . $locale->text('Postscript') . qq|
1992	    <option value=pdf $form->{DF}{pdf}>| . $locale->text('PDF');
1993    }
1994
1995    print qq|
1996<table>
1997  <tr>
1998    <td><select name=type>$type</select></td>
1999    <td><select name=format>$format</select></td>
2000    $media
2001|;
2002
2003    if (   %{LedgerSMB::Sysconfig::printer}
2004        && ${LedgerSMB::Sysconfig::latex}
2005        && $form->{media} ne 'email' )
2006    {
2007        print qq|
2008      <td>| . $locale->text('Copies') . qq|
2009      <input name=copies size=2 value="$form->{copies}"></td>
2010|;
2011    }
2012
2013    print qq|
2014  </tr>
2015</table>
2016|;
2017
2018}
2019
2020sub e_mail {
2021
2022    # get name and email addresses
2023    for $i ( 1 .. $form->{rowcount} ) {
2024        if ( $form->{"statement_$i"} ) {
2025            $form->{"$form->{ct}_id"}  = $form->{"$form->{ct}_id_$i"};
2026            $form->{"statement_1"}     = 1;
2027            $form->{"language_code_1"} = $form->{"language_code_$i"};
2028            $form->{"curr_1"}          = $form->{"curr_$i"};
2029            RP->get_customer( \%myconfig, \%$form );
2030            $selected = 1;
2031            last;
2032        }
2033    }
2034
2035    $form->error( $locale->text('Nothing selected!') ) unless $selected;
2036
2037    if ( $myconfig{role} =~ /(admin|manager)/ ) {
2038        $bcc = qq|
2039          <th align=right nowrap=true>| . $locale->text('Bcc') . qq|</th>
2040	  <td><input name=bcc size=30 value="$form->{bcc}"></td>
2041|;
2042    }
2043
2044    $title =
2045      $locale->text( 'E-mail Statement to [_1]', $form->{ $form->{ct} } );
2046
2047    $form->{media} = "email";
2048
2049    $form->header;
2050
2051    print qq|
2052<body>
2053
2054<form method=post action=$form->{script}>
2055
2056<table width=100%>
2057  <tr class=listtop>
2058    <th>$title</th>
2059  </tr>
2060  <tr height="5"></tr>
2061  <tr>
2062    <td>
2063      <table width=100%>
2064        <tr>
2065	  <th align=right nowrap>| . $locale->text('E-mail') . qq|</th>
2066	  <td><input name=email size=30 value="$form->{email}"></td>
2067	  <th align=right nowrap>| . $locale->text('Cc') . qq|</th>
2068	  <td><input name=cc size=30 value="$form->{cc}"></td>
2069	</tr>
2070	<tr>
2071	  <th align=right nowrap>| . $locale->text('Subject') . qq|</th>
2072	  <td><input name=subject size=30 value="$form->{subject}"></td>
2073	  $bcc
2074	</tr>
2075      </table>
2076    </td>
2077  </tr>
2078  <tr>
2079    <td>
2080      <table width=100%>
2081        <tr>
2082	  <th align=left nowrap>| . $locale->text('Message') . qq|</th>
2083	</tr>
2084	<tr>
2085	  <td><textarea name=message rows=15 cols=60 wrap=soft>$form->{message}</textarea></td>
2086	</tr>
2087      </table>
2088    </td>
2089  </tr>
2090  <tr>
2091    <td>
2092|;
2093
2094    &print_options;
2095
2096    for (qw(email cc bcc subject message type sendmode format action nextsub)) {
2097        delete $form->{$_};
2098    }
2099
2100    $form->hide_form;
2101
2102    print qq|
2103    </td>
2104  </tr>
2105  <tr>
2106    <td><hr size=3 noshade></td>
2107  </tr>
2108</table>
2109
2110<input type="hidden" name="nextsub" value="send_email">
2111
2112<br>
2113<button name="action" class="submit" type="submit" value="continue">|
2114      . $locale->text('Continue')
2115      . qq|</button>
2116</form>
2117
2118</body>
2119</html>
2120|;
2121
2122}
2123
2124sub send_email {
2125
2126    $form->{OUT}       = "${LedgerSMB::Sysconfig::sendmail}";
2127    $form->{printmode} = '|-';
2128
2129    $form->{subject} = $locale->text( 'Statement - [_1]', $form->{todate} )
2130      unless $form->{subject};
2131    $form->isblank( "email", $locale->text('E-mail address missing!') );
2132
2133    RP->aging( \%myconfig, \%$form );
2134
2135    &print_form;
2136
2137    $form->redirect(
2138        $locale->text( 'Statement sent to [_1]', $form->{ $form->{ct} } ) );
2139
2140}
2141
2142sub print {
2143
2144    if ( $form->{media} !~ /(screen|email)/ ) {
2145        $form->error( $locale->text('Select postscript or PDF!') )
2146          if ( $form->{format} !~ /(postscript|pdf)/ );
2147    }
2148
2149    for $i ( 1 .. $form->{rowcount} ) {
2150        if ( $form->{"statement_$i"} ) {
2151            $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
2152            $language_code            = $form->{"language_code_$i"};
2153            $curr                     = $form->{"curr_$i"};
2154            $selected                 = 1;
2155            last;
2156        }
2157    }
2158
2159    $form->error( $locale->text('Nothing selected!') ) unless $selected;
2160
2161    if ( $form->{media} !~ /(screen|email)/ ) {
2162        $form->{OUT}       = ${LedgerSMB::Sysconfig::printer}{ $form->{media} };
2163        $form->{printmode} = '|-';
2164        $form->{"$form->{ct}_id"} = "";
2165        $SIG{INT} = 'IGNORE';
2166    }
2167    else {
2168        $form->{"statement_1"}     = 1;
2169        $form->{"language_code_1"} = $language_code;
2170        $form->{"curr_1"}          = $curr;
2171    }
2172
2173    RP->aging( \%myconfig, \%$form );
2174
2175    &print_form;
2176
2177    $form->redirect( $locale->text('Statements sent to printer!') )
2178      if ( $form->{media} !~ /(screen|email)/ );
2179
2180}
2181
2182sub print_form {
2183
2184    $form->{statementdate} = $locale->date( \%myconfig, $form->{todate}, 1 );
2185
2186    $form->{templates} = "$myconfig{templates}";
2187
2188    # setup variables for the form
2189    @a = qw(company address businessnumber tel fax);
2190    for (@a) { $form->{$_} = $myconfig{$_} }
2191    $form->{address} =~ s/\\n/\n/g;
2192
2193    $form->format_string(@a);
2194
2195    $form->{IN} = "$form->{type}.html";
2196
2197    if ( $form->{format} eq 'postscript' ) {
2198        $form->{IN} =~ s/html$/tex/;
2199    }
2200    if ( $form->{format} eq 'pdf' ) {
2201        $form->{IN} =~ s/html$/tex/;
2202    }
2203
2204    @a = qw(name address1 address2 city state zipcode country contact);
2205    push @a, "$form->{ct}phone", "$form->{ct}fax", "$form->{ct}taxnumber";
2206    push @a, 'email' if !$form->{media} eq 'email';
2207
2208    $i = 0;
2209    while ( @{ $form->{AG} } ) {
2210
2211        $ref = shift @{ $form->{AG} };
2212
2213        if ( $ctid != $ref->{ctid} ) {
2214
2215            $ctid = $ref->{ctid};
2216            $i++;
2217
2218            if ( $form->{"statement_$i"} ) {
2219
2220                for (@a) { $form->{$_} = $ref->{$_} }
2221                $form->format_string(@a);
2222
2223                $form->{ $form->{ct} }    = $form->{name};
2224                $form->{"$form->{ct}_id"} = $ref->{ctid};
2225                $form->{language_code}    = $form->{"language_code_$i"};
2226                $form->{currency}         = $form->{"curr_$i"};
2227
2228                for (qw(invnumber ordnumber ponumber notes invdate duedate)) {
2229                    $form->{$_} = ();
2230                }
2231                $form->{total} = 0;
2232                foreach $item (qw(c0 c30 c60 c90)) {
2233                    $form->{$item} = ();
2234                    $form->{"${item}total"} = 0;
2235                }
2236
2237                &statement_details($ref) if $ref->{curr} eq $form->{currency};
2238
2239                while ($ref) {
2240
2241                    if ( scalar( @{ $form->{AG} } ) > 0 ) {
2242
2243                        # one or more left to go
2244                        if ( $ctid == $form->{AG}->[0]->{ctid} ) {
2245                            $ref = shift @{ $form->{AG} };
2246                            &statement_details($ref)
2247                              if $ref->{curr} eq $form->{currency};
2248
2249                            # any more?
2250                            $ref = scalar( @{ $form->{AG} } );
2251                        }
2252                        else {
2253                            $ref = 0;
2254                        }
2255                    }
2256                    else {
2257
2258                        # set initial ref to 0
2259                        $ref = 0;
2260                    }
2261
2262                }
2263
2264                for ( "c0", "c30", "c60", "c90", "" ) {
2265                    $form->{"${_}total"} =
2266                      $form->format_amount( \%myconfig, $form->{"${_}total"},
2267                        2 );
2268                }
2269
2270                $form->parse_template( \%myconfig,
2271                    ${LedgerSMB::Sysconfig::userspath} );
2272
2273            }
2274        }
2275    }
2276
2277}
2278
2279sub statement_details {
2280    my ($ref) = @_;
2281
2282    $ref->{invdate} = $ref->{transdate};
2283    my @a = qw(invnumber ordnumber ponumber notes invdate duedate);
2284    for (@a) { $form->{"${_}_1"} = $ref->{$_} }
2285    $form->format_string(qw(invnumber_1 ordnumber_1 ponumber_1 notes_1));
2286    for (@a) { push @{ $form->{$_} }, $form->{"${_}_1"} }
2287
2288    foreach $item (qw(c0 c30 c60 c90)) {
2289        eval {
2290            $ref->{$item} =
2291              $form->round_amount( $ref->{$item} / $ref->{exchangerate}, 2 );
2292        };
2293        $form->{"${item}total"} += $ref->{$item};
2294        $form->{total}          += $ref->{$item};
2295        push @{ $form->{$item} },
2296          $form->format_amount( \%myconfig, $ref->{$item}, 2 );
2297    }
2298
2299}
2300
2301sub generate_tax_report {
2302
2303    RP->tax_report( \%myconfig, \%$form );
2304
2305    $descvar     = "$form->{accno}_description";
2306    $description = $form->escape( $form->{$descvar} );
2307    $ratevar     = "$form->{accno}_rate";
2308    $taxrate     = $form->{"$form->{accno}_rate"};
2309
2310    if ( $form->{accno} =~ /^gifi_/ ) {
2311        $descvar     = "gifi_$form->{accno}_description";
2312        $description = $form->escape( $form->{$descvar} );
2313        $ratevar     = "gifi_$form->{accno}_rate";
2314        $taxrate     = $form->{"gifi_$form->{accno}_rate"};
2315    }
2316
2317    $department = $form->escape( $form->{department} );
2318
2319    # construct href
2320    $href =
2321"$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}";
2322
2323    # construct callback
2324    $description = $form->escape( $form->{$descvar}, 1 );
2325    $department = $form->escape( $form->{department}, 1 );
2326
2327    $form->sort_order();
2328
2329    $callback =
2330"$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}";
2331
2332    $form->{title} = $locale->text('GIFI') . " - "
2333      if ( $form->{accno} =~ /^gifi_/ );
2334
2335    $title = $form->escape( $form->{title} );
2336    $href .= "&title=$title";
2337    $title = $form->escape( $form->{title}, 1 );
2338    $callback .= "&title=$title";
2339
2340    $form->{title} = qq|$form->{title} $form->{"$form->{accno}_description"} |;
2341
2342    @columns =
2343      $form->sort_columns(
2344        qw(id transdate invnumber name description netamount tax total));
2345
2346    $form->{"l_description"} = "" if $form->{summary};
2347
2348    foreach $item (@columns) {
2349        if ( $form->{"l_$item"} eq "Y" ) {
2350            push @column_index, $item;
2351
2352            # add column to href and callback
2353            $callback .= "&l_$item=Y";
2354            $href     .= "&l_$item=Y";
2355        }
2356    }
2357
2358    if ( $form->{l_subtotal} eq 'Y' ) {
2359        $callback .= "&l_subtotal=Y";
2360        $href     .= "&l_subtotal=Y";
2361    }
2362
2363    if ( $form->{department} ) {
2364        ($department) = split /--/, $form->{department};
2365        $option = $locale->text('Department') . " : $department";
2366    }
2367
2368    # if there are any dates
2369    if ( $form->{fromdate} || $form->{todate} ) {
2370        if ( $form->{fromdate} ) {
2371            $fromdate = $locale->date( \%myconfig, $form->{fromdate}, 1 );
2372        }
2373        if ( $form->{todate} ) {
2374            $todate = $locale->date( \%myconfig, $form->{todate}, 1 );
2375        }
2376
2377        $form->{period} = "$fromdate - $todate";
2378    }
2379    else {
2380        $form->{period} =
2381          $locale->date( \%myconfig, $form->current_date( \%myconfig ), 1 );
2382    }
2383
2384    if ( $form->{db} eq 'ar' ) {
2385        $name    = $locale->text('Customer');
2386        $invoice = 'is.pl';
2387        $arap    = 'ar.pl';
2388    }
2389    if ( $form->{db} eq 'ap' ) {
2390        $name    = $locale->text('Vendor');
2391        $invoice = 'ir.pl';
2392        $arap    = 'ap.pl';
2393    }
2394
2395    $option .= "<br>" if $option;
2396    $option .= "$form->{period}";
2397
2398    $column_header{id} =
2399        qq|<th><a class=listheading href=$href&sort=id>|
2400      . $locale->text('ID')
2401      . qq|</th>|;
2402    $column_header{invnumber} =
2403        qq|<th><a class=listheading href=$href&sort=invnumber>|
2404      . $locale->text('Invoice')
2405      . qq|</th>|;
2406    $column_header{transdate} =
2407        qq|<th><a class=listheading href=$href&sort=transdate>|
2408      . $locale->text('Date')
2409      . qq|</th>|;
2410    $column_header{netamount} =
2411      qq|<th class=listheading>| . $locale->text('Amount') . qq|</th>|;
2412    $column_header{tax} =
2413      qq|<th class=listheading>| . $locale->text('Tax') . qq|</th>|;
2414    $column_header{total} =
2415      qq|<th class=listheading>| . $locale->text('Total') . qq|</th>|;
2416
2417    $column_header{name} =
2418      qq|<th><a class=listheading href=$href&sort=name>$name</th>|;
2419
2420    $column_header{description} =
2421        qq|<th><a class=listheading href=$href&sort=description>|
2422      . $locale->text('Description')
2423      . qq|</th>|;
2424
2425    $form->header;
2426
2427    print qq|
2428<body>
2429
2430<table width=100%>
2431  <tr>
2432    <th class=listtop colspan=$colspan>$form->{title}</th>
2433  </tr>
2434  <tr height="5"></tr>
2435  <tr>
2436    <td>$option</td>
2437  </tr>
2438  <tr>
2439    <td>
2440      <table width=100%>
2441	<tr class=listheading>
2442|;
2443
2444    for (@column_index) { print "$column_header{$_}\n" }
2445
2446    print qq|
2447	</tr>
2448|;
2449
2450    # add sort and escape callback
2451    $callback = $form->escape( $callback . "&sort=$form->{sort}" );
2452
2453    if ( @{ $form->{TR} } ) {
2454        $sameitem = $form->{TR}->[0]->{ $form->{sort} };
2455    }
2456
2457    foreach $ref ( @{ $form->{TR} } ) {
2458
2459        $module = ( $ref->{invoice} ) ? $invoice : $arap;
2460        $module = 'ps.pl' if $ref->{till};
2461
2462        if ( $form->{l_subtotal} eq 'Y' ) {
2463            if ( $sameitem ne $ref->{ $form->{sort} } ) {
2464                &tax_subtotal;
2465                $sameitem = $ref->{ $form->{sort} };
2466            }
2467        }
2468
2469        $totalnetamount += $ref->{netamount};
2470        $totaltax       += $ref->{tax};
2471        $ref->{total} = $ref->{netamount} + $ref->{tax};
2472
2473        $subtotalnetamount += $ref->{netamount};
2474        $subtotaltax       += $ref->{tax};
2475
2476        for (qw(netamount tax total)) {
2477            $ref->{$_} =
2478              $form->format_amount( \%myconfig, $ref->{$_}, 2, "&nbsp;" );
2479        }
2480
2481        $column_data{id} = qq|<td>$ref->{id}</td>|;
2482        $column_data{invnumber} =
2483qq|<td><a href=$module?path=$form->{path}&action=edit&id=$ref->{id}&login=$form->{login}&sessionid=$form->{sessionid}&callback=$callback>$ref->{invnumber}</a></td>|;
2484
2485        for (qw(id transdate name partnumber description)) {
2486            $column_data{$_} = qq|<td>$ref->{$_}</td>|;
2487        }
2488
2489        for (qw(netamount tax total)) {
2490            $column_data{$_} = qq|<td align=right>$ref->{$_}</td>|;
2491        }
2492
2493        $i++;
2494        $i %= 2;
2495        print qq|
2496	<tr class=listrow$i>
2497|;
2498
2499        for (@column_index) { print "$column_data{$_}\n" }
2500
2501        print qq|
2502	</tr>
2503|;
2504
2505    }
2506
2507    if ( $form->{l_subtotal} eq 'Y' ) {
2508        &tax_subtotal;
2509    }
2510
2511    for (@column_index) { $column_data{$_} = qq|<th>&nbsp;</th>| }
2512
2513    print qq|
2514        </tr>
2515	<tr class=listtotal>
2516|;
2517
2518    $total = $form->format_amount( \%myconfig, $totalnetamount + $totaltax,
2519        2, "&nbsp;" );
2520    $totalnetamount =
2521      $form->format_amount( \%myconfig, $totalnetamount, 2, "&nbsp;" );
2522    $totaltax = $form->format_amount( \%myconfig, $totaltax, 2, "&nbsp;" );
2523
2524    $column_data{netamount} =
2525      qq|<th class=listtotal align=right>$totalnetamount</th>|;
2526    $column_data{tax}   = qq|<th class=listtotal align=right>$totaltax</th>|;
2527    $column_data{total} = qq|<th class=listtotal align=right>$total</th>|;
2528
2529    for (@column_index) { print "$column_data{$_}\n" }
2530
2531    print qq|
2532        </tr>
2533      </table>
2534    </td>
2535  </tr>
2536  <tr>
2537    <td><hr size=3 noshade></td>
2538  </tr>
2539</table>
2540
2541</body>
2542</html>
2543|;
2544
2545}
2546
2547sub tax_subtotal {
2548
2549    for (@column_index) { $column_data{$_} = "<td>&nbsp;</td>" }
2550
2551    $subtotal =
2552      $form->format_amount( \%myconfig, $subtotalnetamount + $subtotaltax,
2553        2, "&nbsp;" );
2554    $subtotalnetamount =
2555      $form->format_amount( \%myconfig, $subtotalnetamount, 2, "&nbsp;" );
2556    $subtotaltax =
2557      $form->format_amount( \%myconfig, $subtotaltax, 2, "&nbsp;" );
2558
2559    $column_data{netamount} =
2560      "<th class=listsubtotal align=right>$subtotalnetamount</th>";
2561    $column_data{tax} = "<th class=listsubtotal align=right>$subtotaltax</th>";
2562    $column_data{total} = "<th class=listsubtotal align=right>$subtotal</th>";
2563
2564    $subtotalnetamount = 0;
2565    $subtotaltax       = 0;
2566
2567    print qq|
2568	<tr class=listsubtotal>
2569|;
2570    for (@column_index) { print "\n$column_data{$_}" }
2571
2572    print qq|
2573        </tr>
2574|;
2575
2576}
2577
2578sub list_payments {
2579
2580    if ( $form->{account} ) {
2581        ( $form->{paymentaccounts} ) = split /--/, $form->{account};
2582    }
2583    if ( $form->{department} ) {
2584        ( $department, $form->{department_id} ) = split /--/,
2585          $form->{department};
2586        $option = $locale->text('Department') . " : $department";
2587    }
2588
2589    RP->payments( \%myconfig, \%$form );
2590
2591    @columns = $form->sort_columns(qw(transdate name paid source memo));
2592
2593    if ( $form->{till} ) {
2594        @columns =
2595          $form->sort_columns(qw(transdate name paid curr source till));
2596        if ( $myconfig{role} ne 'user' ) {
2597            @columns =
2598              $form->sort_columns(
2599                qw(transdate name paid curr source till employee));
2600        }
2601    }
2602
2603    # construct href
2604    $title = $form->escape( $form->{title} );
2605    $form->{paymentaccounts} =~ s/ /%20/g;
2606
2607    $href =
2608"$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="
2609      . $form->escape( $form->{title} );
2610
2611    $form->sort_order();
2612
2613    $form->{callback} =
2614"$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="
2615      . $form->escape( $form->{title}, 1 );
2616
2617    if ( $form->{account} ) {
2618        $callback .= "&account=" . $form->escape( $form->{account}, 1 );
2619        $href   .= "&account=" . $form->escape( $form->{account} );
2620        $option .= "\n<br>" if ($option);
2621        $option .= $locale->text('Account') . " : $form->{account}";
2622    }
2623    if ( $form->{department} ) {
2624        $callback .= "&department=" . $form->escape( $form->{department}, 1 );
2625        $href   .= "&department=" . $form->escape( $form->{department} );
2626        $option .= "\n<br>" if ($option);
2627        $option .= $locale->text('Department') . " : $form->{department}";
2628    }
2629    if ( $form->{description} ) {
2630        $callback .= "&description=" . $form->escape( $form->{description}, 1 );
2631        $href   .= "&description=" . $form->escape( $form->{description} );
2632        $option .= "\n<br>" if ($option);
2633        $option .= $locale->text('Description') . " : $form->{description}";
2634    }
2635    if ( $form->{source} ) {
2636        $callback .= "&source=" . $form->escape( $form->{source}, 1 );
2637        $href   .= "&source=" . $form->escape( $form->{source} );
2638        $option .= "\n<br>" if ($option);
2639        $option .= $locale->text('Source') . " : $form->{source}";
2640    }
2641    if ( $form->{memo} ) {
2642        $callback .= "&memo=" . $form->escape( $form->{memo}, 1 );
2643        $href   .= "&memo=" . $form->escape( $form->{memo} );
2644        $option .= "\n<br>" if ($option);
2645        $option .= $locale->text('Memo') . " : $form->{memo}";
2646    }
2647    if ( $form->{fromdate} ) {
2648        $option .= "\n<br>" if ($option);
2649        $option .=
2650            $locale->text('From') . "&nbsp;"
2651          . $locale->date( \%myconfig, $form->{fromdate}, 1 );
2652    }
2653    if ( $form->{todate} ) {
2654        $option .= "\n<br>" if ($option);
2655        $option .=
2656            $locale->text('To') . "&nbsp;"
2657          . $locale->date( \%myconfig, $form->{todate}, 1 );
2658    }
2659
2660    $callback = $form->escape( $form->{callback} );
2661
2662    $column_header{name} =
2663        "<th><a class=listheading href=$href&sort=name>"
2664      . $locale->text('Description')
2665      . "</a></th>";
2666    $column_header{transdate} =
2667        "<th><a class=listheading href=$href&sort=transdate>"
2668      . $locale->text('Date')
2669      . "</a></th>";
2670    $column_header{paid} =
2671      "<th class=listheading>" . $locale->text('Amount') . "</a></th>";
2672    $column_header{curr} =
2673      "<th class=listheading>" . $locale->text('Curr') . "</a></th>";
2674    $column_header{source} =
2675        "<th><a class=listheading href=$href&sort=source>"
2676      . $locale->text('Source')
2677      . "</a></th>";
2678    $column_header{memo} =
2679        "<th><a class=listheading href=$href&sort=memo>"
2680      . $locale->text('Memo')
2681      . "</a></th>";
2682
2683    $column_header{employee} =
2684        "<th><a class=listheading href=$href&sort=employee>"
2685      . $locale->text('Salesperson')
2686      . "</a></th>";
2687    $column_header{till} =
2688        "<th><a class=listheading href=$href&sort=till>"
2689      . $locale->text('Till')
2690      . "</a></th>";
2691
2692    @column_index = @columns;
2693    $colspan      = $#column_index + 1;
2694
2695    $form->header;
2696
2697    print qq|
2698<body>
2699
2700<table width=100%>
2701  <tr>
2702    <th class=listtop>$form->{title}</th>
2703  </tr>
2704  <tr height="5"></tr>
2705  <tr>
2706    <td>$option</td>
2707  </tr>
2708  <tr>
2709    <td>
2710      <table width=100%>
2711	<tr class=listheading>
2712|;
2713
2714    for (@column_index) { print "\n$column_header{$_}" }
2715
2716    print qq|
2717        </tr>
2718|;
2719
2720    foreach $ref ( sort { $a->{accno} cmp $b->{accno} } @{ $form->{PR} } ) {
2721
2722        next unless @{ $form->{ $ref->{id} } };
2723
2724        print qq|
2725        <tr>
2726	  <th colspan=$colspan align=left>$ref->{accno}--$ref->{description}</th>
2727	</tr>
2728|;
2729
2730        if ( @{ $form->{ $ref->{id} } } ) {
2731            $sameitem = $form->{ $ref->{id} }[0]->{ $form->{sort} };
2732        }
2733
2734        foreach $payment ( @{ $form->{ $ref->{id} } } ) {
2735
2736            if ( $form->{l_subtotal} ) {
2737                if ( $payment->{ $form->{sort} } ne $sameitem ) {
2738
2739                    # print subtotal
2740                    &payment_subtotal;
2741                }
2742            }
2743
2744            next if ( $form->{till} && !$payment->{till} );
2745
2746            $column_data{name}      = "<td>$payment->{name}&nbsp;</td>";
2747            $column_data{transdate} = "<td>$payment->{transdate}&nbsp;</td>";
2748            $column_data{paid} =
2749              "<td align=right>"
2750              . $form->format_amount( \%myconfig, $payment->{paid}, 2,
2751                "&nbsp;" )
2752              . "</td>";
2753            $column_data{curr}     = "<td>$payment->{curr}</td>";
2754            $column_data{source}   = "<td>$payment->{source}&nbsp;</td>";
2755            $column_data{memo}     = "<td>$payment->{memo}&nbsp;</td>";
2756            $column_data{employee} = "<td>$payment->{employee}&nbsp;</td>";
2757            $column_data{till}     = "<td>$payment->{till}&nbsp;</td>";
2758
2759            $subtotalpaid     += $payment->{paid};
2760            $accounttotalpaid += $payment->{paid};
2761            $totalpaid        += $payment->{paid};
2762
2763            $i++;
2764            $i %= 2;
2765            print qq|
2766	<tr class=listrow$i>
2767|;
2768
2769            for (@column_index) { print "\n$column_data{$_}" }
2770
2771            print qq|
2772        </tr>
2773|;
2774
2775            $sameitem = $payment->{ $form->{sort} };
2776
2777        }
2778
2779        &payment_subtotal if $form->{l_subtotal};
2780
2781        # print account totals
2782        for (@column_index) { $column_data{$_} = "<td>&nbsp;</td>" }
2783
2784        $column_data{paid} =
2785            "<th class=listtotal align=right>"
2786          . $form->format_amount( \%myconfig, $accounttotalpaid, 2, "&nbsp;" )
2787          . "</th>";
2788
2789        print qq|
2790	<tr class=listtotal>
2791|;
2792
2793        for (@column_index) { print "\n$column_data{$_}" }
2794
2795        print qq|
2796        </tr>
2797|;
2798
2799        $accounttotalpaid = 0;
2800
2801    }
2802
2803    # print total
2804    for (@column_index) { $column_data{$_} = "<td>&nbsp;</td>" }
2805
2806    $column_data{paid} =
2807      "<th class=listtotal align=right>"
2808      . $form->format_amount( \%myconfig, $totalpaid, 2, "&nbsp;" ) . "</th>";
2809
2810    print qq|
2811        <tr class=listtotal>
2812|;
2813
2814    for (@column_index) { print "\n$column_data{$_}" }
2815
2816    print qq|
2817        </tr>
2818
2819      </table>
2820    </td>
2821  </tr>
2822  <tr>
2823    <td><hr size=3 noshade></td>
2824  </tr>
2825</table>
2826|;
2827
2828    if ( $form->{lynx} ) {
2829        require "bin/menu.pl";
2830        &menubar;
2831    }
2832
2833    print qq|
2834
2835</body>
2836</html>
2837|;
2838
2839}
2840
2841sub payment_subtotal {
2842
2843    if ( $subtotalpaid != 0 ) {
2844        for (@column_index) { $column_data{$_} = "<td>&nbsp;</td>" }
2845
2846        $column_data{paid} =
2847            "<th class=listsubtotal align=right>"
2848          . $form->format_amount( \%myconfig, $subtotalpaid, 2, "&nbsp;" )
2849          . "</th>";
2850
2851        print qq|
2852  <tr class=listsubtotal>
2853|;
2854
2855        for (@column_index) { print "\n$column_data{$_}" }
2856
2857        print qq|
2858  </tr>
2859|;
2860    }
2861
2862    $subtotalpaid = 0;
2863
2864}
2865
2866