Subversion Repositories sql-ledger-old

Rev

Rev 114 | Details | Compare with Previous | Last modification | View Log | RSS feed

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