1#=====================================================================
2# SQL-Ledger
3# Copyright (c) DWS Systems Inc.
4#
5#  Author: DWS Systems Inc.
6#     Web: http://www.sql-ledger.com
7#
8#======================================================================
9#
10# Payment module
11#
12#======================================================================
13
14
15use SL::CP;
16use SL::OP;
17use SL::AA;
18use SL::VR;
19
20require "$form->{path}/arap.pl";
21require "$form->{path}/js.pl";
22
231;
24# end of main
25
26
27sub edit {
28
29  $form->{payment} = 'payment';
30
31  if ($form->{type} eq 'receipt') {
32    $form->{ARAP} = "AR";
33    $form->{arap} = "ar";
34    $form->{vc} = "customer";
35    $form->{formname} = "receipt";
36  }
37  if ($form->{type} eq 'check') {
38    $form->{ARAP} = "AP";
39    $form->{arap} = "ap";
40    $form->{vc} = "vendor";
41    $form->{formname} = "check";
42  }
43
44  CP->retrieve(\%myconfig, \%$form);
45
46  # departments
47  if (@{ $form->{all_department} }) {
48    $form->{selectdepartment} = "\n";
49    $form->{department} = "$form->{department}--$form->{department_id}" if $form->{department};
50
51    for (@{ $form->{all_department} }) { $form->{selectdepartment} .= qq|$_->{description}--$_->{id}\n| }
52  }
53
54  if (@{ $form->{all_language} }) {
55    $form->{selectlanguage} = "\n";
56    for (@{ $form->{all_language} }) { $form->{selectlanguage} .= qq|$_->{code}--$_->{description}\n| }
57  }
58
59  $form->{"select$form->{ARAP}"} = "";
60  $form->{"select$form->{ARAP}_paid"} = "";
61  $form->{"select$form->{ARAP}_discount"} = "";
62
63  for (@{ $form->{PR}{"$form->{ARAP}_discount"} }) { $form->{"select$form->{ARAP}_discount"} .= "$_->{accno}--$_->{description}\n" }
64  for (@{ $form->{PR}{"$form->{ARAP}_paid"} }) { $form->{"select$form->{ARAP}_paid"} .= "$_->{accno}--$_->{description}\n" }
65  for (@{ $form->{PR}{$form->{ARAP}} }) { $form->{"select$form->{ARAP}"} .= "$_->{accno}--$_->{description}\n" }
66
67  $form->error($locale->text('Payment account missing!')) unless $form->{"select$form->{ARAP}_paid"};
68
69# $locale->text('AR account missing!')
70# $locale->text('AP account missing!')
71  $form->error($locale->text("$form->{ARAP} account missing!")) unless $form->{"select$form->{ARAP}"};
72
73  # currencies
74  @curr = split /:/, $form->{currencies};
75  $form->{defaultcurrency} = $curr[0];
76  chomp $form->{defaultcurrency};
77
78  $form->{selectcurrency} = "";
79  for (@curr) { $form->{selectcurrency} .= "$_\n" }
80
81  $form->{currency} ||= $form->{defaultcurrency};
82
83  $form->{olddatepaid} = $form->{datepaid};
84
85  $form->{$form->{ARAP}} = $form->{"old$form->{ARAP}"} = $form->{arap_accno};
86
87  for ("$form->{vc}", "$form->{ARAP}", "$form->{ARAP}_paid", "$form->{ARAP}_discount") { $form->{"select$_"} = $form->escape($form->{"select$_"},1) }
88  for (qw(currency department business language account)) { $form->{"select$_"} = $form->escape($form->{"select$_"},1) }
89
90  $form->{media} ||= $myconfig{printer};
91  $form->{format} ||= $myconfig{outputformat};
92  $form->{format} ||= "pdf" unless $myconfig{printer};
93
94  if ($form->{batch}) {
95    if ($form->{transdate}) {
96      $form->{olddatepaid} = $form->{datepaid} = $form->{transdate};
97    }
98  }
99
100  # recreate payments
101  $form->{rowcount} = 0;
102
103  $i = 0;
104  if (@{ $form->{transactions} }) {
105    $form->{currency} = $form->{transactions}->[0]->{curr};
106
107    foreach $ref (@{ $form->{transactions} }) {
108      $i++;
109
110      for (qw(id invnumber invdescription transdate duedate calcdiscount discountterms cashdiscount)) { $form->{"${_}_$i"} = $ref->{$_} }
111      $ref->{exchangerate} ||= 1;
112      $form->{"netamount_$i"} = $form->round_amount($ref->{netamount} / $ref->{exchangerate}, $form->{precision});
113      $form->{amount} += $ref->{paid};
114      $ref->{due} = $ref->{amount} / $ref->{exchangerate};
115      $ref->{total} = $ref->{paid} + $ref->{discount};
116      for (qw(amount paid due discount total)) { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $ref->{$_}, $form->{precision}) }
117      $form->{"checked_$i"} = 1;
118    }
119  }
120  $form->{rowcount} = $i;
121
122  $form->{oldcurrency} = $form->{currency};
123  $form->{exchangerate} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{datepaid});
124
125  if (! $form->{readonly}) {
126    if ($form->{batch}) {
127      $form->{readonly} = 1 if $myconfig{acs} =~ /Vouchers--Payment Batch/ || $form->{approved};
128    }
129  }
130
131  &payment_header;
132  &list_invoices;
133  &payment_footer;
134
135}
136
137
138sub payment {
139
140  if ($form->{type} eq 'receipt') {
141    $form->{ARAP} = "AR";
142    $form->{arap} = "ar";
143    $form->{vc} = "customer";
144    $form->{formname} = "receipt";
145
146    $form->helpref("receipt", $myconfig{countrycode});
147  }
148  if ($form->{type} eq 'check') {
149    $form->{ARAP} = "AP";
150    $form->{arap} = "ap";
151    $form->{vc} = "vendor";
152    $form->{formname} = "check";
153
154    if ($form->{batch}) {
155      $form->helpref("payment_voucher", $myconfig{countrycode});
156    } else {
157      $form->helpref("payment", $myconfig{countrycode});
158    }
159  }
160
161  $form->{payment} = "payment";
162
163  $form->{callback} = "$form->{script}?action=payment&path=$form->{path}&login=$form->{login}&all_vc=$form->{all_vc}&type=$form->{type}" unless $form->{callback};
164
165  # setup customer/vendor selection for open invoices
166  if ($form->{all_vc}) {
167    $form->all_vc(\%myconfig, $form->{vc}, $form->{ARAP}, undef, $form->{datepaid});
168  } else {
169    CP->get_openvc(\%myconfig, \%$form);
170    if ($myconfig{vclimit} > 0) {
171      $form->{"all_$form->{vc}"} = $form->{name_list};
172    }
173    if (@{ $form->{"all_$form->{vc}"} }) {
174      $form->{$form->{vc}} = qq|$form->{"all_$form->{vc}"}->[0]->{name}--$form->{"all_$form->{vc}"}->[0]->{id}|;
175      $form->{"old$form->{vc}"} = $form->{$form->{vc}};
176
177      $form->{"$form->{vc}_id"} = $form->{"all_$form->{vc}"}->[0]->{id};
178
179      for (qw(address1 address2 city zipcode state country)) {
180        $form->{$_} = $form->{"all_$form->{vc}"}->[0]->{$_};
181      }
182      $form->{currency} = $form->{"all_$form->{vc}"}->[0]->{curr};
183    }
184
185  }
186
187  $form->{"select$form->{vc}"} = "";
188  if (@{ $form->{"all_$form->{vc}"} }) {
189    for (@{ $form->{"all_$form->{vc}"} }) { $form->{"select$form->{vc}"} .= qq|$_->{name}--$_->{id}\n| }
190  }
191
192  if (@{ $form->{all_language} }) {
193    $form->{selectlanguage} = "\n";
194    for (@{ $form->{all_language} }) { $form->{selectlanguage} .= qq|$_->{code}--$_->{description}\n| }
195  }
196
197  CP->paymentaccounts(\%myconfig, \%$form);
198
199  foreach $item (qw(department business paymentmethod)) {
200    if (@{ $form->{"all_$item"} }) {
201      $form->{"select$item"} = "\n";
202      $form->{$item} = qq|$form->{$item}--$form->{"${item}_id"}| if $form->{$item};
203
204      for (@{ $form->{"all_$item"} }) { $form->{"select$item"} .= qq|$_->{description}--$_->{id}\n| }
205    }
206  }
207
208  $form->{selectprinter} = "";
209  for (@{ $form->{all_printer} }) { $form->{selectprinter} .= "$_->{printer}\n" }
210  chop $form->{selectprinter};
211
212
213  $form->{"select$form->{ARAP}"} = "\n";
214  $form->{"select$form->{ARAP}_paid"} = "";
215  $form->{"select$form->{ARAP}_discount"} = "";
216
217  for (@{ $form->{PR}{"$form->{ARAP}_discount"} }) { $form->{"select$form->{ARAP}_discount"} .= "$_->{accno}--$_->{description}\n" }
218  for (@{ $form->{PR}{"$form->{ARAP}_paid"} }) { $form->{"select$form->{ARAP}_paid"} .= "$_->{accno}--$_->{description}\n" }
219  for (@{ $form->{PR}{$form->{ARAP}} }) { $form->{"select$form->{ARAP}"} .= "$_->{accno}--$_->{description}\n" }
220
221  # currencies
222  @curr = split /:/, $form->{currencies};
223  $form->{defaultcurrency} = $curr[0];
224  chomp $form->{defaultcurrency};
225
226  $form->{selectcurrency} = "";
227  for (@curr) { $form->{selectcurrency} .= "$_\n" }
228
229  $form->{currency} ||= $form->{defaultcurrency};
230  $form->{oldcurrency} = $form->{currency};
231
232  $form->{exchangerate} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{datepaid});
233
234  $form->{olddatepaid} = $form->{datepaid};
235
236  for ("$form->{vc}", "$form->{ARAP}", "$form->{ARAP}_paid", "$form->{ARAP}_discount") { $form->{"select$_"} = $form->escape($form->{"select$_"},1) }
237  for (qw(currency department business language account paymentmethod printer)) { $form->{"select$_"} = $form->escape($form->{"select$_"},1) }
238
239  $form->{media} ||= $myconfig{printer};
240  $form->{format} ||= $myconfig{outputformat};
241  $form->{format} ||= "pdf" unless $myconfig{printer};
242
243  if ($form->{batch}) {
244    if (! $form->{transdate}) {
245      $form->{transdate} = $form->{datepaid};
246    }
247    $form->{olddatepaid} = $form->{datepaid} = $form->{transdate};
248    $form->{memo} ||= $form->{batchdescription};
249  }
250
251  &update_payment;
252
253}
254
255
256sub prepare_payments_header {
257
258  if ($form->{type} eq 'receipt') {
259    $form->{title} = $locale->text('Receipt');
260  }
261  if ($form->{type} eq 'check') {
262    $form->{title} = $locale->text('Payment');
263  }
264
265  if ($form->{batch}) {
266    $form->{title} .= " ".$locale->text('Voucher');
267    if ($form->{batchdescription}) {
268      $form->{title} .= " / $form->{batchdescription}";
269    }
270  }
271
272  for $i (1 .. $form->{rowcount}) {
273    if ($form->{"detail_$i"}) {
274      $form->{"$form->{vc}_id"} = $form->{"$form->{vc}_id_$i"};
275      $form->{$form->{vc}} = qq|$form->{"name_$i"}--$form->{"$form->{vc}_id_$i"}|;
276      $form->{"$form->{vc}number"} = $form->{"$form->{vc}number_$i"};
277      $form->{"old$form->{vc}number"} = $form->{"$form->{vc}number_$i"};
278      $form->{"old$form->{vc}"} = qq|$form->{"name_$i"}--$form->{"$form->{vc}_id_$i"}|;
279      $form->{"select$form->{vc}"} = $form->escape($form->{$form->{vc}},1);
280
281      for (qw(datepaid duedatefrom duedateto)) { $form->{"old$_"} = $form->{$_} }
282      last;
283    }
284  }
285
286  $form->{payment} = "payment";
287  $form->{allbox} = 1;
288
289  CP->get_openinvoices(\%myconfig, \%$form);
290
291  for ("currency","$form->{ARAP}","$form->{ARAP}_paid","$form->{ARAP}_discount","department","business","paymentmethod") {
292    $form->{"old$_"} = $form->{$_};
293  }
294
295  $exchangerate = $form->{exchangerate};
296
297  AA->get_name(\%myconfig, \%$form);
298
299  for ("currency","$form->{ARAP}","$form->{ARAP}_paid","$form->{ARAP}_discount","department","business","paymentmethod") {
300    $form->{$_} = $form->{"old$_"};
301  }
302
303  $form->{$form->{vc}} = qq|$form->{"$form->{vc}"}--$form->{"$form->{vc}_id"}|;
304
305  $form->{exchangerate} = $exchangerate;
306  $form->{rowcount} = 0;
307
308  $i = 0;
309  foreach $ref (@{ $form->{PR} }) {
310    $i++;
311
312    for (qw(id invnumber invdescription transdate duedate calcdiscount discountterms cashdiscount netamount)) { $form->{"${_}_$i"} = $ref->{$_} }
313    $ref->{exchangerate} ||= 1;
314    $due = ($form->{edit}) ? $ref->{amount} : $ref->{amount} - $ref->{paid};
315    $due = $form->round_amount($due / $ref->{exchangerate}, $form->{precision});
316    $netamount = $form->round_amount($ref->{netamount} / $ref->{exchangerate}, $form->{precision});
317
318    if ($ref->{calcdiscount}) {
319      $discount = $form->round_amount($netamount * $ref->{cashdiscount}, $form->{precision})
320    }
321
322    $form->{amount} += $due - $discount;
323
324    $form->{"due_$i"} = $form->format_amount(\%myconfig, $due, $form->{precision});
325    $form->{"discount_$i"} = $form->format_amount(\%myconfig, $discount, $form->{precision});
326    $form->{"amount_$i"} = $form->format_amount(\%myconfig, $ref->{amount} / $ref->{exchangerate}, $form->{precision});
327    $form->{"netamount_$i"} = $form->format_amount(\%myconfig, $netamount, $form->{precision});
328
329    $form->{"olddiscount_$i"} = $form->{"discount_$i"};
330
331    $form->{"checked_$i"} = 1;
332    $form->{"paid_$i"} = $form->format_amount(\%myconfig, $due - $discount, $form->{precision});
333    $form->{"total_$i"} = $form->format_amount(\%myconfig, $due, $form->{precision});
334  }
335  $form->{rowcount} = $i;
336
337  ($accno) = split /--/, $form->{"$form->{ARAP}_paid"};
338  $form->{source} = $form->{"$form->{type}_$accno"};
339
340  &payment_header;
341
342}
343
344
345sub payments {
346
347  if ($form->{type} eq 'receipt') {
348    $form->{ARAP} = "AR";
349    $form->{arap} = "ar";
350    $form->{vc} = "customer";
351    $form->{formname} = "receipt";
352
353    $form->helpref("receipts", $myconfig{countrycode});
354  }
355  if ($form->{type} eq 'check') {
356    $form->{ARAP} = "AP";
357    $form->{arap} = "ap";
358    $form->{vc} = "vendor";
359    $form->{formname} = "check";
360
361    if ($form->{batch}) {
362      $form->helpref("payments_voucher", $myconfig{countrycode});
363    } else {
364      $form->helpref("payments", $myconfig{countrycode});
365    }
366  }
367
368
369  $form->{payment} = "payments";
370
371  $form->{callback} = "$form->{script}?action=payments&path=$form->{path}&login=$form->{login}&type=$form->{type}" unless $form->{callback};
372
373  CP->paymentaccounts(\%myconfig, \%$form);
374
375  if (@{ $form->{all_language} }) {
376    $form->{selectlanguage} = "\n";
377    for (@{ $form->{all_language} }) { $form->{selectlanguage} .= qq|$_->{code}--$_->{description}\n| }
378  }
379
380  foreach $item (qw(department business paymentmethod)) {
381    if (@{ $form->{"all_$item"} }) {
382      $form->{"select$item"} = "\n";
383      $form->{$item} = qq|$form->{$item}--$form->{"${item}_id"}| if $form->{$item};
384
385      for (@{ $form->{"all_$item"} }) { $form->{"select$item"} .= qq|$_->{description}--$_->{id}\n| }
386    }
387  }
388
389  $form->{"select$form->{ARAP}"} = "\n";
390  $form->{"select$form->{ARAP}_paid"} = "";
391  $form->{"select$form->{ARAP}_discount"} = "";
392
393  for (@{ $form->{PR}{"$form->{ARAP}_paid"} }) { $form->{"select$form->{ARAP}_paid"} .= "$_->{accno}--$_->{description}\n" }
394  for (@{ $form->{PR}{"$form->{ARAP}_discount"} }) { $form->{"select$form->{ARAP}_discount"} .= "$_->{accno}--$_->{description}\n" }
395  for (@{ $form->{PR}{$form->{ARAP}} }) { $form->{"select$form->{ARAP}"} .= "$_->{accno}--$_->{description}\n" }
396
397  $form->{selectprinter} = "";
398  for (@{ $form->{all_printer} }) { $form->{selectprinter} .= "$_->{printer}\n" }
399  chop $form->{selectprinter};
400
401  # currencies
402  @curr = split /:/, $form->{currencies};
403  $form->{defaultcurrency} = $curr[0];
404  chomp $form->{defaultcurrency};
405
406  $form->{selectcurrency} = "";
407  for (@curr) { $form->{selectcurrency} .= "$_\n" }
408
409  $form->{oldcurrency} = $form->{currency} = $form->{defaultcurrency};
410  $form->{oldduedateto} = $form->{datepaid};
411  $form->{olddatepaid} = $form->{datepaid};
412
413  for ("$form->{vc}", "$form->{ARAP}", "$form->{ARAP}_paid", "$form->{ARAP}_discount") { $form->{"select$_"} = $form->escape($form->{"select$_"},1) }
414  for (qw(currency department business language account paymentmethod printer)) { $form->{"select$_"} = $form->escape($form->{"select$_"},1) }
415
416  $form->{media} = $myconfig{printer};
417  $form->{format} ||= $myconfig{outputformat};
418  $form->{format} ||= "pdf" unless $myconfig{printer};
419
420  if ($form->{batch}) {
421    if (! $form->{transdate}) {
422      $form->{transdate} = $form->{datepaid};
423    }
424    $form->{olddatepaid} = $form->{datepaid} = $form->{transdate};
425  }
426
427  &payments_header;
428  &invoices_due;
429  &payments_footer;
430
431}
432
433
434sub payments_header {
435
436  if ($form->{type} eq 'receipt') {
437    $form->{title} = $locale->text('Receipts');
438  }
439  if ($form->{type} eq 'check') {
440    $form->{title} = $locale->text('Payments');
441  }
442
443  if ($form->{batch}) {
444    $form->{title} .= " ".$locale->text('Voucher');
445    if ($form->{batchdescription}) {
446      $form->{title} .= " / $form->{batchdescription}";
447    }
448  }
449
450  if ($form->{defaultcurrency}) {
451    $exchangerate = qq|
452	      <tr>
453		<th align=right nowrap>|.$locale->text('Currency').qq|</th>
454		<td><select name=currency onChange="javascript:document.main.submit()">|
455		.$form->select_option($form->{selectcurrency}, $form->{currency})
456		.qq|</select></td>
457	      </tr>
458|;
459  }
460
461  if ($form->{currency} ne $form->{defaultcurrency}) {
462    $form->{exchangerate} = $form->format_amount(\%myconfig, $form->{exchangerate});
463
464    $exchangerate .= qq|
465 	      <tr>
466		<th align=right nowrap>|.$locale->text('Exchange Rate').qq|</th>
467		<td colspan=3><input name=exchangerate class="inputright" size=10 value=$form->{exchangerate}></td>
468	      </tr>
469|;
470  }
471
472  $department = qq|
473              <tr>
474	        <th align="right" nowrap>|.$locale->text('Department').qq|</th>
475		<td><select name=department onChange="javascript:document.main.submit()">|
476		.$form->select_option($form->{selectdepartment}, $form->{department}, 1).qq|
477		</select>
478	      </td>
479	    </tr>
480| if $form->{selectdepartment};
481
482  $business = qq|
483              <tr>
484	        <th align="right" nowrap>|.$locale->text('Type of Business').qq|</th>
485		<td><select name=business onChange="javascript:document.main.submit()">|
486		.$form->select_option($form->{selectbusiness}, $form->{business}, 1).qq|
487		</select>
488	      </td>
489	    </tr>
490| if $form->{selectbusiness};
491
492  $paymentmethod = qq|
493              <tr>
494	        <th align="right" nowrap>|.$locale->text('Payment Method').qq|</th>
495		<td><select name=paymentmethod onChange="javascript:document.main.submit()">|
496		.$form->select_option($form->{selectpaymentmethod}, $form->{paymentmethod}, 1).qq|
497		</select>
498	      </td>
499	    </tr>
500| if $form->{selectpaymentmethod};
501
502
503  $cashdiscount = qq|
504 	      <tr>
505		<th align=right nowrap>|.$locale->text('Cash Discount').qq|</th>
506		<td colspan=3><select name="$form->{ARAP}_discount">|
507		.$form->select_option($form->{"select$form->{ARAP}_discount"}, $form->{"$form->{ARAP}_discount"}).qq|</select>
508		</td>
509	      </tr>
510| if $form->{"select$form->{ARAP}_discount"};
511
512
513  if ($form->{batch}) {
514    $datepaid = qq|
515		<td>$form->{datepaid}</td>
516		<input type=hidden name=datepaid value="$form->{datepaid}"></td>
517|;
518  } else {
519    $datepaid = qq|
520		<td><input name=datepaid value="$form->{datepaid}" title="$myconfig{dateformat}" size=11 class=date>|.&js_calendar("main", "datepaid").qq|</td>
521|;
522  }
523
524
525  $form->header;
526
527  &calendar;
528
529  &check_all(qw(allbox checked_));
530
531  print qq|
532<script language="javascript">
533<!--
534javascript:window.history.forward(1);
535// -->
536</script>
537
538<body>
539
540<form method="post" name="main" action="$form->{script}">
541|;
542
543  $form->hide_form(qw(defaultcurrency closedto vc type formname arap ARAP title payment batch batchid batchnumber batchdescription transdate edit voucherid employee cdt precision));
544  $form->hide_form(map { "old$_" } qw(currency datepaid duedatefrom duedateto department business paymentmethod));
545  $form->hide_form(map { "old$_" } ("$form->{ARAP}", "$form->{ARAP}_paid", "$form->{vc}", "$form->{vc}number"));
546  $form->hide_form(map { "select$_" } qw(currency department business language account paymentmethod printer));
547  $form->hide_form(map { "select$_" } ("$form->{ARAP}", "$form->{ARAP}_paid", "$form->{ARAP}_discount"));
548
549  for (split /%0a/, $form->{"select$form->{ARAP}_paid"}) {
550    ($accno) = split /--/, $_;
551    $form->hide_form("$form->{type}_$accno");
552  }
553
554  print qq|
555<table width=100%>
556  <tr>
557    <th class=listtop>$form->{helpref}$form->{title}</a></th>
558  </tr>
559  <tr height="5"></tr>
560  <tr>
561    <td>
562      <table width=100%>
563        <tr valign=top>
564	  <td>
565	    <table>
566	      <tr>
567          <th align=right>|.$locale->text('Due Date').qq|</th>
568          <td>
569            <table>
570              <tr>
571                <th align=right>|.$locale->text('From').qq|</th>
572                <td><input name=duedatefrom value="$form->{duedatefrom}" title="$myconfig{dateformat}" size=11 class=date>|.&js_calendar("main", "duedatefrom").qq|</td>
573                <th align=right>|.$locale->text('To').qq|</th>
574                <td><input name=duedateto value="$form->{duedateto}" title="$myconfig{dateformat}" size=11 class=date>|.&js_calendar("main", "duedateto").qq|</td>
575              </tr>
576            </table>
577          </td>
578              </tr>
579              $department
580              $business
581            </table>
582          </td>
583          <td>
584            <table>
585              <tr>
586                <th align=right nowrap>|.$locale->text($form->{ARAP}).qq|</th>
587                <td colspan=3><select name=$form->{ARAP} onChange="javascript:document.main.submit()">|
588                .$form->select_option($form->{"select$form->{ARAP}"}, $form->{"$form->{ARAP}"}).qq|</select>
589                </td>
590              </tr>
591              <tr>
592                <th align=right nowrap>|.$locale->text('Payment').qq|</th>
593                <td colspan=3><select name="$form->{ARAP}_paid" onChange="javascript:document.main.submit()">|
594                .$form->select_option($form->{"select$form->{ARAP}_paid"}, $form->{"$form->{ARAP}_paid"}).qq|</select>
595                </td>
596              </tr>
597              $paymentmethod
598              $cashdiscount
599              <tr>
600                <th align=right nowrap>|.$locale->text('Date').qq|</th>
601                $datepaid
602              </tr>
603              $exchangerate
604            </table>
605          </td>
606        </tr>
607      </table>
608    </td>
609  </tr>
610|;
611
612}
613
614
615sub invoices_due {
616
617  @column_index = ();
618  push @column_index, qw(detail name);
619  push @column_index, "$form->{vc}number";
620  push @column_index, qw(amount due checked paid memo source);
621  push @column_index, "language" if $form->{selectlanguage};
622
623  $colspan = $#column_index + 1;
624
625  $invoice = $locale->text('Invoices');
626
627  if ($form->{vc} eq 'customer') {
628    $vcname = $locale->text('Customer');
629    $vcnumber = $locale->text('Customer Number');
630  } else {
631    $vcname = $locale->text('Vendor');
632    $vcnumber = $locale->text('Vendor Number');
633  }
634
635  print qq|
636  <tr>
637    <td>
638      <table width=100%>
639        <tr>
640          <th class=listheading colspan=$colspan>$invoice</th>
641        </tr>
642|;
643
644  $column_data{detail} = qq|<th></th>|;
645  $column_data{"$form->{vc}number"} = qq|<th>$vcnumber</th>|;
646  $column_data{name} = qq|<th>$vcname</th>|;
647  $column_data{amount} = qq|<th>|.$locale->text('Amount')."</th>";
648  $column_data{due} = qq|<th>|.$locale->text('Due')."</th>";
649  $column_data{paid} = qq|<th>|.$locale->text('Paid')."</th>";
650
651  $form->{allbox} = ($form->{allbox}) ? "checked" : "";
652
653  $column_data{checked} = qq|<th><input name="allbox" type=checkbox class=checkbox value="1" $form->{allbox} onChange="CheckAll(); javascript:document.main.submit()"><input type=hidden name=action value="update"></th>|;
654
655  $column_data{memo} = qq|<th>|.$locale->text('Memo')."</th>";
656  $column_data{source} = qq|<th>|.$locale->text('Source')."</th>";
657  $column_data{language} = qq|<th>|.$locale->text('Language')."</th>";
658
659  print qq|
660        <tr>
661|;
662  for (@column_index) { print "$column_data{$_}\n" }
663  print qq|
664        </tr>
665|;
666
667  $sameid = 0;
668
669  for $i (1 .. $form->{rowcount}) {
670
671    for (qw(amount paid due)) { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) }
672
673    $totalamount += $form->{"amount_$i"};
674    $totaldue += $form->{"due_$i"};
675    $totalpaid += $form->{"paid_$i"};
676
677    for (qw(amount due paid)) { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $form->{precision}) }
678
679    $form->hide_form(map { "${_}_$i" } qw(name id amount due));
680
681    for (qw(amount due)) { $column_data{$_} = qq|<td align=right>$form->{"${_}_$i"}</td>| }
682
683    $column_data{paid} = qq|<td align=center><input name="paid_$i" class="inputright" size=11 value=$form->{"paid_$i"}></td>|;
684
685    $form->hide_form("$form->{vc}_id_$i", "$form->{vc}number_$i");
686
687    $form->{"checked_$i"} = ($form->{"checked_$i"}) ? "checked" : "";
688    $column_data{checked} = qq|<td align=center><input name="checked_$i" type=checkbox class=checkbox $form->{"checked_$i"} onChange="javascript:document.main.submit()"></td>|;
689
690    $form->{"detail_$i"} = ($form->{"detail_$i"}) ? "checked" : "";
691    $column_data{detail} = qq|<td align=center><input name="detail_$i" type=checkbox class=checkbox $form->{"detail_$i"} onChange="javascript:document.main.submit()"></td>|;
692
693    $column_data{"$form->{vc}number"} = qq|<td>$form->{"$form->{vc}number_$i"}</td>|;
694    $column_data{name} = qq|<td>$form->{"name_$i"}</td>|;
695
696    $column_data{memo} = qq|<td align=center><input name="memo_$i" size=20 value="|.$form->quote($form->{"memo_$i"}).qq|"></td>|;
697    $column_data{source} = qq|<td align=center><input name="source_$i" size=10 value="|.$form->quote($form->{"source_$i"}).qq|"></td>|;
698
699    if ($form->{selectlanguage}) {
700      $column_data{language} = qq|<td><select name="language_code_$i">|.$form->select_option($form->{selectlanguage}, $form->{"language_code_$i"}, undef, 1).qq|</select></td>|;
701    }
702
703    $j++; $j %= 2;
704    print qq|
705	<tr class=listrow$j>
706|;
707    for (@column_index) { print "$column_data{$_}\n" }
708    print qq|
709        </tr>
710|;
711
712    $sameid = $form->{"$form->{vc}_id_$i"};
713
714  }
715
716  for (@column_index) { $column_data{$_} = "<td>&nbsp;</td>" }
717
718  $column_data{amount} = qq|<th class=listtotal align=right>|.$form->format_amount(\%myconfig, $totalamount, $form->{precision}, "&nbsp;").qq|</th>|;
719  $column_data{due} = qq|<th class=listtotal align=right>|.$form->format_amount(\%myconfig, $totaldue, $form->{precision}, "&nbsp;").qq|</th>|;
720  $column_data{paid} = qq|<th class=listtotal align=right>|.$form->format_amount(\%myconfig, $totalpaid, $form->{precision}, "&nbsp;").qq|</th>|;
721
722  print qq|
723        <tr class=listtotal>
724|;
725  for (@column_index) { print "$column_data{$_}\n" }
726  print qq|
727        </tr>
728      </table>
729    </td>
730  </tr>
731|;
732
733}
734
735
736sub payments_footer {
737
738  $form->{DF}{$form->{format}} = "selected";
739
740  $transdate = $form->datetonum(\%myconfig, $form->{datepaid});
741
742  $media = qq|<select name=media>
743	<option value=screen>|.$locale->text('Screen');
744  if ($form->{selectprinter}) {
745    for (split /\n/, $form->unescape($form->{selectprinter})) { $media .= qq|
746	  <option value="$_">$_| }
747  }
748  $media .= qq|</select>|;
749
750  $format = qq|<select name=format>|;
751
752#  <option value="html" $form->{DF}{html}>|.$locale->text('html').qq|
753#  <option value="xml" $form->{DF}{xml}>|.$locale->text('XML').qq|
754#  <option value="txt" $form->{DF}{txt}>|.$locale->text('Text');
755
756  if ($latex) {
757    $format .= qq|
758            <option value="ps" $form->{DF}{ps}>|.$locale->text('Postscript').qq|
759	    <option value="pdf" $form->{DF}{pdf}>|.$locale->text('PDF');
760  }
761  $format .= qq|</select>|;
762
763  if (! $latex) {
764    $format = "";
765    $media = "";
766  }
767
768  print qq|
769  <tr>
770    <td><hr size=3 noshade></td>
771  </tr>
772</table>
773|;
774
775  %button = ('Update' => { ndx => 1, key => 'U', value => $locale->text('Update') },
776	     'Select all' => { ndx => 3, key => 'A', value => $locale->text('Select all') },
777	     'Deselect all' => { ndx => 4, key => 'A', value => $locale->text('Deselect all') },
778             'Preview' => { ndx => 5, key => 'V', value => $locale->text('Preview') },
779             'Print' => { ndx => 6, key => 'P', value => $locale->text('Print') },
780	     'Post' => { ndx => 7, key => 'O', value => $locale->text('Post') },
781	    );
782
783  if ($form->{allbox}) {
784    delete $button{'Select all'};
785  } else {
786    delete $button{'Deselect all'};
787  }
788
789  if (! $latex) {
790    for ('Print', 'Preview') { delete $button{$_} }
791  }
792
793  if ($transdate <= $form->{closedto}) {
794    for ('Post', 'Print', 'Preview') { delete $button{$_} }
795    $media = $format = "";
796  }
797
798  if (! $form->{payments_detail}) {
799    delete $button{'Back'};
800  }
801
802  $form->print_button(\%button);
803
804  $media =~ s/(<option value="\Q$form->{media}\E")/$1 selected/;
805
806  print qq|
807  $format
808  $media
809|;
810
811  $form->hide_form(qw(helpref callback rowcount path login));
812
813  if ($form->{menubar}) {
814    require "$form->{path}/menu.pl";
815    &menubar;
816  }
817
818  print qq|
819  </form>
820
821</body>
822</html>
823|;
824
825}
826
827
828sub select_all {
829
830  unless ($form->{payment}) {
831    $form->{callback} =~ s/(allbox|deselect)=//g;
832    $form->{callback} .= "&allbox=1&deselect=1";
833    for (1 .. $form->{rowcount}) { $form->{callback} .= "&id_${_}=1" }
834
835    $form->redirect;
836  }
837
838  $source = $form->{"source_1"};
839
840  $form->{"checked_1"} = 1;
841
842  for (2 .. $form->{rowcount}) {
843    $form->{"checked_$_"} = 1;
844    $source =~ s/(\d+)/$1 + 1/e;
845    $form->{"source_$_"} = $source;
846  }
847
848  for (1 .. $form->{rowcount}) {
849    $due = $form->parse_amount(\%myconfig, $form->{"due_$_"});
850    if ($form->{"calcdiscount_$_"}) {
851      $form->{"discount_$_"} = $form->parse_amount(\%myconfig, $form->{"netamount_$_"}) * $form->{"cashdiscount_$_"};
852    }
853
854    $form->{"paid_$_"} = $form->format_amount(\%myconfig, $due - $form->{"discount_$_"}, $form->{precision});
855  }
856
857  $form->{allbox} = 1;
858
859  &{"update_$form->{payment}"};
860
861}
862
863
864sub deselect_all {
865
866  $form->redirect unless $form->{payment};
867
868  for (1 .. $form->{rowcount}) {
869    for my $item (qw(vc checked source memo)) { $form->{"${item}_$_"} = "" };
870  }
871
872  $form->{amount} = 0;
873  $form->{allbox} = "";
874
875  &{"update_$form->{payment}"};
876
877}
878
879
880sub update { &{"update_$form->{payment}"} }
881
882
883sub update_payments {
884
885  for (1 .. $form->{rowcount}) {
886    if ($form->{"detail_$_"}) {
887      $form->{payments_detail} = 1;
888      &prepare_payments_header;
889      &list_invoices;
890      &payment_footer;
891      exit;
892    }
893  }
894
895  $form->{exchangerate} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{datepaid});
896  for ("datepaid", "duedatefrom", "duedateto", "department", "business", "currency", "$form->{ARAP}", "$form->{ARAP}_paid", "paymentmethod") {
897    if ($form->{$_} ne $form->{"old$_"}) {
898      if (!$form->{redo}) {
899	$form->remove_locks(\%myconfig, undef, $form->{arap});
900	CP->get_openinvoices(\%myconfig, \%$form);
901	$form->{redo} = 1;
902      }
903    }
904    $form->{"old$_"} = $form->{$_};
905  }
906
907  if ($form->{redo}) {
908    $form->{allbox} = "";
909
910    for $i (1 .. $form->{rowcount}) {
911      for (qw(id amount due paid totaldue)) { $form->{"${_}_$i"} = "" }
912    }
913
914    $i = 0;
915    foreach $ref (@{ $form->{PR} }) {
916
917      if ($ref->{"$form->{vc}_id"} != $sameid) {
918        chop $form->{"id_$i"};
919        $i++;
920      }
921
922      $amount = $form->round_amount($ref->{amount} / $ref->{exchangerate}, $form->{precision});
923      $paid = $form->round_amount($ref->{paid} / $ref->{exchangerate}, $form->{precision});
924
925      $form->{"amount_$i"} += $amount;
926      $form->{"due_$i"} += $amount - $paid;
927
928      if ($form->{"checked_$i"}) {
929        $form->{"paid_$i"} += $amount - $paid;
930        $form->{"totaldue_$i"} += $amount - $paid;
931      }
932
933      $form->{"id_$i"} .= "$ref->{id} ";
934
935      $form->{"name_$i"} = $ref->{name};
936      for (qw(_id number)) { $form->{"$form->{vc}${_}_$i"} = $ref->{"$form->{vc}$_"} };
937
938      $sameid = $ref->{"$form->{vc}_id"};
939
940    }
941    $form->{rowcount} = $i;
942    chop $form->{"id_$i"};
943
944    # format paid
945    for $i (1 .. $form->{rowcount}) {
946      for (qw(amount paid due)) { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $form->{precision}) }
947    }
948
949  }
950
951  $ndx = 1;
952  $source = "";
953  $done = 0;
954  ($accno) = split /--/, $form->{"$form->{ARAP}_paid"};
955
956  for (1 .. $form->{rowcount}) {
957    $form->{"totaldue_$_"} = 0;
958
959    if ($form->{"checked_$_"}) {
960      $form->{"source_$_"} = $form->{"$form->{type}_$accno"} if $form->{redo} || $form->{"source_$_"} eq "";
961      if (! $done) {
962        $ndx = $_;
963        $source = $form->{"source_$_"};
964        $source =~ s/(\d+)/$1 - 1/e;
965        $done = 1;
966      }
967    } else {
968      $form->{"source_$_"} = "";
969      $form->{"paid_$_"} = "";
970    }
971  }
972
973  for ($ndx .. $form->{rowcount}) {
974    if ($form->{"checked_$_"}) {
975      $source =~ s/(\d+)/$1 + 1/e;
976      $form->{"source_$_"} = $source;
977      if (! $form->{"paid_$_"}) {
978        $form->{"paid_$_"} = $form->{"due_$_"};
979      }
980    }
981  }
982
983  &payments_header;
984  &invoices_due;
985  &payments_footer;
986
987}
988
989
990sub update_payment {
991
992  if ($form->{"$form->{vc}"}) {
993    if ($form->{"$form->{vc}"} !~ /--/) {
994      $name = qq|$form->{"$form->{vc}"}--$form->{"$form->{vc}_id"}|;
995      $new_name_selected = 1 if $name ne $form->{"old$form->{vc}"};
996    } else {
997      $new_name_selected = 1 if $form->{"$form->{vc}"} ne $form->{"old$form->{vc}"};
998    }
999  }
1000
1001  if ($new_name_selected) {
1002    for ("$form->{ARAP}", "$form->{ARAP}_paid", "department", "business", "paymentmethod") {
1003      $form->{$_} = $form->{"old$_"};
1004    }
1005  }
1006
1007  ($accno) = split /--/, $form->{"$form->{ARAP}_paid"};
1008  if ($form->{"old$form->{ARAP}_paid"} ne $form->{"$form->{ARAP}_paid"}) {
1009    $form->{source} = $form->{"$form->{type}_$accno"};
1010  }
1011  $form->{source} = ($form->{"$form->{type}_$accno"}) unless $form->{source};
1012
1013  $department = $form->{department};
1014  $business = $form->{business};
1015  $currency = $form->{currency};
1016  $paymentmethod = $form->{paymentmethod};
1017  $arappaid = $form->{"$form->{ARAP}_paid"};
1018
1019  if (! $form->{all_vc}) {
1020
1021    if ($form->{$form->{ARAP}} ne $form->{"old$form->{ARAP}"} ||
1022        $form->{business} ne $form->{oldbusiness} ||
1023        $form->{department} ne $form->{olddepartment}) {
1024
1025      for ("$form->{ARAP}", "business", "department") { $form->{"old$_"} = $form->{$_}}
1026
1027      $rv = CP->get_openvc(\%myconfig, \%$form);
1028
1029      if ($myconfig{vclimit} > 0) {
1030        $form->{"all_$form->{vc}"} = $form->{name_list};
1031      } else {
1032
1033        if ($rv > 1) {
1034          # assign old values
1035          for ("$form->{ARAP}", "department", "business") {
1036            $form->{"old$_"} = $form->{$_};
1037          }
1038          &select_name($form->{vc});
1039          exit;
1040        }
1041
1042        if ($rv == 1) {
1043          # we got one name
1044          $form->{"$form->{vc}_id"} = $form->{name_list}[0]->{id};
1045          $form->{$form->{vc}} = $form->{name_list}[0]->{name};
1046          $form->{"$form->{vc}number"} = $form->{name_list}[0]->{"$form->{vc}number"};
1047          $form->{currency} = $form->{name_list}[0]->{curr};
1048          $form->{"old$form->{vc}"} = "";
1049          $form->{"old$form->{vc}number"} = "";
1050        } else {
1051          # nothing open
1052          $form->{"$form->{vc}"} = "";
1053          $form->{"$form->{vc}_id"} = 0;
1054          $form->{"$form->{vc}number"} = "";
1055        }
1056
1057      }
1058
1059      $form->{"select$form->{vc}"} = "";
1060      if (@{ $form->{"all_$form->{vc}"} }) {
1061        for (@{ $form->{"all_$form->{vc}"} }) { $form->{"select$form->{vc}"} .= qq|$_->{name}--$_->{id}\n| }
1062      }
1063      $form->{"select$form->{vc}"} = $form->escape($form->{"select$form->{vc}"},1);
1064    }
1065  }
1066
1067  # get customer/vendor
1068  &check_openvc;
1069
1070  $form->{"$form->{ARAP}_paid"} = $arappaid;
1071  $form->{department} = $department;
1072  $form->{business} = $business;
1073  $form->{currency} ||= $currency;
1074  $form->{paymentmethod} = $paymentmethod;
1075
1076  if ($form->{datepaid} ne $form->{olddatepaid}) {
1077    $form->{olddatepaid} = $form->{datepaid};
1078    $form->{redo} = 1;
1079    $form->{oldall_vc} = !$form->{oldall_vc} if $form->{all_vc};
1080  }
1081
1082  for ("duedatefrom", "duedateto", "department", "business", "$form->{ARAP}", "currency", "paymentmethod") {
1083    if ($form->{$_} ne $form->{"old$_"}) {
1084      $form->{redo} = 1;
1085    }
1086    $form->{"old$_"} = $form->{$_};
1087  }
1088
1089  if ($form->{redo}) {
1090    $form->{allbox} = "";
1091    $form->remove_locks(\%myconfig, undef, $form->{arap}) unless $form->{locks_removed};
1092  }
1093
1094  # if we switched to all_vc
1095  if ($form->{all_vc} ne $form->{oldall_vc}) {
1096
1097    $form->{redo} = 1;
1098
1099    $form->{"select$form->{vc}"} = "";
1100    $form->{selectbusiness} = "";
1101    $form->{selectpaymentmethod} = "";
1102    $business = "";
1103    $paymentmethod = "";
1104
1105    if ($form->{all_vc}) {
1106      $form->{business} = "";
1107      $form->{oldbusiness} = "";
1108      $form->{paymentmethod} = "";
1109      $form->{oldpaymentmethod} = "";
1110
1111      $form->all_vc(\%myconfig, $form->{vc}, $form->{ARAP}, undef, $form->{datepaid});
1112
1113      if (@{ $form->{"all_$form->{vc}"} }) {
1114        for (@{ $form->{"all_$form->{vc}"} }) { $form->{"select$form->{vc}"} .= qq|$_->{name}--$_->{id}\n| }
1115        $form->{"select$form->{vc}"} = $form->escape($form->{"select$form->{vc}"},1);
1116      }
1117
1118    } else {
1119      if ($myconfig{vclimit} > 0) {
1120        $form->{$form->{vc}} = "";
1121        $form->{"$form->{vc}number"} = "";
1122      }
1123
1124      $form->remove_locks(\%myconfig, undef, $form->{arap}) unless $form->{locks_removed};
1125
1126      CP->get_openvc(\%myconfig, \%$form);
1127
1128      if ($myconfig{vclimit} > 0) {
1129        $form->{"all_$form->{vc}"} = $form->{name_list};
1130      }
1131
1132      if (@{ $form->{"all_$form->{vc}"} }) {
1133        $newvc = qq|$form->{"all_$form->{vc}"}[0]->{name}--$form->{"all_$form->{vc}"}[0]->{id}|;
1134        for (@{ $form->{"all_$form->{vc}"} }) { $form->{"select$form->{vc}"} .= qq|$_->{name}--$_->{id}\n| }
1135
1136        # if the name is not the same
1137        if ($form->{"select$form->{vc}"} !~ /$form->{$form->{vc}}/) {
1138          $form->{$form->{vc}} = $newvc;
1139          &check_openvc;
1140        }
1141
1142        $form->{"select$form->{vc}"} = $form->escape($form->{"select$form->{vc}"},1);
1143      }
1144
1145      foreach $item (qw(business paymentmethod)) {
1146        if (@{ $form->{"all_$item"} }) {
1147          $form->{"select$item"} = "\n";
1148          $form->{$item} = qq|$form->{$item}--$form->{"${item}_id"}| if $form->{$item};
1149
1150          for (@{ $form->{"all_$item"} }) { $form->{"select$item"} .= qq|$_->{description}--$_->{id}\n| }
1151        }
1152      }
1153
1154    }
1155
1156    if (@{ $form->{all_language} }) {
1157      $form->{selectlanguage} = "\n";
1158      for (@{ $form->{all_language} }) { $form->{selectlanguage} .= qq|$_->{code}--$_->{description}\n| }
1159      $form->{selectlanguage} = $form->escape($form->{selectlanguage},1);
1160    }
1161
1162  }
1163
1164  if ($new_name_selected || $form->{redo}) {
1165    CP->get_openinvoices(\%myconfig, \%$form);
1166
1167    ($newvc) = split /--/, $form->{$form->{vc}};
1168    $form->{"old$form->{vc}"} = qq|$newvc--$form->{"$form->{vc}_id"}|;
1169    $form->{redo} = 1;
1170  }
1171
1172  $form->{exchangerate} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{datepaid});
1173
1174  if ($form->{redo}) {
1175    $form->{rowcount} = 0;
1176    $form->{allbox} = "" if $new_name_selected;
1177    $form->{amount} = 0;
1178    $form->{oldamount} = 0;
1179
1180    $i = 0;
1181    foreach $ref (@{ $form->{PR} }) {
1182      $i++;
1183
1184      for (qw(id invnumber invdescription transdate duedate calcdiscount discountterms cashdiscount netamount)) { $form->{"${_}_$i"} = $ref->{$_} }
1185      $ref->{exchangerate} ||= 1;
1186      $due = ($form->{edit}) ? $ref->{amount} : $ref->{amount} - $ref->{paid};
1187
1188      $form->{"due_$i"} = $form->format_amount(\%myconfig, $due / $ref->{exchangerate}, $form->{precision});
1189      $form->{"amount_$i"} = $form->format_amount(\%myconfig, $ref->{amount} / $ref->{exchangerate}, $form->{precision});
1190      $form->{"netamount_$i"} = $form->format_amount(\%myconfig, $ref->{netamount} / $ref->{exchangerate}, $form->{precision});
1191      if ($new_name_selected) {
1192        for (qw(checked paid discount total)) { $form->{"${_}_$i"} = "" }
1193      }
1194    }
1195    $form->{rowcount} = $i;
1196    $form->{allbox_select} = "" if $i == 0;
1197  }
1198
1199  $form->{amount} = $form->parse_amount(\%myconfig, $form->{amount});
1200
1201  # recalculate
1202  $amount = 0;
1203  for $i (1 .. $form->{rowcount}) {
1204
1205    for (qw(amount due paid discount)) { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) }
1206
1207    if ($form->{"checked_$i"}) {
1208      $ok = 1;
1209      # calculate discount
1210      if ($form->{"calcdiscount_$i"}) {
1211        if (! $form->{"olddiscount_$i"}) {
1212          $form->{"discount_$i"} = $form->parse_amount(\%myconfig, $form->{"netamount_$i"}) * $form->{"cashdiscount_$i"};
1213          $form->{"olddiscount_$i"} = $form->{"discount_$i"};
1214        }
1215      }
1216
1217      # calculate paid_$i
1218      if (!$form->{"paid_$i"}) {
1219        $form->{"paid_$i"} = $form->{"due_$i"} - $form->{"discount_$i"};
1220      }
1221
1222      $amount += $form->{"paid_$i"};
1223      $form->{redo} = 1;
1224    } else {
1225      for (qw(paid discount)) { $form->{"${_}_$i"} = "" }
1226    }
1227
1228    for (qw(amount due paid discount)) { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $form->{precision}) }
1229  }
1230
1231  $form->{amount} += ($amount - $form->{oldamount}) if $form->{redo};
1232
1233  if (! $ok) {
1234    $form->{amount} = 0;
1235    $form->{source} = "";
1236  }
1237
1238  $form->{"old$form->{ARAP}_paid"} = $form->{"$form->{ARAP}_paid"};
1239
1240  &payment_header;
1241  &list_invoices;
1242  &payment_footer;
1243
1244}
1245
1246
1247
1248
1249sub payment_header {
1250
1251  if ($form->{type} eq 'receipt') {
1252    $form->{title} = $locale->text('Receipt');
1253  }
1254  if ($form->{type} eq 'check') {
1255    $form->{title} = $locale->text('Payment');
1256  }
1257
1258  if ($form->{batch}) {
1259    $form->{title} .= " ".$locale->text('Voucher');
1260    if ($form->{batchdescription}) {
1261      $form->{title} .= " / $form->{batchdescription}";
1262    }
1263  }
1264
1265# $locale->text('Customer')
1266# $locale->text('Customer Number')
1267# $locale->text('Vendor')
1268# $locale->text('Vendor Number')
1269
1270  if ($form->{$form->{vc}} eq "") {
1271    for (qw(address1 address2 city zipcode state country)) { $form->{$_} = "" }
1272  }
1273
1274  if ($form->{defaultcurrency}) {
1275    $exchangerate = qq|
1276	      <tr>
1277		<th align=right nowrap>|.$locale->text('Currency').qq|</th>
1278		<td><select name=currency onChange="javascript:document.main.submit()">|
1279		.$form->select_option($form->{selectcurrency}, $form->{currency})
1280		.qq|</select></td>
1281	      </tr>
1282|;
1283  }
1284
1285  if ($form->{currency} ne $form->{defaultcurrency}) {
1286    $form->{exchangerate} = $form->format_amount(\%myconfig, $form->{exchangerate});
1287
1288    $exchangerate .= qq|
1289 	      <tr>
1290		<th align=right nowrap>|.$locale->text('Exchange Rate').qq|</th>
1291		<td colspan=3><input name=exchangerate class="inputright" size=11 value=$form->{exchangerate}></td>
1292	      </tr>
1293|;
1294  }
1295
1296  $allvc = ($form->{all_vc}) ? "checked" : "";
1297  $allvc = qq|
1298  	      <tr>
1299	        <td align=right>
1300		<input name=all_vc type=checkbox class=checkbox value=Y $allvc onChange="javascript:main.submit()">
1301		<input type=hidden name="oldall_vc" value="$form->{all_vc}"></td>
1302		<input type=hidden name="action" value="update">
1303		<th align=left>|.$locale->text('All').qq|</th>
1304	      </tr>
1305|;
1306
1307
1308  %vc = ( customer => { name => 'Customer', number => 'Customer Number' },
1309          vendor => { name => 'Vendor', number => 'Vendor Number' }
1310	);
1311
1312  $vc = qq|
1313	      <tr>
1314		<th align=right>|.$locale->text($vc{$form->{vc}}{name}).qq|</th>
1315|;
1316
1317  $duedate = qq|
1318	      <tr>
1319		<th align=right>|.$locale->text('Due Date').qq|</th>
1320		<td>
1321		  <table>
1322		    <tr>
1323		      <th align=right>|.$locale->text('From').qq|</th>
1324		      <td><input name=duedatefrom value="$form->{duedatefrom}" title="$myconfig{dateformat}" size=11 class=date>|.&js_calendar("main", "duedatefrom").qq|</td>
1325		      <th align=right>|.$locale->text('To').qq|</th>
1326		      <td><input name=duedateto value="$form->{duedateto}" title="$myconfig{dateformat}" size=11 class=date>|.&js_calendar("main", "duedateto").qq|</td>
1327		    </tr>
1328		  </table>
1329		</td>
1330	      </tr>
1331|;
1332
1333  $vcref = qq|<a href=ct.pl?action=edit&db=$form->{vc}&id=$form->{"$form->{vc}_id"}&login=$form->{login}&path=$form->{path} target=_blank>?</a>|;
1334
1335  if ($form->{payments_detail}) {
1336    $allvc = "";
1337
1338    $name = $form->{"$form->{vc}"};
1339    $name =~ s/--.*//;
1340    $vc .= qq|<td>|.$form->quote($name).qq|</td>
1341              </tr>
1342	      <tr>
1343	      <th align=right>|.$locale->text($vc{$form->{vc}}{number}).qq|</th>
1344	      <td>|.$form->quote($form->{"$form->{vc}number"}).qq|</td>
1345	      </tr>
1346|.$form->hide_form("payments_detail","$form->{vc}","$form->{vc}number");
1347
1348    if ($form->{duedatefrom} || $form->{duedateto}) {
1349      $duedate = qq|
1350	      <tr>
1351		<th align=right>|.$locale->text('Due Date').qq|</th>
1352		<td>
1353		  <table>
1354		    <tr>
1355		      <th align=right>|.$locale->text('From').qq|</th>
1356		      <td>$form->{duedatefrom}|.&js_calendar("main", "duedatefrom").qq|</td>
1357		      <th align=right>|.$locale->text('To').qq|</th>
1358		      <td>$form->{duedateto}|.&js_calendar("main", "duedateto").qq|</td>
1359		    </tr>
1360		  </table>
1361		</td>
1362	      </tr>
1363|;
1364    } else {
1365      $duedate = "";
1366    }
1367
1368    $duedate .= $form->hide_form(qw(duedatefrom duedateto));
1369
1370  } else {
1371    if ($form->{"select$form->{vc}"}) {
1372      $vc .= qq|<td><select name="$form->{vc}" onChange="javascript:document.main.submit()">|.$form->select_option($form->{"select$form->{vc}"}, $form->{$form->{vc}}, 1).qq|</select> $vcref</td>
1373      <input name=action type=hidden value=update>
1374		</tr>
1375  |;
1376    } else {
1377      $vc .= qq|<td><input name="$form->{vc}" size=35 value="|.$form->quote($form->{$form->{vc}}).qq|"> $vcref</td>
1378		</tr>
1379		<tr>
1380		<th align=right>|.$locale->text($vc{$form->{vc}}{number}).qq|</th>
1381		<td><input name="$form->{vc}number" size=35 value="|.$form->quote($form->{"$form->{vc}number"}).qq|"></td>
1382		</tr>
1383  |;
1384    }
1385  }
1386
1387
1388# $locale->text('AR')
1389# $locale->text('AP')
1390
1391  $department = qq|
1392              <tr>
1393	        <th align="right" nowrap>|.$locale->text('Department').qq|</th>
1394		<td><select name=department onChange="javascript:document.main.submit()">|
1395		.$form->select_option($form->{selectdepartment}, $form->{department}, 1).qq|
1396		</select>
1397	      </td>
1398	    </tr>
1399| if $form->{selectdepartment};
1400
1401  $business = qq|
1402              <tr>
1403	        <th align="right" nowrap>|.$locale->text('Type of Business').qq|</th>
1404		<td><select name=business onChange="javascript:document.main.submit()">|
1405		.$form->select_option($form->{selectbusiness}, $form->{business}, 1).qq|
1406		</select>
1407	      </td>
1408	    </tr>
1409| if $form->{selectbusiness};
1410
1411  $paymentmethod = qq|
1412              <tr>
1413	        <th align="right" nowrap>|.$locale->text('Payment Method').qq|</th>
1414		<td><select name=paymentmethod onChange="javascript:document.main.submit()">|
1415		.$form->select_option($form->{selectpaymentmethod}, $form->{paymentmethod}, 1).qq|
1416		</select>
1417	      </td>
1418	    </tr>
1419| if $form->{selectpaymentmethod};
1420
1421  $cashdiscount = qq|
1422 	      <tr>
1423		<th align=right nowrap>|.$locale->text('Cash Discount').qq|</th>
1424		<td colspan=3><select name="$form->{ARAP}_discount">|
1425		.$form->select_option($form->{"select$form->{ARAP}_discount"}, $form->{"$form->{ARAP}_discount"}).qq|</select>
1426		</td>
1427	      </tr>
1428| if $form->{"select$form->{ARAP}_discount"};
1429
1430
1431  if ($form->{batch}) {
1432    $datepaid = qq|
1433		<td>$form->{datepaid}</td>
1434		<input type=hidden name=datepaid value="$form->{datepaid}"></td>
1435|;
1436  } else {
1437    $datepaid = qq|
1438		<td><input name=datepaid value="$form->{datepaid}" title="$myconfig{dateformat}" size=11 class=date>|.&js_calendar("main", "datepaid").qq|</td>
1439|;
1440  }
1441
1442
1443  $form->header;
1444
1445  &calendar;
1446
1447  &check_all(qw(allbox checked_));
1448
1449  print qq|
1450<script language="javascript">
1451<!--
1452javascript:window.history.forward(1);
1453// -->
1454</script>
1455
1456<body>
1457
1458<form method="post" name="main" action="$form->{script}">
1459|;
1460
1461  for (split /%0a/, $form->{"select$form->{ARAP}_paid"}) {
1462    ($accno) = split /--/, $_;
1463    $form->hide_form("$form->{type}_$accno");
1464  }
1465
1466  $form->hide_form(qw(defaultcurrency closedto vc type ARAP arap title formname payment batch batchid batchnumber batchdescription transdate edit voucherid vouchernumber employee precision));
1467  $form->hide_form("$form->{vc}_id");
1468  $form->hide_form(map { "old$_" } qw(currency datepaid duedatefrom duedateto department business paymentmethod));
1469  $form->hide_form(map { "old$_" } ("$form->{ARAP}", "$form->{ARAP}_paid", "$form->{vc}", "$form->{vc}number"));
1470  $form->hide_form(map { "select$_" } qw(currency department business paymentmethod printer));
1471  $form->hide_form(map { "select$_" } ("$form->{ARAP}", "$form->{ARAP}_paid", "$form->{ARAP}_discount", "$form->{vc}"));
1472
1473  print qq|
1474
1475<table width=100%>
1476  <tr>
1477    <th class=listtop>$form->{helpref}$form->{title}</a></th>
1478  </tr>
1479  <tr height="5"></tr>
1480  <tr>
1481    <td>
1482      <table width=100%>
1483        <tr valign=top>
1484	  <td>
1485	    <table>
1486	      $allvc
1487
1488	      $duedate
1489
1490              $vc
1491
1492	      <tr valign=top>
1493          <th align=right nowrap>|.$locale->text('Address').qq|</th>
1494          <td colspan=2>
1495		  <table>
1496		    <tr>
1497		      <td>$form->{address1}</td>
1498		    </tr>
1499		    <tr>
1500		      <td>$form->{address2}</td>
1501		    </tr>
1502		      <td>$form->{city}</td>
1503		    </tr>
1504		    </tr>
1505		      <td>$form->{state}</td>
1506		    </tr>
1507		    </tr>
1508		      <td>$form->{zipcode}</td>
1509		    </tr>
1510		    <tr>
1511		      <td>$form->{country}</td>
1512		    </tr>
1513		  </table>
1514		</td>
1515	      </tr>
1516	      <tr>
1517          <th align=right>|.$locale->text('Memo').qq|</th>
1518          <td colspan=2><input name="memo" size=30 value="|.$form->quote($form->{memo}).qq|"></td>
1519              </tr>
1520            </table>
1521          </td>
1522          <td align=right>
1523            <table>
1524              $department
1525              $business
1526              <tr>
1527                <th align=right nowrap>|.$locale->text($form->{ARAP}).qq|</th>
1528                <td colspan=3><select name=$form->{ARAP} onChange="javascript:document.main.submit()">|
1529                .$form->select_option($form->{"select$form->{ARAP}"}, $form->{"$form->{ARAP}"}).qq|</select>
1530                </td>
1531              </tr>
1532              <tr>
1533                <th align=right nowrap>|.$locale->text('Payment').qq|</th>
1534                <td colspan=3><select name="$form->{ARAP}_paid" onChange="javascript:document.main.submit()">|
1535                .$form->select_option($form->{"select$form->{ARAP}_paid"}, $form->{"$form->{ARAP}_paid"}).qq|</select>
1536                </td>
1537                $paymentmethod
1538              </tr>
1539              $cashdiscount
1540              <tr>
1541                <th align=right nowrap>|.$locale->text('Date').qq|</th>
1542                $datepaid
1543              </tr>
1544              $exchangerate
1545              <tr>
1546                <th align=right nowrap>|.$locale->text('Source').qq|</th>
1547                <td colspan=3><input name=source value="|.$form->quote($form->{source}).qq|" size=11></td>
1548              </tr>
1549              <tr>
1550                <th align=right nowrap>|.$locale->text('Amount').qq|</th>
1551                <td colspan=3><input name=amount class="inputright" size=11 value=|.$form->format_amount(\%myconfig, $form->{amount}, $form->{precision}).qq|></td>
1552                <input type=hidden name=oldamount value=|.$form->round_amount($form->{amount}, $form->{precision}).qq|>
1553              </tr>
1554            </table>
1555          </td>
1556        </tr>
1557      </table>
1558    </td>
1559  </tr>
1560|;
1561
1562  $form->hide_form(qw(address1 address2 city state zipcode country));
1563
1564}
1565
1566
1567sub list_invoices {
1568
1569  @column_index = qw(invnumber transdate duedate amount due checked paid discount total);
1570
1571  $colspan = $#column_index + 1;
1572
1573  $invoice = $locale->text('Invoices');
1574
1575  print qq|
1576  <input type=hidden name=column_index value="id @column_index">
1577  <tr>
1578    <td>
1579      <table width=100%>
1580	<tr>
1581	  <th class=listheading colspan=$colspan>$invoice</th>
1582	</tr>
1583|;
1584
1585  $column_data{invnumber} = qq|<th>|.$locale->text('Invoice')."</th>";
1586  $column_data{transdate} = qq|<th>|.$locale->text('Invoice Date')."</th>";
1587  $column_data{duedate} = qq|<th>|.$locale->text('Due Date')."</th>";
1588  $column_data{amount} = qq|<th>|.$locale->text('Amount')."</th>";
1589  $column_data{due} = qq|<th>|.$locale->text('Due')."</th>";
1590  $column_data{paid} = qq|<th>|.$locale->text('Paid')."</th>";
1591  $column_data{discount} = qq|<th>|.$locale->text('Discount')."</th>";
1592  $column_data{total} = qq|<th>|.$locale->text('Total')."</th>";
1593
1594  $form->{allbox} = ($form->{allbox}) ? "checked" : "";
1595
1596  $column_data{checked} = qq|<th><input name="allbox" type=checkbox class=checkbox value="1" $form->{allbox} onChange="CheckAll(); javascript:document.main.submit()" ><input type=hidden name=action value="update"></th>|;
1597
1598  print qq|
1599        <tr>
1600|;
1601  for (@column_index) { print "$column_data{$_}\n" }
1602  print qq|
1603        </tr>
1604|;
1605
1606  for $i (1 .. $form->{rowcount}) {
1607
1608    for (qw(amount due paid discount)) { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) }
1609
1610    $form->{"olddiscount_$i"} = $form->{"discount_$i"};
1611
1612    $totalamount += $form->{"amount_$i"};
1613    $totaldue += $form->{"due_$i"};
1614    $totalpaid += $form->{"paid_$i"};
1615    $totaldiscount += $form->{"discount_$i"};
1616    $form->{"total_$i"} = $form->{"paid_$i"} + $form->{"discount_$i"};
1617    $totaltotal += $form->{"total_$i"};
1618
1619    for (qw(amount due paid discount total)) { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $form->{precision}) }
1620
1621    $column_data{invnumber} = qq|<td width=30%>$form->{"invnumber_$i"}</td>|;
1622    $column_data{transdate} = qq|<td width=30% nowrap>$form->{"transdate_$i"}</td>|;
1623    $column_data{duedate} = qq|<td width=30% nowrap>$form->{"duedate_$i"}</td>|;
1624    $column_data{amount} = qq|<td align=right>$form->{"amount_$i"}</td>|;
1625    $column_data{due} = qq|<td align=right>$form->{"due_$i"}</td>|;
1626    $column_data{total} = qq|<td align=right>$form->{"total_$i"}</td>|;
1627
1628    $form->hide_form(map { "${_}_$i" } qw(id invnumber invdescription transdate duedate due calcdiscount discountterms cashdiscount amount netamount olddiscount));
1629
1630    $column_data{paid} = qq|<td align=center><input name="paid_$i" class="inputright" size=11 value=$form->{"paid_$i"}></td>|;
1631
1632    if ($form->{"calcdiscount_$i"}) {
1633      $column_data{discount} = qq|<td align=center><input name="discount_$i" class="inputright" size=11 value=$form->{"discount_$i"}></td>|;
1634    } else {
1635      $column_data{discount} = qq|<td></td>|;
1636    }
1637
1638    $form->{"checked_$i"} = ($form->{"checked_$i"}) ? "checked" : "";
1639    $column_data{checked} = qq|<td align=center><input name="checked_$i" type=checkbox class=checkbox $form->{"checked_$i"} onChange="javascript:document.main.submit()"></td>|;
1640
1641    $j++; $j %= 2;
1642    print qq|
1643	<tr class=listrow$j>
1644|;
1645    for (@column_index) { print "$column_data{$_}\n" }
1646    print qq|
1647        </tr>
1648|;
1649  }
1650
1651  for (@column_index) { $column_data{$_} = "<td>&nbsp;</td>" }
1652
1653  $column_data{due} = qq|<th class=listtotal align=right>|.$form->format_amount(\%myconfig, $totaldue, $form->{precision}, "&nbsp;").qq|</th>|;
1654  $column_data{paid} = qq|<th class=listtotal align=right>|.$form->format_amount(\%myconfig, $totalpaid, $form->{precision}, "&nbsp;").qq|</th>|;
1655  $column_data{discount} = qq|<th class=listtotal align=right>|.$form->format_amount(\%myconfig, $totaldiscount, $form->{precision}, "&nbsp;").qq|</th>|;
1656  $column_data{amount} = qq|<th class=listtotal align=right>|.$form->format_amount(\%myconfig, $totalamount, $form->{precision}, "&nbsp;").qq|</th>|;
1657  $column_data{total} = qq|<th class=listtotal align=right>|.$form->format_amount(\%myconfig, $totaltotal, $form->{precision}, "&nbsp;").qq|</th>|;
1658
1659  print qq|
1660        <tr class=listtotal>
1661|;
1662  for (@column_index) { print "$column_data{$_}\n" }
1663  print qq|
1664        </tr>
1665      </table>
1666    </td>
1667  </tr>
1668|;
1669
1670}
1671
1672
1673sub payment_footer {
1674
1675  $form->{DF}{$form->{format}} = "selected";
1676
1677  $transdate = $form->datetonum(\%myconfig, $form->{datepaid});
1678
1679  if (!$form->{readonly}) {
1680
1681    $media = qq|<select name=media>
1682	  <option value=screen>|.$locale->text('Screen');
1683
1684    if ($form->{selectprinter}) {
1685      for (split /\n/, $form->unescape($form->{selectprinter})) { $media .= qq|
1686	    <option value="$_">$_| }
1687    }
1688    $media .= qq|</select>|;
1689
1690    $format = qq|<select name=format>|;
1691
1692#    <option value="html" $form->{DF}{html}>|.$locale->text('html').qq|
1693#    <option value="xml" $form->{DF}{xml}>|.$locale->text('XML').qq|
1694#    <option value="txt" $form->{DF}{txt}>|.$locale->text('Text');
1695
1696    if ($latex) {
1697      if ($form->{selectlanguage}) {
1698        $lang = qq|<select name=language_code>|.$form->select_option($form->{"selectlanguage"}, $form->{language_code}, undef, 1).qq|</select>|;
1699        $form->hide_form(qw(selectlanguage));
1700      }
1701
1702      $format .= qq|
1703	      <option value=ps $form->{DF}{ps}>|.$locale->text('Postscript').qq|
1704	      <option value=pdf $form->{DF}{pdf}>|.$locale->text('PDF');
1705    }
1706    $format .= qq|</select>|;
1707
1708    if (! $latex) {
1709      $format = "";
1710      $media = "";
1711    }
1712
1713    print qq|
1714  <tr>
1715    <td><hr size=3 noshade></td>
1716  </tr>
1717|;
1718
1719    $media =~ s/(<option value="\Q$form->{media}\E")/$1 selected/;
1720    if ($transdate <= $form->{closedto}) {
1721      $media = $format = "";
1722    }
1723
1724    print qq|
1725  <tr>
1726    <td>
1727    $lang
1728    $format
1729    $media
1730    </td>
1731  </tr>
1732</table>
1733<p>
1734|;
1735
1736    %button = ('Update' => { ndx => 1, key => 'U', value => $locale->text('Update') },
1737	       'Select all' => { ndx => 2, key => 'A', value => $locale->text('Select all') },
1738	       'Deselect all' => { ndx => 3, key => 'A', value => $locale->text('Deselect all') },
1739               'Preview' => { ndx => 4, key => 'V', value => $locale->text('Preview') },
1740	       'Print' => { ndx => 5, key => 'P', value => $locale->text('Print') },
1741	       'Post' => { ndx => 6, key => 'O', value => $locale->text('Post') },
1742	       'Back' => { ndx => 7, key => 'B', value => $locale->text('Back') }
1743	      );
1744
1745    if ($form->{allbox}) {
1746      delete $button{'Select all'};
1747    } else {
1748      delete $button{'Deselect all'};
1749    }
1750
1751    if (! $latex) {
1752      for ('Print', 'Preview') { delete $button{$_} }
1753    }
1754
1755    if ($transdate <= $form->{closedto}) {
1756      for ('Post', 'Print', 'Preview') { delete $button{$_} }
1757    }
1758
1759    if (! $form->{payments_detail}) {
1760      delete $button{'Back'};
1761    }
1762
1763    $form->print_button(\%button);
1764  }
1765
1766  $form->hide_form(qw(helpref callback rowcount path login));
1767
1768  if ($form->{menubar}) {
1769    require "$form->{path}/menu.pl";
1770    &menubar;
1771  }
1772
1773  print qq|
1774  </form>
1775
1776</body>
1777</html>
1778|;
1779
1780}
1781
1782
1783
1784sub back {
1785
1786  $form->{olddatepaid} = "";
1787  $form->{redo} = "";
1788  for $i (1 .. $form->{rowcount}) {
1789    for (qw(checked amount due paid totaldue id)) { $form->{"${_}_$i"} = "" }
1790  }
1791  $form->{payment} = "payments";
1792  $form->{rowcount} = 0;
1793  $form->{allbox} = 0;
1794
1795  &update_payments;
1796
1797}
1798
1799
1800sub post { &{"post_$form->{payment}"} }
1801
1802
1803
1804sub post_payments {
1805
1806  $msg = $locale->text('Posting Payment');
1807
1808  %oldform = ();
1809  for (keys %$form) { $oldform{$_} = $form->{$_} };
1810
1811  CP->invoice_ids(\%myconfig, \%$form);
1812
1813  $i = 0;
1814  $j = 0;
1815  foreach $ref (@{ $form->{PR} }) {
1816    $i++;
1817
1818    if ($ref->{"$form->{vc}_id"} ne $sameid) {
1819      $j++;
1820      $sameid = $ref->{"$form->{vc}_id"};
1821      $paid = $form->parse_amount(\%myconfig, $oldform{"paid_$j"});
1822    }
1823
1824    for (qw(checked source memo)) { $form->{"${_}_$i"} = $oldform{"${_}_$j"} }
1825    for (qw(id invnumber transdate duedate)) { $form->{"${_}_$i"} = $ref->{$_} }
1826
1827    $form->{"$form->{vc}_id_$i"} = $ref->{"$form->{vc}_id"};
1828    $form->{"$form->{vc}number_$i"} = $ref->{"$form->{vc}number"};
1829    $form->{"name_$i"} = $ref->{name};
1830    $ref->{exchangerate} ||= 1;
1831
1832
1833    # check if we can apply a discount
1834    if (!$ref->{discount}) {
1835      if ($ref->{calcdiscount}) {
1836	$netamount = $form->round_amount($ref->{netamount} / $ref->{exchangerate}, $form->{precision});
1837	$form->{"discount_$i"} = $form->round_amount($netamount * $ref->{cashdiscount}, $form->{precision});
1838      }
1839    }
1840
1841    $due = $form->round_amount(($ref->{amount} - $ref->{paid}) / $ref->{exchangerate}, $form->{precision});
1842
1843    $form->{"paid_$i"} = ($paid > $due) ? $due : $paid;
1844
1845    $paid = $form->round_amount($paid - $form->{"paid_$i"}, $form->{precision});
1846
1847    $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"} - $form->{"discount_$i"}, $form->{precision});
1848    $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"}, $form->{precision});
1849
1850  }
1851
1852  $rowcount = $i;
1853
1854  delete $form->{PR};
1855
1856  $ok = 0;
1857  $j = 0;
1858  $k = 1;
1859
1860  $form->{"$form->{vc}_id"} = $form->{"$form->{vc}_id_1"};
1861
1862  for $i (1 .. $rowcount) {
1863
1864    $j++;
1865
1866    $form->{amount} = $oldform{"paid_$k"};
1867    $form->{rowcount} = $j;
1868
1869    for (qw(source memo)) { $form->{$_} = $form->{"${_}_$i"} }
1870
1871    for (qw(id invnumber checked paid)) { $form->{"${_}_$j"} = $form->{"${_}_$i"} }
1872
1873    $form->{"$form->{vc}number"} = $form->{"$form->{vc}number_$i"};
1874    $form->{name} = $form->{"name_$i"};
1875
1876    $n = $i + 1;
1877    next if $form->{"$form->{vc}_id_$n"} eq $form->{"$form->{vc}_id_$i"};
1878
1879    if ($form->{"checked_$i"}) {
1880      if ($form->{batch}) {
1881        $batchid = $form->{batchid};
1882        VR->post_transaction(\%myconfig, \%$form);
1883      } else {
1884        CP->post_payment(\%myconfig, \%$form);
1885      }
1886      $oldform{header} = 1;
1887
1888      $form->info(qq|$msg $form->{amount}, $form->{name} $form->{"$form->{vc}number"}\n|);
1889    }
1890
1891    $k++;
1892    $j = 0;
1893
1894  }
1895
1896
1897  for (keys %$form) { $form->{$_} = "" }
1898  for (keys %oldform) { $form->{$_} = $oldform{$_} }
1899
1900  $form->{callback} .= "&header=$oldform{header}" if $form->{callback};
1901
1902  $form->remove_locks(\%myconfig, undef, $form->{arap});
1903
1904  $form->redirect;
1905
1906}
1907
1908
1909
1910sub post_payment {
1911
1912  &check_form;
1913
1914  if ($form->{currency} ne $form->{defaultcurrency}) {
1915    $form->error($locale->text('Exchange rate missing!')) unless $form->{exchangerate};
1916  }
1917
1918  $msg1 = $locale->text('Payment posted!');
1919  $msg2 = $locale->text('Posting failed!');
1920
1921  $form->{amount} = $form->format_amount(\%myconfig, $form->{amount}, $form->{precision});
1922
1923  $source = $form->{source};
1924  $source =~ s/(\d+)/$1 + 1/e;
1925
1926  if ($form->{callback}) {
1927    $form->{callback} .= "&source=$source";
1928  }
1929
1930  if ($form->{batch}) {
1931    $batchid = $form->{batchid};
1932    if ($rc = VR->post_transaction(\%myconfig, \%$form)) {
1933      if ($form->{callback}) {
1934	$form->{callback} .= "&batch=$form->{batch}&batchdescription=".$form->escape($form->{batchdescription},1);
1935	if (!$batchid) {
1936	  $form->{callback} .= "&batchid=$form->{batchid}&type=$form->{type}";
1937	}
1938      }
1939      $form->redirect($locale->text($msg1));
1940    }
1941  } else {
1942    if ($rc = CP->post_payment(\%myconfig, \%$form)) {
1943      $form->redirect($locale->text($msg1));
1944    }
1945  }
1946
1947  $form->error($locale->text($msg2)) if ! $rc;
1948
1949}
1950
1951
1952sub print {
1953
1954  &{ "print_$form->{payment}" };
1955  &update if $form->{media} ne 'screen';
1956
1957}
1958
1959
1960
1961sub print_payments {
1962
1963  $form->error($locale->text('Select postscript or PDF!')) if ($form->{format} !~ /(ps|pdf)/);
1964
1965  %oldform = ();
1966  for (keys %$form) { $oldform{$_} = $form->{$_} };
1967
1968  CP->invoice_ids(\%myconfig, \%$form);
1969
1970  $i = 0;
1971  $j = 0;
1972  foreach $ref (@{ $form->{PR} }) {
1973    $i++;
1974
1975    if ($ref->{"$form->{vc}_id"} ne $sameid) {
1976      $j++;
1977      $sameid = $ref->{"$form->{vc}_id"};
1978      $paid = $form->parse_amount(\%myconfig, $oldform{"paid_$j"});
1979    }
1980
1981    for (qw(checked source memo language_code)) { $form->{"${_}_$i"} = $oldform{"${_}_$j"} }
1982
1983    for (qw(id invnumber invdescription transdate duedate)) { $form->{"${_}_$i"} = $ref->{$_} }
1984    $form->{"$form->{vc}_id_$i"} = $ref->{"$form->{vc}_id"};
1985
1986    $ref->{exchangerate} ||= 1;
1987    $due = $form->round_amount(($ref->{amount} - $ref->{paid}) / $ref->{exchangerate}, $form->{precision});
1988
1989    $form->{"due_$i"} = $due;
1990    $form->{"amount_$i"} = $form->round_amount($ref->{amount} / $ref->{exchangerate}, $form->{precision});
1991
1992    $form->{"paid_$i"} = ($paid > $due) ? $due : $paid;
1993
1994    $paid -= $due;
1995    $paid = 0 if $paid < 0;
1996
1997  }
1998
1999  $temp{rowcount} = $i;
2000
2001  delete $form->{PR};
2002
2003  for $i (1 .. $temp{rowcount}) {
2004    for (qw(due amount paid)) { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $form->{precision}) }
2005  }
2006
2007  $ok = 0;
2008  $j = 0;
2009  $k = 0;
2010
2011  $SIG{INT} = 'IGNORE';
2012
2013  $msg = ($form->{vc} eq 'vendor') ? $locale->text('Printing check for') : $locale->text('Printing receipt for');
2014
2015  $form->{"$form->{vc}_id"} = "";
2016
2017  for $i (1 .. $temp{rowcount}) {
2018
2019    if ($form->{"$form->{vc}_id_$i"} ne $form->{"$form->{vc}_id"}) {
2020      $k++;
2021
2022      $form->{rowcount} = $j;
2023      for (1 .. $j) { $form->{"id_$_"} = $temp{"id_$_"} }
2024
2025      if ($ok) {
2026        &print_form;
2027        $oldform{header} = 1;
2028        $form->info(qq|$msg $form->{name} $form->{"$form->{vc}number"}\n|);
2029      }
2030
2031      $ok = 0;
2032      $j = 0;
2033      $form->{amount} = 0;
2034      for (qw(invnumber invdescription invdate due paid)) { @{ $form->{$_} } = () }
2035      for (qw(language_code source memo)) { $form->{$_} = $form->{"${_}_$i"} }
2036
2037    }
2038
2039    if ($form->{"checked_$i"}) {
2040      $j++;
2041      $ok = 1;
2042      $temp{"id_$j"} = $form->{"id_$i"};
2043      $form->{"invdate_$i"} = $form->{"transdate_$i"};
2044      for (qw(invnumber invdescription invdate due paid)) { push @{ $form->{$_} }, $form->{"${_}_$i"} }
2045      $form->{amount} = $form->parse_amount(\%myconfig, $oldform{"paid_$k"});
2046    }
2047
2048    $form->{"$form->{vc}_id"} = $form->{"$form->{vc}_id_$i"};
2049
2050  }
2051
2052  $form->{rowcount} = $j;
2053  for (1 .. $j) { $form->{"id_$_"} = $temp{"id_$_"} }
2054
2055  if ($ok) {
2056    &print_form;
2057    $oldform{header} = 1;
2058    $form->info(qq|$msg $form->{name} $form->{"$form->{vc}number"}\n|);
2059  }
2060
2061  for (keys %$form) { $form->{$_} = "" }
2062  for (keys %oldform) { $form->{$_} = $oldform{$_} }
2063
2064}
2065
2066
2067sub print_form {
2068
2069  $c = CP->new(($form->{language_code}) ? $form->{language_code} : $myconfig{countrycode});
2070  $c->init;
2071
2072  ($whole, $form->{decimal}) = split /\./, $form->{amount};
2073  $form->{amount} = $form->format_amount(\%myconfig, $form->{amount}, $form->{precision});
2074  $form->{decimal} .= "00";
2075  $form->{decimal} = substr($form->{decimal}, 0, 2);
2076  $form->{text_decimal} = $c->num2text($form->{decimal} * 1);
2077  $form->{text_amount} = $c->num2text($whole);
2078  $form->{integer_amount} = $whole;
2079
2080  $datepaid = $form->datetonum(\%myconfig, $form->{datepaid});
2081  ($form->{yyyy}, $form->{mm}, $form->{dd}) = $datepaid =~ /(....)(..)(..)/;
2082
2083  AA->company_details(\%myconfig, \%$form);
2084
2085  $form->format_string(qw(company address companyemail companywebsite));
2086
2087  $form->{templates} = "$templates/$myconfig{templates}";
2088  $form->{IN} = "$form->{formname}.tex";
2089
2090  if ($form->{media} ne 'screen') {
2091    $form->{OUT} = qq~| $form->{"$form->{media}_printer"}~;
2092  }
2093
2094  $form->parse_template(\%myconfig, $userspath, $dvipdf, $xelatex);
2095
2096}
2097
2098
2099sub print_payment {
2100
2101  $form->error($locale->text('Select postscript or PDF!')) if ($form->{format} !~ /(ps|pdf)/);
2102
2103  &check_form;
2104
2105  @a = qw(name text_amount text_decimal address1 address2 city state zipcode country memo);
2106
2107  %temp = ();
2108  for (@a) { $temp{$_} = $form->{$_} }
2109
2110  $form->format_string(@a);
2111
2112  &print_form;
2113
2114  for (keys %temp) { $form->{$_} = $temp{$_} }
2115
2116}
2117
2118
2119
2120sub check_form {
2121
2122  &check_openvc;
2123
2124  if ($form->{currency} ne $form->{oldcurrency}) {
2125    &update;
2126    exit;
2127  }
2128
2129  $form->error($locale->text('Date missing!')) unless $form->{datepaid};
2130
2131  $datepaid = $form->datetonum(\%myconfig, $form->{datepaid});
2132
2133  $form->error($locale->text('Cannot post payment for a closed period!')) if ($datepaid <= $form->{closedto});
2134
2135  # this is just to format the year
2136  $form->{datepaid} = $locale->date(\%myconfig, $form->{datepaid});
2137
2138  $amount = $form->parse_amount(\%myconfig, $form->{amount});
2139  $form->{amount} = $amount;
2140
2141  for $i (1 .. $form->{rowcount}) {
2142    if ($form->{"paid_$i"}) {
2143      $amount -= $form->parse_amount(\%myconfig, $form->{"paid_$i"});
2144
2145      push(@{ $form->{paid} }, $form->{"paid_$i"});
2146      push(@{ $form->{discount} }, $form->{"discount_$i"});
2147      push(@{ $form->{due} }, $form->{"due_$i"});
2148      push(@{ $form->{invnumber} }, $form->{"invnumber_$i"});
2149      push(@{ $form->{invdescription} }, $form->{"invdescription_$i"});
2150      push(@{ $form->{invdate} }, $form->{"transdate_$i"});
2151    }
2152  }
2153
2154  if ($form->round_amount($amount, $form->{precision}) != 0) {
2155    push(@{ $form->{paid} }, $form->format_amount(\%myconfig, $amount, $form->{precision}));
2156    push(@{ $form->{due} }, $form->format_amount(\%myconfig, 0, $form->{precision}));
2157    push(@{ $form->{discount} }, $form->format_amount(\%myconfig, 0, $form->{precision}));
2158    push(@{ $form->{invnumber} }, ($form->{ARAP} eq 'AR') ? $locale->text('Deposit') : $locale->text('Prepayment'));
2159    push(@{ $form->{invdate} }, $form->{datepaid});
2160  }
2161
2162}
2163
2164
2165sub check_openvc {
2166
2167  ($new_name, $new_id) = split /--/, $form->{$form->{vc}};
2168  $new_id ||= $form->{"$form->{vc}_id"};
2169
2170  if ($form->{all_vc}) {
2171    if ($form->{"select$form->{vc}"}) {
2172      $redo = ($form->{"old$form->{vc}"} ne $form->{$form->{vc}});
2173    } else {
2174      $redo = ($form->{"old$form->{vc}"} ne qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|);
2175      $redo = ($form->{"old$form->{vc}number"} ne qq|$form->{"$form->{vc}number"}|) if ! $redo;
2176    }
2177
2178    if ($redo) {
2179      $form->remove_locks(\%myconfig, undef, $form->{arap});
2180      $form->{redo} = 1;
2181      $form->{locks_removed} = 1;
2182
2183      if ($form->{"select$form->{vc}"}) {
2184        $form->{"$form->{vc}_id"} = $new_id;
2185        AA->get_name(\%myconfig, \%$form);
2186        $form->{$form->{vc}} = $form->{"old$form->{vc}"} = "$new_name--$new_id";
2187      } else {
2188        &check_name($form->{vc});
2189      }
2190    }
2191
2192  } else {
2193
2194    # if we use a selection
2195    if ($form->{"select$form->{vc}"}) {
2196
2197      if ($form->{"old$form->{vc}"} ne $form->{$form->{vc}}) {
2198
2199        for (qw(address1 address2 city state zipcode country)) { $form->{$_} = "" }
2200
2201        $form->remove_locks(\%myconfig, undef, $form->{arap});
2202        $form->{locks_removed} = 1;
2203
2204        $form->{"$form->{vc}_id"} = $new_id;
2205        AA->get_name(\%myconfig, \%$form);
2206
2207        if ($form->{"$form->{vc}_id"}) {
2208          $form->{$form->{vc}} = $form->{"old$form->{vc}"} = "$new_name--$new_id";
2209        } else {
2210          $form->{$form->{vc}} = $form->{"old$form->{vc}"} = "";
2211        }
2212
2213        $form->{redo} = 1;
2214      }
2215    } else {
2216
2217      # check name, combine name and id
2218      if ($form->{"old$form->{vc}"} ne qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|) {
2219        if ($form->{"$form->{vc}number"} eq $form->{"old$form->{vc}number"}) {
2220          $form->{"$form->{vc}number"} = $form->{"old$form->{vc}number"} = "";
2221        }
2222        $redo = 1;
2223      }
2224
2225      if ($form->{"old$form->{vc}number"} ne $form->{"$form->{vc}number"}) {
2226        $form->{$form->{vc}} = "";
2227        $redo = 1;
2228      }
2229
2230      if ($redo) {
2231
2232        $form->remove_locks(\%myconfig, undef, $form->{arap});
2233        $form->{locks_removed} = 1;
2234
2235        # return one name or a list of names in $form->{name_list}
2236        if (($rv = CP->get_openvc(\%myconfig, \%$form)) > 1) {
2237          $form->{redo} = 1;
2238          &select_name($form->{vc});
2239          exit;
2240        }
2241
2242        if ($rv == 1) {
2243          # we got one name
2244          $form->{"$form->{vc}_id"} = $form->{name_list}[0]->{id};
2245          $form->{$form->{vc}} = $form->{name_list}[0]->{name};
2246          $form->{"old$form->{vc}"} = qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
2247          $form->{"old$form->{vc}number"} = $form->{name_list}[0]->{"$form->{vc}number"};
2248
2249          AA->get_name(\%myconfig, \%$form);
2250
2251        } else {
2252          # nothing open
2253          $form->{$form->{vc}} = "";
2254          $form->{"$form->{vc}number"} = "";
2255          $form->{"$form->{vc}_id"} = 0;
2256        }
2257
2258        $form->{redo} = 1;
2259      }
2260    }
2261  }
2262
2263  if ($form->{redo}) {
2264    $form->{$form->{ARAP}} = $form->{"old$form->{ARAP}"};
2265    $form->{"$form->{ARAP}_paid"} = $form->{"old$form->{ARAP}_paid"};
2266    $form->{paymentmethod} = $form->{oldpaymentmethod};
2267    $form->{"$form->{ARAP}_discount"} = $form->{"old$form->{ARAP}_discount"};
2268  }
2269
2270}
2271
2272
2273sub payment_register {
2274
2275  $form->{vc} = ($form->{ARAP} eq 'AP') ? 'vendor' : 'customer';
2276
2277  $form->create_links($form->{ARAP}, \%myconfig, $form->{vc});
2278
2279  CP->paymentaccounts(\%myconfig, \%$form);
2280
2281  $form->{"selectaccno"} = "\n" unless $form->{reissue};
2282  for (@{ $form->{PR}{"$form->{ARAP}_paid"} }) { $form->{"selectaccno"} .= "$_->{accno}--$_->{description}\n" }
2283
2284  $vclabel = $locale->text('Customer');
2285  $vcnumber = $locale->text('Customer Number');
2286
2287  if ($form->{vc} eq 'vendor') {
2288    $vclabel = $locale->text('Vendor');
2289    $vcnumber = $locale->text('Vendor Number');
2290  }
2291
2292  if (@{ $form->{"all_$form->{vc}"} }) {
2293    $form->{"select$form->{vc}"} = "\n";
2294    for (@{ $form->{"all_$form->{vc}"} }) { $form->{"select$form->{vc}"} .= qq|$_->{name}--$_->{id}\n| }
2295    $vc = qq|
2296              <tr>
2297	        <th align=right nowrap>$vclabel</th>
2298	        <td colspan=3><select name="$form->{vc}">|
2299		.$form->select_option($form->{"select$form->{vc}"}, undef, 1)
2300		.qq|</select>
2301	        </td>
2302	      </tr>
2303|;
2304  } else {
2305    $vc = qq|
2306              <tr>
2307	        <th align=right nowrap>$vclabel</th>
2308	        <td colspan=3><input name=$form->{vc} size=35>
2309		</td>
2310	      </tr>
2311	      <tr>
2312	        <th align=right nowrap>$vcnumber</th>
2313		<td colspan=3><input name="$form->{vc}number" size=35>
2314		</td>
2315	      </tr>
2316|;
2317  }
2318
2319  if ($form->{ARAP} eq 'AR') {
2320    $rglabel = $locale->text('Void Receipt') if $form->{void};
2321    $rglabel = $locale->text('Reissue Receipt') if $form->{reissue};
2322    $form->{title} = $locale->text('Deposit Register')." / ".$rglabel;
2323    $form->helpref("deposits", $myconfig{countrycode});
2324  } else {
2325    $rglabel = $locale->text('Void Check') if $form->{void};
2326    $rglabel = $locale->text('Reissue Check') if $form->{reissue};
2327    $form->{title} = $locale->text('Payment Register')." / ".$rglabel;
2328    $form->helpref("checks", $myconfig{countrycode});
2329  }
2330
2331  $checknumber = qq|
2332	<tr>
2333	  <th align=right nowrap>|.$locale->text('Source').qq|</th>
2334          <td>
2335            <table>
2336              <tr>
2337                <th>|.$locale->text('From').qq|</th>
2338	        <td><input name=checknumberfrom size=10></td>
2339	        <th>|.$locale->text('To').qq|</th>
2340                <td><input name=checknumberto size=10></td>
2341              </tr>
2342            </table>
2343          </td>
2344	</tr>
2345|;
2346
2347
2348  if (@{ $form->{all_years} }) {
2349    # accounting years
2350    $selectaccountingyear = "\n";
2351    for (@{ $form->{all_years} }) { $selectaccountingyear .= qq|$_\n| }
2352    $selectaccountingmonth = "\n";
2353    for (sort keys %{ $form->{all_month} }) { $selectaccountingmonth .= qq|$_--|.$locale->text($form->{all_month}{$_}).qq|\n| }
2354
2355    $form->{interval} = "1" unless exists $form->{interval};
2356    $checked{$form->{interval}} = "checked";
2357
2358    $selectfrom = qq|
2359      <tr>
2360	<th align=right>|.$locale->text('Period').qq|</th>
2361	<td>
2362	<select name=month>|.$form->select_option($selectaccountingmonth, undef, 1, 1).qq|</select>
2363	<select name=year>|.$form->select_option($selectaccountingyear, undef, 1).qq|</select>
2364	<input name=interval class=radio type=radio value=0 $checked{0}>&nbsp;|.$locale->text('Current').qq|
2365	<input name=interval class=radio type=radio value=1 $checked{1}>&nbsp;|.$locale->text('Month').qq|
2366	<input name=interval class=radio type=radio value=3 $checked{3}>&nbsp;|.$locale->text('Quarter').qq|
2367	<input name=interval class=radio type=radio value=12 $checked{12}>&nbsp;|.$locale->text('Year').qq|
2368	</td>
2369      </tr>
2370|;
2371  }
2372
2373  if ($form->{ARAP} eq 'AR') {
2374    @options = ( { option => all, description => $locale->text('All')},
2375                 { option => void, description => $locale->text('Void Receipt') },
2376                 { option => exclude, description => $locale->text('Exclude Void Receipts') },
2377                 { option => printed, description => $locale->text('Printed Receipts') },
2378                 { option => notprinted, description => $locale->text('Unprinted Receipts') }
2379               );
2380  } else {
2381    @options = ( { option => all, description => $locale->text('All')},
2382                 { option => void, description => $locale->text('Void Checks') },
2383                 { option => exclude, description => $locale->text('Exclude Void Checks') },
2384                 { option => printed, description => $locale->text('Printed Checks') },
2385                 { option => notprinted, description => $locale->text('Unprinted Checks') }
2386               );
2387  }
2388
2389  for (@options) { $selectoption .= "$_->{description}--$_->{option}\n" }
2390
2391  $display = qq|
2392              <tr>
2393	        <th align=right nowrap>|.$locale->text('Display').qq|</th>
2394	        <td><select name=option>|
2395		.$form->select_option($selectoption, undef, 1)
2396		.qq|</select>
2397	        </td>
2398	      </tr>
2399|;
2400
2401  $form->header;
2402
2403  &calendar;
2404
2405  print qq|
2406<body>
2407
2408<form method=post name=main action=$form->{script}>
2409
2410<table width=100%>
2411  <tr><th class=listtop>$form->{helpref}$form->{title}</a></th></tr>
2412  <tr height="5"></tr>
2413  <tr>
2414    <td>
2415      <table>
2416        <tr valign=top>
2417	  <td>
2418	    <table>
2419	      <tr>
2420		<th align=right>|.$locale->text('Account').qq|</th>
2421		<td colspan=3><select name=accno>|
2422		.$form->select_option($form->{"selectaccno"})
2423		.qq|</select></td>
2424	      </tr>
2425	      $vc
2426	      $checknumber
2427	      <tr>
2428		<th align=right nowrap>|.$locale->text('Date Issued').qq|</th>
2429                <td>
2430                  <table>
2431                    <tr>
2432                      <th>|.$locale->text('From').qq|</th>
2433		      <td><input name=datepaidfrom size=11 class=date title="$myconfig{dateformat}" value="$form->{datepaidfrom}" />|.&js_calendar("main", "datepaidfrom").qq|</td>
2434                      <th>|.$locale->text('To').qq|</th>
2435                      <td><input name=datepaidto size=11 class=date title="$myconfig{dateformat}" value="$form->{datepaidto}" />|.&js_calendar("main", "datepaidto").qq|</td>
2436                    </tr>
2437                  </table>
2438                </td>
2439	      </tr>
2440	      $selectfrom
2441              $display
2442	    </table>
2443	  </td>
2444	</tr>
2445      </table>
2446    </td>
2447  </tr>
2448  <tr>
2449    <td><hr size=3 noshade></td>
2450  </tr>
2451</table>
2452
2453<br>
2454<input type=hidden name=action value=continue>
2455<input class=submit type=submit name=action value="|.$locale->text('Continue').qq|">|;
2456
2457  $form->{sort} = "datepaid";
2458  $form->{nextsub} = "list_checks";
2459
2460  $form->hide_form(qw(title sort helpref));
2461  $form->hide_form(qw(ARAP void reissue nextsub path login));
2462
2463  print qq|
2464</form>
2465
2466</body>
2467</html>
2468|;
2469
2470}
2471
2472
2473sub list_checks {
2474
2475  $form->{vc} = ($form->{ARAP} eq 'AP') ? 'vendor' : 'customer';
2476
2477  (undef, $form->{"$form->{vc}_id"}) = split /--/, $form->{$form->{vc}};
2478
2479  $form->sort_order();
2480
2481  CP->payment_register(\%myconfig, \%$form);
2482
2483  $href = "$form->{script}?action=list_checks";
2484  for (qw(ARAP void reissue direction oldsort path login)) { $href .= qq|&$_=$form->{$_}| }
2485  $href .= "&title=".$form->escape($form->{title});
2486  $href .= "&helpref=".$form->escape($form->{helpref});
2487
2488  $form->sort_order();
2489
2490  $callback = "$form->{script}?action=list_checks";
2491  for (qw(ARAP void reissue direction oldsort path login)) { $callback .= qq|&$_=$form->{$_}| }
2492  $callback .= "&title=".$form->escape($form->{title},1);
2493  $callback .= "&helpref=".$form->escape($form->{helpref},1);
2494
2495  if ($form->{accno}) {
2496    $callback .= "&accno=".$form->escape($form->{accno},1);
2497    $href .= "&accno=".$form->escape($form->{accno});
2498    $option = $locale->text('Account')." : $form->{accno}";
2499  }
2500  if ($form->{$form->{vc}}) {
2501    $callback .= "&$form->{vc}=".$form->escape($form->{$form->{vc}},1);
2502    $href .= "&$form->{vc}=".$form->escape($form->{$form->{vc}});
2503    $option .= "\n<br>" if ($option);
2504    $name = ($form->{vc} eq 'customer') ? $locale->text('Customer') : $locale->text('Vendor');
2505    ($vc) = split /--/, $form->{$form->{vc}};
2506    $option .= "$name : $vc";
2507  }
2508  if ($form->{"$form->{vc}number"}) {
2509    $callback .= "&$form->{vc}number=".$form->escape($form->{"$form->{vc}number"},1);
2510    $href .= "&$form->{vc}number=".$form->escape($form->{"$form->{vc}number"});
2511    $option .= "\n<br>" if ($option);
2512    $name = ($form->{vc} eq 'customer') ? $locale->text('Customer Number') : $locale->text('Vendor Number');
2513    $option .= qq|$name : $form->{"$form->{vc}number"}|;
2514  }
2515  if ($form->{checknumberfrom} || $form->{checknumberto}) {
2516    $option .= "\n<br>" if ($option);
2517    $option .= $locale->text('Check Number')." : ";
2518  }
2519  if ($form->{checknumberfrom}) {
2520    $callback .= "&checknumberfrom=".$form->escape($form->{checknumberfrom},1);
2521    $href .= "&checknumberfrom=".$form->escape($form->{checknumberfrom});
2522    $option .= $form->{checknumberfrom};
2523  }
2524  if ($form->{checknumberto}) {
2525    $callback .= "&checknumberto=".$form->escape($form->{checknumberto},1);
2526    $href .= "&checknumberto=".$form->escape($form->{checknumberto});
2527    $option .= " - $form->{checknumberto}";
2528  }
2529  if ($form->{datepaidfrom} || $form->{datepaidto}) {
2530    $option .= "\n<br>" if ($option);
2531    $option .= $locale->text('Date Issued')." : ";
2532  }
2533  if ($form->{datepaidfrom}) {
2534    $callback .= "&datepaidfrom=$form->{datepaidfrom}";
2535    $href .= "&datepaidfrom=$form->{datepaidfrom}";
2536    $option .= $locale->date(\%myconfig, $form->{datepaidfrom}, 1);
2537  }
2538  if ($form->{datepaidto}) {
2539    $callback .= "&datepaidto=$form->{datepaidto}";
2540    $href .= "&datepaidto=$form->{datepaidto}";
2541    $option .= " - ".$locale->date(\%myconfig, $form->{datepaidto}, 1);
2542  }
2543  if ($form->{option}) {
2544    $callback .= "&option=$form->{option}";
2545    $href .= "&option=$form->{option}";
2546    $option .= "\n<br>" if ($option);
2547    ($opt) = split /--/, $form->{option};
2548    $option .= $opt;
2549  }
2550
2551  $name = ucfirst $form->{vc};
2552
2553  @column_index = $form->sort_columns(qw(datepaid source amount name));
2554  unshift @column_index, qw(runningnumber id);
2555
2556  $form->{allbox} = ($form->{allbox}) ? "checked" : "";
2557  $action = ($form->{deselect}) ? "deselect_all" : "select_all";
2558  if ($form->{option} =~ /--void/) {
2559    $column_data{id} = qq|<th>&nbsp;</th>|;
2560  } else {
2561    $column_data{id} = qq|<th class=listheading width=1%><input name="allbox" type=checkbox class=checkbox value="1" $form->{allbox} onChange="CheckAll(); javascript:document.main.submit()"></th>
2562    <input type=hidden name=action value=$action>|;
2563  }
2564  $column_data{runningnumber} = qq|<th class=listheading>&nbsp;</th>|;
2565  $column_data{datepaid} = "<th><a class=listheading href=$href&sort=datepaid>".$locale->text('Date Paid')."</a></th>";
2566  $column_data{name} = "<th><a class=listheading href=$href&sort=name>$name</a></th>";
2567  $column_data{amount} = "<th class=listheading>" . $locale->text('Amount') . "</th>";
2568  $column_data{source} = "<th><a class=listheading href=$href&sort=source>" . $locale->text('Source') . "</a></th>";
2569
2570  $form->{title} = ($form->{title}) ? $form->{title} : $locale->text('Check Register');
2571
2572  $form->header;
2573
2574  &check_all(qw(allbox id_));
2575
2576  print qq|
2577<body>
2578
2579<form method=post name=main action=$form->{script}>
2580
2581<table width=100%>
2582  <tr>
2583    <th class=listtop>$form->{helpref}$form->{title}</a></th>
2584  </tr>
2585  <tr height="5"></tr>
2586    <td>$option</td>
2587  <tr>
2588    <td>
2589      <table width=100%>
2590	<tr class=listheading>
2591|;
2592
2593  for (@column_index) { print "\n$column_data{$_}" }
2594
2595  print qq|
2596	</tr>
2597|;
2598
2599
2600  # add sort and escape callback, this one we use for the add sub
2601  $form->{callback} = $callback .= "&sort=$form->{sort}";
2602
2603  # escape callback for href
2604  $callback = $form->escape($callback);
2605
2606  $colspan = $#column_index + 1;
2607
2608  $void = "(".$locale->text('void').")";
2609
2610  $arap = ($form->{ARAP} eq 'AP') ? 'ap' : 'ar';
2611  $title = $form->escape($locale->text('Payments'),1);
2612
2613  $reportflds = "";
2614  for (qw(l_transdate l_reference l_name l_description l_paid l_source l_memo)) { $reportflds .= '&'."$_=Y" }
2615
2616  for $accno (sort keys %{ $form->{CHK} }) {
2617    $r = 0;
2618
2619    print "<th colspan=$colspan align=left>$form->{$accno}</th>\n";
2620
2621    for $ref (@{ $form->{CHK}{$accno} }) {
2622
2623      $i++;
2624      $r++;
2625
2626      $column_data{runningnumber} = "<td align=right>$r</td>";
2627
2628      $form->{"id_$i"} = "checked" if $form->{"id_$i"};
2629      if ($ref->{source} && $ref->{amount}) {
2630        $column_data{id} = qq|<td><input name="id_$i" type=checkbox class=checkbox value="$ref->{trans_id}" $form->{"id_$i"}></td>|;
2631      } else {
2632        $column_data{id} = qq|<td>&nbsp;</td>|;
2633      }
2634
2635      $column_data{amount} = "<td align=right>".$form->format_amount(\%myconfig, $ref->{amount}, $form->{precision}, "&nbsp;")."</td>";
2636
2637      $column_data{datepaid} = "<td nowrap>$ref->{datepaid}&nbsp;</td>";
2638
2639      $form->{"accno_$i"} = $accno;
2640      $form->{"source_$i"} = $ref->{source};
2641      $vd = ($ref->{void}) ? $void : "";
2642      $column_data{source} = "<td>$ref->{source}&nbsp;$vd</td>";
2643
2644      $source = '"'.$form->escape($ref->{source},1).'"';
2645      $account = $form->escape($form->{$accno},1);
2646      $name = $form->escape($ref->{name},1);
2647      $column_data{source} = "<td><a href=rp.pl?action=list_payments&account=$account&source=$source&db=$arap&vc=$form->{vc}&$form->{vc}=$name$reportflds&path=$form->{path}&login=$form->{login}&title=$title target=_new>$ref->{source}&nbsp;$vd</a></td>";
2648
2649      $column_data{source} .= $form->hide_form("source_$i", "accno_$i");
2650
2651      $column_data{name} = qq|<td><a href=ct.pl?path=$form->{path}&login=$form->{login}&action=edit&id=$ref->{"$form->{vc}_id"}&db=$form->{vc}&callback=$callback>$ref->{name}</a></td>|;
2652
2653      $j++; $j %= 2;
2654
2655      print "
2656          <tr class=listrow$j>
2657  ";
2658
2659      for (@column_index) { print "\n$column_data{$_}" }
2660
2661      print qq|
2662          </tr>
2663|;
2664    }
2665  }
2666
2667  $form->{rowcount} = $i;
2668
2669  print qq|
2670	</tr>
2671      </table>
2672    </td>
2673  </tr>
2674  <tr>
2675    <td><hr size=3 noshade></td>
2676  </tr>
2677</table>
2678
2679<br>
2680|;
2681
2682  if ($myconfig{acs} !~ /Cash--/) {
2683    $button{'Select all'} = { ndx => 1, key => 'S', value => $locale->text('Select all') };
2684    $button{'Deselect all'} = { ndx => 1, key => 'D', value => $locale->text('Deselect all') };
2685    if ($form->{ARAP} eq 'AR') {
2686      $button{'Void Receipts'} = { ndx => 2, key => 'V', value => $locale->text('Void Receipts') };
2687      $button{'Reissue Receipts'} = { ndx => 3, key => 'R', value => $locale->text('Reissue Receipts') };
2688    } else {
2689      $button{'Void Checks'} = { ndx => 2, key => 'V', value => $locale->text('Void Checks') };
2690      $button{'Reissue Checks'} = { ndx => 3, key => 'R', value => $locale->text('Reissue Payments') };
2691    }
2692  }
2693
2694  if ($form->{deselect}) {
2695    delete $button{'Select all'};
2696  } else {
2697    delete $button{'Deselect all'};
2698  }
2699
2700  %button = () if $form->{option} =~ /--void/;
2701
2702  if ($form->{void}) {
2703    delete $button{'Reissue Checks'};
2704    delete $button{'Reissue Receipts'};
2705  } else {
2706    delete $button{'Void Checks'};
2707    delete $button{'Void Receipts'};
2708  }
2709
2710  $form->{helpref} = $form->escape($form->{helpref},1);
2711
2712  $form->hide_form("$form->{vc}", "$form->{vc}_id");
2713  $form->hide_form(qw(ARAP void reissue rowcount helpref callback path login));
2714
2715  $form->print_button(\%button);
2716
2717  if ($form->{menubar}) {
2718    require "$form->{path}/menu.pl";
2719    &menubar;
2720  }
2721
2722  print qq|
2723</form>
2724
2725</body>
2726</html>
2727|;
2728
2729}
2730
2731
2732sub void_receipts { &void_checks }
2733
2734sub void_checks {
2735
2736  $form->{title} = $locale->text('Confirm!');
2737
2738  $source = "";
2739  for (1 .. $form->{rowcount}) {
2740    if ($form->{"id_$_"}) {
2741      $source .= ($source) ? qq|, $form->{"source_$_"}| : $form->{"source_$_"};
2742    }
2743  }
2744
2745  $form->error($locale->text('Nothing selected!')) unless $source;
2746
2747  if ($form->{ARAP} eq 'AR') {
2748    $action = $locale->text('Yes, Void Receipts');
2749    $msg = $locale->text('Are you sure you want to void receipts');
2750  } else {
2751    $action = $locale->text('Yes, Void Checks');
2752    $msg = $locale->text('Are you sure you want to void checks');
2753  }
2754
2755  $form->header;
2756
2757  print qq|
2758<body>
2759
2760<form method=post action=$form->{script}>
2761|;
2762
2763  $form->{action} = "yes__void_payments";
2764  $form->hide_form;
2765
2766  print qq|
2767<h2 class=confirm>$form->{title}</h2>
2768
2769<h4>$msg $source</h4>
2770
2771<input name=action class=submit type=submit value="$action">
2772</form>
2773
2774</body>
2775</html>
2776|;
2777
2778}
2779
2780
2781sub yes__void_checks { &yes__void_payments }
2782sub yes__void_receipts { &yes__void_payments }
2783
2784sub yes__void_payments {
2785
2786  if (CP->void_payments(\%myconfig, \%$form)) {
2787    $form->redirect;
2788  }
2789
2790  $form->error($locale->text('Could not void payments!'));
2791
2792}
2793
2794
2795sub reissue_receipts { &reissue_payments }
2796
2797sub reissue_payments {
2798
2799  $ok = 0;
2800  for (1 .. $form->{rowcount}) { last if $ok = $form->{"id_$_"} }
2801
2802  $form->error($locale->text('Nothing selected!')) unless $ok;
2803
2804  CP->create_selects(\%myconfig, \%$form);
2805
2806  if ($form->{ARAP} eq 'AR') {
2807    $form->{type} = "receipt";
2808    $form->{formname} = "receipt";
2809    $form->{selectformname} = "receipt--".$locale->text('Receipt');
2810    $form->{title} = $locale->text('Reissue Receipts');
2811    $msgchk = $locale->text('Reissue Receipts');
2812    $chknumber = $locale->text('Receipt Number');
2813  } else {
2814    $form->{type} = "check";
2815    $form->{formname} = "check";
2816    $form->{selectformname} = "check--".$locale->text('Check');
2817    $form->{title} = $locale->text('Reissue Checks');
2818    $msgchk = $locale->text('Reissue Checks');
2819    $chknumber = $locale->text('Check Number');
2820  }
2821  $form->{format} ||= $myconfig{outputformat};
2822
2823  $form->{selectprinter} = "";
2824  for (@{ $form->{all_printer} }) { $form->{selectprinter} .= "$_->{printer}\n" }
2825  chomp $form->{selectprinter};
2826  delete $form->{all_printer};
2827
2828  if ($myconfig{printer}) {
2829    $form->{format} ||= "postscript";
2830  } else {
2831    $form->{format} ||= "pdf";
2832  }
2833  $form->{media} ||= $myconfig{printer};
2834
2835  if (@{ $form->{all_language} }) {
2836    $form->{selectlanguage} = "\n";
2837    for (@{ $form->{all_language} }) { $form->{selectlanguage} .= qq|$_->{code}--$_->{description}\n| }
2838  }
2839  delete $form->{all_language};
2840
2841
2842  $source = "";
2843  for (1 .. $form->{rowcount}) {
2844    if ($form->{"id_$_"}) {
2845      $source .= ($source) ? qq|, $form->{"source_$_"}| : $form->{"source_$_"};
2846    }
2847  }
2848
2849  $form->header;
2850
2851  print qq|
2852<body>
2853
2854<form method=post name=main action=$form->{script}>
2855
2856<table width=100%>
2857  <tr>
2858    <th class=listtop>$form->{helpref}$form->{title}</a></th>
2859  </tr>
2860  <tr height="5"></tr>
2861  <tr>
2862    <td>
2863      <table>
2864        <tr>
2865          <th>$msgchk</th><td>$source</td>
2866        </tr>
2867        <tr>
2868          <th>$chknumber</th>
2869          <td><input name="source" value="$form->{source}"></td>
2870        </tr>
2871      </table>
2872    </td>
2873  </tr>
2874  <tr>
2875    <td>
2876|;
2877
2878  require "$form->{path}/arapprn.pl";
2879  $form->{nohold} = 1;
2880  &print_options;
2881
2882  if ($form->{ARAP} eq 'AR') {
2883    $action = $locale->text('Yes, Reissue Receipts');
2884    $form->{action} = "yes__reissue_receipts";
2885  } else {
2886    $action = $locale->text('Yes, Reissue Checks');
2887    $form->{action} = "yes__reissue_checks";
2888  }
2889
2890  for (qw(action admin format formname media selectformname selectlanguage selectprinter)) { delete $form->{$_} }
2891  $form->hide_form;
2892
2893  print qq|
2894    </td>
2895  </tr>
2896  <tr>
2897    <td><hr size=3 noshade></td>
2898  </tr>
2899</table>
2900
2901<input name=action class=submit type=submit value="$action">
2902</form>
2903
2904</body>
2905</html>
2906|;
2907
2908}
2909
2910
2911sub yes__reissue_receipts { &yes__reissue_checks }
2912
2913sub yes__reissue_checks {
2914
2915  for (1 .. $form->{rowcount}) {
2916    if ($form->{"id_$_"}) {
2917      if (CP->reissue_payment(\%myconfig, \%$form, $_)) {
2918        if (exists $form->{longformat}) {
2919          $form->{datepaid} = $locale->date(\%myconfig, $form->{datepaid}, $form->{longformat});
2920        }
2921        &print_form;
2922        $form->{source}++;
2923$form->{file}++;
2924      } else {
2925        $form->error($locale->text('Error processing payment!'));
2926      }
2927    }
2928  }
2929
2930  $form->redirect;
2931
2932}
2933
2934
2935